Krita Source Code Documentation
Loading...
Searching...
No Matches
KoToolBox Class Reference

#include <KoToolBox_p.h>

+ Inheritance diagram for KoToolBox:

Classes

class  Private
 

Public Slots

bool isCompact ()
 
void setActiveTool (KoCanvasController *canvas)
 
void setButtonsVisible (const QList< QString > &codes)
 
void setCompact (bool)
 
void setFloating (bool v)
 
void setOrientation (Qt::Orientation orientation)
 Set the orientation of the layout to orientation.
 
KoToolBoxLayout * toolBoxLayout () const
 

Public Member Functions

void applyIconSize ()
 
 KoToolBox ()
 constructor
 
void setViewManager (KisViewManager *viewManager)
 
 ~KoToolBox () override
 

Protected Member Functions

void changeEvent (QEvent *event) override
 
void paintEvent (QPaintEvent *event) override
 

Private Slots

void setCurrentLayer (const KoCanvasController *canvas, const KoShapeLayer *newLayer)
 
void slotContextIconSize ()
 set the icon size for all the buttons
 
void toolAdded (KoToolAction *toolAction, KoCanvasController *canvas)
 add a tool post-initialization. The tool will also be activated.
 

Private Member Functions

void addButton (KoToolAction *toolAction)
 
void disableButtonsByName (QList< QString > names)
 Disables all buttons who's names are in the provided list.
 
void resetButtonVisibility ()
 Resets visibility of all buttons.
 
void setupIconSizeMenu (QMenu *menu)
 

Private Attributes

Private *const d
 

Friends

class KoToolBoxDocker
 

Detailed Description

KoToolBox is a dock widget that can order tools according to type and priority.

The ToolBox is a container for tool buttons which are themselves divided into sections.

Adding buttons using addButton() will allow you to show those buttons. You should connect the button to your handling method yourself.

The unique property of this toolbox is that it can be shown horizontal as well as vertical, rotating in a smart way to show the buttons optimally.

See also
KoToolManager

Definition at line 39 of file KoToolBox_p.h.

Constructor & Destructor Documentation

◆ KoToolBox()

KoToolBox::KoToolBox ( )
explicit

constructor

Definition at line 87 of file KoToolBox.cpp.

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}
static int buttonSize(int screen)
Definition KoToolBox.cpp:41
KoToolBoxLayout * layout
Definition KoToolBox.cpp:70
void addSection(Section *section, const QString &name)
Definition KoToolBox.cpp:80
QButtonGroup * buttonGroup
Definition KoToolBox.cpp:71
Private *const d
void setCompact(bool)
int getScreenNumberForWidget(const QWidget *w)

References KoToolBox::Private::addSection(), KoToolBox::Private::buttonGroup, buttonSize(), d, KisPortingUtils::getScreenNumberForWidget(), KoToolBox::Private::iconSize, KoToolBox::Private::layout, and setCompact().

◆ ~KoToolBox()

KoToolBox::~KoToolBox ( )
override

Definition at line 108 of file KoToolBox.cpp.

109{
110 delete d;
111}

References d.

Member Function Documentation

◆ addButton()

void KoToolBox::addButton ( KoToolAction * toolAction)
private

Add a button to the toolbox. The buttons should all be added before the first showing since adding will not really add them to the UI until setup() is called.

Parameters
toolActionthe action of the tool
See also
setup()

Definition at line 158 of file KoToolBox.cpp.

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}
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()
QHash< QString, KoToolBoxButton * > buttonsByToolId
Definition KoToolBox.cpp:68
QHash< QToolButton *, QString > visibilityCodes
Definition KoToolBox.cpp:72
QList< QToolButton * > buttons
Definition KoToolBox.cpp:66
QMap< QString, Section * > sections
Definition KoToolBox.cpp:69
void addButton(QAbstractButton *button, int priority)
QString button(const QWheelEvent &ev)

References Section::addButton(), KoToolBox::Private::addSection(), button(), KoToolBox::Private::buttonGroup, KoToolBox::Private::buttons, KoToolBox::Private::buttonsByToolId, d, KoToolAction::id(), KoToolAction::priority(), KoToolAction::section(), KoToolBox::Private::sections, KoToolAction::visibilityCode(), and KoToolBox::Private::visibilityCodes.

◆ applyIconSize()

void KoToolBox::applyIconSize ( )

Definition at line 113 of file KoToolBox.cpp.

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}
#define BUTTON_MARGIN
Definition KoToolBox.cpp:39
void setButtonSize(QSize size)

References button(), BUTTON_MARGIN, KoToolBox::Private::buttons, d, KoToolBox::Private::iconSize, KoToolBox::Private::sections, and Section::setButtonSize().

◆ changeEvent()

void KoToolBox::changeEvent ( QEvent * event)
overrideprotected

Definition at line 299 of file KoToolBox.cpp.

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}

References button(), KoToolBox::Private::buttons, d, and KoToolBoxButton::setHighlightColor().

◆ disableButtonsByName()

void KoToolBox::disableButtonsByName ( QList< QString > names)
private

Disables all buttons who's names are in the provided list.

Definition at line 405 of file KoToolBox.cpp.

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}
const char * name(StandardAction id)
bool update(QSpinBox *spinBox)

References KoToolBox::Private::buttonsByToolId, and d.

◆ isCompact

bool KoToolBox::isCompact ( )
slot

Definition at line 326 of file KoToolBox.cpp.

327{
328 return d->layout->isCompact();
329}

References d, KoToolBoxLayout::isCompact(), and KoToolBox::Private::layout.

◆ paintEvent()

void KoToolBox::paintEvent ( QPaintEvent * event)
overrideprotected

Definition at line 255 of file KoToolBox.cpp.

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}
Separators separators() const

References d, KoToolBox::Private::sections, Section::SeparatorLeft, Section::SeparatorNone, Section::separators(), and Section::SeparatorTop.

◆ resetButtonVisibility()

void KoToolBox::resetButtonVisibility ( )
private

Resets visibility of all buttons.

Definition at line 394 of file KoToolBox.cpp.

395{
396 Q_FOREACH (auto b, d->buttons) {
397 b->setEnabled(true);
398 b->setVisible(true);
399 }
400
401 layout()->invalidate();
402 update();
403}

References KoToolBox::Private::buttons, and d.

◆ setActiveTool

void KoToolBox::setActiveTool ( KoCanvasController * canvas)
slot

Set the new active button based on the currently active tool.

Parameters
canvasthe currently active canvas.

Definition at line 189 of file KoToolBox.cpp.

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}
#define warnWidgets
QPointer< KoToolManager > toolManager
KoToolManager * toolManager
Definition KoToolBox.cpp:77
KoToolBoxButton * selectedButton
Definition KoToolBox.cpp:67
QString activeToolId() const
Returns the toolId of the currently active tool.
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130

References KoToolManager::activeToolId(), button(), KoToolBox::Private::buttonsByToolId, d, KIS_SAFE_ASSERT_RECOVER_NOOP, KoToolBox::Private::selectedButton, KoToolBoxButton::setHighlightColor(), KoCanvasController::toolManager, KoToolBox::Private::toolManager, and warnWidgets.

◆ setButtonsVisible

void KoToolBox::setButtonsVisible ( const QList< QString > & codes)
slot

Show only the dynamic buttons that have a code from parameter codes. The toolbox allows buttons to be optionally registered with a visibilityCode. This code can be passed here and all buttons that have that code are shown. All buttons that have another visibility code registered are hidden.

Parameters
codesa list of all the codes to show.

Definition at line 209 of file KoToolBox.cpp.

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}
QList< QString > QStringList
void disableButtonsByName(QList< QString > names)
Disables all buttons who's names are in the provided list.

References button(), d, disableButtonsByName(), and KoToolBox::Private::visibilityCodes.

◆ setCompact

void KoToolBox::setCompact ( bool state)
slot

Definition at line 321 of file KoToolBox.cpp.

322{
323 d->layout->setCompact(state);
324}
void setCompact(bool state)

References d, KoToolBox::Private::layout, and KoToolBoxLayout::setCompact().

◆ setCurrentLayer

void KoToolBox::setCurrentLayer ( const KoCanvasController * canvas,
const KoShapeLayer * newLayer )
privateslot

Definition at line 243 of file KoToolBox.cpp.

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}

References button(), d, KoShape::isShapeEditable(), KoShape::isVisible(), and KoToolBox::Private::visibilityCodes.

◆ setFloating

void KoToolBox::setFloating ( bool v)
slot

Definition at line 331 of file KoToolBox.cpp.

332{
333 d->floating = v;
334}
qreal v

References d, KoToolBox::Private::floating, and v.

◆ setOrientation

void KoToolBox::setOrientation ( Qt::Orientation orientation)
slot

Set the orientation of the layout to orientation.

Definition at line 312 of file KoToolBox.cpp.

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}
void setOrientation(Qt::Orientation orientation)
void setOrientation(Qt::Orientation orientation)

References d, KoToolBox::Private::layout, KoToolBox::Private::sections, Section::setOrientation(), and KoToolBoxLayout::setOrientation().

◆ setupIconSizeMenu()

void KoToolBox::setupIconSizeMenu ( QMenu * menu)
private

Definition at line 368 of file KoToolBox.cpp.

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}
QMap< QAction *, int > contextIconSizes
Definition KoToolBox.cpp:75
QAction * defaultIconSizeAction
Definition KoToolBox.cpp:76
void slotContextIconSize()
set the icon size for all the buttons

References KoToolBox::Private::contextIconSizes, d, KoToolBox::Private::defaultIconSizeAction, KoToolBox::Private::iconSize, and slotContextIconSize().

◆ setViewManager()

void KoToolBox::setViewManager ( KisViewManager * viewManager)

Definition at line 125 of file KoToolBox.cpp.

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}
A container for a set of QAction objects.
QAction * action(int index) const
KoToolManager * toolManager() const
virtual KisKActionCollection * actionCollection() const
void setButtonsVisible(const QList< QString > &codes)
void addButton(KoToolAction *toolAction)
void setCurrentLayer(const KoCanvasController *canvas, const KoShapeLayer *newLayer)
void applyIconSize()
void toolAdded(KoToolAction *toolAction, KoCanvasController *canvas)
add a tool post-initialization. The tool will also be activated.
void setActiveTool(KoCanvasController *canvas)
QList< KoToolAction * > toolActionList

References KisKActionCollection::action(), KisViewManager::actionCollection(), addButton(), applyIconSize(), button(), KoToolBox::Private::buttonsByToolId, d, KoToolAction::id(), setActiveTool(), setButtonsVisible(), setCurrentLayer(), KoToolManager::toolActionList, toolAdded(), KisViewManager::toolManager(), and KoToolBox::Private::toolManager.

◆ slotContextIconSize

void KoToolBox::slotContextIconSize ( )
privateslot

set the icon size for all the buttons

Definition at line 344 of file KoToolBox.cpp.

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}
int iconSize(qreal width, qreal height)
#define KIS_SAFE_ASSERT_RECOVER(cond)
Definition kis_assert.h:126

References applyIconSize(), buttonSize(), KoToolBox::Private::contextIconSizes, d, KoToolBox::Private::defaultIconSizeAction, KisPortingUtils::getScreenNumberForWidget(), KoToolBox::Private::iconSize, iconSize(), and KIS_SAFE_ASSERT_RECOVER.

◆ toolAdded

void KoToolBox::toolAdded ( KoToolAction * toolAction,
KoCanvasController * canvas )
privateslot

add a tool post-initialization. The tool will also be activated.

Definition at line 336 of file KoToolBox.cpp.

337{
338 Q_UNUSED(canvas);
339 addButton(toolAction);
341
342}

References addButton(), and setButtonsVisible().

◆ toolBoxLayout

KoToolBoxLayout * KoToolBox::toolBoxLayout ( ) const
slot

Definition at line 389 of file KoToolBox.cpp.

390{
391 return d->layout;
392}

References d, and KoToolBox::Private::layout.

Friends And Related Symbol Documentation

◆ KoToolBoxDocker

friend class KoToolBoxDocker
friend

Definition at line 108 of file KoToolBox_p.h.

Member Data Documentation

◆ d

Private* const KoToolBox::d
private

Definition at line 112 of file KoToolBox_p.h.


The documentation for this class was generated from the following files: