[fbreader] Added optional arguments to ZLTextSelectionModel methods

... extendTo() and selectWord() to make it easier to implement selection
by words.
This commit is contained in:
Slava Monich 2017-09-07 18:16:03 +03:00
parent c8bad41b7f
commit 6f6e037eb3
2 changed files with 28 additions and 4 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (C) 2004-2010 Geometer Plus <contact@geometerplus.com>
* Copyright (C) 2016-2017 Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -144,7 +145,7 @@ bool ZLTextSelectionModel::BoundElement::operator != (const ZLTextSelectionModel
return !operator == (element);
}
ZLTextSelectionModel::ExtensionResult ZLTextSelectionModel::extendTo(int x, int y) {
ZLTextSelectionModel::ExtensionResult ZLTextSelectionModel::extendTo(int x, int y, bool word) {
if (!myIsActive || myArea.myTextElementMap.empty()) {
return BOUND_NOT_CHANGED;
}
@ -155,6 +156,25 @@ ZLTextSelectionModel::ExtensionResult ZLTextSelectionModel::extendTo(int x, int
myStoredX = x;
myStoredY = y;
if (word) {
const bool swap = (mySecondBound < myFirstBound);
Bound &from = swap ? mySecondBound : myFirstBound;
Bound &to = swap ? myFirstBound : mySecondBound;
BoundElement &after = from.After;
BoundElement &before = to.Before;
ZLTextWordCursor cursor = myArea.startCursor();
cursor.moveToParagraph(before.ParagraphIndex);
cursor.moveTo(before.ElementIndex, before.CharIndex);
const ZLTextElement &element = cursor.element();
if (element.kind() == ZLTextElement::WORD_ELEMENT) {
const ZLTextWord &word = (const ZLTextWord&)element;
before.CharIndex = word.Length;
}
after.CharIndex = 0;
from.Before = after;
to.After = before;
}
ExtensionResult result = BOUND_NOT_CHANGED;
if ((oldRange.first != newRange.first) || (oldRange.second != newRange.second)) {
myTextIsUpToDate = false;
@ -460,7 +480,7 @@ shared_ptr<ZLImageData> ZLTextSelectionModel::image() const {
return myImage;
}
bool ZLTextSelectionModel::selectWord(int x, int y) {
bool ZLTextSelectionModel::selectWord(int x, int y, bool activate) {
clear();
const ZLTextElementRectangle *rectangle = myArea.elementByCoordinates(x, y);
@ -517,6 +537,9 @@ bool ZLTextSelectionModel::selectWord(int x, int y) {
mySecondBound.Before.CharIndex = endIndex;
mySecondBound.After = mySecondBound.Before;
myStoredX = x;
myStoredY = y;
myIsActive = activate;
myIsEmpty = false;
myTextIsUpToDate = false;
myRangeVectorIsUpToDate = false;

View file

@ -1,5 +1,6 @@
/*
* Copyright (C) 2004-2010 Geometer Plus <contact@geometerplus.com>
* Copyright (C) 2016-2017 Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -58,13 +59,13 @@ public:
BOUND_OVER_BEFORE,
BOUND_OVER_AFTER
};
ExtensionResult extendTo(int x, int y);
ExtensionResult extendTo(int x, int y, bool word = false);
void invalidate();
void update();
void deactivate();
void clear();
bool selectWord(int x, int y);
bool selectWord(int x, int y, bool activate = false);
void extendWordSelectionToParagraph();
const std::string &text() const;