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