From 2875ac951d21bbf824f3b714b1078faf4ae77878 Mon Sep 17 00:00:00 2001 From: Yurii Puchkov Date: Tue, 20 Apr 2021 18:35:08 +0300 Subject: [PATCH] fix - Crash on Move Tool with an active Text editor (#1569) --- src/tools/rectangle/rectangletool.cpp | 1 - src/tools/text/texttool.cpp | 7 ++- src/widgets/capture/capturetoolbutton.cpp | 15 ++++++- src/widgets/capture/capturetoolbutton.h | 1 + src/widgets/capture/capturewidget.cpp | 54 +++++++++++++++-------- 5 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/tools/rectangle/rectangletool.cpp b/src/tools/rectangle/rectangletool.cpp index 08a01059..4dba3369 100644 --- a/src/tools/rectangle/rectangletool.cpp +++ b/src/tools/rectangle/rectangletool.cpp @@ -54,7 +54,6 @@ void RectangleTool::process(QPainter& painter, const QPixmap& pixmap) if (thickness() == 0) { painter.drawRect(QRect(points().first, points().second)); } else { - painter.setRenderHint(QPainter::Antialiasing); QPainterPath path; int offset = thickness() <= 1 ? 1 : static_cast(round(thickness() / 2 + 0.5)); diff --git a/src/tools/text/texttool.cpp b/src/tools/text/texttool.cpp index 1e760d9f..62548209 100644 --- a/src/tools/text/texttool.cpp +++ b/src/tools/text/texttool.cpp @@ -83,12 +83,15 @@ QWidget* TextTool::widget() void TextTool::closeEditor() { if (!m_widget.isNull()) { - disconnect( - m_widget, &TextWidget::textUpdated, this, &TextTool::updateText); m_widget->close(); delete m_widget; m_widget = nullptr; } + if (!m_confW.isNull()) { + m_confW->close(); + delete m_confW; + m_confW = nullptr; + } } QWidget* TextTool::configurationWidget() diff --git a/src/widgets/capture/capturetoolbutton.cpp b/src/widgets/capture/capturetoolbutton.cpp index c5967462..6db5ffe5 100644 --- a/src/widgets/capture/capturetoolbutton.cpp +++ b/src/widgets/capture/capturetoolbutton.cpp @@ -7,7 +7,6 @@ #include "src/utils/colorutils.h" #include "src/utils/confighandler.h" #include "src/utils/globalvalues.h" -#include "src/widgets/capture/capturewidget.h" #include #include #include @@ -20,6 +19,8 @@ CaptureToolButton::CaptureToolButton(const ButtonType t, QWidget* parent) : CaptureButton(parent) , m_buttonType(t) + , m_tool(nullptr) + , m_emergeAnimation(nullptr) { initButton(); if (t == TYPE_SELECTIONINDICATOR) { @@ -30,6 +31,18 @@ CaptureToolButton::CaptureToolButton(const ButtonType t, QWidget* parent) } } +CaptureToolButton::~CaptureToolButton() +{ + if (m_tool) { + delete m_tool; + m_tool = nullptr; + } + if (m_emergeAnimation) { + delete m_emergeAnimation; + m_emergeAnimation = nullptr; + } +} + void CaptureToolButton::initButton() { m_tool = ToolFactory().CreateTool(m_buttonType, this); diff --git a/src/widgets/capture/capturetoolbutton.h b/src/widgets/capture/capturetoolbutton.h index 32a6260f..7ae676be 100644 --- a/src/widgets/capture/capturetoolbutton.h +++ b/src/widgets/capture/capturetoolbutton.h @@ -46,6 +46,7 @@ public: Q_ENUM(ButtonType) explicit CaptureToolButton(const ButtonType, QWidget* parent = nullptr); + ~CaptureToolButton(); static QVector getIterableButtonTypes(); static int getPriorityByButton(CaptureToolButton::ButtonType); diff --git a/src/widgets/capture/capturewidget.cpp b/src/widgets/capture/capturewidget.cpp index acc5686e..53f8b256 100644 --- a/src/widgets/capture/capturewidget.cpp +++ b/src/widgets/capture/capturewidget.cpp @@ -231,7 +231,7 @@ void CaptureWidget::updateButtons() new QShortcut(QKeySequence(shortcut), this); CaptureWidget* captureWidget = this; connect(key, &QShortcut::activated, this, [=]() { - emit captureWidget->setState(b); + captureWidget->setState(b); }); } break; @@ -255,6 +255,7 @@ QPixmap CaptureWidget::pixmap() if (m_toolWidget && m_activeTool) { p = m_context.selectedScreenshotArea().copy(); QPainter painter(&p); + painter.setRenderHint(QPainter::Antialiasing); m_activeTool->process(painter, p); } else { p = m_context.selectedScreenshotArea(); @@ -274,7 +275,9 @@ bool CaptureWidget::commitCurrentTool() m_activeTool = nullptr; } if (m_toolWidget) { - m_toolWidget->deleteLater(); + m_toolWidget->close(); + delete m_toolWidget; + m_toolWidget = nullptr; return true; } } @@ -294,7 +297,8 @@ void CaptureWidget::deleteToolWidgetOrClose() m_panel->hide(); } else if (m_toolWidget) { // delete toolWidget if exists - m_toolWidget->deleteLater(); + m_toolWidget->close(); + delete m_toolWidget; m_toolWidget = nullptr; } else { // close CaptureWidget @@ -323,8 +327,8 @@ void CaptureWidget::paintEvent(QPaintEvent* paintEvent) painter.save(); m_activeTool->process(painter, m_context.screenshot); painter.restore(); - } else if (m_activeButton && m_activeButton->tool()->showMousePreview() && - m_previewEnabled) { + } else if (m_previewEnabled && m_activeButton && m_activeButton->tool() && + m_activeButton->tool()->showMousePreview()) { painter.save(); m_activeButton->tool()->paintMousePreview(painter, m_context); painter.restore(); @@ -357,7 +361,8 @@ void CaptureWidget::showColorPicker(const QPoint& pos) bool CaptureWidget::startDrawObjectTool(const QPoint& pos) { - if (m_activeButton && m_activeButton->tool()->nameID() != ToolType::MOVE) { + if (m_activeButton && m_activeButton->tool() && + m_activeButton->tool()->nameID() != ToolType::MOVE) { if (commitCurrentTool()) { return false; } @@ -477,7 +482,7 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent* e) drawToolsData(false); } else if (m_mouseIsClicked && (!m_activeButton || - (m_activeButton && + (m_activeButton && m_activeButton->tool() && m_activeButton->tool()->nameID() == ToolType::MOVE))) { // Drawing, moving, or stretching a selection m_selection->setVisible(true); @@ -568,7 +573,8 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent* e) m_buttonHandler->show(); } } - } else if (m_activeButton && m_activeButton->tool()->showMousePreview()) { + } else if (m_activeButton && m_activeButton->tool() && + m_activeButton->tool()->showMousePreview()) { update(); } else { if (!m_selection->isVisible()) { @@ -735,7 +741,8 @@ void CaptureWidget::wheelEvent(QWheelEvent* e) int offset = m_notifierBox->width() / 4; m_notifierBox->move(mapFromGlobal(topLeft) + QPoint(offset, offset)); m_notifierBox->showMessage(QString::number(m_context.thickness)); - if (m_activeButton && m_activeButton->tool()->showMousePreview()) { + if (m_activeButton && m_activeButton->tool() && + m_activeButton->tool()->showMousePreview()) { update(); } emit thicknessChanged(m_context.thickness); @@ -916,8 +923,11 @@ void CaptureWidget::setState(CaptureToolButton* b) if (!b) { return; } + if (m_toolWidget) { - m_toolWidget->deleteLater(); + m_toolWidget->close(); + delete m_toolWidget; + m_toolWidget = nullptr; if (m_activeTool != nullptr) { if (m_activeTool->isValid()) { pushToolToStack(); @@ -927,6 +937,7 @@ void CaptureWidget::setState(CaptureToolButton* b) if (m_activeButton != b) { processTool(b->tool()); } + // Only close activated from button if (b->tool()->closeOnButtonPressed()) { close(); @@ -1015,7 +1026,9 @@ void CaptureWidget::handleButtonSignal(CaptureTool::Request r) break; } if (m_toolWidget) { - m_toolWidget->deleteLater(); + m_toolWidget->close(); + delete m_toolWidget; + m_toolWidget = nullptr; } m_toolWidget = m_activeTool->widget(); if (m_toolWidget) { @@ -1073,12 +1086,14 @@ void CaptureWidget::setDrawColor(const QColor& c) if (m_context.color.isValid()) { ConfigHandler().setDrawColor(m_context.color); emit colorChanged(c); - } - auto toolItem = activeToolObject(); - if (toolItem) { - // Change color - emit toolItem->colorChanged(c); - drawToolsData(false, true); + + // change color for the active tool + auto toolItem = activeToolObject(); + if (toolItem) { + // Change color + emit toolItem->colorChanged(c); + drawToolsData(false, true); + } } } @@ -1278,7 +1293,7 @@ void CaptureWidget::updateCursor() } else { setCursor(Qt::ArrowCursor); } - } else if (m_activeButton && + } else if (m_activeButton && m_activeButton->tool() && m_activeButton->tool()->nameID() == ToolType::MOVE) { setCursor(Qt::OpenHandCursor); } else if (!m_activeButton) { @@ -1358,6 +1373,7 @@ void CaptureWidget::drawToolsData(bool updateLayersPanel, bool drawSelection) { QPixmap pixmapItem = m_context.origScreenshot.copy(); QPainter painter(&pixmapItem); + painter.setRenderHint(QPainter::Antialiasing); int index = 0; m_context.circleCount = 1; for (auto toolItem : m_captureToolObjects.captureToolObjects()) { @@ -1432,6 +1448,7 @@ void CaptureWidget::copyScreenshot() m_captureDone = true; if (m_activeTool != nullptr) { QPainter painter(&m_context.screenshot); + painter.setRenderHint(QPainter::Antialiasing); m_activeTool->process(painter, m_context.screenshot); } @@ -1447,6 +1464,7 @@ void CaptureWidget::saveScreenshot() m_captureDone = true; if (m_activeTool != nullptr) { QPainter painter(&m_context.screenshot); + painter.setRenderHint(QPainter::Antialiasing); m_activeTool->process(painter, m_context.screenshot); } hide();