Introduce ListWidget.
authorAkos Polster <polster@nolove.pipacs.com>
Wed, 4 Aug 2010 14:15:02 +0000 (16:15 +0200)
committerAkos Polster <polster@nolove.pipacs.com>
Wed, 4 Aug 2010 14:15:02 +0000 (16:15 +0200)
chaptersdialog.cpp
chaptersdialog.h
dorian.pro
librarydialog.cpp
librarydialog.h
widgets/listwindow.cpp [new file with mode: 0644]
widgets/listwindow.h [new file with mode: 0644]

index c74983d..9ff0ab8 100644 (file)
@@ -3,34 +3,24 @@
 #include "chaptersdialog.h"
 #include "book.h"
 
-ChaptersDialog::ChaptersDialog(Book *b, QWidget *parent):
-    QMainWindow(parent), book(b)
+ChaptersDialog::ChaptersDialog(Book *book, QWidget *parent): ListWindow(parent)
 {
-#ifdef Q_WS_MAEMO_5
-    setAttribute(Qt::WA_Maemo5StackedWindow, true);
-#endif
-    setWindowTitle(tr("Bookmarks"));
-
-    QFrame *frame = new QFrame(this);
-    setCentralWidget(frame);
-    QHBoxLayout *horizontalLayout = new QHBoxLayout(frame);
-    frame->setLayout(horizontalLayout);
+    setWindowTitle(tr("Chapters"));
 
     list = new QListWidget(this);
     list->setSelectionMode(QAbstractItemView::SingleSelection);
-    foreach (QString id, book->toc) {
-        QString contentTitle = book->content[id].name;
-        (void)new QListWidgetItem(contentTitle, list);
+    if (book) {
+        foreach (QString id, book->toc) {
+            QString contentTitle = book->content[id].name;
+            (void)new QListWidgetItem(contentTitle, list);
+        }
     }
-    horizontalLayout->addWidget(list);
+    addList(list);
     connect(list, SIGNAL(itemActivated(QListWidgetItem*)),
             this, SLOT(onItemActivated(QListWidgetItem*)));
 
 #ifndef Q_WS_MAEMO_5
-    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
-    QPushButton *closeButton = buttonBox->addButton(QDialogButtonBox::Close);
-    connect(closeButton, SIGNAL(clicked()), this, SLOT(onClose()));
-    horizontalLayout->addWidget(buttonBox);
+    addAction(tr("Close"), this, SLOT(close()), QDialogButtonBox::RejectRole);
 #endif // Q_WS_MAEMO_5
 }
 
@@ -39,8 +29,3 @@ void ChaptersDialog::onItemActivated(QListWidgetItem *item)
     emit goToChapter(list->row(item));
     close();
 }
-
-void ChaptersDialog::onClose()
-{
-    close();
-}
index 018470f..03d18ff 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef CHAPTERSDIALOG_H
 #define CHAPTERSDIALOG_H
 
-#include <QMainWindow>
+#include "listwindow.h"
 
 class QWidget;
 class QListWidget;
@@ -9,7 +9,7 @@ class QListWidgetItem;
 class Book;
 
 /** Display book chapters. */
-class ChaptersDialog: public QMainWindow
+class ChaptersDialog: public ListWindow
 {
     Q_OBJECT
 
@@ -21,10 +21,8 @@ signals:
 
 public slots:
     void onItemActivated(QListWidgetItem *);
-    void onClose();
 
 protected:
-    Book *book;
     QListWidget *list;
 };
 
index d160ac9..a6982aa 100644 (file)
@@ -27,7 +27,8 @@ SOURCES += \
     widgets/fullscreenwindow.cpp \
     trace.cpp \
     widgets/toolbuttonbox.cpp \
-    model/bookfinder.cpp
+    model/bookfinder.cpp \
+    widgets/listwindow.cpp
 
 HEADERS += \
     mainwindow.h \
@@ -55,7 +56,8 @@ HEADERS += \
     widgets/fullscreenwindow.h \
     trace.h \
     widgets/toolbuttonbox.h \
-    model/bookfinder.h
+    model/bookfinder.h \
+    widgets/listwindow.h
 
 RESOURCES += \
     dorian.qrc
index 172a1ab..32e7ad3 100644 (file)
 #include "book.h"
 #include "infodialog.h"
 #include "settings.h"
+#include "listwindow.h"
 
-LibraryDialog::LibraryDialog(QWidget *parent): QMainWindow(parent)
+LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
 {
-#ifdef Q_WS_MAEMO_5
-    setAttribute(Qt::WA_Maemo5StackedWindow, true);
-#endif
     setWindowTitle(tr("Library"));
 
-    QFrame *frame = new QFrame(this);
-    setCentralWidget(frame);
-    QHBoxLayout *horizontalLayout = new QHBoxLayout(frame);
-    frame->setLayout(horizontalLayout);
+    // Create and add list view
 
     list = new QListView(this);
     sortedLibrary = new SortedLibrary(this);
@@ -33,27 +28,21 @@ LibraryDialog::LibraryDialog(QWidget *parent): QMainWindow(parent)
     list->setSelectionMode(QAbstractItemView::SingleSelection);
     list->setSpacing(1);
     list->setUniformItemSizes(true);
-
     Library *library = Library::instance();
     QModelIndex current = library->nowReading();
     setSelected(current);
-    horizontalLayout->addWidget(list);
+    addList(list);
+
+    // Add actions
 
 #ifndef Q_WS_MAEMO_5
-    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
-    detailsButton = new QPushButton(tr("Details"), this);
-    readButton = new QPushButton(tr("Read"), this);
-    removeButton = new QPushButton(tr("Delete"), this);
-    addButton = new QPushButton(tr("Add"), this);
-
-    buttonBox->addButton(detailsButton, QDialogButtonBox::ActionRole);
-    buttonBox->addButton(readButton, QDialogButtonBox::AcceptRole);
-    buttonBox->addButton(removeButton, QDialogButtonBox::ActionRole);
-    buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
-    horizontalLayout->addWidget(buttonBox);
-#else
-    QAction *addBookAction = menuBar()->addAction(tr("Add book"));
-#endif // Q_WS_MAEMO_5
+    addAction(tr("Details"), this, SLOT(onDetails()));
+    addAction(tr("Read"), this, SLOT(onRead()));
+    addAction(tr("Delete"), this, SLOT(onRemove()));
+#endif // ! Q_WS_MAEMO_5
+
+    addAction(tr("Add"), this, SLOT(onAdd()));
+    addAction(tr("Folders"), this, SLOT(onShowFolders()));
 
     connect(Library::instance(), SIGNAL(nowReadingChanged()),
             this, SLOT(onCurrentBookChanged()));
@@ -64,17 +53,11 @@ LibraryDialog::LibraryDialog(QWidget *parent): QMainWindow(parent)
     connect(list, SIGNAL(activated(const QModelIndex &)),
             this, SLOT(onItemActivated(const QModelIndex &)));
 #ifndef Q_WS_MAEMO_5
-    connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
-    connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
-    connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
-    connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
     connect(list->selectionModel(),
             SIGNAL(selectionChanged(const QItemSelection &,
                                     const QItemSelection &)),
             this, SLOT(onItemSelectionChanged()));
     onItemSelectionChanged();
-#else
-    connect(addBookAction, SIGNAL(triggered()), this, SLOT(onAdd()));
 #endif // !Q_WS_MAEMO_5
 }
 
@@ -180,10 +163,12 @@ QString LibraryDialog::createItemText(const Book *book)
 
 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
@@ -213,10 +198,6 @@ QModelIndex LibraryDialog::selected() const
     return QModelIndex();
 }
 
-void LibraryDialog::closeEvent(QCloseEvent *event)
+void LibraryDialog::onShowFolders()
 {
-#ifdef Q_WS_MAEMO_5
-    menuBar()->clear();
-#endif
-    event->accept();
 }
index bef71fc..60cce81 100644 (file)
@@ -4,7 +4,8 @@
 #include <QDialog>
 #include <QString>
 #include <QModelIndexList>
-#include <QMainWindow>
+
+#include "listwindow.h"
 
 class QListView;
 class QPushButton;
@@ -14,7 +15,7 @@ class Book;
 class InfoWindow;
 class SortedLibrary;
 
-class LibraryDialog: public QMainWindow
+class LibraryDialog: public ListWindow
 {
     Q_OBJECT
 
@@ -23,6 +24,7 @@ public:
 
 public slots:
     void onAdd();
+    void onShowFolders();
 #ifndef Q_WS_MAEMO_5
     void onRemove();
     void onDetails();
@@ -33,21 +35,12 @@ public slots:
     void onItemActivated(const QModelIndex &index);
     void onCurrentBookChanged();
 
-protected:
-    void closeEvent(QCloseEvent *event);
-
 private:
     QString createItemText(const Book *book);
     void setSelected(const QModelIndex &index);
     QModelIndex selected() const;
     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
diff --git a/widgets/listwindow.cpp b/widgets/listwindow.cpp
new file mode 100644 (file)
index 0000000..6159231
--- /dev/null
@@ -0,0 +1,53 @@
+#include <QtGui>
+
+#include "listwindow.h"
+#include "trace.h"
+
+ListWindow::ListWindow(QWidget *parent): QMainWindow(parent)
+{
+#ifdef Q_WS_MAEMO_5
+    setAttribute(Qt::WA_Maemo5StackedWindow, true);
+#endif
+
+    QFrame *frame = new QFrame(this);
+    setCentralWidget(frame);
+    layout = new QHBoxLayout(frame);
+    frame->setLayout(layout);
+
+#ifndef Q_WS_MAEMO_5
+    buttonBox = new QDialogButtonBox(Qt::Vertical, this);
+    layout->addWidget(buttonBox);
+#endif
+}
+
+void ListWindow::addList(QListView *list)
+{
+    layout->insertWidget(0, list);
+}
+
+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);
+    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
+}
+
+#ifdef Q_WS_MAEMO_5
+
+void ListWindow::closeEvent(QCloseEvent *event)
+{
+    // Work around Maemo/Qt but: Menu items are not removed on close
+    menuBar()->clear();
+    event->accept();
+}
+
+#endif // Q_WS_MAEMO_5
diff --git a/widgets/listwindow.h b/widgets/listwindow.h
new file mode 100644 (file)
index 0000000..8f42793
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef LISTWINDOW_H
+#define LISTWINDOW_H
+
+#include <QMainWindow>
+#include <QDialogButtonBox>
+
+class QListView;
+class QString;
+class QHBoxLayout;
+
+/** A window with a list and menu actions (Maemo) or buttons (non-Maemo). */
+class ListWindow: public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    explicit ListWindow(QWidget *parent = 0);
+    void addList(QListView *list);
+    void addAction(const QString &title, QObject *receiver, const char *slot,
+        QDialogButtonBox::ButtonRole role = QDialogButtonBox::ActionRole);
+
+protected:
+#ifdef Q_WS_MAEMO_5
+    void closeEvent(QCloseEvent *event);
+#else
+    QDialogButtonBox *buttonBox;
+#endif
+    QHBoxLayout *layout;
+};
+
+#endif // LISTWINDOW_H