-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
152 lines (122 loc) · 4.15 KB
/
Copy pathmainwindow.cpp
File metadata and controls
152 lines (122 loc) · 4.15 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
#include "workerthread.h"
#include "setting.h"
#include <qt_windows.h>
#include <QtDebug>
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
tray_(QIcon(":/new/app/favicon.png"))
{
ui->setupUi(this);
connect(&tableModel_, &TableModel::reported, this, &MainWindow::on_Reported);
ui->tableView->setModel(&tableModel_);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
ui->tableView->setColumnWidth(Column_Name, 120);
ui->tableView->setColumnWidth(Column_Cmd, 380);
tableModel_.load();
connect(&tray_, &QSystemTrayIcon::activated, this, &MainWindow::on_IconActivated);
tray_.show();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_action_new_triggered()
{
std::unique_ptr<Dialog> dialog = std::make_unique<Dialog>("", "[]", ListEnvironment(), MODE_CREATE, this);
connect(dialog.get(), &Dialog::created, this, &MainWindow::on_Created);
dialog->setModal(true);
dialog->exec();
}
void MainWindow::on_action_edit_triggered()
{
if (ui->tableView->selectionModel()->hasSelection()) {
auto indexes = ui->tableView->selectionModel()->selectedRows();
auto model = ui->tableView->selectionModel()->model();
auto name = model->data(indexes[0]).toString();
auto cmd = model->data(model->index(indexes[0].row(), Column_Cmd)).toString();
auto env = model->data(model->index(indexes[0].row(), Column_Env)).toString();
std::unique_ptr<Dialog> dialog = std::make_unique<Dialog>(name, cmd, WorkerThread::StringToListEnvironment(env), MODE_EDIT, this);
connect(dialog.get(), &Dialog::edited, this, &MainWindow::on_Edited);
dialog->setModal(true);
dialog->exec();
}
}
void MainWindow::on_Created(const QString& name, const QString& cmd, const ListEnvironment& env)
{
tableModel_.add(name, cmd, WorkerThread::ListEnvironmentToString(env));
}
void MainWindow::on_Edited(const QString& name, const QString& cmd, const ListEnvironment& env)
{
if (ui->tableView->selectionModel()->hasSelection()) {
auto indexes = ui->tableView->selectionModel()->selectedRows();
tableModel_.editCurrent(indexes[0].row(), name, cmd, WorkerThread::ListEnvironmentToString(env));
}
}
void MainWindow::on_Reported(const QString& message)
{
ui->plainTextEdit->appendPlainText(message);
}
void MainWindow::on_action_del_triggered()
{
if (ui->tableView->selectionModel()->hasSelection()) {
auto indexes = ui->tableView->selectionModel()->selectedRows();
tableModel_.delCurrent(indexes[0].row());
}
}
void MainWindow::on_tableView_doubleClicked(const QModelIndex& index)
{
auto model = ui->tableView->model();
auto name = model->data(model->index(index.row(), Column_Name)).toString();
auto cmd = model->data(model->index(index.row(), Column_Cmd)).toString();
auto env = model->data(model->index(index.row(), Column_Env)).toString();
WorkerThread* workerThread = new WorkerThread(name, cmd, WorkerThread::StringToListEnvironment(env));
connect(workerThread, SIGNAL(reported(const QString&)), ui->plainTextEdit, SLOT(appendPlainText(const QString&)));
connect(workerThread, &WorkerThread::finished, workerThread, &WorkerThread::deleteLater);
workerThread->start();
}
void MainWindow::closeEvent(QCloseEvent* event)
{
hide();
tray_.showMessage("Tips", "I'm here.");
event->ignore();
}
bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result)
{
if (eventType == "windows_generic_MSG") {
MSG* msg = static_cast<MSG*>(message);
if (msg->message == WM_SHOWWINDOW && msg->wParam == TRUE) {
show();
}
}
return QMainWindow::nativeEvent(eventType, message, result);
}
void MainWindow::on_action_quit_triggered()
{
qApp->exit();
}
void MainWindow::on_IconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
setWindowState(Qt::WindowActive);
showNormal();
break;
case QSystemTrayIcon::MiddleClick:
case QSystemTrayIcon::Context:
qApp->exit();
break;
default:
;
}
}
void MainWindow::on_action_setting_triggered()
{
std::unique_ptr<Setting> dialog = std::make_unique<Setting>();
dialog->setModal(true);
dialog->exec();
}