28 #include <QHBoxLayout>
29 #include <QVBoxLayout>
31 #include <QPushButton>
33 #include <QMessageBox>
34 #include <QFormLayout>
40 using boost::property_tree::ptree;
44 m_path(path), m_current_server(nullptr), m_last_server(nullptr)
46 setWindowTitle(
"Servers");
48 QVBoxLayout* layout =
new QVBoxLayout;
50 layout->addWidget(
new QLabel(
"Servers:"));
51 m_server_list =
new QComboBox;
52 layout->addWidget(m_server_list);
54 QFormLayout* form_layout =
new QFormLayout;
55 form_layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
57 m_name =
new QLineEdit;
58 m_address =
new QLineEdit;
59 m_port =
new QLineEdit;
61 form_layout->addRow(tr(
"&Name"), m_name);
62 form_layout->addRow(tr(
"&Address"), m_address);
63 form_layout->addRow(tr(
"&Port"), m_port);
65 QVBoxLayout* button_layout =
new QVBoxLayout;
67 QHBoxLayout* button_top =
new QHBoxLayout;
68 QHBoxLayout* button_bottom =
new QHBoxLayout;
70 QPushButton* button_connect =
new QPushButton(
"Connect");
71 QPushButton* button_save =
new QPushButton(
"Save Server");
72 QPushButton* button_delete =
new QPushButton(
"Delete server");
73 button_top->addWidget(button_connect);
74 button_top->addWidget(button_save);
75 button_top->addWidget(button_delete);
77 QPushButton* button_cancel =
new QPushButton(
"Cancel");
78 button_bottom->addStretch();
79 button_bottom->addWidget(button_cancel);
80 button_bottom->addStretch();
82 button_layout->addLayout(button_top);
83 button_layout->addLayout(button_bottom);
85 layout->addLayout(form_layout);
86 layout->addLayout(button_layout);
90 connect(m_server_list, SIGNAL(currentIndexChanged(
int)),
this, SLOT(SelectionChanged(
int)));
92 connect(button_connect, SIGNAL(clicked()),
this, SLOT(Connect()));
93 connect(button_save, SIGNAL(clicked()),
this, SLOT(SaveServer()));
94 connect(button_delete, SIGNAL(clicked()),
this, SLOT(DeleteServer()));
95 connect(button_cancel, SIGNAL(clicked()),
this, SLOT(reject()));
106 delete m_current_server;
107 delete m_last_server;
108 delete m_server_list;
112 void ServerDialog::Connect()
114 m_current_server =
new ServerInfo(m_name->text(), m_address->text(), m_port->text().toInt());
115 SaveServerList(
true);
121 return m_current_server;
124 void ServerDialog::UpdateName(
const QString name)
126 m_name->setText(name);
129 void ServerDialog::UpdateAddress(
const QString address)
131 m_address->setText(address);
134 void ServerDialog::UpdatePort(
const QString port)
136 m_port->setText(port);
139 void ServerDialog::SelectionChanged(
int index)
141 auto server = m_servers[index];
142 m_name->setText(server->GetName());
143 m_address->setText(server->GetAddress());
144 m_port->setText(QString::number(server->GetPort()));
145 m_current_server = server;
148 void ServerDialog::LoadServerList()
152 read_xml(m_path +
"/serverList.xml", input);
154 const std::string NOTFOUND =
")_NOT_FOUND_(";
155 int size = input.get<
int>(
"number");
156 if (size == 0 && input.get(
"last", NOTFOUND) == NOTFOUND)
159 for (
int i = 0; i < size; i++) {
160 std::string temp =
"server";
161 temp.append(to_string(i));
162 m_servers.push_back(
new ServerInfo(input.get<std::string>(temp +
".name"),
163 input.get<std::string>(temp +
".address"),
164 input.get<
int>(temp +
".port")));
167 for (
auto server : m_servers)
168 m_server_list->addItem(server->GetName());
170 if (input.get(
"last", NOTFOUND) != NOTFOUND) {
171 m_servers.push_back(
new ServerInfo(input.get<std::string>(
"last.name"),
172 input.get<std::string>(
"last.address"),
173 input.get<
int>(
"last.port")));
174 m_last_server = m_servers[m_servers.size()-1];
175 m_server_list->addItem(
"Most recently connected");
178 m_server_list->setCurrentIndex(m_servers.size()-1);
179 SelectionChanged(m_servers.size()-1);
181 catch(xml_parser_error &e) {
186 void ServerDialog::SaveServerList(
bool last)
191 for (
auto server : m_servers) {
192 if (m_server_list->itemText(i).toStdString() !=
"Most recently connected"){
193 std::string temp =
"server";
194 temp.append(to_string(count));
195 output.add(temp +
".name", server->GetName().toStdString());
196 output.add(temp +
".address", server->GetAddress().toStdString());
197 output.add(temp +
".port", server->GetPort());
202 output.add(
"number", count);
204 ServerInfo* last_server =
nullptr;
206 if (last && m_current_server !=
nullptr) {
207 last_server = m_current_server;
209 else if (m_last_server !=
nullptr){
210 last_server = m_last_server;
213 if (last_server !=
nullptr) {
214 output.add(
"last.name", last_server->GetName().toStdString());
215 output.add(
"last.address", last_server->GetAddress().toStdString());
216 output.add(
"last.port", last_server->GetPort());
219 write_xml(m_path +
"/serverList.xml", output, std::locale(), XmlWriterSettings::GetTab());
222 void ServerDialog::SaveServer()
224 if (m_current_server !=
nullptr && m_current_server->
GetName() == m_name->text()) {
225 m_current_server->
Update(m_address->text(), m_port->text().toInt());
228 if (m_name->text().toStdString() ==
"Most recently connected") {
229 QMessageBox::critical(
this,
"Server Error",
"The given name may not be used for a server.",
234 ServerInfo* server =
new ServerInfo(m_name->text(), m_address->text(), m_port->text().toInt());
235 m_servers.push_back(server);
236 m_server_list->addItem(server->GetName());
237 m_server_list->setCurrentIndex(m_servers.size()-1);
239 SaveServerList(
false);
242 void ServerDialog::DeleteServer()
244 int index = m_server_list->currentIndex();
245 m_servers.erase(m_servers.begin() + index);
246 m_server_list->removeItem(index);
247 SelectionChanged(index % m_servers.size());
248 SaveServerList(
false);
Interface for ServerInfo.
virtual ~ServerDialog()
Destructor.
Settings for xml output of Property Trees (ptree).
QString GetName()
Return the name of the server.
void Update(QString address, int port)
Update values for server.
ServerDialog(std::string path)
Constructor.
String manipulation utilities.
ServerInfo * GetServer()
Return the server to connect to.
Class for storing server info used on client-side.
Namespace for SimPT parameter explorer package.
Interface for ServerDialog.
Interface for PTreeModel.
Xml writer settings class.