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

#include <KoToolBox_p.h>

+ Inheritance diagram for KoToolBox:

Classes

class  Private
 

Public Slots

void setActiveTool (KoCanvasController *canvas)
 
void setButtonsVisible (const QList< QString > &codes)
 
void setFloating (bool v)
 
void setOrientation (Qt::Orientation orientation)
 Set the orientation of the layout to orientation.
 
KoToolBoxLayouttoolBoxLayout () 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 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 Q_FOREACH (KoToolAction *toolAction, KoToolManager::instance()->toolActionList()) {
106 addButton(toolAction);
107 }
108
110
111 // Update visibility of buttons
113
114 connect(KoToolManager::instance(), SIGNAL(changedTool(KoCanvasController*)),
115 this, SLOT(setActiveTool(KoCanvasController*)));
116 connect(KoToolManager::instance(), SIGNAL(currentLayerChanged(const KoCanvasController*,const KoShapeLayer*)),
117 this, SLOT(setCurrentLayer(const KoCanvasController*,const KoShapeLayer*)));
118 connect(KoToolManager::instance(), SIGNAL(toolCodesSelected(QList<QString>)), this, SLOT(setButtonsVisible(QList<QString>)));
120 SIGNAL(addedTool(KoToolAction*,KoCanvasController*)),
122
123}
static int buttonSize(int screen)
Definition KoToolBox.cpp:41
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
KoToolBoxLayout * layout
Definition KoToolBox.cpp:70
void addSection(Section *section, const QString &name)
Definition KoToolBox.cpp:80
QButtonGroup * buttonGroup
Definition KoToolBox.cpp:71
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)
Private *const d
static KoToolManager * instance()
Return the toolmanager singleton.
int getScreenNumberForWidget(const QWidget *w)

References addButton(), KoToolBox::Private::addSection(), applyIconSize(), KoToolBox::Private::buttonGroup, buttonSize(), connect(), d, KisPortingUtils::getScreenNumberForWidget(), KoToolBox::Private::iconSize, KoToolManager::instance(), KoToolBox::Private::layout, setActiveTool(), setButtonsVisible(), setCurrentLayer(), and toolAdded().

◆ ~KoToolBox()

KoToolBox::~KoToolBox ( )
override

Definition at line 125 of file KoToolBox.cpp.

126{
127 delete d;
128}

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 156 of file KoToolBox.cpp.

157{
158 KoToolBoxButton *button = new KoToolBoxButton(toolAction, this);
159
160 d->buttons << button;
161
162 QString sectionToBeAddedTo;
163 const QString section = toolAction->section();
164 if (section.contains(qApp->applicationName())) {
165 sectionToBeAddedTo = "main";
166 } else if (section.contains("main")) {
167 sectionToBeAddedTo = "main";
168 } else if (section.contains("dynamic")) {
169 sectionToBeAddedTo = "dynamic";
170 } else {
171 sectionToBeAddedTo = section;
172 }
173
174 Section *sectionWidget = d->sections.value(sectionToBeAddedTo);
175 if (sectionWidget == 0) {
176 sectionWidget = new Section(this);
177 d->addSection(sectionWidget, sectionToBeAddedTo);
178 }
179 sectionWidget->addButton(button, toolAction->priority());
180
181 d->buttonGroup->addButton(button);
182 d->buttonsByToolId.insert(toolAction->id(), button);
183
184 d->visibilityCodes.insert(button, toolAction->visibilityCode());
185}
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 130 of file KoToolBox.cpp.

131{
132 Q_FOREACH (QToolButton *button, d->buttons) {
133 button->setIconSize(QSize(d->iconSize, d->iconSize));
134 }
135
136 Q_FOREACH (Section *section, d->sections.values()) {
138 }
139}
#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 286 of file KoToolBox.cpp.

287{
288 QWidget::changeEvent(event);
289 if (event->type() == QEvent::PaletteChange) {
290 Q_FOREACH (QToolButton *button, d->buttons) {
291 KoToolBoxButton* toolBoxButton = qobject_cast<KoToolBoxButton*>(button);
292 if (toolBoxButton) {
293 toolBoxButton->setHighlightColor();
294 }
295 }
296 }
297}

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

◆ paintEvent()

void KoToolBox::paintEvent ( QPaintEvent * event)
overrideprotected

Definition at line 243 of file KoToolBox.cpp.

244{
245 QPainter painter(this);
246
247 const QList<Section*> sections = d->sections.values();
248 QList<Section*>::const_iterator iterator = sections.begin();
249 int halfSpacing = layout()->spacing();
250 if (halfSpacing > 0) {
251 halfSpacing /= 2;
252 }
253 while(iterator != sections.end()) {
254 Section *section = *iterator;
255 QStyleOption styleoption;
256 styleoption.palette = palette();
257
258 if (section->separators() & Section::SeparatorTop) {
259 int y = section->y() - halfSpacing;
260 styleoption.state = QStyle::State_None;
261 styleoption.rect = QRect(section->x(), y-1, section->width(), 2);
262
263 style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &styleoption, &painter);
264 }
265
266 if (section->separators() & Section::SeparatorLeft && section->isLeftToRight()) {
267 int x = section->x() - halfSpacing;
268 styleoption.state = QStyle::State_Horizontal;
269 styleoption.rect = QRect(x-1, section->y(), 2, section->height());
270
271 style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &styleoption, &painter);
272 } else if (section->separators() & Section::SeparatorLeft && section->isRightToLeft()) {
273 int x = section->x() + section->width() + halfSpacing;
274 styleoption.state = QStyle::State_Horizontal;
275 styleoption.rect = QRect(x-1, section->y(), 2, section->height());
276
277 style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &styleoption, &painter);
278 }
279
280 ++iterator;
281 }
282
283 painter.end();
284}
Separators separators() const
rgba palette[MAX_PALETTE]
Definition palette.c:35

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

◆ 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 187 of file KoToolBox.cpp.

188{
189 Q_UNUSED(canvas);
190
191 QString id = KoToolManager::instance()->activeToolId();
193 if (button) {
194 button->setChecked(true);
195 button->setHighlightColor();
196 if (d->selectedButton) {
198 }
200 }
201 else {
202 warnWidgets << "KoToolBox::setActiveTool(" << id << "): no such button found";
203 }
204}
#define warnWidgets
KoToolBoxButton * selectedButton
Definition KoToolBox.cpp:67
QString activeToolId() const
Returns the toolId of the currently active tool.

References KoToolManager::activeToolId(), button(), KoToolBox::Private::buttonsByToolId, d, KoToolManager::instance(), KoToolBox::Private::selectedButton, KoToolBoxButton::setHighlightColor(), 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 206 of file KoToolBox.cpp.

207{
208 Q_FOREACH (QToolButton *button, d->visibilityCodes.keys()) {
209 QString code = d->visibilityCodes.value(button);
210
211 if (code.startsWith(QLatin1String("flake/"))) {
212 continue;
213 }
214
215 if (code.endsWith( QLatin1String( "/always"))) {
216 button->setVisible(true);
217 button->setEnabled(true);
218 }
219 else if (code.isEmpty()) {
220 button->setVisible(true);
221 button->setEnabled( codes.count() != 0 );
222 }
223 else {
224 button->setVisible( codes.contains(code) );
225 }
226 }
227 layout()->invalidate();
228 update();
229}
bool update(QSpinBox *spinBox)

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

◆ setCurrentLayer

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

Definition at line 231 of file KoToolBox.cpp.

232{
233 Q_UNUSED(canvas);
234 const bool enabled = layer == 0 || (layer->isShapeEditable() && layer->isVisible());
235 foreach (QToolButton *button, d->visibilityCodes.keys()) {
236 if (d->visibilityCodes[button].endsWith( QLatin1String( "/always") ) ) {
237 continue;
238 }
239 button->setEnabled(enabled);
240 }
241}

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

◆ setFloating

void KoToolBox::setFloating ( bool v)
slot

Definition at line 309 of file KoToolBox.cpp.

310{
311 d->floating = v;
312}
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 299 of file KoToolBox.cpp.

300{
301 d->orientation = orientation;
302 d->layout->setOrientation(orientation);
303 QTimer::singleShot(0, this, SLOT(update()));
304 Q_FOREACH (Section* section, d->sections) {
305 section->setOrientation(orientation);
306 }
307}
void setOrientation(Qt::Orientation orientation)
Qt::Orientation orientation
Definition KoToolBox.cpp:77
void setOrientation(Qt::Orientation orientation)

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

◆ setupIconSizeMenu()

void KoToolBox::setupIconSizeMenu ( QMenu * menu)
private

Definition at line 346 of file KoToolBox.cpp.

347{
348 if (d->contextIconSizes.isEmpty()) {
349 d->defaultIconSizeAction = menu->addAction(i18nc("@item:inmenu Icon size", "Default"),
350 this, SLOT(slotContextIconSize()));
351
352 QActionGroup *sizeGroup = new QActionGroup(menu);
353 QList<int> sizes;
354 sizes << 12 << 14 << 16 << 22 << 32 << 48 << 64; //<< 96 << 128 << 192 << 256;
355 Q_FOREACH (int i, sizes) {
356 QAction *action = menu->addAction(i18n("%1x%2", i, i), this, SLOT(slotContextIconSize()));
357 d->contextIconSizes.insert(action, i);
358 action->setActionGroup(sizeGroup);
359 action->setCheckable(true);
360 if (d->iconSize == i) {
361 action->setChecked(true);
362 }
363 }
364 }
365}
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 142 of file KoToolBox.cpp.

143{
144 KisKActionCollection *actionCollection = viewManager->actionCollection();
145 Q_FOREACH(KoToolAction *toolAction, KoToolManager::instance()->toolActionList()) {
146 QAction *toolQAction = actionCollection->action(toolAction->id());
147 auto button = d->buttonsByToolId.find(toolAction->id());
148 if (button == d->buttonsByToolId.end()) {
149 qWarning() << "Toolbox is missing button for tool" << toolAction->id();
150 continue;
151 }
152 (*button)->attachAction(toolQAction);
153 }
154}
A container for a set of QAction objects.
QAction * action(int index) const
virtual KisKActionCollection * actionCollection() const

References KisKActionCollection::action(), KisViewManager::actionCollection(), button(), KoToolBox::Private::buttonsByToolId, d, KoToolAction::id(), and KoToolManager::instance().

◆ slotContextIconSize

void KoToolBox::slotContextIconSize ( )
privateslot

set the icon size for all the buttons

Definition at line 322 of file KoToolBox.cpp.

323{
324 QAction* action = qobject_cast<QAction*>(sender());
325 if (action) {
326 int iconSize = -1;
327 if (action == d->defaultIconSizeAction) {
328 iconSize = buttonSize(KisPortingUtils::getScreenNumberForWidget(QApplication::activeWindow()));
329 QAction *action = d->contextIconSizes.key(iconSize);
330 if (action) {
331 action->setChecked(true);
332 }
333 } else if (d->contextIconSizes.contains(action)) {
334 iconSize = d->contextIconSizes.value(action);
335 }
337
338 KConfigGroup cfg = KSharedConfig::openConfig()->group("KoToolBox");
339 cfg.writeEntry("iconSize", iconSize);
341
343 }
344}
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 314 of file KoToolBox.cpp.

315{
316 Q_UNUSED(canvas);
317 addButton(toolAction);
319
320}

References addButton(), and setButtonsVisible().

◆ toolBoxLayout

KoToolBoxLayout * KoToolBox::toolBoxLayout ( ) const
slot

Definition at line 367 of file KoToolBox.cpp.

368{
369 return d->layout;
370}

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

Friends And Related Symbol Documentation

◆ KoToolBoxDocker

friend class KoToolBoxDocker
friend

Definition at line 100 of file KoToolBox_p.h.

Member Data Documentation

◆ d

Private* const KoToolBox::d
private

Definition at line 104 of file KoToolBox_p.h.


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