Added custom button for users dialog and changed place of www-button. Www-button...
[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     this->close();
150     // emit settingsdialog --> mainwindow --> httpclient
151     //emit saveprofile();
152 }
153
154 /**
155   * This get function return manufacturer
156   * @return QString
157   */
158 QString ProfileDialog::getManufacturer()
159 {
160     return manufacturer;
161 }
162
163 /**
164   * This get function return type
165   * @return QString
166   */
167 QString ProfileDialog::getType()
168 {
169     return type;
170 }
171
172 /**
173   * This get function return model
174   * @return QString
175   */
176 QString ProfileDialog::getModel()
177 {
178     return model;
179 }
180
181 /**
182   * This get function return description
183   * @return QString
184   */
185 QString ProfileDialog::getDescription()
186 {
187     return description;
188 }
189
190 /**
191   * This get function return description
192   * @return QString
193   */
194 QString ProfileDialog::getPicture()
195 {
196     return picture;
197 }
198
199 /**
200   * This set function set manufacturer
201   * @param QString
202   */
203 void ProfileDialog::setManufacturer(QString m)
204 {
205     manufacturer = m;
206     ui->lineEditManufacturer->setText(m);
207 }
208
209 /**
210   * This set function set type
211   * @param QString
212   */
213 void ProfileDialog::setType(QString t)
214 {
215     type = t;
216     ui->lineEditType->setText(t);
217 }
218
219 /**
220   * This set function set model
221   * @param QString
222   */
223 void ProfileDialog::setModel(QString m)
224 {
225     model = m;
226     ui->lineEditModel->setText(m);
227 }
228
229 /**
230   * This set function set description
231   * @param QString
232   */
233 void ProfileDialog::setDescription(QString d)
234 {
235     description = d;
236     ui->textEditDescription->setText(d);
237 }
238
239 /**
240   * This set function set description
241   * @param QString
242   */
243 void ProfileDialog::setPicture(QString p)
244 {
245     picture = p;
246     loadPicture(p);
247 }
248
249 /**
250   * This function set label info text to user
251   * @param QString
252   */
253 void ProfileDialog::setLabelInfoToUser(QString infoText)
254 {
255     ui->labelInfoToUser->setVisible(1);
256     this->ui->labelInfoToUser->setText(infoText);
257 }