harbour-books/app/src/BooksSettings.cpp

407 lines
10 KiB
C++
Raw Normal View History

2015-06-28 14:22:35 +03:00
/*
* Copyright (C) 2015 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "BooksSettings.h"
#include "BooksTextStyle.h"
#include "BooksBook.h"
#include "BooksDefs.h"
#include "BooksUtil.h"
2015-06-28 14:22:35 +03:00
#include "HarbourDebug.h"
#include <MGConfItem>
2015-07-12 17:11:55 +03:00
#define DCONF_PATH BOOKS_DCONF_ROOT
2015-06-28 14:22:35 +03:00
#define KEY_FONT_SIZE "fontSize"
#define KEY_PAGE_DETAILS "pageDetails"
#define KEY_CURRENT_BOOK "currentBook"
#define KEY_CURRENT_FOLDER "currentFolder"
2015-07-11 01:35:13 +03:00
#define KEY_INVERT_COLORS "invertColors"
2015-06-28 14:22:35 +03:00
#define DEFAULT_FONT_SIZE 0
#define DEFAULT_PAGE_DETAILS 0
#define DEFAULT_CURRENT_BOOK QString()
#define DEFAULT_CURRENT_FOLDER QString()
2015-07-11 01:35:13 +03:00
#define DEFAULT_INVERT_COLORS false
2015-06-28 14:22:35 +03:00
#define PAGETOOL_COLOR QColor(128,128,128) // any bg
#define NORMAL_PAGETOOL_HIGHLIGHT_COLOR QColor(64,64,64) // on white
#define INVERTED_PAGETOOL_HIGHLIGHT_COLOR QColor(192,192,192) // on black
2015-06-28 14:22:35 +03:00
// ==========================================================================
// BooksSettings::TextStyle
// ==========================================================================
class BooksSettings::TextStyle : public ZLTextStyle
{
public:
TextStyle(int aFontSizeModifier) :
iDefaultStyle(BooksTextStyle::defaults()),
iFontSize(iDefaultStyle->fontSize() + 2*aFontSizeModifier)
{ HDEBUG(iFontSize); }
bool isDecorated() const;
const std::vector<std::string> &fontFamilies() const;
2015-06-28 14:22:35 +03:00
int fontSize() const;
bool bold() const;
bool italic() const;
const std::string &colorStyle() const;
short spaceBefore(const ZLTextStyleEntry::Metrics& aMetrics) const;
short spaceAfter(const ZLTextStyleEntry::Metrics& aMetrics) const;
short lineStartIndent(const ZLTextStyleEntry::Metrics& aMetrics, bool aRtl) const;
short lineEndIndent(const ZLTextStyleEntry::Metrics& aMetrics, bool aRtl) const;
short firstLineIndentDelta(const ZLTextStyleEntry::Metrics& aMetrics) const;
int verticalShift() const;
ZLTextAlignmentType alignment() const;
double lineSpace() const;
bool allowHyphenations() const;
private:
shared_ptr<ZLTextStyle> iDefaultStyle;
int iFontSize;
};
const std::string&
BooksSettings::TextStyle::colorStyle() const
{
return iDefaultStyle->colorStyle();
}
bool
BooksSettings::TextStyle::isDecorated() const
{
return iDefaultStyle->isDecorated();
}
const std::vector<std::string>&
BooksSettings::TextStyle::fontFamilies() const
2015-06-28 14:22:35 +03:00
{
return iDefaultStyle->fontFamilies();
2015-06-28 14:22:35 +03:00
}
int
BooksSettings::TextStyle::fontSize() const
{
return iFontSize;
}
bool
BooksSettings::TextStyle::bold() const
{
return iDefaultStyle->bold();
}
bool
BooksSettings::TextStyle::italic() const
{
return iDefaultStyle->italic();
}
short
BooksSettings::TextStyle::spaceBefore(
const ZLTextStyleEntry::Metrics& aMetrics) const
{
return iDefaultStyle->spaceBefore(aMetrics);
}
short
BooksSettings::TextStyle::spaceAfter(
const ZLTextStyleEntry::Metrics& aMetrics) const
{
return iDefaultStyle->spaceAfter(aMetrics);
}
short
BooksSettings::TextStyle::lineStartIndent(
const ZLTextStyleEntry::Metrics& aMetrics,
bool aRtl) const
{
return iDefaultStyle->lineStartIndent(aMetrics, aRtl);
}
short
BooksSettings::TextStyle::lineEndIndent(
const ZLTextStyleEntry::Metrics& aMetrics,
bool aRtl) const
{
return iDefaultStyle->lineEndIndent(aMetrics, aRtl);
}
short
BooksSettings::TextStyle::firstLineIndentDelta(
const ZLTextStyleEntry::Metrics& aMetrics) const
{
return iDefaultStyle->firstLineIndentDelta(aMetrics);
}
int
BooksSettings::TextStyle::verticalShift() const
{
return iDefaultStyle->verticalShift();
}
ZLTextAlignmentType
BooksSettings::TextStyle::alignment() const
{
return iDefaultStyle->alignment();
}
double
BooksSettings::TextStyle::lineSpace() const
{
return iDefaultStyle->lineSpace();
}
bool
BooksSettings::TextStyle::allowHyphenations() const
{
return iDefaultStyle->allowHyphenations();
}
// ==========================================================================
// BooksSettings
// ==========================================================================
BooksSettings::BooksSettings(QObject* aParent) :
QObject(aParent),
iFontSize(new MGConfItem(DCONF_PATH KEY_FONT_SIZE, this)),
iPageDetails(new MGConfItem(DCONF_PATH KEY_PAGE_DETAILS, this)),
2015-07-11 01:35:13 +03:00
iInvertColors(new MGConfItem(DCONF_PATH KEY_INVERT_COLORS, this)),
iCurrentFolder(new MGConfItem(DCONF_PATH KEY_CURRENT_FOLDER, this)),
2015-06-28 14:22:35 +03:00
iCurrentBookPath(new MGConfItem(DCONF_PATH KEY_CURRENT_BOOK, this)),
iCurrentBook(NULL)
{
iTextStyle = new TextStyle(fontSize());
updateCurrentBook();
updateCurrentStorage();
2015-06-28 14:22:35 +03:00
connect(iFontSize, SIGNAL(valueChanged()), SLOT(onFontSizeValueChanged()));
connect(iPageDetails, SIGNAL(valueChanged()), SIGNAL(pageDetailsChanged()));
2015-07-11 01:35:13 +03:00
connect(iInvertColors, SIGNAL(valueChanged()), SIGNAL(invertColorsChanged()));
connect(iCurrentFolder, SIGNAL(valueChanged()), SLOT(onCurrentFolderChanged()));
2015-06-28 14:22:35 +03:00
connect(iCurrentBookPath, SIGNAL(valueChanged()), SLOT(onCurrentBookPathChanged()));
}
bool
BooksSettings::increaseFontSize()
{
int size = fontSize();
if (size < MaxFontSize) {
setFontSize(size+1);
return true;
} else {
return false;
}
}
bool
BooksSettings::decreaseFontSize()
{
int size = fontSize();
if (size > MinFontSize) {
setFontSize(size-1);
return true;
} else {
return false;
}
}
2015-06-28 14:22:35 +03:00
int
BooksSettings::fontSize() const
{
return iFontSize->value(DEFAULT_FONT_SIZE).toInt();
}
void
BooksSettings::setFontSize(
int aValue)
{
HDEBUG(aValue);
iFontSize->set(aValue);
}
void
BooksSettings::onFontSizeValueChanged()
{
const int newSize = fontSize();
HDEBUG(newSize);
iTextStyle = new TextStyle(newSize);
Q_EMIT fontSizeChanged();
Q_EMIT textStyleChanged();
}
int
BooksSettings::pageDetails() const
{
return iPageDetails->value(DEFAULT_PAGE_DETAILS).toInt();
}
void
BooksSettings::setPageDetails(
int aValue)
{
HDEBUG(aValue);
iPageDetails->set(aValue);
}
2015-07-11 01:35:13 +03:00
bool
BooksSettings::invertColors() const
{
return iInvertColors->value(DEFAULT_INVERT_COLORS).toBool();
}
void
BooksSettings::setInvertColors(
bool aValue)
{
HDEBUG(aValue);
iInvertColors->set(aValue);
}
QString
BooksSettings::currentFolder() const
{
return iCurrentFolder->value(DEFAULT_CURRENT_FOLDER).toString();
}
void
BooksSettings::setCurrentFolder(
QString aValue)
{
HDEBUG(aValue);
iCurrentFolder->set(aValue);
}
void
BooksSettings::onCurrentFolderChanged()
{
if (updateCurrentStorage()) {
Q_EMIT currentStorageChanged();
}
Q_EMIT currentFolderChanged();
}
bool
BooksSettings::updateCurrentStorage()
{
BooksStorageManager* mgr = BooksStorageManager::instance();
BooksStorage storage = mgr->storageForPath(currentFolder());
if (storage.isValid() && storage.device() != iCurrentStorageDevice) {
iCurrentStorageDevice = storage.device();
return true;
}
return false;
}
2015-06-28 14:22:35 +03:00
QObject*
BooksSettings::currentBook() const
{
return iCurrentBook;
}
void
BooksSettings::setCurrentBook(QObject* aBook)
{
BooksBook* book = qobject_cast<BooksBook*>(aBook);
if (iCurrentBook != book) {
if (iCurrentBook) iCurrentBook->release();
if (book) {
HDEBUG(book->path());
(iCurrentBook = book)->retain();
iCurrentBookPath->set(book->path());
} else {
iCurrentBook = NULL;
iCurrentBookPath->set(QString());
}
Q_EMIT currentBookChanged();
}
}
bool
BooksSettings::updateCurrentBook()
{
QString path = iCurrentBookPath->value(DEFAULT_CURRENT_BOOK).toString();
if (path.isEmpty()) {
if (iCurrentBook) {
iCurrentBook->release();
iCurrentBook = NULL;
return true;
}
} else if (!iCurrentBook || iCurrentBook->path() != path) {
shared_ptr<Book> book = BooksUtil::bookFromFile(path);
2015-06-28 14:22:35 +03:00
if (!book.isNull()) {
QFileInfo info(path);
BooksStorageManager* mgr = BooksStorageManager::instance();
BooksStorage storage = mgr->storageForPath(info.path());
if (storage.isValid()) {
if (iCurrentBook) iCurrentBook->release();
iCurrentBook = new BooksBook(storage, book);
iCurrentBook->requestCoverImage();
return true;
}
}
if (iCurrentBook) {
iCurrentBook->release();
iCurrentBook = NULL;
return true;
}
}
return false;
}
void
BooksSettings::onCurrentBookPathChanged()
{
if (updateCurrentBook()) {
Q_EMIT currentBookChanged();
}
}
QColor
BooksSettings::primaryPageToolColor() const
{
return PAGETOOL_COLOR;
}
QColor
BooksSettings::highlightPageToolColor() const
{
return invertColors() ?
INVERTED_PAGETOOL_HIGHLIGHT_COLOR :
NORMAL_PAGETOOL_HIGHLIGHT_COLOR;
}