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);
37 // setAttribute(Qt::WA_Maemo5AutoOrientation, true);
38 // FIXME: There is not enough space for the toolbar in portrait mode
40 setWindowTitle("Dorian");
43 view = new BookView(this);
44 setCentralWidget(view);
47 setUnifiedTitleAndToolBarOnMac(true);
48 settings = new QDialog(this);
49 toolBar = addToolBar("controls");
50 toolBar->setMovable(false);
51 toolBar->setFloatable(false);
52 toolBar->toggleViewAction()->setVisible(false);
53 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
54 toolBar->setIconSize(QSize(42, 42));
56 previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
57 nextAction = addToolBarAction(view, SLOT(goNext()), "next");
58 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
61 infoAction = new QAction(this);
63 infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
65 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
66 "system-file-manager");
67 settingsAction = addToolBarAction(this, SLOT(showSettings()),
68 "preferences-system");
70 devToolsAction = new QAction(this);
72 devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
73 QFrame *frame = new QFrame(toolBar);
74 frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
75 toolBar->addWidget(frame);
77 fullScreenAction = addToolBarAction(this, SLOT(showFullScreen()),
80 // Handle model changes
81 connect(Library::instance(), SIGNAL(currentBookChanged()),
82 this, SLOT(onCurrentBookChanged()));
84 normalFlags = windowFlags();
85 restoreButton = new TranslucentButton("view-fullscreen", this);
87 // Load book on command line, or load last read book, or load default book
88 Library *library = Library::instance();
89 if (QCoreApplication::arguments().size() == 2) {
90 QString path = QCoreApplication::arguments()[1];
92 int index = library->find(path);
94 library->setCurrent(index);
98 Book *current = library->current();
100 setCurrentBook(current);
103 if (!library->size()) {
104 library->add(":/books/2 B R 0 2 B.epub");
106 library->setCurrent(0);
110 // Handle settings changes
111 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
112 this, SLOT(onSettingsChanged(const QString &)));
113 Settings::instance()->setValue("orientation",
114 Settings::instance()->value("orientation"));
117 void MainWindow::onCurrentBookChanged()
119 setCurrentBook(Library::instance()->current());
122 void MainWindow::showNormal()
124 qDebug() << "MainWindow::showNormal";
125 isFullscreen = false;
126 setWindowFlags(normalFlags);
128 setGeometry(normalGeometry);
130 restoreButton->hide();
134 void MainWindow::showFullScreen()
136 qDebug() << "MainWindow::showFullscreen";
137 normalGeometry = geometry();
140 setWindowFlags(normalFlags | WIN_BIG_FLAGS);
142 restoreButton->flash();
145 void MainWindow::setCurrentBook(Book *current)
148 view->setBook(current);
149 setWindowTitle(current? current->title: "Dorian");
152 QAction *MainWindow::addToolBarAction(const QObject *receiver, const char *member,
156 addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
159 void MainWindow::showLibrary()
161 LibraryDialog *dialog = new LibraryDialog();
165 void MainWindow::showSettings()
167 SettingsWindow *settings = new SettingsWindow(this);
171 void MainWindow::showInfo()
174 InfoDialog *info = new InfoDialog(book, this);
179 void MainWindow::showDevTools()
181 DevTools *devTools = new DevTools();
185 void MainWindow::showBookmarks()
188 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
189 int ret = bookmarks->exec();
192 view->goToBookmark(book->bookmarks()[index]);
200 void MainWindow::MOUSE_ACTIVATE_EVENT(QMouseEvent *event)
202 qDebug() << "MainWindow::mousePress/ReleaseEvent at" << event->pos()
203 << "against" << fullScreenZone();
204 if (isFullscreen && fullScreenZone().contains(event->x(), event->y())) {
205 qDebug() << " In fullScreenZone";
208 QMainWindow::MOUSE_ACTIVATE_EVENT(event);
211 QRect MainWindow::fullScreenZone() const
213 return QRect(width() / 2 - 45, height() - 104, 95, 95);
216 void MainWindow::resizeEvent(QResizeEvent *event)
219 restoreButton->setGeometry(fullScreenZone());
222 void MainWindow::closeEvent(QCloseEvent *event)
224 qDebug() << "MainWindow::closeEvent";
225 view->setLastBookmark();
229 void MainWindow::onSettingsChanged(const QString &key)
232 if (key == "orientation") {
233 QString value = Settings::instance()->value(key).toString();
234 if (value == "portrait") {
235 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
236 setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
239 setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
240 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
245 #endif // Q_WS_MAEMO_5