Show cover images in library.
[dorian] / librarydialog.cpp
1 #include <QtGui>
2 #include <QDebug>
3 #include <QFileInfo>
4 #include <QDir>
5 #include <QModelIndex>
6
7 #ifdef Q_WS_MAEMO_5
8 #include <QtMaemo5/QMaemo5InformationBox>
9 #endif
10
11 #include "librarydialog.h"
12 #include "library.h"
13 #include "sortedlibrary.h"
14 #include "book.h"
15 #include "infodialog.h"
16 #include "settings.h"
17
18 LibraryDialog::LibraryDialog(QWidget *parent): QMainWindow(parent)
19 {
20 #ifdef Q_WS_MAEMO_5
21     setAttribute(Qt::WA_Maemo5StackedWindow, true);
22 #endif
23     setWindowTitle(tr("Library"));
24
25     QFrame *frame = new QFrame(this);
26     setCentralWidget(frame);
27     QHBoxLayout *horizontalLayout = new QHBoxLayout(frame);
28     frame->setLayout(horizontalLayout);
29
30     list = new QListView(this);
31     sortedLibrary = new SortedLibrary(this);
32     list->setModel(sortedLibrary);
33     list->setSelectionMode(QAbstractItemView::SingleSelection);
34     list->setSpacing(1);
35     list->setUniformItemSizes(true);
36
37     Library *library = Library::instance();
38     QModelIndex current = library->nowReading();
39     setSelected(current);
40     horizontalLayout->addWidget(list);
41
42 #ifndef Q_WS_MAEMO_5
43     QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
44     detailsButton = new QPushButton(tr("Details"), this);
45     readButton = new QPushButton(tr("Read"), this);
46     removeButton = new QPushButton(tr("Delete"), this);
47     addButton = new QPushButton(tr("Add"), this);
48
49     buttonBox->addButton(detailsButton, QDialogButtonBox::ActionRole);
50     buttonBox->addButton(readButton, QDialogButtonBox::AcceptRole);
51     buttonBox->addButton(removeButton, QDialogButtonBox::ActionRole);
52     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
53     horizontalLayout->addWidget(buttonBox);
54 #else
55     QAction *addBookAction = menuBar()->addAction(tr("Add book"));
56 #endif // Q_WS_MAEMO_5
57
58     connect(Library::instance(), SIGNAL(nowReadingChanged()),
59             this, SLOT(onCurrentBookChanged()));
60     connect(Library::instance(),
61             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
62             this,
63             SLOT(onBookAdded()));
64     connect(list, SIGNAL(activated(const QModelIndex &)),
65             this, SLOT(onItemActivated(const QModelIndex &)));
66 #ifndef Q_WS_MAEMO_5
67     connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
68     connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
69     connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
70     connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
71     connect(list->selectionModel(),
72             SIGNAL(selectionChanged(const QItemSelection &,
73                                     const QItemSelection &)),
74             this, SLOT(onItemSelectionChanged()));
75     onItemSelectionChanged();
76 #else
77     connect(addBookAction, SIGNAL(triggered()), this, SLOT(onAdd()));
78 #endif // !Q_WS_MAEMO_5
79 }
80
81 void LibraryDialog::onAdd()
82 {
83     Library *library = Library::instance();
84
85     // Figure out directory to show
86     QString lastDir = Settings::instance()->value("lastdir").toString();
87     if (lastDir == "") {
88         lastDir = QDir::homePath();
89     }
90
91     // Get book file name
92     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
93                                                 lastDir, "Books (*.epub)");
94     if (path == "") {
95         return;
96     }
97
98     // Save directory selected
99     Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
100
101     // Add book to library
102     QModelIndex index = library->find(path);
103     if (index.isValid()) {
104 #ifdef Q_WS_MAEMO_5
105         QMaemo5InformationBox::information(this,
106             tr("This book is already in the library"),
107             QMaemo5InformationBox::DefaultTimeout);
108 #else
109         (void)QMessageBox::information(this, tr("Dorian"),
110             tr("This book is already in the library"), QMessageBox::Ok);
111 #endif // Q_WS_MAEMO_5
112         setSelected(index);
113     }
114     else {
115         library->add(path);
116     }
117 }
118
119 void LibraryDialog::onBookAdded()
120 {
121     Library *library = Library::instance();
122     setSelected(library->index(library->rowCount() - 1));
123 }
124
125 #ifndef Q_WS_MAEMO_5
126
127 void LibraryDialog::onRemove()
128 {
129     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
130     if (current.isValid()) {
131         Book *currentBook = Library::instance()->book(current);
132         QString title = currentBook->name();
133         if (QMessageBox::Yes ==
134             QMessageBox::question(this, "Delete book",
135                                   "Delete book \"" + title + "\"?",
136                                   QMessageBox::Yes | QMessageBox::No)) {
137             Library::instance()->remove(current);
138         }
139     }
140 }
141
142 void LibraryDialog::onRead()
143 {
144     qDebug() << "LibraryDialog::onRead";
145     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
146     if (current.isValid()) {
147         Library::instance()->setNowReading(current);
148     }
149 }
150
151 void LibraryDialog::onDetails()
152 {
153     onItemActivated(list->currentIndex());
154 }
155
156 #endif // Q_WS_MAEMO_5
157
158 void LibraryDialog::onItemActivated(const QModelIndex &index)
159 {
160     qDebug() << "LibraryDialog::onItemActivated";
161     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
162     Book *book = Library::instance()->book(libraryIndex);
163     (new InfoDialog(book, this))->exec();
164 }
165
166 QString LibraryDialog::createItemText(const Book *book)
167 {
168     QString text = book->title + "\n";
169     if (book->creators.size()) {
170         text += book->creators[0];
171         for (int i = 1; i < book->creators.size(); i++) {
172             text += ", " + book->creators[i];
173         }
174     }
175     return text;
176 }
177
178 #ifndef Q_WS_MAEMO_5
179
180 void LibraryDialog::onItemSelectionChanged()
181 {
182     bool enable = selected().isValid();
183     readButton->setEnabled(enable);
184     detailsButton->setEnabled(enable);
185     removeButton->setEnabled(enable);
186 }
187
188 #endif // Q_WS_MAEMO_5
189
190 void LibraryDialog::onCurrentBookChanged()
191 {
192     close();
193 }
194
195 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
196 {
197     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
198     list->selectionModel()->clearSelection();
199     if (sortedIndex.isValid()) {
200         list->selectionModel()->select(sortedIndex,
201                                        QItemSelectionModel::Select);
202         list->setCurrentIndex(sortedIndex);
203     }
204 }
205
206 QModelIndex LibraryDialog::selected() const
207 {
208     QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
209     if (selectedItems.size()) {
210         return sortedLibrary->mapToSource(selectedItems[0]);
211     }
212     return QModelIndex();
213 }
214
215 void LibraryDialog::closeEvent(QCloseEvent *event)
216 {
217 #ifdef Q_WS_MAEMO_5
218     menuBar()->clear();
219 #endif
220     event->accept();
221 }