82707cd805b44a46ab9cec6b4d24c73cf1d408bb
[speedfreak] / Client / profiledialog.cpp
1 /*
2  * Profile 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 <QString>
10 #include <QFileDialog>
11 #include <QImage>
12 #include <QMessageBox>
13 #include <QPixmap>
14 #include <QSize>
15 #include <QDebug>
16 #include "profiledialog.h"
17 #include "ui_profiledialog.h"
18 #include "usersettings.h"
19 #include "settingsdialog.h"
20 #include "xmlreader.h"
21
22 /**
23   * Constructor of this class.
24   * @param QWidget pointer to parent object. By default the value is NULL.
25   */
26 ProfileDialog::ProfileDialog(SettingsDialog *parent) : QDialog(parent), ui(new Ui::ProfileDialog)
27 {
28     qDebug() << "__ProfileDialog";
29     ui->setupUi(this);
30
31     if (loginSaved())
32     {
33         this->setWindowTitle("Profile - " + parent->getUserName());
34     }
35     else
36     {
37         this->setWindowTitle("Profile");
38     }
39
40     // User info label
41     ui->labelInfoToUser->setVisible(0);
42     ui->labelInfoToUser->setText("");
43
44     // Button
45     imageButtonState = false;
46
47     manufacturer="";
48     type="";
49     model="";
50     description="";
51     picture="";
52
53     // Read user profile xml
54     QString filename = "/home/user/MyDocs/speedfreak/profile/" + parent->getUserName() + "_profile.xml";
55     QFile file(filename);
56
57     if (!file.open(QFile::ReadOnly))
58     {
59         qDebug() << "_xmlRead fail";
60         return;
61     }
62     else
63     {
64         xmlReader = new XmlReader();
65         xmlReader->xmlReadProfile(&file,this);
66     }
67     file.close();
68 }
69
70 /**
71   * Destructor of this class. Deletes all dynamic objects and sets them to NULL.
72   */
73 ProfileDialog::~ProfileDialog()
74 {
75     qDebug() << "__~ProfileDialog";
76
77     if(ui)
78         delete ui;
79
80     if(xmlReader)
81         delete xmlReader;
82 }
83
84 /**
85   *
86   */
87 void ProfileDialog::changeEvent(QEvent *e)
88 {
89     QDialog::changeEvent(e);
90     switch (e->type()) {
91     case QEvent::LanguageChange:
92         ui->retranslateUi(this);
93         break;
94     default:
95         break;
96     }
97 }
98
99 /**
100   * This slot function called when image button clicked.
101   *
102   */
103 void ProfileDialog::on_buttonImage_clicked()
104 {
105     if (imageButtonState == false)
106     {
107         ui->buttonImage->setText("Load image");
108     }
109     else
110     {
111         ui->buttonImage->setText("Change image");
112     }
113
114     QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
115     loadPicture(fileName);
116 }
117
118 /**
119   * This function load picture.
120   * @param QString
121   */
122 void ProfileDialog::loadPicture(QString fileName)
123 {
124     if (!fileName.isEmpty())
125     {
126         QImage image(fileName);
127         if (image.isNull())
128         {
129             QMessageBox::information(this, tr("Profile"),tr("Cannot load %1.").arg(fileName));
130             return;
131         }
132         ui->labelImage->setPixmap(QPixmap::fromImage(image.scaled(QSize(120,120), Qt::KeepAspectRatio)));
133         picture = fileName;
134     }
135 }
136
137 /**
138   * This slot function called when save button clicked.
139   * @todo server connection
140   */
141 void ProfileDialog::on_buttonSave_clicked()
142 {
143     // Save labels data
144     setManufacturer(ui->lineEditManufacturer->text());
145     setType(ui->lineEditType->text());
146     setModel(ui->lineEditModel->text());
147     setDescription(ui->textEditDescription->toPlainText());
148
149     // emit settingsdialog --> mainwindow --> httpclient
150     emit saveprofile();
151 }
152
153 /**
154   * This get function return manufacturer
155   * @return QString
156   */
157 QString ProfileDialog::getManufacturer()
158 {
159     return manufacturer;
160 }
161
162 /**
163   * This get function return type
164   * @return QString
165   */
166 QString ProfileDialog::getType()
167 {
168     return type;
169 }
170
171 /**
172   * This get function return model
173   * @return QString
174   */
175 QString ProfileDialog::getModel()
176 {
177     return model;
178 }
179
180 /**
181   * This get function return description
182   * @return QString
183   */
184 QString ProfileDialog::getDescription()
185 {
186     return description;
187 }
188
189 /**
190   * This get function return description
191   * @return QString
192   */
193 QString ProfileDialog::getPicture()
194 {
195     return picture;
196 }
197
198 /**
199   * This set function set manufacturer
200   * @param QString
201   */
202 void ProfileDialog::setManufacturer(QString m)
203 {
204     manufacturer = m;
205     ui->lineEditManufacturer->setText(m);
206 }
207
208 /**
209   * This set function set type
210   * @param QString
211   */
212 void ProfileDialog::setType(QString t)
213 {
214     type = t;
215     ui->lineEditType->setText(t);
216 }
217
218 /**
219   * This set function set model
220   * @param QString
221   */
222 void ProfileDialog::setModel(QString m)
223 {
224     model = m;
225     ui->lineEditModel->setText(m);
226 }
227
228 /**
229   * This set function set description
230   * @param QString
231   */
232 void ProfileDialog::setDescription(QString d)
233 {
234     description = d;
235     ui->textEditDescription->setText(d);
236 }
237
238 /**
239   * This set function set description
240   * @param QString
241   */
242 void ProfileDialog::setPicture(QString p)
243 {
244     picture = p;
245     loadPicture(p);
246 }
247
248 /**
249   * This function set label info text to user
250   * @param QString
251   */
252 void ProfileDialog::setLabelInfoToUser(QString infoText)
253 {
254     ui->labelInfoToUser->setVisible(1);
255     this->ui->labelInfoToUser->setText(infoText);
256 }