-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCtrlAgent.h
183 lines (154 loc) · 6.21 KB
/
CtrlAgent.h
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef CTRLAGENT_H
#define CTRLAGENT_H
#include <QtWidgets/QSystemTrayIcon>
#include <QtWidgets/QMenu>
#include <QAction>
#include "CtrlSettings.h"
QT_BEGIN_NAMESPACE
namespace Ui { class CtrlPopupWidget; }
QT_END_NAMESPACE
class CtrlAgent : public QSystemTrayIcon
{
Q_OBJECT
public:
CtrlAgent(CtrlSettings *conf)
: QSystemTrayIcon(new QSystemTrayIcon),
conf(conf)
{
QIcon icon(":/main/amd_icon.ico");
this->setIcon(icon);
this->setToolTip("RyzenCtrl");
checkIcon.addFile(QString::fromUtf8(":/main/unchecked.png"),
QSize(), QIcon::Selected, QIcon::Off);
checkIcon.addFile(QString::fromUtf8(":/main/checked.png"),
QSize(), QIcon::Selected, QIcon::On);
openAction = new QAction(icon, "Open RyzenCtrl", this);
openAction->setObjectName(QString::fromUtf8("openAction"));
showAction = new QAction(checkIcon, "Show Info Widget", this);
showAction->setObjectName(QString::fromUtf8("showAction"));
showAction->setCheckable(true);
quitAction = new QAction(QIcon(":/main/application-exit.png"), "Quit", this);
quitAction->setObjectName(QString::fromUtf8("quitAction"));
trayMenu = new QMenu;
trayMenu->setObjectName(QString::fromUtf8("trayMenu"));
presetsMenu = new QMenu("Presets switch", trayMenu);
presetsMenu->setObjectName(QString::fromUtf8("presetsMenu"));
presetsMenu->setIcon(QIcon(":/main/settings.png"));
this->setContextMenu(trayMenu);
trayMenu->addAction(openAction);
trayMenu->addAction(presetsMenu->menuAction());
trayMenu->addAction(showAction);
trayMenu->addAction(quitAction);
presetActionList = new QList<QAction*>;
QAction *presetAction;
const QList<presetStr*> *presetsList = conf->getPresetsList();
for(qsizetype i = 0; i < presetsList->count(); i++){
const presetStr *presetBuffer = presetsList->at(i);
presetAction = new QAction(checkIcon, presetBuffer->presetName, this);
presetAction->setCheckable(true);
presetAction->setProperty("idx", presetBuffer->presetId);
presetsMenu->addAction(presetAction);
presetActionList->append(presetAction);
}
connection();
this->show();
}
~CtrlAgent() {
this->hide();
disconnect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
disconnect(openAction, SIGNAL(triggered()),
this, SIGNAL(showCtrlGui()));
disconnect(showAction, &QAction::triggered,
this, &CtrlAgent::showCtrlInfoWidget);
disconnect(quitAction, SIGNAL(triggered()), this, SIGNAL(closeCtrlGui()));
for(qsizetype i = 0; i < presetActionList->count(); i++){
QAction *action = presetActionList->at(i);
disconnect(action, &QAction::triggered,
this, &CtrlAgent::presetButtonClicked);
}
}
void connection(){
connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
connect(openAction, SIGNAL(triggered()),
this, SIGNAL(showCtrlGui()));
connect(showAction, &QAction::triggered,
this, &CtrlAgent::showCtrlInfoWidget);
connect(quitAction, SIGNAL(triggered()), this, SIGNAL(closeCtrlGui()));
for(qsizetype i = 0; i < presetActionList->count(); i++){
QAction *action = presetActionList->at(i);
connect(action, &QAction::triggered,
this, &CtrlAgent::presetButtonClicked);
}
}
void notificationToTray(QString message) {
if(conf->getSettingsBuffer()->showNotifications)
if(lastMessage != message) {
lastMessage = message;
this->showMessage("Preset Switch", message);
}
}
void infoPushButtonClicked(bool activate){
showAction->setChecked(activate);
}
void setCurrentPresetId(int idx){
for(qsizetype i = 0; i < presetActionList->count(); i++){
QAction *action = presetActionList->at(i);
action->setChecked(false);
if(action->property("idx").toInt() == idx)
action->setChecked(true);
}
}
void presetButtonClicked(){
int idx = reinterpret_cast<QAction *>(sender())->property("idx").toInt();
setCurrentPresetId(idx);
emit changePreset(idx);
}
void addPresetButton(int idx){
const QList<presetStr*> *presetsList = conf->getPresetsList();
for(qsizetype i = 0; i < presetsList->count(); i++){
const presetStr *presetBuffer = presetsList->at(i);
if(presetBuffer->presetId == idx){
QAction *presetAction = new QAction(checkIcon, presetBuffer->presetName, this);
presetAction->setCheckable(true);
presetAction->setProperty("idx", presetBuffer->presetId);
presetsMenu->addAction(presetAction);
presetActionList->append(presetAction);
break;
}
}
}
void delPresetButton(int idx){
for(qsizetype i = 0; i < presetActionList->count(); i++){
QAction *presetAction = presetActionList->at(i);
if(presetAction->property("idx").toInt() == idx){
presetsMenu->removeAction(presetAction);
presetActionList->removeOne(presetAction);
delete presetAction;
break;
}
}
}
signals:
void showCtrlGui();
void closeCtrlGui();
void showCtrlInfoWidget(bool checked);
void changePreset(int idx);
private slots:
void iconActivated(QSystemTrayIcon::ActivationReason reason) {
if(reason == ActivationReason::DoubleClick)
emit showCtrlGui();
}
private:
QString lastMessage;
QIcon checkIcon;
QAction *openAction;
QAction *showAction;
QAction *quitAction;
QList<QAction*> *presetActionList;
QMenu *trayMenu;
QMenu *presetsMenu;
CtrlSettings *conf;
};
#endif // CTRLAGENT_H