Improve look on Mac.
[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 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
60     boxLayout->setMargin(0);
61 #endif
62     boxLayout->addWidget(scroller);
63 #ifndef Q_OS_SYMBIAN
64     if (showButtons) {
65         boxLayout->addWidget(buttonBox);
66     }
67 #endif
68     setLayout(boxLayout);
69
70     scroller->setWidget(content);
71     content->show();
72     scroller->setWidgetResizable(true);
73
74 #if defined(Q_OS_SYMBIAN)
75     QAction *closeAction = new QAction(tr("Back"), this);
76     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
77     connect(closeAction, SIGNAL(triggered()), this, SLOT(reject()));
78     addAction(closeAction);
79     leftSoftKey = 0;
80     menuBar = 0;
81 #endif // Q_OS_SYMBIAN
82 }
83
84 void Dyalog::addWidget(QWidget *widget)
85 {
86     contentLayout->addWidget(widget);
87 }
88
89 void Dyalog::addStretch(int stretch)
90 {
91     contentLayout->addStretch(stretch);
92 }
93
94 void Dyalog::addButton(const QString &label, QObject *receiver,
95                        const char *slot, QDialogButtonBox::ButtonRole role)
96 {
97     TRACE;
98     if (!showButtons) {
99         qDebug() << "Ignored: showButtons is false";
100         return;
101     }
102 #ifdef Q_OS_SYMBIAN
103     Q_UNUSED(role);
104     if (!leftSoftKey) {
105         // Add new action as left softkey
106         leftSoftKey = new QAction(label, this);
107         leftSoftKey->setSoftKeyRole(QAction::PositiveSoftKey);
108         connect(leftSoftKey, SIGNAL(triggered()), receiver, slot);
109         addAction(leftSoftKey);
110     } else {
111         if (!menuBar) {
112             // Create menu bar
113             menuBar = new QMenuBar(this);
114             // Add previous LSK to menu bar
115             leftSoftKey->setSoftKeyRole(QAction::NoSoftKey);
116             menuBar->addAction(leftSoftKey);
117         }
118         // Add new action to menu bar
119         QAction *action = new QAction(label, this);
120         connect(action, SIGNAL(triggered()), receiver, slot);
121         menuBar->addAction(action);
122     }
123 #else
124     QPushButton *button = new QPushButton(label, this);
125     connect(button, SIGNAL(clicked()), receiver, slot);
126     buttonBox->addButton(button, role);
127 #endif // Q_OS_SYMBIAN
128 }
129
130 #ifdef Q_OS_SYMBIAN
131
132 void Dyalog::show()
133 {
134     foreach (QWidget *w, QApplication::allWidgets()) {
135         w->setContextMenuPolicy(Qt::NoContextMenu);
136     }
137     QDialog::showMaximized();
138 }
139
140 int Dyalog::exec()
141 {
142     show();
143     return QDialog::exec();
144 }
145
146 #endif // Q_OS_SYMBIAN