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

87 : d(new Private)
88{
89 d->layout = new KoToolBoxLayout(this);
90 // add defaults
91 d->addSection(new Section(this), "main");
92 d->addSection(new Section(this), "dynamic");
93
94 d->buttonGroup = new QButtonGroup(this);
95
96 // Get screen the widget exists in, but fall back to primary screen if invalid.
97 const int widgetsScreen = KisPortingUtils::getScreenNumberForWidget(QApplication::activeWindow());
98 const int primaryScreen = 0; //In QT, primary screen should always be the first index of QGuiApplication::screens()
99 const int screen = (widgetsScreen >= 0 && widgetsScreen < QGuiApplication::screens().size()) ? widgetsScreen : primaryScreen;
100 const int toolbuttonSize = buttonSize(screen);
101 KConfigGroup cfg = KSharedConfig::openConfig()->group("KoToolBox");
102 d->iconSize = cfg.readEntry("iconSize", toolbuttonSize);
103
104 Q_FOREACH (KoToolAction *toolAction, KoToolManager::instance()->toolActionList()) {
105 addButton(toolAction);
106 }
107
109
110 // Update visibility of buttons
112
113 setCompact(cfg.readEntry<bool>("compact", false));
114
115 connect(KoToolManager::instance(), SIGNAL(changedTool(KoCanvasController*)),
116 this, SLOT(setActiveTool(KoCanvasController*)));
117 connect(KoToolManager::instance(), SIGNAL(currentLayerChanged(const KoCanvasController*,const KoShapeLayer*)),
118 this, SLOT(setCurrentLayer(const KoCanvasController*,const KoShapeLayer*)));
119 connect(KoToolManager::instance(), SIGNAL(toolCodesSelected(QList<QString>)), this, SLOT(setButtonsVisible(QList<QString>)));
120 connect(KoToolManager::instance(),
121 SIGNAL(addedTool(KoToolAction*,KoCanvasController*)),
123
124}
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:79
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
void setCompact(bool)
static KoToolManager * instance()
Return the toolmanager singleton.
int getScreenNumberForWidget(const QWidget *w)

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

◆ ~KoToolBox()

KoToolBox::~KoToolBox ( )
override

Definition at line 126 of file KoToolBox.cpp.

127{
128 delete d;
129}

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

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

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

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

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

◆ isCompact

bool KoToolBox::isCompact ( )
slot

Definition at line 315 of file KoToolBox.cpp.

316{
317 return d->layout->isCompact();
318}

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

◆ paintEvent()

void KoToolBox::paintEvent ( QPaintEvent * event)
overrideprotected

Definition at line 244 of file KoToolBox.cpp.

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

References d, palette, KoToolBox::Private::sections, Section::SeparatorLeft, Section::SeparatorNone, 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 188 of file KoToolBox.cpp.

189{
190 Q_UNUSED(canvas);
191
192 QString id = KoToolManager::instance()->activeToolId();
194 if (button) {
195 button->setChecked(true);
196 button->setHighlightColor();
197 if (d->selectedButton) {
199 }
201 }
202 else {
203 warnWidgets << "KoToolBox::setActiveTool(" << id << "): no such button found";
204 }
205}
#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 207 of file KoToolBox.cpp.

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

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

◆ setCompact

void KoToolBox::setCompact ( bool state)
slot

Definition at line 310 of file KoToolBox.cpp.

311{
312 d->layout->setCompact(state);
313}
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 232 of file KoToolBox.cpp.

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

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

◆ setFloating

void KoToolBox::setFloating ( bool v)
slot

Definition at line 320 of file KoToolBox.cpp.

321{
322 d->floating = v;
323}
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 301 of file KoToolBox.cpp.

302{
303 d->layout->setOrientation(orientation);
304 QTimer::singleShot(0, this, SLOT(update()));
305 Q_FOREACH (Section* section, d->sections) {
306 section->setOrientation(orientation);
307 }
308}
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 357 of file KoToolBox.cpp.

358{
359 if (d->contextIconSizes.isEmpty()) {
360 d->defaultIconSizeAction = menu->addAction(i18nc("@item:inmenu Icon size", "Default"),
361 this, SLOT(slotContextIconSize()));
362
363 QActionGroup *sizeGroup = new QActionGroup(menu);
364 QList<int> sizes;
365 sizes << 12 << 14 << 16 << 22 << 32 << 48 << 64; //<< 96 << 128 << 192 << 256;
366 Q_FOREACH (int i, sizes) {
367 QAction *action = menu->addAction(i18n("%1x%2", i, i), this, SLOT(slotContextIconSize()));
368 d->contextIconSizes.insert(action, i);
369 action->setActionGroup(sizeGroup);
370 action->setCheckable(true);
371 if (d->iconSize == i) {
372 action->setChecked(true);
373 }
374 }
375 }
376}
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 143 of file KoToolBox.cpp.

144{
145 KisKActionCollection *actionCollection = viewManager->actionCollection();
146 Q_FOREACH(KoToolAction *toolAction, KoToolManager::instance()->toolActionList()) {
147 QAction *toolQAction = actionCollection->action(toolAction->id());
148 auto button = d->buttonsByToolId.find(toolAction->id());
149 if (button == d->buttonsByToolId.end()) {
150 qWarning() << "Toolbox is missing button for tool" << toolAction->id();
151 continue;
152 }
153 (*button)->attachAction(toolQAction);
154 }
155}
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 333 of file KoToolBox.cpp.

334{
335 QAction* action = qobject_cast<QAction*>(sender());
336 if (action) {
337 int iconSize = -1;
338 if (action == d->defaultIconSizeAction) {
339 iconSize = buttonSize(KisPortingUtils::getScreenNumberForWidget(QApplication::activeWindow()));
340 QAction *action = d->contextIconSizes.key(iconSize);
341 if (action) {
342 action->setChecked(true);
343 }
344 } else if (d->contextIconSizes.contains(action)) {
345 iconSize = d->contextIconSizes.value(action);
346 }
348
349 KConfigGroup cfg = KSharedConfig::openConfig()->group("KoToolBox");
350 cfg.writeEntry("iconSize", iconSize);
352
354 }
355}
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 325 of file KoToolBox.cpp.

326{
327 Q_UNUSED(canvas);
328 addButton(toolAction);
330
331}

References addButton(), and setButtonsVisible().

◆ toolBoxLayout

KoToolBoxLayout * KoToolBox::toolBoxLayout ( ) const
slot

Definition at line 378 of file KoToolBox.cpp.

379{
380 return d->layout;
381}

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

Friends And Related Symbol Documentation

◆ KoToolBoxDocker

friend class KoToolBoxDocker
friend

Definition at line 103 of file KoToolBox_p.h.

Member Data Documentation

◆ d

Private* const KoToolBox::d
private

Definition at line 107 of file KoToolBox_p.h.


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