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(){
68 * Function create header and content fields
73 void MainWindow::createTextLayout(){
74 mainGridlayout = new QGridLayout();
75 dtime = QDateTime::currentDateTime();
76 strDate = dtime.toString("dd.MM.yyyy hh:mm");
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);
88 mainGridlayout->addLayout(textLayout,0,0);
92 * Function create layout for coordinate field
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);
106 lonCoordData = new QLineEdit(longitudeString.text());
107 lonCoordData->setMaximumWidth(250);
110 accLabel = new QLabel("acc:",this);;
111 accData = new QLineEdit(QString::number(0));
112 accData->setMaximumWidth(250);
115 getCoordButton = new QPushButton(tr("Get location"),this);
116 getCoordButton->setMaximumWidth(220);
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);
127 connect(getCoordButton,SIGNAL(clicked()),this,SLOT(getCoordinates()));
129 mainGridlayout->addLayout(coordGridLayout,0,1);
133 * Function create buttons
138 void MainWindow::createButtons(){
139 buttonLayout = new QHBoxLayout();
140 QString strText[MAX_BUTTONS] = {tr("Save"),
144 for(int i = 0; i < MAX_BUTTONS; i++){
145 buttons[i] = new QPushButton(strText[i],this);
146 buttonLayout->addWidget(buttons[i]);
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()));
156 * Function clear all text fields
161 void MainWindow::clearTextFields(){
162 headerText->setText("");
163 textContent->setText("");
168 * Function get coordinates from GPS
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());
185 * Function create actions
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);
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);
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()));
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()));
214 * Function create menus
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);
229 * Function create toolbar
234 void MainWindow::createToolBars(){
236 toolBar = this->addToolBar(tr("ToolBar"));
237 toolBar->addAction(saveIconAction);
238 toolBar->addAction(clearIconAction);
239 toolBar->addAction(aboutIconAction);
240 toolBar->addAction(exitIconAction);
250 void MainWindow::showAbout(){
251 QMessageBox::about(this,tr("About"),tr("Hiking Diary. <a href=www.redneck-wanderers.com>Redneck-Wanderers</a>"));
255 * Function saves data to the text file
260 bool MainWindow::saveData(){
262 //xmlparser->writeXmlData(headerText->text(),date, textContent->toPlainText(),latitudeString.text(),longitudeString.text());
267 QString diaryPath = "/home/user";
268 QDir dataDir(diaryPath);
269 dataDir.setPath(diaryPath);
271 if(!dataDir.exists(diaryPath)){
272 qDebug() << "The path is" << dataDir.path();
273 dataDir.mkpath("hikingdiary");
276 fileNameString = "/home/user/diaryContent.xml";
278 QFile file(fileNameString);
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()));
287 QXmlStreamWriter stream( &file );
288 stream.setAutoFormatting(true);
289 stream.writeStartDocument();
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());
301 //stream.writeTextElement("title", "Qt Home");
302 stream.writeStartElement("content");
303 stream.writeTextElement("content", textContent->toPlainText());
305 stream.writeEndElement(); // content
306 stream.writeEndElement(); // position
307 stream.writeEndElement(); // date
308 stream.writeEndElement(); // header
311 stream.writeEndDocument();
318 statusBar()->showMessage(tr("File saved"), 2000);
324 bool MainWindow::writeXml(){