3 * Tähän tekstiä luokan/koodin yleisestä toiminnasta…
4 * Copyright (c) Ari Iivari 2009
8 #include "mainwindow.h"
9 //#include "location.cpp"
11 //#include <Q3VBoxLayout>
21 diaryContent diaryArray[100];
29 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
31 this->createActions();
32 this->createToolBars();
34 this->createTextLayout();
35 this->createCoordLayout();
36 this->createButtons();
39 QVBoxLayout* mainLayout = new QVBoxLayout();
42 mainLayout->addLayout(mainGridlayout);
43 mainLayout->addLayout(buttonLayout);
45 location = new Maemo5Location(this);
46 //parser = new xmlParser();
47 manager = new diaryDataManager();
50 this->setCentralWidget(new QWidget());
51 this->centralWidget()->setLayout(mainLayout);
55 * MainWindows destructor.
60 MainWindow::~MainWindow(){
69 * Function create header and content fields
74 void MainWindow::createTextLayout(){
75 mainGridlayout = new QGridLayout();
76 dtime = QDateTime::currentDateTime();
77 strDate = dtime.toString("dd.MM.yyyy hh:mm");
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);
89 mainGridlayout->addLayout(textLayout,0,0);
93 * Function create layout for coordinate field
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);
107 lonCoordData = new QLineEdit(longitudeString.text());
108 lonCoordData->setMaximumWidth(250);
111 accLabel = new QLabel("acc:",this);;
112 accData = new QLineEdit(QString::number(0));
113 accData->setMaximumWidth(250);
116 getCoordButton = new QPushButton(tr("Get location"),this);
117 getCoordButton->setMaximumWidth(220);
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);
128 connect(getCoordButton,SIGNAL(clicked()),this,SLOT(getCoordinates()));
130 mainGridlayout->addLayout(coordGridLayout,0,1);
134 * Function create buttons
139 void MainWindow::createButtons(){
140 buttonLayout = new QHBoxLayout();
141 QString strText[MAX_BUTTONS] = {tr("Save"),
145 for(int i = 0; i < MAX_BUTTONS; i++){
146 buttons[i] = new QPushButton(strText[i],this);
147 buttonLayout->addWidget(buttons[i]);
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()));
157 * Function clear all text fields
162 void MainWindow::clearTextFields(){
163 headerText->setText("");
164 textContent->setText("");
169 * Function get coordinates from GPS
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());
186 * Function create actions
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);
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);
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()));
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()));
215 * Function create menus
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);
230 * Function create toolbar
235 void MainWindow::createToolBars(){
237 toolBar = this->addToolBar(tr("ToolBar"));
238 toolBar->addAction(saveIconAction);
239 toolBar->addAction(clearIconAction);
240 toolBar->addAction(aboutIconAction);
241 toolBar->addAction(exitIconAction);
251 void MainWindow::showAbout(){
252 QMessageBox::about(this,tr("About"),tr("Hiking Diary. <a href=www.redneck-wanderers.com>Redneck-Wanderers</a>"));
256 * Function saves data to the text file
261 bool MainWindow::saveData(){
263 //xmlparser->writeXmlData(headerText->text(),date, textContent->toPlainText(),latitudeString.text(),longitudeString.text());
268 QString diaryPath = "/home/user";
269 QDir dataDir(diaryPath);
270 dataDir.setPath(diaryPath);
272 if(!dataDir.exists(diaryPath)){
273 qDebug() << "The path is" << dataDir.path();
274 dataDir.mkpath("hikingdiary");
277 fileNameString = "/home/user/diaryContent.xml";
279 QFile file(fileNameString);
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()));
288 QXmlStreamWriter stream( &file );
289 stream.setAutoFormatting(true);
290 stream.writeStartDocument();
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());
302 //stream.writeTextElement("title", "Qt Home");
303 stream.writeStartElement("content");
304 stream.writeTextElement("content", textContent->toPlainText());
306 stream.writeEndElement(); // content
307 stream.writeEndElement(); // position
308 stream.writeEndElement(); // date
309 stream.writeEndElement(); // header
312 stream.writeEndDocument();
319 statusBar()->showMessage(tr("File saved"), 2000);
325 bool MainWindow::writeXml(){