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.
This commit is contained in:
lupoDharkael
2017-07-28 11:34:39 +02:00
parent 165939c88f
commit 3199059ede
11 changed files with 99 additions and 93 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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() {

View File

@@ -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);

View File

@@ -16,75 +16,50 @@
// along with Flameshot. If not, see <http://www.gnu.org/licenses/>.
#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 <QAction>
#include <QApplication>
#include <QMenu>
#include "src/infowindow.h"
#include "src/config/configwindow.h"
#include "src/capture/capturebutton.h"
#include <QFile>
#include <QApplication>
#include <QSystemTrayIcon>
#include <QAction>
#include <QMenu>
// 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<int> >("QList<int>");
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<CaptureWidget> 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<CaptureWidget> Controller::createCaptureWidget(const QString &forcedSavePath) {
QPointer<CaptureWidget> 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);
}

View File

@@ -20,20 +20,20 @@
#include <QObject>
#include <QPointer>
#include <QSystemTrayIcon>
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<CaptureWidget> createCaptureWidget(const QString &forcedSavePath = "");
void createActions();
void createTrayIcon();
QAction *m_configAction;
QAction *m_infoAction;
QAction *m_quitAction;
QSystemTrayIcon *m_trayIcon;
QMenu *m_trayIconMenu;
QPointer<CaptureWidget> m_captureWindow;
QPointer<InfoWindow> m_infoWindow;
QPointer<ConfigWindow> m_configWindow;
QPointer<QSystemTrayIcon> m_trayIcon;
};
#endif // CONTROLLER_H

View File

@@ -19,7 +19,7 @@
#define FLAMESHOTDBUSADAPTER_H
#include <QtDBus/QDBusAbstractAdaptor>
#include "src/controller.h"
#include "src/core/controller.h"
class FlameshotDBusAdapter : public QDBusAbstractAdaptor
{

View File

@@ -15,9 +15,9 @@
// You should have received a copy of the GNU General Public License
// along with Flameshot. If not, see <http://www.gnu.org/licenses/>.
#include "controller.h"
#include "src/core/controller.h"
#include "singleapplication.h"
#include "src/flameshotdbusadapter.h"
#include "src/core/flameshotdbusadapter.h"
#include <QApplication>
#include <QTranslator>
#include <QObject>
@@ -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();
}

View File

@@ -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();
}

View File

@@ -54,6 +54,9 @@ public:
QString getFilenamePattern();
void setFilenamePattern(const QString &);
bool getDisabledTrayIcon();
void setDisabledTrayIcon(const bool);
bool initiatedIsSet();
void setInitiated();
void setNotInitiated();