#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
}
emit goToChapter(list->row(item));
close();
}
-
-void ChaptersDialog::onClose()
-{
- close();
-}
#ifndef CHAPTERSDIALOG_H
#define CHAPTERSDIALOG_H
-#include <QMainWindow>
+#include "listwindow.h"
class QWidget;
class QListWidget;
class Book;
/** Display book chapters. */
-class ChaptersDialog: public QMainWindow
+class ChaptersDialog: public ListWindow
{
Q_OBJECT
public slots:
void onItemActivated(QListWidgetItem *);
- void onClose();
protected:
- Book *book;
QListWidget *list;
};
widgets/fullscreenwindow.cpp \
trace.cpp \
widgets/toolbuttonbox.cpp \
- model/bookfinder.cpp
+ model/bookfinder.cpp \
+ widgets/listwindow.cpp
HEADERS += \
mainwindow.h \
widgets/fullscreenwindow.h \
trace.h \
widgets/toolbuttonbox.h \
- model/bookfinder.h
+ model/bookfinder.h \
+ widgets/listwindow.h
RESOURCES += \
dorian.qrc
#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);
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()));
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
}
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
return QModelIndex();
}
-void LibraryDialog::closeEvent(QCloseEvent *event)
+void LibraryDialog::onShowFolders()
{
-#ifdef Q_WS_MAEMO_5
- menuBar()->clear();
-#endif
- event->accept();
}
#include <QDialog>
#include <QString>
#include <QModelIndexList>
-#include <QMainWindow>
+
+#include "listwindow.h"
class QListView;
class QPushButton;
class InfoWindow;
class SortedLibrary;
-class LibraryDialog: public QMainWindow
+class LibraryDialog: public ListWindow
{
Q_OBJECT
public slots:
void onAdd();
+ void onShowFolders();
#ifndef Q_WS_MAEMO_5
void onRemove();
void onDetails();
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
--- /dev/null
+#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
--- /dev/null
+#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