Fix sorting of library. Add basic search framework.
[dorian] / widgets / listwindow.cpp
1 #include <QtGui>
2
3 #include "listwindow.h"
4 #include "trace.h"
5 #include "listview.h"
6 #include "platform.h"
7
8 ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
9 {
10 #ifdef Q_WS_MAEMO_5
11     setAttribute(Qt::WA_Maemo5StackedWindow, true);
12     popup = new QMenu(this);
13
14     QScrollArea *scroller = new QScrollArea(this);
15     setCentralWidget(scroller);
16     scroller->setProperty("FingerScrollable", true);
17     // scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
18     // scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
19     scroller->setFrameStyle(QFrame::NoFrame);
20     scroller->show();
21
22     QWidget *content = new QWidget(scroller);
23     contentLayout = new QVBoxLayout(content);
24     contentLayout->setMargin(0);
25     content->setLayout(contentLayout);
26     content->show();
27
28     scroller->setWidget(content);
29     scroller->setWidgetResizable(true);
30 #else
31     QFrame *frame = new QFrame(this);
32     setCentralWidget(frame);
33     contentLayout = new QHBoxLayout();
34     frame->setLayout(contentLayout);
35 #   ifdef Q_OS_SYMBIAN
36     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
37     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
38     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
39     QMainWindow::addAction(closeAction);
40 #   else
41     buttonBox = new QDialogButtonBox(Qt::Vertical, this);
42     contentLayout->addWidget(buttonBox);
43 #   endif // Q_OS_SYMBIAN
44 #endif // Q_WS_MAEMO_5
45
46 #ifdef Q_WS_MAC
47     addAction(tr("Close"), this, SLOT(close()), QString(),
48               QDialogButtonBox::RejectRole);
49 #endif // Q_WS_MAC
50 }
51
52 void ListWindow::addList(ListView *listView)
53 {
54     Trace t("ListWindow::addList");
55     list = listView;
56 #ifdef Q_WS_MAEMO_5
57     list->installEventFilter(this);
58     list->setMinimumHeight(list->contentsHeight());
59     contentLayout->addWidget(list);
60     connect(list->model(),
61             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
62             this, SLOT(onModelChanged()));
63     connect(list->model(),
64             SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
65             this, SLOT(onModelChanged()));
66 #else
67     contentLayout->insertWidget(0, list);
68 #endif
69     connect(list->selectionModel(),
70       SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
71       this,
72       SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
73 }
74
75 void ListWindow::addAction(const QString &title, QObject *receiver,
76                            const char *slot, const QString &iconName,
77                            QDialogButtonBox::ButtonRole role)
78 {
79     Trace t("ListWindow::addAction");
80 #ifdef Q_WS_MAEMO_5
81     Q_UNUSED(role);
82     QPushButton *button =
83             new QPushButton(QIcon(Platform::icon(iconName)), title, this);
84     contentLayout->addWidget(button);
85     connect(button, SIGNAL(clicked()), receiver, slot);
86 #elif defined(Q_OS_SYMBIAN)
87     Q_UNUSED(role);
88     Q_UNUSED(iconName);
89     QAction *action = new QAction(title, this);
90     connect(action, SIGNAL(triggered()), receiver, slot);
91     action->setSoftKeyRole(QAction::PositiveSoftKey);
92     menuBar()->addAction(action);
93 #else
94     Q_UNUSED(iconName);
95     QPushButton *button = buttonBox->addButton(title, role);
96     connect(button, SIGNAL(clicked()), receiver, slot);
97 #endif // Q_WS_MAEMO_5
98 }
99
100 void ListWindow::addItemAction(const QString &title, QObject *receiver,
101                                const char *slot)
102 {
103     Trace t("ListWindow::addItemAction");
104 #ifdef Q_WS_MAEMO_5
105     popup->addAction(title, receiver, slot);
106 #elif defined Q_OS_SYMBIAN
107     QAction *action = new QAction(title, this);
108     connect(action, SIGNAL(triggered()), receiver, slot);
109     action->setSoftKeyRole(QAction::PositiveSoftKey);
110     menuBar()->addAction(action);
111     // FIXME: Add action to the list of item specific actions
112 #else
113     QPushButton *button =
114             buttonBox->addButton(title, QDialogButtonBox::ActionRole);
115     connect(button, SIGNAL(clicked()), receiver, slot);
116     itemButtons.append(button);
117     activateItemButtons();
118 #endif // Q_WS_MAEMO_5
119 }
120
121 #ifdef Q_WS_MAEMO_5
122
123 void ListWindow::closeEvent(QCloseEvent *event)
124 {
125     // Work around Maemo/Qt but: Menu items are not removed on close
126     menuBar()->clear();
127     event->accept();
128 }
129
130 #endif // Q_WS_MAEMO_5
131
132 void ListWindow::onSelectionChanged(const QItemSelection &selected,
133                                     const QItemSelection &deselected)
134 {
135     Q_UNUSED(selected);
136     Q_UNUSED(deselected);
137 #ifndef Q_WS_MAEMO_5
138     activateItemButtons();
139 #endif
140 }
141
142 #ifndef Q_WS_MAEMO_5
143
144 void ListWindow::activateItemButtons()
145 {
146     bool enable = false;
147     if (list) {
148         enable = list->selectionModel()->hasSelection();
149     }
150     foreach (QPushButton *button, itemButtons) {
151         button->setEnabled(enable);
152     }
153 }
154
155 #endif // ! Q_WS_MAEMO_5
156
157 #ifdef Q_WS_MAEMO_5
158
159 bool ListWindow::eventFilter(QObject *obj, QEvent *event)
160 {
161     if (event->type() == QEvent::ContextMenu) {
162         qDebug() << "ListWindow::eventFiler: Received QEvent::ContextMenu";
163         if (popup->actions().size()) {
164             QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
165             QPoint pos = mouseEvent->globalPos();
166             pos.setX(pos.x() - 150);
167             if (pos.x() < 0) {
168                 pos.setX(0);
169             }
170             popup->exec(pos);
171         }
172         return true;
173     } else {
174         return QObject::eventFilter(obj, event);
175     }
176 }
177
178 void ListWindow::onModelChanged()
179 {
180     qDebug() << "ListWindow::onModelChanged";
181     list->setMinimumHeight(list->contentsHeight());
182 }
183
184 #endif // Q_WS_MAEMO_5
185
186 #ifdef Q_OS_SYMBIAN
187
188 void ListWindow::show()
189 {
190     foreach (QWidget *w, QApplication::allWidgets()) {
191         w->setContextMenuPolicy(Qt::NoContextMenu);
192     }
193     showMaximized();
194 }
195
196 #endif // Q_OS_SYMBIAN