Fixed bug 5709. Now result dialog shows only two decimals in time labels.
[speedfreak] / Client / registerdialog.cpp
1 #include "registerdialog.h"
2 #include "ui_registerdialog.h"
3 #include <QMessageBox>
4 #include <QDebug>
5 #include <QString>
6 #include <QFileDialog>
7 #include <QImage>
8 #include <QPixmap>
9 #include <QSize>
10 #include "usersettings.h"
11 #include "settingsdialog.h"
12 #include "xmlreader.h"
13
14 /**
15   * Constructor of this class.
16   */
17 RegisterDialog::RegisterDialog(QWidget *parent) :
18     QDialog(parent), ui(new Ui::RegisterDialog)
19 {
20     ui->setupUi(this);
21     this->setWindowTitle("Register new user");
22     this->ui->regEMailLineEdit->setText("@");
23     // User info label
24     ui->labelInfoToUser->setVisible(0);
25     ui->labelInfoToUser->setText("");
26     // Button
27     imageButtonState = false;
28     manufacturer="";
29     type="";
30     model="";
31     description="";
32     picture="";
33
34     ui->buttonImage->setVisible(false);
35     ui->labelImage->setVisible(false);
36     // Read user profile xml
37     /*QString filename = "/home/user/MyDocs/speedfreak/profile/" + parent->getUserName() + "_profile.xml";
38     QFile file(filename);
39
40     if (!file.open(QFile::ReadOnly))
41     {
42         qDebug() << "_xmlRead fail";
43         return;
44     }
45     else
46     {
47         xmlReader = new XmlReader();
48         xmlReader->xmlReadProfile(&file,this);
49     }
50     file.close();*/
51 }
52
53 /**
54   * Destructor of this class.
55   */
56 RegisterDialog::~RegisterDialog()
57 {
58     delete ui;
59 }
60
61 /**
62   *
63   */
64 void RegisterDialog::changeEvent(QEvent *e)
65 {
66     QDialog::changeEvent(e);
67     switch (e->type()) {
68     case QEvent::LanguageChange:
69         ui->retranslateUi(this);
70         break;
71     default:
72         break;
73     }
74 }
75
76 /**
77   * This function is used to clear lineEdits and close this dialog.
78   */
79 void RegisterDialog::clearRegisterLineEdits()
80 {
81     ui->regEMailLineEdit->setText("@");
82     ui->regPasswordLineEdit->setText("");
83     ui->regUserNameLineEdit->setText("");
84     ui->lineEditManufacturer->setText("");
85     ui->lineEditModel->setText("");
86     ui->lineEditType->setText("");
87     ui->textEditDescription->setText("");
88
89     this->close();
90 }
91
92 // Next 6 functions can be removed if Settingsdialog is implemented without
93 // own copy of username, password & email
94 /**
95   *
96   */
97 void RegisterDialog::setRegUserName(QString username)
98 {
99     this->regUsername = username;
100 }
101
102 /**
103   *
104   */
105 void RegisterDialog::setRegPassword(QString password)
106 {
107     this->regPassword = password;
108 }
109
110 /**
111   *
112   */
113 void RegisterDialog::setRegEmail(QString email)
114 {
115     this->regEmail = email;
116 }
117
118 /**
119   *
120   */
121 QString RegisterDialog::getRegUserName()
122 {
123     return this->regUsername;
124 }
125
126 /**
127   *
128   */
129 QString RegisterDialog::getRegPassword()
130 {
131     return this->regPassword;
132 }
133
134 /**
135   *
136   */
137 QString RegisterDialog::getRegEmail()
138 {
139     return this->regEmail;
140 }
141
142 void RegisterDialog::on_registratePushButton_clicked()
143 {
144     // Save labels data
145     setManufacturer(ui->lineEditManufacturer->text());
146     setType(ui->lineEditType->text());
147     setModel(ui->lineEditModel->text());
148     setDescription(ui->textEditDescription->toPlainText());
149
150     // emit settingsdialog --> mainwindow --> httpclient
151     //emit saveprofile();
152
153
154
155     // Send username, password and email to SpeedFreak server
156     this->regUsername = ui->regUserNameLineEdit->text();
157     this->regPassword = ui->regPasswordLineEdit->text();
158     this->regEmail = ui->regEMailLineEdit->text();
159
160     if (this->regUsername.compare("") && this->regPassword.compare("") && this->regEmail.compare("") && this->regEmail.compare("@"))
161     {
162         emit registrate();
163     }
164     else
165     {
166         QMessageBox::about(this, "One or more of the fields is empty", "Set username (3-12 characters), password (at least 6 characters) and valid email address");
167     }
168
169     //close();      //using close() hides popup-window which reports error from server
170 }
171
172 /**
173   * This get function return manufacturer
174   * @return QString
175   */
176 QString RegisterDialog::getManufacturer()
177 {
178     return manufacturer;
179 }
180
181 /**
182   * This get function return type
183   * @return QString
184   */
185 QString RegisterDialog::getType()
186 {
187     return type;
188 }
189
190 /**
191   * This get function return model
192   * @return QString
193   */
194 QString RegisterDialog::getModel()
195 {
196     return model;
197 }
198
199 /**
200   * This get function return description
201   * @return QString
202   */
203 QString RegisterDialog::getDescription()
204 {
205     QString all = manufacturer + ";" + type + ";" + model + ";" + description;
206     return all;
207 }
208
209 /**
210   * This get function return description
211   * @return QString
212   */
213 QString RegisterDialog::getPicture()
214 {
215     return picture;
216 }
217
218 /**
219   * This set function set manufacturer
220   * @param QString
221   */
222 void RegisterDialog::setManufacturer(QString m)
223 {
224     manufacturer = m;
225     ui->lineEditManufacturer->setText(m);
226 }
227
228 /**
229   * This set function set type
230   * @param QString
231   */
232 void RegisterDialog::setType(QString t)
233 {
234     type = t;
235     ui->lineEditType->setText(t);
236 }
237
238 /**
239   * This set function set model
240   * @param QString
241   */
242 void RegisterDialog::setModel(QString m)
243 {
244     model = m;
245     ui->lineEditModel->setText(m);
246 }
247
248 /**
249   * This set function set description
250   * @param QString
251   */
252 void RegisterDialog::setDescription(QString d)
253 {
254     description = d;
255     ui->textEditDescription->setText(d);
256 }
257
258 /**
259   * This set function set description
260   * @param QString
261   */
262 void RegisterDialog::setPicture(QString p)
263 {
264     picture = p;
265     loadPicture(p);
266 }
267
268 /**
269   * This function set label info text to user
270   * @param QString
271   */
272 void RegisterDialog::setLabelInfoToUser(QString infoText)
273 {
274     ui->labelInfoToUser->setVisible(1);
275     this->ui->labelInfoToUser->setText(infoText);
276 }
277
278 /**
279   * This slot function called when image button clicked.
280   */
281 void RegisterDialog::on_buttonImage_clicked()
282 {
283     if (imageButtonState == false)
284     {
285         ui->buttonImage->setText("Load image");
286     }
287     else
288     {
289         ui->buttonImage->setText("Change image");
290     }
291
292     QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
293     loadPicture(fileName);
294 }
295
296 /**
297   * This function load picture.
298   * @param QString
299   */
300 void RegisterDialog::loadPicture(QString fileName)
301 {
302     if (!fileName.isEmpty())
303     {
304         QImage image(fileName);
305         if (image.isNull())
306         {
307             QMessageBox::information(this, tr("Profile"),tr("Cannot load %1.").arg(fileName));
308             return;
309         }
310         ui->labelImage->setPixmap(QPixmap::fromImage(image.scaled(QSize(120,120), Qt::KeepAspectRatio)));
311         picture = fileName;
312     }
313 }