Invert colors on double-click

This commit is contained in:
Slava Monich 2015-07-11 01:35:13 +03:00
parent 0efb865586
commit d6cacd2fd1
9 changed files with 125 additions and 25 deletions

View file

@ -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()
}
}
}

View file

@ -51,7 +51,7 @@ public:
bool paint(QPainter* aPainter);
public:
shared_ptr<ZLView> iView;
shared_ptr<BooksTextView> iView;
shared_ptr<ZLTextModel> 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<ZLTextStyle> 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();

View file

@ -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

View file

@ -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
{

View file

@ -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<ZLTextStyle> 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<ZLTextStyle> iTextStyle;
BooksBook* iCurrentBook;

View file

@ -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<ZLTextStyle> 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<ZLTextStyle> BooksTextView::baseStyle() const

View file

@ -50,9 +50,13 @@ public:
shared_ptr<ZLTextStyle> 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<ZLTextStyle> iTextStyle;
};
inline BooksPos BooksTextView::position() const
{ return BooksPos(textArea().startCursor()); }
inline void BooksTextView::setInvertColors(bool aInvertColors)
{ iInvertColors = aInvertColors; }
#endif // BOOKS_TEXT_VIEW_H

View file

@ -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");

View file

@ -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 \