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()));
}
// 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;
}
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()
// 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()));
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()
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();
void onRemove();
void onDetails();
void onRead();
- void onItemSelectionChanged();
#endif // Q_WS_MAEMO_5
void onBookAdded();
void onItemActivated(const QModelIndex &index);
#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);
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
}
#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
#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
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