Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_selection_actions_panel.cpp
Go to the documentation of this file.
1
2/*
3 * SPDX-FileCopyrightText: 2025 Ross Rosales <ross.erosales@gmail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
9
11#include "kis_action.h"
12#include "kis_action_manager.h"
13#include "kis_config_notifier.h"
15#include "KisDocument.h"
16#include "KisViewManager.h"
17#include "kis_canvas2.h"
19#include "kis_icon_utils.h"
20#include "kis_painting_tweaks.h"
21#include "kis_selection.h"
26#include <QList>
27#include <QMouseEvent>
28#include <QPointF>
29#include <QPushButton>
30#include <QTabletEvent>
31#include <QTouchEvent>
32#include <QTransform>
33#include <kactioncollection.h>
34#include <kis_algebra_2d.h>
35#include <klocalizedstring.h>
36#include <ktoggleaction.h>
37
38#include <QApplication>
39#include <QPainter>
40#include <QPainterPath>
41#include <QMenu>
42
44
45static constexpr int BUTTON_SIZE = 30;
46static constexpr int BUFFER_SPACE = 5;
47
49
57
60 {
61 }
64
66 bool m_pressed = false;
67 bool m_visible = false;
68 bool m_enabled = true;
69
70 struct DragHandle {
71 QPoint position = QPoint(0, 0);
72 QPoint dragOrigin = QPoint(0, 0);
73 };
74
75 QScopedPointer<DragHandle> m_dragHandle;
76
80 {
81 static const QVector<ActionButtonData> data = {
82 {"select-all", i18n("Select All"), &KisSelectionManager::selectAll},
83 {"select-invert", i18n("Invert Selection"), &KisSelectionManager::invert},
84 {"select-clear", i18n("Deselect"), &KisSelectionManager::deselect},
85 {"krita_tool_color_fill", i18n("Fill Selection with Color"), &KisSelectionManager::fillForegroundColor},
86 {"draw-eraser", i18n("Clear Selection"), &KisSelectionManager::clear},
87 {"duplicatelayer", i18n("Copy To New Layer"), &KisSelectionManager::copySelectionToNewLayer},
88 {"tool_crop", i18n("Crop to Selection"), &KisSelectionManager::imageResizeToSelection}};
89 return data;
90 }
91 int m_buttonCount = buttonData().size() + 1; // buttons + drag handle
92
96};
97
99 : d(new Private)
100{
101 d->m_viewManager = viewManager;
102 d->m_selectionManager = viewManager->selectionManager();
103
104 // Setup buttons...
105 for (const ActionButtonData &buttonData : Private::buttonData()) {
106 KisSelectionActionsPanelButton *button = new KisSelectionActionsPanelButton(buttonData.iconName, buttonData.tooltip, BUTTON_SIZE, viewManager->canvas());
107 connect(button, &QAbstractButton::clicked, d->m_selectionManager, buttonData.slot);
108 connect(button, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
109 d->m_buttons.append(button);
110 }
111
112 d->m_handleWidget = new KisSelectionActionsPanelHandle(BUTTON_SIZE, viewManager->canvas());
113 connect(d->m_handleWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
114
115 d->disable_action = new KisAction(i18n("Disable selection actions bar"));
116 connect(d->disable_action, SIGNAL(triggered()), SLOT(disableSelectionActionsPanel()) );
117
118 d->configure_action = viewManager->actionManager()->actionByName("configure_sap");
119 connect(d->configure_action, SIGNAL(triggered()), SLOT(configureSelectionActionsPanel()));
120}
121
125
126void KisSelectionActionsPanel::draw(QPainter &painter, const KoColorDisplayRendererInterface *displayRendererInterface)
127{
128 KisSelectionSP selection = d->m_viewManager->selection();
129
130 if (!selection || !d->m_enabled || !d->m_visible) {
131 return;
132 }
133
134 drawActionBarBackground(painter, displayRendererInterface);
135 Q_FOREACH(KisSelectionActionsPanelButton* button, d->m_buttons){
136 button->draw(painter, displayRendererInterface);
137 }
138
139 d->m_handleWidget->draw(painter, displayRendererInterface);
140}
141
143{
144 d->configure_action->setVisible(p_visible && d->m_enabled);
145
146 QWidget *canvasWidget = dynamic_cast<QWidget *>(d->m_viewManager->canvas());
147 if (!canvasWidget) {
148 return;
149 }
150
151 p_visible &= d->m_enabled;
152
153 const bool VISIBILITY_CHANGED = d->m_visible != p_visible;
154 if (!VISIBILITY_CHANGED) {
155 return;
156 }
157
158 if (d->m_viewManager->selection() && p_visible) { // Now visible!
159 d->m_handleWidget->installEventFilter(this);
160
161 if (!d->m_dragHandle) {
162 d->m_dragHandle.reset(new Private::DragHandle());
163 d->m_dragHandle->position = initialDragHandlePosition();
165 }
166 } else { // Now hidden!
167 d->m_handleWidget->removeEventFilter(this);
168
169 for (KisSelectionActionsPanelButton *button : d->m_buttons) {
170 button->hide();
171 }
172 d->m_handleWidget->hide();
173
174 d->m_pressed = false;
175 d->m_dragHandle.reset();
176 }
177
178 d->m_visible = p_visible;
179}
180
182{
183 d->configure_action->setVisible(enabled);
184
185 bool configurationChanged = enabled != d->m_enabled;
186 d->m_enabled = enabled;
187 if (configurationChanged) {
188 // Reset visibility when configuration changes
189 setVisible(enabled);
190 }
191}
192
193bool KisSelectionActionsPanel::eventFilter(QObject *obj, QEvent *event)
194{
195 Q_UNUSED(obj);
196
197 switch (event->type()) {
198 case QEvent::MouseButtonPress: {
199 const QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
200 return handlePress(event, mouseEventPos(mouseEvent), mouseEvent->button());
201 }
202 case QEvent::TabletPress: {
203 const QTabletEvent *tabletEvent = static_cast<QTabletEvent *>(event);
204 return handlePress(event, tabletEventPos(tabletEvent));
205 }
206 case QEvent::TouchBegin: {
207 const QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
208 QPoint pos;
209 if (touchEventPos(touchEvent, pos)) {
210 return handlePress(event, pos);
211 }
212 break;
213 }
214
215 case QEvent::MouseMove:
216 if (d->m_pressed) {
217 const QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
218 return handleMove(event, mouseEventPos(mouseEvent));
219 }
220 break;
221 case QEvent::TabletMove:
222 if (d->m_pressed) {
223 const QTabletEvent *tabletEvent = static_cast<QTabletEvent *>(event);
224 return handleMove(event, tabletEventPos(tabletEvent));
225 }
226 break;
227 case QEvent::TouchUpdate:
228 if (d->m_pressed) {
229 const QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
230 QPoint pos;
231 if (touchEventPos(touchEvent, pos)) {
232 return handleMove(event, pos);
233 }
234 }
235 break;
236
237 case QEvent::MouseButtonRelease:
238 case QEvent::TabletRelease:
239 case QEvent::TouchEnd:
240 case QEvent::TouchCancel:
241 if (d->m_pressed) {
242 d->m_handleWidget->set_held(false);
243 d->m_pressed = false;
244 event->accept();
245 return true;
246 }
247 break;
248
249 default:
250 break;
251 }
252 return false;
253}
254
255
257{
258
259 Q_FOREACH(QWidget* btn, d->m_buttons) {
260 btn->setParent(canvas->widget());
261 btn->show();
262 }
263
264 d->m_handleWidget->setParent(canvas->widget());
265 d->m_handleWidget->show();
266}
267
268QPoint KisSelectionActionsPanel::updateCanvasBoundaries(QPoint position, QWidget *canvasWidget) const
269{
270 QRect canvasBounds = canvasWidget->rect();
271
272 const int ACTION_BAR_WIDTH = d->m_actionBarWidth;
273 const int ACTION_BAR_HEIGHT = BUTTON_SIZE;
274
275 int pos_x_min = canvasBounds.left() + BUFFER_SPACE;
276 int pos_x_max = canvasBounds.right() - ACTION_BAR_WIDTH - BUFFER_SPACE;
277
278 int pos_y_min = canvasBounds.top() + BUFFER_SPACE;
279 int pos_y_max = canvasBounds.bottom() - ACTION_BAR_HEIGHT - BUFFER_SPACE;
280
281 //Ensure that max is always bigger than min
282 //If the window is small enough max could be smaller than min
283 if (pos_x_max < pos_x_min) {
284 pos_x_max = pos_x_min;
285 }
286
287 //It is pretty implausible for it to happen vertically but better safe than sorry
288 if (pos_y_max < pos_y_min) {
289 pos_y_max = pos_y_min;
290 }
291
292 position.setX(qBound(pos_x_min, position.x(), pos_x_max));
293 position.setY(qBound(pos_y_min, position.y(), pos_y_max));
294
295 return position;
296}
297
299{
300 KisSelectionSP selection = d->m_viewManager->selection();
301 KisCanvasWidgetBase *canvas = dynamic_cast<KisCanvasWidgetBase*>(d->m_viewManager->canvas());
302 KIS_ASSERT(selection);
303 KIS_ASSERT(canvas);
304
305 QRectF selectionBounds = selection->selectedRect();
306 int selectionBottom = selectionBounds.bottom();
307 QPointF selectionCenter = selectionBounds.center();
308 QPointF bottomCenter(selectionCenter.x(), selectionBottom);
309
310 QPointF widgetBottomCenter = canvas->coordinatesConverter()->imageToWidget(bottomCenter); // converts current selection's QPointF into canvasWidget's QPointF space
311
312 widgetBottomCenter.setX(widgetBottomCenter.x() - (d->m_actionBarWidth / 2)); // centers toolbar midpoint with the selection center
313 widgetBottomCenter.setY(widgetBottomCenter.y() + BUFFER_SPACE);
314
315 return updateCanvasBoundaries(widgetBottomCenter.toPoint(), d->m_viewManager->canvas());
316}
317
318void KisSelectionActionsPanel::drawActionBarBackground(QPainter &painter, const KoColorDisplayRendererInterface *displayRendererInterface) const
319{
320 const int cornerRadius = 4;
321 QColor outlineColor = qApp->palette().window().color();
322
323 QColor bgColor = qApp->palette().window().color();
324
325 //Slightly lighten the color, to force `dragColor` to move it in the lighter direction, without this it will always darken the color
327 bgColor = bgColor.lighter(120);
328 }
329
330 KisPaintingTweaks::dragColor(&bgColor, outlineColor, 0.25);
331
332 // Manually convert here, to keep as much consistency as possible.
333 KoColor c;
334 c.fromQColor(bgColor);
335 bgColor = displayRendererInterface->convertColorToDisplayColorSpace(c);
336 c.fromQColor(outlineColor);
337 outlineColor = displayRendererInterface->convertColorToDisplayColorSpace(c);
338
339 QColor bgColorTrans = bgColor;
340 bgColorTrans.setAlpha(80);
341 const int outline_width = 4;
342
343 //an outer 1px wide outline for contrast against the background
344 QRectF contrastOutline(d->m_dragHandle->position - QPoint(outline_width + 1,outline_width + 1), QSize(d->m_actionBarWidth, BUTTON_SIZE) +QSize(outline_width + 1,outline_width + 1) * 2);
345 QRectF midOutline(d->m_dragHandle->position - QPoint(outline_width,outline_width), QSize(d->m_actionBarWidth, BUTTON_SIZE) +QSize(outline_width,outline_width) * 2);
346 //Add a bit of padding here for the icons
347 QRectF centerBackground(d->m_dragHandle->position - QPoint(outline_width,outline_width) / 2, QSize(d->m_actionBarWidth - BUTTON_SIZE, BUTTON_SIZE) +QSize(outline_width,outline_width));
348 QPainterPath bgPath;
349 QPainterPath outlinePath;
350 QPainterPath contrastOutlinePath;
351
352 bgPath.addRoundedRect(centerBackground, cornerRadius, cornerRadius);
353 outlinePath.addRoundedRect(midOutline, cornerRadius, cornerRadius);
354 contrastOutlinePath.addRoundedRect(contrastOutline, cornerRadius, cornerRadius);
355
356 painter.fillPath(contrastOutlinePath, bgColorTrans);
357 painter.fillPath(outlinePath, outlineColor);
358 painter.fillPath(bgPath, bgColor);
359}
360
361bool KisSelectionActionsPanel::handlePress(QEvent *event, const QPoint &pos, Qt::MouseButton button)
362{
363 if (d->m_pressed) {
364 event->accept();
365 return true;
366 }
367
368 if (button == Qt::LeftButton) {
369 d->m_pressed = true;
370 d->m_dragHandle->dragOrigin = pos - d->m_dragHandle->position;
371 d->m_handleWidget->set_held(true);
372
373 event->accept();
374 return true;
375 }
376
377 return false;
378}
379
380bool KisSelectionActionsPanel::handleMove(QEvent *event, const QPoint &pos)
381{
382 QWidget *canvasWidget = d->m_viewManager->canvas();
383 QPoint newPos = pos - d->m_dragHandle->dragOrigin;
384 d->m_dragHandle->position = updateCanvasBoundaries(newPos, canvasWidget);
386 canvasWidget->update();
387 event->accept();
388 return true;
389}
390
392{
393 d->m_handleWidget->move(d->m_dragHandle->position.x() + d->m_buttons.size() * BUTTON_SIZE, d->m_dragHandle->position.y());
394 d->m_handleWidget->show();
395
396 int i = 0;
397 Q_FOREACH(KisSelectionActionsPanelButton *button, d->m_buttons) {
398 int buttonPosition = i * BUTTON_SIZE;
399 button->move(d->m_dragHandle->position.x() + buttonPosition, d->m_dragHandle->position.y());
400 button->show();
401 i++;
402 }
403}
404
405QPoint KisSelectionActionsPanel::mouseEventPos(const QMouseEvent *mouseEvent)
406{
407#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
408 return transformHandleCoords(mouseEvent->position().toPoint());
409#else
410 return transformHandleCoords(mouseEvent->pos());
411#endif
412}
413
414QPoint KisSelectionActionsPanel::tabletEventPos(const QTabletEvent *tabletEvent)
415{
416#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
417 return transformHandleCoords(tabletEvent->position().toPoint());
418#else
419 return transformHandleCoords(tabletEvent->pos());
420#endif
421}
422
423bool KisSelectionActionsPanel::touchEventPos(const QTouchEvent *touchEvent, QPoint &outPos)
424{
425#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
426 if (touchEvent->pointCount() < 1) {
427 return false;
428 } else {
429 outPos = transformHandleCoords(touchEvent->points().first().position().toPoint());
430 return true;
431 }
432#else
433 const QList<QTouchEvent::TouchPoint> &touchPoints = touchEvent->touchPoints();
434 if (touchPoints.isEmpty()) {
435 return false;
436 } else {
437 outPos = transformHandleCoords(touchPoints.first().pos().toPoint());
438 return true;
439 }
440#endif
441}
442
444 return d->m_dragHandle->position + pos;
445}
446
448{
449 QMenu menu = QMenu();
450 menu.addAction(d->disable_action);
451 menu.addAction(d->configure_action);
452 menu.exec(pos);
453}
454
461
463{
464 KisAction *a = d->m_viewManager->actionManager()->actionByName("options_configure");
465
467
468 a->trigger();
469}
virtual QWidget * widget()=0
KisAction * actionByName(const QString &name) const
KisActionManager * actionManager
KisCoordinatesConverter * coordinatesConverter() const
static KisConfigNotifier * instance()
void setSelectionActionBar(bool value)
_Private::Traits< T >::Result imageToWidget(const T &obj) const
Custom widget for selection actions panel buttons, to prevent them being drawn over the pop-up palett...
bool handlePress(QEvent *event, const QPoint &pos, Qt::MouseButton button=Qt::LeftButton)
void movePanelWidgets()
Moves all the widgets that are a part of the panel.
QPoint updateCanvasBoundaries(QPoint position, QWidget *canvasWidget) const
bool eventFilter(QObject *obj, QEvent *event) override
QPoint tabletEventPos(const QTabletEvent *tabletEvent)
bool handleMove(QEvent *event, const QPoint &pos)
QPoint mouseEventPos(const QMouseEvent *mouseEvent)
void canvasWidgetChanged(KisCanvasWidgetBase *canvas)
void draw(QPainter &painter, const KoColorDisplayRendererInterface *displayRendererInterface)
bool touchEventPos(const QTouchEvent *touchEvent, QPoint &outPos)
void drawActionBarBackground(QPainter &gc, const KoColorDisplayRendererInterface *displayRendererInterface) const
KisActionManager * actionManager() const
QWidget * canvas() const
Return the actual widget that is displaying the current image.
KisSelectionManager * selectionManager()
virtual QColor convertColorToDisplayColorSpace(const KoColor color) const =0
convertColorToDisplayColorSpace
void fromQColor(const QColor &c)
Convenient function for converting from a QColor.
Definition KoColor.cpp:213
#define KIS_ASSERT(cond)
Definition kis_assert.h:33
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)
static constexpr int BUFFER_SPACE
static constexpr int BUTTON_SIZE
QString button(const QWheelEvent &ev)
void dragColor(QColor *color, const QColor &baseColor, qreal threshold)
void(KisSelectionManager::*)() TargetSlot
KisSelectionActionsPanelHandle * m_handleWidget
QList< KisSelectionActionsPanelButton * > m_buttons
static const QVector< ActionButtonData > & buttonData()
QRect selectedRect() const