Add context dependent actions to LibraryDialog. Make folder delete work on non-Maemo.
authorAkos Polster <polster@nolove.pipacs.com>
Thu, 5 Aug 2010 14:34:39 +0000 (16:34 +0200)
committerAkos Polster <polster@nolove.pipacs.com>
Thu, 5 Aug 2010 14:34:39 +0000 (16:34 +0200)
foldersdialog.cpp
librarydialog.cpp
librarydialog.h
widgets/listwindow.cpp
widgets/listwindow.h

index 4f4966a..be44a49 100644 (file)
@@ -22,9 +22,7 @@ FoldersDialog::FoldersDialog(QWidget *parent): ListWindow(parent)
     list->setUniformItemSizes(true);
     addList(list);
     addAction(tr("Add folder"), this, SLOT(onAdd()));
-#ifndef Q_WS_MAEMO_5
-    addAction(tr("Delete folder"), this, SLOT(onDelete()));
-#endif
+    addItemAction(tr("Delete folder"), this, SLOT(onRemove()));
     addAction(tr("Re-scan folders"), this, SLOT(onRefresh()));
 }
 
@@ -34,8 +32,10 @@ void FoldersDialog::onAdd()
 
     // Get folder name
     Settings *settings = Settings::instance();
-    QString last = settings->value("lastfolderadded", QDir::homePath()).toString();
-    QString path = QFileDialog::getExistingDirectory(this, tr("Add Folder"), last);
+    QString last =
+            settings->value("lastfolderadded", QDir::homePath()).toString();
+    QString path =
+            QFileDialog::getExistingDirectory(this, tr("Add Folder"), last);
     if (path == "") {
         return;
     }
@@ -62,6 +62,19 @@ void FoldersDialog::onAdd()
 
 void FoldersDialog::onRemove()
 {
+    Trace t("FoldersDialog::onRemove");
+
+    QModelIndexList selection = list->selectionModel()->selectedIndexes();
+    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();
+    }
 }
 
 void FoldersDialog::onRefresh()
index 7a1cc2b..d11c6a4 100644 (file)
@@ -37,9 +37,9 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
     // Add actions
 
 #ifndef Q_WS_MAEMO_5
-    addAction(tr("Details"), this, SLOT(onDetails()));
-    addAction(tr("Read"), this, SLOT(onRead()));
-    addAction(tr("Delete"), this, SLOT(onRemove()));
+    addItemAction(tr("Details"), this, SLOT(onDetails()));
+    addItemAction(tr("Read"), this, SLOT(onRead()));
+    addItemAction(tr("Delete"), this, SLOT(onRemove()));
 #endif // ! Q_WS_MAEMO_5
 
     addAction(tr("Add book"), this, SLOT(onAdd()));
@@ -53,13 +53,6 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
             SLOT(onBookAdded()));
     connect(list, SIGNAL(activated(const QModelIndex &)),
             this, SLOT(onItemActivated(const QModelIndex &)));
-#ifndef Q_WS_MAEMO_5
-    connect(list->selectionModel(),
-            SIGNAL(selectionChanged(const QItemSelection &,
-                                    const QItemSelection &)),
-            this, SLOT(onItemSelectionChanged()));
-    onItemSelectionChanged();
-#endif // !Q_WS_MAEMO_5
 }
 
 void LibraryDialog::onAdd()
@@ -160,20 +153,6 @@ QString LibraryDialog::createItemText(const Book *book)
     return text;
 }
 
-#ifndef Q_WS_MAEMO_5
-
-void LibraryDialog::onItemSelectionChanged()
-{
-#if 0 // FIXME: API missing from ListWindow
-    bool enable = selected().isValid();
-    readButton->setEnabled(enable);
-    detailsButton->setEnabled(enable);
-    removeButton->setEnabled(enable);
-#endif
-}
-
-#endif // Q_WS_MAEMO_5
-
 void LibraryDialog::onCurrentBookChanged()
 {
     close();
index 60cce81..ed8c66b 100644 (file)
@@ -29,7 +29,6 @@ public slots:
     void onRemove();
     void onDetails();
     void onRead();
-    void onItemSelectionChanged();
 #endif // Q_WS_MAEMO_5
     void onBookAdded();
     void onItemActivated(const QModelIndex &index);
index 6159231..bc3c65f 100644 (file)
@@ -3,7 +3,7 @@
 #include "listwindow.h"
 #include "trace.h"
 
-ListWindow::ListWindow(QWidget *parent): QMainWindow(parent)
+ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
 {
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5StackedWindow, true);
@@ -11,34 +11,50 @@ ListWindow::ListWindow(QWidget *parent): QMainWindow(parent)
 
     QFrame *frame = new QFrame(this);
     setCentralWidget(frame);
-    layout = new QHBoxLayout(frame);
-    frame->setLayout(layout);
+    frameLayout = new QHBoxLayout(frame);
+    frame->setLayout(frameLayout);
 
 #ifndef Q_WS_MAEMO_5
     buttonBox = new QDialogButtonBox(Qt::Vertical, this);
-    layout->addWidget(buttonBox);
+    frameLayout->addWidget(buttonBox);
 #endif
 }
 
-void ListWindow::addList(QListView *list)
+void ListWindow::addList(QListView *listView)
 {
-    layout->insertWidget(0, list);
+    list = listView;
+    frameLayout->insertWidget(0, list);
+    connect(list->selectionModel(),
+      SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
+      this,
+      SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection&)));
 }
 
 void ListWindow::addAction(const QString &title, QObject *receiver,
                            const char *slot, QDialogButtonBox::ButtonRole role)
 {
 #ifndef Q_WS_MAEMO_5
-    QPushButton *button = new QPushButton(title, this);
-    QList<QAction *> actions = button->actions();
-    Trace::trace(QString("ListWindow::addAction: Button has %1 default action(s)").arg(actions.length()));
-    buttonBox->addButton(button, role);
+    QPushButton *button = buttonBox->addButton(title, role);
     connect(button, SIGNAL(clicked()), receiver, slot);
 #else
     Q_UNUSED(role);
     QAction *action = menuBar()->addAction(title);
     connect(action, SIGNAL(triggered()), receiver, slot);
-#endif // Q_WS_MAEMO_5
+#endif // ! Q_WS_MAEMO_5
+}
+
+void ListWindow::addItemAction(const QString &title, QObject *receiver,
+                               const char *slot)
+{
+#ifndef Q_WS_MAEMO_5
+    QPushButton *button =
+            buttonBox->addButton(title, QDialogButtonBox::ActionRole);
+    connect(button, SIGNAL(clicked()), receiver, slot);
+    itemButtons.append(button);
+    activateItemButtons();
+#else
+    // FIXME
+#endif // ! Q_WS_MAEMO_5
 }
 
 #ifdef Q_WS_MAEMO_5
@@ -51,3 +67,28 @@ void ListWindow::closeEvent(QCloseEvent *event)
 }
 
 #endif // Q_WS_MAEMO_5
+
+void ListWindow::onSelectionChanged(const QItemSelection &selected,
+                                    const QItemSelection &deselected)
+{
+    Q_UNUSED(selected);
+    Q_UNUSED(deselected);
+#ifndef Q_WS_MAEMO_5
+    activateItemButtons();
+#endif
+}
+
+#ifndef Q_WS_MAEMO_5
+
+void ListWindow::activateItemButtons()
+{
+    bool enable = false;
+    if (list) {
+        enable = list->selectionModel()->hasSelection();
+    }
+    foreach (QPushButton *button, itemButtons) {
+        button->setEnabled(enable);
+    }
+}
+
+#endif // ! Q_WS_MAEMO_5
index 8f42793..f912df3 100644 (file)
@@ -3,10 +3,14 @@
 
 #include <QMainWindow>
 #include <QDialogButtonBox>
+#include <QList>
 
 class QListView;
 class QString;
 class QHBoxLayout;
+class QPushButton;
+class QModelIndex;
+class QItemSelection;
 
 /** A window with a list and menu actions (Maemo) or buttons (non-Maemo). */
 class ListWindow: public QMainWindow
@@ -15,17 +19,43 @@ class ListWindow: public QMainWindow
 
 public:
     explicit ListWindow(QWidget *parent = 0);
+
+    /** Add a list view to the window. */
     void addList(QListView *list);
+
+    /**
+     * Add an action to the window: either a button, or, on Maemo, a top
+     * level menu item.
+     * Activating the action invokes the slot with no parameters.
+     */
     void addAction(const QString &title, QObject *receiver, const char *slot,
         QDialogButtonBox::ButtonRole role = QDialogButtonBox::ActionRole);
 
+    /**
+     * Add an action to the selected item in the list: either a button which is
+     * enabled when a list item is selected, or, on Maemo, a pop-up menu item
+     * which is displayed when a list item is long-pressed.
+     * Activating the action invokes the slot with no parameters.
+     */
+    void addItemAction(const QString &title, QObject *receiver,
+                       const char *slot);
+
+protected slots:
+    void onSelectionChanged(const QItemSelection &selected,
+                            const QItemSelection &deselected);
+#ifndef Q_WS_MAEMO_5
+    void activateItemButtons();
+#endif
+
 protected:
 #ifdef Q_WS_MAEMO_5
     void closeEvent(QCloseEvent *event);
 #else
     QDialogButtonBox *buttonBox;
+    QList<QPushButton *> itemButtons;
 #endif
-    QHBoxLayout *layout;
+    QHBoxLayout *frameLayout;
+    QListView *list;
 };
 
 #endif // LISTWINDOW_H