From 8e25f438f21a0deb9089cedf0fbaff8dab0177ee Mon Sep 17 00:00:00 2001 From: Yuriy Puchkov Date: Fri, 16 Oct 2020 10:08:13 +0300 Subject: [PATCH 1/5] Code refactoring - HistoryWidget, add clearHistoryLayout and loadHistory methods --- src/core/controller.cpp | 1 + src/core/globalshortcutfilter.cpp | 1 + src/widgets/historywidget.cpp | 20 ++++++++++++++++++-- src/widgets/historywidget.h | 5 +++-- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/core/controller.cpp b/src/core/controller.cpp index f11c3f8f..fe0c713b 100644 --- a/src/core/controller.cpp +++ b/src/core/controller.cpp @@ -298,6 +298,7 @@ void Controller::updateConfigComponents() void Controller::showRecentScreenshots() { HistoryWidget* pHistory = new HistoryWidget(); + pHistory->loadHistory(); pHistory->exec(); } diff --git a/src/core/globalshortcutfilter.cpp b/src/core/globalshortcutfilter.cpp index 57f198ba..a956a1ae 100644 --- a/src/core/globalshortcutfilter.cpp +++ b/src/core/globalshortcutfilter.cpp @@ -58,6 +58,7 @@ bool GlobalShortcutFilter::nativeEventFilter(const QByteArray& eventType, if (VK_SNAPSHOT == keycode && MOD_SHIFT == modifiers) { if (m_history == nullptr) { m_history = new HistoryWidget(); + m_history->loadHistory(); } m_history->show(); } diff --git a/src/widgets/historywidget.cpp b/src/widgets/historywidget.cpp index 25df2ccd..f35c382a 100644 --- a/src/widgets/historywidget.cpp +++ b/src/widgets/historywidget.cpp @@ -41,8 +41,6 @@ HistoryWidget::HistoryWidget(QWidget* parent) QWidget* widget = new QWidget(); scrollArea->setWidget(widget); widget->setLayout(m_pVBox); - - loadHistory(); } HistoryWidget::~HistoryWidget() @@ -50,8 +48,26 @@ HistoryWidget::~HistoryWidget() delete m_notification; } +void HistoryWidget::clearHistoryLayout(QLayout* layout) +{ + QLayoutItem* child; + while (layout->count() != 0) { + child = layout->takeAt(0); + if (child->layout() != 0) { + clearHistoryLayout(child->layout()); + } else if (child->widget() != 0) { + delete child->widget(); + } + + delete child; + } +} + void HistoryWidget::loadHistory() { + // clear old history if exists + clearHistoryLayout(m_pVBox); + // read history files History history = History(); QList historyFiles = history.history(); diff --git a/src/widgets/historywidget.h b/src/widgets/historywidget.h index abd4e0b9..b713f666 100644 --- a/src/widgets/historywidget.h +++ b/src/widgets/historywidget.h @@ -21,10 +21,11 @@ public: explicit HistoryWidget(QWidget* parent = nullptr); ~HistoryWidget(); -signals: + void loadHistory(); private: - void loadHistory(); + void clearHistoryLayout(QLayout* layout); + void addLine(const QString&, const QString&); void setEmptyMessage(); void removeItem(QLayout* pl, From 161ecc3c04461435303878d79a5ba766f14b4c40 Mon Sep 17 00:00:00 2001 From: Yuriy Puchkov Date: Fri, 16 Oct 2020 10:17:29 +0300 Subject: [PATCH 2/5] Fix - HistoryWidget is not deleted in Controller --- src/core/controller.cpp | 15 ++++++++++++--- src/core/controller.h | 4 ++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/core/controller.cpp b/src/core/controller.cpp index fe0c713b..2666d1e8 100644 --- a/src/core/controller.cpp +++ b/src/core/controller.cpp @@ -44,6 +44,8 @@ Controller::Controller() : m_captureWindow(nullptr) { + m_history = nullptr; + qApp->setQuitOnLastWindowClosed(false); // set default shortcusts if not set yet @@ -68,6 +70,11 @@ Controller::Controller() qApp->setStyleSheet(StyleSheet); } +Controller::~Controller() +{ + delete m_history; +} + Controller* Controller::getInstance() { static Controller c; @@ -297,9 +304,11 @@ void Controller::updateConfigComponents() void Controller::showRecentScreenshots() { - HistoryWidget* pHistory = new HistoryWidget(); - pHistory->loadHistory(); - pHistory->exec(); + if (nullptr == m_history) { + m_history = new HistoryWidget(); + } + m_history->loadHistory(); + m_history->show(); } void Controller::startFullscreenCapture(const uint id) diff --git a/src/core/controller.h b/src/core/controller.h index 2308abcc..0cb9ee24 100644 --- a/src/core/controller.h +++ b/src/core/controller.h @@ -30,6 +30,7 @@ class ConfigWindow; class InfoWindow; class QSystemTrayIcon; class CaptureLauncher; +class HistoryWidget; using lambda = std::function; class Controller : public QObject @@ -40,6 +41,7 @@ public: static Controller* getInstance(); Controller(const Controller&) = delete; + ~Controller(); void operator=(const Controller&) = delete; void enableExports(); @@ -87,4 +89,6 @@ private: QPointer m_launcherWindow; QPointer m_configWindow; QPointer m_trayIcon; + + HistoryWidget* m_history; }; From 3c5ff1d29c4e6e2449ea22073266c54ee8cbf218 Mon Sep 17 00:00:00 2001 From: Yuriy Puchkov Date: Fri, 16 Oct 2020 10:41:51 +0300 Subject: [PATCH 3/5] Code refactoring - Cleanup code --- src/config/shortcutswidget.cpp | 2 -- src/utils/configshortcuts.cpp | 4 +--- src/widgets/historywidget.cpp | 2 -- src/widgets/orientablepushbutton.cpp | 1 - 4 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/config/shortcutswidget.cpp b/src/config/shortcutswidget.cpp index afd1aabc..b11e9afa 100644 --- a/src/config/shortcutswidget.cpp +++ b/src/config/shortcutswidget.cpp @@ -33,8 +33,6 @@ #include #endif -#include - ShortcutsWidget::ShortcutsWidget(QWidget* parent) : QWidget(parent) { diff --git a/src/utils/configshortcuts.cpp b/src/utils/configshortcuts.cpp index 7f957e36..8bc28d56 100644 --- a/src/utils/configshortcuts.cpp +++ b/src/utils/configshortcuts.cpp @@ -1,11 +1,9 @@ #include "configshortcuts.h" #include "src/tools/capturetool.h" -#include +#include ConfigShortcuts::ConfigShortcuts() {} -// QVector getButtons() - const QVector& ConfigShortcuts::captureShortcutsDefault( const QVector& buttons) { diff --git a/src/widgets/historywidget.cpp b/src/widgets/historywidget.cpp index f35c382a..4a80eddf 100644 --- a/src/widgets/historywidget.cpp +++ b/src/widgets/historywidget.cpp @@ -20,8 +20,6 @@ #include #include -#include - HistoryWidget::HistoryWidget(QWidget* parent) : QDialog(parent) { diff --git a/src/widgets/orientablepushbutton.cpp b/src/widgets/orientablepushbutton.cpp index f85b66d5..68cfdcc5 100644 --- a/src/widgets/orientablepushbutton.cpp +++ b/src/widgets/orientablepushbutton.cpp @@ -18,7 +18,6 @@ // Based on https://stackoverflow.com/a/53135675/964478 #include "orientablepushbutton.h" -#include #include #include #include From f9497ee2ea94c491c9412890a92c5aa52335fb3a Mon Sep 17 00:00:00 2001 From: Yuriy Puchkov Date: Fri, 16 Oct 2020 13:22:32 +0300 Subject: [PATCH 4/5] Fix - HistoryWidget is not updated if called by Shortcut --- src/core/globalshortcutfilter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/globalshortcutfilter.cpp b/src/core/globalshortcutfilter.cpp index a956a1ae..0f311134 100644 --- a/src/core/globalshortcutfilter.cpp +++ b/src/core/globalshortcutfilter.cpp @@ -58,8 +58,8 @@ bool GlobalShortcutFilter::nativeEventFilter(const QByteArray& eventType, if (VK_SNAPSHOT == keycode && MOD_SHIFT == modifiers) { if (m_history == nullptr) { m_history = new HistoryWidget(); - m_history->loadHistory(); } + m_history->loadHistory(); m_history->show(); } From 0919d26145aefd70edcba0a3f8c802862cf06849 Mon Sep 17 00:00:00 2001 From: Yuriy Puchkov Date: Fri, 16 Oct 2020 13:43:50 +0300 Subject: [PATCH 5/5] Fix - call the same HistoryWidget object on Shortcut and from the Menu --- src/core/globalshortcutfilter.cpp | 14 +------------- src/core/globalshortcutfilter.h | 4 ---- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/src/core/globalshortcutfilter.cpp b/src/core/globalshortcutfilter.cpp index 0f311134..0a6c2ebe 100644 --- a/src/core/globalshortcutfilter.cpp +++ b/src/core/globalshortcutfilter.cpp @@ -17,14 +17,11 @@ #include "globalshortcutfilter.h" #include "src/core/controller.h" -#include "src/widgets/historywidget.h" #include GlobalShortcutFilter::GlobalShortcutFilter(QObject* parent) : QObject(parent) { - m_history = nullptr; - // Forced Print Screen if (RegisterHotKey(NULL, 1, 0, VK_SNAPSHOT)) { // ok - capture screen @@ -35,11 +32,6 @@ GlobalShortcutFilter::GlobalShortcutFilter(QObject* parent) } } -GlobalShortcutFilter::~GlobalShortcutFilter() -{ - delete m_history; -} - bool GlobalShortcutFilter::nativeEventFilter(const QByteArray& eventType, void* message, long* result) @@ -56,11 +48,7 @@ bool GlobalShortcutFilter::nativeEventFilter(const QByteArray& eventType, // Show screenshots history if (VK_SNAPSHOT == keycode && MOD_SHIFT == modifiers) { - if (m_history == nullptr) { - m_history = new HistoryWidget(); - } - m_history->loadHistory(); - m_history->show(); + Controller::getInstance()->showRecentScreenshots(); } // Capture screen diff --git a/src/core/globalshortcutfilter.h b/src/core/globalshortcutfilter.h index 83137c84..3ed8f10d 100644 --- a/src/core/globalshortcutfilter.h +++ b/src/core/globalshortcutfilter.h @@ -20,8 +20,6 @@ #include #include -class HistoryWidget; - class GlobalShortcutFilter : public QObject , public QAbstractNativeEventFilter @@ -29,7 +27,6 @@ class GlobalShortcutFilter Q_OBJECT public: explicit GlobalShortcutFilter(QObject* parent = nullptr); - ~GlobalShortcutFilter(); bool nativeEventFilter(const QByteArray& eventType, void* message, @@ -43,5 +40,4 @@ private: quint32 nativeKeycode(Qt::Key key); bool registerShortcut(quint32 nativeKey, quint32 nativeMods); bool unregisterShortcut(quint32 nativeKey, quint32 nativeMods); - HistoryWidget* m_history; };