Set zoom level in steps. Add icons to some buttons. Adjust dialog box layout accordin...
[dorian] / widgets / listwindow.cpp
1 #include <QtGui>
2
3 #include "listwindow.h"
4 #include "trace.h"
5 #include "listview.h"
6
7 ListWindow::ListWindow(QWidget *parent): QMainWindow(parent), list(0)
8 {
9 #ifdef Q_WS_MAEMO_5
10     setAttribute(Qt::WA_Maemo5StackedWindow, true);
11     popup = new QMenu(this);
12
13     QScrollArea *scroller = new QScrollArea(this);
14     setCentralWidget(scroller);
15     scroller->setProperty("FingerScrollable", true);
16     // scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17     // scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
18     scroller->setFrameStyle(QFrame::NoFrame);
19     scroller->show();
20
21     QWidget *content = new QWidget(scroller);
22     contentLayout = new QVBoxLayout(content);
23     contentLayout->setMargin(0);
24     content->setLayout(contentLayout);
25     content->show();
26
27     scroller->setWidget(content);
28     scroller->setWidgetResizable(true);
29 #else
30     QFrame *frame = new QFrame(this);
31     setCentralWidget(frame);
32     contentLayout = new QHBoxLayout();
33     frame->setLayout(contentLayout);
34     buttonBox = new QDialogButtonBox(Qt::Vertical, this);
35     contentLayout->addWidget(buttonBox);
36 #endif
37 }
38
39 void ListWindow::addList(ListView *listView)
40 {
41     Trace t("ListWindow::addList");
42     list = listView;
43 #ifdef Q_WS_MAEMO_5
44     list->installEventFilter(this);
45     list->setMinimumHeight(list->contentsHeight());
46     contentLayout->addWidget(list);
47     connect(list->model(),
48             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
49             this, SLOT(onModelChanged()));
50     connect(list->model(),
51             SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
52             this, SLOT(onModelChanged()));
53 #else
54     contentLayout->insertWidget(0, list);
55 #endif
56     connect(list->selectionModel(),
57       SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
58       this,
59       SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
60 }
61
62 void ListWindow::addAction(const QString &title, QObject *receiver,
63                            const char *slot, const QString &iconPath,
64                            QDialogButtonBox::ButtonRole role)
65 {
66     Trace t("ListWindow::addAction");
67 #ifndef Q_WS_MAEMO_5
68     Q_UNUSED(iconPath);
69     QPushButton *button = buttonBox->addButton(title, role);
70     connect(button, SIGNAL(clicked()), receiver, slot);
71 #else
72     Q_UNUSED(role);
73     QPushButton *button = new QPushButton(QIcon(iconPath), title, this);
74     contentLayout->addWidget(button);
75     connect(button, SIGNAL(clicked()), receiver, slot);
76 #endif // ! Q_WS_MAEMO_5
77 }
78
79 void ListWindow::addItemAction(const QString &title, QObject *receiver,
80                                const char *slot)
81 {
82     Trace t("ListWindow::addItemAction");
83 #ifndef Q_WS_MAEMO_5
84     QPushButton *button =
85             buttonBox->addButton(title, QDialogButtonBox::ActionRole);
86     connect(button, SIGNAL(clicked()), receiver, slot);
87     itemButtons.append(button);
88     activateItemButtons();
89 #else
90     popup->addAction(title, receiver, slot);
91 #endif // ! Q_WS_MAEMO_5
92 }
93
94 #ifdef Q_WS_MAEMO_5
95
96 void ListWindow::closeEvent(QCloseEvent *event)
97 {
98     // Work around Maemo/Qt but: Menu items are not removed on close
99     menuBar()->clear();
100     event->accept();
101 }
102
103 #endif // Q_WS_MAEMO_5
104
105 void ListWindow::onSelectionChanged(const QItemSelection &selected,
106                                     const QItemSelection &deselected)
107 {
108     Q_UNUSED(selected);
109     Q_UNUSED(deselected);
110 #ifndef Q_WS_MAEMO_5
111     activateItemButtons();
112 #endif
113 }
114
115 #ifndef Q_WS_MAEMO_5
116
117 void ListWindow::activateItemButtons()
118 {
119     bool enable = false;
120     if (list) {
121         enable = list->selectionModel()->hasSelection();
122     }
123     foreach (QPushButton *button, itemButtons) {
124         button->setEnabled(enable);
125     }
126 }
127
128 #endif // ! Q_WS_MAEMO_5
129
130 #ifdef Q_WS_MAEMO_5
131
132 bool ListWindow::eventFilter(QObject *obj, QEvent *event)
133 {
134     if (event->type() == QEvent::ContextMenu) {
135         qDebug() << "ListWindow::eventFiler: Received QEvent::ContextMenu";
136         if (popup->actions().size()) {
137             QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
138             QPoint pos = mouseEvent->globalPos();
139             pos.setX(pos.x() - 150);
140             if (pos.x() < 0) {
141                 pos.setX(0);
142             }
143             popup->exec(pos);
144         }
145         return true;
146     } else {
147         return QObject::eventFilter(obj, event);
148     }
149 }
150
151 void ListWindow::onModelChanged()
152 {
153     qDebug() << "ListWindow::onModelChanged";
154     list->setMinimumHeight(list->contentsHeight());
155 }
156
157 #endif // Q_WS_MAEMO_5