Small dialog improvements.
[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     scroller = new QScrollArea(this);
16
17 #if defined(Q_WS_MAEMO_5)
18     scroller->setProperty("FingerScrollable", true);
19     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
20 #elif defined(Q_OS_SYMBIAN)
21     FlickCharm *charm = new FlickCharm(this);
22     charm->activateOn(scroller);
23 #else
24     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
25 #endif
26     scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
27     scroller->setFrameStyle(QFrame::NoFrame);
28
29     content = new QWidget(scroller);
30     contentLayout = new QVBoxLayout(content);
31     contentLayout->setMargin(0);
32     content->setLayout(contentLayout);
33
34     QBoxLayout *boxLayout;
35     QRect screenGeometry = QApplication::desktop()->screenGeometry();
36     if (screenGeometry.width() < screenGeometry.height()) {
37 #ifndef Q_OS_SYMBIAN
38         if (showButtons) {
39             buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
40         }
41 #endif
42         boxLayout = new QVBoxLayout(this);
43     } else {
44 #ifndef Q_OS_SYMBIAN
45         if (showButtons) {
46             buttonBox = new QDialogButtonBox(Qt::Vertical, this);
47         }
48 #endif
49         boxLayout = new QHBoxLayout(this);
50     }
51     boxLayout->addWidget(scroller);
52 #ifndef Q_OS_SYMBIAN
53     if (showButtons) {
54         boxLayout->addWidget(buttonBox);
55     }
56 #endif
57     setLayout(boxLayout);
58
59     scroller->setWidget(content);
60     content->show();
61     scroller->setWidgetResizable(true);
62
63 #if defined(Q_OS_SYMBIAN)
64     QAction *closeAction = new QAction(tr("Back"), this);
65     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
66     connect(closeAction, SIGNAL(triggered()), this, SLOT(reject()));
67     addAction(closeAction);
68     leftSoftKey = 0;
69     menuBar = 0;
70 #endif // Q_OS_SYMBIAN
71 }
72
73 void Dyalog::addWidget(QWidget *widget)
74 {
75     contentLayout->addWidget(widget);
76 }
77
78 void Dyalog::addStretch(int stretch)
79 {
80     contentLayout->addStretch(stretch);
81 }
82
83 void Dyalog::addButton(const QString &label, QObject *receiver,
84                        const char *slot, QDialogButtonBox::ButtonRole role)
85 {
86     TRACE;
87     if (!showButtons) {
88         qDebug() << "Ignored: showButtons is false";
89         return;
90     }
91 #ifdef Q_OS_SYMBIAN
92     Q_UNUSED(role);
93 #if 0
94     if (!leftSoftKey) {
95         qDebug() << "Adding left soft key";
96         leftSoftKey = new QAction(label, this);
97         leftSoftKey->setSoftKeyRole(QAction::PositiveSoftKey);
98         connect(leftSoftKey, SIGNAL(triggered()), receiver, slot);
99         addAction(leftSoftKey);
100     } else {
101 #endif
102         if (!menuBar) {
103             qDebug() << "Creating menu bar";
104             menuBar = new QMenuBar(this);
105             menuBar->addAction(leftSoftKey);
106         }
107         qDebug() << "Adding to menu bar";
108         QAction *action = new QAction(label, this);
109         connect(action, SIGNAL(triggered()), receiver, slot);
110         menuBar->addAction(action);
111 #if 0
112     }
113 #endif
114 #else
115     QPushButton *button = new QPushButton(label, this);
116     connect(button, SIGNAL(clicked()), receiver, slot);
117     buttonBox->addButton(button, role);
118 #endif // Q_OS_SYMBIAN
119 }
120
121 #ifdef Q_OS_SYMBIAN
122
123 void Dyalog::show()
124 {
125     foreach (QWidget *w, QApplication::allWidgets()) {
126         w->setContextMenuPolicy(Qt::NoContextMenu);
127     }
128     showMaximized();
129 }
130
131 int Dyalog::exec()
132 {
133     show();
134     return QDialog::exec();
135 }
136
137 #endif // Q_OS_SYMBIAN