VPTissue Reference Manual
ServerDialog.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "ServerDialog.h"
21 
22 #include "gui/qtmodel/PTreeModel.h"
23 #include "gui/PTreeView.h"
25 #include "util/misc/StringUtils.h"
27 
28 #include <QHBoxLayout>
29 #include <QVBoxLayout>
30 #include <QLabel>
31 #include <QPushButton>
32 #include <QLineEdit>
33 #include <QMessageBox>
34 #include <QFormLayout>
35 
36 namespace SimPT_Parex {
37 
38 using namespace std;
40 using boost::property_tree::ptree;
41 using namespace boost::property_tree::xml_parser;
42 
43 ServerDialog::ServerDialog(std::string path):
44  m_path(path), m_current_server(nullptr), m_last_server(nullptr)
45 {
46  setWindowTitle("Servers");
47 
48  QVBoxLayout* layout = new QVBoxLayout;
49 
50  layout->addWidget(new QLabel("Servers:"));
51  m_server_list = new QComboBox;
52  layout->addWidget(m_server_list);
53 
54  QFormLayout* form_layout = new QFormLayout;
55  form_layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
56 
57  m_name = new QLineEdit;
58  m_address = new QLineEdit;
59  m_port = new QLineEdit;
60 
61  form_layout->addRow(tr("&Name"), m_name);
62  form_layout->addRow(tr("&Address"), m_address);
63  form_layout->addRow(tr("&Port"), m_port);
64 
65  QVBoxLayout* button_layout = new QVBoxLayout;
66 
67  QHBoxLayout* button_top = new QHBoxLayout;
68  QHBoxLayout* button_bottom = new QHBoxLayout;
69 
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);
76 
77  QPushButton* button_cancel = new QPushButton("Cancel");
78  button_bottom->addStretch();
79  button_bottom->addWidget(button_cancel);
80  button_bottom->addStretch();
81 
82  button_layout->addLayout(button_top);
83  button_layout->addLayout(button_bottom);
84 
85  layout->addLayout(form_layout);
86  layout->addLayout(button_layout);
87 
88  setLayout(layout);
89 
90  connect(m_server_list, SIGNAL(currentIndexChanged(int)), this, SLOT(SelectionChanged(int)));
91 
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()));
96 
97  LoadServerList();
98 }
99 
101 {
102  delete m_name;
103  delete m_address;
104  delete m_port;
105  m_servers.clear();
106  delete m_current_server;
107  delete m_last_server;
108  delete m_server_list;
109 
110 }
111 
112 void ServerDialog::Connect()
113 {
114  m_current_server = new ServerInfo(m_name->text(), m_address->text(), m_port->text().toInt());
115  SaveServerList(true);
116  accept();
117 }
118 
120 {
121  return m_current_server;
122 }
123 
124 void ServerDialog::UpdateName(const QString name)
125 {
126  m_name->setText(name);
127 }
128 
129 void ServerDialog::UpdateAddress(const QString address)
130 {
131  m_address->setText(address);
132 }
133 
134 void ServerDialog::UpdatePort(const QString port)
135 {
136  m_port->setText(port);
137 }
138 
139 void ServerDialog::SelectionChanged(int index)
140 {
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;
146 }
147 
148 void ServerDialog::LoadServerList()
149 {
150  try {
151  ptree input;
152  read_xml(m_path + "/serverList.xml", input);
153 
154  const std::string NOTFOUND = ")_NOT_FOUND_(";
155  int size = input.get<int>("number");
156  if (size == 0 && input.get("last", NOTFOUND) == NOTFOUND)
157  return;
158 
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")));
165  }
166 
167  for (auto server : m_servers)
168  m_server_list->addItem(server->GetName());
169 
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");
176  }
177 
178  m_server_list->setCurrentIndex(m_servers.size()-1);
179  SelectionChanged(m_servers.size()-1);
180  }
181  catch(xml_parser_error &e) {
182 
183  }
184 }
185 
186 void ServerDialog::SaveServerList(bool last)
187 {
188  ptree output;
189  int i = 0;
190  int count = 0;
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());
198  count++;
199  }
200  i++;
201  }
202  output.add("number", count);
203 
204  ServerInfo* last_server = nullptr;
205 
206  if (last && m_current_server != nullptr) {
207  last_server = m_current_server;
208  }
209  else if (m_last_server != nullptr){
210  last_server = m_last_server;
211  }
212 
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());
217  }
218 
219  write_xml(m_path + "/serverList.xml", output, std::locale(), XmlWriterSettings::GetTab());
220 }
221 
222 void ServerDialog::SaveServer()
223 {
224  if (m_current_server != nullptr && m_current_server->GetName() == m_name->text()) { // Update values
225  m_current_server->Update(m_address->text(), m_port->text().toInt());
226  }
227  else { // Insert new server
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.",
230  QMessageBox::Ok);
231  return;
232  }
233 
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);
238  }
239  SaveServerList(false);
240 }
241 
242 void ServerDialog::DeleteServer()
243 {
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);
249 }
250 
251 } // namespace
Interface for ServerInfo.
STL namespace.
virtual ~ServerDialog()
Destructor.
Settings for xml output of Property Trees (ptree).
QString GetName()
Return the name of the server.
Definition: ServerInfo.cpp:44
void Update(QString address, int port)
Update values for server.
Definition: ServerInfo.cpp:38
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.
Definition: ServerInfo.h:29
Interface for PTreeView.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
Interface for ServerDialog.
Interface for PTreeModel.
Xml writer settings class.