Add feature to remove folder from library. Experiment with context menus on Maemo.
authorAkos Polster <polster@marzipan.pipacs.com>
Thu, 5 Aug 2010 21:28:00 +0000 (23:28 +0200)
committerAkos Polster <polster@marzipan.pipacs.com>
Thu, 5 Aug 2010 21:28:00 +0000 (23:28 +0200)
foldersdialog.cpp
foldersdialog.h
widgets/listwindow.cpp
widgets/listwindow.h

index be44a49..3582471 100644 (file)
@@ -22,8 +22,9 @@ FoldersDialog::FoldersDialog(QWidget *parent): ListWindow(parent)
     list->setUniformItemSizes(true);
     addList(list);
     addAction(tr("Add folder"), this, SLOT(onAdd()));
-    addItemAction(tr("Delete folder"), this, SLOT(onRemove()));
-    addAction(tr("Re-scan folders"), this, SLOT(onRefresh()));
+    addItemAction(tr("Re-scan"), this, SLOT(onRefresh()));
+    addItemAction(tr("Remove"), this, SLOT(onRemove()));
+    addAction(tr("Re-scan all folders"), this, SLOT(onRefreshAll()));
 }
 
 void FoldersDialog::onAdd()
@@ -51,8 +52,7 @@ void FoldersDialog::onAdd()
     } else {
 #ifdef Q_WS_MAEMO_5
         QMaemo5InformationBox::information(this,
-            tr("This folder is already in the library"),
-            QMaemo5InformationBox::DefaultTimeout);
+            tr("This folder is already in the library"));
 #else
         (void)QMessageBox::information(this, tr("Dorian"),
             tr("This folder is already in the library"), QMessageBox::Ok);
@@ -68,21 +68,36 @@ void FoldersDialog::onRemove()
     if (selection.size() != 1) {
         return;
     }
+
     QModelIndex selected = selection[0];
     QString path = list->model()->data(selected).toString();
     t.trace(path);
-    if (Library::instance()->removeFolder(path)) {
-        model->removeRow(selected.row());
-        onRefresh();
+
+    if (QMessageBox::Yes ==
+        QMessageBox::question(this, tr("Remove folder"),
+            tr("Remove folder \"%1\" from library?").arg(path),
+            QMessageBox::Yes | QMessageBox::No)) {
+        if (Library::instance()->removeFolder(path)) {
+            model->removeRow(selected.row());
+        }
     }
 }
 
 void FoldersDialog::onRefresh()
 {
+    Trace t("FoldersDialog::onRefresh");
+
+    QModelIndexList selection = list->selectionModel()->selectedIndexes();
+    if (selection.size() != 1) {
+        return;
+    }
+
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
 #endif
-
+    QModelIndex selected = selection[0];
+    QString path = list->model()->data(selected).toString();
+    t.trace(path);
     BookFinder *bookFinder = new BookFinder(this);
     Library *library = Library::instance();
     connect(bookFinder, SIGNAL(add(const QString &)),
@@ -91,14 +106,27 @@ void FoldersDialog::onRefresh()
             library, SLOT(remove(const QString &)));
     connect(bookFinder, SIGNAL(done(int,int)),
             this, SLOT(onRefreshDone(int, int)));
-    // bookFinder->moveToThread(&bookFinderThread);
-    // bookFinderThread.start();
+    bookFinder->find(QStringList(path), library->bookPaths());
+#ifdef Q_WS_MAEMO_5
+    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
+#endif
+}
+
+void FoldersDialog::onRefreshAll()
+{
+#ifdef Q_WS_MAEMO_5
+    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
+#endif
 
-    (void)QMetaObject::invokeMethod(
-        bookFinder,
-        "find",
-        Q_ARG(QStringList, model->stringList()),
-        Q_ARG(QStringList, library->bookPaths()));
+    BookFinder *bookFinder = new BookFinder(this);
+    Library *library = Library::instance();
+    connect(bookFinder, SIGNAL(add(const QString &)),
+            library, SLOT(add(const QString &)));
+    connect(bookFinder, SIGNAL(remove(const QString &)),
+            library, SLOT(remove(const QString &)));
+    connect(bookFinder, SIGNAL(done(int,int)),
+            this, SLOT(onRefreshDone(int, int)));
+    bookFinder->find(model->stringList(), library->bookPaths());
 
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
@@ -122,12 +150,11 @@ void FoldersDialog::onRefreshDone(int added, int removed)
     default: removedMsg = tr("%1 books removed").arg(removed);
     }
 
-    QString msg(tr("Scanning folders complete\n\n%1, %2.").
+    QString msg(tr("Scanning complete\n\n%1, %2.").
                 arg(addedMsg).arg(removedMsg));
     Trace::trace(QString("FoldersDialog::onRefreshDone: " + msg));
 #ifdef Q_WS_MAEMO_5
-    QMaemo5InformationBox::
-            information(this, msg, QMaemo5InformationBox::NoTimeout);
+    QMaemo5InformationBox::information(this, msg);
 #else
     // FIXME
 #endif
index 8acf672..45adba1 100644 (file)
@@ -20,6 +20,7 @@ public slots:
     void onAdd();
     void onRemove();
     void onRefresh();
+    void onRefreshAll();
     void onRefreshDone(int added, int removed);
 
 protected:
index bc3c65f..55ce10f 100644 (file)
@@ -11,10 +11,12 @@ ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
 
     QFrame *frame = new QFrame(this);
     setCentralWidget(frame);
-    frameLayout = new QHBoxLayout(frame);
+    frameLayout = new QHBoxLayout();
     frame->setLayout(frameLayout);
 
-#ifndef Q_WS_MAEMO_5
+#ifdef Q_WS_MAEMO_5
+    popup = new QMenu(this);
+#else
     buttonBox = new QDialogButtonBox(Qt::Vertical, this);
     frameLayout->addWidget(buttonBox);
 #endif
@@ -23,11 +25,14 @@ ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
 void ListWindow::addList(QListView *listView)
 {
     list = listView;
+#ifdef Q_WS_MAEMO_5
+    list->installEventFilter(this);
+#endif
     frameLayout->insertWidget(0, list);
     connect(list->selectionModel(),
       SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
       this,
-      SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection&)));
+      SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
 }
 
 void ListWindow::addAction(const QString &title, QObject *receiver,
@@ -53,7 +58,7 @@ void ListWindow::addItemAction(const QString &title, QObject *receiver,
     itemButtons.append(button);
     activateItemButtons();
 #else
-    // FIXME
+    popup->addAction(title, receiver, slot);
 #endif // ! Q_WS_MAEMO_5
 }
 
@@ -92,3 +97,27 @@ void ListWindow::activateItemButtons()
 }
 
 #endif // ! Q_WS_MAEMO_5
+
+#ifdef Q_WS_MAEMO_5
+
+bool ListWindow::eventFilter(QObject *obj, QEvent *event)
+{
+    if (event->type() == QEvent::ContextMenu) {
+        Trace::trace("ListWindow::eventFiler: Received QEvent::ContextMenu");
+        if (popup->actions().size()) {
+            QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
+            QPoint pos = mouseEvent->globalPos();
+            pos.setX(pos.x() - 150);
+            if (pos.x() < 0) {
+                pos.setX(0);
+            }
+            popup->exec(pos);
+        }
+        return true;
+    } else {
+        Trace::trace("ListWindow::eventFilter");
+        return QObject::eventFilter(obj, event);
+    }
+}
+
+#endif // Q_WS_MAEMO_5
index f912df3..7b7d585 100644 (file)
@@ -11,6 +11,7 @@ class QHBoxLayout;
 class QPushButton;
 class QModelIndex;
 class QItemSelection;
+class QEvent;
 
 /** A window with a list and menu actions (Maemo) or buttons (non-Maemo). */
 class ListWindow: public QMainWindow
@@ -49,7 +50,9 @@ protected slots:
 
 protected:
 #ifdef Q_WS_MAEMO_5
+    bool eventFilter(QObject *obj, QEvent *event);
     void closeEvent(QCloseEvent *event);
+    QMenu *popup;
 #else
     QDialogButtonBox *buttonBox;
     QList<QPushButton *> itemButtons;