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

#include <kis_selection_actions_panel.h>

+ Inheritance diagram for KisSelectionActionsPanel:

Classes

struct  Private
 

Public Member Functions

void draw (QPainter &painter)
 
bool eventFilter (QObject *obj, QEvent *event) override
 
 KisSelectionActionsPanel ()=delete
 
 KisSelectionActionsPanel (KisViewManager *viewManager, QWidget *parent)
 
void setEnabled (bool enabled)
 
void setVisible (bool visible)
 
 ~KisSelectionActionsPanel ()
 

Private Member Functions

QPushButton * createButton (const QString &iconName, const QString &tooltip)
 
void drawActionBarBackground (QPainter &gc) const
 
QPoint initialDragHandlePosition () const
 
void setupButtons ()
 
QPoint updateCanvasBoundaries (QPoint position, QWidget *canvasWidget) const
 

Private Attributes

QScopedPointer< Privated
 

Detailed Description

Definition at line 30 of file kis_selection_actions_panel.h.

Constructor & Destructor Documentation

◆ KisSelectionActionsPanel() [1/2]

KisSelectionActionsPanel::KisSelectionActionsPanel ( )
delete

◆ KisSelectionActionsPanel() [2/2]

KisSelectionActionsPanel::KisSelectionActionsPanel ( KisViewManager * viewManager,
QWidget * parent )

Definition at line 81 of file kis_selection_actions_panel.cpp.

82 : QWidget(parent)
83 , d(new Private)
84{
85 d->m_viewManager = viewManager;
86 d->m_selectionManager = viewManager->selectionManager();
87
88 // Setup buttons...
89 for (const ActionButtonData &buttonData : Private::buttonData()) {
90 QPushButton *button = createButton(buttonData.iconName, buttonData.tooltip);
91 connect(button, &QPushButton::clicked, d->m_selectionManager, buttonData.slot);
92 d->m_buttons.append(button);
93 }
94}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
QPushButton * createButton(const QString &iconName, const QString &tooltip)
KisSelectionManager * selectionManager()
QString button(const QWheelEvent &ev)

References button(), KisSelectionActionsPanel::Private::buttonData(), connect(), createButton(), d, and KisViewManager::selectionManager().

◆ ~KisSelectionActionsPanel()

KisSelectionActionsPanel::~KisSelectionActionsPanel ( )

Definition at line 96 of file kis_selection_actions_panel.cpp.

97{
98 // buttons are children of the canvas, but we should still delete
99 // them to make sure they are not accessed after the decoration dies
100 qDeleteAll(d->m_buttons);
101 d->m_buttons.clear();
102}

References d.

Member Function Documentation

◆ createButton()

QPushButton * KisSelectionActionsPanel::createButton ( const QString & iconName,
const QString & tooltip )
private

Definition at line 258 of file kis_selection_actions_panel.cpp.

259{
260 QPushButton *button = new QPushButton();
261 button->setIcon(KisIconUtils::loadIcon(iconName));
262 button->setFixedSize(BUTTON_SIZE, BUTTON_SIZE);
263 button->setToolTip(tooltip);
264 return button;
265}
const int BUTTON_SIZE
QIcon loadIcon(const QString &name)

References button(), BUTTON_SIZE, and KisIconUtils::loadIcon().

◆ draw()

void KisSelectionActionsPanel::draw ( QPainter & painter)

Definition at line 104 of file kis_selection_actions_panel.cpp.

105{
106 KisSelectionSP selection = d->m_viewManager->selection();
107
108 if (!selection || !d->m_enabled || !d->m_visible) {
109 return;
110 }
111
113
114 for (int i = 0; i < d->m_buttons.size(); i++) {
115 QPushButton *button = d->m_buttons[i];
116 int buttonPosition = i * BUTTON_SIZE;
117 button->move(d->m_dragHandle->position.x() + buttonPosition, d->m_dragHandle->position.y());
118 button->show();
119 }
120}
void drawActionBarBackground(QPainter &gc) const

References button(), BUTTON_SIZE, d, and drawActionBarBackground().

◆ drawActionBarBackground()

void KisSelectionActionsPanel::drawActionBarBackground ( QPainter & gc) const
private

Definition at line 267 of file kis_selection_actions_panel.cpp.

268{
269 const int CORNER_RADIUS = 4;
270 const int PEN_WIDTH = 5;
271 const QColor BACKGROUND_COLOR = Qt::darkGray;
272 const QColor OUTLINE_COLOR(60, 60, 60, 80);
273 const QColor DOT_COLOR = Qt::lightGray;
274 const int DOT_SIZE = 4;
275 const int DOT_SPACING = 5;
276 const QPoint DRAG_HANDLE_RECT_DOTS_OFFSET(10, 10);
277
278 QRectF actionBarRect(d->m_dragHandle->position, QSize(d->m_actionBarWidth, BUTTON_SIZE));
279 QPainterPath bgPath;
280 bgPath.addRoundedRect(actionBarRect, CORNER_RADIUS, CORNER_RADIUS);
281 painter.fillPath(bgPath, BACKGROUND_COLOR);
282
283 QPen pen(OUTLINE_COLOR);
284 pen.setWidth(PEN_WIDTH);
285 painter.setPen(pen);
286 painter.drawPath(bgPath);
287
288 QRectF dragHandleRect(
289 QPoint(d->m_dragHandle->position.x() + d->m_actionBarWidth - BUTTON_SIZE, d->m_dragHandle->position.y()),
290 QSize(BUTTON_SIZE, BUTTON_SIZE));
291 QPainterPath dragHandlePath;
292 dragHandlePath.addRect(dragHandleRect);
293 painter.fillPath(dragHandlePath, BACKGROUND_COLOR);
294
295 const std::list<std::pair<int, int>> DOT_OFFSETS = {{0, 0},
296 {DOT_SPACING, 0},
297 {-DOT_SPACING, 0},
298 {0, DOT_SPACING},
299 {0, -DOT_SPACING},
300 {DOT_SPACING, DOT_SPACING},
301 {DOT_SPACING, -DOT_SPACING},
302 {-DOT_SPACING, DOT_SPACING},
303 {-DOT_SPACING, -DOT_SPACING}};
304
305 QPainterPath dragHandleRectDots;
306 for (const std::pair<int, int> &offset : DOT_OFFSETS) {
307 dragHandleRectDots.addEllipse(offset.first, offset.second, DOT_SIZE, DOT_SIZE);
308 };
309
310 dragHandleRectDots.translate(dragHandleRect.topLeft() + DRAG_HANDLE_RECT_DOTS_OFFSET);
311 painter.fillPath(dragHandleRectDots, DOT_COLOR);
312}

References BUTTON_SIZE, and d.

◆ eventFilter()

bool KisSelectionActionsPanel::eventFilter ( QObject * obj,
QEvent * event )
override

Definition at line 170 of file kis_selection_actions_panel.cpp.

171{
172 bool eventHandled = false;
173
174 bool focusInEvent = event->type() == QEvent::FocusIn;
175 if (focusInEvent && !eventHandled) {
176 eventHandled = true;
177 }
178
179 // Clicks...
180 bool clickEvent = event->type() == QEvent::MouseButtonPress || event->type() == QEvent::TabletPress || event->type() == QEvent::TouchBegin;
181 if (clickEvent && !eventHandled ) {
182 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
183 QRect dragHandleRect(d->m_dragHandle->position, QSize(25 * (d->m_buttonCount), 25));
184 if (dragHandleRect.contains(mouseEvent->pos())) {
185 d->m_dragging = true;
186 d->m_dragHandle->dragOrigin = mouseEvent->pos() - d->m_dragHandle->position;
187
188 eventHandled = true;
189 }
190 }
191
192 // Drags...
193 bool dragEvent = d->m_dragging && (event->type() == QEvent::MouseMove || event->type() == QEvent::TouchUpdate);
194
195 if (dragEvent && !eventHandled) {
196 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
197 QPoint newPos = mouseEvent->pos() - d->m_dragHandle->dragOrigin;
198
199 // bound actionBar to stay within canvas space
200 QWidget *canvasWidget = dynamic_cast<QWidget *>(d->m_viewManager->canvas());
201
202 if (obj == canvasWidget) {
203 d->m_dragHandle->position = updateCanvasBoundaries(newPos, canvasWidget);
204 canvasWidget->update();
205 eventHandled = true;
206 }
207 }
208
209 // Releases...
210 bool releaseEvent = d->m_dragging && (event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::TabletRelease || event->type() == QEvent::TouchEnd);
211
212 if (releaseEvent && d->m_dragging) {
213 d->m_dragging = false;
214 eventHandled = true;
215 }
216
217 return eventHandled;
218}
QPoint updateCanvasBoundaries(QPoint position, QWidget *canvasWidget) const

References d, and updateCanvasBoundaries().

◆ initialDragHandlePosition()

QPoint KisSelectionActionsPanel::initialDragHandlePosition ( ) const
private

Definition at line 238 of file kis_selection_actions_panel.cpp.

239{
240 KisSelectionSP selection = d->m_viewManager->selection();
241 KisCanvasWidgetBase *canvas = dynamic_cast<KisCanvasWidgetBase*>(d->m_viewManager->canvas());
242 KIS_ASSERT(selection);
243 KIS_ASSERT(canvas);
244
245 QRectF selectionBounds = selection->selectedRect();
246 int selectionBottom = selectionBounds.bottom();
247 QPointF selectionCenter = selectionBounds.center();
248 QPointF bottomCenter(selectionCenter.x(), selectionBottom);
249
250 QPointF widgetBottomCenter = canvas->coordinatesConverter()->imageToWidget(bottomCenter); // converts current selection's QPointF into canvasWidget's QPointF space
251
252 widgetBottomCenter.setX(widgetBottomCenter.x() - (d->m_actionBarWidth / 2)); // centers toolbar midpoint with the selection center
253 widgetBottomCenter.setY(widgetBottomCenter.y() + BUFFER_SPACE);
254
255 return updateCanvasBoundaries(widgetBottomCenter.toPoint(), d->m_viewManager->canvas());
256}
KisCoordinatesConverter * coordinatesConverter() const
_Private::Traits< T >::Result imageToWidget(const T &obj) const
#define KIS_ASSERT(cond)
Definition kis_assert.h:33
const int BUFFER_SPACE
QRect selectedRect() const

References BUFFER_SPACE, KisCanvasWidgetBase::coordinatesConverter(), d, KisCoordinatesConverter::imageToWidget(), KIS_ASSERT, KisSelection::selectedRect(), and updateCanvasBoundaries().

◆ setEnabled()

void KisSelectionActionsPanel::setEnabled ( bool enabled)

Definition at line 160 of file kis_selection_actions_panel.cpp.

161{
162 bool configurationChanged = enabled != d->m_enabled;
163 d->m_enabled = enabled;
164 if (configurationChanged) {
165 // Reset visibility when configuration changes
166 setVisible(d->m_visible);
167 }
168}

References d, and setVisible().

◆ setupButtons()

void KisSelectionActionsPanel::setupButtons ( )
private

◆ setVisible()

void KisSelectionActionsPanel::setVisible ( bool visible)

Definition at line 122 of file kis_selection_actions_panel.cpp.

123{
124 QWidget *canvasWidget = dynamic_cast<QWidget *>(d->m_viewManager->canvas());
125 if (!canvasWidget) {
126 return;
127 }
128
129 p_visible &= d->m_enabled;
130
131 const bool VISIBILITY_CHANGED = d->m_visible != p_visible;
132 if (!VISIBILITY_CHANGED) {
133 return;
134 }
135
136 if (d->m_viewManager->selection() && p_visible) { // Now visible!
137 canvasWidget->installEventFilter(this);
138
139 if (!d->m_dragHandle) {
140 d->m_dragHandle.reset(new Private::DragHandle());
141 d->m_dragHandle->position = initialDragHandlePosition();
142 }
143
144 for (QPushButton *button : d->m_buttons) {
145 button->setParent(canvasWidget);
146 }
147 } else { // Now hidden!
148 canvasWidget->removeEventFilter(this);
149
150 for (QPushButton *button : d->m_buttons) {
151 button->hide();
152 }
153
154 d->m_dragHandle.reset();
155 }
156
157 d->m_visible = p_visible;
158}

References button(), d, and initialDragHandlePosition().

◆ updateCanvasBoundaries()

QPoint KisSelectionActionsPanel::updateCanvasBoundaries ( QPoint position,
QWidget * canvasWidget ) const
private

Definition at line 220 of file kis_selection_actions_panel.cpp.

221{
222 QRect canvasBounds = canvasWidget->rect();
223
224 const int ACTION_BAR_WIDTH = d->m_actionBarWidth;
225 const int ACTION_BAR_HEIGHT = BUTTON_SIZE;
226
227 position.setX(qBound(canvasBounds.left() + BUFFER_SPACE,
228 position.x(),
229 canvasBounds.right() - ACTION_BAR_WIDTH - BUFFER_SPACE));
230
231 position.setY(qBound(canvasBounds.top() + BUFFER_SPACE,
232 position.y(),
233 canvasBounds.bottom() - ACTION_BAR_HEIGHT - BUFFER_SPACE));
234
235 return position;
236}

References BUFFER_SPACE, BUTTON_SIZE, and d.

Member Data Documentation

◆ d

QScopedPointer<Private> KisSelectionActionsPanel::d
private

Definition at line 51 of file kis_selection_actions_panel.h.


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