Refactor CaptureTool::pressed and derivatives

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>
This commit is contained in:
Haris Gušić
2021-10-06 18:09:27 +02:00
parent 151f6e35b4
commit d39737fd46
53 changed files with 70 additions and 96 deletions

View File

@@ -39,7 +39,7 @@ public:
ExportTask tasks() const;
void addTask(ExportTask task);
void addSaveTask(const QString& path);
void addSaveTask(const QString& path = QString());
void exportCapture(const QPixmap& p);
private:

View File

@@ -8,7 +8,6 @@
#include <QStyle>
#if defined(Q_OS_MACOS)
#include "src/widgets/capture/capturewidget.h"
#include <QApplication>
#include <QWidget>
#endif
@@ -49,19 +48,8 @@ CaptureTool* AcceptTool::copy(QObject* parent)
return new AcceptTool(parent);
}
void AcceptTool::pressed(const CaptureContext&)
void AcceptTool::pressed(CaptureContext&)
{
#if defined(Q_OS_MACOS)
for (QWidget* widget : qApp->topLevelWidgets()) {
QString className(widget->metaObject()->className());
if (0 ==
className.compare(CaptureWidget::staticMetaObject.className())) {
widget->showNormal();
widget->hide();
break;
}
}
#endif
emit requestAction(REQ_CAPTURE_DONE_OK);
emit requestAction(REQ_CLOSE_GUI);
}

View File

@@ -23,5 +23,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -153,7 +153,7 @@ void ArrowTool::process(QPainter& painter, const QPixmap& pixmap)
painter.fillPath(m_arrowPath, QBrush(color()));
}
void ArrowTool::pressed(const CaptureContext& context)
void ArrowTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -26,7 +26,7 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
private:
QPainterPath m_arrowPath;

View File

@@ -29,6 +29,7 @@ struct CaptureContext
int thickness;
// Mode of the capture widget
bool fullscreen;
CaptureRequest* request;
QPixmap selectedScreenshotArea() const;
};

View File

@@ -190,7 +190,7 @@ public slots:
// Called when the tool is activated.
virtual void drawStart(const CaptureContext& context) = 0;
// Called right after pressign the button which activates the tool.
virtual void pressed(const CaptureContext& context) = 0;
virtual void pressed(CaptureContext& context) = 0;
// Called when the color is changed in the editor.
virtual void onColorChanged(const QColor& c) = 0;
// Called when the thickness of the tool is updated in the editor.

View File

@@ -44,7 +44,7 @@ void CircleTool::process(QPainter& painter, const QPixmap& pixmap)
painter.drawEllipse(QRect(points().first, points().second));
}
void CircleTool::pressed(const CaptureContext& context)
void CircleTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -22,5 +22,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -166,7 +166,7 @@ void CircleCountTool::drawStart(const CaptureContext& context)
m_valid = true;
}
void CircleCountTool::pressed(const CaptureContext& context)
void CircleCountTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -31,7 +31,7 @@ protected:
public slots:
void drawStart(const CaptureContext& context) override;
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
private:
QString m_tempString;

View File

@@ -39,9 +39,9 @@ CaptureTool* CopyTool::copy(QObject* parent)
return new CopyTool(parent);
}
void CopyTool::pressed(const CaptureContext& context)
void CopyTool::pressed(CaptureContext& context)
{
context.request->addTask(CaptureRequest::COPY_TASK);
emit requestAction(REQ_CAPTURE_DONE_OK);
ScreenshotSaver().saveToClipboard(context.selectedScreenshotArea());
emit requestAction(REQ_CLOSE_GUI);
}

View File

@@ -23,5 +23,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -38,7 +38,7 @@ CaptureTool* ExitTool::copy(QObject* parent)
return new ExitTool(parent);
}
void ExitTool::pressed(const CaptureContext& context)
void ExitTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
emit requestAction(REQ_CLOSE_GUI);

View File

@@ -24,5 +24,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -44,7 +44,7 @@ CaptureTool* ImgurUploaderTool::copy(QObject* parent)
return new ImgurUploaderTool(parent);
}
void ImgurUploaderTool::pressed(const CaptureContext& context)
void ImgurUploaderTool::pressed(CaptureContext& context)
{
capture = context.selectedScreenshotArea();
emit requestAction(REQ_CAPTURE_DONE_OK);

View File

@@ -25,7 +25,7 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
private:
QPixmap capture;

View File

@@ -82,7 +82,7 @@ void InvertTool::paintMousePreview(QPainter& painter,
Q_UNUSED(painter)
}
void InvertTool::pressed(const CaptureContext& context)
void InvertTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -26,5 +26,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -43,7 +43,7 @@ CaptureTool* AppLauncher::copy(QObject* parent)
return new AppLauncher(parent);
}
void AppLauncher::pressed(const CaptureContext& context)
void AppLauncher::pressed(CaptureContext& context)
{
capture = context.selectedScreenshotArea();
emit requestAction(REQ_CAPTURE_DONE_OK);

View File

@@ -25,7 +25,7 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
private:
QPixmap capture;

View File

@@ -46,7 +46,7 @@ void LineTool::process(QPainter& painter, const QPixmap& pixmap)
painter.drawLine(points().first, points().second);
}
void LineTool::pressed(const CaptureContext& context)
void LineTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -22,5 +22,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -84,7 +84,7 @@ void MarkerTool::drawStart(const CaptureContext& context)
onThicknessChanged(context.thickness + PADDING_VALUE);
}
void MarkerTool::pressed(const CaptureContext& context)
void MarkerTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -26,5 +26,5 @@ protected:
public slots:
void drawStart(const CaptureContext& context) override;
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -38,7 +38,7 @@ CaptureTool* MoveTool::copy(QObject* parent)
return new MoveTool(parent);
}
void MoveTool::pressed(const CaptureContext& context)
void MoveTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -22,5 +22,5 @@ public:
CaptureTool* copy(QObject* parent = nullptr) override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -58,7 +58,7 @@ void PencilTool::drawStart(const CaptureContext& context)
m_pathArea.setBottomRight(context.mousePos);
}
void PencilTool::pressed(const CaptureContext& context)
void PencilTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -26,5 +26,5 @@ protected:
public slots:
void drawStart(const CaptureContext& context) override;
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -70,7 +70,7 @@ CaptureTool* PinTool::copy(QObject* parent)
return new PinTool(parent);
}
void PinTool::pressed(const CaptureContext& context)
void PinTool::pressed(CaptureContext& context)
{
emit requestAction(REQ_CAPTURE_DONE_OK);
m_geometry = context.selection;

View File

@@ -25,7 +25,7 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
private:
QRect m_geometry;

View File

@@ -105,7 +105,7 @@ void PixelateTool::paintMousePreview(QPainter& painter,
Q_UNUSED(painter)
}
void PixelateTool::pressed(const CaptureContext& context)
void PixelateTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -26,5 +26,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -73,7 +73,7 @@ void RectangleTool::drawStart(const CaptureContext& context)
onThicknessChanged(context.thickness);
}
void RectangleTool::pressed(const CaptureContext& context)
void RectangleTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -23,5 +23,5 @@ protected:
public slots:
void drawStart(const CaptureContext& context) override;
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -38,7 +38,7 @@ CaptureTool* RedoTool::copy(QObject* parent)
return new RedoTool(parent);
}
void RedoTool::pressed(const CaptureContext& context)
void RedoTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
emit requestAction(REQ_REDO_MODIFICATION);

View File

@@ -23,5 +23,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -45,33 +45,9 @@ CaptureTool* SaveTool::copy(QObject* parent)
return new SaveTool(parent);
}
void SaveTool::pressed(const CaptureContext& context)
void SaveTool::pressed(CaptureContext& context)
{
#if defined(Q_OS_MACOS)
for (QWidget* widget : qApp->topLevelWidgets()) {
QString className(widget->metaObject()->className());
if (0 ==
className.compare(CaptureWidget::staticMetaObject.className())) {
widget->showNormal();
widget->hide();
break;
}
}
#endif
if (context.savePath.isEmpty()) {
emit requestAction(REQ_HIDE_GUI);
qApp->processEvents();
bool ok = ScreenshotSaver().saveToFilesystemGUI(
context.selectedScreenshotArea());
if (ok) {
emit requestAction(REQ_CAPTURE_DONE_OK);
}
} else {
bool ok = ScreenshotSaver().saveToFilesystem(
context.selectedScreenshotArea(), context.savePath);
if (ok) {
emit requestAction(REQ_CAPTURE_DONE_OK);
}
}
context.request->addSaveTask();
emit requestAction(REQ_CAPTURE_DONE_OK);
emit requestAction(REQ_CLOSE_GUI);
}

View File

@@ -23,5 +23,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -50,7 +50,7 @@ void SelectionTool::process(QPainter& painter, const QPixmap& pixmap)
painter.drawRect(QRect(points().first, points().second));
}
void SelectionTool::pressed(const CaptureContext& context)
void SelectionTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -24,5 +24,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -52,7 +52,7 @@ CaptureTool* SizeDecreaseTool::copy(QObject* parent)
return new SizeDecreaseTool(parent);
}
void SizeDecreaseTool::pressed(const CaptureContext& context)
void SizeDecreaseTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
emit requestAction(REQ_DECREASE_TOOL_SIZE);

View File

@@ -37,5 +37,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -52,7 +52,7 @@ CaptureTool* SizeIncreaseTool::copy(QObject* parent)
return new SizeIncreaseTool(parent);
}
void SizeIncreaseTool::pressed(const CaptureContext& context)
void SizeIncreaseTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
emit requestAction(REQ_INCREASE_TOOL_SIZE);

View File

@@ -37,5 +37,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -38,7 +38,7 @@ CaptureTool* SizeIndicatorTool::copy(QObject* parent)
return new SizeIndicatorTool(parent);
}
void SizeIndicatorTool::pressed(const CaptureContext& context)
void SizeIndicatorTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -23,5 +23,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -252,7 +252,7 @@ void TextTool::drawStart(const CaptureContext& context)
emit requestAction(REQ_ADD_CHILD_WIDGET);
}
void TextTool::pressed(const CaptureContext& context)
void TextTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
}

View File

@@ -50,7 +50,7 @@ public slots:
void drawEnd(const QPoint& p) override;
void drawMove(const QPoint& p) override;
void drawStart(const CaptureContext& context) override;
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
void onColorChanged(const QColor& c) override;
void onThicknessChanged(int th) override;
virtual int thickness() const override { return m_size; };

View File

@@ -38,7 +38,7 @@ CaptureTool* UndoTool::copy(QObject* parent)
return new UndoTool(parent);
}
void UndoTool::pressed(const CaptureContext& context)
void UndoTool::pressed(CaptureContext& context)
{
Q_UNUSED(context)
emit requestAction(REQ_UNDO_MODIFICATION);

View File

@@ -23,5 +23,5 @@ protected:
CaptureTool::Type type() const override;
public slots:
void pressed(const CaptureContext& context) override;
void pressed(CaptureContext& context) override;
};

View File

@@ -64,7 +64,6 @@ CaptureWidget::CaptureWidget(uint id,
, m_toolWidget(nullptr)
, m_colorPicker(nullptr)
, m_id(id)
, m_request(&*Controller::getInstance()->requests().find(id))
, m_lastMouseWheel(0)
, m_updateNotificationWidget(nullptr)
, m_activeToolIsMoved(false)
@@ -92,7 +91,7 @@ CaptureWidget::CaptureWidget(uint id,
m_uiColor = m_config.uiColor();
m_contrastUiColor = m_config.contrastUiColor();
setMouseTracking(true);
initContext(savePath, fullScreen);
initContext(fullScreen);
initSelection();
initShortcuts(); // must be called after initSelection
#if (defined(Q_OS_WIN) || defined(Q_OS_MACOS))
@@ -234,6 +233,17 @@ CaptureWidget::CaptureWidget(uint id,
CaptureWidget::~CaptureWidget()
{
#if defined(Q_OS_MACOS)
for (QWidget* widget : qApp->topLevelWidgets()) {
QString className(widget->metaObject()->className());
if (0 ==
className.compare(CaptureWidget::staticMetaObject.className())) {
widget->showNormal();
widget->hide();
break;
}
}
#endif
if (m_captureDone) {
emit captureTaken(m_id, this->pixmap(), m_context.selection);
} else {
@@ -245,7 +255,7 @@ void CaptureWidget::initButtons()
{
auto allButtonTypes = CaptureToolButton::getIterableButtonTypes();
auto visibleButtonTypes = m_config.buttons();
if (m_request->tasks() == CaptureRequest::NO_TASK) {
if (m_context.request->tasks() == CaptureRequest::NO_TASK) {
allButtonTypes.removeOne(CaptureTool::TYPE_ACCEPT);
visibleButtonTypes.removeOne(CaptureTool::TYPE_ACCEPT);
}
@@ -792,14 +802,14 @@ void CaptureWidget::moveEvent(QMoveEvent* e)
m_context.widgetOffset = mapToGlobal(QPoint(0, 0));
}
void CaptureWidget::initContext(const QString& savePath, bool fullscreen)
void CaptureWidget::initContext(bool fullscreen)
{
m_context.color = m_config.drawColor();
m_context.savePath = savePath;
m_context.widgetOffset = mapToGlobal(QPoint(0, 0));
m_context.mousePos = mapFromGlobal(QCursor::pos());
m_context.thickness = m_config.drawThickness();
m_context.fullscreen = fullscreen;
m_context.request = &*Controller::getInstance()->requests().find(m_id);
}
void CaptureWidget::initPanel()

View File

@@ -101,7 +101,7 @@ private:
void showColorPicker(const QPoint& pos);
bool startDrawObjectTool(const QPoint& pos);
QPointer<CaptureTool> activeToolObject();
void initContext(const QString& savePath, bool fullscreen);
void initContext(bool fullscreen);
void initPanel();
void initSelection();
void initShortcuts();
@@ -174,7 +174,6 @@ private:
SelectionWidget::SideType m_mouseOverHandle;
uint m_id;
CaptureRequest* m_request;
CaptureToolObjects m_captureToolObjects;
CaptureToolObjects m_captureToolObjectsBackup;