setWindowTitle(tr("Library"));
setAttribute(Qt::WA_DeleteOnClose, true);
- // Add actions
-
+ // Add menu actions
sortByTitle = addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle()));
sortByAuthor =
addMenuAction(tr("Sort by author"), this, SLOT(onSortByAuthor()));
- addAction(tr("Add book"), this, SLOT(onAdd()), "add");
- addAction(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
- addAction(tr("Search the Web"), this, SLOT(onSearch()), "search");
-
- // Create and add list view
- list = new ListView(this);
+ // Set model
sortedLibrary = new SortedLibrary(this);
- list->setModel(sortedLibrary);
- list->setSelectionMode(QAbstractItemView::SingleSelection);
- list->setSpacing(1);
+ setModel(sortedLibrary);
+
+ // Add action buttons
+ addButton(tr("Add book"), this, SLOT(onAdd()), "add");
+ addButton(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
+ addButton(tr("Search the Web"), this, SLOT(onSearch()), "search");
+
+ // Set selected item
Library *library = Library::instance();
QModelIndex current = library->nowReading();
- setSelected(sortedLibrary->mapFromSource(current));
- addList(list);
+ // FIXME: setSelected(sortedLibrary->mapFromSource(current));
progress = new ProgressDialog(tr("Adding books"), this);
SIGNAL(rowsInserted(const QModelIndex &, int, int)),
this,
SLOT(onBookAdded()));
- connect(list, SIGNAL(activated(const QModelIndex &)),
+ connect(this, SIGNAL(activated(const QModelIndex &)),
this, SLOT(onItemActivated(const QModelIndex &)));
// Create search dialog
#include <QtGui>
+#include <QListWidget>
+#include <QAbstractItemModel>
#include "listwindow.h"
#include "trace.h"
-#include "listview.h"
#include "platform.h"
#ifdef Q_OS_SYMBIAN
#include "flickcharm.h"
#endif
-ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
+ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), model(0)
{
#if defined(Q_WS_MAEMO_5)
setAttribute(Qt::WA_Maemo5StackedWindow, true);
- popup = new QMenu(this);
-
- QScrollArea *scroller = new QScrollArea(this);
- setCentralWidget(scroller);
- scroller->setProperty("FingerScrollable", true);
- // scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
- // scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
- scroller->setFrameStyle(QFrame::NoFrame);
- scroller->show();
-
- QWidget *content = new QWidget(scroller);
- contentLayout = new QVBoxLayout(content);
- contentLayout->setMargin(0);
- content->setLayout(contentLayout);
- content->show();
-
- scroller->setWidget(content);
- scroller->setWidgetResizable(true);
-#else
- QFrame *frame = new QFrame(this);
- setCentralWidget(frame);
- contentLayout = new QHBoxLayout();
- frame->setLayout(contentLayout);
- buttonBox = new QDialogButtonBox(Qt::Vertical, this);
- contentLayout->addWidget(buttonBox);
-#endif // Q_WS_MAEMO_5
+#endif
+
+ list = new QListWidget(this);
+ populateList();
+ setCentralWidget(list);
#ifdef Q_OS_SYMBIAN
- charm = 0;
+ charm = new FlickCharm(this);
+ charm->activateOn(list);
QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
#endif // Q_OS_SYMBIAN
#ifdef Q_WS_MAC
- addAction(tr("Close"), this, SLOT(close()), QString(),
- QDialogButtonBox::RejectRole);
+ // FIXME
+ // addAction(tr("Close"), this, SLOT(close()), QString(),
+ // QDialogButtonBox::RejectRole);
#endif // Q_WS_MAC
+
+ connect(list, SIGNAL(activated(const QModelIndex &)),
+ this, SLOT(onItemActicvated(const QModelIndex &)));
}
-void ListWindow::addList(ListView *listView)
+void ListWindow::populateList()
{
TRACE;
- list = listView;
-#if defined(Q_WS_MAEMO_5)
- // FIXME: list->installEventFilter(this);
- list->setProperty("FingerScrollable", false);
- int height = list->sizeHintForRow(0) * list->model()->rowCount();
- qDebug() << "Minimum height" << height;
- list->setMinimumHeight(height);
- contentLayout->addWidget(list);
- connect(list->model(),
- SIGNAL(rowsInserted(const QModelIndex &, int, int)),
- this, SLOT(onModelChanged()));
- connect(list->model(),
- SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
- this, SLOT(onModelChanged()));
-#else
- contentLayout->insertWidget(0, list);
-#endif // Q_WS_MAEMO5
-#ifdef Q_OS_SYMBIAN
- if (!charm) {
- charm = new FlickCharm(this);
+ list->clear();
+ if (model) {
+ for (int i = 0; i < model->rowCount(); i++) {
+ QModelIndex index = model->index(i, 0);
+ QString text = model->data(index, Qt::DisplayRole).toString();
+ QIcon icon;
+ QVariant iconData = model->data(index, Qt::DecorationRole);
+ if (iconData.canConvert<QIcon>()) {
+ icon = iconData.value<QIcon>();
+ }
+ (void)new QListWidgetItem(icon, text, list);
+ }
}
- // FIXME: Charms need more work...: charm->activateOn(list);
-#endif // Q_OS_SYMBIAN
+ for (int i = 0; i < buttons.count(); i++) {
+ QListWidgetItem *item = new QListWidgetItem();
+ list->insertItem(i, item);
+ list->setItemWidget(item, buttons[i]);
+ }
+}
- connect(list->selectionModel(),
- SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
- this,
- SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
+void ListWindow::setModel(QAbstractItemModel *model_)
+{
+ model = model_;
+ populateList();
+ if (model) {
+ connect(model,
+ SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
+ this, SLOT(populateList()));
+ }
}
-void ListWindow::addAction(const QString &title, QObject *receiver,
- const char *slot, const QString &iconName,
- QDialogButtonBox::ButtonRole role)
+void ListWindow::addButton(const QString &title, QObject *receiver,
+ const char *slot, const QString &iconName)
{
TRACE;
-#ifdef Q_WS_MAEMO_5
- Q_UNUSED(role);
+
QPushButton *button = new QPushButton(QIcon(Platform::instance()->
icon(iconName)), title, this);
- contentLayout->addWidget(button);
- connect(button, SIGNAL(clicked()), receiver, slot);
-#elif defined(Q_OS_SYMBIAN)
- Q_UNUSED(role);
- Q_UNUSED(iconName);
- QAction *action = new QAction(title, this);
- connect(action, SIGNAL(triggered()), receiver, slot);
- action->setSoftKeyRole(QAction::PositiveSoftKey);
- menuBar()->addAction(action);
-#else
- Q_UNUSED(iconName);
- QPushButton *button = buttonBox->addButton(title, role);
connect(button, SIGNAL(clicked()), receiver, slot);
-#endif // Q_WS_MAEMO_5
-}
+ buttons.append(button);
-void ListWindow::addItemAction(const QString &title, QObject *receiver,
- const char *slot)
-{
- TRACE;
-#ifdef Q_WS_MAEMO_5
- popup->addAction(title, receiver, slot);
-#elif defined Q_OS_SYMBIAN
- QAction *action = new QAction(title, this);
- connect(action, SIGNAL(triggered()), receiver, slot);
- action->setSoftKeyRole(QAction::PositiveSoftKey);
- menuBar()->addAction(action);
- // FIXME: Add action to the list of item specific actions
-#else
- QPushButton *button =
- buttonBox->addButton(title, QDialogButtonBox::ActionRole);
- connect(button, SIGNAL(clicked()), receiver, slot);
- itemButtons.append(button);
- activateItemButtons();
-#endif // Q_WS_MAEMO_5
+ int pos = buttons.length() - 1;
+ QListWidgetItem *item = new QListWidgetItem();
+ list->insertItem(pos, item);
+ list->setItemWidget(item, button);
}
QAction *ListWindow::addMenuAction(const QString &title, QObject *receiver,
return action;
}
-#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
-
-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()
+void ListWindow::onItemActivated(const QModelIndex &)
{
- bool enable = false;
- if (list) {
- enable = list->selectionModel()->hasSelection();
- }
- foreach (QPushButton *button, itemButtons) {
- button->setEnabled(enable);
- }
+ TRACE;
+ // FIXME
}
-#endif // ! Q_WS_MAEMO_5
-
#ifdef Q_WS_MAEMO_5
-bool ListWindow::eventFilter(QObject *obj, QEvent *event)
-{
- if (event->type() == QEvent::ContextMenu) {
- qDebug() << "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 {
- return QObject::eventFilter(obj, event);
- }
-}
-
-void ListWindow::onModelChanged()
+void ListWindow::closeEvent(QCloseEvent *event)
{
- list->setMinimumHeight(list->contentsHeight());
+ // Work around Maemo/Qt bug: Menu items are not removed on close
+ menuBar()->clear();
+ // FIXME: Is this needed? event->accept();
+ QMainWindow::closeEvent(event);
}
#endif // Q_WS_MAEMO_5
#include <QDialogButtonBox>
#include <QList>
-class QListView;
class QString;
-class QBoxLayout;
class QPushButton;
-class QModelIndex;
-class QItemSelection;
-class QEvent;
-class ListView;
class FlickCharm;
+class QAbstractItemModel;
+class QListWidget;
+class QModelIndex;
/** A window with a list and menu actions (Maemo) or buttons (non-Maemo). */
class ListWindow: public QMainWindow
Q_OBJECT
public:
+ /**
+ * Constructor.
+ * @param model Model for the list widget contents.
+ * @param parent Parent widget.
+ */
explicit ListWindow(QWidget *parent = 0);
- /** Add a list view to the window. */
- void addList(ListView *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.
+ * Set the model for the list.
*/
- void addAction(const QString &title, QObject *receiver, const char *slot,
- const QString &iconPath = QString(),
- QDialogButtonBox::ButtonRole role = QDialogButtonBox::ActionRole);
+ void setModel(QAbstractItemModel *model);
/**
- * 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.
+ * Add an action button to the beginning of the list.
*/
- void addItemAction(const QString &title, QObject *receiver,
- const char *slot);
+ void addButton(const QString &title, QObject *receiver, const char *slot,
+ const QString &iconPath = QString());
/**
* Add an action to the menu.
#endif
protected slots:
- void onSelectionChanged(const QItemSelection &selected,
- const QItemSelection &deselected);
-#ifdef Q_WS_MAEMO_5
- void onModelChanged();
-#else
- void activateItemButtons();
-#endif
+ void onItemActivated(const QModelIndex &);
+ void populateList();
protected:
-#ifdef Q_WS_MAEMO_5
- bool eventFilter(QObject *obj, QEvent *event);
- void closeEvent(QCloseEvent *event);
- QMenu *popup;
-#else
- QDialogButtonBox *buttonBox;
- QList<QPushButton *> itemButtons;
-#endif // Q_WS_MAEMO_5
+ QListWidget *list;
+ QAbstractItemModel *model;
+ QList<QPushButton *> buttons;
#ifdef Q_OS_SYMBIAN
FlickCharm *charm;
#endif
- QBoxLayout *contentLayout;
- ListView *list;
};
#endif // LISTWINDOW_H