/
mainwindow.cpp
469 lines (400 loc) · 13.5 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*!
* Copyright 2008-2012 Charles Amey
*
* This file is part of Fuel Manager.
*
* Fuel Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fuel Manager is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fuel Manager. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <QSettings>
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
#include <QDialog>
#include <QTableView>
#include <QDesktopWidget>
#include <QSqlDatabase>
#include <QIcon>
#include "configure.h"
#include "selectvehicle.h"
#include "entryform.h"
#include "about.h"
#include "alldata.h"
#include "login.h"
#include "statistics.h"
//#include "assistant.h"
#include "mainwindow.h"
#include "editdate.h"
//#include "testwidget.h"
MainWindow::MainWindow(QMainWindow *parent)
:QMainWindow(parent)
{
mw.setupUi(this);
//set up the basic info for qsettings
QCoreApplication::setOrganizationName("fuelmanager");
//QCoreApplication::setOrganizationDomain("homelinux.com");
QCoreApplication::setApplicationName("fuelmanager");
//set the icon
//QString filename = "/usr/share/icons/hicolor/scalable/apps/fuelmanager.svg";
QString filename = ":fuelmanager.svg";
// QFileInfo iconFilename(filename);
// if (iconFilename.exists())
// {
// QIcon icon(filename);
// setWindowIcon(icon);
// }
//load the icon from the resources file
QIcon icon(filename);
setWindowIcon(icon);
//set the window's desired location
QDesktopWidget *desktop = QApplication::desktop();
int screenWidth, width;
int screenHeight, height;
int x, y;
QSize windowSize;
screenWidth = desktop->width(); // get width of screen
screenHeight = desktop->height(); // get height of screen
windowSize = size(); // size of our application window
width = windowSize.width();
height = windowSize.height();
// little computations
x = (screenWidth - width) / 2;
y = (screenHeight - height) / 2;
y -= 50;
// move window to desired coordinates
move ( x, y );
//set up the action and select vehicle buttons
mw.actionLogin->setEnabled(false);
mw.actionSelect_Vehicle->setEnabled(false);
//get the instructions tab
instructionsTab = mw.tabWidget->widget(0);
instructionsTab->setWindowTitle("Instructions");
//remove that tab
mw.tabWidget->removeTab(0);
//create the tabs
entry = new EntryForm();
alldata = new AllData();
stats = new Statistics();
//add all the possible databases
QSqlDatabase::addDatabase("QMYSQL","mariadb");
QSqlDatabase::addDatabase("QSQLITE","sqlite");
//check the settings
checkSettings();
setVehicleName();
updateInterface();
refreshAllData();
//assistant = new Assistant;
mw.actionLogin->setIcon(QIcon::fromTheme("format-join-node"));
mw.actionExport_Data->setIcon(QIcon::fromTheme("document-export-table"));
//mw.actionConfigure_Fuel_Manager->setIcon(QIcon::fromTheme("applications-system")); //big gear
mw.actionConfigure_Fuel_Manager->setIcon(QIcon::fromTheme("preferences-other")); //wrench with a gear
//mw.actionConfigure_Fuel_Manager->setIcon(QIcon::fromTheme("folder")); //
mw.actionHelp->setIcon(QIcon::fromTheme("help-contents"));
mw.actionAbout->setIcon(QIcon::fromTheme("help-about"));
}//end constructor
//destructor
MainWindow::~MainWindow()
{
//delete queries;
//because it spawns a qprocess, we gotta kill it
//delete assistant;
}
//configuration
void MainWindow::configure()
{
Configure *c = new Configure();
//c->show();
int returnVal = c->exec();
//qDebug()<<"what is the return value:"<<returnVal;
if (returnVal == 1)
{
checkSettings();
}
} //end configuration
void MainWindow::selectVehicle()
{
//qDebug()<<"show the select vehicle dialog box";
SelectVehicle *s = new SelectVehicle();
//s->show();
if (s->exec())
{
//qDebug()<<"now we refresh all the data since they chose a vehicle";
setVehicleName();
updateInterface();
refreshAllData();
}
}
void MainWindow::setVehicleName()
{
QSettings settings;
if (settings.contains("config/vehicle"))
{
int id = settings.value("config/vehicle").toInt();
QString dbaseType = settings.value("config/databasetype").toString();
QSqlDatabase db = QSqlDatabase::database(dbaseType,false);
if (db.open())
{
//qDebug()<<"setvehiclename db is open, and we need to get the desc";
Query q;
QString name = q.getVehicleDescription(id);
mw.vehicleLabel->setText(name);
}
}
}
//read the config file
void MainWindow::checkSettings()
{
QSettings settings;
if (settings.contains("config/databasetype"))
{
mw.actionSelect_Vehicle->setEnabled(true);
QString dbaseType = settings.value("config/databasetype").toString();
QSqlDatabase db = QSqlDatabase::database(dbaseType,false);
if (dbaseType == "sqlite")
{
mw.actionLogin->setEnabled(false);
//qDebug()<<"very beginning get dbase: "<<db.lastError().text();
QString location = settings.value("config/location").toString();
QString filename = settings.value("config/filename").toString();
db.setDatabaseName(location+"/"+filename);
db.open();
//create tables just in case
Query q;
q.createVehicleTable();
q.createMileageTable();
}
else if (dbaseType == "mariadb")
{
mw.actionLogin->setEnabled(true);
db.setHostName(settings.value("config/hostname").toString());
db.setDatabaseName(settings.value("config/database").toString());
db.setPort(settings.value("config/port").toInt());
if (!db.isOpen())
{
Login login;
if (login.exec())
{
db.setUserName(login.getUsername());
db.setPassword(login.getPassword());
if (!db.open())
{
QMessageBox mBox (QMessageBox::Critical, "Error opening database!",
db.lastError().text(), QMessageBox::Ok, this,Qt::Dialog);
mBox.exec();
}
else
{
Query q;
q.createVehicleTable();
q.createMileageTable();
}
}
else
{
QMessageBox mBox (QMessageBox::Critical, "No login",
"You didn't log in! Click Login to login.", QMessageBox::Ok, this,Qt::Dialog);
mBox.exec();
}
}
}
}//end settings contains a database type
}//end checkSettings
void MainWindow::updateInterface()
{
//hide all the tabs except for the instructions if they haven't selected a vehicle
QSettings settings;
QString dbaseType = settings.value("config/databasetype").toString();
QSqlDatabase db = QSqlDatabase::database(dbaseType);
if (!settings.contains("config/vehicle"))
{
//only show the instructions tab;
if (!db.isOpen())
mw.tabWidget->addTab(instructionsTab,instructionsTab->windowTitle());
}
else //hide the instructions tab
{
if (db.isOpen())
{
//get rid of all the tabs first
for (int i=0;i<mw.tabWidget->count();i++)
{
QWidget *t = mw.tabWidget->widget(i);
mw.tabWidget->removeTab(mw.tabWidget->indexOf(t));
}
//add all the tabs
mw.tabWidget->addTab(entry,entry->windowTitle());
mw.tabWidget->addTab(alldata,alldata->windowTitle());
mw.tabWidget->addTab(stats,stats->windowTitle());
}
}
}
void MainWindow::refreshAllData()
{
QSettings settings;
if(settings.contains("config/vehicle"))
{
QString dbaseType = settings.value("config/databasetype").toString();
QSqlDatabase db = QSqlDatabase::database(dbaseType,false);
if (db.isOpen())
{
int id = settings.value("config/vehicle").toInt();
//refresh the all data table
alldata->refreshTable(id);
//refresh the statistics
stats->refreshAllStats(id);
}
}
}
//show the about dialog
void MainWindow::about()
{
About *a = new About(this);
a->show();
}
void MainWindow::help()
{
}
void MainWindow::login()
{
QSettings settings;
QString dbaseType = settings.value("config/databasetype").toString();
QSqlDatabase db = QSqlDatabase::database(dbaseType,false);
Login login;
if (login.exec())
{
db.setUserName(login.getUsername());
db.setPassword(login.getPassword());
if (!db.open())
{
QMessageBox mBox (QMessageBox::Critical, "Error opening database!",
db.lastError().text(), QMessageBox::Ok, this,Qt::Dialog);
mBox.exec();
}
else
{
Query q;
q.createVehicleTable();
q.createMileageTable();
setVehicleName();
updateInterface();
refreshAllData();
}
}
else
{
QMessageBox mBox (QMessageBox::Critical, "No Login",
"You didn't log in! Click Login to login.", QMessageBox::Ok, this,Qt::Dialog);
mBox.exec();
}
}
//when tabs get clicked on, update the info on that particular tab
void MainWindow::updateAllData()
{
QSettings settings;
int id = settings.value("config/vehicle").toInt();
if(mw.tabWidget->currentIndex() == 1)
{
alldata->refreshTable(id);
}
if (mw.tabWidget->currentIndex() == 2)
{
stats->refreshAllStats(id);
}
} //end updateAllData
void MainWindow::exportData()
{
}
//void MainWindow::help()
//{
// assistant->showDocs("index.html");
//}
//
//
//void MainWindow::exportData()
//{
// //qDebug()<<"export the data for the currently selected vehicle";
// if (connection.isOpen())
// {
// QFileDialog dialog(this, tr("Save As..."));
// dialog.setFileMode(QFileDialog::AnyFile);
// dialog.setViewMode(QFileDialog::Detail);
// dialog.setDirectory(QDir::homePath()+"/");
// QStringList filename;
// if (dialog.exec())
// {
// filename = dialog.selectedFiles();
// if (filename[0] != "")
// {
// //qDebug()<<filename[0];
// if (QFile::exists(filename[0]))
// {
// QMessageBox message;
// message.setText(tr("File already exists!"));
// message.setInformativeText(tr("Overwrite?"));
// message.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
// message.setDefaultButton(QMessageBox::No);
// //int value = message.exec();
// if (message.exec() == QMessageBox::Yes)
// {
// //qDebug()<<"overwrite the file and write out the data";
// writeData(filename[0]);
// }
// }//if file exists
// else
// {
// //qDebug()<<"create file and write out the data";
// writeData(filename[0]);
// }
// }
// }//end dialog executed
// }
// else
// {
// QMessageBox mBox (QMessageBox::Critical, "Not logged in",
// "Please login first! Click file->login.", QMessageBox::Ok, this,Qt::Dialog);
// mBox.exec();
// }
//}//end exportData();
//void MainWindow::writeData(QString f)
//{
// //create file to write to
// QFile file(f);
// file.open(QIODevice::WriteOnly|QIODevice::Text);
// QTextStream out(&file);
// int vehicleId = record.value("id").toInt();
// QSqlQuery query(connection);
// QString q = queries->selectFuelMileage();
// query.prepare(q);
// query.bindValue(":vehicleid",vehicleId);
// if (query.exec())
// {
// out<<"\"id\",\"miles\",\"gallons\",\"cost\",\"fillup_date\""<<"\n";
// while(query.next())
// {
// QSqlRecord t = query.record();
// QString theData = "";
// for (int i=0;i<t.count();i++)
// {
// theData = theData + "\"" + t.value(i).toString() + "\",";
// }
// theData = theData.remove(theData.length()-1,1);
// //qDebug()<<theData;
// out<<theData<<"\n";
// }//loop through resultset
// file.close();
// QMessageBox mBox (QMessageBox::Information, "Export Data",
// "Export is completed!", QMessageBox::Ok, this,Qt::Dialog);
// mBox.exec();
// }//query was executed
//}//end writeData