-
Notifications
You must be signed in to change notification settings - Fork 1
/
qmainwidget.cpp
122 lines (102 loc) · 3.14 KB
/
qmainwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
#include <QApplication>
#include <QMessageBox>
#include <QDebug>
#include "qmainwidget.h"
#include <tlhelp32.h>
QMainWidget::QMainWidget(bool remapEnabled, QWidget *parent) :
QWidget(parent)
{
createTrayIcon();
controllerWindow = NULL;
controllerRemapper = new ControllerRemapper((HWND)winId(), remapEnabled, NULL);
controllerRemapper->moveToThread(controllerRemapper);
connect(controllerRemapper, SIGNAL(initializationError(QString)), this, SLOT(error(QString)));
controllerRemapper->start();
}
void QMainWidget::deinitialize()
{
if (controllerWindow) {
controllerWindow->close();
}
controllerRemapper->exit(0);
controllerRemapper->wait(5000);
delete controllerRemapper;
}
void QMainWidget::createTrayIcon()
{
QSystemTrayIcon *trayIcon = new QSystemTrayIcon(this);
QMenu *menu = new QMenu(this);
QAction *windowAction = new QAction("Controller Window", this);
QAction *quitAction = new QAction("Shutdown xboxToVJoy", this);
enableAction = new QAction("Disable", this);
connect(enableAction, SIGNAL(triggered()), this, SLOT(toggleEnabled()));
connect(windowAction, SIGNAL(triggered()), this, SLOT(showControllerWindow()));
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
menu->addAction(windowAction);
menu->addSeparator();
menu->addAction(enableAction);
menu->addSeparator();
menu->addAction(quitAction);
trayIcon->setContextMenu(menu);
QIcon icon(":/icons/icon.png");
trayIcon->setIcon(icon);
trayIcon->show();
}
void QMainWidget::error(QString msg)
{
QMessageBox msgBox;
msgBox.setText("xboxToVJoy failed to start");
msgBox.setInformativeText(msg);
msgBox.setIcon(QMessageBox::Critical);
msgBox.addButton("Quit", QMessageBox::AcceptRole);
msgBox.exec();
qApp->quit();
}
void QMainWidget::showControllerWindow()
{
if (!controllerWindow) {
controllerWindow = new ControllerWindow(controllerRemapper, this);
connect(controllerWindow, SIGNAL(destroyed()), this, SLOT(controllerWindowDestroyed()));
}
controllerWindow->show();
controllerWindow->raise();
}
void QMainWidget::controllerWindowDestroyed()
{
controllerWindow = NULL;
}
void QMainWidget::closeEvent(QCloseEvent *event)
{
qApp->quit();
event->ignore();
}
void QMainWidget::setRemappingEnabled(bool enabled)
{
controllerRemapper->setEnabled(enabled);
if (enabled) {
enableAction->setText("Disable");
} else {
enableAction->setText("Enable");
}
}
void QMainWidget::toggleEnabled()
{
if (controllerRemapper->isEnabled()) {
setRemappingEnabled(false);
} else {
setRemappingEnabled(true);
}
}
void QMainWidget::appMessageReceived(const QString &message)
{
if (message == "enable") {
setRemappingEnabled(true);
} else if (message == "disable") {
setRemappingEnabled(false);
} else if (message == "quit") {
qApp->quit();
}
}