From 3199059ede9eb38af99a0636276a8752a845b1d7 Mon Sep 17 00:00:00 2001 From: lupoDharkael Date: Fri, 28 Jul 2017 11:34:39 +0200 Subject: [PATCH] The controller is globally accesible The controller class has some important methods which may be required in multiple parts of the code. Now that class is a singleton (that may change in the future). The core parts have been moved to src/core. Now the tray Icon can be disabled by the controller. I need to reimplement a new notification system due to its dependency with the tray icon, they are disabled in this actual commit. --- docs/dev/qsettings.md | 4 + flameshot.pro | 12 +-- src/capture/capturewidget.cpp | 6 +- src/capture/capturewidget.h | 3 - src/{ => core}/controller.cpp | 116 ++++++++++++------------ src/{ => core}/controller.h | 28 +++--- src/{ => core}/flameshotdbusadapter.cpp | 0 src/{ => core}/flameshotdbusadapter.h | 2 +- src/main.cpp | 10 +- src/utils/confighandler.cpp | 8 ++ src/utils/confighandler.h | 3 + 11 files changed, 99 insertions(+), 93 deletions(-) rename src/{ => core}/controller.cpp (60%) rename src/{ => core}/controller.h (78%) rename src/{ => core}/flameshotdbusadapter.cpp (100%) rename src/{ => core}/flameshotdbusadapter.h (97%) diff --git a/docs/dev/qsettings.md b/docs/dev/qsettings.md index 0a63aa85..b3c7eccb 100644 --- a/docs/dev/qsettings.md +++ b/docs/dev/qsettings.md @@ -30,3 +30,7 @@ - value: "filenamePattern" - type: QString - description: pattern for the saved files. +- show System Tray + - value: "showTrayIcon" + - type: bool + - description: show Tray Icon in the taskbar. diff --git a/flameshot.pro b/flameshot.pro index 61ac4f81..7287772a 100644 --- a/flameshot.pro +++ b/flameshot.pro @@ -36,7 +36,6 @@ include(src/Qt-Color-Widgets//color_widgets.pri) DEFINES += QAPPLICATION_CLASS=QApplication SOURCES += src/main.cpp\ - src/controller.cpp \ src/capture/buttonhandler.cpp \ src/infowindow.cpp \ src/config/configwindow.cpp \ @@ -47,7 +46,7 @@ SOURCES += src/main.cpp\ src/config/buttonlistview.cpp \ src/config/uicoloreditor.cpp \ src/config/geneneralconf.cpp \ - src/flameshotdbusadapter.cpp \ + src/core/flameshotdbusadapter.cpp \ src/config/clickablelabel.cpp \ src/config/filenameeditor.cpp \ src/utils/filenamehandler.cpp \ @@ -70,10 +69,10 @@ SOURCES += src/main.cpp\ src/capture/tools/selectiontool.cpp \ src/capture/tools/sizeindicatortool.cpp \ src/capture/tools/toolfactory.cpp \ - src/utils/confighandler.cpp + src/utils/confighandler.cpp \ + src/core/controller.cpp HEADERS += \ - src/controller.h \ src/capture/buttonhandler.h \ src/infowindow.h \ src/config/configwindow.h \ @@ -84,7 +83,7 @@ HEADERS += \ src/config/buttonlistview.h \ src/config/uicoloreditor.h \ src/config/geneneralconf.h \ - src/flameshotdbusadapter.h \ + src/core/flameshotdbusadapter.h \ src/config/clickablelabel.h \ src/config/filenameeditor.h \ src/utils/filenamehandler.h \ @@ -107,7 +106,8 @@ HEADERS += \ src/capture/tools/selectiontool.h \ src/capture/tools/sizeindicatortool.h \ src/capture/tools/toolfactory.h \ - src/utils/confighandler.h + src/utils/confighandler.h \ + src/core/controller.h RESOURCES += \ graphics.qrc diff --git a/src/capture/capturewidget.cpp b/src/capture/capturewidget.cpp index b454ffcb..7524724a 100644 --- a/src/capture/capturewidget.cpp +++ b/src/capture/capturewidget.cpp @@ -375,7 +375,7 @@ QString CaptureWidget::saveScreenshot(bool toClipboard) { savePath = m_screenshot->fileSave(ok, getExtendedSelection()); if(!ok || config.getSavePath() != m_forcedSavePath) { saveMessage = tr("Error trying to save in ") + savePath; - Q_EMIT newMessage(saveMessage); + // TODO send saveMessage } } if (toClipboard) { @@ -383,7 +383,7 @@ QString CaptureWidget::saveScreenshot(bool toClipboard) { } if(ok) { saveMessage = tr("Capture saved in ") + savePath; - Q_EMIT newMessage(saveMessage); + // TODO send saveMessage } close(); return savePath; @@ -437,7 +437,7 @@ void CaptureWidget::uploadScreenshot() { m_screenshot->uploadToImgur(am, getExtendedSelection()); } hide(); - Q_EMIT newMessage(tr("Uploading image...")); + // TODO send tr("Uploading image...") } bool CaptureWidget::undo() { diff --git a/src/capture/capturewidget.h b/src/capture/capturewidget.h index 81108049..45e2522b 100644 --- a/src/capture/capturewidget.h +++ b/src/capture/capturewidget.h @@ -54,9 +54,6 @@ public slots: QString saveScreenshot(bool toClipboard = false); void handleButtonSignal(CaptureTool::Request r); -signals: - void newMessage(QString); - private slots: void copyScreenshot(); void openURL(QNetworkReply *reply); diff --git a/src/controller.cpp b/src/core/controller.cpp similarity index 60% rename from src/controller.cpp rename to src/core/controller.cpp index 23f35ee4..7ddb9a01 100644 --- a/src/controller.cpp +++ b/src/core/controller.cpp @@ -16,75 +16,50 @@ // along with Flameshot. If not, see . #include "controller.h" -#include "capture/capturewidget.h" +#include "src/capture/capturewidget.h" #include "src/utils/confighandler.h" -#include "infowindow.h" -#include "config/configwindow.h" -#include "capture/capturebutton.h" -#include -#include -#include +#include "src/infowindow.h" +#include "src/config/configwindow.h" +#include "src/capture/capturebutton.h" #include +#include +#include +#include +#include // Controller is the core component of Flameshot, creates the trayIcon and // launches the capture widget -Controller::Controller(QObject *parent) : QObject(parent), - m_captureWindow(nullptr) +Controller::Controller() : m_captureWindow(nullptr) { // required for the button serialization qRegisterMetaTypeStreamOperators >("QList"); - createActions(); - createTrayIcon(); - m_trayIcon->show(); + qApp->setQuitOnLastWindowClosed(false); + + // init tray icon + if(!ConfigHandler().getDisabledTrayIcon()) { + enableTrayIcon(); + } initDefaults(); - qApp->setQuitOnLastWindowClosed(false); QString StyleSheet = CaptureButton::getGlobalStyleSheet(); qApp->setStyleSheet(StyleSheet); } +Controller *Controller::getInstance() { + static Controller c; + return &c; +} + QString Controller::saveScreenshot(const QString &path, bool const toClipboard) { QPointer w = createCaptureWidget(path); return w->saveScreenshot(toClipboard); } -// creates the items of the trayIcon -void Controller::createActions() { - m_configAction = new QAction(tr("&Configuration"), this); - connect(m_configAction, &QAction::triggered, this, - &Controller::openConfigWindow); - - m_infoAction = new QAction(tr("&Information"), this); - connect(m_infoAction, &QAction::triggered, this, - &Controller::openInfoWindow); - - m_quitAction = new QAction(tr("&Quit"), this); - connect(m_quitAction, &QAction::triggered, qApp, - &QCoreApplication::quit); -} - -// creates the trayIcon -void Controller::createTrayIcon() { - // requires a widget as parent but it should be used the whole app live period - m_trayIconMenu = new QMenu(); - m_trayIconMenu->addAction(m_configAction); - m_trayIconMenu->addAction(m_infoAction); - m_trayIconMenu->addSeparator(); - m_trayIconMenu->addAction(m_quitAction); - - m_trayIcon = new QSystemTrayIcon(this); - m_trayIcon->setToolTip("Flameshot"); - m_trayIcon->setContextMenu(m_trayIconMenu); - m_trayIcon->setIcon(QIcon(":img/flameshot.png")); - connect(m_trayIcon, &QSystemTrayIcon::activated, - this, &Controller::trayIconActivated); -} - -// initDefaults inits the global config in the very first run of the program +// initDefaults inits the global config in the first execution of the program void Controller::initDefaults() { ConfigHandler config; //config.setNotInitiated(); @@ -93,17 +68,9 @@ void Controller::initDefaults() { } } -void Controller::trayIconActivated(QSystemTrayIcon::ActivationReason r) { - if (r == QSystemTrayIcon::Trigger) { - createVisualCapture(); - } -} - // creation of a new capture QPointer Controller::createCaptureWidget(const QString &forcedSavePath) { QPointer w = new CaptureWidget(forcedSavePath); - connect(w, &CaptureWidget::newMessage, - this, &Controller::showDesktopNotification); return w; } @@ -130,9 +97,42 @@ void Controller::openInfoWindow() { } } -void Controller::showDesktopNotification(QString msg) { - bool showMessages = ConfigHandler().getDesktopNotification(); - if (showMessages && m_trayIcon->supportsMessages()) { - m_trayIcon->showMessage("Flameshot Info", msg); +void Controller::enableTrayIcon() { + if(m_trayIcon) { + return; } + ConfigHandler().setDisabledTrayIcon(false); + QAction *configAction = new QAction(tr("&Configuration")); + connect(configAction, &QAction::triggered, this, + &Controller::openConfigWindow); + QAction *infoAction = new QAction(tr("&Information")); + connect(infoAction, &QAction::triggered, this, + &Controller::openInfoWindow); + QAction *quitAction = new QAction(tr("&Quit")); + connect(quitAction, &QAction::triggered, qApp, + &QCoreApplication::quit); + + QMenu *trayIconMenu = new QMenu(); + trayIconMenu->addAction(configAction); + trayIconMenu->addAction(infoAction); + trayIconMenu->addSeparator(); + trayIconMenu->addAction(quitAction); + + m_trayIcon = new QSystemTrayIcon(); + m_trayIcon->setToolTip("Flameshot"); + m_trayIcon->setContextMenu(trayIconMenu); + m_trayIcon->setIcon(QIcon(":img/flameshot.png")); + + auto trayIconActivated = [this](QSystemTrayIcon::ActivationReason r){ + if (r == QSystemTrayIcon::Trigger) { + createVisualCapture(); + } + }; + connect(m_trayIcon, &QSystemTrayIcon::activated, this, trayIconActivated); + m_trayIcon->show(); +} + +void Controller::disableTrayIcon() { + m_trayIcon->deleteLater(); + ConfigHandler().setDisabledTrayIcon(true); } diff --git a/src/controller.h b/src/core/controller.h similarity index 78% rename from src/controller.h rename to src/core/controller.h index 897651e3..0ac38def 100644 --- a/src/controller.h +++ b/src/core/controller.h @@ -20,20 +20,20 @@ #include #include -#include -class QMenu; -class QSystemTrayIcon; -class QAction; class CaptureWidget; class ConfigWindow; class InfoWindow; +class QSystemTrayIcon; class Controller : public QObject { Q_OBJECT public: - explicit Controller(QObject *parent = nullptr); + static Controller* getInstance(); + + Controller(const Controller&) = delete; + void operator =(const Controller&) = delete; public slots: QString saveScreenshot(const QString &path = "", @@ -42,28 +42,22 @@ public slots: void openConfigWindow(); void openInfoWindow(); - void showDesktopNotification(QString); + + void enableTrayIcon(); + void disableTrayIcon(); private slots: void initDefaults(); - void trayIconActivated(QSystemTrayIcon::ActivationReason r); private: + Controller(); + QPointer createCaptureWidget(const QString &forcedSavePath = ""); - void createActions(); - void createTrayIcon(); - - QAction *m_configAction; - QAction *m_infoAction; - QAction *m_quitAction; - - QSystemTrayIcon *m_trayIcon; - QMenu *m_trayIconMenu; - QPointer m_captureWindow; QPointer m_infoWindow; QPointer m_configWindow; + QPointer m_trayIcon; }; #endif // CONTROLLER_H diff --git a/src/flameshotdbusadapter.cpp b/src/core/flameshotdbusadapter.cpp similarity index 100% rename from src/flameshotdbusadapter.cpp rename to src/core/flameshotdbusadapter.cpp diff --git a/src/flameshotdbusadapter.h b/src/core/flameshotdbusadapter.h similarity index 97% rename from src/flameshotdbusadapter.h rename to src/core/flameshotdbusadapter.h index 9ea38903..df349466 100644 --- a/src/flameshotdbusadapter.h +++ b/src/core/flameshotdbusadapter.h @@ -19,7 +19,7 @@ #define FLAMESHOTDBUSADAPTER_H #include -#include "src/controller.h" +#include "src/core/controller.h" class FlameshotDBusAdapter : public QDBusAbstractAdaptor { diff --git a/src/main.cpp b/src/main.cpp index 68c497ae..70725282 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,9 +15,9 @@ // You should have received a copy of the GNU General Public License // along with Flameshot. If not, see . -#include "controller.h" +#include "src/core/controller.h" #include "singleapplication.h" -#include "src/flameshotdbusadapter.h" +#include "src/core/flameshotdbusadapter.h" #include #include #include @@ -39,10 +39,10 @@ int main(int argc, char *argv[]) { app.setApplicationName("flameshot"); app.setOrganizationName("Dharkael"); - Controller c; - new FlameshotDBusAdapter(&c); + auto c = Controller::getInstance(); + new FlameshotDBusAdapter(c); QDBusConnection dbus = QDBusConnection::sessionBus(); - dbus.registerObject("/", &c); + dbus.registerObject("/", c); dbus.registerService("org.dharkael.Flameshot"); return app.exec(); } diff --git a/src/utils/confighandler.cpp b/src/utils/confighandler.cpp index caac1a3a..a2501e81 100644 --- a/src/utils/confighandler.cpp +++ b/src/utils/confighandler.cpp @@ -93,6 +93,14 @@ void ConfigHandler::setFilenamePattern(const QString &pattern) { return m_settings->setValue("filenamePattern", pattern); } +bool ConfigHandler::getDisabledTrayIcon() { + return m_settings->value("disabledTrayIcon").toBool(); +} + +void ConfigHandler::setDisabledTrayIcon(const bool disabledTrayIcon) { + m_settings->setValue("disabledTrayIcon", disabledTrayIcon); +} + bool ConfigHandler::initiatedIsSet() { return m_settings->value("initiated").toBool(); } diff --git a/src/utils/confighandler.h b/src/utils/confighandler.h index 45861996..ee17f6ff 100644 --- a/src/utils/confighandler.h +++ b/src/utils/confighandler.h @@ -54,6 +54,9 @@ public: QString getFilenamePattern(); void setFilenamePattern(const QString &); + bool getDisabledTrayIcon(); + void setDisabledTrayIcon(const bool); + bool initiatedIsSet(); void setInitiated(); void setNotInitiated();