Improve initialization order.
authorAkos Polster <akos@pipacs.com>
Mon, 8 Nov 2010 20:26:48 +0000 (21:26 +0100)
committerAkos Polster <akos@pipacs.com>
Mon, 8 Nov 2010 20:26:48 +0000 (21:26 +0100)
adopterwindow.cpp
bookview.cpp
main.cpp
mainwindow.cpp
mainwindow.h
model/settings.cpp
model/settings.h
platform.cpp
platform.h
settingswindow.cpp
settingswindow.h

index 27d5dbf..3775155 100644 (file)
@@ -13,7 +13,8 @@
 #include "platform.h"
 #include "settings.h"
 
-AdopterWindow::AdopterWindow(QWidget *parent): QMainWindow(parent), bookView(0)
+AdopterWindow::AdopterWindow(QWidget *parent):
+    QMainWindow(parent), bookView(0), grabbingVolumeKeys(false)
 {
     TRACE;
 
@@ -45,10 +46,8 @@ AdopterWindow::AdopterWindow(QWidget *parent): QMainWindow(parent), bookView(0)
 #endif // Q_OS_SYMBIAN
 
     // Monitor settings
-    Settings *settings = Settings::instance();
-    connect(settings, SIGNAL(valueChanged(const QString &)),
+    connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
             this, SLOT(onSettingsChanged(const QString &)));
-    settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
 }
 
 void AdopterWindow::takeChildren(BookView *view, const QList<QWidget *> &others)
@@ -204,10 +203,10 @@ void AdopterWindow::keyPressEvent(QKeyEvent *event)
 
 void AdopterWindow::onSettingsChanged(const QString &key)
 {
-    TRACE;
     if (key == "usevolumekeys") {
-        qDebug() << key;
-        grabVolumeKeys(Settings::instance()->value(key).toBool());
+        bool grab = Settings::instance()->value(key, false).toBool();
+        qDebug() << "AdopterWindow::onSettingsChanged: usevolumekeys" << grab;
+        grabVolumeKeys(grab);
     }
 }
 
index e4b4773..503b40e 100644 (file)
@@ -58,13 +58,8 @@ BookView::BookView(QWidget *parent): QWebView(parent), contentIndex(-1),
     bookmarkImage = QImage(":/icons/bookmark.png");
 
     // Handle settings changes, force handling initial settings
-    Settings *s = Settings::instance();
-    connect(s, SIGNAL(valueChanged(const QString &)),
+    connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
             this, SLOT(onSettingsChanged(const QString &)));
-    s->setValue("zoom", s->value("zoom", 160));
-    s->setValue("font", s->value("font", Platform::instance()->defaultFont()));
-    s->setValue("scheme", s->value("scheme", "default"));
-    s->setValue("usevolumekeys", s->value("usevolumekeys", false));
     setBook(0);
 
     // Enable kinetic scrolling
@@ -262,14 +257,17 @@ void BookView::restoreAfterLoad()
 
 void BookView::onSettingsChanged(const QString &key)
 {
-    TRACE;
-    qDebug() << key << Settings::instance()->value(key);
+    Settings *s = Settings::instance();
+    Platform *p = Platform::instance();
 
     if (key == "zoom") {
-        setZoomFactor(Settings::instance()->value(key).toFloat() / 100.);
+        int value = s->value(key, p->defaultZoom()).toInt();
+        qDebug() << "BookView::onSettingsChanged: zoom" << value;
+        setZoomFactor(value / 100.);
     }
     else if (key == "font") {
-        QString face = Settings::instance()->value(key).toString();
+        QString face = s->value(key, p->defaultFont()).toString();
+        qDebug() << "BookView::onSettingsChanged: font" << face;
         settings()->setFontFamily(QWebSettings::StandardFont, face);
     }
     else if (key == "scheme") {
@@ -279,14 +277,17 @@ void BookView::onSettingsChanged(const QString &key)
             (scheme != "default")) {
             scheme = "default";
         }
+        qDebug() << "BookView::onSettingsChanged: scheme" << scheme;
         QFile script(":/styles/" + scheme + ".js");
         script.open(QFile::ReadOnly);
         QString scriptText = script.readAll();
         script.close();
-        QVariant ret = frame->evaluateJavaScript(scriptText);
+        (void)frame->evaluateJavaScript(scriptText);
     }
     else if (key == "usevolumekeys") {
-        grabVolumeKeys(Settings::instance()->value(key).toBool());
+        bool grab = s->value(key, false).toBool();
+        qDebug() << "BookView::onSettingsChanged: usevolumekeys" << grab;
+        grabVolumeKeys(grab);
     }
 }
 
index 7de740b..3f56fbc 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -51,15 +51,16 @@ int main(int argc, char *argv[])
     a.processEvents();
 #endif
 
-    // Create main window, run event loop
-    {
-        MainWindow w;
+    // Create main window, re-apply settings, run event loop
+    MainWindow *mainWindow = new MainWindow();
+    settings->apply();
+    mainWindow->initialize();
 #ifdef Q_OS_SYMBIAN
-        splash->close();
-        splash->deleteLater();
+    splash->close();
+    splash->deleteLater();
 #endif
-        ret = a.exec();
-    }
+    ret = a.exec();
+    delete mainWindow;
 
     // Re-start application if event loop exit code was 1000
     if (ret == 1000) {
index 3cd94f7..42e7e77 100755 (executable)
@@ -118,34 +118,6 @@ MainWindow::MainWindow(QWidget *parent):
     connect(library, SIGNAL(upgrading(const QString &)),
             this, SLOT(onUpgrading(const QString &)));
     connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
-#ifndef Q_OS_SYMBIAN
-    connect(library, SIGNAL(beginLoad(int)), this, SLOT(onBeginLoad(int)));
-    connect(library, SIGNAL(loading(const QString &)),
-            this, SLOT(onLoading(const QString &)));
-    connect(library, SIGNAL(endLoad()), this, SLOT(onEndLoad()));
-#endif
-    library->upgrade();
-    library->load();
-
-    // Load book on command line, or load last read book, or load default book
-    if (QCoreApplication::arguments().size() == 2) {
-        QString path = QCoreApplication::arguments()[1];
-        library->add(path);
-        QModelIndex index = library->find(path);
-        if (index.isValid()) {
-            library->setNowReading(index);
-        }
-    } else {
-        QModelIndex index = library->nowReading();
-        if (index.isValid()) {
-            library->setNowReading(index);
-        } else {
-            if (!library->rowCount()) {
-                library->add(":/books/2BR02B.epub");
-            }
-            library->setNowReading(library->index(0));
-        }
-    }
 
     // Handle loading book parts
     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
@@ -159,11 +131,8 @@ MainWindow::MainWindow(QWidget *parent):
     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
 
     // Handle settings changes
-    Settings *settings = Settings::instance();
-    connect(settings, SIGNAL(valueChanged(const QString &)),
+    connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
             this, SLOT(onSettingsChanged(const QString &)));
-    settings->setValue("orientation", settings->value("orientation"));
-    settings->setValue("lightson", settings->value("lightson"));
 
     // Handle book view buttons
     connect(nextButton, SIGNAL(triggered()), this, SLOT(goToNextPage()));
@@ -177,6 +146,36 @@ MainWindow::MainWindow(QWidget *parent):
 #endif
 }
 
+void MainWindow::initialize()
+{
+    TRACE;
+    Library *library = Library::instance();
+
+    // Upgrade library if needed, then load it
+    library->upgrade();
+    library->load();
+
+    // Load book on command line, or load last read book, or load default book
+    if (QCoreApplication::arguments().size() == 2) {
+        QString path = QCoreApplication::arguments()[1];
+        library->add(path);
+        QModelIndex index = library->find(path);
+        if (index.isValid()) {
+            library->setNowReading(index);
+        }
+    } else {
+        QModelIndex index = library->nowReading();
+        if (index.isValid()) {
+            library->setNowReading(index);
+        } else {
+            if (!library->rowCount()) {
+                library->add(":/books/2BR02B.epub");
+            }
+            library->setNowReading(library->index(0));
+        }
+    }
+}
+
 void MainWindow::onCurrentBookChanged()
 {
     TRACE;
@@ -220,9 +219,6 @@ void MainWindow::showRegular()
     show();
 #if defined(Q_OS_SYMBIAN)
     activateWindow();
-#elif defined(Q_WS_MAEMO_5)
-    // FIXME: This is ugly.
-    // view->restoreLastBookmark();
 #endif
     progress->flash();
     nextButton->flash();
@@ -318,11 +314,11 @@ void MainWindow::closeEvent(QCloseEvent *event)
 
 void MainWindow::onSettingsChanged(const QString &key)
 {
-    TRACE;
-    qDebug() << key;
 #if defined(Q_WS_MAEMO_5)
     if (key == "orientation") {
-        QString value = Settings::instance()->value(key).toString();
+        QString value = Settings::instance()->value(key,
+            Platform::instance()->defaultOrientation()).toString();
+        qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
         if (value == "portrait") {
             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
@@ -332,6 +328,7 @@ void MainWindow::onSettingsChanged(const QString &key)
         }
     } else if (key == "lightson") {
         bool enable = Settings::instance()->value(key, false).toBool();
+        qDebug() << "MainWindow::onSettingsChanged: lightson" << enable;
         killTimer(preventBlankingTimer);
         if (enable) {
             preventBlankingTimer = startTimer(29 * 1000);
index e3d61d9..5de2120 100755 (executable)
@@ -22,6 +22,7 @@ class MainWindow: public AdopterWindow
 
 public:
     MainWindow(QWidget *parent = 0);
+    void initialize();
 
 public slots:
     void showLibrary();
index 62fdcec..810e15e 100644 (file)
@@ -1,11 +1,13 @@
 #include <QSettings>
+#include <QString>
+#include <QStringList>
 
 #include "settings.h"
+#include "trace.h"
 
 static Settings *theInstance;
 
-Settings::Settings(QObject *parent) :
-    QObject(parent)
+Settings::Settings(): QObject(0)
 {
 }
 
@@ -35,3 +37,15 @@ QVariant Settings::value(const QString &key, const QVariant &defaultValue) const
     QSettings s;
     return s.value(QString("settings/") + key, defaultValue);
 }
+
+void Settings::apply()
+{
+    TRACE;
+    QSettings s;
+    foreach (QString key, s.allKeys()) {
+        if (key.startsWith("settings/")) {
+            key = key.mid(9);
+            setValue(key, value(key));
+        }
+    }
+}
index ad09efe..793cbd4 100644 (file)
@@ -15,17 +15,18 @@ class Settings: public QObject
     Q_OBJECT
 
 public:
-    explicit Settings(QObject *parent = 0);
     static Settings *instance();
     static void close();
     QVariant value(const QString &key, const QVariant &defaultValue = QVariant())
         const;
     void setValue(const QString &key, const QVariant &value);
+    void apply();
 
 signals:
-    void valueChanged(const QString &name);
+    void valueChanged(const QString &key);
 
-public slots:
+protected:
+    Settings();
 };
 
 #endif // SETTINGS_H
index d2b8f3f..291a4b5 100644 (file)
@@ -123,3 +123,17 @@ QString Platform::traceFileName()
 {
     return QDir::home().absoluteFilePath(DORIAN_LOG);
 }
+
+int Platform::defaultZoom()
+{
+    return 150;
+}
+
+QString Platform::defaultOrientation()
+{
+#ifdef Q_OS_SYMBIAN
+    return QString("portrait");
+#else
+    return QString("landscape");
+#endif
+}
index b648ffc..00cdac1 100644 (file)
@@ -18,6 +18,8 @@ public:
     QString version();
     QString downloadDir();
     QString defaultFont();
+    int defaultZoom();
+    QString defaultOrientation();
     void information(const QString &label, QWidget *parent = 0);
     void showBusy(QWidget *w, bool isBusy);
     QString traceFileName();
index a5ac084..524dc7d 100644 (file)
@@ -7,13 +7,7 @@
 #include "trace.h"
 
 #ifdef Q_OS_SYMBIAN
-#include "flickcharm.h"
-#endif
-
-#ifdef Q_OS_SYMBIAN
-const char *DEFAULT_ORIENTATION = "portrait";
-#else
-const char *DEFAULT_ORIENTATION = "landscape";
+#   include "flickcharm.h"
 #endif
 
 const int ZOOM_MIN = 75;
@@ -29,6 +23,8 @@ SettingsWindow::SettingsWindow(QWidget *parent):  AdopterWindow(parent)
     setWindowTitle("Settings");
 
     Settings *settings = Settings::instance();
+    Platform *platform = Platform::instance();
+
     QScrollArea *scroller = new QScrollArea(this);
 #if defined(Q_WS_MAEMO_5)
     scroller->setProperty("FingerScrollable", true);
@@ -57,7 +53,7 @@ SettingsWindow::SettingsWindow(QWidget *parent):  AdopterWindow(parent)
     layout->addWidget(grabVolume);
     grabVolume->setChecked(settings->value("usevolumekeys", false).toBool());
 
-    int zoom = Settings::instance()->value("zoom").toInt();
+    int zoom = settings->value("zoom", platform->defaultZoom()).toInt();
     if (zoom < ZOOM_MIN) {
         zoom = ZOOM_MIN;
     } else if (zoom > ZOOM_MAX) {
@@ -75,9 +71,7 @@ SettingsWindow::SettingsWindow(QWidget *parent):  AdopterWindow(parent)
 
     QLabel *fontLabel = new QLabel(tr("Font:"), contents);
     layout->addWidget(fontLabel);
-    QString defaultFamily = fontLabel->fontInfo().family();
-    QString family =
-            Settings::instance()->value("font", defaultFamily).toString();
+    QString family = settings->value("font", platform->defaultFont()).toString();
     fontButton = new QFontComboBox(contents);
     fontButton->setCurrentFont(QFont(family));
     fontButton->setEditable(false);
@@ -117,7 +111,7 @@ SettingsWindow::SettingsWindow(QWidget *parent):  AdopterWindow(parent)
                               ":/icons/settings-landscape.png");
     orientationBox->addStretch();
     QString orientation =
-        settings->value("orientation", DEFAULT_ORIENTATION).toString();
+        settings->value("orientation", platform->defaultOrientation()).toString();
     if (orientation == "portrait") {
         orientationBox->toggle(OrientationPortrait);
     } else {
@@ -165,20 +159,11 @@ void SettingsWindow::onSliderValueChanged(int value)
         return;
     }
     zoomLabel->setText(tr("Zoom level: %1%").arg(value));
-#if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
-    // Constant re-scaling of the book view is too much for mobiles
-#else
-    Settings::instance()->setValue("zoom", value);
-#endif // Q_WS_MAEMO_5
 }
 
 void SettingsWindow::onCurrentFontChanged(const QFont &font)
 {
-#ifdef Q_WS_MAEMO_5
     Q_UNUSED(font);
-#else
-    Settings::instance()->setValue("font", font.family());
-#endif // Q_WS_MAEMO_5
 }
 
 void SettingsWindow::onSchemeButtonClicked(int id)
@@ -211,8 +196,6 @@ void SettingsWindow::onOrientationButtonClicked(int id)
 #endif // Q_WS_MAEMO_5
 }
 
-#if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
-
 void SettingsWindow::closeEvent(QCloseEvent *e)
 {
     TRACE;
@@ -227,8 +210,6 @@ void SettingsWindow::closeEvent(QCloseEvent *e)
     e->accept();
 }
 
-#endif // Q_WS_MAEMO_5 || Q_OS_SYMBIAN
-
 void SettingsWindow::onLightsToggled(bool value)
 {
     Settings::instance()->setValue("lightson", value);
index 5117698..6cf421f 100644 (file)
@@ -35,9 +35,7 @@ public slots:
     void onGrabVolumeToggled(bool enable);
 
 protected:
-#if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
     void closeEvent(QCloseEvent *e);
-#endif
 
 private:
     QSlider *zoomSlider;