2015-06-28 14:22:35 +03:00
|
|
|
/*
|
2018-01-20 03:43:05 +03:00
|
|
|
* Copyright (C) 2015-2018 Jolla Ltd.
|
2015-06-28 14:22:35 +03:00
|
|
|
* 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.
|
2016-10-08 19:57:54 +03:00
|
|
|
* * Neither the name of Jolla Ltd nor the names of its contributors
|
2015-06-28 14:22:35 +03:00
|
|
|
* 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 "BooksPageWidget.h"
|
2016-10-09 12:01:05 +03:00
|
|
|
#include "BooksImageProvider.h"
|
2015-06-28 14:22:35 +03:00
|
|
|
#include "BooksTextStyle.h"
|
|
|
|
#include "BooksDefs.h"
|
|
|
|
|
2016-10-08 14:56:14 +03:00
|
|
|
#include "bookmodel/FBTextKind.h"
|
2016-11-02 16:33:26 +03:00
|
|
|
#include "image/ZLQtImageManager.h"
|
2016-10-08 14:56:14 +03:00
|
|
|
#include "ZLStringUtil.h"
|
|
|
|
|
2015-06-28 14:22:35 +03:00
|
|
|
#include "HarbourDebug.h"
|
|
|
|
|
2017-09-07 19:03:17 +03:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QClipboard>
|
2015-06-28 14:22:35 +03:00
|
|
|
#include <QPainter>
|
|
|
|
|
2016-11-02 16:33:26 +03:00
|
|
|
static const QString IMAGE_URL("image://%1/%2");
|
|
|
|
|
2015-06-28 14:22:35 +03:00
|
|
|
// ==========================================================================
|
|
|
|
// BooksPageWidget::Data
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
class BooksPageWidget::Data {
|
|
|
|
public:
|
|
|
|
Data(shared_ptr<ZLTextModel> aModel, int aWidth, int aHeight) :
|
|
|
|
iModel(aModel), iPaintContext(aWidth, aHeight) {}
|
|
|
|
|
|
|
|
bool paint(QPainter* aPainter);
|
|
|
|
|
|
|
|
public:
|
2015-07-11 01:35:13 +03:00
|
|
|
shared_ptr<BooksTextView> iView;
|
2015-06-28 14:22:35 +03:00
|
|
|
shared_ptr<ZLTextModel> iModel;
|
|
|
|
BooksPaintContext iPaintContext;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool BooksPageWidget::Data::paint(QPainter* aPainter)
|
|
|
|
{
|
|
|
|
if (!iView.isNull()) {
|
|
|
|
iPaintContext.beginPaint(aPainter);
|
|
|
|
iView->paint();
|
|
|
|
iPaintContext.endPaint();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// BooksPageWidget::ResetTask
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
class BooksPageWidget::ResetTask : public BooksTask
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ResetTask(shared_ptr<ZLTextModel> aModel,
|
|
|
|
shared_ptr<ZLTextStyle> aTextStyle, int aWidth, int aHeight,
|
|
|
|
const BooksMargins& aMargins, const BooksPos& aPosition);
|
|
|
|
~ResetTask();
|
|
|
|
|
|
|
|
void performTask();
|
|
|
|
|
|
|
|
public:
|
|
|
|
BooksPageWidget::Data* iData;
|
|
|
|
shared_ptr<ZLTextStyle> iTextStyle;
|
|
|
|
BooksMargins iMargins;
|
|
|
|
BooksPos iPosition;
|
|
|
|
};
|
|
|
|
|
|
|
|
BooksPageWidget::ResetTask::ResetTask(shared_ptr<ZLTextModel> aModel,
|
|
|
|
shared_ptr<ZLTextStyle> aTextStyle, int aWidth, int aHeight,
|
|
|
|
const BooksMargins& aMargins, const BooksPos& aPosition) :
|
|
|
|
iData(new BooksPageWidget::Data(aModel, aWidth, aHeight)),
|
|
|
|
iTextStyle(aTextStyle),
|
|
|
|
iMargins(aMargins),
|
|
|
|
iPosition(aPosition)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BooksPageWidget::ResetTask::~ResetTask()
|
|
|
|
{
|
|
|
|
delete iData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::ResetTask::performTask()
|
|
|
|
{
|
|
|
|
if (!isCanceled()) {
|
|
|
|
BooksTextView* view = new BooksTextView(iData->iPaintContext,
|
|
|
|
iTextStyle, iMargins);
|
|
|
|
if (!isCanceled()) {
|
|
|
|
view->setModel(iData->iModel);
|
|
|
|
if (!isCanceled()) {
|
|
|
|
view->gotoPosition(iPosition);
|
|
|
|
if (!isCanceled()) {
|
|
|
|
iData->iView = view;
|
|
|
|
} else {
|
|
|
|
delete view;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// BooksPageWidget::RenderTask
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
class BooksPageWidget::RenderTask : public BooksTask {
|
|
|
|
public:
|
|
|
|
RenderTask(shared_ptr<BooksPageWidget::Data> aData, int aWidth, int aHeight) :
|
2017-09-07 19:03:17 +03:00
|
|
|
iData(aData), iWidth(aWidth), iHeight(aHeight) {}
|
2015-06-28 14:22:35 +03:00
|
|
|
|
|
|
|
void performTask();
|
|
|
|
|
|
|
|
public:
|
|
|
|
shared_ptr<BooksPageWidget::Data> iData;
|
|
|
|
int iWidth;
|
|
|
|
int iHeight;
|
2016-11-02 16:33:26 +03:00
|
|
|
QImage iImage;
|
2015-06-28 14:22:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
void BooksPageWidget::RenderTask::performTask()
|
|
|
|
{
|
|
|
|
if (!isCanceled() && !iData.isNull() && !iData->iView.isNull() &&
|
|
|
|
iWidth > 0 && iHeight > 0) {
|
2016-11-02 16:33:26 +03:00
|
|
|
iImage = QImage(iWidth, iHeight, QImage::Format_RGB32);
|
2015-06-28 14:22:35 +03:00
|
|
|
if (!isCanceled()) {
|
2016-11-02 16:33:26 +03:00
|
|
|
QPainter painter(&iImage);
|
2015-06-28 14:22:35 +03:00
|
|
|
iData->paint(&painter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 19:03:17 +03:00
|
|
|
// ==========================================================================
|
|
|
|
// BooksPageWidget::ClearSelectionTask
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
class BooksPageWidget::ClearSelectionTask : public BooksTask {
|
|
|
|
public:
|
|
|
|
ClearSelectionTask(shared_ptr<BooksPageWidget::Data> aData, int aWidth,
|
|
|
|
int aHeight) : iData(aData), iWidth(aWidth), iHeight(aHeight),
|
|
|
|
iImageUpdated(false) {}
|
|
|
|
|
|
|
|
void performTask();
|
|
|
|
|
|
|
|
public:
|
|
|
|
shared_ptr<BooksPageWidget::Data> iData;
|
|
|
|
int iWidth;
|
|
|
|
int iHeight;
|
|
|
|
QImage iImage;
|
|
|
|
bool iImageUpdated;
|
|
|
|
};
|
|
|
|
|
|
|
|
void BooksPageWidget::ClearSelectionTask::performTask()
|
|
|
|
{
|
|
|
|
iData->iView->endSelection();
|
|
|
|
if (!isCanceled() && iWidth > 0 && iHeight > 0) {
|
|
|
|
const ZLTextArea& area = iData->iView->textArea();
|
|
|
|
if (!area.selectionIsEmpty()) {
|
|
|
|
area.clearSelection();
|
|
|
|
iImage = QImage(iWidth, iHeight, QImage::Format_RGB32);
|
|
|
|
if (!isCanceled()) {
|
|
|
|
QPainter painter(&iImage);
|
|
|
|
iData->paint(&painter);
|
|
|
|
iImageUpdated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// BooksPageWidget::StartSelectionTask
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
class BooksPageWidget::StartSelectionTask : public BooksTask {
|
|
|
|
public:
|
|
|
|
StartSelectionTask(shared_ptr<BooksPageWidget::Data> aData, int aX, int aY,
|
|
|
|
int aWidth, int aHeight) : iData(aData), iX(aX), iY(aY),
|
|
|
|
iWidth(aWidth), iHeight(aHeight), iSelectionEmpty(true) {}
|
|
|
|
|
|
|
|
void performTask();
|
|
|
|
|
|
|
|
public:
|
|
|
|
shared_ptr<BooksPageWidget::Data> iData;
|
|
|
|
int iX;
|
|
|
|
int iY;
|
|
|
|
int iWidth;
|
|
|
|
int iHeight;
|
|
|
|
QImage iImage;
|
|
|
|
bool iSelectionEmpty;
|
|
|
|
};
|
|
|
|
|
|
|
|
void BooksPageWidget::StartSelectionTask::performTask()
|
|
|
|
{
|
|
|
|
if (!isCanceled() && iWidth > 0 && iHeight > 0) {
|
|
|
|
iData->iView->startSelection(iX, iY);
|
|
|
|
iSelectionEmpty = iData->iView->textArea().selectionIsEmpty();
|
|
|
|
if (!isCanceled()) {
|
|
|
|
iImage = QImage(iWidth, iHeight, QImage::Format_RGB32);
|
|
|
|
if (!isCanceled()) {
|
|
|
|
QPainter painter(&iImage);
|
|
|
|
iData->paint(&painter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// BooksPageWidget::ExtendSelectionTask
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
class BooksPageWidget::ExtendSelectionTask : public BooksTask {
|
|
|
|
public:
|
|
|
|
ExtendSelectionTask(shared_ptr<BooksPageWidget::Data> aData, int aX, int aY,
|
|
|
|
int aWidth, int aHeight) : iData(aData), iX(aX), iY(aY),
|
|
|
|
iWidth(aWidth), iHeight(aHeight), iSelectionChanged(false),
|
|
|
|
iSelectionEmpty(true) {}
|
|
|
|
|
|
|
|
void performTask();
|
|
|
|
|
|
|
|
public:
|
|
|
|
shared_ptr<BooksPageWidget::Data> iData;
|
|
|
|
int iX;
|
|
|
|
int iY;
|
|
|
|
int iWidth;
|
|
|
|
int iHeight;
|
|
|
|
QImage iImage;
|
|
|
|
bool iSelectionChanged;
|
|
|
|
bool iSelectionEmpty;
|
|
|
|
};
|
|
|
|
|
|
|
|
void BooksPageWidget::ExtendSelectionTask::performTask()
|
|
|
|
{
|
|
|
|
if (!isCanceled() && iWidth > 0 && iHeight > 0) {
|
|
|
|
iSelectionChanged = iData->iView->extendSelection(iX, iY);
|
|
|
|
iSelectionEmpty = iData->iView->textArea().selectionIsEmpty();
|
|
|
|
if (iSelectionChanged && !isCanceled()) {
|
|
|
|
iImage = QImage(iWidth, iHeight, QImage::Format_RGB32);
|
|
|
|
if (!isCanceled()) {
|
|
|
|
QPainter painter(&iImage);
|
|
|
|
iData->paint(&painter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:33:26 +03:00
|
|
|
// ==========================================================================
|
|
|
|
// BooksPageWidget::FootnoteTask
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
class BooksPageWidget::FootnoteTask : public BooksTask, ZLTextArea::Properties {
|
|
|
|
public:
|
|
|
|
FootnoteTask(int aX, int aY, int aMaxWidth, int aMaxHeight,
|
|
|
|
QString aPath, QString aLinkText, QString aRef,
|
|
|
|
shared_ptr<ZLTextModel> aTextModel, shared_ptr<ZLTextStyle> aTextStyle,
|
|
|
|
bool aInvertColors) :
|
|
|
|
iTextModel(aTextModel), iTextStyle(aTextStyle),
|
|
|
|
iInvertColors(aInvertColors), iX(aX), iY(aY),
|
|
|
|
iMaxWidth(aMaxWidth), iMaxHeight(aMaxHeight),
|
|
|
|
iRef(aRef), iLinkText(aLinkText), iPath(aPath) {}
|
|
|
|
~FootnoteTask();
|
|
|
|
|
|
|
|
void performTask();
|
|
|
|
|
|
|
|
// ZLTextArea::Properties
|
|
|
|
shared_ptr<ZLTextStyle> baseStyle() const;
|
|
|
|
ZLColor color(const std::string& aStyle) const;
|
|
|
|
bool isSelectionEnabled() const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
shared_ptr<ZLTextModel> iTextModel;
|
|
|
|
shared_ptr<ZLTextStyle> iTextStyle;
|
|
|
|
bool iInvertColors;
|
|
|
|
int iX;
|
|
|
|
int iY;
|
|
|
|
int iMaxWidth;
|
|
|
|
int iMaxHeight;
|
|
|
|
QString iRef;
|
|
|
|
QString iLinkText;
|
|
|
|
QString iPath;
|
|
|
|
QImage iImage;
|
|
|
|
};
|
|
|
|
|
|
|
|
BooksPageWidget::FootnoteTask::~FootnoteTask()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<ZLTextStyle> BooksPageWidget::FootnoteTask::baseStyle() const
|
|
|
|
{
|
|
|
|
return iTextStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZLColor BooksPageWidget::FootnoteTask::color(const std::string& aStyle) const
|
|
|
|
{
|
|
|
|
return BooksPaintContext::realColor(aStyle, iInvertColors);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BooksPageWidget::FootnoteTask::isSelectionEnabled() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::FootnoteTask::performTask()
|
|
|
|
{
|
|
|
|
if (!isCanceled()) {
|
|
|
|
// Determine the size of the footnote canvas
|
|
|
|
ZLTextParagraphCursorCache cache;
|
|
|
|
BooksPaintContext sizeContext(iMaxWidth, iMaxHeight);
|
|
|
|
ZLTextAreaController sizeController(sizeContext, *this, &cache);
|
|
|
|
ZLSize size;
|
|
|
|
sizeController.setModel(iTextModel);
|
|
|
|
sizeController.preparePaintInfo();
|
|
|
|
sizeController.area().paint(&size);
|
|
|
|
if (!size.isEmpty() && !isCanceled()) {
|
|
|
|
// Now actually paint it
|
|
|
|
size.myWidth = (size.myWidth + 3) & -4;
|
|
|
|
HDEBUG("footnote size:" << size.myWidth << "x" << size.myHeight);
|
|
|
|
cache.clear();
|
|
|
|
BooksPaintContext paintContext(size.myWidth, size.myHeight);
|
|
|
|
paintContext.setInvertColors(iInvertColors);
|
|
|
|
ZLTextAreaController paintController(paintContext, *this, &cache);
|
|
|
|
iImage = QImage(size.myWidth, size.myHeight, QImage::Format_RGB32);
|
|
|
|
QPainter painter(&iImage);
|
|
|
|
paintContext.beginPaint(&painter);
|
|
|
|
paintContext.clear(iInvertColors ?
|
|
|
|
BooksTextView::INVERTED_BACKGROUND :
|
|
|
|
BooksTextView::DEFAULT_BACKGROUND);
|
|
|
|
paintController.setModel(iTextModel);
|
|
|
|
paintController.preparePaintInfo();
|
|
|
|
paintController.area().paint();
|
|
|
|
paintContext.endPaint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 14:56:14 +03:00
|
|
|
// ==========================================================================
|
2016-10-16 20:07:03 +03:00
|
|
|
// BooksPageWidget::PressTask
|
2016-10-08 14:56:14 +03:00
|
|
|
// ==========================================================================
|
|
|
|
|
2016-10-16 20:07:03 +03:00
|
|
|
class BooksPageWidget::PressTask : public BooksTask {
|
2016-10-08 14:56:14 +03:00
|
|
|
public:
|
2016-10-16 20:07:03 +03:00
|
|
|
PressTask(shared_ptr<BooksPageWidget::Data> aData, int aX, int aY) :
|
2016-10-08 19:57:54 +03:00
|
|
|
iData(aData), iX(aX), iY(aY), iKind(REGULAR) {}
|
2016-10-08 14:56:14 +03:00
|
|
|
|
|
|
|
void performTask();
|
2016-11-02 16:33:26 +03:00
|
|
|
QString getLinkText(ZLTextWordCursor& aCursor);
|
2016-10-08 14:56:14 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
shared_ptr<BooksPageWidget::Data> iData;
|
|
|
|
int iX;
|
|
|
|
int iY;
|
2016-10-08 19:57:54 +03:00
|
|
|
QRect iRect;
|
2016-10-08 14:56:14 +03:00
|
|
|
ZLTextKind iKind;
|
2016-10-08 19:57:54 +03:00
|
|
|
std::string iLink;
|
2016-10-08 14:56:14 +03:00
|
|
|
std::string iLinkType;
|
2016-10-08 19:57:54 +03:00
|
|
|
std::string iImageId;
|
2016-11-02 16:33:26 +03:00
|
|
|
QString iLinkText;
|
|
|
|
QImage iImage;
|
2016-10-08 14:56:14 +03:00
|
|
|
};
|
|
|
|
|
2016-11-02 16:33:26 +03:00
|
|
|
QString BooksPageWidget::PressTask::getLinkText(ZLTextWordCursor& aCursor)
|
|
|
|
{
|
|
|
|
QString text;
|
|
|
|
while (!aCursor.isEndOfParagraph() && !isCanceled() &&
|
|
|
|
aCursor.element().kind() != ZLTextElement::WORD_ELEMENT) {
|
|
|
|
aCursor.nextWord();
|
|
|
|
}
|
|
|
|
while (!aCursor.isEndOfParagraph() && !isCanceled() &&
|
|
|
|
aCursor.element().kind() == ZLTextElement::WORD_ELEMENT) {
|
|
|
|
const ZLTextWord& word = (ZLTextWord&)aCursor.element();
|
|
|
|
if (!text.isEmpty()) text.append(' ');
|
|
|
|
text.append(QString::fromUtf8(word.Data, word.Size));
|
|
|
|
aCursor.nextWord();
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2016-10-16 20:07:03 +03:00
|
|
|
void BooksPageWidget::PressTask::performTask()
|
2016-10-08 14:56:14 +03:00
|
|
|
{
|
|
|
|
if (!isCanceled()) {
|
2016-10-08 23:36:28 +03:00
|
|
|
const BooksTextView& view = *iData->iView;
|
|
|
|
const ZLTextArea& area = view.textArea();
|
2016-10-08 14:56:14 +03:00
|
|
|
const ZLTextElementRectangle* rect = area.elementByCoordinates(iX, iY);
|
2016-10-08 19:57:54 +03:00
|
|
|
if (rect && !isCanceled()) {
|
|
|
|
iRect.setLeft(rect->XStart);
|
|
|
|
iRect.setRight(rect->XEnd);
|
|
|
|
iRect.setTop(rect->YStart);
|
|
|
|
iRect.setBottom(rect->YEnd);
|
2016-10-08 23:36:28 +03:00
|
|
|
iRect.translate(view.leftMargin(), view.topMargin());
|
2016-10-08 19:57:54 +03:00
|
|
|
if (rect->Kind == ZLTextElement::WORD_ELEMENT) {
|
|
|
|
ZLTextWordCursor cursor = area.startCursor();
|
|
|
|
cursor.moveToParagraph(rect->ParagraphIndex);
|
2016-10-15 13:27:43 +03:00
|
|
|
cursor.moveTo(rect->ElementIndex, 0);
|
|
|
|
|
|
|
|
// Basically, the idea is that we are going backwards
|
|
|
|
// looking for the START of the link. If we have crossed
|
|
|
|
// the END of the linked element, it means that we have
|
|
|
|
// missed it. We are recording which stop control elements
|
|
|
|
// we have encountered, to avoid making assumptions which
|
|
|
|
// ones are links and which are not. We rely on isHyperlink()
|
|
|
|
// method to tell us the ultimate truth.
|
|
|
|
bool stopped[NUM_KINDS];
|
|
|
|
memset(stopped, 0, sizeof(stopped));
|
|
|
|
while (!cursor.isStartOfParagraph() && !isCanceled()) {
|
|
|
|
cursor.previousWord();
|
2016-10-08 19:57:54 +03:00
|
|
|
const ZLTextElement& element = cursor.element();
|
|
|
|
if (element.kind() == ZLTextElement::CONTROL_ELEMENT) {
|
2016-10-15 13:27:43 +03:00
|
|
|
const ZLTextControlEntry& entry =
|
|
|
|
((ZLTextControlElement&)element).entry();
|
|
|
|
ZLTextKind kind = entry.kind();
|
|
|
|
if (kind < NUM_KINDS && !entry.isStart()) {
|
|
|
|
stopped[kind] = true;
|
|
|
|
}
|
|
|
|
if (entry.isHyperlink()) {
|
|
|
|
if (entry.isStart() && !stopped[entry.kind()]) {
|
|
|
|
const ZLTextHyperlinkControlEntry& link =
|
|
|
|
(ZLTextHyperlinkControlEntry&) entry;
|
2016-11-02 16:33:26 +03:00
|
|
|
iKind = kind;
|
2016-10-15 13:27:43 +03:00
|
|
|
iLink = link.label();
|
|
|
|
iLinkType = link.hyperlinkType();
|
2016-11-02 16:33:26 +03:00
|
|
|
iLinkText = getLinkText(cursor);
|
|
|
|
HDEBUG("link" << kind << iLinkText <<
|
|
|
|
iLink.c_str());
|
2016-10-15 13:27:43 +03:00
|
|
|
}
|
2016-10-08 19:57:54 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-10-08 14:56:14 +03:00
|
|
|
}
|
2016-10-08 19:57:54 +03:00
|
|
|
}
|
|
|
|
} else if (rect->Kind == ZLTextElement::IMAGE_ELEMENT) {
|
|
|
|
ZLTextWordCursor cursor = area.startCursor();
|
|
|
|
cursor.moveToParagraph(rect->ParagraphIndex);
|
|
|
|
cursor.moveTo(rect->ElementIndex, 0);
|
|
|
|
const ZLTextElement& element = cursor.element();
|
|
|
|
HASSERT(element.kind() == ZLTextElement::IMAGE_ELEMENT);
|
|
|
|
if (element.kind() == ZLTextElement::IMAGE_ELEMENT) {
|
|
|
|
const ZLTextImageElement& imageElement =
|
|
|
|
(const ZLTextImageElement&)element;
|
2016-11-02 16:33:26 +03:00
|
|
|
shared_ptr<ZLImageData> data = imageElement.image();
|
|
|
|
if (!data.isNull()) {
|
|
|
|
const QImage* image = ((ZLQtImageData&)(*data)).image();
|
|
|
|
if (image && !image->isNull()) {
|
|
|
|
iKind = IMAGE;
|
|
|
|
iImage = *image;
|
|
|
|
iImageId = imageElement.id();
|
|
|
|
HDEBUG("image element" << iImageId.c_str() <<
|
|
|
|
iImage.width() << iImage.height());
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 14:56:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-28 14:22:35 +03:00
|
|
|
// ==========================================================================
|
|
|
|
// BooksPageWidget
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
BooksPageWidget::BooksPageWidget(QQuickItem* aParent) :
|
|
|
|
QQuickPaintedItem(aParent),
|
2016-10-16 23:36:13 +03:00
|
|
|
iSettings(BooksSettings::sharedInstance()),
|
2015-12-12 11:36:22 +03:00
|
|
|
iTaskQueue(BooksTaskQueue::defaultQueue()),
|
2015-06-28 14:22:35 +03:00
|
|
|
iTextStyle(BooksTextStyle::defaults()),
|
|
|
|
iResizeTimer(new QTimer(this)),
|
|
|
|
iModel(NULL),
|
|
|
|
iResetTask(NULL),
|
|
|
|
iRenderTask(NULL),
|
2017-09-07 19:03:17 +03:00
|
|
|
iClearSelectionTask(NULL),
|
|
|
|
iStartSelectionTask(NULL),
|
2016-10-16 20:07:03 +03:00
|
|
|
iPressTask(NULL),
|
2016-10-08 14:56:14 +03:00
|
|
|
iLongPressTask(NULL),
|
2016-11-02 16:33:26 +03:00
|
|
|
iFootnoteTask(NULL),
|
2015-06-28 14:22:35 +03:00
|
|
|
iEmpty(false),
|
2017-09-07 19:03:17 +03:00
|
|
|
iPressed(false),
|
|
|
|
iSelecting(false),
|
|
|
|
iSelectionEmpty(true),
|
|
|
|
iCurrentPage(false),
|
2015-06-28 14:22:35 +03:00
|
|
|
iPage(-1)
|
|
|
|
{
|
2016-10-16 23:36:13 +03:00
|
|
|
connect(iSettings.data(),
|
|
|
|
SIGNAL(invertColorsChanged()),
|
|
|
|
SLOT(onInvertColorsChanged()));
|
|
|
|
setFillColor(qtColor(iSettings->invertColors() ?
|
|
|
|
BooksTextView::INVERTED_BACKGROUND :
|
|
|
|
BooksTextView::DEFAULT_BACKGROUND));
|
2015-06-28 14:22:35 +03:00
|
|
|
setFlag(ItemHasContents, true);
|
|
|
|
iResizeTimer->setSingleShot(true);
|
|
|
|
iResizeTimer->setInterval(0);
|
|
|
|
connect(iResizeTimer, SIGNAL(timeout()), SLOT(onResizeTimeout()));
|
|
|
|
connect(this, SIGNAL(widthChanged()), SLOT(onWidthChanged()));
|
|
|
|
connect(this, SIGNAL(heightChanged()), SLOT(onHeightChanged()));
|
|
|
|
}
|
|
|
|
|
|
|
|
BooksPageWidget::~BooksPageWidget()
|
|
|
|
{
|
|
|
|
HDEBUG("page" << iPage);
|
2017-09-07 19:03:17 +03:00
|
|
|
releaseExtendSelectionTasks();
|
2015-06-28 14:22:35 +03:00
|
|
|
if (iResetTask) iResetTask->release(this);
|
|
|
|
if (iRenderTask) iRenderTask->release(this);
|
2017-09-07 19:03:17 +03:00
|
|
|
if (iClearSelectionTask) iClearSelectionTask->release(this);
|
|
|
|
if (iStartSelectionTask) iStartSelectionTask->release(this);
|
2016-10-16 20:07:03 +03:00
|
|
|
if (iPressTask) iPressTask->release(this);
|
2016-10-08 14:56:14 +03:00
|
|
|
if (iLongPressTask) iLongPressTask->release(this);
|
2016-11-02 16:33:26 +03:00
|
|
|
if (iFootnoteTask) iFootnoteTask->release(this);
|
2015-06-28 14:22:35 +03:00
|
|
|
}
|
|
|
|
|
2017-09-07 19:03:17 +03:00
|
|
|
void BooksPageWidget::releaseExtendSelectionTasks()
|
|
|
|
{
|
|
|
|
while (!iExtendSelectionTasks.isEmpty()) {
|
|
|
|
const int i = iExtendSelectionTasks.count()-1;
|
|
|
|
iExtendSelectionTasks.at(i)->release(this);
|
|
|
|
iExtendSelectionTasks.removeAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::setPressed(bool aPressed)
|
|
|
|
{
|
|
|
|
if (iPressed != aPressed) {
|
|
|
|
iPressed = aPressed;
|
|
|
|
if (!iPressed && iSelecting) {
|
|
|
|
HDEBUG("leaving selection mode");
|
|
|
|
iSelecting = false;
|
|
|
|
Q_EMIT selectingChanged();
|
|
|
|
}
|
|
|
|
Q_EMIT pressedChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::setCurrentPage(bool aCurrentPage)
|
|
|
|
{
|
|
|
|
if (iCurrentPage != aCurrentPage) {
|
|
|
|
iCurrentPage = aCurrentPage;
|
|
|
|
HDEBUG(iCurrentPage);
|
|
|
|
if (!iCurrentPage) clearSelection();
|
|
|
|
Q_EMIT currentPageChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-28 14:22:35 +03:00
|
|
|
void BooksPageWidget::setModel(BooksBookModel* aModel)
|
|
|
|
{
|
|
|
|
if (iModel != aModel) {
|
2015-12-13 14:46:53 +03:00
|
|
|
if (iModel) iModel->disconnect(this);
|
2015-06-28 14:22:35 +03:00
|
|
|
iModel = aModel;
|
|
|
|
if (iModel) {
|
|
|
|
#if HARBOUR_DEBUG
|
|
|
|
if (iPage >= 0) {
|
|
|
|
HDEBUG(iModel->title() << iPage);
|
|
|
|
} else {
|
|
|
|
HDEBUG(iModel->title());
|
|
|
|
}
|
|
|
|
#endif // HARBOUR_DEBUG
|
2015-12-13 14:46:53 +03:00
|
|
|
iTextStyle = iModel->textStyle();
|
2015-06-28 14:22:35 +03:00
|
|
|
iPageMark = iModel->pageMark(iPage);
|
|
|
|
connect(iModel, SIGNAL(bookModelChanged()),
|
|
|
|
SLOT(onBookModelChanged()));
|
|
|
|
connect(iModel, SIGNAL(destroyed()),
|
|
|
|
SLOT(onBookModelDestroyed()));
|
|
|
|
connect(iModel, SIGNAL(pageMarksChanged()),
|
|
|
|
SLOT(onBookModelPageMarksChanged()));
|
|
|
|
connect(iModel, SIGNAL(loadingChanged()),
|
|
|
|
SLOT(onBookModelLoadingChanged()));
|
2015-12-13 14:46:53 +03:00
|
|
|
connect(iModel, SIGNAL(textStyleChanged()),
|
|
|
|
SLOT(onTextStyleChanged()));
|
2015-06-28 14:22:35 +03:00
|
|
|
} else {
|
|
|
|
iPageMark.invalidate();
|
2015-12-13 14:46:53 +03:00
|
|
|
iTextStyle = BooksTextStyle::defaults();
|
2015-06-28 14:22:35 +03:00
|
|
|
}
|
|
|
|
resetView();
|
|
|
|
Q_EMIT modelChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::onTextStyleChanged()
|
|
|
|
{
|
|
|
|
HDEBUG(iPage);
|
2015-12-13 14:46:53 +03:00
|
|
|
HASSERT(sender() == iModel);
|
|
|
|
iTextStyle = iModel->textStyle();
|
2015-06-28 14:22:35 +03:00
|
|
|
resetView();
|
|
|
|
}
|
|
|
|
|
2015-07-11 01:35:13 +03:00
|
|
|
void BooksPageWidget::onInvertColorsChanged()
|
|
|
|
{
|
|
|
|
HDEBUG(iPage);
|
|
|
|
HASSERT(sender() == iSettings);
|
|
|
|
if (!iData.isNull() && !iData->iView.isNull()) {
|
|
|
|
iData->iView->setInvertColors(iSettings->invertColors());
|
|
|
|
scheduleRepaint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-28 14:22:35 +03:00
|
|
|
void BooksPageWidget::onBookModelChanged()
|
|
|
|
{
|
|
|
|
HDEBUG(iModel->title());
|
|
|
|
BooksLoadingSignalBlocker block(this);
|
|
|
|
iPageMark = iModel->pageMark(iPage);
|
|
|
|
resetView();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::onBookModelDestroyed()
|
|
|
|
{
|
|
|
|
HDEBUG("model destroyed");
|
|
|
|
HASSERT(iModel == sender());
|
|
|
|
BooksLoadingSignalBlocker block(this);
|
|
|
|
iModel = NULL;
|
|
|
|
Q_EMIT modelChanged();
|
|
|
|
resetView();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::onBookModelPageMarksChanged()
|
|
|
|
{
|
|
|
|
const BooksPos pos = iModel->pageMark(iPage);
|
|
|
|
if (iPageMark != pos) {
|
|
|
|
BooksLoadingSignalBlocker block(this);
|
|
|
|
iPageMark = pos;
|
|
|
|
HDEBUG("page" << iPage);
|
|
|
|
resetView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::onBookModelLoadingChanged()
|
|
|
|
{
|
|
|
|
BooksLoadingSignalBlocker block(this);
|
|
|
|
if (!iModel->loading()) {
|
|
|
|
HDEBUG("page" << iPage);
|
|
|
|
const BooksPos pos = iModel->pageMark(iPage);
|
|
|
|
if (iPageMark != pos) {
|
|
|
|
iPageMark = pos;
|
|
|
|
resetView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::setPage(int aPage)
|
|
|
|
{
|
|
|
|
if (iPage != aPage) {
|
|
|
|
BooksLoadingSignalBlocker block(this);
|
|
|
|
iPage = aPage;
|
|
|
|
HDEBUG(iPage);
|
|
|
|
const BooksPos pos = iModel->pageMark(iPage);
|
|
|
|
if (iPageMark != pos) {
|
|
|
|
iPageMark = pos;
|
|
|
|
resetView();
|
|
|
|
}
|
|
|
|
resetView();
|
|
|
|
Q_EMIT pageChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::setLeftMargin(int aMargin)
|
|
|
|
{
|
|
|
|
if (iMargins.iLeft != aMargin) {
|
|
|
|
iMargins.iLeft = aMargin;
|
2017-01-30 00:56:23 +03:00
|
|
|
HVERBOSE(aMargin);
|
2015-06-28 14:22:35 +03:00
|
|
|
resetView();
|
|
|
|
Q_EMIT leftMarginChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::setRightMargin(int aMargin)
|
|
|
|
{
|
|
|
|
if (iMargins.iRight != aMargin) {
|
|
|
|
iMargins.iRight = aMargin;
|
2017-01-30 00:56:23 +03:00
|
|
|
HVERBOSE(aMargin);
|
2015-06-28 14:22:35 +03:00
|
|
|
resetView();
|
|
|
|
Q_EMIT rightMarginChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::setTopMargin(int aMargin)
|
|
|
|
{
|
|
|
|
if (iMargins.iTop != aMargin) {
|
|
|
|
iMargins.iTop = aMargin;
|
2017-01-30 00:56:23 +03:00
|
|
|
HVERBOSE(aMargin);
|
2015-06-28 14:22:35 +03:00
|
|
|
resetView();
|
|
|
|
Q_EMIT topMarginChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::setBottomMargin(int aMargin)
|
|
|
|
{
|
|
|
|
if (iMargins.iBottom != aMargin) {
|
|
|
|
iMargins.iBottom = aMargin;
|
2017-01-30 00:56:23 +03:00
|
|
|
HVERBOSE(aMargin);
|
2015-06-28 14:22:35 +03:00
|
|
|
resetView();
|
|
|
|
Q_EMIT bottomMarginChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::paint(QPainter* aPainter)
|
|
|
|
{
|
|
|
|
if (!iImage.isNull()) {
|
|
|
|
HDEBUG("page" << iPage);
|
2016-11-02 16:33:26 +03:00
|
|
|
aPainter->drawImage(0, 0, iImage);
|
2015-06-28 14:22:35 +03:00
|
|
|
iEmpty = false;
|
2015-07-06 22:16:29 +03:00
|
|
|
} else if (iPage >= 0 && iPageMark.valid() && !iData.isNull()) {
|
2015-07-03 17:24:20 +03:00
|
|
|
if (!iRenderTask) {
|
2015-06-28 14:22:35 +03:00
|
|
|
HDEBUG("page" << iPage << "(scheduled)");
|
|
|
|
scheduleRepaint();
|
|
|
|
} else {
|
|
|
|
HDEBUG("page" << iPage << "(not yet ready)");
|
|
|
|
}
|
|
|
|
iEmpty = true;
|
|
|
|
} else {
|
|
|
|
HDEBUG("page" << iPage << "(empty)");
|
|
|
|
iEmpty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BooksPageWidget::loading() const
|
|
|
|
{
|
2017-09-07 19:03:17 +03:00
|
|
|
return iPage >= 0 && iImage.isNull() && (iResetTask || iRenderTask);
|
2015-06-28 14:22:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::resetView()
|
|
|
|
{
|
|
|
|
BooksLoadingSignalBlocker block(this);
|
|
|
|
if (iResetTask) {
|
|
|
|
iResetTask->release(this);
|
|
|
|
iResetTask = NULL;
|
|
|
|
}
|
2016-11-02 16:33:26 +03:00
|
|
|
if (iPressTask) {
|
|
|
|
iPressTask->release(this);
|
|
|
|
iPressTask = NULL;
|
|
|
|
}
|
|
|
|
if (iLongPressTask) {
|
|
|
|
iLongPressTask->release(this);
|
|
|
|
iLongPressTask = NULL;
|
|
|
|
}
|
|
|
|
if (iFootnoteTask) {
|
|
|
|
iFootnoteTask->release(this);
|
|
|
|
iFootnoteTask = NULL;
|
|
|
|
}
|
2015-06-28 14:22:35 +03:00
|
|
|
iData.reset();
|
|
|
|
if (iPage >= 0 && iPageMark.valid() &&
|
|
|
|
width() > 0 && height() > 0 && iModel) {
|
|
|
|
shared_ptr<ZLTextModel> textModel = iModel->bookTextModel();
|
|
|
|
if (!textModel.isNull()) {
|
|
|
|
iResetTask = new ResetTask(textModel, iTextStyle,
|
|
|
|
width(), height(), iMargins, iPageMark);
|
|
|
|
iTaskQueue->submit(iResetTask, this, SLOT(onResetTaskDone()));
|
|
|
|
cancelRepaint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!iResetTask && !iEmpty) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::cancelRepaint()
|
|
|
|
{
|
|
|
|
BooksLoadingSignalBlocker block(this);
|
|
|
|
if (iRenderTask) {
|
|
|
|
iRenderTask->release(this);
|
|
|
|
iRenderTask = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::scheduleRepaint()
|
|
|
|
{
|
|
|
|
BooksLoadingSignalBlocker block(this);
|
|
|
|
cancelRepaint();
|
2015-07-11 01:35:13 +03:00
|
|
|
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);
|
2015-06-28 14:22:35 +03:00
|
|
|
iTaskQueue->submit(iRenderTask, this, SLOT(onRenderTaskDone()));
|
|
|
|
} else {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::onResetTaskDone()
|
|
|
|
{
|
|
|
|
BooksLoadingSignalBlocker block(this);
|
|
|
|
HASSERT(sender() == iResetTask);
|
|
|
|
iData = iResetTask->iData;
|
|
|
|
iResetTask->iData = NULL;
|
|
|
|
iResetTask->release(this);
|
|
|
|
iResetTask = NULL;
|
2015-07-03 17:24:20 +03:00
|
|
|
scheduleRepaint();
|
2015-06-28 14:22:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::onRenderTaskDone()
|
|
|
|
{
|
|
|
|
BooksLoadingSignalBlocker block(this);
|
|
|
|
HASSERT(sender() == iRenderTask);
|
|
|
|
iImage = iRenderTask->iImage;
|
|
|
|
iRenderTask->release(this);
|
|
|
|
iRenderTask = NULL;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-10-16 20:07:03 +03:00
|
|
|
void BooksPageWidget::onPressTaskDone()
|
|
|
|
{
|
|
|
|
HASSERT(sender() == iPressTask);
|
|
|
|
HDEBUG(iPressTask->iKind);
|
|
|
|
|
|
|
|
PressTask* task = iPressTask;
|
|
|
|
iPressTask = NULL;
|
|
|
|
|
|
|
|
if (task->iKind != REGULAR) {
|
|
|
|
Q_EMIT activeTouch(task->iX, task->iY);
|
|
|
|
}
|
|
|
|
|
|
|
|
task->release(this);
|
|
|
|
}
|
|
|
|
|
2017-09-07 19:03:17 +03:00
|
|
|
void BooksPageWidget::onClearSelectionTaskDone()
|
|
|
|
{
|
|
|
|
HASSERT(sender() == iClearSelectionTask);
|
|
|
|
ClearSelectionTask* task = iClearSelectionTask;
|
|
|
|
iClearSelectionTask = NULL;
|
|
|
|
|
|
|
|
if (!iSelectionEmpty) {
|
|
|
|
iSelectionEmpty = true;
|
|
|
|
HDEBUG("selection cleared");
|
|
|
|
Q_EMIT selectionEmptyChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (task->iImageUpdated) {
|
|
|
|
iImage = task->iImage;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
task->release(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::onStartSelectionTaskDone()
|
|
|
|
{
|
|
|
|
HASSERT(sender() == iStartSelectionTask);
|
|
|
|
StartSelectionTask* task = iStartSelectionTask;
|
|
|
|
iStartSelectionTask = NULL;
|
|
|
|
|
|
|
|
if (iPressed) {
|
|
|
|
iImage = task->iImage;
|
|
|
|
|
|
|
|
// Emit signals when we are in a consistent state
|
|
|
|
bool emitSelectionEmpty;
|
|
|
|
if (iSelectionEmpty != task->iSelectionEmpty) {
|
|
|
|
iSelectionEmpty = task->iSelectionEmpty;
|
|
|
|
HDEBUG("selection" << iSelectionEmpty);
|
|
|
|
emitSelectionEmpty = true;
|
|
|
|
}
|
|
|
|
if (!iSelecting) {
|
|
|
|
iSelecting = true;
|
|
|
|
HDEBUG("entering selection mode");
|
|
|
|
Q_EMIT selectingChanged();
|
|
|
|
}
|
|
|
|
if (emitSelectionEmpty) {
|
|
|
|
Q_EMIT selectionEmptyChanged();
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
task->release(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::onExtendSelectionTaskDone()
|
|
|
|
{
|
|
|
|
ExtendSelectionTask* task = (ExtendSelectionTask*)sender();
|
|
|
|
HASSERT(iExtendSelectionTasks.contains(task));
|
|
|
|
iExtendSelectionTasks.removeOne(task);
|
|
|
|
|
|
|
|
if (iSelecting && task->iSelectionChanged) {
|
|
|
|
iImage = task->iImage;
|
|
|
|
if (iSelectionEmpty != task->iSelectionEmpty) {
|
|
|
|
iSelectionEmpty = task->iSelectionEmpty;
|
|
|
|
HDEBUG("selection" << iSelectionEmpty);
|
|
|
|
Q_EMIT selectionEmptyChanged();
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
task->release(this);
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:33:26 +03:00
|
|
|
void BooksPageWidget::onFootnoteTaskDone()
|
|
|
|
{
|
|
|
|
HASSERT(sender() == iFootnoteTask);
|
|
|
|
|
|
|
|
FootnoteTask* task = iFootnoteTask;
|
|
|
|
iFootnoteTask = NULL;
|
|
|
|
if (!task->iImage.isNull()) {
|
|
|
|
// Footnotes with normal and inverted background need to
|
|
|
|
// have different ids so that the cached image with the wrong
|
|
|
|
// background doesn't show up after we invert the colors
|
|
|
|
static const QString NORMAL("n");
|
|
|
|
static const QString INVERTED("i");
|
|
|
|
static const QString FOOTNOTE_ID("footnote/%1#%2?p=%3&t=%4&s=%5x%6");
|
|
|
|
QString id = FOOTNOTE_ID.arg(task->iPath, task->iRef).
|
|
|
|
arg(iPage).arg(task->iInvertColors ? INVERTED : NORMAL).
|
|
|
|
arg(task->iImage.width()).arg(task->iImage.height());
|
|
|
|
QString url = IMAGE_URL.arg(BooksImageProvider::PROVIDER_ID, id);
|
|
|
|
HDEBUG(url);
|
|
|
|
BooksImageProvider::instance()->addImage(iModel, id, task->iImage);
|
|
|
|
Q_EMIT showFootnote(task->iX, task->iY, task->iLinkText, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
task->release(this);
|
|
|
|
}
|
|
|
|
|
2016-10-08 14:56:14 +03:00
|
|
|
void BooksPageWidget::onLongPressTaskDone()
|
|
|
|
{
|
|
|
|
HASSERT(sender() == iLongPressTask);
|
2016-10-08 19:57:54 +03:00
|
|
|
HDEBUG(iLongPressTask->iKind);
|
|
|
|
|
2016-10-16 20:07:03 +03:00
|
|
|
PressTask* task = iLongPressTask;
|
|
|
|
iLongPressTask = NULL;
|
|
|
|
|
|
|
|
if (task->iKind == EXTERNAL_HYPERLINK) {
|
2016-10-08 19:57:54 +03:00
|
|
|
static const std::string HTTP("http://");
|
|
|
|
static const std::string HTTPS("https://");
|
2016-10-16 20:07:03 +03:00
|
|
|
if (ZLStringUtil::stringStartsWith(task->iLink, HTTP) ||
|
|
|
|
ZLStringUtil::stringStartsWith(task->iLink, HTTPS)) {
|
|
|
|
QString url(QString::fromStdString(task->iLink));
|
2016-10-08 19:57:54 +03:00
|
|
|
Q_EMIT browserLinkPressed(url);
|
|
|
|
}
|
2016-10-16 20:07:03 +03:00
|
|
|
} else if (task->iKind == INTERNAL_HYPERLINK) {
|
|
|
|
if (iModel) {
|
2017-08-03 18:48:17 +03:00
|
|
|
BooksPos pos = iModel->linkPosition(task->iLink);
|
|
|
|
if (pos.valid()) {
|
|
|
|
HDEBUG("link to" << pos);
|
|
|
|
Q_EMIT pushPosition(pos);
|
2016-10-16 20:07:03 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 16:33:26 +03:00
|
|
|
} else if (task->iKind == FOOTNOTE) {
|
|
|
|
if (iModel && task->iLink.length() > 0) {
|
2018-01-20 03:43:05 +03:00
|
|
|
shared_ptr<ZLTextModel> note = iModel->footnoteModel(task->iLink);
|
2016-11-02 16:33:26 +03:00
|
|
|
BooksBook* book = iModel->book();
|
|
|
|
if (!note.isNull() && book) {
|
|
|
|
// Render the footnote
|
2018-01-20 03:43:05 +03:00
|
|
|
HDEBUG("footnote" << QString(task->iLink.c_str()));
|
2016-11-02 16:33:26 +03:00
|
|
|
if (iFootnoteTask) iFootnoteTask->release(this);
|
|
|
|
iFootnoteTask = new FootnoteTask(task->iX, task->iY,
|
|
|
|
width()*3/4, height()*10, book->path(), task->iLinkText,
|
2018-01-20 03:43:05 +03:00
|
|
|
QString::fromStdString(task->iLink), note, iTextStyle,
|
2016-11-02 16:33:26 +03:00
|
|
|
iSettings->invertColors());
|
|
|
|
iTaskQueue->submit(iFootnoteTask, this,
|
|
|
|
SLOT(onFootnoteTaskDone()));
|
2018-01-20 03:43:05 +03:00
|
|
|
} else {
|
|
|
|
HDEBUG("bad footnote" << QString(task->iLink.c_str()));
|
2016-11-02 16:33:26 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-16 20:07:03 +03:00
|
|
|
} else if (task->iKind == IMAGE) {
|
2016-10-09 12:01:05 +03:00
|
|
|
// Make sure that the book path is mixed into the image id to handle
|
|
|
|
// the case of different books having images with identical ids
|
2016-11-02 16:33:26 +03:00
|
|
|
QString imageId = QString::fromStdString(task->iImageId);
|
2016-10-09 12:01:05 +03:00
|
|
|
QString path;
|
|
|
|
if (iModel) {
|
|
|
|
BooksBook* book = iModel->book();
|
|
|
|
if (book) {
|
|
|
|
path = book->path();
|
|
|
|
if (!path.isEmpty()) {
|
2016-11-02 16:33:26 +03:00
|
|
|
if (!imageId.contains(path)) {
|
|
|
|
QString old = imageId;
|
|
|
|
imageId = path + ":" + old;
|
|
|
|
HDEBUG(old << "-> " << imageId);
|
2016-10-09 12:01:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 16:33:26 +03:00
|
|
|
static const QString IMAGE_ID("image/%1");
|
|
|
|
QString id = IMAGE_ID.arg(imageId);
|
|
|
|
BooksImageProvider::instance()->addImage(iModel, id, task->iImage);
|
|
|
|
Q_EMIT imagePressed(IMAGE_URL.arg(BooksImageProvider::PROVIDER_ID, id),
|
|
|
|
task->iRect);
|
2017-09-07 19:03:17 +03:00
|
|
|
} else if (!iData.isNull()) {
|
|
|
|
if (iStartSelectionTask) iStartSelectionTask->release(this);
|
|
|
|
iStartSelectionTask = new StartSelectionTask(iData,
|
|
|
|
task->iX, task->iY, width(), height());
|
|
|
|
iTaskQueue->submit(iStartSelectionTask, this,
|
|
|
|
SLOT(onStartSelectionTaskDone()));
|
2016-10-08 14:56:14 +03:00
|
|
|
}
|
|
|
|
|
2016-10-16 20:07:03 +03:00
|
|
|
task->release(this);
|
2016-10-08 14:56:14 +03:00
|
|
|
}
|
|
|
|
|
2015-06-28 14:22:35 +03:00
|
|
|
void BooksPageWidget::updateSize()
|
|
|
|
{
|
2015-07-03 17:24:20 +03:00
|
|
|
HDEBUG("page" << iPage << QSize(width(), height()));
|
2016-11-02 16:33:26 +03:00
|
|
|
iImage = QImage();
|
2015-06-28 14:22:35 +03:00
|
|
|
resetView();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::onWidthChanged()
|
|
|
|
{
|
2017-01-30 00:56:23 +03:00
|
|
|
HVERBOSE((int)width());
|
2015-06-28 14:22:35 +03:00
|
|
|
// Width change will probably be followed by height change
|
|
|
|
iResizeTimer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::onHeightChanged()
|
|
|
|
{
|
2017-01-30 00:56:23 +03:00
|
|
|
HVERBOSE((int)height());
|
2015-06-28 14:22:35 +03:00
|
|
|
if (iResizeTimer->isActive()) {
|
|
|
|
// Height is usually changed after width, repaint right away
|
|
|
|
iResizeTimer->stop();
|
|
|
|
updateSize();
|
|
|
|
} else {
|
|
|
|
iResizeTimer->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::onResizeTimeout()
|
|
|
|
{
|
|
|
|
// This can only happen if only width or height has changed. Normally,
|
|
|
|
// width change is followed by height change and view is reset from the
|
|
|
|
// setHeight() method
|
|
|
|
updateSize();
|
|
|
|
}
|
2016-10-08 14:56:14 +03:00
|
|
|
|
|
|
|
void BooksPageWidget::handleLongPress(int aX, int aY)
|
|
|
|
{
|
|
|
|
HDEBUG(aX << aY);
|
|
|
|
if (!iResetTask && !iRenderTask && !iData.isNull()) {
|
|
|
|
if (iLongPressTask) iLongPressTask->release(this);
|
2017-09-07 19:03:17 +03:00
|
|
|
iTaskQueue->submit(iLongPressTask = new PressTask(iData, aX, aY),
|
|
|
|
this, SLOT(onLongPressTaskDone()));
|
2016-10-08 14:56:14 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-16 20:07:03 +03:00
|
|
|
|
|
|
|
void BooksPageWidget::handlePress(int aX, int aY)
|
|
|
|
{
|
|
|
|
HDEBUG(aX << aY);
|
|
|
|
if (!iResetTask && !iRenderTask && !iData.isNull()) {
|
|
|
|
if (iPressTask) iPressTask->release(this);
|
2017-09-07 19:03:17 +03:00
|
|
|
iTaskQueue->submit(iPressTask = new PressTask(iData, aX, aY),
|
|
|
|
this, SLOT(onPressTaskDone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::handlePositionChanged(int aX, int aY)
|
|
|
|
{
|
|
|
|
if (iSelecting && !iData.isNull()) {
|
|
|
|
HDEBUG(aX << aY);
|
|
|
|
// Drop the tasks which haven't been started yet
|
|
|
|
ExtendSelectionTask* task;
|
|
|
|
for (int i = iExtendSelectionTasks.count()-1; i>=0; i--) {
|
|
|
|
task = iExtendSelectionTasks.at(i);
|
|
|
|
if (task->isStarted()) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
task->release(this);
|
|
|
|
iExtendSelectionTasks.removeAt(i);
|
|
|
|
HDEBUG("dropped queued task," << i << "left");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
task = new ExtendSelectionTask(iData, aX, aY, width(), height());
|
|
|
|
iTaskQueue->submit(task, this, SLOT(onExtendSelectionTaskDone()));
|
|
|
|
iExtendSelectionTasks.append(task);
|
|
|
|
} else {
|
|
|
|
// Finger was moved before we entered selection mode
|
|
|
|
if (iStartSelectionTask) {
|
|
|
|
iStartSelectionTask->release(this);
|
|
|
|
iStartSelectionTask = NULL;
|
|
|
|
HDEBUG("oops");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BooksPageWidget::clearSelection()
|
|
|
|
{
|
|
|
|
if (!iData.isNull()) {
|
|
|
|
if (iClearSelectionTask) iClearSelectionTask->release(this);
|
|
|
|
iTaskQueue->submit(iClearSelectionTask =
|
|
|
|
new ClearSelectionTask(iData, width(), height()),
|
|
|
|
this, SLOT(onClearSelectionTaskDone()));
|
|
|
|
}
|
|
|
|
if (iSelecting) {
|
|
|
|
iSelecting = false;
|
|
|
|
Q_EMIT selectingChanged();
|
2016-10-16 20:07:03 +03:00
|
|
|
}
|
|
|
|
}
|