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