Krita Source Code Documentation
Loading...
Searching...
No Matches
overviewdocker_page.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2009 Cyrille Berger <cberger@cberger.net>
3 * SPDX-FileCopyrightText: 2021 Deif Lou <ginoba@gmail.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
9#include "overviewwidget.h"
10#include "overviewdocker_page.h"
11
12#include <QVBoxLayout>
13#include <QHBoxLayout>
14#include <QToolButton>
15#include <QStatusBar>
16#include <QApplication>
17
18#include <KisAngleSelector.h>
19#include <klocalizedstring.h>
20#include "kis_canvas2.h"
21#include <KisViewManager.h>
22#include <kactioncollection.h>
23#include <kis_action.h>
24#include <kis_zoom_manager.h>
25#include "kis_image.h"
26#include "kis_paint_device.h"
29#include "kis_icon_utils.h"
30#include "kis_signals_blocker.h"
31#include <KoZoomWidget.h>
32#include <kis_icon_utils.h>
33
34#include <kconfiggroup.h>
35#include <ksharedconfig.h>
36
37OverviewDockerPage::OverviewDockerPage(QWidget *parent, const char *name, Qt::WindowFlags f)
38 : QWidget(parent, f)
39 , m_lastOverviewMousePos(0.0, 0.0)
40{
41 setObjectName(name);
42
44 m_overviewWidget->setMinimumHeight(50);
45 m_overviewWidget->setBackgroundRole(QPalette::Base);
46 // paints background role before paint()
47 m_overviewWidget->setAutoFillBackground(true);
48 m_overviewWidget->setAttribute(Qt::WA_AcceptTouchEvents, true);
49 m_overviewWidget->installEventFilter(this);
50 connect(m_overviewWidget, SIGNAL(signalDraggingStarted()), SLOT(on_overviewWidget_signalDraggingStarted()));
51 connect(m_overviewWidget, SIGNAL(signalDraggingFinished()), SLOT(on_overviewWidget_signalDraggingFinished()));
52
53 m_controlsContainer = new QWidget(this);
54
55 m_controlsLayout = new QVBoxLayout;
56 m_controlsLayout->setContentsMargins(2, 2, 2, 2);
57 m_controlsLayout->setSpacing(2);
59
60 m_showControlsTimer.setSingleShot(true);
61
62 m_showControlsAnimation.setEasingCurve(QEasingCurve(QEasingCurve::InOutCubic));
63 connect(&m_showControlsAnimation, &QVariantAnimation::valueChanged, this, &OverviewDockerPage::layoutMainWidgets);
64
65 KConfigGroup config = KSharedConfig::openConfig()->group("OverviewDocker");
66 m_pinControls = config.readEntry("pinControls", true);
68 m_grayscale = config.readEntry("grayscale", false);
70
71 setEnabled(false);
72}
73
75{
76 KConfigGroup config = KSharedConfig::openConfig()->group("OverviewDocker");
77 config.writeEntry("pinControls", m_pinControls);
78 config.writeEntry("grayscale", m_grayscale);
79}
80
82{
83 if(m_canvas == canvas)
84 return;
85
86 setEnabled(canvas != nullptr);
87
88 if (m_canvas) {
89 m_canvas->disconnectCanvasObserver(this);
90 m_canvas->image()->disconnect(this);
91 m_canvas = nullptr;
92 }
93
94 if (m_zoomSlider) {
96 delete m_zoomSlider;
97 m_zoomSlider = nullptr;
98 }
99
103 m_rotateAngleSelector = nullptr;
104 }
105
106 if (m_mirrorCanvas) {
108 delete m_mirrorCanvas;
109 m_mirrorCanvas = nullptr;
110 }
111
114 delete m_pinControlsButton;
115 m_pinControlsButton = nullptr;
116 }
117
118 if (m_grayscaleButton) {
120 delete m_grayscaleButton;
121 m_grayscaleButton = nullptr;
122 }
123
124 // Delete the stretch
125 while (m_controlsSecondRowLayout && m_controlsSecondRowLayout->count() && m_controlsSecondRowLayout->itemAt(0)->spacerItem()) {
126 delete m_controlsSecondRowLayout->takeAt(0);
127 }
128
130
133
134 m_canvas = dynamic_cast<KisCanvas2*>(canvas);
135
137 if (m_canvas && m_canvas->viewManager()) {
138 bool usePrintResolutionMode = m_canvas->imageView()->canvasController()->usePrintResolutionMode();
139 m_zoomSlider = m_canvas->imageView()->zoomManager()->zoomAction()->createWidget(m_canvas->imageView()->KisView::statusBar());
140 KoZoomWidget* zoomWidget = static_cast<KoZoomWidget*>(m_zoomSlider);
141 zoomWidget->setZoomInputFlat(false);
142 zoomWidget->setUsePrintResolutionMode(usePrintResolutionMode);
143 m_controlsLayout->addWidget(m_zoomSlider);
144
146 m_rotateAngleSelector->setRange(-360.00, 360.0);
147 m_rotateAngleSelector->setAngle(m_canvas->rotationAngle());
150 connect(m_rotateAngleSelector, SIGNAL(angleChanged(qreal)), this, SLOT(rotateCanvasView(qreal)), Qt::UniqueConnection);
151 connect(m_canvas->canvasController()->proxyObject, SIGNAL(documentRotationChanged(qreal)), this, SLOT(updateRotationSlider(qreal)));
152
153 m_mirrorCanvas = new QToolButton();
154 QList<QAction *> actions = m_canvas->viewManager()->actionCollection()->actions();
155 Q_FOREACH(QAction* action, actions) {
156 if (action->objectName()=="mirror_canvas") {
157 m_mirrorCanvas->setDefaultAction(action);
158 }
159 }
160 m_mirrorCanvas->setIcon(KisIconUtils::loadIcon("mirror-view-16"));
161 m_mirrorCanvas->setAutoRaise(true);
162 connect(m_mirrorCanvas, SIGNAL(toggled(bool)), this, SLOT(mirrorUpdateIcon()));
163
164 m_pinControlsButton = new QToolButton;
165 m_pinControlsButton->setCheckable(true);
167 m_pinControlsButton->setToolTip(
168 i18nc("Make the controls in the overview docker auto-hide or always visible", "Pin navigation controls")
169 );
170 m_pinControlsButton->setIcon(KisIconUtils::loadIcon("krita_tool_reference_images"));
171 m_pinControlsButton->setAutoRaise(true);
172 connect(m_pinControlsButton, SIGNAL(toggled(bool)), SLOT(setPinControls(bool)));
173
174 m_grayscaleButton = new QToolButton;
175 m_grayscaleButton->setCheckable(true);
176 m_grayscaleButton->setChecked(m_grayscale);
177 m_grayscaleButton->setToolTip(
178 i18nc("Show the overview docker preview in shades of gray", "Grayscale preview")
179 );
180 m_grayscaleButton->setIcon(KisIconUtils::loadIcon("saturation-decrease"));
181 m_grayscaleButton->setAutoRaise(true);
182 connect(m_grayscaleButton, SIGNAL(toggled(bool)), SLOT(setGrayscale(bool)));
183
184 m_controlsSecondRowLayout = new QHBoxLayout();
185
189 m_controlsSecondRowLayout->addStretch();
192
193 //m_zoomSlider->setVisible(true);
194 m_rotateAngleSelector->setVisible(true);
195
196 // Show/hide the controls
197 if (m_pinControls) {
198 showControls(0);
199 } else {
200 if (m_cursorIsHover) {
201 showControls(0);
202 } else {
203 hideControls(0);
204 }
205 }
206 }
207}
208
210{
211 setEnabled(false);
212 m_canvas = nullptr;
214}
215
217{
218 if(!m_mirrorCanvas) return;
219 m_mirrorCanvas->setIcon(KisIconUtils::loadIcon("mirror-view-16"));
220}
221
223{
224 if (!m_canvas) return;
225 KisCanvasController *canvasController =
226 dynamic_cast<KisCanvasController*>(m_canvas->viewManager()->canvasBase()->canvasController());
227 if (canvasController) {
228 canvasController->rotateCanvas(rotation-m_canvas->rotationAngle());
229 }
230}
231
233{
234 if (qAbs(m_rotateAngleSelector->angle() - angle) > 0.01) {
236 m_rotateAngleSelector->setAngle(m_canvas->rotationAngle());
237 }
238}
239
241{
242 m_pinControls = pin;
243}
244
246{
247 m_grayscale = grayscale;
248 m_overviewWidget->setGrayscale(grayscale);
249}
250
252{
254}
255
257{
258 m_cursorIsHover = false;
259 if (isEnabled() && !m_pinControls) {
260 hideControls(0);
262 }
263}
264
265#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
267#else
269#endif
270{
271 m_cursorIsHover = true;
272 if (isEnabled() && !m_pinControls) {
273 showControls(showControlsTimerDuration);
274 }
275}
276
278{
279 if (e->type() == QEvent::StyleChange || e->type() == QEvent::FontChange) {
280 resizeEvent(nullptr);
281 }
282 return QWidget::event(e);
283}
284
285bool OverviewDockerPage::eventFilter(QObject *o, QEvent *e)
286{
287 if (!isEnabled()) {
288 return false;
289 }
290
291 if (o == m_overviewWidget) {
292 // Filter out the mouse events if we are touching the overview widget
293 // and the event was not synthesized in this function from the touch events
294 if (e->type() == QEvent::MouseButtonPress) {
295 if (m_isTouching) {
296 return static_cast<QMouseEvent*>(e)->source() != Qt::MouseEventSynthesizedByApplication;
297 }
298
299 } else if (e->type() == QEvent::MouseButtonRelease) {
300 if (m_isTouching) {
301 return static_cast<QMouseEvent*>(e)->source() != Qt::MouseEventSynthesizedByApplication;
302 }
303
304 } else if (e->type() == QEvent::MouseMove) {
305 if (m_isTouching) {
306 return static_cast<QMouseEvent*>(e)->source() != Qt::MouseEventSynthesizedByApplication;
307 }
309 QMouseEvent *me = static_cast<QMouseEvent*>(e);
310 constexpr double showControlsAreaRadiusSquared = showControlsAreaRadius * showControlsAreaRadius;
311 const QPointF d = me->localPos() - m_lastOverviewMousePos;
312 const double distanceSquared = d.x() * d.x() + d.y() * d.y();
313 if (distanceSquared > m_cumulatedMouseDistanceSquared) {
314 if (distanceSquared >= showControlsAreaRadiusSquared) {
316 m_lastOverviewMousePos = me->localPos();
318 } else {
319 m_cumulatedMouseDistanceSquared = distanceSquared;
320 }
321 }
322 }
323
324 } else if (e->type() == QEvent::TouchBegin) {
325 if (!m_isTouching) {
326 QTouchEvent *te = static_cast<QTouchEvent*>(e);
327 m_isTouching = true;
328 // Store the first touch point. We will only track this one
329 m_touchPointId = te->touchPoints().first().id();
330 m_lastTouchPos = te->touchPoints().first().pos();
331 }
332 // Accept the event so that other touch events keep coming
333 e->accept();
334 return true;
335
336 } else if (e->type() == QEvent::TouchUpdate) {
337 if (!m_isTouching) {
338 return true;
339 }
340 QTouchEvent *te = static_cast<QTouchEvent*>(e);
341 // Get the touch point position
342 QPointF currentPosition;
343 for (const QTouchEvent::TouchPoint &touchPoint : te->touchPoints()) {
344 if (touchPoint.id() == m_touchPointId) {
345 // If the touch point wasn't moved, this event wasn't
346 // generated from our touch point
347 if (touchPoint.state() == Qt::TouchPointStationary) {
348 return true;
349 }
350 currentPosition = touchPoint.pos();
351 break;
352 }
353 }
355 // Compute distance
356 const QPointF delta = currentPosition - m_lastTouchPos;
357 const qreal distanceSquared = delta.x() * delta.x() + delta.y() * delta.y();
358 if (distanceSquared >= touchDragDistanceSquared) {
360 // synthesize mouse press event
361 QMouseEvent *se = new QMouseEvent(QEvent::MouseButtonPress,
362 m_lastTouchPos, QPointF(), QPointF(),
363 Qt::LeftButton, Qt::LeftButton, Qt::NoModifier,
364 Qt::MouseEventSynthesizedByApplication);
365 qApp->sendEvent(m_overviewWidget, se);
366 }
367 }
369 // Synthesize mouse move event
370 QMouseEvent *se = new QMouseEvent(QEvent::MouseMove,
371 currentPosition, QPointF(), QPointF(),
372 Qt::LeftButton, Qt::LeftButton, Qt::NoModifier,
373 Qt::MouseEventSynthesizedByApplication);
374 qApp->sendEvent(m_overviewWidget, se);
375 // Update
376 m_lastTouchPos = currentPosition;
377 }
378 return true;
379
380 } else if (e->type() == QEvent::TouchEnd || e->type() == QEvent::TouchCancel) {
381 if (!m_isTouching) {
382 return true;
383 }
384 QTouchEvent *te = static_cast<QTouchEvent*>(e);
385 if (e->type() == QEvent::TouchEnd) {
386 // If the touch point is not in the released state
387 // then this event wasn't generated from our touch point
388 for (const QTouchEvent::TouchPoint &touchPoint : te->touchPoints()) {
389 if (touchPoint.id() == m_touchPointId) {
390 if (touchPoint.state() != Qt::TouchPointReleased) {
391 return true;
392 }
393 break;
394 }
395 }
396 }
397 // If we are dragging then synthesize mouse release event.
398 // Show/hide the controls otherwise
400 QMouseEvent *se = new QMouseEvent(QEvent::MouseButtonRelease,
401 m_lastTouchPos, QPointF(), QPointF(),
402 Qt::LeftButton, Qt::LeftButton, Qt::NoModifier,
403 Qt::MouseEventSynthesizedByApplication);
404 qApp->sendEvent(m_overviewWidget, se);
405 } else if (e->type() == QEvent::TouchEnd) {
410 showControls(0);
411 } else {
412 hideControls(0);
413 }
414 }
415 // Reset
416 m_isTouching = false;
417 m_isDraggingWithTouch = false;
418 return true;
419 }
420 }
421 return false;
422}
423
425{
426 this->setMinimumHeight(m_overviewWidget->minimumHeight() +
427 m_controlsContainer->minimumSizeHint().height());
428
429 if (m_showControlsAnimation.state() == QVariantAnimation::Running) {
430 const qreal pageHeight = static_cast<qreal>(this->height());
431 const qreal controlsContainerHeight = static_cast<qreal>(m_controlsContainer->sizeHint().height());
432 const qreal animationProgress = m_showControlsAnimation.currentValue().toReal();
433 const int widgetLimitPosition = static_cast<int>(std::round(pageHeight - animationProgress * controlsContainerHeight));
434 m_overviewWidget->setGeometry(0, 0, this->width(), widgetLimitPosition);
435 m_controlsContainer->setGeometry(0, widgetLimitPosition, this->width(), static_cast<int>(controlsContainerHeight));
436 } else {
437 const int controlsContainerHeight = m_controlsContainer->sizeHint().height();
439 const int widgetLimitPosition = this->height() - controlsContainerHeight;
440 m_overviewWidget->setGeometry(0, 0, this->width(), widgetLimitPosition);
441 m_controlsContainer->setGeometry(0, widgetLimitPosition, this->width(), controlsContainerHeight);
442 } else {
443 m_overviewWidget->setGeometry(0, 0, this->width(), this->height());
444 m_controlsContainer->setGeometry(0, this->height(), this->width(), controlsContainerHeight);
445 }
446 }
447}
448
450{
451 auto animFunction =
452 [this]() -> void
453 {
454 int animationDuration;
455 qreal animationStartValue;
456
458 if (m_showControlsAnimation.state() == QVariantAnimation::Running) {
460 animationDuration =
461 static_cast<int>(std::round((1.0 - m_showControlsAnimation.currentValue().toReal()) * showControlsAnimationDuration));
462 animationStartValue = m_showControlsAnimation.currentValue().toReal();
463 } else {
464 animationDuration = showControlsAnimationDuration;
465 animationStartValue = 0.0;
466 }
467 } else {
468 animationDuration = 1;
469 animationStartValue = 1.0;
470 }
471
472 m_areControlsHidden = false;
473 m_showControlsAnimation.setStartValue(animationStartValue);
474 m_showControlsAnimation.setEndValue(1.0);
475 m_showControlsAnimation.setDuration(animationDuration);
477 };
478
479 delay = qMax(delay, 0);
480
481 m_showControlsTimer.disconnect(SIGNAL(timeout()));
482 connect(&m_showControlsTimer, &QTimer::timeout, this, animFunction);
483 m_showControlsTimer.start(delay);
484}
485
487{
488 auto animFunction =
489 [this]() -> void
490 {
491 int animationDuration;
492 qreal animationStartValue;
493
494 if (!m_areControlsHidden) {
495 if (m_showControlsAnimation.state() == QVariantAnimation::Running) {
497 animationDuration =
498 static_cast<int>(std::round(m_showControlsAnimation.currentValue().toReal() * showControlsAnimationDuration));
499 animationStartValue = m_showControlsAnimation.currentValue().toReal();
500 } else {
501 animationDuration = showControlsAnimationDuration;
502 animationStartValue = 1.0;
503 }
504 } else {
505 animationDuration = 1;
506 animationStartValue = 0.0;
507 }
508
509 m_areControlsHidden = true;
510 m_showControlsAnimation.setStartValue(animationStartValue);
511 m_showControlsAnimation.setEndValue(0.0);
512 m_showControlsAnimation.setDuration(animationDuration);
514 };
515
516 delay = qMax(delay, 0);
517
518 m_showControlsTimer.disconnect(SIGNAL(timeout()));
519 connect(&m_showControlsTimer, &QTimer::timeout, this, animFunction);
520 m_showControlsTimer.start(delay);
521}
522
529
KisMagneticGraph::vertex_descriptor source(typename KisMagneticGraph::edge_descriptor e, KisMagneticGraph g)
@ IncreasingDirection_Clockwise
A widget with several options to select an angle.
@ FlipOptionsMode_ContextMenu
The flip options are shown only as a context menu when right-clicking the gauge widget.
qreal angle() const
Gets the current angle.
void setFlipOptionsMode(FlipOptionsMode newMode)
Sets the mode in which the flip options should be shown.
void setIncreasingDirection(KisAngleGauge::IncreasingDirection newIncreasingDirection)
Sets the increasing direction in the angle gauge.
void setAngle(qreal newAngle)
Sets the current angle.
void setRange(qreal newMinimum, qreal newMaximum)
Sets the minimum and maximum values for the angle.
void rotateCanvas(qreal angle, const std::optional< KoViewTransformStillPoint > &stillPoint, bool isNativeGesture=false)
void setZoomInputFlat(bool flat)
void setUsePrintResolutionMode(bool value)
void leaveEvent(QEvent *) override
bool eventFilter(QObject *o, QEvent *e) override
void showControls(int delay) const
OverviewDockerPage(QWidget *parent=0, const char *name=0, Qt::WindowFlags f=Qt::WindowFlags())
void setGrayscale(bool grayscale)
QToolButton * m_pinControlsButton
static constexpr double touchDragDistanceSquared
static constexpr double showControlsAnimationDuration
QHBoxLayout * m_controlsSecondRowLayout
QToolButton * m_grayscaleButton
static constexpr double showControlsAreaRadius
QVariantAnimation m_showControlsAnimation
KisAngleSelector * m_rotateAngleSelector
static constexpr int showControlsTimerDuration
QVBoxLayout * m_controlsLayout
void setCanvas(KoCanvasBase *canvas)
void resizeEvent(QResizeEvent *) override
bool event(QEvent *e) override
QPointer< KisCanvas2 > m_canvas
void on_overviewWidget_signalDraggingFinished()
void rotateCanvasView(qreal rotation)
void updateRotationSlider(qreal rotation)
void hideControls(int delay) const
void enterEvent(QEnterEvent *event) override
QToolButton * m_mirrorCanvas
OverviewWidget * m_overviewWidget
void setGrayscale(bool value)
void setCanvas(KisCanvas2 *canvas) override
bool isDragging() const
QIcon loadIcon(const QString &name)