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