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