-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverselectwindow.cpp
More file actions
155 lines (119 loc) · 4.52 KB
/
serverselectwindow.cpp
File metadata and controls
155 lines (119 loc) · 4.52 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
// serverselectwindow.cpp
#include "serverselectwindow.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QMessageBox>
#include <QInputDialog>
#include "global.h"
#include "setting.h"
#include "processdialog.h"
ServerSelectWindow::ServerSelectWindow(QWidget *parent) : QWidget(parent) {
setWindowTitle("数据安全柜 - 选择服务器");
setFixedSize(400, 200);
mainLayout = new QVBoxLayout(this);
// 服务器类型选择
serverTypeGroup = new QButtonGroup(this);
officialServerRadio = new QRadioButton("官方服务器", this);
customServerRadio = new QRadioButton("指定服务器", this);
serverTypeGroup->addButton(officialServerRadio, 0);
serverTypeGroup->addButton(customServerRadio, 1);
officialServerRadio->setChecked(true);
// 指定服务器选择
customServerCombo = new QComboBox(this);
customServerCombo->setEditable(true);
// 连接按钮
connectButton = new QPushButton("连接", this);
// 布局设置
mainLayout->addSpacing(10);
mainLayout->addWidget(officialServerRadio, 0, Qt::AlignHCenter);
mainLayout->addSpacing(24);
mainLayout->addWidget(customServerRadio, 0, Qt::AlignHCenter);
mainLayout->addWidget(customServerCombo);
customServerCombo->setVisible(false);
//QSpacerItem *spacer = new QSpacerItem(20, 0, QSizePolicy::Expanding, QSizePolicy::Expanding);
//mainLayout->addSpacerItem(spacer);
mainLayout->addStretch();
mainLayout->addWidget(connectButton, 0, Qt::AlignCenter);
mainLayout->addSpacing(20);
// 加载保存的服务器
loadSavedServers();
// 连接信号和槽
connect(officialServerRadio, &QRadioButton::toggled, this, &ServerSelectWindow::onServerTypeChanged);
connect(customServerRadio, &QRadioButton::toggled, this, &ServerSelectWindow::onServerTypeChanged);
connect(connectButton, &QPushButton::clicked, this, &ServerSelectWindow::onConnectButtonClicked);
// 初始化UI状态
onServerTypeChanged();
}
ServerSelectWindow::~ServerSelectWindow() = default;
void ServerSelectWindow::onServerTypeChanged() {
bool isCustom = customServerRadio->isChecked();
customServerCombo->setEnabled(isCustom);
customServerCombo->setVisible(isCustom);
// 优化显示效果
QFont font = customServerRadio->font();
if (isCustom) {
font.setWeight(QFont::Bold);
customServerRadio->setFont(font);
font.setWeight(QFont::Normal);
officialServerRadio->setFont(font);
} else {
font.setWeight(QFont::Bold);
officialServerRadio->setFont(font);
font.setWeight(QFont::Normal);
customServerRadio->setFont(font);
}
}
void ServerSelectWindow::onConnectButtonClicked() {
QString serverAddress;
if (officialServerRadio->isChecked()) {
// 获取官方服务器地址
/*QString selected = */
if (customServerRadio->isChecked()) {
serverAddress = QString(OFFICIAL_URL);
}
/*
// 获取自定义服务器地址
serverAddress = customServerEdit->text().trimmed();
if (serverAddress.isEmpty()) {
serverAddress = customServerCombo->currentText();
}*/
}
serverAddress = QString("https://test-sso.dianshudata.com/");
if (serverAddress.isEmpty()) {
QMessageBox::warning(this, "错误", "请选择或输入服务器地址");
return;
}
// 保存选中的服务器(如果是自定义服务器)
if (customServerRadio->isChecked() && !serverAddress.isEmpty()) {
saveServer(serverAddress);
}
emit serverSelected(serverAddress);
}
void ServerSelectWindow::loadSavedServers() {
settings.beginGroup("SavedServers");
QStringList serverList = settings.childKeys();
customServerCombo->clear();
for (const QString &key : serverList) {
QString address = settings.value(key).toString();
customServerCombo->addItem(address);
}
settings.endGroup();
}
void ServerSelectWindow::saveServer(const QString &serverAddress) {
if (serverAddress.isEmpty()) return;
settings.beginGroup("SavedServers");
// 检查是否已存在
QStringList serverList = settings.childKeys();
for (const QString &key : serverList) {
if (settings.value(key).toString() == serverAddress) {
settings.endGroup();
return;
}
}
// 保存新服务器
QString newKey = "server" + QString::number(serverList.size() + 1);
settings.setValue(newKey, serverAddress);
settings.endGroup();
// 更新UI
loadSavedServers();
}