More Symbian fixes.
[dorian] / widgets / dyalog.cpp
1 #include <QtGui>
2
3 #include "dyalog.h"
4 #include "trace.h"
5
6 #ifdef Q_OS_SYMBIAN
7 #include "flickcharm.h"
8 #endif
9
10 Dyalog::Dyalog(QWidget *parent, bool showButtons_):
11     QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
12                     Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint),
13     showButtons(showButtons_)
14 {
15     setAttribute(Qt::WA_DeleteOnClose);
16
17     scroller = new QScrollArea(this);
18
19 #if defined(Q_WS_MAEMO_5)
20     scroller->setProperty("FingerScrollable", true);
21     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
22 #elif defined(Q_OS_SYMBIAN)
23     FlickCharm *charm = new FlickCharm(this);
24     charm->activateOn(scroller);
25 #else
26     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
27 #endif
28     scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
29     scroller->setFrameStyle(QFrame::NoFrame);
30 #if defined(Q_OS_SYMBIAN)
31     // setStyleSheet("QFrame {margin:0; border:0; padding:0}");
32     setStyleSheet("QScrollArea {margin:0; border:0; padding:0}");
33 #endif
34
35     content = new QWidget(scroller);
36     contentLayout = new QVBoxLayout(content);
37 #if !defined(Q_OS_SYMBIAN)
38     contentLayout->setMargin(0);
39 #endif
40     content->setLayout(contentLayout);
41
42     QBoxLayout *boxLayout;
43     QRect screenGeometry = QApplication::desktop()->screenGeometry();
44     if (screenGeometry.width() < screenGeometry.height()) {
45 #ifndef Q_OS_SYMBIAN
46         if (showButtons) {
47             buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
48         }
49 #endif
50         boxLayout = new QVBoxLayout(this);
51     } else {
52 #ifndef Q_OS_SYMBIAN
53         if (showButtons) {
54             buttonBox = new QDialogButtonBox(Qt::Vertical, this);
55         }
56 #endif
57         boxLayout = new QHBoxLayout(this);
58     }
59     boxLayout->setMargin(0);
60     boxLayout->addWidget(scroller);
61 #ifndef Q_OS_SYMBIAN
62     if (showButtons) {
63         boxLayout->addWidget(buttonBox);
64     }
65 #endif
66     setLayout(boxLayout);
67
68     scroller->setWidget(content);
69     content->show();
70     scroller->setWidgetResizable(true);
71
72 #if defined(Q_OS_SYMBIAN)
73     QAction *closeAction = new QAction(tr("Back"), this);
74     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
75     connect(closeAction, SIGNAL(triggered()), this, SLOT(reject()));
76     addAction(closeAction);
77     leftSoftKey = 0;
78     menuBar = 0;
79 #endif // Q_OS_SYMBIAN
80 }
81
82 void Dyalog::addWidget(QWidget *widget)
83 {
84     contentLayout->addWidget(widget);
85 }
86
87 void Dyalog::addStretch(int stretch)
88 {
89     contentLayout->addStretch(stretch);
90 }
91
92 void Dyalog::addButton(const QString &label, QObject *receiver,
93                        const char *slot, QDialogButtonBox::ButtonRole role)
94 {
95     TRACE;
96     if (!showButtons) {
97         qDebug() << "Ignored: showButtons is false";
98         return;
99     }
100 #ifdef Q_OS_SYMBIAN
101     Q_UNUSED(role);
102     if (!leftSoftKey) {
103         // Add new action as left softkey
104         leftSoftKey = new QAction(label, this);
105         leftSoftKey->setSoftKeyRole(QAction::PositiveSoftKey);
106         connect(leftSoftKey, SIGNAL(triggered()), receiver, slot);
107         addAction(leftSoftKey);
108     } else {
109         if (!menuBar) {
110             // Create menu bar
111             menuBar = new QMenuBar(this);
112             // Add previous LSK to menu bar
113             leftSoftKey->setSoftKeyRole(QAction::NoSoftKey);
114             menuBar->addAction(leftSoftKey);
115         }
116         // Add new action to menu bar
117         QAction *action = new QAction(label, this);
118         connect(action, SIGNAL(triggered()), receiver, slot);
119         menuBar->addAction(action);
120     }
121 #else
122     QPushButton *button = new QPushButton(label, this);
123     connect(button, SIGNAL(clicked()), receiver, slot);
124     buttonBox->addButton(button, role);
125 #endif // Q_OS_SYMBIAN
126 }
127
128 #ifdef Q_OS_SYMBIAN
129
130 void Dyalog::show()
131 {
132     foreach (QWidget *w, QApplication::allWidgets()) {
133         w->setContextMenuPolicy(Qt::NoContextMenu);
134     }
135     QDialog::showMaximized();
136 }
137
138 int Dyalog::exec()
139 {
140     show();
141     return QDialog::exec();
142 }
143
144 #endif // Q_OS_SYMBIAN