Make selection handling work.
authorAkos Polster <polster@nolove.pipacs.com>
Tue, 20 Jul 2010 11:49:31 +0000 (13:49 +0200)
committerAkos Polster <polster@nolove.pipacs.com>
Tue, 20 Jul 2010 11:49:31 +0000 (13:49 +0200)
library.cpp
librarydialog.cpp
librarydialog.h

index a3bc5b5..873b343 100644 (file)
@@ -52,8 +52,6 @@ Book *Library::book(const QModelIndex &index)
 {
     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();
@@ -78,7 +76,6 @@ void Library::load()
         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();
@@ -87,8 +84,6 @@ void Library::load()
 
 void Library::save()
 {
-    qDebug() << "Library::save";
-
     QSettings settings;
     settings.setValue("lib/size", mBooks.size());
     for (int i = 0; i < mBooks.size(); i++) {
@@ -102,13 +97,12 @@ void Library::save()
 
 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();
index ddc6ce4..906d5f4 100644 (file)
@@ -31,7 +31,7 @@ LibraryDialog::LibraryDialog(QWidget *parent):
 
     Library *library = Library::instance();
     QModelIndex current = library->nowReading();
-    list->setCurrentIndex(current);
+    select(current);
 
     QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
     horizontalLayout->addWidget(list);
@@ -55,6 +55,10 @@ LibraryDialog::LibraryDialog(QWidget *parent):
 
     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()));
@@ -94,7 +98,8 @@ void LibraryDialog::onAdd()
     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"),
@@ -103,7 +108,7 @@ void LibraryDialog::onAdd()
         (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);
@@ -112,15 +117,8 @@ void LibraryDialog::onAdd()
 
 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
@@ -208,3 +206,14 @@ void LibraryDialog::onCurrentBookChanged()
 {
     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);
+    }
+}
index 95d8579..cc2fec7 100644 (file)
@@ -19,14 +19,6 @@ class LibraryDialog: public QDialog
 
 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();
@@ -42,6 +34,15 @@ public slots:
 
 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