diff --git a/src/config/generalconf.cpp b/src/config/generalconf.cpp index 538cdb3e..0cda8c96 100644 --- a/src/config/generalconf.cpp +++ b/src/config/generalconf.cpp @@ -38,6 +38,8 @@ GeneralConf::GeneralConf(QWidget* parent) initUploadHistoryMaxSize(); initUndoLimit(); + m_layout->addStretch(); + // this has to be at the end initConfigButtons(); updateComponents(); @@ -267,7 +269,6 @@ void GeneralConf::initHistoryConfirmationToDelete() void GeneralConf::initConfigButtons() { QHBoxLayout* buttonLayout = new QHBoxLayout(); - m_layout->addStretch(); QGroupBox* box = new QGroupBox(tr("Configuration File")); box->setFlat(true); box->setLayout(buttonLayout); @@ -364,7 +365,6 @@ void GeneralConf::initSaveAfterCopy() QGroupBox* box = new QGroupBox(tr("Save Path")); box->setFlat(true); m_layout->addWidget(box); - m_layout->addStretch(); QVBoxLayout* vboxLayout = new QVBoxLayout(); box->setLayout(vboxLayout); @@ -378,9 +378,6 @@ void GeneralConf::initSaveAfterCopy() } m_savePath = new QLineEdit(path, this); m_savePath->setDisabled(true); - // #if QT_DEPRECATED_SINCE(5, 13) - // QT_DEPRECATED_X("Use QPalette::windowText() instead") - // inline const QBrush &foreground() const { return windowText(); } QString foreground = this->palette().windowText().color().name(); m_savePath->setStyleSheet(QStringLiteral("color: %1").arg(foreground)); pathLayout->addWidget(m_savePath); @@ -414,7 +411,6 @@ void GeneralConf::initUploadHistoryMaxSize() QGroupBox* box = new QGroupBox(tr("Latest Uploads Max Size")); box->setFlat(true); m_layout->addWidget(box); - m_layout->addStretch(); QVBoxLayout* vboxLayout = new QVBoxLayout(); box->setLayout(vboxLayout); @@ -422,7 +418,7 @@ void GeneralConf::initUploadHistoryMaxSize() int max = ConfigHandler().uploadHistoryMaxSizeValue(); m_uploadHistoryMaxSize = new QSpinBox(this); - m_uploadHistoryMaxSize->setMaximum(1000); + m_uploadHistoryMaxSize->setMaximum(50); m_uploadHistoryMaxSize->setValue(max); QString foreground = this->palette().windowText().color().name(); m_uploadHistoryMaxSize->setStyleSheet( @@ -445,7 +441,6 @@ void GeneralConf::initUndoLimit() QGroupBox* box = new QGroupBox(tr("Undo limit")); box->setFlat(true); m_layout->addWidget(box); - m_layout->addStretch(); QVBoxLayout* vboxLayout = new QVBoxLayout(); box->setLayout(vboxLayout); diff --git a/src/tools/abstracttwopointtool.cpp b/src/tools/abstracttwopointtool.cpp index a1341441..5beb9879 100644 --- a/src/tools/abstracttwopointtool.cpp +++ b/src/tools/abstracttwopointtool.cpp @@ -6,8 +6,6 @@ #include #include -#define PADDING_VALUE_DEFAULT 2 - namespace { const double ADJ_UNIT = std::atan(1.0); @@ -97,8 +95,7 @@ void AbstractTwoPointTool::thicknessChanged(int th) void AbstractTwoPointTool::paintMousePreview(QPainter& painter, const CaptureContext& context) { - painter.setPen( - QPen(context.color, context.thickness + PADDING_VALUE_DEFAULT)); + painter.setPen(QPen(context.color, context.thickness)); painter.drawLine(context.mousePos, context.mousePos); } @@ -107,7 +104,7 @@ void AbstractTwoPointTool::drawStart(const CaptureContext& context) colorChanged(context.color); m_points.first = context.mousePos; m_points.second = context.mousePos; - thicknessChanged(context.thickness + PADDING_VALUE_DEFAULT); + thicknessChanged(context.thickness); } QPoint AbstractTwoPointTool::adjustedVector(QPoint v) const diff --git a/src/tools/arrow/arrowtool.cpp b/src/tools/arrow/arrowtool.cpp index 6b63fb25..85ef5f79 100644 --- a/src/tools/arrow/arrowtool.cpp +++ b/src/tools/arrow/arrowtool.cpp @@ -5,9 +5,6 @@ #include namespace { - -#define PADDING_VALUE 2 - const int ArrowWidth = 10; const int ArrowHeight = 18; diff --git a/src/tools/capturecontext.h b/src/tools/capturecontext.h index 93c79853..c5e27dc0 100644 --- a/src/tools/capturecontext.h +++ b/src/tools/capturecontext.h @@ -16,19 +16,16 @@ struct CaptureContext QPixmap origScreenshot; // Selection area QRect selection; - // Widget dimensions - QRect widgetDimensions; // Selected tool color QColor color; // Path where the content has to be saved QString savePath; - // Ofset of the capture widget based on the system's screen (top-left) + // Offset of the capture widget based on the system's screen (top-left) QPoint widgetOffset; // Mouse position inside the widget QPoint mousePos; // Value of the desired thickness int thickness; - int circleCount; // Mode of the capture widget bool fullscreen; diff --git a/src/tools/circle/circletool.cpp b/src/tools/circle/circletool.cpp index 744fc0dc..5bcfc364 100644 --- a/src/tools/circle/circletool.cpp +++ b/src/tools/circle/circletool.cpp @@ -4,10 +4,6 @@ #include "circletool.h" #include -namespace { -#define PADDING_VALUE 2 -} - CircleTool::CircleTool(QObject* parent) : AbstractTwoPointTool(parent) { diff --git a/src/tools/circlecount/circlecounttool.cpp b/src/tools/circlecount/circlecounttool.cpp index 108f5181..39113966 100644 --- a/src/tools/circlecount/circlecounttool.cpp +++ b/src/tools/circlecount/circlecounttool.cpp @@ -139,7 +139,6 @@ void CircleCountTool::paintMousePreview(QPainter& painter, void CircleCountTool::drawStart(const CaptureContext& context) { AbstractTwoPointTool::drawStart(context); - setCount(context.circleCount); } void CircleCountTool::pressed(const CaptureContext& context) diff --git a/src/tools/line/linetool.cpp b/src/tools/line/linetool.cpp index dc553273..bb1f8e7f 100644 --- a/src/tools/line/linetool.cpp +++ b/src/tools/line/linetool.cpp @@ -4,12 +4,6 @@ #include "linetool.h" #include -namespace { - -#define PADDING_VALUE 2 - -} - LineTool::LineTool(QObject* parent) : AbstractTwoPointTool(parent) { diff --git a/src/tools/pencil/penciltool.cpp b/src/tools/pencil/penciltool.cpp index 078d6134..cb4d0e6e 100644 --- a/src/tools/pencil/penciltool.cpp +++ b/src/tools/pencil/penciltool.cpp @@ -4,8 +4,6 @@ #include "penciltool.h" #include -#define PADDING_VALUE 2 - PencilTool::PencilTool(QObject* parent) : AbstractPathTool(parent) {} @@ -54,7 +52,7 @@ void PencilTool::paintMousePreview(QPainter& painter, void PencilTool::drawStart(const CaptureContext& context) { m_color = context.color; - thicknessChanged(context.thickness + PADDING_VALUE); + thicknessChanged(context.thickness); m_points.append(context.mousePos); m_pathArea.setTopLeft(context.mousePos); m_pathArea.setBottomRight(context.mousePos); diff --git a/src/tools/rectangle/rectangletool.cpp b/src/tools/rectangle/rectangletool.cpp index 4dba3369..40531bd8 100644 --- a/src/tools/rectangle/rectangletool.cpp +++ b/src/tools/rectangle/rectangletool.cpp @@ -6,10 +6,6 @@ #include #include -namespace { -#define PADDING_VALUE 2 -} - RectangleTool::RectangleTool(QObject* parent) : AbstractTwoPointTool(parent) { diff --git a/src/tools/selection/selectiontool.cpp b/src/tools/selection/selectiontool.cpp index 4468b1d9..a8da6891 100644 --- a/src/tools/selection/selectiontool.cpp +++ b/src/tools/selection/selectiontool.cpp @@ -4,10 +4,6 @@ #include "selectiontool.h" #include -namespace { -#define PADDING_VALUE 2 -} - SelectionTool::SelectionTool(QObject* parent) : AbstractTwoPointTool(parent) { diff --git a/src/tools/text/textconfig.cpp b/src/tools/text/textconfig.cpp index e9bc1ae2..fc7e4525 100644 --- a/src/tools/text/textconfig.cpp +++ b/src/tools/text/textconfig.cpp @@ -3,12 +3,12 @@ #include "textconfig.h" #include "src/utils/colorutils.h" +#include "src/utils/confighandler.h" #include "src/utils/pathinfo.h" #include #include #include #include -#include TextConfig::TextConfig(QWidget* parent) : QWidget(parent) @@ -28,8 +28,7 @@ TextConfig::TextConfig(QWidget* parent) this, &TextConfig::fontFamilyChanged); m_fontsCB->addItems(fontDB.families()); - // TODO save family in config - setFontFamily(font().family()); + setFontFamily(ConfigHandler().fontFamily()); QString iconPrefix = ColorUtils::colorIsDark(palette().windowText().color()) ? PathInfo::blackIconPath() @@ -82,7 +81,8 @@ TextConfig::TextConfig(QWidget* parent) void TextConfig::setFontFamily(const QString& fontFamily) { - m_fontsCB->setCurrentIndex(m_fontsCB->findText(fontFamily)); + m_fontsCB->setCurrentIndex( + m_fontsCB->findText(fontFamily.isEmpty() ? font().family() : fontFamily)); } void TextConfig::setUnderline(const bool u) diff --git a/src/tools/text/texttool.cpp b/src/tools/text/texttool.cpp index 5e562c66..d22f2ef7 100644 --- a/src/tools/text/texttool.cpp +++ b/src/tools/text/texttool.cpp @@ -2,6 +2,7 @@ // SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors #include "texttool.h" +#include "src/utils/confighandler.h" #include "textconfig.h" #include "textwidget.h" @@ -11,7 +12,12 @@ TextTool::TextTool(QObject* parent) : CaptureTool(parent) , m_size(1) -{} +{ + QString fontFamily = ConfigHandler().fontFamily(); + if (!fontFamily.isEmpty()) { + m_font.setFamily(ConfigHandler().fontFamily()); + } +} TextTool::~TextTool() { @@ -253,6 +259,9 @@ void TextTool::updateText(const QString& s) void TextTool::updateFamily(const QString& s) { m_font.setFamily(s); + if (m_textOld.isEmpty()) { + ConfigHandler().setFontFamily(m_font.family()); + } if (m_widget) { m_widget->setFont(m_font); } diff --git a/src/utils/confighandler.cpp b/src/utils/confighandler.cpp index 1d06e5f8..1e77d30f 100644 --- a/src/utils/confighandler.cpp +++ b/src/utils/confighandler.cpp @@ -186,6 +186,20 @@ void ConfigHandler::setDrawColor(const QColor& c) m_settings.setValue(QStringLiteral("drawColor"), c.name()); } +void ConfigHandler::setFontFamily(const QString& fontFamily) +{ + m_settings.setValue(QStringLiteral("fontFamily"), fontFamily); +} + +const QString& ConfigHandler::fontFamily() +{ + m_strRes.clear(); + if (m_settings.contains(QStringLiteral("fontFamily"))) { + m_strRes = m_settings.value(QStringLiteral("fontFamily")).toString(); + } + return m_strRes; +} + bool ConfigHandler::showHelpValue() { bool res = true; @@ -289,7 +303,7 @@ void ConfigHandler::setDisabledTrayIcon(const bool disabledTrayIcon) int ConfigHandler::drawThicknessValue() { - int res = 0; + int res = 3; if (m_settings.contains(QStringLiteral("drawThickness"))) { res = m_settings.value(QStringLiteral("drawThickness")).toInt(); } @@ -301,6 +315,20 @@ void ConfigHandler::setDrawThickness(const int thickness) m_settings.setValue(QStringLiteral("drawThickness"), thickness); } +int ConfigHandler::drawFontSizeValue() +{ + int res = 8; + if (m_settings.contains(QStringLiteral("drawFontSize"))) { + res = m_settings.value(QStringLiteral("drawFontSize")).toInt(); + } + return res; +} + +void ConfigHandler::setDrawFontSize(const int fontSize) +{ + m_settings.setValue(QStringLiteral("drawFontSize"), fontSize); +} + bool ConfigHandler::keepOpenAppLauncherValue() { return m_settings.value(QStringLiteral("keepOpenAppLauncher")).toBool(); diff --git a/src/utils/confighandler.h b/src/utils/confighandler.h index 5e99d4fb..908a52eb 100644 --- a/src/utils/confighandler.h +++ b/src/utils/confighandler.h @@ -34,6 +34,9 @@ public: QColor drawColorValue(); void setDrawColor(const QColor&); + void setFontFamily(const QString&); + const QString& fontFamily(); + bool showHelpValue(); void setShowHelp(const bool); @@ -53,6 +56,9 @@ public: int drawThicknessValue(); void setDrawThickness(const int); + int drawFontSizeValue(); + void setDrawFontSize(const int); + bool keepOpenAppLauncherValue(); void setKeepOpenAppLauncher(const bool); diff --git a/src/widgets/capture/capturetoolobjects.cpp b/src/widgets/capture/capturetoolobjects.cpp index d02abad1..12d26eab 100644 --- a/src/widgets/capture/capturetoolobjects.cpp +++ b/src/widgets/capture/capturetoolobjects.cpp @@ -7,10 +7,16 @@ #define SEARCH_RADIUS_FAR 5 #define SEARCH_RADIUS_TEXT_HANDICAP 3 +CaptureToolObjects::CaptureToolObjects(QObject* parent) + : QObject(parent) +{} + void CaptureToolObjects::append(const QPointer& captureTool) { - m_captureToolObjects.append(captureTool); - m_imageCache.clear(); + if (!captureTool.isNull()) { + m_captureToolObjects.append(captureTool->copy(captureTool->parent())); + m_imageCache.clear(); + } } QPointer CaptureToolObjects::at(int index) diff --git a/src/widgets/capture/capturetoolobjects.h b/src/widgets/capture/capturetoolobjects.h index 96380ce0..26e17690 100644 --- a/src/widgets/capture/capturetoolobjects.h +++ b/src/widgets/capture/capturetoolobjects.h @@ -11,7 +11,7 @@ class CaptureToolObjects : public QObject { public: - explicit CaptureToolObjects(QObject* parent = nullptr) { Q_UNUSED(parent) }; + explicit CaptureToolObjects(QObject* parent = nullptr); QList> captureToolObjects(); void append(const QPointer& captureTool); void removeAt(int index); diff --git a/src/widgets/capture/capturewidget.cpp b/src/widgets/capture/capturewidget.cpp index 86029a77..1fa320e2 100644 --- a/src/widgets/capture/capturewidget.cpp +++ b/src/widgets/capture/capturewidget.cpp @@ -23,6 +23,7 @@ #include "src/widgets/capture/notifierbox.h" #include "src/widgets/orientablepushbutton.h" #include "src/widgets/panel/sidepanelwidget.h" +#include "src/widgets/panel/utilitypanel.h" #include "src/widgets/updatenotificationwidget.h" #include #include @@ -62,14 +63,13 @@ CaptureWidget::CaptureWidget(uint id, , m_lastMouseWheel(0) , m_updateNotificationWidget(nullptr) , m_activeToolIsMoved(false) - , m_lastPressedUndo(false) - , m_lastPressedRedo(false) , m_panel(nullptr) + , m_sidePanel(nullptr) , m_selection(nullptr) , m_existingObjectIsChanged(false) , m_startMove(false) { - m_undoStack.setUndoLimit(ConfigHandler().undoLimit() + 1); + m_undoStack.setUndoLimit(ConfigHandler().undoLimit()); // Base config of the widget m_eventFilter = new HoverEventFilter(this); @@ -87,7 +87,6 @@ CaptureWidget::CaptureWidget(uint id, setMouseTracking(true); initContext(savePath, fullScreen); initShortcuts(); - m_context.circleCount = 1; #if (defined(Q_OS_WIN) || defined(Q_OS_MACOS)) // Top left of the whole set of screens QPoint topLeft(0, 0); @@ -197,7 +196,6 @@ CaptureWidget::~CaptureWidget() } else { emit captureFailed(m_id); } - m_config.setDrawThickness(m_context.thickness); } // redefineButtons retrieves the buttons configured to be shown with the @@ -333,6 +331,7 @@ void CaptureWidget::releaseActiveTool() void CaptureWidget::uncheckActiveTool() { // uncheck active tool + m_panel->setToolWidget(nullptr); m_activeButton->setColor(m_uiColor); m_activeButton = nullptr; releaseActiveTool(); @@ -369,10 +368,15 @@ void CaptureWidget::paintEvent(QPaintEvent* paintEvent) void CaptureWidget::showColorPicker(const QPoint& pos) { - // Reset object selection if mouse pos is not in selection area + // Try to select new object if current pos out of active object auto toolItem = activeToolObject(); - if (toolItem && !toolItem->selectionRect().contains(pos)) { - m_panel->setActiveLayer(-1); + if (!toolItem || toolItem && !toolItem->selectionRect().contains(pos)) { + selectToolItemAtPos(pos); + } + + // save current state for undo/redo stack + if (m_panel->activeLayerIndex() >= 0) { + m_captureToolObjectsBackup = m_captureToolObjects; } // Call color picker @@ -409,9 +413,13 @@ bool CaptureWidget::startDrawObjectTool(const QPoint& pos) // While it is based on AbstractTwoPointTool it has the only one // point and shouldn't wait for second point and move event m_activeTool->drawEnd(m_context.mousePos); + + m_captureToolObjectsBackup = m_captureToolObjects; m_captureToolObjects.append(m_activeTool); pushObjectsStateToUndoStack(); releaseActiveTool(); + drawToolsData(); + m_mouseIsClicked = false; } return true; @@ -421,12 +429,9 @@ bool CaptureWidget::startDrawObjectTool(const QPoint& pos) void CaptureWidget::pushObjectsStateToUndoStack() { - m_existingObjectIsChanged = false; - // push zero state to be able to do a complete undo - if (m_undoStack.count() == 0 || m_undoStack.index() == 0) { - m_undoStack.push(new ModificationCommand(this, m_captureToolObjects)); - } - m_undoStack.push(new ModificationCommand(this, m_captureToolObjects)); + m_undoStack.push(new ModificationCommand( + this, m_captureToolObjects, m_captureToolObjectsBackup)); + m_captureToolObjectsBackup.clear(); } int CaptureWidget::selectToolItemAtPos(const QPoint& pos) @@ -472,6 +477,7 @@ void CaptureWidget::mousePressEvent(QMouseEvent* e) return; } showColorPicker(m_mousePressedPos); + return; } else if (e->button() == Qt::LeftButton) { m_showInitialMsg = false; m_mouseIsClicked = true; @@ -522,6 +528,7 @@ void CaptureWidget::mouseDoubleClickEvent(QMouseEvent* event) if (m_activeTool && m_activeTool->nameID() == ToolType::TEXT) { m_mouseIsClicked = false; m_context.mousePos = *m_activeTool->pos(); + m_captureToolObjectsBackup = m_captureToolObjects; m_activeTool->setEditMode(true); drawToolsData(true, false); m_mouseIsClicked = false; @@ -560,8 +567,12 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent* e) m_activeToolOffsetToMouseOnStart = e->pos() - *activeTool->pos(); } - activeTool->move(e->pos() - m_activeToolOffsetToMouseOnStart); + if (!m_activeToolIsMoved) { + // save state before movement for undo stack + m_captureToolObjectsBackup = m_captureToolObjects; + } m_activeToolIsMoved = true; + activeTool->move(e->pos() - m_activeToolOffsetToMouseOnStart); drawToolsData(false); } } else if (m_mouseIsClicked && @@ -673,12 +684,14 @@ void CaptureWidget::mouseReleaseEvent(QMouseEvent* e) { if (e->button() == Qt::LeftButton && m_colorPicker->isVisible()) { // Color picker + if (m_colorPicker->isVisible() && m_panel->activeLayerIndex() >= 0 && + m_context.color.isValid()) { + pushObjectsStateToUndoStack(); + } m_colorPicker->hide(); if (!m_context.color.isValid()) { m_context.color = ConfigHandler().drawColorValue(); m_panel->show(); - } else if (m_panel->activeLayerIndex() >= 0) { - pushObjectsStateToUndoStack(); } } else if (m_mouseIsClicked) { if (m_activeTool) { @@ -691,13 +704,12 @@ void CaptureWidget::mouseReleaseEvent(QMouseEvent* e) } } else { if (m_activeToolIsMoved) { + m_activeToolIsMoved = false; pushObjectsStateToUndoStack(); - } - - // Try to select existing tool if it was in the selection area but - // need to select another one - if (e->pos() == m_mousePressedPos && !m_activeToolIsMoved && - m_activeButton.isNull()) { + } else if (e->pos() == m_mousePressedPos && + m_activeButton.isNull()) { + // Try to select existing tool if it was in the selection area + // but need to select another one m_panel->setActiveLayer( m_captureToolObjects.find(e->pos(), size())); } @@ -832,7 +844,10 @@ void CaptureWidget::wheelEvent(QWheelEvent* e) auto toolItem = activeToolObject(); if (toolItem) { toolItem->thicknessChanged(m_context.thickness); - m_existingObjectIsChanged = true; + if (!m_existingObjectIsChanged) { + m_captureToolObjectsBackup = m_captureToolObjects; + m_existingObjectIsChanged = true; + } } emit thicknessChanged(m_context.thickness); } @@ -840,7 +855,6 @@ void CaptureWidget::wheelEvent(QWheelEvent* e) void CaptureWidget::resizeEvent(QResizeEvent* e) { QWidget::resizeEvent(e); - m_context.widgetDimensions = rect(); m_context.widgetOffset = mapToGlobal(QPoint(0, 0)); if (!m_context.fullscreen) { m_panel->setFixedHeight(height()); @@ -856,7 +870,6 @@ void CaptureWidget::moveEvent(QMoveEvent* e) void CaptureWidget::initContext(const QString& savePath, bool fullscreen) { - m_context.widgetDimensions = rect(); m_context.color = m_config.drawColorValue(); m_context.savePath = savePath; m_context.widgetOffset = mapToGlobal(QPoint(0, 0)); @@ -927,28 +940,30 @@ void CaptureWidget::initPanel() this, &CaptureWidget::updateActiveLayer); - SidePanelWidget* sidePanel = new SidePanelWidget(&m_context.screenshot); - connect(sidePanel, + m_sidePanel = new SidePanelWidget(&m_context.screenshot); + connect(m_sidePanel, &SidePanelWidget::colorChanged, this, &CaptureWidget::setDrawColor); - connect(sidePanel, + connect(m_sidePanel, &SidePanelWidget::thicknessChanged, this, &CaptureWidget::setDrawThickness); connect(this, &CaptureWidget::colorChanged, - sidePanel, + m_sidePanel, &SidePanelWidget::updateColor); connect(this, &CaptureWidget::thicknessChanged, - sidePanel, + m_sidePanel, &SidePanelWidget::updateThickness); - connect( - sidePanel, &SidePanelWidget::togglePanel, m_panel, &UtilityPanel::toggle); - sidePanel->colorChanged(m_context.color); - sidePanel->thicknessChanged(m_context.thickness); - m_panel->pushWidget(sidePanel); + connect(m_sidePanel, + &SidePanelWidget::togglePanel, + m_panel, + &UtilityPanel::toggle); + m_sidePanel->colorChanged(m_context.color); + m_sidePanel->thicknessChanged(m_context.thickness); + m_panel->pushWidget(m_sidePanel); // Fill undo/redo/history list widget m_panel->fillCaptureTools(m_captureToolObjects.captureToolObjects()); @@ -1034,11 +1049,24 @@ void CaptureWidget::setState(CaptureToolButton* b) m_activeButton->setColor(m_uiColor); m_activeButton = nullptr; } + loadDrawThickness(); updateCursor(); update(); // clear mouse preview } } +void CaptureWidget::loadDrawThickness() +{ + if ((m_activeButton && m_activeButton->tool() && + m_activeButton->tool()->nameID() == ToolType::TEXT) || + (m_activeTool && m_activeTool->nameID() == ToolType::TEXT)) { + m_context.thickness = m_config.drawFontSizeValue(); + } else { + m_context.thickness = m_config.drawThicknessValue(); + } + m_sidePanel->thicknessChanged(m_context.thickness); +} + void CaptureWidget::processTool(CaptureTool* t) { auto backup = m_activeTool; @@ -1172,6 +1200,7 @@ void CaptureWidget::updateActiveLayer(const int& layer) commitCurrentTool(); } if (m_existingObjectIsChanged) { + m_existingObjectIsChanged = false; pushObjectsStateToUndoStack(); } drawToolsData(false, true); @@ -1183,6 +1212,7 @@ void CaptureWidget::removeToolObject(int index) if (index >= 0 && index < m_captureToolObjects.size()) { const ToolType currentToolType = m_captureToolObjects.at(index)->nameID(); + m_captureToolObjectsBackup = m_captureToolObjects; m_captureToolObjects.removeAt(index); if (currentToolType == ToolType::CIRCLECOUNT) { // Do circle count reindex @@ -1197,7 +1227,6 @@ void CaptureWidget::removeToolObject(int index) } circleCount++; } - m_context.circleCount = circleCount; } pushObjectsStateToUndoStack(); drawToolsData(); @@ -1207,7 +1236,15 @@ void CaptureWidget::removeToolObject(int index) void CaptureWidget::setDrawThickness(const int& t) { m_context.thickness = qBound(1, t, 100); - ConfigHandler().setDrawThickness(m_context.thickness); + // save draw thickness for text and other tool separately + if (m_activeButton) { + if (m_activeButton->tool() && + m_activeButton->tool()->nameID() == ToolType::TEXT) { + m_config.setDrawFontSize(m_context.thickness); + } else { + m_config.setDrawThickness(m_context.thickness); + } + } auto toolItem = activeToolObject(); if (toolItem) { @@ -1419,11 +1456,6 @@ void CaptureWidget::updateCursor() void CaptureWidget::pushToolToStack() { // append current tool to the new state - bool isChanged = true; - if (m_activeTool && m_activeTool->editMode()) { - m_activeTool->setEditMode(false); - isChanged = m_activeTool->isChanged(); - } if (m_activeTool && m_activeButton) { disconnect(this, &CaptureWidget::colorChanged, @@ -1437,12 +1469,24 @@ void CaptureWidget::pushToolToStack() disconnect(m_panel->toolWidget(), nullptr, m_activeTool, nullptr); } - m_captureToolObjects.append(m_activeTool); - releaseActiveTool(); - } + // disable signal connect for updating layer because it may call this + // function again on text objects + disconnect(m_panel, + &UtilityPanel::layerChanged, + this, + &CaptureWidget::updateActiveLayer); - if (isChanged) { + m_captureToolObjectsBackup = m_captureToolObjects; + m_captureToolObjects.append(m_activeTool); pushObjectsStateToUndoStack(); + releaseActiveTool(); + drawToolsData(); + + // restore signal connection for updating layer + connect(m_panel, + &UtilityPanel::layerChanged, + this, + &CaptureWidget::updateActiveLayer); } } @@ -1451,12 +1495,10 @@ 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; + int circleCount = 1; for (auto toolItem : m_captureToolObjects.captureToolObjects()) { if (toolItem->nameID() == ToolType::CIRCLECOUNT) { - toolItem->setCount(m_context.circleCount); - m_context.circleCount++; + toolItem->setCount(circleCount++); } toolItem->process(painter, pixmapItem); } @@ -1564,33 +1606,14 @@ void CaptureWidget::setCaptureToolObjects( void CaptureWidget::undo() { - // FIXME - m_lastPressedUndo and m_lastPressedRedo is a brutal hack, cannot - // understand why first undo/redo has no effect so need to do it twice if it - // is the firs operation - if (!m_lastPressedUndo) { - m_undoStack.undo(); - } - m_lastPressedUndo = true; - m_lastPressedRedo = false; m_undoStack.undo(); - if (m_undoStack.index() == 0 && m_captureToolObjects.size() > 0) { - m_lastPressedUndo = false; - m_captureToolObjects.clear(); - drawToolsData(); - } + drawToolsData(); } void CaptureWidget::redo() { - // FIXME - m_lastPressedUndo and m_lastPressedRedo is a brutal hack, cannot - // understand why first undo/redo has no effect so need to do it twice if it - // is the firs operation - if (!m_lastPressedRedo) { - m_undoStack.redo(); - } - m_lastPressedUndo = false; - m_lastPressedRedo = true; m_undoStack.redo(); + drawToolsData(); } QRect CaptureWidget::extendedSelection() const diff --git a/src/widgets/capture/capturewidget.h b/src/widgets/capture/capturewidget.h index 999bb4e1..8eaad970 100644 --- a/src/widgets/capture/capturewidget.h +++ b/src/widgets/capture/capturewidget.h @@ -18,7 +18,6 @@ #include "src/tools/capturetool.h" #include "src/utils/confighandler.h" #include "src/widgets/capture/selectionwidget.h" -#include "src/widgets/panel/utilitypanel.h" #include #include #include @@ -32,6 +31,8 @@ class ColorPicker; class NotifierBox; class HoverEventFilter; class UpdateNotificationWidget; +class UtilityPanel; +class SidePanelWidget; class CaptureWidget : public QWidget { @@ -107,6 +108,7 @@ protected: void moveEvent(QMoveEvent* moveEvent) override; private: + void loadDrawThickness(); void pushObjectsStateToUndoStack(); void releaseActiveTool(); void uncheckActiveTool(); @@ -169,6 +171,7 @@ private: ButtonHandler* m_buttonHandler; UtilityPanel* m_panel; + SidePanelWidget* m_sidePanel; ColorPicker* m_colorPicker; ConfigHandler m_config; NotifierBox* m_notifierBox; @@ -180,6 +183,7 @@ private: uint m_id; CaptureToolObjects m_captureToolObjects; + CaptureToolObjects m_captureToolObjectsBackup; QPoint m_mousePressedPos; QPoint m_activeToolOffsetToMouseOnStart; @@ -191,8 +195,4 @@ private: // For start moving after more than X offset QPoint m_startMovePos; bool m_startMove; - - // TODO - should be remove after fixing undo()/redo() functions - bool m_lastPressedUndo; - bool m_lastPressedRedo; }; diff --git a/src/widgets/capture/modificationcommand.cpp b/src/widgets/capture/modificationcommand.cpp index 9240fe69..635ee18c 100644 --- a/src/widgets/capture/modificationcommand.cpp +++ b/src/widgets/capture/modificationcommand.cpp @@ -6,15 +6,17 @@ ModificationCommand::ModificationCommand( CaptureWidget* captureWidget, - const CaptureToolObjects& captureToolObjects) + const CaptureToolObjects& captureToolObjects, + const CaptureToolObjects& captureToolObjectsBackup) : m_captureWidget(captureWidget) { m_captureToolObjects = captureToolObjects; + m_captureToolObjectsBackup = captureToolObjectsBackup; } void ModificationCommand::undo() { - m_captureWidget->setCaptureToolObjects(m_captureToolObjects); + m_captureWidget->setCaptureToolObjects(m_captureToolObjectsBackup); } void ModificationCommand::redo() diff --git a/src/widgets/capture/modificationcommand.h b/src/widgets/capture/modificationcommand.h index 2bcbc416..292054d1 100644 --- a/src/widgets/capture/modificationcommand.h +++ b/src/widgets/capture/modificationcommand.h @@ -13,13 +13,15 @@ class ModificationCommand : public QUndoCommand { public: ModificationCommand(CaptureWidget* captureWidget, - const CaptureToolObjects& captureToolObjects); + const CaptureToolObjects& captureToolObjects, + const CaptureToolObjects& captureToolObjectsBackup); virtual void undo() override; virtual void redo() override; private: CaptureToolObjects m_captureToolObjects; + CaptureToolObjects m_captureToolObjectsBackup; CaptureWidget* m_captureWidget; };