bb6369a77e4fe8ff7adb72cbda4ca4aae3b91cb0
[hikingdiary] / src / mainwindow.cpp
1 /**
2 * @mainwindow.cpp
3 * Tähän tekstiä luokan/koodin yleisestä toiminnasta…
4 * Copyright (c) Ari Iivari 2009
5 * @version 1.0
6 */
7
8 #include "mainwindow.h"
9 //#include "location.cpp"
10
11 //#include <Q3VBoxLayout>
12
13 struct diaryContent{
14     QString eDateTime;
15     QString eHeader;
16     QString eText;
17     QString elat;
18     QString elon;
19 };
20
21 diaryContent diaryArray[100];
22
23 /**
24 * Constructor
25 *
26 * @param -
27 * @return -
28 */
29 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
30
31     this->createActions();
32     this->createToolBars();
33     this->createMenus();
34     this->createTextLayout();
35     this->createCoordLayout();
36     this->createButtons();
37
38     //Päänäkymän layout
39     QVBoxLayout* mainLayout = new QVBoxLayout();
40
41     //Nest the layouts
42     mainLayout->addLayout(mainGridlayout);
43     mainLayout->addLayout(buttonLayout);
44
45     location = new Maemo5Location(this);
46     parser = new xmlParser();
47     manager = new diaryDataManager();
48     manager->printData();
49
50     this->setCentralWidget(new QWidget());
51     this->centralWidget()->setLayout(mainLayout);
52 }
53
54 /**
55 * MainWindows destructor.
56 *
57 * @param -
58 * @return -
59 */
60 MainWindow::~MainWindow(){
61     if (xmlparser)
62         delete xmlparser;
63     if (manager)
64         delete manager;
65 }
66
67 /**
68 * Function create header and content fields
69 *
70 * @param -
71 * @return -
72 */
73 void MainWindow::createTextLayout(){
74     mainGridlayout = new QGridLayout();
75     dtime = QDateTime::currentDateTime();
76     strDate = dtime.toString("dd.MM.yyyy hh:mm");
77
78     textLayout = new QVBoxLayout();
79     dateLabel = new QLabel(strDate,this);
80     //dateLabel->setText("Now: %s",strDate);
81     textLayout->addWidget(dateLabel);
82     headerText = new QLineEdit(this);
83     headerText->setMaxLength(50);
84     textLayout->addWidget(headerText);;
85     textContent = new QTextEdit(this);
86     textLayout->addWidget(textContent);
87
88     mainGridlayout->addLayout(textLayout,0,0);
89 }
90
91 /**
92 * Function create layout for coordinate field
93 *
94 * @param -
95 * @return -
96 */
97 void MainWindow::createCoordLayout(){
98     latitudeString.setText("65");
99     longitudeString.setText("30");
100     coordGridLayout = new QGridLayout();
101     latCoordLabel = new QLabel("lat:",this);
102     lonCoordLabel = new QLabel("lon:",this);;
103     latCoordData = new QLineEdit(latitudeString.text());
104     latCoordData->setMaximumWidth(250);
105
106     lonCoordData = new QLineEdit(longitudeString.text());
107     lonCoordData->setMaximumWidth(250);
108
109     /*
110     accLabel = new QLabel("acc:",this);;
111     accData = new QLineEdit(QString::number(0));
112     accData->setMaximumWidth(250);
113     */
114
115     getCoordButton = new QPushButton(tr("Get location"),this);
116     getCoordButton->setMaximumWidth(220);
117
118     coordGridLayout->verticalSpacing();
119     coordGridLayout->addWidget(latCoordLabel,0,0,1,1,Qt::AlignRight);
120     coordGridLayout->addWidget(latCoordData,0,1,1,1,Qt::AlignLeft);
121     coordGridLayout->addWidget(lonCoordLabel,1,0,1,1,Qt::AlignRight);
122     coordGridLayout->addWidget(lonCoordData,1,1,1,1,Qt::AlignLeft);
123     //coordGridLayout->addWidget(accLabel,3,0,1,1,Qt::AlignRight);
124     //coordGridLayout->addWidget(accData,3,1,1,1,Qt::AlignLeft);
125     coordGridLayout->addWidget(getCoordButton,4,0,20,0,Qt::AlignTop);
126
127     connect(getCoordButton,SIGNAL(clicked()),this,SLOT(getCoordinates()));
128
129     mainGridlayout->addLayout(coordGridLayout,0,1);
130 }
131
132 /**
133 * Function create buttons
134 *
135 * @param -
136 * @return -
137 */
138 void MainWindow::createButtons(){
139     buttonLayout = new QHBoxLayout();
140     QString strText[MAX_BUTTONS] = {tr("Save"),
141                                     tr("Clear"),
142                                     tr("Exit")};
143
144     for(int i = 0; i < MAX_BUTTONS; i++){
145         buttons[i] = new QPushButton(strText[i],this);
146         buttonLayout->addWidget(buttons[i]);
147     }
148
149 //    connect(buttons[OPEN],SIGNAL(clicked()),this,SLOT(openApplication()));
150     connect(buttons[CLEAR],SIGNAL(clicked()),this,SLOT(clearTextFields()));
151     connect(buttons[EXIT],SIGNAL(clicked()),this,SLOT(close()));
152     connect(buttons[SAVE],SIGNAL(clicked()),this,SLOT(saveData()));
153 }
154
155 /**
156 * Function clear all text fields
157 *
158 * @param -
159 * @return -
160 */
161 void MainWindow::clearTextFields(){
162     headerText->setText("");
163     textContent->setText("");
164
165 }
166
167 /**
168 * Function get coordinates from GPS
169 *
170 * @param -
171 * @return -
172 */
173 void MainWindow::getCoordinates(){
174     //connect(location, SIGNAL(agnss()), this, SLOT(getCoordinates()));
175     latitudeString.setText(QString::number(location->getLatitude()));
176     longitudeString.setText(QString::number(location->getLongitude()));
177     //latitudeString = "3434458";
178     //longitudeString = "7384738";
179     latCoordData->setText(latitudeString.text());
180     lonCoordData->setText(longitudeString.text());
181 }
182
183
184 /**
185 * Function create actions
186 *
187 * @param -
188 * @return -
189 */
190 void MainWindow::createActions(){
191     exitAction = new QAction(tr("E&xit"),this);
192     aboutAction = new QAction(tr("&About"),this);
193     clearAction = new QAction(tr("&Clear"),this);
194     saveAction = new QAction(tr("&Save"),this);
195     /*
196     aboutIconAction = new QAction(QIcon("about.png"),tr("About"),this);
197     saveIconAction = new QAction(QIcon("save.png"),tr("Save"),this);
198     clearIconAction = new QAction(QIcon("Pensseli.png"),tr("Clear"),this);
199     exitIconAction = new QAction(QIcon("exit.png"),tr("Exit"),this);
200     */
201     connect(exitAction,SIGNAL(triggered()),this,SLOT(close()));
202     connect(clearAction,SIGNAL(triggered()),this,SLOT(clearTextFields()));
203     connect(aboutAction,SIGNAL(triggered()),this,SLOT(showAbout()));
204     connect(saveAction,SIGNAL(triggered()),this,SLOT(saveData()));
205     /*
206     connect(aboutIconAction,SIGNAL(triggered()),this,SLOT(showAbout()));
207     connect(clearIconAction,SIGNAL(triggered()),this,SLOT(clearTextFields()));
208     connect(exitIconAction,SIGNAL(triggered()),this,SLOT(close()));
209     connect(saveIconAction,SIGNAL(triggered()),this,SLOT(saveData()));
210     */
211 }
212
213 /**
214 * Function create menus
215 *
216 * @param -
217 * @return -
218 */
219 void MainWindow::createMenus(){
220     //fileMenu = this->menuBar()->addMenu(tr("&File"));
221     //aboutMenu = this->menuBar()->addMenu(tr("&Help"));
222     menuBar()->addAction(exitAction);
223     menuBar()->addAction(clearAction);
224     menuBar()->addAction(saveAction);
225     menuBar()->addAction(aboutAction);
226 }
227
228 /**
229 * Function create toolbar
230 *
231 * @param -
232 * @return -
233 */
234 void MainWindow::createToolBars(){
235     /*
236     toolBar = this->addToolBar(tr("ToolBar"));
237     toolBar->addAction(saveIconAction);
238     toolBar->addAction(clearIconAction);
239     toolBar->addAction(aboutIconAction);
240     toolBar->addAction(exitIconAction);
241     */
242 }
243
244 /**
245 * About message
246 *
247 * @param -
248 * @return -
249 */
250 void MainWindow::showAbout(){
251     QMessageBox::about(this,tr("About"),tr("Hiking Diary. <a href=www.redneck-wanderers.com>Redneck-Wanderers</a>"));
252 }
253
254 /**
255 * Function saves data to the text file
256 *
257 * @param -
258 * @return -
259 */
260 bool MainWindow::saveData(){
261
262     //xmlparser->writeXmlData(headerText->text(),date, textContent->toPlainText(),latitudeString.text(),longitudeString.text());
263
264     //return true;
265
266     /*
267     QString diaryPath = "/home/user";
268     QDir dataDir(diaryPath);
269     dataDir.setPath(diaryPath);
270
271     if(!dataDir.exists(diaryPath)){
272         qDebug() << "The path is" << dataDir.path();
273         dataDir.mkpath("hikingdiary");
274     }
275
276     fileNameString = "/home/user/diaryContent.xml";
277
278     QFile file(fileNameString);
279
280     if(!file.open( QIODevice::ReadWrite | QIODevice::Append )){
281         QMessageBox::warning(this, tr("Hiking Diary"),
282                              tr("Cannot write file Error: %1.\n %2")
283                              .arg(file.fileName())
284                              .arg(file.errorString()));
285         return false;
286     }
287     QXmlStreamWriter stream( &file );
288     stream.setAutoFormatting(true);
289     stream.writeStartDocument();
290
291     stream.writeAttribute("version", "1.0");
292     stream.writeStartElement("header");
293     stream.writeAttribute("header", headerText->text());
294     stream.writeStartElement("date");
295     stream.writeAttribute("date", strDate);
296     stream.writeStartElement("position");
297     stream.writeAttribute("latitude", latCoordData->text());
298     stream.writeAttribute("longitude",lonCoordData->text());
299     //! [write element]
300
301     //stream.writeTextElement("title", "Qt Home");
302     stream.writeStartElement("content");
303     stream.writeTextElement("content", textContent->toPlainText());
304
305     stream.writeEndElement(); // content
306     stream.writeEndElement(); // position
307     stream.writeEndElement(); // date
308     stream.writeEndElement(); // header
309
310     //! [finish stream]
311     stream.writeEndDocument();
312
313     //! [finish stream]
314     //! [write output]
315     file.close();
316
317     */
318     statusBar()->showMessage(tr("File saved"), 2000);
319
320     clearTextFields();
321     return true;
322 }
323
324 bool MainWindow::writeXml(){
325     return true;
326 }