-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathaddons-context.cpp
More file actions
120 lines (96 loc) · 3.61 KB
/
Copy pathaddons-context.cpp
File metadata and controls
120 lines (96 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (C) 2023-2025 Nick Egorrov, nicegorov@yandex.ru
*
* This file is part of GCodeWorkShop.
*
* GCodeWorkShop is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm> // for max, min
#include <QChar> // for QChar::LineFeed, QChar::ParagraphSeparator
#include <QPlainTextEdit> // for QPlainTextEdit
#include <QTextCursor> // for QTextCursor, QTextCursor::KeepAnchor, QTextCursor::MoveAnchor, QTextCursor::End
#include <document.h> // for Document
#include <gcodeworkshop.h> // for GCodeWorkShop
#include <gcoderdocument.h> //
#include "addons-context.h"
bool Addons::Context::pull(int mode)
{
GCoderDocument* gdoc = dynamic_cast<GCoderDocument*>(GCodeWorkShop::instance()->activeDocument());
if (!gdoc) {
return false;
}
m_mode = mode;
m_edit = gdoc->textEdit();
QTextCursor cursor(m_edit->document());
m_selectionStart = m_edit->textCursor().selectionStart();
m_selectionEnd = m_edit->textCursor().selectionEnd();
m_fragmentStart = std::min(m_selectionStart, m_selectionEnd);
m_fragmentEnd = std::max(m_selectionStart, m_selectionEnd);
switch (m_mode) {
case Addons::Context::SELECTED:
if (m_fragmentStart == m_fragmentEnd) {
return false;
}
cursor.setPosition(m_fragmentEnd, QTextCursor::MoveAnchor);
cursor.setPosition(m_fragmentStart, QTextCursor::KeepAnchor);
break;
case Addons::Context::SELECTED_OR_ALL:
if (m_fragmentStart == m_fragmentEnd) {
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::Start, QTextCursor::KeepAnchor);
} else {
cursor.setPosition(m_fragmentEnd, QTextCursor::MoveAnchor);
cursor.setPosition(m_fragmentStart, QTextCursor::KeepAnchor);
}
break;
case Addons::Context::SELECTED_BLOCKS:
cursor.setPosition(m_fragmentEnd, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
cursor.setPosition(m_fragmentStart, QTextCursor::KeepAnchor);
cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
break;
case Addons::Context::ALL:
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::Start, QTextCursor::KeepAnchor);
break;
default:
return false;
}
m_fragmentStart = cursor.position();
m_fragmentEnd = cursor.anchor();
m_text = cursor.selectedText();
m_text.replace(QChar::ParagraphSeparator, QChar::LineFeed);
return true;
}
void Addons::Context::push(const QString& text)
{
QTextCursor cursor(m_edit->document());
cursor.setPosition(m_fragmentEnd, QTextCursor::MoveAnchor);
cursor.setPosition(m_fragmentStart, QTextCursor::KeepAnchor);
cursor.insertText(text);
int diff = text.size() - m_fragmentEnd + m_fragmentStart;
if (m_selectionStart == m_fragmentEnd) {
m_selectionStart += diff;
}
if (m_selectionEnd == m_fragmentEnd) {
m_selectionEnd += diff;
}
cursor.setPosition(m_selectionEnd, QTextCursor::MoveAnchor);
cursor.setPosition(m_selectionStart, QTextCursor::KeepAnchor);
m_edit->setTextCursor(cursor);
}
void Addons::Context::push()
{
push(m_text);
}