4 #include <QCoreApplication>
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"
23 #ifdef DORIAN_TEST_MODEL
24 #include "modeltest.h"
28 # define ICON_PREFIX ":/icons/mac/"
30 # define ICON_PREFIX ":/icons/"
33 const Qt::WindowFlags WIN_BIG_FLAGS =
34 Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint;
35 const int WIN_BIG_TIMER = 3000;
37 MainWindow::MainWindow(QWidget *parent):
38 QMainWindow(parent), view(0), fullScreenWindow(0)
41 setAttribute(Qt::WA_Maemo5StackedWindow, true);
43 setWindowTitle("Dorian");
46 view = new BookView(this);
47 setCentralWidget(view);
50 setUnifiedTitleAndToolBarOnMac(true);
51 settings = new QDialog(this);
52 toolBar = addToolBar("controls");
53 toolBar->setMovable(false);
54 toolBar->setFloatable(false);
55 toolBar->toggleViewAction()->setVisible(false);
56 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
57 toolBar->setIconSize(QSize(42, 42));
60 previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
61 nextAction = addToolBarAction(view, SLOT(goNext()), "next");
62 chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
63 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
66 infoAction = menuBar()->addAction(tr("Book details"));
67 connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
68 libraryAction = menuBar()->addAction(tr("Library"));
69 connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
70 settingsAction = menuBar()->addAction(tr("Settings"));
71 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
72 devToolsAction = menuBar()->addAction(tr("Developer"));
73 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
75 infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
76 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
77 "system-file-manager");
78 settingsAction = addToolBarAction(this, SLOT(showSettings()),
79 "preferences-system");
80 devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
81 #endif // Q_WS_MAEMO_5
83 QFrame *frame = new QFrame(toolBar);
84 frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
85 toolBar->addWidget(frame);
87 fullScreenAction = addToolBarAction(this, SLOT(showFullScreen()),
90 // Handle model changes
91 connect(Library::instance(), SIGNAL(nowReadingChanged()),
92 this, SLOT(onCurrentBookChanged()));
94 // Load book on command line, or load last read book, or load default book
95 Library *library = Library::instance();
96 if (QCoreApplication::arguments().size() == 2) {
97 QString path = QCoreApplication::arguments()[1];
99 QModelIndex index = library->find(path);
100 if (index.isValid()) {
101 library->setNowReading(index);
105 QModelIndex index = library->nowReading();
106 if (index.isValid()) {
107 library->setNowReading(index);
110 if (!library->rowCount()) {
111 library->add(":/books/2 B R 0 2 B.epub");
113 library->setNowReading(library->index(0));
117 // Handle settings changes
118 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
119 this, SLOT(onSettingsChanged(const QString &)));
120 Settings::instance()->setValue("orientation",
121 Settings::instance()->value("orientation"));
123 // Handle loading chapters
124 connect(view, SIGNAL(chapterLoadStart(int)),
125 this, SLOT(onChapterLoadStart()));
126 connect(view, SIGNAL(chapterLoadEnd(int)),
127 this, SLOT(onChapterLoadEnd(int)));
129 #ifdef DORIAN_TEST_MODEL
130 (void)new ModelTest(Library::instance(), this);
134 void MainWindow::onCurrentBookChanged()
136 setCurrentBook(Library::instance()->nowReading());
139 void MainWindow::showNormal()
141 qDebug() << "MainWindow::showNormal";
142 view->setParent(this);
143 setCentralWidget(view);
145 delete fullScreenWindow;
146 fullScreenWindow = 0;
149 void MainWindow::showFullScreen()
151 qDebug() << "MainWindow::showFullscreen";
152 fullScreenWindow = new FullScreenWindow(view, this);
153 fullScreenWindow->showFullScreen();
154 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showNormal()));
157 void MainWindow::setCurrentBook(const QModelIndex ¤t)
160 Book *book = Library::instance()->book(current);
162 setWindowTitle(book? book->name(): tr("Dorian"));
165 QAction *MainWindow::addToolBarAction(const QObject *receiver,
170 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
173 void MainWindow::showLibrary()
175 LibraryDialog *dialog = new LibraryDialog(this);
179 void MainWindow::showSettings()
181 SettingsWindow *settings = new SettingsWindow(this);
185 void MainWindow::showInfo()
187 if (mCurrent.isValid()) {
189 new InfoDialog(Library::instance()->book(mCurrent), this);
194 void MainWindow::showDevTools()
196 DevTools *devTools = new DevTools();
200 void MainWindow::showBookmarks()
202 Book *book = Library::instance()->book(mCurrent);
204 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
205 bookmarks->setWindowModality(Qt::WindowModal);
206 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
207 connect(bookmarks, SIGNAL(goToBookmark(int)),
208 this, SLOT(onGoToBookmark(int)));
213 void MainWindow::closeEvent(QCloseEvent *event)
215 qDebug() << "MainWindow::closeEvent";
216 view->setLastBookmark();
220 void MainWindow::onSettingsChanged(const QString &key)
223 if (key == "orientation") {
224 QString value = Settings::instance()->value(key).toString();
225 if (value == "portrait") {
226 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
227 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
230 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
231 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
236 #endif // Q_WS_MAEMO_5
239 void MainWindow::onChapterLoadStart()
242 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
246 void MainWindow::onChapterLoadEnd(int index)
248 bool enablePrevious = false;
249 bool enableNext = false;
250 Book *book = Library::instance()->book(mCurrent);
253 enablePrevious = true;
255 if (index < (book->toc.size() - 1)) {
260 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
261 previousAction->setIcon(QIcon(enablePrevious?
262 ":/icons/previous.png" : ":/icons/previous-disabled.png"));
263 nextAction->setIcon(QIcon(enableNext?
264 ":/icons/next.png": ":/icons/next-disabled.png"));
265 #endif // Q_WS_MAEMO_5
266 previousAction->setEnabled(enablePrevious);
267 nextAction->setEnabled(enableNext);
270 void MainWindow::onAddBookmark()
275 void MainWindow::onGoToBookmark(int index)
277 Book *book = Library::instance()->book(mCurrent);
278 view->goToBookmark(book->bookmarks()[index]);
281 void MainWindow::showChapters()
283 Book *book = Library::instance()->book(mCurrent);
285 ChaptersDialog *chapters = new ChaptersDialog(book, this);
286 chapters->setWindowModality(Qt::WindowModal);
287 connect(chapters, SIGNAL(goToChapter(int)),
288 this, SLOT(onGoToChapter(int)));
293 void MainWindow::onGoToChapter(int index)
295 view->goToBookmark(Book::Bookmark(index, 0));