Files
flameshot/src/core/controller.cpp
lupoDharkael 3199059ede 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.
2017-07-28 11:34:39 +02:00

139 lines
4.2 KiB
C++

// Copyright 2017 Alejandro Sirgo Rica
//
// This file is part of Flameshot.
//
// Flameshot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Flameshot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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/capture/capturewidget.h"
#include "src/utils/confighandler.h"
#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() : m_captureWindow(nullptr)
{
// required for the button serialization
qRegisterMetaTypeStreamOperators<QList<int> >("QList<int>");
qApp->setQuitOnLastWindowClosed(false);
// init tray icon
if(!ConfigHandler().getDisabledTrayIcon()) {
enableTrayIcon();
}
initDefaults();
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);
}
// initDefaults inits the global config in the first execution of the program
void Controller::initDefaults() {
ConfigHandler config;
//config.setNotInitiated();
if (!config.initiatedIsSet()) {
config.setDefaults();
}
}
// creation of a new capture
QPointer<CaptureWidget> Controller::createCaptureWidget(const QString &forcedSavePath) {
QPointer<CaptureWidget> w = new CaptureWidget(forcedSavePath);
return w;
}
// creation of a new capture in GUI mode
void Controller::createVisualCapture(const QString &forcedSavePath) {
if (!m_captureWindow) {
m_captureWindow = createCaptureWidget(forcedSavePath);
m_captureWindow->showFullScreen();
}
}
// creation of the configuration window
void Controller::openConfigWindow() {
if (!m_configWindow) {
m_configWindow = new ConfigWindow();
m_configWindow->show();
}
}
// creation of the window of information
void Controller::openInfoWindow() {
if (!m_infoWindow) {
m_infoWindow = new InfoWindow();
}
}
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);
}