-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnectmanagerui.cpp
351 lines (316 loc) · 12.5 KB
/
connectmanagerui.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "connectmanagerui.h"
#include <QAction>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QMenu>
#include <QMovie>
#include "connecteditui.h"
#include "db/connectdao.h"
#include "mylabel.h"
#include "sftpdialog.h"
#include "ui_connectmanagerui.h"
ConnectManagerUI::ConnectManagerUI(QWidget *parent)
: QWidget(parent)
// ui(new Ui::ConnectManagerUI)
{
// ui->setupUi(this);
// setWindowTitle("连接管理器");
// setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
// setFixedSize(600, 300);
backupAndRecoveryDialog = new BackupAndRecoveryDialog(this);
QVBoxLayout *rootlayout = new QVBoxLayout();
QHBoxLayout *hlayout = new QHBoxLayout();
QVBoxLayout *vAddFolderlayout = new QVBoxLayout();
MyLabel *folderlabel = new MyLabel();
connect(folderlabel, &MyLabel::click, [=]() { createNewGroup(); });
QPixmap folderpixmap(":/icons/folder.png");
QSize sz(25, 25);
folderpixmap = folderpixmap.scaled(sz, Qt::KeepAspectRatio);
folderlabel->setPixmap(folderpixmap);
folderlabel->show();
MyLabel *grouplabel = new MyLabel("新建组");
connect(grouplabel, &MyLabel::click, [=]() { createNewGroup(); });
vAddFolderlayout->addWidget(folderlabel, 0, Qt::AlignCenter);
vAddFolderlayout->addWidget(grouplabel, 0, Qt::AlignCenter);
QVBoxLayout *vBackUplayout = new QVBoxLayout();
MyLabel *backupImagelabel = new MyLabel();
connect(backupImagelabel, &MyLabel::click,
[=]() { backupAndRecoveryDialog->show(); });
QPixmap backuppixmap(":/icons/backup_recovery.png");
backuppixmap = backuppixmap.scaled(sz, Qt::KeepAspectRatio);
backupImagelabel->setPixmap(backuppixmap);
backupImagelabel->show();
MyLabel *backuplabel = new MyLabel("备份/恢复");
connect(backuplabel, &MyLabel::click,
[=]() { backupAndRecoveryDialog->show(); });
vBackUplayout->addWidget(backupImagelabel, 0, Qt::AlignCenter);
vBackUplayout->addWidget(backuplabel, 0, Qt::AlignCenter);
QVBoxLayout *vAddServerlayout = new QVBoxLayout();
MyLabel *refreshIconlabel = new MyLabel();
connect(refreshIconlabel, &MyLabel::click,
[=]() { this->refreshTreeWidget(); });
QPixmap refreshpixmap(":/icons/refresh.png");
refreshpixmap = refreshpixmap.scaled(sz, Qt::KeepAspectRatio);
refreshIconlabel->setPixmap(refreshpixmap);
refreshIconlabel->show();
MyLabel *refreshlabel = new MyLabel("刷新");
connect(refreshlabel, &MyLabel::click, [=]() { this->refreshTreeWidget(); });
vAddServerlayout->addWidget(refreshIconlabel, 0, Qt::AlignCenter);
vAddServerlayout->addWidget(refreshlabel, 0, Qt::AlignCenter);
hlayout->addLayout(vAddFolderlayout);
hlayout->addLayout(vBackUplayout);
hlayout->addLayout(vAddServerlayout);
rootlayout->addLayout(hlayout);
treeView = new QTreeWidget(this);
treeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(treeView, &QTreeWidget::itemClicked,
[&](QTreeWidgetItem *item, int column) {
if (treeView->isExpanded(treeView->currentIndex())) {
treeView->collapseItem(item);
} else {
treeView->expandItem(item);
}
});
connect(
treeView, &QTreeWidget::itemPressed,
[&](QTreeWidgetItem *item, int column) { qDebug() << "itemPressed"; });
connect(treeView, &QTreeWidget::itemDoubleClicked, this,
[=](QTreeWidgetItem *item, int column) {
qDebug() << "双击";
ConnectInfo info = getConnectInfo(item);
if (info.parentId != 0) {
this->hide();
emit openSSHConnect(info);
}
});
connect(treeView, &QTreeWidget::itemActivated,
[&](QTreeWidgetItem *item, int column) { qDebug() << "回车"; });
connect(treeView, SIGNAL(customContextMenuRequested(const QPoint &)), this,
SLOT(popMenu(const QPoint &)));
treeView->setColumnWidth(0, 200);
treeView->setColumnWidth(1, 150);
treeView->setColumnCount(8); //设置列
treeView->hideColumn(4);
treeView->hideColumn(5);
treeView->setHeaderLabels(QStringList() << "名称"
<< "用户名"
<< "Host"
<< "SSH端口"
<< "id"
<< "parent_id"
<< "VNC端口"
<< "RDP端口");
rootlayout->addWidget(treeView);
this->setLayout(rootlayout);
this->refreshTreeWidget();
}
void ConnectManagerUI::createNewGroup() {
int id = ConnectDao::GetInstance()->addConnectInfo("新建文件夹");
if (id > 1) {
QTreeWidgetItem *group2 = new QTreeWidgetItem(treeView);
group2->setText(0, "新建文件夹");
group2->setIcon(0, QIcon(":/icons/folder.png"));
group2->setText(4, QString::number(id));
}
// group2->setFlags(group2->flags() | Qt::ItemIsEditable);
}
void ConnectManagerUI::createNewServer() {
QMessageBox::information(NULL, "单击", "创建服务器",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
}
void ConnectManagerUI::popMenu(const QPoint &p) {
QTreeWidgetItem *curItem = treeView->currentItem(); //获取当前被点击的节点
QModelIndex modelIndex = treeView->currentIndex();
if (curItem == NULL) {
return; //这种情况是右键的位置不在treeItem的范围内,即在空白位置右击
}
QMenu menu(treeView);
QString wellName = curItem->text(0);
int id = curItem->text(4).toInt();
ConnectInfo info = ConnectDao::GetInstance()->getConnectInfo(id);
if (info.parentId == 0) {
QAction *refreshAction = new QAction("刷新", this);
connect(refreshAction, &QAction::triggered,
[&]() { treeView->expand(modelIndex); });
menu.addAction(refreshAction);
QAction *addconnect = new QAction("新增连接", this); //新增连接
connect(addconnect, &QAction::triggered, [&]() {
QString title = "新增连接";
ConnectEditUI *connectEditUI = new ConnectEditUI(this, title);
connectEditUI->setConnectInfo(info);
connectEditUI->show();
connect(connectEditUI, &ConnectEditUI::addSuccess,
[=](ConnectInfo info) { this->addChileItem(curItem, info); });
});
menu.addAction(addconnect);
}
QAction *connectWell;
QAction *sftpWell;
QAction *connectVNC;
QAction *connectRDP;
if (info.parentId != 0) {
connectWell = new QAction("SSH连接", this); //连接
connect(connectWell, &QAction::triggered,
[&]() { emit openSSHConnect(info); });
menu.addAction(connectWell);
connectVNC = new QAction("VNC连接", this);
connect(connectVNC, &QAction::triggered,
[&]() { emit openVNCConnect(info); });
menu.addAction(connectVNC);
#ifdef Q_OS_LINUX
connectRDP = new QAction("RDP连接", this);
connect(connectRDP, &QAction::triggered,
[&]() { emit openRDPConnect(info); });
menu.addAction(connectRDP);
#endif
sftpWell = new QAction("文件管理", this);
connect(sftpWell, &QAction::triggered,
[&]() { emit openSFTPConnect(info); });
menu.addAction(sftpWell);
}
QAction *editWell = new QAction("编辑", this);
connect(editWell, &QAction::triggered, [&]() {
if (info.parentId == 0) {
ConfirmDialog *dialog = new ConfirmDialog(this, "请输入资源名称");
dialog->show();
connect(dialog, &ConfirmDialog::confirmEditEvent, [=](QString input) {
int id = curItem->text(4).toInt();
this->updateConnectName(curItem, id, input);
});
} else {
QString title = curItem->text(0);
title += "-编辑";
ConnectEditUI *connectEditUI = new ConnectEditUI(this, title);
connectEditUI->setConnectInfo(info);
connectEditUI->show();
connect(connectEditUI, &ConnectEditUI::updateSuccess,
[=](ConnectInfo info) { this->upadateChileItem(curItem, info); });
}
});
menu.addAction(editWell);
QAction *deleteWell;
if (info.id != 1) {
deleteWell = new QAction("删除", this); //删除
//在界面上删除该item
connect(deleteWell, &QAction::triggered, this, [&]() {
if (info.parentId != 0) {
ConnectDao::GetInstance()->deleteById(info.id);
delete curItem;
} else {
QMessageBox messageBox(QMessageBox::NoIcon, "数据删除警告",
"删除该组下所有资源?",
QMessageBox::Yes | QMessageBox::No, this);
int result = messageBox.exec();
if (result == QMessageBox::Yes) {
ConnectDao::GetInstance()->deleteById(info.id);
ConnectDao::GetInstance()->deleteByParentId(info.id);
delete curItem;
}
messageBox.close();
}
});
menu.addAction(deleteWell);
}
if (info.parentId != 0) {
QAction *copyAction = new QAction("复制", this);
connect(copyAction, &QAction::triggered, this,
[=]() { this->copyInfo = info; });
menu.addAction(copyAction);
}
if (info.parentId == 0 && copyInfo.id > 0) {
QAction *pasteAction = new QAction("黏贴", this);
connect(pasteAction, &QAction::triggered, this, [=]() {
copyInfo.parentId = id;
bool result =
ConnectDao::GetInstance()->addConnectInfo(&(this->copyInfo));
if (result) {
addChileItem(curItem, this->copyInfo);
copyInfo = ConnectInfo();
treeView->expand(modelIndex);
}
});
menu.addAction(pasteAction);
}
menu.exec(QCursor::pos()); //在当前鼠标位置显示
}
void ConnectManagerUI::updateConnectName(QTreeWidgetItem *item, int id,
QString name) {
bool result = ConnectDao::GetInstance()->updateConnectName(id, name);
if (!result) {
QMessageBox::warning(this, "提示", "名称修改失败",
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
return;
}
item->setText(0, name);
}
void ConnectManagerUI::refreshList(QTreeWidgetItem *item) {
int id = item->text(4).toInt();
QList<ConnectInfo> infos = ConnectDao::GetInstance()->getConnectInfos(id);
for (auto info : infos) {
addChileItem(rootItem, info);
// QTreeWidgetItem *child = new QTreeWidgetItem(rootItem);
// child->setText(0, info.name);
// child->setIcon(0, QIcon(":/icons/server.png"));
// child->setText(1, info.username);
// child->setText(2, info.hostName);
// if (info.port > 0) {
// child->setText(3, QString::number(info.port));
// }
// child->setText(4, QString::number(info.id));
// if (info.vncPort > 0) {
// child->setText(6, QString::number(info.vncPort));
// }
// if (info.rdpPort > 0) {
// child->setText(7, QString::number(info.rdpPort));
// }
// child->setFlags(child->flags() | Qt::ItemIsEditable);
// rootItem->addChild(child);
}
}
void ConnectManagerUI::addChileItem(QTreeWidgetItem *item, ConnectInfo info) {
QTreeWidgetItem *child = new QTreeWidgetItem(item);
child->setText(0, info.name);
child->setIcon(0, QIcon(":/icons/server.png"));
child->setText(1, info.username);
child->setText(2, info.hostName);
// child->setText(3, QString::number(info.port));
if (info.port > 0) {
child->setText(3, QString::number(info.port));
}
child->setText(4, QString::number(info.id));
if (info.vncPort > 0) {
child->setText(6, QString::number(info.vncPort));
}
if (info.rdpPort > 0) {
child->setText(7, QString::number(info.rdpPort));
}
item->addChild(child);
}
void ConnectManagerUI::upadateChileItem(QTreeWidgetItem *item,
ConnectInfo info) {
item->setText(0, info.name);
item->setIcon(0, QIcon(":/icons/server.png"));
item->setText(1, info.username);
item->setText(2, info.hostName);
item->setText(3, QString::number(info.port));
item->setText(4, QString::number(info.id));
}
ConnectInfo ConnectManagerUI::getConnectInfo(QTreeWidgetItem *item) {
int id = item->text(4).toInt();
ConnectInfo info = ConnectDao::GetInstance()->getConnectInfo(id);
return info;
}
void ConnectManagerUI::refreshTreeWidget() {
treeView->clear();
this->connectInfos = ConnectDao::GetInstance()->getConnectInfos(0);
for (ConnectInfo info : this->connectInfos) {
rootItem = new QTreeWidgetItem(treeView);
rootItem->setText(0, info.name);
rootItem->setIcon(0, QIcon(":/icons/folder.png"));
rootItem->setText(4, QString::number(info.id));
refreshList(rootItem);
}
}
ConnectManagerUI::~ConnectManagerUI() {}