4 #include <QApplication>
7 # include <QtMaemo5/QMaemo5InformationBox>
13 #include "infodialog.h"
14 #include "librarydialog.h"
16 #include "mainwindow.h"
17 #include "settingswindow.h"
18 #include "bookmarksdialog.h"
20 #include "chaptersdialog.h"
21 #include "fullscreenwindow.h"
24 #ifdef DORIAN_TEST_MODEL
25 #include "modeltest.h"
29 # define ICON_PREFIX ":/icons/mac/"
31 # define ICON_PREFIX ":/icons/"
34 MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), view(0)
37 setAttribute(Qt::WA_Maemo5StackedWindow, true);
39 setWindowTitle("Dorian");
41 // Central widget. Must be an intermediate because of reparenting the book view
42 QFrame *central = new QFrame(this);
43 QVBoxLayout *layout = new QVBoxLayout(central);
45 central->setLayout(layout);
46 setCentralWidget(central);
49 view = new BookView(central);
51 layout->addWidget(view);
54 setUnifiedTitleAndToolBarOnMac(true);
55 settings = new QDialog(this);
56 toolBar = addToolBar("controls");
57 toolBar->setMovable(false);
58 toolBar->setFloatable(false);
59 toolBar->toggleViewAction()->setVisible(false);
60 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
61 toolBar->setIconSize(QSize(42, 42));
64 previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
65 nextAction = addToolBarAction(view, SLOT(goNext()), "next");
66 chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
67 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
70 infoAction = menuBar()->addAction(tr("Book details"));
71 connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
72 libraryAction = menuBar()->addAction(tr("Library"));
73 connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
74 settingsAction = menuBar()->addAction(tr("Settings"));
75 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
76 devToolsAction = menuBar()->addAction(tr("Developer"));
77 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
79 infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
80 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
81 "system-file-manager");
82 settingsAction = addToolBarAction(this, SLOT(showSettings()),
83 "preferences-system");
84 devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
85 #endif // Q_WS_MAEMO_5
87 QFrame *frame = new QFrame(toolBar);
88 frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
89 toolBar->addWidget(frame);
91 fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
93 // Handle model changes
94 connect(Library::instance(), SIGNAL(nowReadingChanged()),
95 this, SLOT(onCurrentBookChanged()));
97 // Load book on command line, or load last read book, or load default book
98 Library *library = Library::instance();
99 if (QCoreApplication::arguments().size() == 2) {
100 QString path = QCoreApplication::arguments()[1];
102 QModelIndex index = library->find(path);
103 if (index.isValid()) {
104 library->setNowReading(index);
108 QModelIndex index = library->nowReading();
109 if (index.isValid()) {
110 library->setNowReading(index);
113 if (!library->rowCount()) {
114 library->add(":/books/2 B R 0 2 B.epub");
116 library->setNowReading(library->index(0));
120 // Handle settings changes
121 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
122 this, SLOT(onSettingsChanged(const QString &)));
123 Settings::instance()->setValue("orientation",
124 Settings::instance()->value("orientation"));
126 // Handle loading chapters
127 connect(view, SIGNAL(chapterLoadStart(int)),
128 this, SLOT(onChapterLoadStart()));
129 connect(view, SIGNAL(chapterLoadEnd(int)),
130 this, SLOT(onChapterLoadEnd(int)));
132 // Shadow window for full screen
133 fullScreenWindow = new FullScreenWindow(this);
134 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
136 #ifdef DORIAN_TEST_MODEL
137 (void)new ModelTest(Library::instance(), this);
141 void MainWindow::onCurrentBookChanged()
143 setCurrentBook(Library::instance()->nowReading());
146 void MainWindow::showRegular()
148 Trace t("MainWindow::showRegular");
149 fullScreenWindow->hide();
150 fullScreenWindow->leaveChild();
151 view->setParent(centralWidget());
152 centralWidget()->layout()->addWidget(view);
155 void MainWindow::showBig()
157 Trace t("MainWindow::showBig");
158 centralWidget()->layout()->removeWidget(view);
159 fullScreenWindow->takeChild(view);
160 fullScreenWindow->showFullScreen();
163 void MainWindow::setCurrentBook(const QModelIndex ¤t)
166 Book *book = Library::instance()->book(current);
168 setWindowTitle(book? book->name(): tr("Dorian"));
171 QAction *MainWindow::addToolBarAction(const QObject *receiver,
176 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
179 void MainWindow::showLibrary()
181 LibraryDialog *dialog = new LibraryDialog(this);
185 void MainWindow::showSettings()
187 SettingsWindow *settings = new SettingsWindow(this);
191 void MainWindow::showInfo()
193 if (mCurrent.isValid()) {
195 new InfoDialog(Library::instance()->book(mCurrent), this);
200 void MainWindow::showDevTools()
202 DevTools *devTools = new DevTools();
206 void MainWindow::showBookmarks()
208 Book *book = Library::instance()->book(mCurrent);
210 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
211 bookmarks->setWindowModality(Qt::WindowModal);
212 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
213 connect(bookmarks, SIGNAL(goToBookmark(int)),
214 this, SLOT(onGoToBookmark(int)));
219 void MainWindow::closeEvent(QCloseEvent *event)
221 Trace t("MainWindow::closeEvent");
222 view->setLastBookmark();
226 void MainWindow::onSettingsChanged(const QString &key)
228 Trace t("MainWindow::onSettingsChanged");
230 if (key == "orientation") {
231 QString value = Settings::instance()->value(key).toString();
232 if (value == "portrait") {
233 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
234 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
237 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
238 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
241 // FIXME: Orientation change should re-activate the window but it doesn't.
242 // And I have no idea how to force it
244 // view->restoreLastBookmark();
248 // QApplication::setActiveWindow(this);
250 // QEvent *enter = new QEvent(QEvent::Enter);
251 // QApplication::postEvent(view, enter);
253 // view->grabKeyboard();
256 // QTestEventList events;
257 // events.addMouseClick(Qt::LeftButton);
258 // events.simulate(view);
262 #endif // Q_WS_MAEMO_5
265 void MainWindow::onChapterLoadStart()
267 Trace t("MainWindow::onChapterLoadStart");
269 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
273 void MainWindow::onChapterLoadEnd(int index)
275 Trace t("MainWindow::onChapterLoadEnd");
276 bool enablePrevious = false;
277 bool enableNext = false;
278 Book *book = Library::instance()->book(mCurrent);
281 enablePrevious = true;
283 if (index < (book->toc.size() - 1)) {
288 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
289 previousAction->setIcon(QIcon(enablePrevious?
290 ":/icons/previous.png" : ":/icons/previous-disabled.png"));
291 nextAction->setIcon(QIcon(enableNext?
292 ":/icons/next.png": ":/icons/next-disabled.png"));
293 #endif // Q_WS_MAEMO_5
294 previousAction->setEnabled(enablePrevious);
295 nextAction->setEnabled(enableNext);
298 void MainWindow::onAddBookmark()
300 Trace t("MainWindow:onAddBookmark");
304 void MainWindow::onGoToBookmark(int index)
306 Trace t("MainWindow::onGoToBookmark");
307 Book *book = Library::instance()->book(mCurrent);
308 view->goToBookmark(book->bookmarks()[index]);
311 void MainWindow::showChapters()
313 Book *book = Library::instance()->book(mCurrent);
315 ChaptersDialog *chapters = new ChaptersDialog(book, this);
316 chapters->setWindowModality(Qt::WindowModal);
317 connect(chapters, SIGNAL(goToChapter(int)),
318 this, SLOT(onGoToChapter(int)));
323 void MainWindow::onGoToChapter(int index)
325 view->goToBookmark(Book::Bookmark(index, 0));