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"
22 #ifdef DORIAN_TEST_MODEL
23 #include "modeltest.h"
27 # define ICON_PREFIX ":/icons/mac/"
29 # define ICON_PREFIX ":/icons/"
32 const Qt::WindowFlags WIN_BIG_FLAGS =
33 Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint;
34 const int WIN_BIG_TIMER = 3000;
36 MainWindow::MainWindow(QWidget *parent):
37 QMainWindow(parent), view(0), isFullscreen(false)
40 setAttribute(Qt::WA_Maemo5StackedWindow, true);
42 setWindowTitle("Dorian");
45 view = new BookView(this);
46 setCentralWidget(view);
49 setUnifiedTitleAndToolBarOnMac(true);
50 settings = new QDialog(this);
51 toolBar = addToolBar("controls");
52 toolBar->setMovable(false);
53 toolBar->setFloatable(false);
54 toolBar->toggleViewAction()->setVisible(false);
55 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
56 toolBar->setIconSize(QSize(42, 42));
58 previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
59 nextAction = addToolBarAction(view, SLOT(goNext()), "next");
60 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
63 infoAction = new QAction(this);
65 infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
67 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
68 "system-file-manager");
69 settingsAction = addToolBarAction(this, SLOT(showSettings()),
70 "preferences-system");
72 devToolsAction = new QAction(this);
74 devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
75 QFrame *frame = new QFrame(toolBar);
76 frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
77 toolBar->addWidget(frame);
79 fullScreenAction = addToolBarAction(this, SLOT(showFullScreen()),
82 // Handle model changes
83 connect(Library::instance(), SIGNAL(nowReadingChanged()),
84 this, SLOT(onCurrentBookChanged()));
86 normalFlags = windowFlags();
87 restoreButton = new TranslucentButton("view-fullscreen", this);
89 // Load book on command line, or load last read book, or load default book
90 Library *library = Library::instance();
91 if (QCoreApplication::arguments().size() == 2) {
92 QString path = QCoreApplication::arguments()[1];
94 QModelIndex index = library->find(path);
95 if (index.isValid()) {
96 library->setNowReading(index);
100 QModelIndex index = library->nowReading();
101 if (index.isValid()) {
102 library->setNowReading(index);
105 if (!library->rowCount()) {
106 library->add(":/books/2 B R 0 2 B.epub");
108 library->setNowReading(library->index(0));
112 // Handle settings changes
113 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
114 this, SLOT(onSettingsChanged(const QString &)));
115 Settings::instance()->setValue("orientation",
116 Settings::instance()->value("orientation"));
118 // Handle loading chapters
119 connect(view, SIGNAL(chapterLoaded(int)), this, SLOT(onChapterLoaded(int)));
121 #ifdef DORIAN_TEST_MODEL
122 (void)new ModelTest(Library::instance(), this);
126 void MainWindow::onCurrentBookChanged()
128 setCurrentBook(Library::instance()->nowReading());
131 void MainWindow::showNormal()
133 qDebug() << "MainWindow::showNormal";
134 isFullscreen = false;
135 setWindowFlags(normalFlags);
137 setGeometry(normalGeometry);
139 restoreButton->hide();
143 void MainWindow::showFullScreen()
145 qDebug() << "MainWindow::showFullscreen";
146 normalGeometry = geometry();
149 setWindowFlags(normalFlags | WIN_BIG_FLAGS);
151 restoreButton->flash();
154 void MainWindow::setCurrentBook(const QModelIndex ¤t)
157 Book *book = Library::instance()->book(current);
159 setWindowTitle(book? book->name(): tr("Dorian"));
162 QAction *MainWindow::addToolBarAction(const QObject *receiver,
167 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
170 void MainWindow::showLibrary()
172 LibraryDialog *dialog = new LibraryDialog();
176 void MainWindow::showSettings()
178 SettingsWindow *settings = new SettingsWindow(this);
182 void MainWindow::showInfo()
184 if (mCurrent.isValid()) {
186 new InfoDialog(Library::instance()->book(mCurrent), this);
191 void MainWindow::showDevTools()
193 DevTools *devTools = new DevTools();
197 void MainWindow::showBookmarks()
199 Book *book = Library::instance()->book(mCurrent);
201 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
202 int ret = bookmarks->exec();
205 view->goToBookmark(book->bookmarks()[index]);
213 void MainWindow::MOUSE_ACTIVATE_EVENT(QMouseEvent *event)
215 qDebug() << "MainWindow::mousePress/ReleaseEvent at" << event->pos()
216 << "against" << fullScreenZone();
217 if (isFullscreen && fullScreenZone().contains(event->x(), event->y())) {
218 qDebug() << " In fullScreenZone";
221 QMainWindow::MOUSE_ACTIVATE_EVENT(event);
224 QRect MainWindow::fullScreenZone() const
226 return QRect(width() / 2 - 45, height() - 104, 95, 95);
229 void MainWindow::resizeEvent(QResizeEvent *event)
232 restoreButton->setGeometry(fullScreenZone());
235 void MainWindow::closeEvent(QCloseEvent *event)
237 qDebug() << "MainWindow::closeEvent";
238 view->setLastBookmark();
242 void MainWindow::onSettingsChanged(const QString &key)
245 if (key == "orientation") {
246 QString value = Settings::instance()->value(key).toString();
247 if (value == "portrait") {
248 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
249 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
252 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
253 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
258 #endif // Q_WS_MAEMO_5
261 void MainWindow::onChapterLoaded(int index)
263 bool enablePrevious = false;
264 bool enableNext = false;
265 Book *book = Library::instance()->book(mCurrent);
268 enablePrevious = true;
270 if (index < (book->toc.size() - 1)) {
275 previousAction->setIcon(QIcon(enablePrevious?
276 ":/icons/previous.png" : ":/icons/previous-disabled.png"));
277 nextAction->setIcon(QIcon(enableNext?
278 ":/icons/next.png": ":/icons/next-disabled.png"));
279 #endif // Q_WS_MAEMO_5
280 previousAction->setEnabled(enablePrevious);
281 nextAction->setEnabled(enableNext);