{
if (index.isValid()) {
if ((index.row() >= 0) && (index.row() < mBooks.size())) {
- qDebug() << "Library::book:" << index.row() << "is"
- << mBooks[index.row()]->name();
return mBooks[index.row()];
} else {
qWarning() << "*** Library::book: Bad index" << index.row();
QString path = settings.value(key).toString();
Book *book = new Book(path);
book->load();
- qDebug() << "Library::load: Add" << book->title << "from" << path;
mBooks.append(book);
}
QString currentPath = settings.value("lib/nowreading").toString();
void Library::save()
{
- qDebug() << "Library::save";
-
QSettings settings;
settings.setValue("lib/size", mBooks.size());
for (int i = 0; i < mBooks.size(); i++) {
bool Library::add(QString path)
{
- qDebug() << "Library::add" << path;
if (path == "") {
- qWarning() << "Library::add: Empty path";
+ qWarning() << "*** Library::add: Empty path";
return false;
}
if (find(path).isValid()) {
- qDebug() << " Book already exists in library";
+ qDebug() << "Library::add: Book already exists in library";
return false;
}
int size = mBooks.size();
Library *library = Library::instance();
QModelIndex current = library->nowReading();
- list->setCurrentIndex(current);
+ select(current);
QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
horizontalLayout->addWidget(list);
connect(Library::instance(), SIGNAL(nowReadingChanged()),
this, SLOT(onCurrentBookChanged()));
+ connect(Library::instance(),
+ SIGNAL(rowsInserted(const QModelIndex &, int, int)),
+ this,
+ SLOT(onBookAdded()));
#ifndef Q_WS_MAEMO_5
connect(list, SIGNAL(itemSelectionChanged()),
this, SLOT(onItemSelectionChanged()));
Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
// Add book to library
- if (library->find(path).isValid()) {
+ QModelIndex index = library->find(path);
+ if (index.isValid()) {
#ifdef Q_WS_MAEMO_5
QMaemo5InformationBox::information(this,
tr("This book is already in the library"),
(void)QMessageBox::information(this, tr("Dorian"),
tr("This book is already in the library"), QMessageBox::Ok);
#endif // Q_WS_MAEMO_5
- // FIXME: Select existing book
+ select(index);
}
else {
library->add(path);
void LibraryDialog::onBookAdded()
{
-#if 0
Library *library = Library::instance();
- int index = library->size() - 1;
- Book *book = library->at(index);
- QListWidgetItem *item = new QListWidgetItem(book->cover,
- createItemText(book));
- list->addItem(item);
- list->setCurrentItem(item);
-#endif
+ select(library->index(library->rowCount() - 1));
}
#ifndef Q_WS_MAEMO_5
{
close();
}
+
+void LibraryDialog::select(const QModelIndex &libraryIndex)
+{
+ QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
+ list->selectionModel()->clearSelection();
+ if (sortedIndex.isValid()) {
+ list->selectionModel()->select(sortedIndex,
+ QItemSelectionModel::Select);
+ list->setCurrentIndex(sortedIndex);
+ }
+}
public:
explicit LibraryDialog(QWidget *parent = 0);
- QListView *list;
- SortedLibrary *sortedLibrary;
-#ifndef Q_WS_MAEMO_5
- QPushButton *detailsButton;
- QPushButton *removeButton;
- QPushButton *readButton;
-#endif // Q_WS_MAEMO_5
- QPushButton *addButton;
public slots:
void onAdd();
private:
QString createItemText(const Book *book);
+ void select(const QModelIndex &index);
+ QListView *list;
+ SortedLibrary *sortedLibrary;
+#ifndef Q_WS_MAEMO_5
+ QPushButton *detailsButton;
+ QPushButton *removeButton;
+ QPushButton *readButton;
+#endif // Q_WS_MAEMO_5
+ QPushButton *addButton;
};
#endif // LIBRARYDIALOG_H