Krita Source Code Documentation
Loading...
Searching...
No Matches
KoToolBox.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2005-2009 Thomas Zander <zander@kde.org>
3 * SPDX-FileCopyrightText: 2009 Peter Simonsson <peter.simonsson@gmail.com>
4 * SPDX-FileCopyrightText: 2010 Cyrille Berger <cberger@cberger.net>
5 * SPDX-FileCopyrightText: 2022 Alvin Wong <alvin@alvinhc.com>
6 *
7 * SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9
10#include "KoToolBox_p.h"
11#include "KoToolBoxLayout_p.h"
12#include "KoToolBoxButton_p.h"
13#include "kis_assert.h"
14
15#include <QButtonGroup>
16#include <QToolButton>
17#include <QStyleOption>
18#include <QActionGroup>
19#include <QPainter>
20#include <QHash>
21#include <QApplication>
22#include <QStyle>
23#include <QTimer>
24#include <QMenu>
25#include <QAction>
26#include <QScreen>
27
28#include <klocalizedstring.h>
29#include <WidgetsDebug.h>
30#include <kconfiggroup.h>
31#include <ksharedconfig.h>
32#include <KisPortingUtils.h>
33
34#include <kactioncollection.h>
35#include <KisViewManager.h>
36#include <KoCanvasController.h>
37#include <KoShapeLayer.h>
38
39#define BUTTON_MARGIN 10
40
41static int buttonSize(int screen)
42{
43 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(screen < QGuiApplication::screens().size() && screen >= 0, 16);
44
45 QRect rc = QGuiApplication::screens().at(screen)->availableGeometry();
46 if (rc.width() <= 1024) {
47 return 12;
48 }
49 else if (rc.width() <= 1377) {
50 return 14;
51 }
52 else if (rc.width() <= 1920 ) {
53 return 16;
54 }
55 else {
56 return 22;
57 }
58
59}
60
62{
63public:
64 void addSection(Section *section, const QString &name);
65
68 QHash<QString, KoToolBoxButton*> buttonsByToolId;
69 QMap<QString, Section*> sections;
71 QButtonGroup *buttonGroup {0};
72 QHash<QToolButton*, QString> visibilityCodes;
73 bool floating {false};
74 int iconSize {0};
75 QMap<QAction*,int> contextIconSizes;
78};
79
80void KoToolBox::Private::addSection(Section *section, const QString &name)
81{
82 section->setName(name);
83 layout->addSection(section);
84 sections.insert(name, section);
85}
86
88 : d(new Private)
89{
90 d->layout = new KoToolBoxLayout(this);
91 // add defaults
92 d->addSection(new Section(this), "main");
93 d->addSection(new Section(this), "dynamic");
94
95 d->buttonGroup = new QButtonGroup(this);
96
97 // Get screen the widget exists in, but fall back to primary screen if invalid.
98 const int widgetsScreen = KisPortingUtils::getScreenNumberForWidget(QApplication::activeWindow());
99 const int primaryScreen = 0; //In QT, primary screen should always be the first index of QGuiApplication::screens()
100 const int screen = (widgetsScreen >= 0 && widgetsScreen < QGuiApplication::screens().size()) ? widgetsScreen : primaryScreen;
101 const int toolbuttonSize = buttonSize(screen);
102 KConfigGroup cfg = KSharedConfig::openConfig()->group("KoToolBox");
103 d->iconSize = cfg.readEntry("iconSize", toolbuttonSize);
104
105 setCompact(cfg.readEntry<bool>("compact", false));
106}
107
109{
110 delete d;
111}
112
114{
115 Q_FOREACH (QToolButton *button, d->buttons) {
116 button->setIconSize(QSize(d->iconSize, d->iconSize));
117 }
118
119 Q_FOREACH (Section *section, d->sections.values()) {
121 }
122}
123
124
126{
127 d->toolManager = viewManager->toolManager();
128
129 Q_FOREACH (KoToolAction *toolAction, d->toolManager->toolActionList()) {
130 addButton(toolAction);
131 }
132
134
135 connect(d->toolManager, SIGNAL(changedTool(KoCanvasController*)),
136 this, SLOT(setActiveTool(KoCanvasController*)));
137 connect(d->toolManager, SIGNAL(currentLayerChanged(const KoCanvasController*,const KoShapeLayer*)),
138 this, SLOT(setCurrentLayer(const KoCanvasController*,const KoShapeLayer*)));
139 connect(d->toolManager, SIGNAL(toolCodesSelected(QList<QString>)), this, SLOT(setButtonsVisible(QList<QString>)));
140 connect(d->toolManager,
141 SIGNAL(addedTool(KoToolAction*,KoCanvasController*)),
143
145
146 KisKActionCollection *actionCollection = viewManager->actionCollection();
147 Q_FOREACH(KoToolAction *toolAction, d->toolManager->toolActionList()) {
148 QAction *toolQAction = actionCollection->action(toolAction->id());
149 auto button = d->buttonsByToolId.find(toolAction->id());
150 if (button == d->buttonsByToolId.end()) {
151 qWarning() << "Toolbox is missing button for tool" << toolAction->id();
152 continue;
153 }
154 (*button)->attachAction(toolQAction);
155 }
156}
157
159{
160 KoToolBoxButton *button = new KoToolBoxButton(toolAction, this);
161
162 d->buttons << button;
163
164 QString sectionToBeAddedTo;
165 const QString section = toolAction->section();
166 if (section.contains(qApp->applicationName())) {
167 sectionToBeAddedTo = "main";
168 } else if (section.contains("main")) {
169 sectionToBeAddedTo = "main";
170 } else if (section.contains("dynamic")) {
171 sectionToBeAddedTo = "dynamic";
172 } else {
173 sectionToBeAddedTo = section;
174 }
175
176 Section *sectionWidget = d->sections.value(sectionToBeAddedTo);
177 if (sectionWidget == 0) {
178 sectionWidget = new Section(this);
179 d->addSection(sectionWidget, sectionToBeAddedTo);
180 }
181 sectionWidget->addButton(button, toolAction->priority());
182
183 d->buttonGroup->addButton(button);
184 d->buttonsByToolId.insert(toolAction->id(), button);
185
186 d->visibilityCodes.insert(button, toolAction->visibilityCode());
187}
188
190{
191 // verify the canvas comes from the same window
193
194 QString id = d->toolManager->activeToolId();
196 if (button) {
197 button->setChecked(true);
198 button->setHighlightColor();
199 if (d->selectedButton) {
201 }
203 }
204 else {
205 warnWidgets << "KoToolBox::setActiveTool(" << id << "): no such button found";
206 }
207}
208
210{
211 Q_FOREACH (QToolButton *button, d->visibilityCodes.keys()) {
212 QString code = d->visibilityCodes.value(button);
213
214 if (code.startsWith(QLatin1String("flake/"))) {
215 continue;
216 }
217
218 if (code.endsWith( QLatin1String( "/always"))) {
219 button->setVisible(true);
220 button->setEnabled(true);
221 }
222 else if (code.isEmpty()) {
223 button->setVisible(true);
224 button->setEnabled( codes.count() != 0 );
225 }
226 else {
227 button->setVisible( codes.contains(code) );
228 }
229 }
230
231 KConfigGroup cfg = KSharedConfig::openConfig()->group("KoToolBox");
232 QList<QString> disabledTools = cfg.readEntry("disabledTools", QStringList());
233
234 //Make sure to disable disabled tools
235 if (disabledTools.isEmpty()) {
236 layout()->invalidate();
237 update();
238 } else {
239 disableButtonsByName(disabledTools);
240 }
241}
242
244{
245 Q_UNUSED(canvas);
246 const bool enabled = layer == 0 || (layer->isShapeEditable() && layer->isVisible());
247 foreach (QToolButton *button, d->visibilityCodes.keys()) {
248 if (d->visibilityCodes[button].endsWith( QLatin1String( "/always") ) ) {
249 continue;
250 }
251 button->setEnabled(enabled);
252 }
253}
254
255void KoToolBox::paintEvent(QPaintEvent *)
256{
257 QPainter painter(this);
258
259 const QList<Section*> sections = d->sections.values();
260 int halfSpacing = layout()->spacing();
261 if (halfSpacing > 0) {
262 halfSpacing /= 2;
263 }
264 for(auto iterator = sections.begin(); iterator != sections.end(); ++iterator) {
265 Section *section = *iterator;
266 if (section->separators() == Section::SeparatorNone) {
267 continue;
268 }
269
270 QStyleOption styleoption;
271 styleoption.palette = palette();
272
273 if (section->separators() & Section::SeparatorTop) {
274 int y = section->y() - halfSpacing;
275 styleoption.state = QStyle::State_None;
276 styleoption.rect = QRect(section->x(), y-1, section->width(), 2);
277
278 style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &styleoption, &painter);
279 }
280
281 if (section->separators() & Section::SeparatorLeft && section->isLeftToRight()) {
282 int x = section->x() - halfSpacing;
283 styleoption.state = QStyle::State_Horizontal;
284 styleoption.rect = QRect(x-1, section->y(), 2, section->height());
285
286 style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &styleoption, &painter);
287 } else if (section->separators() & Section::SeparatorLeft && section->isRightToLeft()) {
288 int x = section->x() + section->width() + halfSpacing;
289 styleoption.state = QStyle::State_Horizontal;
290 styleoption.rect = QRect(x-1, section->y(), 2, section->height());
291
292 style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &styleoption, &painter);
293 }
294 }
295
296 painter.end();
297}
298
299void KoToolBox::changeEvent(QEvent *event)
300{
301 QWidget::changeEvent(event);
302 if (event->type() == QEvent::PaletteChange) {
303 Q_FOREACH (QToolButton *button, d->buttons) {
304 KoToolBoxButton* toolBoxButton = qobject_cast<KoToolBoxButton*>(button);
305 if (toolBoxButton) {
306 toolBoxButton->setHighlightColor();
307 }
308 }
309 }
310}
311
312void KoToolBox::setOrientation(Qt::Orientation orientation)
313{
314 d->layout->setOrientation(orientation);
315 QTimer::singleShot(0, this, SLOT(update()));
316 Q_FOREACH (Section* section, d->sections) {
317 section->setOrientation(orientation);
318 }
319}
320
321void KoToolBox::setCompact(bool state)
322{
323 d->layout->setCompact(state);
324}
325
327{
328 return d->layout->isCompact();
329}
330
332{
333 d->floating = v;
334}
335
337{
338 Q_UNUSED(canvas);
339 addButton(toolAction);
341
342}
343
345{
346 QAction* action = qobject_cast<QAction*>(sender());
347 if (action) {
348 int iconSize = -1;
349 if (action == d->defaultIconSizeAction) {
350 iconSize = buttonSize(KisPortingUtils::getScreenNumberForWidget(QApplication::activeWindow()));
351 QAction *action = d->contextIconSizes.key(iconSize);
352 if (action) {
353 action->setChecked(true);
354 }
355 } else if (d->contextIconSizes.contains(action)) {
356 iconSize = d->contextIconSizes.value(action);
357 }
359
360 KConfigGroup cfg = KSharedConfig::openConfig()->group("KoToolBox");
361 cfg.writeEntry("iconSize", iconSize);
363
365 }
366}
367
369{
370 if (d->contextIconSizes.isEmpty()) {
371 d->defaultIconSizeAction = menu->addAction(i18nc("@item:inmenu Icon size", "Default"),
372 this, SLOT(slotContextIconSize()));
373
374 QActionGroup *sizeGroup = new QActionGroup(menu);
375 QList<int> sizes;
376 sizes << 12 << 14 << 16 << 22 << 32 << 48 << 64; //<< 96 << 128 << 192 << 256;
377 Q_FOREACH (int i, sizes) {
378 QAction *action = menu->addAction(i18n("%1x%2", i, i), this, SLOT(slotContextIconSize()));
379 d->contextIconSizes.insert(action, i);
380 action->setActionGroup(sizeGroup);
381 action->setCheckable(true);
382 if (d->iconSize == i) {
383 action->setChecked(true);
384 }
385 }
386 }
387}
388
390{
391 return d->layout;
392}
393
395{
396 Q_FOREACH (auto b, d->buttons) {
397 b->setEnabled(true);
398 b->setVisible(true);
399 }
400
401 layout()->invalidate();
402 update();
403}
404
406{
407 Q_FOREACH (QString name, names) {
408 if (d->buttonsByToolId.contains(name)) {
409 auto b = d->buttonsByToolId[name];
410
411 b->setEnabled(false);
412 b->setVisible(false);
413 }
414 }
415
416 layout()->invalidate();
417 update();
418}
419
420#include "moc_KoToolBoxScrollArea_p.cpp"
qreal v
QList< QString > QStringList
#define BUTTON_MARGIN
Definition KoToolBox.cpp:39
static int buttonSize(int screen)
Definition KoToolBox.cpp:41
#define warnWidgets
int iconSize(qreal width, qreal height)
A container for a set of QAction objects.
QAction * action(int index) const
KoToolManager * toolManager() const
virtual KisKActionCollection * actionCollection() const
QPointer< KoToolManager > toolManager
virtual bool isShapeEditable(bool recursive=true) const
checks recursively if the shape or one of its parents is not visible or locked
Definition KoShape.cpp:968
bool isVisible(bool recursive=true) const
Definition KoShape.cpp:797
QString section() const
The section the tool wants to be in.
QString id() const
The id of the tool.
int priority() const
Lower number (higher priority) means coming first in the section.
QString visibilityCode() const
This tool should become visible when we Q_EMIT this string in toolCodesSelected()
void setCompact(bool state)
void setOrientation(Qt::Orientation orientation)
void addSection(Section *section)
QHash< QString, KoToolBoxButton * > buttonsByToolId
Definition KoToolBox.cpp:68
QHash< QToolButton *, QString > visibilityCodes
Definition KoToolBox.cpp:72
QMap< QAction *, int > contextIconSizes
Definition KoToolBox.cpp:75
KoToolBoxLayout * layout
Definition KoToolBox.cpp:70
QList< QToolButton * > buttons
Definition KoToolBox.cpp:66
QAction * defaultIconSizeAction
Definition KoToolBox.cpp:76
KoToolManager * toolManager
Definition KoToolBox.cpp:77
void addSection(Section *section, const QString &name)
Definition KoToolBox.cpp:80
KoToolBoxButton * selectedButton
Definition KoToolBox.cpp:67
QMap< QString, Section * > sections
Definition KoToolBox.cpp:69
QButtonGroup * buttonGroup
Definition KoToolBox.cpp:71
void setButtonsVisible(const QList< QString > &codes)
void addButton(KoToolAction *toolAction)
void setupIconSizeMenu(QMenu *menu)
~KoToolBox() override
void disableButtonsByName(QList< QString > names)
Disables all buttons who's names are in the provided list.
void paintEvent(QPaintEvent *event) override
void setCurrentLayer(const KoCanvasController *canvas, const KoShapeLayer *newLayer)
void setFloating(bool v)
void applyIconSize()
void toolAdded(KoToolAction *toolAction, KoCanvasController *canvas)
add a tool post-initialization. The tool will also be activated.
void setActiveTool(KoCanvasController *canvas)
KoToolBoxLayout * toolBoxLayout() const
void setViewManager(KisViewManager *viewManager)
void changeEvent(QEvent *event) override
bool isCompact()
void slotContextIconSize()
set the icon size for all the buttons
Private *const d
void setOrientation(Qt::Orientation orientation)
Set the orientation of the layout to orientation.
KoToolBox()
constructor
Definition KoToolBox.cpp:87
void setCompact(bool)
void resetButtonVisibility()
Resets visibility of all buttons.
QList< KoToolAction * > toolActionList
QString activeToolId() const
Returns the toolId of the currently active tool.
void addButton(QAbstractButton *button, int priority)
void setButtonSize(QSize size)
Separators separators() const
void setOrientation(Qt::Orientation orientation)
void setName(const QString &name)
#define KIS_SAFE_ASSERT_RECOVER(cond)
Definition kis_assert.h:126
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
QString button(const QWheelEvent &ev)
int getScreenNumberForWidget(const QWidget *w)