-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorpickerplugin.cpp
More file actions
254 lines (188 loc) · 7.55 KB
/
colorpickerplugin.cpp
File metadata and controls
254 lines (188 loc) · 7.55 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "colorpickerplugin.h"
#include "colorpickerplugin_p.h"
// Qt includes
#include <QMenu>
// QtCreator includes
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <cppeditor/cppeditorconstants.h>
#include <extensionsystem/pluginmanager.h>
#include <glsleditor/glsleditorconstants.h>
#include <qmljseditor/qmljseditorconstants.h>
#include <texteditor/texteditor.h>
#include <utils/theme/theme.h>
// Plugin includes
#include "colorpickeroptionspage.h"
#include "colorpickerconstants.h"
using namespace Core;
using namespace TextEditor;
namespace ColorPicker {
namespace Internal {
////////////////////////// ColorPickerPluginImpl //////////////////////////
ColorPickerPluginImpl::ColorPickerPluginImpl(ColorPickerPlugin *qq) :
q(qq),
watchers(),
colorModifier(new ColorModifier(qq)),
colorEditor(nullptr),
generalSettings()
{}
ColorPickerPluginImpl::~ColorPickerPluginImpl()
{}
////////////////////////// ColorPickerPlugin //////////////////////////
ColorPickerPlugin::ColorPickerPlugin() :
d(new ColorPickerPluginImpl(this))
{
}
ColorPickerPlugin::~ColorPickerPlugin()
{}
bool ColorPickerPlugin::initialize(const QStringList & /* arguments */, QString * /* errorMessage */)
{
auto *optionsPage = new ColorPickerOptionsPage;
d->generalSettings = optionsPage->generalSettings();
connect(optionsPage, &ColorPickerOptionsPage::generalSettingsChanged,
this, &ColorPickerPlugin::onGeneralSettingsChanged);
// Register the plugin actions
ActionContainer *toolsContainer = ActionManager::actionContainer(Core::Constants::M_TOOLS);
ActionContainer *myContainer = ActionManager::createMenu("ColorPicker");
QMenu *myMenu = myContainer->menu();
myMenu->setTitle(tr("&ColorPicker"));
myMenu->setEnabled(true);
auto triggerColorEditAction = new QAction(tr(Constants::ACTION_NAME_TRIGGER_COLOR_EDIT), this);
Command *command = ActionManager::registerAction(triggerColorEditAction,
Constants::TRIGGER_COLOR_EDIT);
command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+C")));
myContainer->addAction(command);
connect(triggerColorEditAction, &QAction::triggered,
this, &ColorPickerPlugin::onColorEditTriggered);
toolsContainer->addMenu(myContainer);
// Register objects
addAutoReleasedObject(optionsPage);
return true;
}
void ColorPickerPlugin::extensionsInitialized()
{
d->colorEditor = new ColorEditor; // no parent
// Create connections between internal objects
setInsertOnChange(d->generalSettings.m_insertOnChange);
connect(d->colorEditor, &ColorEditor::colorSelected,
this, &ColorPickerPlugin::onColorSelected);
connect(EditorManager::instance(), &EditorManager::editorAboutToClose,
this, &ColorPickerPlugin::onEditorAboutToClose);
}
void ColorPickerPlugin::onColorEditTriggered()
{
IEditor *currentEditor = EditorManager::instance()->currentEditor();
if (!currentEditor)
return;
TextEditorWidget *editorWidget = qobject_cast<TextEditorWidget *>(currentEditor->widget());
if (editorWidget) {
ColorCategory cat = (d->generalSettings.m_editorSensitive)
? colorCategoryForEditor(currentEditor)
: ColorCategory::AnyCategory;
ColorWatcher *watcher = nullptr;
if (!d->watchers.contains(currentEditor)) {
watcher = new ColorWatcher(editorWidget);
watcher->setColorCategory(cat);
d->watchers.insert(currentEditor, watcher);
}
else {
watcher = d->watchers.value(currentEditor);
}
Q_ASSERT(watcher);
d->colorEditor->setColorCategory(cat);
ColorExpr toEdit = watcher->process();
if (toEdit.value.isValid()) {
d->colorEditor->setOutputFormat(toEdit.format);
d->colorEditor->setColor(toEdit.value);
} else {
d->colorEditor->setColor(d->colorEditor->color());
}
QWidget *editorViewport = editorWidget->viewport();
d->colorEditor->setParent(editorViewport);
d->colorEditor->move(clampColorEditorPosition(toEdit.pos, editorViewport->rect()));
d->colorEditor->show();
}
}
void ColorPickerPlugin::onGeneralSettingsChanged(const GeneralSettings &gs)
{
bool editorSensitiveChanged = (d->generalSettings.m_editorSensitive != gs.m_editorSensitive);
bool insertOnChangeChanged = (d->generalSettings.m_insertOnChange != gs.m_insertOnChange);
// Setting : editor sensitive
if (editorSensitiveChanged) {
for (auto it = d->watchers.begin(); it != d->watchers.end(); ++it) {
ColorCategory newCat = (gs.m_editorSensitive) ? colorCategoryForEditor(it.key())
: ColorCategory::AnyCategory;
ColorWatcher *watcher = it.value();
watcher->setColorCategory(newCat);
// Update the color editor
TextEditorWidget *editorWidget = qobject_cast<TextEditorWidget *>(it.key()->widget());
Q_ASSERT(editorWidget);
if (d->colorEditor->parentWidget() == editorWidget->viewport())
d->colorEditor->setColorCategory(newCat);
}
}
// Setting : insert on change
if (insertOnChangeChanged)
setInsertOnChange(gs.m_insertOnChange);
d->generalSettings = gs;
}
void ColorPickerPlugin::onEditorAboutToClose()
{
d->colorEditor->setParent(0);
}
void ColorPickerPlugin::onColorSelected(const QColor &color, ColorFormat format)
{
d->colorModifier->insertColor(color, format);
}
void ColorPickerPlugin::onColorChanged(const QColor &color)
{
d->colorModifier->insertColor(color, d->colorEditor->outputFormat());
}
void ColorPickerPlugin::onOutputFormatChanged(ColorFormat format)
{
d->colorModifier->insertColor(d->colorEditor->color(), format);
}
void ColorPickerPlugin::setInsertOnChange(bool enable)
{
if (enable) {
connect(d->colorEditor, &ColorEditor::colorChanged,
this, &ColorPickerPlugin::onColorChanged);
connect(d->colorEditor, &ColorEditor::outputFormatChanged,
this, &ColorPickerPlugin::onOutputFormatChanged);
}
else {
disconnect(d->colorEditor, &ColorEditor::colorChanged,
this, &ColorPickerPlugin::onColorChanged);
disconnect(d->colorEditor, &ColorEditor::outputFormatChanged,
this, &ColorPickerPlugin::onOutputFormatChanged);
}
}
ColorCategory ColorPickerPlugin::colorCategoryForEditor(IEditor *editor) const
{
ColorCategory ret = ColorCategory::AnyCategory;
Id lastId = *(--editor->context().end());
if (lastId == QmlJSEditor::Constants::C_QMLJSEDITOR_ID)
ret = ColorCategory::QmlCategory;
else if (lastId == GlslEditor::Constants::C_GLSLEDITOR_ID)
ret = ColorCategory::GlslCategory;
return ret;
}
QPoint ColorPickerPlugin::clampColorEditorPosition(const QPoint &cursorPos, const QRect &rect) const
{
QPoint ret;
ret.ry() = cursorPos.y();
int colorEditorWidth = d->colorEditor->width();
int colorEditorHalfWidth = (colorEditorWidth / 2);
int posX = cursorPos.x() - colorEditorHalfWidth;
int widgetRight = rect.right();
if (posX < 0)
posX = 0;
else if ( (cursorPos.x() + colorEditorHalfWidth) > (widgetRight) )
posX = widgetRight - colorEditorWidth;
ret.rx() = posX;
return ret;
}
} // namespace Internal
} // namespace ColorPicker