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"
23 # define ICON_PREFIX ":/icons/mac/"
25 # define ICON_PREFIX ":/icons/"
28 const Qt::WindowFlags WIN_BIG_FLAGS =
29 Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint;
30 const int WIN_BIG_TIMER = 3000;
32 MainWindow::MainWindow(QWidget *parent):
33 QMainWindow(parent), view(0), book(0), isFullscreen(false)
36 setAttribute(Qt::WA_Maemo5StackedWindow, true);
38 setWindowTitle("Dorian");
41 view = new BookView(this);
42 setCentralWidget(view);
45 setUnifiedTitleAndToolBarOnMac(true);
46 settings = new QDialog(this);
47 toolBar = addToolBar("controls");
48 toolBar->setMovable(false);
49 toolBar->setFloatable(false);
50 toolBar->toggleViewAction()->setVisible(false);
51 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
52 toolBar->setIconSize(QSize(42, 42));
54 previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
55 nextAction = addToolBarAction(view, SLOT(goNext()), "next");
56 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
59 infoAction = new QAction(this);
61 infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
63 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
64 "system-file-manager");
65 settingsAction = addToolBarAction(this, SLOT(showSettings()),
66 "preferences-system");
68 devToolsAction = new QAction(this);
70 devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
71 QFrame *frame = new QFrame(toolBar);
72 frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
73 toolBar->addWidget(frame);
75 fullScreenAction = addToolBarAction(this, SLOT(showFullScreen()),
78 // Handle model changes
79 connect(Library::instance(), SIGNAL(currentBookChanged()),
80 this, SLOT(onCurrentBookChanged()));
82 normalFlags = windowFlags();
83 restoreButton = new TranslucentButton("view-fullscreen", this);
85 // Load book on command line, or load last read book, or load default book
86 Library *library = Library::instance();
87 if (QCoreApplication::arguments().size() == 2) {
88 QString path = QCoreApplication::arguments()[1];
90 QModelIndex index = library->find(path);
91 if (index.isValid()) {
92 library->setCurrent(index);
96 Book *current = library->current();
98 setCurrentBook(current);
101 if (!library->rowCount()) {
102 library->add(":/books/2 B R 0 2 B.epub");
104 library->setCurrent(library->index(0));
108 // Handle settings changes
109 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
110 this, SLOT(onSettingsChanged(const QString &)));
111 Settings::instance()->setValue("orientation",
112 Settings::instance()->value("orientation"));
114 // Handle loading chapters
115 connect(view, SIGNAL(chapterLoaded(int)), this, SLOT(onChapterLoaded(int)));
118 void MainWindow::onCurrentBookChanged()
120 setCurrentBook(Library::instance()->current());
123 void MainWindow::showNormal()
125 qDebug() << "MainWindow::showNormal";
126 isFullscreen = false;
127 setWindowFlags(normalFlags);
129 setGeometry(normalGeometry);
131 restoreButton->hide();
135 void MainWindow::showFullScreen()
137 qDebug() << "MainWindow::showFullscreen";
138 normalGeometry = geometry();
141 setWindowFlags(normalFlags | WIN_BIG_FLAGS);
143 restoreButton->flash();
146 void MainWindow::setCurrentBook(Book *current)
149 view->setBook(current);
150 setWindowTitle(current? current->title: "Dorian");
153 QAction *MainWindow::addToolBarAction(const QObject *receiver, const char *member,
157 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
160 void MainWindow::showLibrary()
162 LibraryDialog *dialog = new LibraryDialog();
166 void MainWindow::showSettings()
168 SettingsWindow *settings = new SettingsWindow(this);
172 void MainWindow::showInfo()
175 InfoDialog *info = new InfoDialog(book, this);
180 void MainWindow::showDevTools()
182 DevTools *devTools = new DevTools();
186 void MainWindow::showBookmarks()
189 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
190 int ret = bookmarks->exec();
193 view->goToBookmark(book->bookmarks()[index]);
201 void MainWindow::MOUSE_ACTIVATE_EVENT(QMouseEvent *event)
203 qDebug() << "MainWindow::mousePress/ReleaseEvent at" << event->pos()
204 << "against" << fullScreenZone();
205 if (isFullscreen && fullScreenZone().contains(event->x(), event->y())) {
206 qDebug() << " In fullScreenZone";
209 QMainWindow::MOUSE_ACTIVATE_EVENT(event);
212 QRect MainWindow::fullScreenZone() const
214 return QRect(width() / 2 - 45, height() - 104, 95, 95);
217 void MainWindow::resizeEvent(QResizeEvent *event)
220 restoreButton->setGeometry(fullScreenZone());
223 void MainWindow::closeEvent(QCloseEvent *event)
225 qDebug() << "MainWindow::closeEvent";
226 view->setLastBookmark();
230 void MainWindow::onSettingsChanged(const QString &key)
233 if (key == "orientation") {
234 QString value = Settings::instance()->value(key).toString();
235 if (value == "portrait") {
236 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
237 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
240 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
241 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
246 #endif // Q_WS_MAEMO_5
249 void MainWindow::onChapterLoaded(int index)
251 bool enablePrevious = false;
252 bool enableNext = false;
255 enablePrevious = true;
257 if (index < (book->toc.size() - 1)) {
262 previousAction->setIcon(QIcon(enablePrevious?
263 ":/icons/previous.png" : ":/icons/previous-disabled.png"));
264 nextAction->setIcon(QIcon(enableNext?
265 ":/icons/next.png": ":/icons/next-disabled.png"));
266 #endif // Q_WS_MAEMO_5
267 previousAction->setEnabled(enablePrevious);
268 nextAction->setEnabled(enableNext);