Add feature to remove folder from library. Experiment with context menus on 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();
15     frame->setLayout(frameLayout);
16
17 #ifdef Q_WS_MAEMO_5
18     popup = new QMenu(this);
19 #else
20     buttonBox = new QDialogButtonBox(Qt::Vertical, this);
21     frameLayout->addWidget(buttonBox);
22 #endif
23 }
24
25 void ListWindow::addList(QListView *listView)
26 {
27     list = listView;
28 #ifdef Q_WS_MAEMO_5
29     list->installEventFilter(this);
30 #endif
31     frameLayout->insertWidget(0, list);
32     connect(list->selectionModel(),
33       SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
34       this,
35       SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
36 }
37
38 void ListWindow::addAction(const QString &title, QObject *receiver,
39                            const char *slot, QDialogButtonBox::ButtonRole role)
40 {
41 #ifndef Q_WS_MAEMO_5
42     QPushButton *button = buttonBox->addButton(title, role);
43     connect(button, SIGNAL(clicked()), receiver, slot);
44 #else
45     Q_UNUSED(role);
46     QAction *action = menuBar()->addAction(title);
47     connect(action, SIGNAL(triggered()), receiver, slot);
48 #endif // ! Q_WS_MAEMO_5
49 }
50
51 void ListWindow::addItemAction(const QString &title, QObject *receiver,
52                                const char *slot)
53 {
54 #ifndef Q_WS_MAEMO_5
55     QPushButton *button =
56             buttonBox->addButton(title, QDialogButtonBox::ActionRole);
57     connect(button, SIGNAL(clicked()), receiver, slot);
58     itemButtons.append(button);
59     activateItemButtons();
60 #else
61     popup->addAction(title, receiver, slot);
62 #endif // ! Q_WS_MAEMO_5
63 }
64
65 #ifdef Q_WS_MAEMO_5
66
67 void ListWindow::closeEvent(QCloseEvent *event)
68 {
69     // Work around Maemo/Qt but: Menu items are not removed on close
70     menuBar()->clear();
71     event->accept();
72 }
73
74 #endif // Q_WS_MAEMO_5
75
76 void ListWindow::onSelectionChanged(const QItemSelection &selected,
77                                     const QItemSelection &deselected)
78 {
79     Q_UNUSED(selected);
80     Q_UNUSED(deselected);
81 #ifndef Q_WS_MAEMO_5
82     activateItemButtons();
83 #endif
84 }
85
86 #ifndef Q_WS_MAEMO_5
87
88 void ListWindow::activateItemButtons()
89 {
90     bool enable = false;
91     if (list) {
92         enable = list->selectionModel()->hasSelection();
93     }
94     foreach (QPushButton *button, itemButtons) {
95         button->setEnabled(enable);
96     }
97 }
98
99 #endif // ! Q_WS_MAEMO_5
100
101 #ifdef Q_WS_MAEMO_5
102
103 bool ListWindow::eventFilter(QObject *obj, QEvent *event)
104 {
105     if (event->type() == QEvent::ContextMenu) {
106         Trace::trace("ListWindow::eventFiler: Received QEvent::ContextMenu");
107         if (popup->actions().size()) {
108             QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
109             QPoint pos = mouseEvent->globalPos();
110             pos.setX(pos.x() - 150);
111             if (pos.x() < 0) {
112                 pos.setX(0);
113             }
114             popup->exec(pos);
115         }
116         return true;
117     } else {
118         Trace::trace("ListWindow::eventFilter");
119         return QObject::eventFilter(obj, event);
120     }
121 }
122
123 #endif // Q_WS_MAEMO_5