144 lines
3.5 KiB
C++
144 lines
3.5 KiB
C++
// for testing
|
|
#include <QMessageBox>
|
|
#include <QProcess>
|
|
#include <QCloseEvent>
|
|
|
|
#include "gui/formmain.h"
|
|
#include "gui/formsettings.h"
|
|
#include "gui/themefactory.h"
|
|
#include "gui/systemtrayicon.h"
|
|
#include "core/settings.h"
|
|
#include "core/defs.h"
|
|
#include "qtsingleapplication/qtsingleapplication.h"
|
|
|
|
|
|
FormMain *FormMain::m_this;
|
|
|
|
FormMain::FormMain(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::FormMain) {
|
|
m_ui->setupUi(this);
|
|
|
|
// Initialize singleton.
|
|
m_this = this;
|
|
|
|
// Establish connections.
|
|
createConnections();
|
|
|
|
prepareMenus();
|
|
}
|
|
|
|
FormMain::~FormMain() {
|
|
delete m_ui;
|
|
|
|
if (SystemTrayIcon::isSystemTrayAvailable()) {
|
|
delete m_trayMenu;
|
|
qDebug("Deleting tray icon menu.");
|
|
}
|
|
}
|
|
|
|
FormMain *FormMain::getInstance() {
|
|
return m_this;
|
|
}
|
|
|
|
QMenu *FormMain::getTrayMenu() {
|
|
return m_trayMenu;
|
|
}
|
|
|
|
void FormMain::prepareMenus() {
|
|
// Setup menu for tray icon.
|
|
if (SystemTrayIcon::isSystemTrayAvailable()) {
|
|
m_trayMenu = new QMenu(APP_NAME, this);
|
|
|
|
// Add needed items to the menu.
|
|
m_trayMenu->addAction(m_ui->m_actionSettings);
|
|
m_trayMenu->addAction(m_ui->m_actionQuit);
|
|
|
|
qDebug("Creating tray icon menu.");
|
|
}
|
|
}
|
|
|
|
void FormMain::processExecutionMessage(const QString &message) {
|
|
// TODO: Implement proper reaction when application is launched more than once.
|
|
qDebug("Received '%s' execution message from another application instance.",
|
|
qPrintable(message));
|
|
display();
|
|
}
|
|
|
|
void FormMain::quit() {
|
|
qDebug("Quitting the application.");
|
|
qApp->quit();
|
|
}
|
|
|
|
void FormMain::switchVisibility() {
|
|
if (isVisible()) {
|
|
hide();
|
|
}
|
|
else {
|
|
display();
|
|
}
|
|
}
|
|
|
|
void FormMain::display() {
|
|
// Make sure window is not minimized.
|
|
setWindowState(windowState() & ~Qt::WindowMinimized);
|
|
|
|
// Display the window and make sure it is raised on top.
|
|
show();
|
|
activateWindow();
|
|
raise();
|
|
|
|
// Raise alert event. Check the documentation for more info on this.
|
|
QtSingleApplication::alert(this);
|
|
}
|
|
|
|
void FormMain::cleanupResources() {
|
|
qDebug("Cleaning up resources before the application exits.");
|
|
}
|
|
|
|
#if !defined(Q_OS_WIN)
|
|
bool FormMain::event(QEvent *event) {
|
|
if (event->type() == ThemeFactoryEvent::type()) {
|
|
// Handle the change of icon theme.
|
|
setupIcons();
|
|
return true;
|
|
}
|
|
|
|
return QMainWindow::event(event);
|
|
}
|
|
|
|
void FormMain::setupIcons() {
|
|
// NOTE: Call QIcon::fromTheme for all needed widgets here.
|
|
}
|
|
#endif
|
|
|
|
void FormMain::createConnections() {
|
|
// Menu "File" connections.
|
|
connect(m_ui->m_actionQuit, &QAction::triggered, this, &FormMain::quit);
|
|
|
|
// Menu "Tools" connections.
|
|
connect(m_ui->m_actionSettings, &QAction::triggered, this, &FormMain::showSettings);
|
|
|
|
// General connections.
|
|
connect(qApp, &QCoreApplication::aboutToQuit, this, &FormMain::cleanupResources);
|
|
}
|
|
|
|
void FormMain::closeEvent(QCloseEvent *event) {
|
|
if (SystemTrayIcon::isSystemTrayActivated()) {
|
|
if (Settings::getInstance()->value(APP_CFG_GUI,
|
|
"close_win_action",
|
|
0).toInt() == 0) {
|
|
// User selected to minimize the application if its main
|
|
// window gets closed and tray icon is activated.
|
|
hide();
|
|
event->ignore();
|
|
}
|
|
else {
|
|
// User selected to quit the application if its main
|
|
// window gets closed and tray icon is activated.
|
|
qApp->quit();
|
|
}
|
|
}
|
|
}
|
|
|
|
void FormMain::showSettings() {
|
|
FormSettings(this).exec();
|
|
}
|