-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorwatcher.cpp
More file actions
168 lines (126 loc) · 4.62 KB
/
colorwatcher.cpp
File metadata and controls
168 lines (126 loc) · 4.62 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "colorwatcher.h"
// std includes
#include <set>
// Qt includes
#include <QDebug> //REMOVEME
#include <QRegularExpression>
#include <QTextBlock>
#include <QTextCursor>
// QtCreator includes
#include <coreplugin/editormanager/editormanager.h>
#include <texteditor/texteditor.h>
// Plugin includes
#include "colorpickerconstants.h"
using namespace Core;
using namespace TextEditor;
namespace ColorPicker {
namespace Internal {
static QMultiMap<ColorFormat, QRegularExpression> colorRegexes
{
{ ColorFormat::QCssRgbUCharFormat, Constants::REGEX_QCSS_RGB_UCHAR },
{ ColorFormat::QCssRgbUCharFormat, Constants::REGEX_QCSS_RGBA_UCHAR },
{ ColorFormat::QCssRgbPercentFormat, Constants::REGEX_QCSS_RGB_PERCENT },
{ ColorFormat::QCssRgbPercentFormat, Constants::REGEX_QCSS_RGBA_PERCENT },
{ ColorFormat::QssHsvFormat, Constants::REGEX_QSS_HSV },
{ ColorFormat::QssHsvFormat, Constants::REGEX_QSS_HSVA },
{ ColorFormat::CssHslFormat, Constants::REGEX_CSS_HSL },
{ ColorFormat::CssHslFormat, Constants::REGEX_CSS_HSLA },
{ ColorFormat::QmlRgbaFormat, Constants::REGEX_QML_RGBA },
{ ColorFormat::QmlHslaFormat, Constants::REGEX_QML_HSLA },
{ ColorFormat::GlslFormat, Constants::REGEX_VEC3 },
{ ColorFormat::GlslFormat, Constants::REGEX_VEC4 },
{ ColorFormat::HexFormat, Constants::REGEX_HEXCOLOR }
};
////////////////////////// ColorWatcherImpl //////////////////////////
class ColorWatcherImpl
{
public:
ColorWatcherImpl();
~ColorWatcherImpl();
/* functions */
void updateSearchFormats();
/* variables */
TextEditor::TextEditorWidget *watched;
ColorCategory category;
ColorFormatSet searchFormats;
};
ColorWatcherImpl::ColorWatcherImpl() :
watched(nullptr),
category(ColorCategory::AnyCategory),
searchFormats()
{}
ColorWatcherImpl::~ColorWatcherImpl()
{}
void ColorWatcherImpl::updateSearchFormats()
{
searchFormats = formatsFromCategory(category);
}
////////////////////////// ColorWatcher //////////////////////////
ColorWatcher::ColorWatcher(TextEditorWidget *textEditor) :
QObject(textEditor),
d(new ColorWatcherImpl)
{
Q_ASSERT_X(textEditor, Q_FUNC_INFO, "ColorPickerPlugin > The text editor is invalid.");
d->watched = textEditor;
d->updateSearchFormats();
}
ColorWatcher::~ColorWatcher()
{
// disconnect if necessary
}
ColorCategory ColorWatcher::colorCategory() const
{
return d->category;
}
void ColorWatcher::setColorCategory(ColorCategory category)
{
if (d->category != category) {
d->category = category;
d->updateSearchFormats();
}
}
ColorExpr ColorWatcher::process()
{
ColorExpr ret;
QTextCursor currentCursor = d->watched->textCursor();
QRect cursorRect = d->watched->cursorRect();
// Search for a color pattern
QString lineText = currentCursor.block().text();
for (auto it = colorRegexes.begin(); it != colorRegexes.end(); ++it) {
ColorFormat format = it.key();
if (!d->searchFormats.contains(format))
continue;
QRegularExpressionMatchIterator matchIt = it.value().globalMatch(lineText);
if (matchIt.hasNext()) {
QRegularExpressionMatch match = matchIt.next();
int cursorPosInLine = currentCursor.positionInBlock();
int capturedStart = match.capturedStart();
int capturedEnd = match.capturedEnd();
bool cursorIsUnderColor = (cursorPosInLine >= capturedStart) &&
(cursorPosInLine <= capturedEnd);
if (cursorIsUnderColor) {
// If a part of the selection is already selected, deselect it
if (currentCursor.hasSelection())
currentCursor.clearSelection();
// Select the expression
currentCursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor,
cursorPosInLine - capturedStart);
cursorRect.setLeft(d->watched->cursorRect(currentCursor).left());
currentCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,
capturedEnd - capturedStart);
cursorRect.setRight(d->watched->cursorRect(currentCursor).right());
d->watched->setTextCursor(currentCursor);
//
QColor color = parseColor(format, match);
ret.format = format;
ret.value = color;
break;
}
}
}
ret.pos = QPoint(cursorRect.center().x(),
cursorRect.bottom() + 2);
return ret;
}
} // namespace Internal
} // namespace ColorPicker