Commit e0253e7d authored by Emmet O'Neill's avatar Emmet O'Neill
Browse files

Applied KisPopupWidgetInterface to tools hierarchy.

Replaced the QWidget-based `KisToolBase::popupWidget()` with one that's
based on KisPopupWidgetInterface, and implemented it where possible.

A companion class, KisPopupWidget, can be used as a simple wrapper
around any QWidget to turn it into a popup widget.
parent 4e101683
......@@ -23,6 +23,7 @@ class KoToolFactoryBase;
class KoToolSelection;
class KoToolBasePrivate;
class KoShapeControllerBase;
class KisPopupWidgetInterface;
class QAction;
class QKeyEvent;
......@@ -303,7 +304,7 @@ public:
* @return a widget with useful controls to be popped up on top of the canvas.
* Will not be called if `popupActionsMenu()` does not return null.
*/
virtual QWidget* popupWidget() {return nullptr;}
virtual KisPopupWidgetInterface* popupWidget() {return nullptr;}
/// Returns the canvas the tool is working on
KoCanvasBase *canvas() const;
......
......@@ -349,7 +349,7 @@ QMenu *KoToolProxy::popupActionsMenu()
return d->activeTool ? d->activeTool->popupActionsMenu() : 0;
}
QWidget* KoToolProxy::popupWidget()
KisPopupWidgetInterface* KoToolProxy::popupWidget()
{
return d->activeTool ? d->activeTool->popupWidget() : nullptr;
}
......
......@@ -32,6 +32,7 @@ class QTouchEvent;
class QPainter;
class QPointF;
class QMenu;
class KisPopupWidgetInterface;
/**
* Tool proxy object which allows an application to address the current tool.
......@@ -99,7 +100,7 @@ public:
QMenu* popupActionsMenu();
/// Forwarded to the current KoToolBase
QWidget* popupWidget();
KisPopupWidgetInterface* popupWidget();
/// Forwarded to the current KoToolBase
void deleteSelection();
......
#ifndef KISPOPUPWIDGETINTERFACE_H
#define KISPOPUPWIDGETINTERFACE_H
#include <QWidget>
#include <QGridLayout>
#include "kis_debug.h"
#include "kis_assert.h"
#include "KoCanvasBase.h"
#include "input/kis_input_manager.h"
/**
* @brief The PopupWidgetInterface class
*/
struct KisPopupWidgetInterface {
virtual ~KisPopupWidgetInterface() {}
virtual void popup(const QPoint& position) = 0;
};
/**
* @brief The PopupWidget class
*/
class KisPopupWidget : public QWidget, public 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 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_H
......@@ -97,9 +97,9 @@ void KisInputManager::removeTrackedCanvas(KisCanvas2 *canvas)
d->canvasSwitcher.removeCanvas(canvas);
}
void KisInputManager::registerPopupWidget(QWidget *popup)
void KisInputManager::registerPopupWidget(KisPopupWidgetInterface *popupWidget)
{
d->popupWidget = popup;
d->popupWidget = popupWidget;
}
void KisInputManager::toggleTabletLogger()
......@@ -340,21 +340,30 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
// Try closing any open popup widget and
// consume input if possible.
if (d->popupWidget) {
QEvent::Type type = event->type();
bool wasVisible = d->popupWidget->isVisible();
if (type == QEvent::MouseButtonPress
|| type == QEvent::MouseButtonDblClick
|| type == QEvent::TabletPress
|| type == QEvent::TouchBegin
|| type == QEvent::NativeGesture) {
d->popupWidget->setVisible(false);
d->popupWidget = nullptr;
if (wasVisible) {
return true;
//TEMP WORKAROUND TEMP WORKAROUND TEMP WORKAROUND//
QWidget* popup = dynamic_cast<QWidget*>(d->popupWidget);
//TEMP WORKAROUND TEMP WORKAROUND TEMP WORKAROUND//
if (popup) { //TEMP
QEvent::Type type = event->type();
bool wasVisible = popup->isVisible();
if (type == QEvent::MouseButtonPress
|| type == QEvent::MouseButtonDblClick
|| type == QEvent::TabletPress
|| type == QEvent::TouchBegin
|| type == QEvent::NativeGesture) {
popup->setVisible(false);
d->popupWidget = nullptr;
if (wasVisible) {
return true;
}
}
}
} //TEMP
}
......
......@@ -52,7 +52,7 @@ public:
void addTrackedCanvas(KisCanvas2 *canvas);
void removeTrackedCanvas(KisCanvas2 *canvas);
void registerPopupWidget(QWidget* popup);
void registerPopupWidget(KisPopupWidgetInterface *popupWidget);
void toggleTabletLogger();
......
......@@ -72,7 +72,7 @@ public:
QPointF previousPos;
bool buttonPressed = false;
QWidget* popupWidget;
KisPopupWidgetInterface* popupWidget;
void blockMouseEvents();
void allowMouseEvents();
......
......@@ -50,7 +50,7 @@ KisPopupWidgetAction::~KisPopupWidgetAction()
void KisPopupWidgetAction::begin(int, QEvent *event)
{
QMenu *popupMenu = inputManager()->toolProxy()->popupActionsMenu();
QWidget *popupWidget = inputManager()->toolProxy()->popupWidget();
KisPopupWidgetInterface *popupWidget = inputManager()->toolProxy()->popupWidget();
if (popupMenu) { // Handle popup menus...
m_requestedWithStylus = event && event->type() == QEvent::TabletPress;
......@@ -81,8 +81,7 @@ void KisPopupWidgetAction::begin(int, QEvent *event)
pos = inputManager()->canvas()->canvasWidget()->mapFromGlobal(QCursor::pos());
}
m_activePopup.reset(new PopupWidgetHolder(popupWidget, inputManager()));
m_activePopup->popup(pos);
inputManager()->registerPopupWidget(popupWidget);
popupWidget->popup(pos);
}
}
......@@ -16,60 +16,10 @@
#include "kis_global.h"
#include "kis_debug.h"
#include "kis_input_manager.h"
#include "kis_canvas2.h"
#include "KisPopupWidgetInterface.h"
class QMenu;
/**
* @brief The PopupWidgetInterface class
*/
struct PopupWidgetInterface {
virtual ~PopupWidgetInterface() {}
virtual void popup(const QPoint& position) = 0;
};
/**
* @brief The PopupWidgetHolder class
*/
class PopupWidgetHolder : public QObject, PopupWidgetInterface
{
public:
PopupWidgetHolder(QWidget* toPopUp, KisInputManager* inputManager)
: m_toPopUp(toPopUp)
, m_inputManager(inputManager)
{
m_toPopUp->setParent(m_inputManager->canvas()->canvasWidget());
}
~PopupWidgetHolder() {}
void popup(const QPoint& position) override {
m_toPopUp->setVisible(!m_toPopUp->isVisible());
m_inputManager->registerPopupWidget(m_toPopUp);
adjustPopupLayout(position);
}
private:
void adjustPopupLayout(const QPoint& postition) {
if (m_toPopUp->isVisible() && m_toPopUp->parentWidget()) {
const float widgetMargin = -20.0f;
const QRect fitRect = kisGrowRect(m_toPopUp->parentWidget()->rect(), widgetMargin);
const QPoint paletteCenterOffset(m_toPopUp->sizeHint().width() / 2, m_toPopUp->sizeHint().height() / 2);
QRect paletteRect = m_toPopUp->rect();
paletteRect.moveTo(postition - paletteCenterOffset);
paletteRect = kisEnsureInRect(paletteRect, fitRect);
m_toPopUp->move(paletteRect.topLeft());
}
}
QWidget* m_toPopUp;
KisInputManager* m_inputManager;
};
/**
* \brief Get the current tool's popup widget and display it.
......@@ -87,7 +37,6 @@ public:
void begin(int, QEvent *) override;
private:
QScopedPointer<PopupWidgetHolder> m_activePopup;
bool m_requestedWithStylus;
};
......
......@@ -785,6 +785,23 @@ void KisPopupPalette::tabletEvent(QTabletEvent *event) {
event->ignore();
}
void KisPopupPalette::popup(const QPoint &position) {
setVisible(!isVisible());
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());
}
}
void KisPopupPalette::showEvent(QShowEvent *event)
{
m_clicksEater->reset();
......
......@@ -16,7 +16,8 @@
#include "kactioncollection.h"
#include "kis_tool_button.h"
#include "KisHighlightedToolButton.h"
#include <KisColorSelectorInterface.h>
#include "KisColorSelectorInterface.h"
#include "KisPopupWidgetInterface.h"
class KisFavoriteResourceManager;
class QWidget;
......@@ -30,7 +31,7 @@ class KisVisualColorSelector;
class KisAcyclicSignalConnector;
class KisMouseClickEater;
class KisPopupPalette : public QWidget
class KisPopupPalette : public QWidget, public KisPopupWidgetInterface
{
Q_OBJECT
......@@ -55,8 +56,9 @@ public:
void tabletEvent(QTabletEvent *event) override;
protected:
void popup(const QPoint& position) override;
protected:
void showEvent(QShowEvent *event) override;
void paintEvent(QPaintEvent*) override;
void resizeEvent(QResizeEvent*) override {}
......@@ -87,7 +89,6 @@ private:
int numSlots();
private:
int m_hoveredPreset {0};
int m_hoveredColor {0};
int m_selectedColor {0};
......@@ -157,7 +158,6 @@ private Q_SLOTS:
void slotZoomSliderPressed();
void slotZoomSliderReleased();
};
#endif // KIS_POPUP_PALETTE_H
......@@ -62,6 +62,7 @@
#include <kis_action_manager.h>
#include <kis_action.h>
#include "strokes/kis_color_sampler_stroke_strategy.h"
#include "kis_popup_palette.h"
KisToolPaint::KisToolPaint(KoCanvasBase *canvas, const QCursor &cursor)
......@@ -459,15 +460,16 @@ void KisToolPaint::mouseMoveEvent(KoPointerEvent *event)
}
}
QWidget *KisToolPaint::popupWidget()
KisPopupWidgetInterface *KisToolPaint::popupWidget()
{
KisCanvas2 *kisCanvas = dynamic_cast<KisCanvas2*>(canvas());
if (kisCanvas) {
return (QWidget*)kisCanvas->popupPalette();
} else {
if (!kisCanvas) {
return nullptr;
}
KisPopupWidgetInterface* popupWidget = kisCanvas->popupPalette();
return popupWidget;
}
void KisToolPaint::mouseReleaseEvent(KoPointerEvent *event)
......
......@@ -44,7 +44,7 @@ public:
void mouseReleaseEvent(KoPointerEvent *event) override;
void mouseMoveEvent(KoPointerEvent *event) override;
QWidget* popupWidget() override;
KisPopupWidgetInterface* popupWidget() override;
protected:
......
......@@ -23,6 +23,7 @@
#include <KoColor.h>
#include <KoShapePaintingContext.h>
#include <KoViewConverter.h>
#include <KisPopupWidgetInterface.h>
#include <QAction>
#include <QDebug>
......@@ -429,10 +430,10 @@ QList<QPointer<QWidget>> KarbonCalligraphyTool::createOptionWidgets()
return widgets;
}
QWidget *KarbonCalligraphyTool::popupWidget()
KisPopupWidgetInterface *KarbonCalligraphyTool::popupWidget()
{
/**** HACK PROOF OF CONCEPT, DONT WORRY 'BOUT IT! ****/
return new KarbonCalligraphyOptionWidget();
return new KisPopupWidget(new KarbonCalligraphyOptionWidget(), canvas());
}
void KarbonCalligraphyTool::setStrokeWidth(double width)
......
......@@ -32,7 +32,7 @@ public:
QList<QPointer<QWidget>> createOptionWidgets() override;
QWidget *popupWidget() override;
KisPopupWidgetInterface *popupWidget() override;
void activate(const QSet<KoShape *> &shapes) override;
void deactivate() override;
......
......@@ -111,9 +111,9 @@ void SvgTextTool::deactivate()
canvas()->updateCanvas(updateRect);
}
QWidget *SvgTextTool::popupWidget()
KisPopupWidgetInterface *SvgTextTool::popupWidget()
{
return m_editor;
return nullptr;
}
QWidget *SvgTextTool::createOptionWidget()
......
......@@ -43,7 +43,7 @@ public:
/// reimplemented from KoToolBase
void deactivate() override;
QWidget* popupWidget() override;
KisPopupWidgetInterface* popupWidget() override;
protected:
/// reimplemented from KoToolBase
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment