7#include "textautogeneratelistviewtextselection.h"
8#include "textautogeneratelistviewdelegate.h"
11#include <QTextDocument>
12#include <QTextDocumentFragment>
13using namespace TextAutogenerateText;
15TextAutogenerateListViewTextSelection::TextAutogenerateListViewTextSelection(TextAutogenerateListViewDelegate *delegate,
QObject *parent)
21bool TextAutogenerateListViewTextSelection::hasSelection()
const
23 return mStartIndex.isValid() && mEndIndex.isValid() && ((mStartPos > -1 && mEndPos > -1 && mStartPos != mEndPos));
26TextAutogenerateListViewTextSelection::OrderedPositions TextAutogenerateListViewTextSelection::orderedPositions()
const
28 Q_ASSERT(!mStartIndex.isValid() || !mEndIndex.isValid() || mStartIndex.model() == mEndIndex.model());
29 TextAutogenerateListViewTextSelection::OrderedPositions ret{mStartIndex.row(), mStartPos, mEndIndex.row(), mEndPos};
30 if (ret.fromRow > ret.toRow) {
31 std::swap(ret.fromRow, ret.toRow);
32 std::swap(ret.fromCharPos, ret.toCharPos);
37QTextCursor TextAutogenerateListViewTextSelection::selectionForIndex(
const QModelIndex &index, QTextDocument *doc)
const
39 if (!hasSelection()) {
42 Q_ASSERT(index.
model() == mStartIndex.model());
43 Q_ASSERT(index.
model() == mEndIndex.model());
45 const OrderedPositions ordered = orderedPositions();
46 int fromCharPos = ordered.fromCharPos;
47 int toCharPos = ordered.toCharPos;
49 QTextCursor cursor(doc);
52 const int row = index.
row();
53 if (row == ordered.fromRow)
54 cursor.setPosition(qMax(fromCharPos, 0));
55 else if (row > ordered.fromRow)
56 cursor.setPosition(0);
59 if (row == ordered.toRow)
61 else if (row < ordered.toRow)
68void TextAutogenerateListViewTextSelection::selectionText(
const OrderedPositions ordered,
71 const QModelIndex &index,
75 const QTextCursor cursor = selectionForIndex(index, doc);
76 const QTextDocumentFragment fragment(cursor);
77 str += format == Format::Text ? fragment.toPlainText() : fragment.toHtml();
78 if (row < ordered.toRow) {
79 str += QLatin1Char(
'\n');
83QString TextAutogenerateListViewTextSelection::selectedText(Format format)
const
85 if (!hasSelection()) {
88 const OrderedPositions ordered = orderedPositions();
90 for (
int row = ordered.fromRow; row <= ordered.toRow; ++row) {
91 const QModelIndex index = QModelIndex(mStartIndex).
siblingAtRow(row);
92 QTextDocument *doc = mDelegate->documentForIndex(index, -1);
94 selectionText(ordered, format, row, index, doc, str);
100bool TextAutogenerateListViewTextSelection::contains(
const QModelIndex &index,
int charPos)
const
102 if (!hasSelection()) {
105 Q_ASSERT(index.
model() == mStartIndex.model());
106 const int row = index.
row();
107 const OrderedPositions ordered = orderedPositions();
108 if (row == ordered.fromRow) {
109 if (row == ordered.toRow)
110 return ordered.fromCharPos <= charPos && charPos <= ordered.toCharPos;
111 return ordered.fromCharPos <= charPos;
112 }
else if (row == ordered.toRow) {
113 return charPos <= ordered.toCharPos;
115 return row > ordered.fromRow && row < ordered.toRow;
119void TextAutogenerateListViewTextSelection::clear()
121 const QModelIndex index = mStartIndex;
122 const OrderedPositions ordered = orderedPositions();
124 mStartIndex = QPersistentModelIndex{};
125 mEndIndex = QPersistentModelIndex{};
130 if (ordered.fromRow > -1) {
131 if (ordered.toRow > -1) {
132 for (
int row = ordered.fromRow; row <= ordered.toRow; ++row) {
136 Q_EMIT repaintNeeded(index);
141void TextAutogenerateListViewTextSelection::setTextSelectionStart(
const QModelIndex &index,
int charPos)
149void TextAutogenerateListViewTextSelection::setTextSelectionEnd(
const QModelIndex &index,
int charPos)
151 int from = mEndIndex.row();
152 int to = index.
row();
153 if (from != -1 && from != to) {
165 for (
int row = from; row <= to; ++row) {
174void TextAutogenerateListViewTextSelection::selectWord(
const QModelIndex &index,
int charPos, QTextDocument *doc)
176 QTextCursor cursor(doc);
186void TextAutogenerateListViewTextSelection::selectWordUnderCursor(
const QModelIndex &index,
int charPos)
188 QTextDocument *doc = mDelegate->documentForIndex(index, -1);
189 selectWord(index, charPos, doc);
192void TextAutogenerateListViewTextSelection::selectMessage(
const QModelIndex &index)
199 QTextDocument *doc = mDelegate->documentForIndex(index, -1);
205bool TextAutogenerateListViewTextSelection::mightStartDrag()
const
207 return mMightStartDrag;
210void TextAutogenerateListViewTextSelection::setMightStartDrag(
bool newMightStartDrag)
212 mMightStartDrag = newMightStartDrag;
215#include "moc_textautogeneratelistviewtextselection.cpp"
bool isValid() const const
const QAbstractItemModel * model() const const
QModelIndex siblingAtRow(int row) const const
void select(SelectionType selection)
int selectionEnd() const const
int selectionStart() const const
void setPosition(int pos, MoveMode m)
int characterCount() const const