Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Fix popup palette to be a native popup on Wayland
Dmitry Kazakov25716e5536db
diff --git a/libs/ui/KisPopupWidgetInterface.h b/libs/ui/KisPopupWidgetInterface.hindex 4d2be1f2fa..ae848be86b 100644--- a/libs/ui/KisPopupWidgetInterface.h+++ b/libs/ui/KisPopupWidgetInterface.h@@ -11,12 +11,6 @@#include <QWidget>#include <QGridLayout>-#include "kis_debug.h"-#include "kis_assert.h"--#include "KoCanvasBase.h"-#include "input/kis_input_manager.h"-/*** @brief The PopupWidgetInterface abstract class defines* the basic interface that will be used by all popup widgets.@@ -52,68 +46,4 @@ Q_SIGNALS:Q_DECLARE_INTERFACE(KisPopupWidgetInterface, "KisPopupWidgetInterface")-//===================================================================================--/**- * @brief The KisPopupWidget class is a simple wrapper that- * turns any QWidget into a popup widget that can be temporarily- * displayed over the canvas.- */-class KisPopupWidget : public QWidget, public KisPopupWidgetInterface-{- Q_OBJECT- Q_INTERFACES(KisPopupWidgetInterface)--public:- KisPopupWidget(QWidget* toPopup, KoCanvasBase* canvas)- : QWidget(canvas->canvasWidget())- {- KIS_ASSERT(toPopup);-- m_toPopup = toPopup;- m_toPopup->setParent(this);-- setLayout(new QGridLayout());- layout()->addWidget(m_toPopup);-- setAutoFillBackground(true);- }-- void popup(const QPoint& position) override {- setVisible(true);- adjustPopupLayout(position);- }-- void dismiss() override {- setVisible(false);- }-- bool onScreen() override {- return isVisible();- }-- void adjustPopupLayout(const QPoint& position) {- if (isVisible() && parentWidget()) {- const float widgetMargin = -20.0f;- const QRect fitRect = kisGrowRect(parentWidget()->rect(), widgetMargin);- const QPoint paletteCenterOffset(sizeHint().width() / 2, sizeHint().height() / 2);-- QRect paletteRect = rect();-- paletteRect.moveTo(position - paletteCenterOffset);-- paletteRect = kisEnsureInRect(paletteRect, fitRect);- move(paletteRect.topLeft());- }- }-- QSize sizeHint() const override {- KIS_ASSERT(m_toPopup);- return m_toPopup->sizeHint();- }--private:- QWidget* m_toPopup;-};-#endif // KISPOPUPWIDGETINTERFACE_Hdiff --git a/libs/ui/kis_popup_palette.cpp b/libs/ui/kis_popup_palette.cppindex 3acde6dc0f..c0ddf7950f 100644--- a/libs/ui/kis_popup_palette.cpp+++ b/libs/ui/kis_popup_palette.cpp@@ -31,6 +31,7 @@#include "kis_acyclic_signal_connector.h"#include <kis_paintop_preset.h>#include "KisMouseClickEater.h"+#include <KisPlatformPluginInterfaceFactory.h>static const int WIDGET_MARGIN = 16;static const qreal BORDER_WIDTH = 3.0;@@ -90,7 +91,12 @@ private:KisPopupPalette::KisPopupPalette(KisViewManager* viewManager, KisCoordinatesConverter* coordinatesConverter ,KisFavoriteResourceManager* manager,const KoColorDisplayRendererInterface *displayRenderer, QWidget *parent)- : QWidget(parent, Qt::FramelessWindowHint)+ : QWidget(parent,+ KisPlatformPluginInterfaceFactory::instance()->surfaceColorManagedByOS() ?+ // when using Wayland, the popup should have a native window+ // to avoid wrong color space to be used+ Qt::Popup :+ Qt::FramelessWindowHint), m_coordinatesConverter(coordinatesConverter), m_viewManager(viewManager), m_actionManager(viewManager->actionManager())@@ -253,6 +259,9 @@ KisPopupPalette::KisPopupPalette(KisViewManager* viewManager, KisCoordinatesConvKisConfig cfg(true);m_dockerHudButton->setChecked(cfg.showBrushHud());m_bottomBarButton->setChecked(cfg.showPaletteBottomBar());++ m_dockerHud->setVisible(m_dockerHudButton->isChecked());+ m_bottomBarWidget->setVisible(m_bottomBarButton->isChecked());}KisPopupPalette::~KisPopupPalette()@@ -736,9 +745,16 @@ void KisPopupPalette::resizeEvent(QResizeEvent* resizeEvent) {m_resetCanvasRotationIndicatorRect = rotationIndicatorRect(0);m_canvasRotationIndicatorRect = rotationIndicatorRect(m_coordinatesConverter->rotationAngle());// Ensure that the resized geometry fits within the desired rect...- QRect tempGeo = rect();- tempGeo.translate(pos());- ensureWithinParent(tempGeo.topLeft(), true);++ /**+ * When popup is native, its geometry report the global position; then+ * the popup is non-native, its geometry is in its parent's coordinate+ * system.+ */+ const QPoint globalTopLeft = windowHandle() ?+ geometry().topLeft() :+ parentWidget()->mapToGlobal(geometry().topLeft());+ ensureWithinParent(globalTopLeft, true);}QPainterPath KisPopupPalette::drawDonutPathFull(int x, int y, int inner_radius, int outer_radius)@@ -1044,7 +1060,9 @@ void KisPopupPalette::slotRemoveMirrorPos() {void KisPopupPalette::popup(const QPoint &position) {setVisible(true);- ensureWithinParent(position, false);+ KIS_SAFE_ASSERT_RECOVER_RETURN(parentWidget());+ const QPoint globalPos = parentWidget()->mapToGlobal(position);+ ensureWithinParent(globalPos, false);m_mirrorPos = QCursor::pos();}@@ -1058,22 +1076,31 @@ bool KisPopupPalette::onScreen()return isVisible();}-void KisPopupPalette::ensureWithinParent(const QPoint& position, bool useUpperLeft) {- if (isVisible() && parentWidget()) {- const qreal widgetMargin = -20.0;- const QRect fitRect = kisGrowRect(parentWidget()->rect(), widgetMargin);- const QPoint paletteCenterOffset(sizeHint().width() / 2, sizeHint().height() / 2);-- QRect paletteRect = rect();+void KisPopupPalette::ensureWithinParent(const QPoint& globalPos, bool useUpperLeft) {+ if (isVisible()) {+ const QSize paletteSize = geometry().size();+ const QPoint paletteCenterOffset(paletteSize.width() / 2, paletteSize.height() / 2);+ QPoint paletteGlobalPos = globalPos;if (!useUpperLeft) {- paletteRect.moveTo(position - paletteCenterOffset);- } else {- paletteRect.moveTopLeft(position);+ paletteGlobalPos -= paletteCenterOffset;+ }++ if (parentWidget()) {+ const qreal widgetMargin = -20.0;++ const QPoint paletteParentPos = parentWidget()->mapFromGlobal(paletteGlobalPos);+ QRect paletteParentRect(paletteParentPos, paletteSize);++ const QRect fitRect = kisGrowRect(parentWidget()->rect(), widgetMargin);+ paletteParentRect = kisEnsureInRect(paletteParentRect, fitRect);+ paletteGlobalPos = parentWidget()->mapToGlobal(paletteParentRect.topLeft());}- paletteRect = kisEnsureInRect(paletteRect, fitRect);- move(paletteRect.topLeft());++ const QPoint moveToPoint = this->windowHandle() ? paletteGlobalPos : parentWidget()->mapFromGlobal(paletteGlobalPos);++ move(moveToPoint);}}@@ -1089,9 +1116,6 @@ void KisPopupPalette::showEvent(QShowEvent *event)zoomCanvasSlider->setValue(m_coordinatesConverter->zoomInPercent()); // sync the zoom slider}- m_dockerHud->setVisible(m_dockerHudButton->isChecked());- m_bottomBarWidget->setVisible(m_bottomBarButton->isChecked());-QWidget::showEvent(event);}@@ -1109,6 +1133,14 @@ void KisPopupPalette::mouseReleaseEvent(QMouseEvent *event)QPointF point = event->localPos();event->accept();+ /**+ * We are in native and modal mode, so handle our closure ourselves+ */+ if (this->windowHandle() && !QRectF(rect()).contains(event->position())) {+ Q_EMIT finished();+ return;+ }+if (m_isRotatingCanvasIndicator) {update();}diff --git a/libs/ui/kis_popup_palette.h b/libs/ui/kis_popup_palette.hindex a20b406b8c..a59177d665 100644--- a/libs/ui/kis_popup_palette.h+++ b/libs/ui/kis_popup_palette.h@@ -67,7 +67,7 @@ public:void dismiss() override;bool onScreen() override;- void ensureWithinParent(const QPoint& position, bool useUpperLeft);+ void ensureWithinParent(const QPoint& globalPos, bool useUpperLeft);protected:void showEvent(QShowEvent *event) override;