addef7033cf43edd76fc680e247d35752aa12e26
[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
31     content = new QWidget(scroller);
32     contentLayout = new QVBoxLayout(content);
33     contentLayout->setMargin(0);
34     content->setLayout(contentLayout);
35
36     QBoxLayout *boxLayout;
37     QRect screenGeometry = QApplication::desktop()->screenGeometry();
38     if (screenGeometry.width() < screenGeometry.height()) {
39 #ifndef Q_OS_SYMBIAN
40         if (showButtons) {
41             buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
42         }
43 #endif
44         boxLayout = new QVBoxLayout(this);
45     } else {
46 #ifndef Q_OS_SYMBIAN
47         if (showButtons) {
48             buttonBox = new QDialogButtonBox(Qt::Vertical, this);
49         }
50 #endif
51         boxLayout = new QHBoxLayout(this);
52     }
53     boxLayout->addWidget(scroller);
54 #ifndef Q_OS_SYMBIAN
55     if (showButtons) {
56         boxLayout->addWidget(buttonBox);
57     }
58 #endif
59     setLayout(boxLayout);
60
61     scroller->setWidget(content);
62     content->show();
63     scroller->setWidgetResizable(true);
64
65 #if defined(Q_OS_SYMBIAN)
66     QAction *closeAction = new QAction(tr("Back"), this);
67     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
68     connect(closeAction, SIGNAL(triggered()), this, SLOT(reject()));
69     addAction(closeAction);
70     leftSoftKey = 0;
71     menuBar = 0;
72 #endif // Q_OS_SYMBIAN
73 }
74
75 void Dyalog::addWidget(QWidget *widget)
76 {
77     contentLayout->addWidget(widget);
78 }
79
80 void Dyalog::addStretch(int stretch)
81 {
82     contentLayout->addStretch(stretch);
83 }
84
85 void Dyalog::addButton(const QString &label, QObject *receiver,
86                        const char *slot, QDialogButtonBox::ButtonRole role)
87 {
88     TRACE;
89     if (!showButtons) {
90         qDebug() << "Ignored: showButtons is false";
91         return;
92     }
93 #ifdef Q_OS_SYMBIAN
94     Q_UNUSED(role);
95     if (!leftSoftKey) {
96         // Add new action as left softkey
97         leftSoftKey = new QAction(label, this);
98         leftSoftKey->setSoftKeyRole(QAction::PositiveSoftKey);
99         connect(leftSoftKey, SIGNAL(triggered()), receiver, slot);
100         addAction(leftSoftKey);
101     } else {
102         if (!menuBar) {
103             // Create menu bar
104             menuBar = new QMenuBar(this);
105             // Add previous LSK to menu bar
106             leftSoftKey->setSoftKeyRole(QAction::NoSoftKey);
107             menuBar->addAction(leftSoftKey);
108         }
109         // Add new action to menu bar
110         QAction *action = new QAction(label, this);
111         connect(action, SIGNAL(triggered()), receiver, slot);
112         menuBar->addAction(action);
113     }
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