Add context dependent actions to LibraryDialog. Make folder delete work on non-Maemo.
[dorian] / widgets / listwindow.cpp
1 #include <QtGui>
2
3 #include "listwindow.h"
4 #include "trace.h"
5
6 ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
7 {
8 #ifdef Q_WS_MAEMO_5
9     setAttribute(Qt::WA_Maemo5StackedWindow, true);
10 #endif
11
12     QFrame *frame = new QFrame(this);
13     setCentralWidget(frame);
14     frameLayout = new QHBoxLayout(frame);
15     frame->setLayout(frameLayout);
16
17 #ifndef Q_WS_MAEMO_5
18     buttonBox = new QDialogButtonBox(Qt::Vertical, this);
19     frameLayout->addWidget(buttonBox);
20 #endif
21 }
22
23 void ListWindow::addList(QListView *listView)
24 {
25     list = listView;
26     frameLayout->insertWidget(0, list);
27     connect(list->selectionModel(),
28       SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
29       this,
30       SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection&)));
31 }
32
33 void ListWindow::addAction(const QString &title, QObject *receiver,
34                            const char *slot, QDialogButtonBox::ButtonRole role)
35 {
36 #ifndef Q_WS_MAEMO_5
37     QPushButton *button = buttonBox->addButton(title, role);
38     connect(button, SIGNAL(clicked()), receiver, slot);
39 #else
40     Q_UNUSED(role);
41     QAction *action = menuBar()->addAction(title);
42     connect(action, SIGNAL(triggered()), receiver, slot);
43 #endif // ! Q_WS_MAEMO_5
44 }
45
46 void ListWindow::addItemAction(const QString &title, QObject *receiver,
47                                const char *slot)
48 {
49 #ifndef Q_WS_MAEMO_5
50     QPushButton *button =
51             buttonBox->addButton(title, QDialogButtonBox::ActionRole);
52     connect(button, SIGNAL(clicked()), receiver, slot);
53     itemButtons.append(button);
54     activateItemButtons();
55 #else
56     // FIXME
57 #endif // ! Q_WS_MAEMO_5
58 }
59
60 #ifdef Q_WS_MAEMO_5
61
62 void ListWindow::closeEvent(QCloseEvent *event)
63 {
64     // Work around Maemo/Qt but: Menu items are not removed on close
65     menuBar()->clear();
66     event->accept();
67 }
68
69 #endif // Q_WS_MAEMO_5
70
71 void ListWindow::onSelectionChanged(const QItemSelection &selected,
72                                     const QItemSelection &deselected)
73 {
74     Q_UNUSED(selected);
75     Q_UNUSED(deselected);
76 #ifndef Q_WS_MAEMO_5
77     activateItemButtons();
78 #endif
79 }
80
81 #ifndef Q_WS_MAEMO_5
82
83 void ListWindow::activateItemButtons()
84 {
85     bool enable = false;
86     if (list) {
87         enable = list->selectionModel()->hasSelection();
88     }
89     foreach (QPushButton *button, itemButtons) {
90         button->setEnabled(enable);
91     }
92 }
93
94 #endif // ! Q_WS_MAEMO_5