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