From d6cacd2fd1fa29d220a16739e9e2f86c4c95f6f1 Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Sat, 11 Jul 2015 01:35:13 +0300 Subject: [PATCH] Invert colors on double-click --- app/qml/BooksPageView.qml | 20 ++++++++++++++++- app/src/BooksPageWidget.cpp | 43 ++++++++++++++++++++++++++++++++----- app/src/BooksPageWidget.h | 5 +++++ app/src/BooksSettings.cpp | 18 ++++++++++++++++ app/src/BooksSettings.h | 6 ++++++ app/src/BooksTextView.cpp | 31 ++++++++++++++++++++------ app/src/BooksTextView.h | 12 +++++++---- app/src/ZLibrary.cpp | 6 +++++- fbreader/fbreader.pro | 9 +------- 9 files changed, 125 insertions(+), 25 deletions(-) diff --git a/app/qml/BooksPageView.qml b/app/qml/BooksPageView.qml index 2256b03..900e3ac 100644 --- a/app/qml/BooksPageView.qml +++ b/app/qml/BooksPageView.qml @@ -100,8 +100,26 @@ Item { Behavior on opacity { FadeAnimation {} } } + // For some reason this MouseArea receives clicks but not double-click + // events. It was easier to reimplement double-click functionality than + // to figure out what's wrong with double clicks. MouseArea { anchors.fill: parent - onClicked: view.pageClicked() + onClicked: { + if (!clickTimer.running) { + // If there're no more clicks within DoubleClickInterval + // then it's a single click + clickTimer.start() + } else { + // Otherwise it's a double click + clickTimer.stop() + settings.invertColors = !settings.invertColors + } + } + Timer { + id: clickTimer + interval: DoubleClickInterval + onTriggered: view.pageClicked() + } } } diff --git a/app/src/BooksPageWidget.cpp b/app/src/BooksPageWidget.cpp index f66a997..7ecf47d 100644 --- a/app/src/BooksPageWidget.cpp +++ b/app/src/BooksPageWidget.cpp @@ -51,7 +51,7 @@ public: bool paint(QPainter* aPainter); public: - shared_ptr iView; + shared_ptr iView; shared_ptr iModel; BooksPaintContext iPaintContext; }; @@ -169,7 +169,7 @@ BooksPageWidget::BooksPageWidget(QQuickItem* aParent) : iPage(-1) { setFlag(ItemHasContents, true); - setFillColor(Qt::white); + setFillColor(qtColor(BooksTextView::DEFAULT_BACKGROUND)); iResizeTimer->setSingleShot(true); iResizeTimer->setInterval(0); connect(iResizeTimer, SIGNAL(timeout()), SLOT(onResizeTimeout())); @@ -220,17 +220,36 @@ void BooksPageWidget::setModel(BooksBookModel* aModel) void BooksPageWidget::setSettings(BooksSettings* aSettings) { if (iSettings != aSettings) { + const bool colorsWereInverted = invertColors(); shared_ptr oldTextStyle(iTextStyle); if (iSettings) iSettings->disconnect(this); iSettings = aSettings; if (iSettings) { iTextStyle = iSettings->textStyle(); - connect(iSettings, SIGNAL(textStyleChanged()), SLOT(onTextStyleChanged())); + connect(iSettings, + SIGNAL(textStyleChanged()), + SLOT(onTextStyleChanged())); + connect(iSettings, + SIGNAL(invertColorsChanged()), + SLOT(onInvertColorsChanged())); } else { iTextStyle = BooksTextStyle::defaults(); } + const bool colorsAreInverted = invertColors(); + if (colorsWereInverted != colorsAreInverted) { + setFillColor(qtColor(colorsAreInverted ? + BooksTextView::INVERTED_BACKGROUND : + BooksTextView::DEFAULT_BACKGROUND)); + } if (!BooksTextStyle::equalLayout(oldTextStyle, iTextStyle)) { resetView(); + } else if (colorsWereInverted != colorsAreInverted) { + if (!iData.isNull() && !iData->iView.isNull()) { + iData->iView->setInvertColors(colorsAreInverted); + scheduleRepaint(); + } else { + update(); + } } Q_EMIT settingsChanged(); } @@ -239,10 +258,21 @@ void BooksPageWidget::setSettings(BooksSettings* aSettings) void BooksPageWidget::onTextStyleChanged() { HDEBUG(iPage); + HASSERT(sender() == iSettings); iTextStyle = iSettings->textStyle(); resetView(); } +void BooksPageWidget::onInvertColorsChanged() +{ + HDEBUG(iPage); + HASSERT(sender() == iSettings); + if (!iData.isNull() && !iData->iView.isNull()) { + iData->iView->setInvertColors(iSettings->invertColors()); + scheduleRepaint(); + } +} + void BooksPageWidget::onBookModelChanged() { HDEBUG(iModel->title()); @@ -402,8 +432,11 @@ void BooksPageWidget::scheduleRepaint() { BooksLoadingSignalBlocker block(this); cancelRepaint(); - if (width() > 0 && height() > 0 && !iData.isNull()) { - iRenderTask = new RenderTask(iData, width(), height()); + const int w = width(); + const int h = height(); + if (w > 0 && h > 0 && !iData.isNull() && !iData->iView.isNull()) { + iData->iView->setInvertColors(invertColors()); + iRenderTask = new RenderTask(iData, w, h); iTaskQueue->submit(iRenderTask, this, SLOT(onRenderTaskDone())); } else { update(); diff --git a/app/src/BooksPageWidget.h b/app/src/BooksPageWidget.h index 79576c5..8d343d3 100644 --- a/app/src/BooksPageWidget.h +++ b/app/src/BooksPageWidget.h @@ -109,6 +109,7 @@ private Q_SLOTS: void onBookModelPageMarksChanged(); void onBookModelLoadingChanged(); void onTextStyleChanged(); + void onInvertColorsChanged(); void onResetTaskDone(); void onRenderTaskDone(); @@ -118,6 +119,7 @@ private: void resetView(); void scheduleRepaint(); void cancelRepaint(); + bool invertColors() const; private: class ResetTask; @@ -138,4 +140,7 @@ private: int iPage; }; +inline bool BooksPageWidget::invertColors() const + { return iSettings && iSettings->invertColors(); } + #endif // BOOKS_PAGE_WIDGET_H diff --git a/app/src/BooksSettings.cpp b/app/src/BooksSettings.cpp index 9cc7c3d..9833ba6 100644 --- a/app/src/BooksSettings.cpp +++ b/app/src/BooksSettings.cpp @@ -43,9 +43,11 @@ #define KEY_FONT_SIZE "fontSize" #define KEY_PAGE_DETAILS "pageDetails" #define KEY_CURRENT_BOOK "currentBook" +#define KEY_INVERT_COLORS "invertColors" #define DEFAULT_FONT_SIZE 0 #define DEFAULT_PAGE_DETAILS 0 #define DEFAULT_CURRENT_BOOK QString() +#define DEFAULT_INVERT_COLORS false // ========================================================================== // BooksSettings::TextStyle @@ -191,6 +193,7 @@ BooksSettings::BooksSettings(QObject* aParent) : QObject(aParent), iFontSize(new MGConfItem(DCONF_PATH KEY_FONT_SIZE, this)), iPageDetails(new MGConfItem(DCONF_PATH KEY_PAGE_DETAILS, this)), + iInvertColors(new MGConfItem(DCONF_PATH KEY_INVERT_COLORS, this)), iCurrentBookPath(new MGConfItem(DCONF_PATH KEY_CURRENT_BOOK, this)), iCurrentBook(NULL) { @@ -198,6 +201,7 @@ BooksSettings::BooksSettings(QObject* aParent) : updateCurrentBook(); connect(iFontSize, SIGNAL(valueChanged()), SLOT(onFontSizeValueChanged())); connect(iPageDetails, SIGNAL(valueChanged()), SIGNAL(pageDetailsChanged())); + connect(iInvertColors, SIGNAL(valueChanged()), SIGNAL(invertColorsChanged())); connect(iCurrentBookPath, SIGNAL(valueChanged()), SLOT(onCurrentBookPathChanged())); } @@ -239,6 +243,20 @@ BooksSettings::setPageDetails( iPageDetails->set(aValue); } +bool +BooksSettings::invertColors() const +{ + return iInvertColors->value(DEFAULT_INVERT_COLORS).toBool(); +} + +void +BooksSettings::setInvertColors( + bool aValue) +{ + HDEBUG(aValue); + iInvertColors->set(aValue); +} + QObject* BooksSettings::currentBook() const { diff --git a/app/src/BooksSettings.h b/app/src/BooksSettings.h index 586cd63..074e237 100644 --- a/app/src/BooksSettings.h +++ b/app/src/BooksSettings.h @@ -46,6 +46,7 @@ class BooksSettings : public QObject Q_ENUMS(FontSize) Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged) Q_PROPERTY(int pageDetails READ pageDetails WRITE setPageDetails NOTIFY pageDetailsChanged) + Q_PROPERTY(bool invertColors READ invertColors WRITE setInvertColors NOTIFY invertColorsChanged) Q_PROPERTY(QObject* currentBook READ currentBook WRITE setCurrentBook NOTIFY currentBookChanged) class TextStyle; @@ -66,6 +67,9 @@ public: shared_ptr textStyle() const { return iTextStyle; } + bool invertColors() const; + void setInvertColors(bool aValue); + QObject* currentBook() const; void setCurrentBook(QObject* aBook); @@ -73,6 +77,7 @@ signals: void fontSizeChanged(); void textStyleChanged(); void pageDetailsChanged(); + void invertColorsChanged(); void currentBookChanged(); private Q_SLOTS: @@ -86,6 +91,7 @@ private: private: MGConfItem* iFontSize; MGConfItem* iPageDetails; + MGConfItem* iInvertColors; MGConfItem* iCurrentBookPath; shared_ptr iTextStyle; BooksBook* iCurrentBook; diff --git a/app/src/BooksTextView.cpp b/app/src/BooksTextView.cpp index 2d9fddf..2b11444 100644 --- a/app/src/BooksTextView.cpp +++ b/app/src/BooksTextView.cpp @@ -34,17 +34,18 @@ #include "BooksTextView.h" #include "BooksTextStyle.h" -#include "options/FBOptions.h" - #define SUPER ZLTextView +const ZLColor BooksTextView::DEFAULT_BACKGROUND(255, 255, 255); +const ZLColor BooksTextView::INVERTED_BACKGROUND(0, 0, 0); + BooksTextView::BooksTextView( ZLPaintContext& aContext, shared_ptr aTextStyle, BooksMargins aMargins) : SUPER(aContext), iMargins(aMargins), - iBackgroundColor(255, 255, 255), + iInvertColors(false), iTextStyle(aTextStyle) { } @@ -81,12 +82,30 @@ int BooksTextView::bottomMargin() const ZLColor BooksTextView::backgroundColor() const { - return iBackgroundColor; + return iInvertColors ? INVERTED_BACKGROUND : DEFAULT_BACKGROUND; } -ZLColor BooksTextView::color(const std::string &colorStyle) const +ZLColor BooksTextView::color(const std::string &aStyle) const { - return FBOptions::Instance().colorOption(colorStyle).value(); + static const std::string INTERNAL_HYPERLINK("internal"); + static const std::string EXTERNAL_HYPERLINK("external"); + static const std::string BOOK_HYPERLINK("book"); + + if (aStyle == INTERNAL_HYPERLINK) { + return ZLColor(33, 96, 180); + } else if (aStyle == EXTERNAL_HYPERLINK) { + return ZLColor(33, 96, 180); + } else if (aStyle == BOOK_HYPERLINK) { + return ZLColor(23, 68, 128); + } else if (aStyle == ZLTextStyle::SELECTION_BACKGROUND) { + return ZLColor(82, 131, 194); + } else if (aStyle == ZLTextStyle::HIGHLIGHTED_TEXT) { + return ZLColor(60, 139, 255); + } else if (iInvertColors) { + return ZLColor(255, 255, 255); + } else { + return ZLColor(0, 0, 0); + } } shared_ptr BooksTextView::baseStyle() const diff --git a/app/src/BooksTextView.h b/app/src/BooksTextView.h index 8a35be1..32c9d99 100644 --- a/app/src/BooksTextView.h +++ b/app/src/BooksTextView.h @@ -50,9 +50,13 @@ public: shared_ptr aTextStyle, BooksMargins aMargin); + static const ZLColor DEFAULT_BACKGROUND; + static const ZLColor INVERTED_BACKGROUND; + public: BooksPos position() const; const BooksPos rewind(); + void setInvertColors(bool aInvertColors); void gotoPosition(const BooksPos& aPos); bool nextPage(); void paint(); @@ -74,16 +78,16 @@ public: virtual ZLColor color(const std::string &style = std::string()) const; virtual bool isSelectionEnabled() const; -public: - BooksMargins iMargins; - ZLColor iBackgroundColor; - private: + BooksMargins iMargins; + bool iInvertColors; std::string iCaption; shared_ptr iTextStyle; }; inline BooksPos BooksTextView::position() const { return BooksPos(textArea().startCursor()); } +inline void BooksTextView::setInvertColors(bool aInvertColors) + { iInvertColors = aInvertColors; } #endif // BOOKS_TEXT_VIEW_H diff --git a/app/src/ZLibrary.cpp b/app/src/ZLibrary.cpp index d5007f5..5a1e748 100644 --- a/app/src/ZLibrary.cpp +++ b/app/src/ZLibrary.cpp @@ -166,7 +166,11 @@ void ZLibrary::run(ZLApplication* aApp) QQuickView* view = SailfishApp::createView(); view->setSource(QUrl::fromLocalFile(qml)); - view->rootContext()->setContextProperty("PointsPerInch", BOOKS_PPI); + + QQmlContext* root = view->rootContext(); + root->setContextProperty("PointsPerInch", BOOKS_PPI); + root->setContextProperty("DoubleClickInterval", + qApp->styleHints()->mouseDoubleClickInterval()); view->show(); HDEBUG("started"); diff --git a/fbreader/fbreader.pro b/fbreader/fbreader.pro index 6b0bddd..2c1503d 100644 --- a/fbreader/fbreader.pro +++ b/fbreader/fbreader.pro @@ -273,14 +273,10 @@ FBREADER_SRC = $$FBREADER_DIR/fbreader/src SOURCES += \ $$FBREADER_SRC/migration/BookInfo.cpp \ - $$FBREADER_SRC/options/FBOptions.cpp \ $$FBREADER_SRC/options/FBCategoryKey.cpp \ - $$FBREADER_SRC/fbreader/PreferencesPopupData.cpp \ - $$FBREADER_SRC/fbreader/TimeUpdater.cpp \ $$FBREADER_SRC/fbreader/SearchActions.cpp \ $$FBREADER_SRC/fbreader/ContentsView.cpp \ $$FBREADER_SRC/fbreader/BookTextView.cpp \ - $$FBREADER_SRC/fbreader/RecentBooksPopupData.cpp \ $$FBREADER_SRC/library/Book.cpp \ $$FBREADER_SRC/library/Comparators.cpp \ $$FBREADER_SRC/library/Library.cpp \ @@ -373,14 +369,11 @@ SOURCES += \ HEADERS += \ $$FBREADER_SRC/migration/BookInfo.h \ - $$FBREADER_SRC/options/FBOptions.h \ + $$FBREADER_SRC/options/FBCategoryKey.h \ $$FBREADER_SRC/fbreader/FBView.h \ $$FBREADER_SRC/fbreader/ReadingState.h \ $$FBREADER_SRC/fbreader/FBReaderActions.h \ - $$FBREADER_SRC/fbreader/PreferencesPopupData.h \ - $$FBREADER_SRC/fbreader/TimeUpdater.h \ $$FBREADER_SRC/fbreader/ContentsView.h \ - $$FBREADER_SRC/fbreader/RecentBooksPopupData.h \ $$FBREADER_SRC/fbreader/FootnoteView.h \ $$FBREADER_SRC/fbreader/BookTextView.h \ $$FBREADER_SRC/library/Author.h \