Repository navigation
Expand file tree
/
Copy pathconnectmanagerui.cpp
More file actions
1056 lines (959 loc) · 36.2 KB
/
Copy pathconnectmanagerui.cpp
File metadata and controls
1056 lines (959 loc) · 36.2 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "connectmanagerui.h"
#include <QAbstractItemView>
#include <QAction>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLayout>
#include <QSizePolicy>
#include <QHeaderView>
#include <QLabel>
#include <QLineEdit>
#include <QMenu>
#include <QMovie>
#include <QToolButton>
#include <QVBoxLayout>
#include "connecteditui.h"
#include "db/connectdao.h"
#include "sftpdialog.h"
#include "ui_connectmanagerui.h"
#include "components/messagedialog.h"
namespace {
// 侧边栏分组行的操作列;完整资产管理窗口不显示该列。
const int kGroupActionColumn = 9;
bool confirmDelete(QWidget *parent, const QString &title,
const QString &text) {
return MessageDialog::confirm(parent, title, text, QStringLiteral("删除"),
QStringLiteral("取消"), true);
}
// 侧边栏操作栏使用流式布局:按钮按可用宽度自动换行,宽度足够时保持一行。
class FlowLayout : public QLayout {
public:
explicit FlowLayout(QWidget *parent = nullptr, int horizontalSpacing = 2,
int verticalSpacing = 2)
: QLayout(parent),
hSpace(horizontalSpacing),
vSpace(verticalSpacing) {}
~FlowLayout() override {
while (QLayoutItem *item = takeAt(0)) {
delete item;
}
}
void addItem(QLayoutItem *item) override { items.append(item); }
int count() const override { return items.size(); }
QLayoutItem *itemAt(int index) const override {
return items.value(index);
}
QLayoutItem *takeAt(int index) override {
if (index < 0 || index >= items.size()) {
return nullptr;
}
return items.takeAt(index);
}
Qt::Orientations expandingDirections() const override { return {}; }
bool hasHeightForWidth() const override { return true; }
int heightForWidth(int width) const override {
return doLayout(QRect(0, 0, width, 0), true);
}
void setGeometry(const QRect &rect) override {
QLayout::setGeometry(rect);
doLayout(rect, false);
}
QSize sizeHint() const override {
QSize size;
int width = 0;
for (QLayoutItem *item : items) {
const QSize itemSize = item->sizeHint();
size = size.expandedTo(itemSize);
width += itemSize.width() + hSpace;
}
if (!items.isEmpty()) {
width -= hSpace;
}
size.setWidth(width);
return size;
}
QSize minimumSize() const override {
QSize size;
for (QLayoutItem *item : items) {
size = size.expandedTo(item->minimumSize());
}
return size;
}
private:
int doLayout(const QRect &rect, bool testOnly) const {
int x = rect.x();
int y = rect.y();
int lineHeight = 0;
const int right = rect.x() + rect.width();
for (QLayoutItem *item : items) {
const QSize hint = item->sizeHint();
if (x + hint.width() > right && lineHeight > 0) {
x = rect.x();
y += lineHeight + vSpace;
lineHeight = 0;
}
if (!testOnly) {
item->setGeometry(QRect(QPoint(x, y), hint));
}
x += hint.width() + hSpace;
lineHeight = qMax(lineHeight, hint.height());
}
return y + lineHeight - rect.y();
}
QList<QLayoutItem *> items;
int hSpace = 2;
int vSpace = 2;
};
// 带流式布局的操作栏容器:父布局按 heightForWidth 给它分配所需高度。
class FlowBar : public QWidget {
public:
explicit FlowBar(QWidget *parent = nullptr)
: QWidget(parent), flow(new FlowLayout(this)) {}
FlowLayout *flowLayout() const { return flow; }
bool hasHeightForWidth() const override { return true; }
int heightForWidth(int width) const override {
return flow->heightForWidth(width);
}
QSize sizeHint() const override { return flow->sizeHint(); }
QSize minimumSizeHint() const override { return flow->minimumSize(); }
private:
FlowLayout *flow;
};
QToolButton *createSidebarButton(const QString &iconPath,
const QString &tooltip, QWidget *parent) {
QToolButton *button = new QToolButton(parent);
button->setObjectName(QStringLiteral("sidebarIconButton"));
button->setIcon(QIcon(iconPath));
button->setIconSize(QSize(18, 18));
button->setToolTip(tooltip);
button->setAutoRaise(true);
button->setCursor(Qt::PointingHandCursor);
button->setFixedSize(28, 28);
return button;
}
QString assetToolTip(const ConnectInfo &info) {
QString tip = info.name;
if (!info.username.isEmpty()) {
tip += QStringLiteral("\n%1@%2").arg(info.username, info.hostName);
} else if (!info.hostName.isEmpty()) {
tip += QStringLiteral("\n") + info.hostName;
}
if (info.port > 0 && !info.hostName.isEmpty()) {
tip += QStringLiteral(":%1").arg(info.port);
}
if (!info.serialPortName.isEmpty()) {
tip += QStringLiteral("\n串口:%1(%2)")
.arg(info.serialPortName)
.arg(info.serialBaudRate);
}
return tip;
}
// 分组在数据库里用 parent_id=0 表示;再排除带有连接字段的顶层资源,
// 确保右侧的“新增连接”按钮只出现在真正的分组上。
bool isGroupInfo(const ConnectInfo &info) {
return info.parentId == 0 && info.hostName.trimmed().isEmpty() &&
info.serialPortName.trimmed().isEmpty() &&
info.username.trimmed().isEmpty();
}
// 只配置了串口的资产用终端图标,避免在树里看起来像一台 SSH 服务器。
QIcon assetIcon(const ConnectInfo &info) {
if (info.hostName.trimmed().isEmpty() &&
!info.serialPortName.trimmed().isEmpty()) {
return QIcon(QStringLiteral(":/icons/console.png"));
}
return QIcon(QStringLiteral(":/icons/server.png"));
}
// 分组行右侧的小按钮:比标题栏按钮更紧凑,避免把组名挤窄。
QToolButton *createRowActionButton(const QString &iconPath,
const QString &tooltip, QWidget *parent) {
QToolButton *button = new QToolButton(parent);
button->setObjectName(QStringLiteral("assetRowAction"));
button->setIcon(QIcon(iconPath));
button->setIconSize(QSize(16, 16));
button->setToolTip(tooltip);
button->setAccessibleName(tooltip);
button->setAutoRaise(true);
button->setCursor(Qt::PointingHandCursor);
button->setFixedSize(24, 24);
return button;
}
// 资产/分组的直接操作按钮:侧边栏用纯图标,完整窗口用图标 + 文字。
QToolButton *createActionButton(const QString &text, const QString &iconPath,
bool compact, QWidget *parent) {
QToolButton *button = new QToolButton(parent);
button->setObjectName(QStringLiteral("assetActionButton"));
button->setText(text);
button->setIcon(QIcon(iconPath));
button->setIconSize(QSize(16, 16));
button->setToolTip(text);
button->setAutoRaise(true);
button->setCursor(Qt::PointingHandCursor);
if (compact) {
button->setToolButtonStyle(Qt::ToolButtonIconOnly);
button->setFixedSize(30, 30);
} else {
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
button->setMinimumHeight(30);
}
return button;
}
QToolButton *createQuickAction(const QString &text, const QString &iconPath,
QWidget *parent) {
QToolButton *button = new QToolButton(parent);
button->setObjectName(QStringLiteral("quickAction"));
button->setText(text);
button->setIcon(QIcon(iconPath));
button->setIconSize(QSize(30, 30));
button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
button->setCursor(Qt::PointingHandCursor);
button->setFixedSize(118, 82);
return button;
}
} // namespace
ConnectManagerUI::ConnectManagerUI(QWidget *parent, Mode mode)
: QWidget(parent), mode_(mode)
// ui(new Ui::ConnectManagerUI)
{
// ui->setupUi(this);
// setWindowTitle("连接管理器");
// setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
// setFixedSize(600, 300);
backupAndRecoveryDialog = new BackupAndRecoveryDialog(this);
connect(backupAndRecoveryDialog, &QDialog::finished, this,
[this](int) { refreshTreeWidget(); });
const bool sidebarMode = (mode_ == Mode::Sidebar);
QVBoxLayout *rootlayout = new QVBoxLayout(this);
if (sidebarMode) {
setObjectName(QStringLiteral("assetSidebar"));
rootlayout->setContentsMargins(12, 10, 12, 12);
rootlayout->setSpacing(8);
QHBoxLayout *headerLayout = new QHBoxLayout;
headerLayout->setContentsMargins(0, 0, 0, 0);
headerLayout->setSpacing(2);
QLabel *titleLabel = new QLabel(tr("资产"), this);
titleLabel->setObjectName(QStringLiteral("sidebarTitle"));
headerLayout->addWidget(titleLabel);
headerLayout->addStretch(1);
QToolButton *newGroupButton = createSidebarButton(
QStringLiteral(":/icons/folder.png"), tr("新建组"), this);
connect(newGroupButton, &QToolButton::clicked, this,
[this]() { createNewGroup(); });
QToolButton *backupButton = createSidebarButton(
QStringLiteral(":/icons/backup_recovery.png"), tr("备份/恢复"), this);
connect(backupButton, &QToolButton::clicked, this,
[this]() { backupAndRecoveryDialog->show(); });
QToolButton *refreshButton = createSidebarButton(
QStringLiteral(":/icons/refresh.png"), tr("刷新"), this);
connect(refreshButton, &QToolButton::clicked, this,
[this]() { refreshTreeWidget(); });
headerLayout->addWidget(newGroupButton);
headerLayout->addWidget(backupButton);
headerLayout->addWidget(refreshButton);
rootlayout->addLayout(headerLayout);
searchEdit_ = new QLineEdit(this);
searchEdit_->setObjectName(QStringLiteral("assetSearch"));
searchEdit_->setPlaceholderText(tr("搜索名称、用户、主机或串口..."));
searchEdit_->setClearButtonEnabled(true);
rootlayout->addWidget(searchEdit_);
connect(searchEdit_, &QLineEdit::textChanged, this,
[this](const QString &text) { applyFilter(text); });
setupActionBar(rootlayout);
} else {
rootlayout->setContentsMargins(18, 16, 18, 16);
rootlayout->setSpacing(12);
QLabel *titleLabel = new QLabel(tr("资产管理"), this);
titleLabel->setObjectName(QStringLiteral("pageTitle"));
QLabel *subtitleLabel = new QLabel(
tr("双击打开连接;选中资产后可用上方操作栏,右键菜单保留全部操作"),
this);
subtitleLabel->setObjectName(QStringLiteral("pageSubtitle"));
rootlayout->addWidget(titleLabel);
rootlayout->addWidget(subtitleLabel);
QHBoxLayout *hlayout = new QHBoxLayout;
hlayout->setContentsMargins(0, 4, 0, 4);
hlayout->setSpacing(12);
QToolButton *newGroupButton =
createQuickAction(tr("新建组"), QStringLiteral(":/icons/folder.png"),
this);
connect(newGroupButton, &QToolButton::clicked, this,
[this]() { createNewGroup(); });
QToolButton *backupButton = createQuickAction(
tr("备份/恢复"), QStringLiteral(":/icons/backup_recovery.png"), this);
connect(backupButton, &QToolButton::clicked, this,
[this]() { backupAndRecoveryDialog->show(); });
QToolButton *refreshButton = createQuickAction(
tr("刷新"), QStringLiteral(":/icons/refresh.png"), this);
connect(refreshButton, &QToolButton::clicked, this,
[this]() { refreshTreeWidget(); });
hlayout->addWidget(newGroupButton);
hlayout->addWidget(backupButton);
hlayout->addWidget(refreshButton);
hlayout->addStretch(1);
rootlayout->addLayout(hlayout);
setupActionBar(rootlayout);
}
treeView = new QTreeWidget(this);
treeView->setObjectName(QStringLiteral("assetTree"));
treeView->setAlternatingRowColors(true);
treeView->setUniformRowHeights(true);
treeView->setAnimated(true);
treeView->setIconSize(QSize(20, 20));
treeView->setIndentation(sidebarMode ? 14 : 18);
treeView->setSelectionMode(QAbstractItemView::SingleSelection);
treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
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,
[this](QTreeWidgetItem *item, int) { openItemConnection(item); });
connect(treeView, &QTreeWidget::itemActivated,
[&](QTreeWidgetItem *item, int column) { qDebug() << "回车"; });
connect(treeView, SIGNAL(customContextMenuRequested(const QPoint &)), this,
SLOT(popMenu(const QPoint &)));
connect(treeView, &QTreeWidget::currentItemChanged, this,
[this](QTreeWidgetItem *, QTreeWidgetItem *) {
updateActionBar();
});
// 侧边栏多一列放分组行右侧的“新增连接”按钮;完整窗口仍用 9 列。
treeView->setColumnCount(sidebarMode ? 10 : 9);
if (sidebarMode) {
treeView->setHeaderHidden(true);
treeView->setRootIsDecorated(true);
for (int column = 1; column < kGroupActionColumn; ++column) {
treeView->hideColumn(column);
}
// 名称列吃掉剩余宽度,操作列固定在右侧,窗口缩放时按钮始终贴边。
// 必须先关掉“拉伸最后一列”,否则最后一列会抢走宽度,名称列被压窄。
treeView->header()->setStretchLastSection(false);
treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
treeView->header()->setSectionResizeMode(kGroupActionColumn,
QHeaderView::Fixed);
treeView->setColumnWidth(kGroupActionColumn, 32);
} else {
treeView->hideColumn(4);
treeView->hideColumn(5);
treeView->setHeaderLabels(QStringList() << "名称"
<< "用户名"
<< "Host"
<< "SSH端口"
<< "id"
<< "parent_id"
<< "VNC端口"
<< "RDP端口"
<< "串口");
treeView->setColumnWidth(0, 200);
treeView->setColumnWidth(1, 150);
treeView->header()->setStretchLastSection(true);
}
rootlayout->addWidget(treeView, 1);
if (sidebarMode) {
QLabel *hint = new QLabel(tr("双击打开 · 选中后用操作栏或右键菜单"), this);
hint->setObjectName(QStringLiteral("sidebarHint"));
rootlayout->addWidget(hint);
}
this->refreshTreeWidget();
updateActionBar();
}
void ConnectManagerUI::setupActionBar(QVBoxLayout *layout) {
const bool compact = (mode_ == Mode::Sidebar);
QWidget *bar = compact ? static_cast<QWidget *>(new FlowBar(this))
: new QWidget(this);
bar->setObjectName(QStringLiteral("assetActionBar"));
FlowLayout *flowLayout = nullptr;
QGridLayout *gridLayout = nullptr;
if (compact) {
flowLayout = static_cast<FlowBar *>(bar)->flowLayout();
bar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
QSizePolicy policy = bar->sizePolicy();
policy.setHeightForWidth(true);
bar->setSizePolicy(policy);
} else {
gridLayout = new QGridLayout(bar);
gridLayout->setContentsMargins(0, 0, 0, 0);
gridLayout->setHorizontalSpacing(6);
gridLayout->setVerticalSpacing(2);
}
// 资产常用入口:SSH / VNC / RDP / 文件管理 / 编辑 / 删除 / 复制。
sshAction_ = createActionButton(
tr("SSH"), QStringLiteral(":/icons/console.png"), compact, bar);
vncAction_ = createActionButton(
tr("VNC"), QStringLiteral(":/icons/desktop.png"), compact, bar);
#ifdef Q_OS_LINUX
rdpAction_ = createActionButton(
tr("RDP"), QStringLiteral(":/icons/server.png"), compact, bar);
#endif
fileAction_ = createActionButton(
tr("文件管理"), QStringLiteral(":/icons/folder.png"), compact, bar);
editAction_ = createActionButton(
tr("编辑"), QStringLiteral(":/icons/settings.png"), compact, bar);
deleteAction_ = createActionButton(
tr("删除"), QStringLiteral(":/icons/delete.png"), compact, bar);
deleteAction_->setProperty("danger", true);
copyAction_ = createActionButton(
tr("复制"), QStringLiteral(":/icons/copy.png"), compact, bar);
moreAction_ = createActionButton(
tr("更多"), QStringLiteral(":/icons/chevron-down.png"), compact, bar);
QList<QToolButton *> actions;
actions << sshAction_ << vncAction_;
#ifdef Q_OS_LINUX
actions << rdpAction_;
#endif
actions << fileAction_ << editAction_ << deleteAction_ << copyAction_;
if (compact) {
// 按侧边栏实际宽度自动换行:宽时一行,窄时折成多行。
for (QToolButton *button : actions) {
flowLayout->addWidget(button);
}
flowLayout->addWidget(moreAction_);
} else {
int column = 0;
for (QToolButton *button : actions) {
gridLayout->addWidget(button, 0, column++);
}
gridLayout->setColumnStretch(column, 1);
gridLayout->addWidget(moreAction_, 0, column + 1);
}
connect(sshAction_, &QToolButton::clicked, this,
[this]() { openItemSsh(currentTreeItem()); });
connect(vncAction_, &QToolButton::clicked, this,
[this]() { openItemVnc(currentTreeItem()); });
#ifdef Q_OS_LINUX
connect(rdpAction_, &QToolButton::clicked, this,
[this]() { openItemRdp(currentTreeItem()); });
#endif
connect(fileAction_, &QToolButton::clicked, this,
[this]() { openItemFileManager(currentTreeItem()); });
connect(editAction_, &QToolButton::clicked, this,
[this]() { editItem(currentTreeItem()); });
connect(deleteAction_, &QToolButton::clicked, this,
[this]() { deleteItem(currentTreeItem()); });
connect(copyAction_, &QToolButton::clicked, this,
[this]() { copyItem(currentTreeItem()); });
connect(moreAction_, &QToolButton::clicked, this, [this]() {
QTreeWidgetItem *item = currentTreeItem();
if (!item || !moreAction_) {
return;
}
showItemMenu(item,
moreAction_->mapToGlobal(QPoint(0, moreAction_->height() + 2)));
});
layout->addWidget(bar);
}
QTreeWidgetItem *ConnectManagerUI::currentTreeItem() const {
return treeView ? treeView->currentItem() : nullptr;
}
void ConnectManagerUI::updateActionBar() {
if (!sshAction_) {
return;
}
QTreeWidgetItem *item = currentTreeItem();
const bool hasItem = item != nullptr;
bool asset = false;
if (hasItem) {
const ConnectInfo info = getConnectInfo(item);
asset = !isGroupInfo(info);
}
sshAction_->setEnabled(asset);
#ifdef WITH_LIBVNCSERVER
vncAction_->setEnabled(asset);
#else
vncAction_->setEnabled(false);
#endif
#if defined(Q_OS_LINUX) && defined(WITH_FREERDP)
rdpAction_->setEnabled(asset);
#elif defined(Q_OS_LINUX)
rdpAction_->setEnabled(false);
#endif
fileAction_->setEnabled(asset);
editAction_->setEnabled(hasItem);
deleteAction_->setEnabled(hasItem);
copyAction_->setEnabled(asset);
moreAction_->setEnabled(hasItem);
sshAction_->setToolTip(tr("打开 SSH 连接"));
#ifdef WITH_LIBVNCSERVER
vncAction_->setToolTip(tr("打开 VNC 连接"));
#else
vncAction_->setToolTip(tr("当前构建未启用 VNC 支持"));
#endif
#if defined(Q_OS_LINUX) && defined(WITH_FREERDP)
rdpAction_->setToolTip(tr("打开 RDP 连接"));
#elif defined(Q_OS_LINUX)
rdpAction_->setToolTip(tr("当前构建未启用 RDP 支持"));
#endif
fileAction_->setToolTip(tr("打开文件管理"));
editAction_->setToolTip(asset ? tr("编辑连接") : tr("重命名分组"));
deleteAction_->setToolTip(tr("删除"));
copyAction_->setToolTip(tr("复制资产"));
moreAction_->setToolTip(tr("更多操作"));
}
void ConnectManagerUI::openItemConnection(QTreeWidgetItem *item) {
if (!item) {
return;
}
const ConnectInfo info = getConnectInfo(item);
if (isGroupInfo(info)) {
return;
}
if (info.hostName.trimmed().isEmpty() &&
!info.serialPortName.trimmed().isEmpty()) {
emit openSerialConnect(info);
} else {
emit openSSHConnect(info);
}
}
void ConnectManagerUI::openItemSsh(QTreeWidgetItem *item) {
if (!item || isGroupInfo(getConnectInfo(item))) {
return;
}
emit openSSHConnect(getConnectInfo(item));
}
void ConnectManagerUI::openItemVnc(QTreeWidgetItem *item) {
if (!item || isGroupInfo(getConnectInfo(item))) {
return;
}
emit openVNCConnect(getConnectInfo(item));
}
void ConnectManagerUI::openItemRdp(QTreeWidgetItem *item) {
if (!item || isGroupInfo(getConnectInfo(item))) {
return;
}
emit openRDPConnect(getConnectInfo(item));
}
void ConnectManagerUI::openItemFileManager(QTreeWidgetItem *item) {
if (!item) {
return;
}
const ConnectInfo info = getConnectInfo(item);
if (isGroupInfo(info)) {
return;
}
emit openSFTPConnect(info);
}
void ConnectManagerUI::editItem(QTreeWidgetItem *item) {
if (!item) {
return;
}
const ConnectInfo info = getConnectInfo(item);
const int itemId = info.id;
if (isGroupInfo(info)) {
ConfirmDialog *dialog = new ConfirmDialog(this, tr("请输入分组名称"));
dialog->show();
connect(dialog, &ConfirmDialog::confirmEditEvent, this,
[this, itemId](QString input) {
QTreeWidgetItem *target = nullptr;
for (int i = 0; i < treeView->topLevelItemCount(); ++i) {
QTreeWidgetItem *candidate = treeView->topLevelItem(i);
if (candidate->text(4).toInt() == itemId) {
target = candidate;
break;
}
}
if (target) {
updateConnectName(target, itemId, input);
}
});
return;
}
ConnectEditUI *editor = new ConnectEditUI(this, item->text(0) + tr("-编辑"));
editor->setConnectInfo(info);
editor->show();
connect(editor, &ConnectEditUI::updateSuccess, this,
[this, itemId](ConnectInfo updated) {
QTreeWidgetItem *target = nullptr;
for (int i = 0; i < treeView->topLevelItemCount(); ++i) {
QTreeWidgetItem *group = treeView->topLevelItem(i);
for (int j = 0; j < group->childCount(); ++j) {
if (group->child(j)->text(4).toInt() == itemId) {
target = group->child(j);
break;
}
}
if (target) {
break;
}
}
if (target) {
upadateChileItem(target, updated);
} else {
refreshTreeWidget();
}
});
}
void ConnectManagerUI::deleteItem(QTreeWidgetItem *item) {
if (!item) {
return;
}
const ConnectInfo info = getConnectInfo(item);
if (!isGroupInfo(info)) {
QString text = tr("确定要删除资源“%1”吗?").arg(info.name);
if (!info.hostName.isEmpty()) {
QString host = info.username.isEmpty()
? info.hostName
: QStringLiteral("%1@%2").arg(info.username,
info.hostName);
if (info.port > 0) {
host += QStringLiteral(":%1").arg(info.port);
}
text += QStringLiteral("\n\n") + host;
}
text += QStringLiteral("\n\n") + tr("该操作不可恢复。");
if (!confirmDelete(this, tr("删除资源"), text)) {
return;
}
if (!ConnectDao::GetInstance()->deleteById(info.id)) {
MessageDialog::warning(this, tr("删除失败"),
tr("无法删除资源,请稍后重试。"));
return;
}
delete item;
} else {
const QString text =
tr("确定要删除分组“%1”及其下的所有资源吗?\n\n"
"该操作不可恢复。")
.arg(info.name);
if (!confirmDelete(this, tr("删除分组"), text)) {
return;
}
bool ok = ConnectDao::GetInstance()->deleteByParentId(info.id);
ok = ConnectDao::GetInstance()->deleteById(info.id) && ok;
if (!ok) {
MessageDialog::warning(this, tr("删除失败"),
tr("无法删除分组,请稍后重试。"));
refreshTreeWidget();
return;
}
delete item;
}
updateActionBar();
}
void ConnectManagerUI::copyItem(QTreeWidgetItem *item) {
if (!item) {
return;
}
const ConnectInfo info = getConnectInfo(item);
if (isGroupInfo(info)) {
return;
}
copyInfo = info;
}
void ConnectManagerUI::addGroupAction(QTreeWidgetItem *groupItem) {
if (mode_ != Mode::Sidebar || !groupItem) {
return;
}
QWidget *host = new QWidget(treeView);
host->setObjectName(QStringLiteral("assetGroupActionHost"));
QHBoxLayout *layout = new QHBoxLayout(host);
layout->setContentsMargins(0, 0, 4, 0);
layout->setSpacing(0);
layout->addStretch(1);
QToolButton *button = createRowActionButton(
QStringLiteral(":/icons/add.png"), tr("新增连接"), host);
connect(button, &QToolButton::clicked, this,
[this, groupItem]() { addConnectionToGroup(groupItem); });
layout->addWidget(button);
treeView->setItemWidget(groupItem, kGroupActionColumn, host);
}
void ConnectManagerUI::addConnectionToGroup(QTreeWidgetItem *groupItem) {
if (!groupItem) {
return;
}
// 新增连接沿用它所属分组的 id 作为 parentId,保存时由 ConnectEditUI
// 走“追加子项”的分支。
const ConnectInfo groupInfo = getConnectInfo(groupItem);
const int groupId = groupInfo.id;
ConnectEditUI *editor = new ConnectEditUI(this, tr("新增连接"));
editor->setConnectInfo(groupInfo);
editor->show();
// 对话框是非模态的,期间树可能被刷新(分组节点会重建)。这里按分组 id
// 找回当前节点,避免持有已经 delete 的 QTreeWidgetItem 指针。
connect(editor, &ConnectEditUI::addSuccess, this,
[this, groupId](ConnectInfo info) {
for (int i = 0; i < treeView->topLevelItemCount(); ++i) {
QTreeWidgetItem *group = treeView->topLevelItem(i);
if (group->text(4).toInt() != groupId) {
continue;
}
addChileItem(group, info);
treeView->expandItem(group);
return;
}
refreshTreeWidget();
});
}
void ConnectManagerUI::createNewGroup() {
// 先让用户输入组名,确认后再插入,避免直接生成一堆“新建文件夹”。
auto *dialog =
new ConfirmDialog(this, tr("请输入组名"), tr("新建"), tr("取消"));
dialog->setAttribute(Qt::WA_DeleteOnClose);
// 校验失败时只提示,不关闭输入框,用户可以接着改。
dialog->setInputValidator([dialog](const QString &input) {
if (input.trimmed().isEmpty()) {
MessageDialog::warning(dialog, tr("提示"), tr("组名不能为空"));
return false;
}
return true;
});
dialog->show();
connect(dialog, &ConfirmDialog::confirmEditEvent, this,
[this](QString input) {
const QString groupName = input.trimmed();
if (groupName.isEmpty()) {
return;
}
const int id =
ConnectDao::GetInstance()->addConnectInfo(groupName);
if (id <= 1) {
return;
}
auto *groupItem = new QTreeWidgetItem(treeView);
groupItem->setText(0, groupName);
groupItem->setIcon(0, QIcon(":/icons/folder.png"));
groupItem->setText(4, QString::number(id));
groupItem->setToolTip(0, groupName);
addGroupAction(groupItem);
treeView->setCurrentItem(groupItem);
treeView->scrollToItem(groupItem);
updateActionBar();
});
}
void ConnectManagerUI::createNewServer() {
MessageDialog::information(nullptr, tr("单击"), tr("创建服务器"));
}
void ConnectManagerUI::popMenu(const QPoint &p) {
// 右键菜单总是作用于鼠标下的节点,而不是上一次选中的节点。
QTreeWidgetItem *item = treeView->itemAt(p);
if (!item) {
return; // 在空白位置右击
}
treeView->setCurrentItem(item);
showItemMenu(item, QCursor::pos());
}
void ConnectManagerUI::showItemMenu(QTreeWidgetItem *curItem,
const QPoint &globalPos) {
if (!curItem) {
return;
}
treeView->setCurrentItem(curItem);
const QModelIndex modelIndex = treeView->currentIndex();
QMenu menu(treeView);
const int id = curItem->text(4).toInt();
const ConnectInfo info = ConnectDao::GetInstance()->getConnectInfo(id);
const bool group = isGroupInfo(info);
if (group) {
QAction *refreshAction = new QAction("刷新", this);
connect(refreshAction, &QAction::triggered,
[this, modelIndex]() { treeView->expand(modelIndex); });
menu.addAction(refreshAction);
QAction *addconnect = new QAction("新增连接", this); //新增连接
connect(addconnect, &QAction::triggered, this,
[this, curItem]() { addConnectionToGroup(curItem); });
menu.addAction(addconnect);
} else {
QAction *connectWell = new QAction("SSH连接", this); //连接
connect(connectWell, &QAction::triggered,
[this, info]() { emit openSSHConnect(info); });
menu.addAction(connectWell);
if (!info.serialPortName.trimmed().isEmpty()) {
QAction *connectSerial = new QAction("串口连接", this);
connect(connectSerial, &QAction::triggered,
[this, info]() { emit openSerialConnect(info); });
menu.addAction(connectSerial);
}
#ifdef WITH_LIBVNCSERVER
QAction *connectVNC = new QAction("VNC连接", this);
connect(connectVNC, &QAction::triggered,
[this, info]() { emit openVNCConnect(info); });
menu.addAction(connectVNC);
#endif
#if defined(Q_OS_LINUX) && defined(WITH_FREERDP)
QAction *connectRDP = new QAction("RDP连接", this);
connect(connectRDP, &QAction::triggered,
[this, info]() { emit openRDPConnect(info); });
menu.addAction(connectRDP);
#endif
QAction *sftpWell = new QAction("文件管理", this);
connect(sftpWell, &QAction::triggered,
[this, info]() { emit openSFTPConnect(info); });
menu.addAction(sftpWell);
}
QAction *editWell = new QAction("编辑", this);
connect(editWell, &QAction::triggered, this,
[this, curItem]() { editItem(curItem); });
menu.addAction(editWell);
QAction *deleteWell = new QAction("删除", this); //删除
connect(deleteWell, &QAction::triggered, this,
[this, curItem]() { deleteItem(curItem); });
menu.addAction(deleteWell);
if (!group) {
QAction *copyAction = new QAction("复制", this);
connect(copyAction, &QAction::triggered, this,
[this, curItem]() { copyItem(curItem); });
menu.addAction(copyAction);
}
if (group && copyInfo.id > 0) {
QAction *pasteAction = new QAction("黏贴", this);
connect(pasteAction, &QAction::triggered, this,
[this, curItem, modelIndex]() {
copyInfo.parentId = curItem->text(4).toInt();
bool result = ConnectDao::GetInstance()->addConnectInfo(©Info);
if (result) {
addChileItem(curItem, this->copyInfo);
copyInfo = ConnectInfo();
treeView->expand(modelIndex);
}
});
menu.addAction(pasteAction);
}
menu.exec(globalPos); //在当前鼠标位置或“更多”按钮下方显示
}
void ConnectManagerUI::updateConnectName(QTreeWidgetItem *item, int id,
QString name) {
bool result = ConnectDao::GetInstance()->updateConnectName(id, name);
if (!result) {
MessageDialog::warning(this, tr("提示"), tr("名称修改失败"));
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, assetIcon(info));
// 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, assetIcon(info));
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));
}
if (!info.serialPortName.isEmpty()) {
child->setText(8, info.serialPortName);
}
child->setToolTip(0, assetToolTip(info));
item->addChild(child);
}
void ConnectManagerUI::upadateChileItem(QTreeWidgetItem *item,
ConnectInfo info) {
item->setText(0, info.name);
item->setIcon(0, assetIcon(info));
item->setText(1, info.username);
item->setText(2, info.hostName);
item->setText(3, QString::number(info.port));
item->setText(4, QString::number(info.id));
item->setText(8, info.serialPortName);
item->setToolTip(0, assetToolTip(info));
}
ConnectInfo ConnectManagerUI::getConnectInfo(QTreeWidgetItem *item) {
int id = item->text(4).toInt();
ConnectInfo info = ConnectDao::GetInstance()->getConnectInfo(id);
return info;
}
void ConnectManagerUI::refreshAssets() { refreshTreeWidget(); }
void ConnectManagerUI::applyFilter(const QString &text) {
const QString needle = text.trimmed();
for (int i = 0; i < treeView->topLevelItemCount(); ++i) {
filterItem(treeView->topLevelItem(i), needle);
}
}
bool ConnectManagerUI::filterItem(QTreeWidgetItem *item,
const QString &needle) {
if (needle.isEmpty()) {
item->setHidden(false);
if (treeView->columnCount() > kGroupActionColumn) {
if (QWidget *action = treeView->itemWidget(item, kGroupActionColumn)) {
action->setVisible(true);
}
}
for (int i = 0; i < item->childCount(); ++i) {
filterItem(item->child(i), needle);
}