4 #include <QCoreApplication>
7 # include <QtMaemo5/QMaemo5InformationBox>
13 #include "infodialog.h"
14 #include "librarydialog.h"
16 #include "mainwindow.h"
17 #include "translucentbutton.h"
18 #include "settingswindow.h"
19 #include "bookmarksdialog.h"
21 #include "chaptersdialog.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), isFullscreen(false)
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 normalFlags = windowFlags();
95 restoreButton = new TranslucentButton("view-fullscreen", this);
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 #ifdef DORIAN_TEST_MODEL
133 (void)new ModelTest(Library::instance(), this);
137 void MainWindow::onCurrentBookChanged()
139 setCurrentBook(Library::instance()->nowReading());
142 void MainWindow::showNormal()
144 qDebug() << "MainWindow::showNormal";
145 isFullscreen = false;
146 setWindowFlags(normalFlags);
148 setGeometry(normalGeometry);
150 restoreButton->hide();
154 void MainWindow::showFullScreen()
156 qDebug() << "MainWindow::showFullscreen";
157 normalGeometry = geometry();
160 setWindowFlags(normalFlags | WIN_BIG_FLAGS);
162 restoreButton->flash();
165 void MainWindow::setCurrentBook(const QModelIndex ¤t)
168 Book *book = Library::instance()->book(current);
170 setWindowTitle(book? book->name(): tr("Dorian"));
173 QAction *MainWindow::addToolBarAction(const QObject *receiver,
178 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
181 void MainWindow::showLibrary()
183 LibraryDialog *dialog = new LibraryDialog(this);
187 void MainWindow::showSettings()
189 SettingsWindow *settings = new SettingsWindow(this);
193 void MainWindow::showInfo()
195 if (mCurrent.isValid()) {
197 new InfoDialog(Library::instance()->book(mCurrent), this);
202 void MainWindow::showDevTools()
204 DevTools *devTools = new DevTools();
208 void MainWindow::showBookmarks()
210 Book *book = Library::instance()->book(mCurrent);
212 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
213 bookmarks->setWindowModality(Qt::WindowModal);
214 connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
215 connect(bookmarks, SIGNAL(goToBookmark(int)),
216 this, SLOT(onGoToBookmark(int)));
221 void MainWindow::MOUSE_ACTIVATE_EVENT(QMouseEvent *event)
223 qDebug() << "MainWindow::mousePress/ReleaseEvent at" << event->pos()
224 << "against" << fullScreenZone();
225 if (isFullscreen && fullScreenZone().contains(event->x(), event->y())) {
226 qDebug() << " In fullScreenZone";
229 QMainWindow::MOUSE_ACTIVATE_EVENT(event);
232 QRect MainWindow::fullScreenZone() const
234 return QRect(width() / 2 - 45, height() - 104, 95, 95);
237 void MainWindow::resizeEvent(QResizeEvent *event)
240 restoreButton->setGeometry(fullScreenZone());
243 void MainWindow::closeEvent(QCloseEvent *event)
245 qDebug() << "MainWindow::closeEvent";
246 view->setLastBookmark();
250 void MainWindow::onSettingsChanged(const QString &key)
253 if (key == "orientation") {
254 QString value = Settings::instance()->value(key).toString();
255 if (value == "portrait") {
256 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
257 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
260 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
261 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
266 #endif // Q_WS_MAEMO_5
269 void MainWindow::onChapterLoadStart()
272 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
276 void MainWindow::onChapterLoadEnd(int index)
278 bool enablePrevious = false;
279 bool enableNext = false;
280 Book *book = Library::instance()->book(mCurrent);
283 enablePrevious = true;
285 if (index < (book->toc.size() - 1)) {
290 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
291 previousAction->setIcon(QIcon(enablePrevious?
292 ":/icons/previous.png" : ":/icons/previous-disabled.png"));
293 nextAction->setIcon(QIcon(enableNext?
294 ":/icons/next.png": ":/icons/next-disabled.png"));
295 #endif // Q_WS_MAEMO_5
296 previousAction->setEnabled(enablePrevious);
297 nextAction->setEnabled(enableNext);
300 void MainWindow::onAddBookmark()
305 void MainWindow::onGoToBookmark(int index)
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));