Avoid complex types as statics.
[dorian] / adopterwindow.cpp
1 #include <QtGui>
2
3 #if defined(Q_WS_MAEMO_5)
4 #   include <QtGui/QX11Info>
5 #   include <X11/Xlib.h>
6 #   include <X11/Xatom.h>
7 #   include <QAbstractKineticScroller>
8 #endif
9
10 #include "adopterwindow.h"
11 #include "trace.h"
12 #include "bookview.h"
13 #include "platform.h"
14 #include "settings.h"
15
16 AdopterWindow::AdopterWindow(QWidget *parent): QMainWindow(parent), bookView(0)
17 {
18     TRACE;
19
20 #ifdef Q_WS_MAEMO_5
21     setAttribute(Qt::WA_Maemo5StackedWindow, true);
22 #endif // Q_WS_MAEMO_5
23
24     QFrame *frame = new QFrame(this);
25     QVBoxLayout *layout = new QVBoxLayout(frame);
26     layout->setMargin(0);
27     frame->setLayout(layout);
28     setCentralWidget(frame);
29
30 #ifdef Q_OS_SYMBIAN
31     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
32     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
33     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
34     QMainWindow::addAction(closeAction);
35 #else
36     // Tool bar
37     setUnifiedTitleAndToolBarOnMac(true);
38     toolBar = addToolBar("controls");
39     toolBar->setMovable(false);
40     toolBar->setFloatable(false);
41     toolBar->toggleViewAction()->setVisible(false);
42 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
43     toolBar->setIconSize(QSize(42, 42));
44 #endif
45 #endif // Q_OS_SYMBIAN
46
47     // Monitor settings
48     Settings *settings = Settings::instance();
49     connect(settings, SIGNAL(valueChanged(const QString &)),
50             this, SLOT(onSettingsChanged(const QString &)));
51     settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
52 }
53
54 void AdopterWindow::takeChildren(BookView *view, const QList<QWidget *> &others)
55 {
56     TRACE;
57     leaveChildren();
58     if (view) {
59         bookView = view;
60         bookView->setParent(centralWidget());
61         centralWidget()->layout()->addWidget(bookView);
62         bookView->show();
63     }
64     foreach (QWidget *child, others) {
65         if (child) {
66             child->setParent(this);
67         }
68     }
69 }
70
71 void AdopterWindow::leaveChildren()
72 {
73     TRACE;
74     if (bookView) {
75         centralWidget()->layout()->removeWidget(bookView);
76         bookView = 0;
77     }
78 }
79
80 void AdopterWindow::show()
81 {
82 #ifdef Q_OS_SYMBIAN
83     foreach (QWidget *w, QApplication::allWidgets()) {
84         w->setContextMenuPolicy(Qt::NoContextMenu);
85     }
86     showMaximized();
87     raise();
88 #else
89     QMainWindow::show();
90 #endif
91 }
92
93 QAction *AdopterWindow::addToolBarAction(QObject *receiver,
94                                          const char *member,
95                                          const QString &iconName,
96                                          const QString &text)
97 {
98     TRACE;
99     qDebug() << "icon" << iconName << "text" << text;
100 #ifndef Q_OS_SYMBIAN
101     return toolBar->addAction(QIcon(Platform::instance()->icon(iconName)),
102                               text, receiver, member);
103 #else
104     Q_UNUSED(iconName);
105     QAction *action = new QAction(text, this);
106     menuBar()->addAction(action);
107     connect(action, SIGNAL(triggered()), receiver, member);
108     return action;
109 #endif
110 }
111
112 void AdopterWindow::addToolBarSpace()
113 {
114 #ifndef Q_OS_SYMBIAN
115     QFrame *frame = new QFrame(toolBar);
116     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
117     toolBar->addWidget(frame);
118 #endif
119 }
120
121 void AdopterWindow::grabVolumeKeys(bool grab)
122 {
123     TRACE;
124     grabbingVolumeKeys = grab;
125 #ifdef Q_WS_MAEMO_5
126     doGrabVolumeKeys(grab);
127 #endif
128 }
129
130 #ifdef Q_WS_MAEMO_5
131
132 void AdopterWindow::doGrabVolumeKeys(bool grab)
133 {
134     TRACE;
135     if (!isVisible()) {
136         qDebug() << "Not visible - skipping";
137         return;
138     }
139     if (!winId()) {
140         qDebug() << "Could not get window ID - skipping";
141         return;
142     }
143     unsigned long val = grab? 1: 0;
144     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
145     if (!atom) {
146         qCritical() << "Unable to obtain _HILDON_ZOOM_KEY_ATOM";
147         return;
148     }
149     XChangeProperty(QX11Info::display(),
150         winId(),
151         atom,
152         XA_INTEGER,
153         32,
154         PropModeReplace,
155         reinterpret_cast<unsigned char *>(&val),
156         1);
157     qDebug() << "Grabbed volume keys";
158 }
159
160 #endif // Q_WS_MAEMO_5
161
162 #ifdef Q_WS_MAEMO_5
163
164 void AdopterWindow::showEvent(QShowEvent *e)
165 {
166     TRACE;
167     doGrabVolumeKeys(grabbingVolumeKeys);
168     QMainWindow::showEvent(e);
169 }
170
171 #endif // Q_WS_MAEMO_5
172
173 void AdopterWindow::keyPressEvent(QKeyEvent *event)
174 {
175     TRACE;
176     if (bookView && grabbingVolumeKeys) {
177         switch (event->key()) {
178 #ifdef Q_WS_MAEMO_5
179         case Qt::Key_F7:
180             qDebug() << "F7";
181             bookView->goNextPage();
182             event->accept();
183             break;
184         case Qt::Key_F8:
185             qDebug() << "F8";
186             bookView->goPreviousPage();
187             event->accept();
188             break;
189 #endif // Q_WS_MAEMO_5
190         case Qt::Key_PageUp:
191             bookView->goPreviousPage();
192             event->accept();
193             break;
194         case Qt::Key_PageDown:
195             bookView->goNextPage();
196             event->accept();
197             break;
198         default:
199             ;
200         }
201     }
202     QMainWindow::keyPressEvent(event);
203 }
204
205 void AdopterWindow::onSettingsChanged(const QString &key)
206 {
207     TRACE;
208     if (key == "usevolumekeys") {
209         qDebug() << key;
210         grabVolumeKeys(Settings::instance()->value(key).toBool());
211     }
212 }
213