Skip to content

Commit

Permalink
added new import file selection page, bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thmichel committed Jun 27, 2015
1 parent 72e7923 commit b80cdd8
Show file tree
Hide file tree
Showing 15 changed files with 1,025 additions and 68 deletions.
2 changes: 2 additions & 0 deletions CarBudget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "station.h"
#include "car.h"
#include "carmanager.h"
#include "filemodel.h"

#include <QtCore/QTranslator>
#include <QQmlApplicationEngine>
Expand Down Expand Up @@ -65,6 +66,7 @@ int main(int argc, char *argv[])
qmlRegisterType<Tire>( "harbour.carbudget",1,0,"Tire");
qmlRegisterType<Tiremount>( "harbour.carbudget",1,0,"Tiremount");
qmlRegisterType<Car>( "harbour.carbudget",1,0,"Car");
qmlRegisterType<FileModel>("harbour.carbudget", 1, 0, "FileModel");


CarManager manager;
Expand Down
104 changes: 59 additions & 45 deletions car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ void Car::db_init()

void Car::db_load()
{
db_loading=true;
QSqlQuery query(this->db);

_tanklist.clear();
Expand All @@ -88,6 +89,7 @@ void Car::db_load()
{
while(query.next())
{
qDebug() << "Adding tank " << query.value(2).toInt();
int id = query.value(0).toInt();
QDate date = query.value(1).toDate();
unsigned int distance = query.value(2).toInt();
Expand All @@ -100,14 +102,7 @@ void Car::db_load()
Tank *tank = new Tank(date, distance, quantity, price, full, fueltype, station, id, note, this);
_tanklist.append(tank);
}
emit nbtankChanged(_tanklist.count());
emit consumptionChanged(this->consumption());
emit consumptionmaxChanged(this->consumptionmax());
emit consumptionlastChanged(this->consumptionlast());
emit consumptionminChanged(this->consumptionmin());
emit fueltotalChanged(this->fueltotal());
emit maxdistanceChanged(this->maxdistance());
emit mindistanceChanged(this->mindistance());

}
else
{
Expand Down Expand Up @@ -230,12 +225,21 @@ void Car::db_load()
{
qDebug() << "Failed to load tiremounts: " << query.lastError();
}
qSort(_tanklist.begin(), _tanklist.end(), sortTankByDistance);
qSort(_costlist.begin(), _costlist.end(), sortCostByDate);
qSort(_stationlist.begin(), _stationlist.end(), sortStationByQuantity);
qSort(_fueltypelist.begin(), _fueltypelist.end(), sortFueltypeById);
qSort(_costtypelist.begin(), _costtypelist.end(), sortCosttypeById);
qSort(_tiremountlist.begin(),_tiremountlist.end(),sortTiremountByDistance);
if (!_tanklist.empty()) qSort(_tanklist.begin(), _tanklist.end(), sortTankByDistance);
if (!_costlist.empty()) qSort(_costlist.begin(), _costlist.end(), sortCostByDate);
if (!_stationlist.empty()) qSort(_stationlist.begin(), _stationlist.end(), sortStationByQuantity);
if (!_fueltypelist.empty()) qSort(_fueltypelist.begin(), _fueltypelist.end(), sortFueltypeById);
if (!_costtypelist.empty()) qSort(_costtypelist.begin(), _costtypelist.end(), sortCosttypeById);
if (!_tiremountlist.empty()) qSort(_tiremountlist.begin(),_tiremountlist.end(),sortTiremountByDistance);
db_loading=false;
nbtankChanged(_tanklist.count());
emit consumptionChanged(this->consumption());
emit consumptionmaxChanged(this->consumptionmax());
emit consumptionlastChanged(this->consumptionlast());
emit consumptionminChanged(this->consumptionmin());
emit fueltotalChanged(this->fueltotal());
emit maxdistanceChanged(this->maxdistance());
emit mindistanceChanged(this->mindistance());
}

int Car::db_get_version()
Expand Down Expand Up @@ -320,6 +324,7 @@ Car::Car(CarManager *parent) : QObject(parent), _manager(parent)
Car::Car(QString name, CarManager *parent) : QObject(parent), _manager(parent), _name(name), _nbtire(0),_buyingprice(0),_sellingprice(0),_lifetime(0)
{
this->db_init();
db_loading=false;
while(this->db_get_version() < DB_VERSION)
{
qDebug() << "Update configuation database " << this->db_get_version() << " >> " << DB_VERSION;
Expand Down Expand Up @@ -359,10 +364,10 @@ unsigned int Car::nbtank() const

double Car::consumption() const
{
if (_tanklist.empty()) return 0.0;
unsigned long int maxDistance = 0;
unsigned long int minDistance = 999999999;
double totalConsumption = 0;

foreach(Tank *tank, _tanklist)
{
if(tank->distance() > maxDistance)
Expand All @@ -380,6 +385,7 @@ double Car::consumption() const
break;
}
}
if (maxDistance==minDistance) return 0;
return totalConsumption / ((maxDistance - minDistance)/ 100.0);
}

Expand All @@ -396,8 +402,10 @@ double Car::consumptionmax() const

double Car::consumptionlast() const
{
if (_tanklist.empty()) return 0.0;
QList<Tank*>::const_iterator tank = _tanklist.constBegin();
return (*tank)->consumption();
if (*tank) return (*tank)->consumption();
else return 0;
}

double Car::consumptionmin() const
Expand Down Expand Up @@ -490,16 +498,13 @@ QQmlListProperty<Tiremount> Car::tiremounts()
const Tank *Car::previousTank(unsigned int distance) const
{
const Tank *previous = NULL;

int currentPrevDistance=0;
foreach(Tank *tank, _tanklist)
{
if(previous == NULL && tank->distance() < distance)
{
previous = tank;
}
else if(tank->distance() < distance && tank->distance() > previous->distance())
if ((tank->distance() < distance) && (tank->distance() > currentPrevDistance))
{
previous = tank;
currentPrevDistance=tank->distance();
}
}
return previous;
Expand Down Expand Up @@ -651,6 +656,7 @@ double Car::budget_cost_byType(unsigned int id)
if (cost->costtype()==id)
totalPrice += cost->cost();
}
if (maxdistance()==mindistance()) return 0;
return totalPrice / ((maxdistance() - mindistance())/ 100.0);
}
double Car::budget_fuel_total()
Expand Down Expand Up @@ -685,7 +691,7 @@ double Car::budget_fuel()
break;
}
}
if(maxDistance == 0) return 0;
if((maxDistance == 0) || (maxDistance == minDistance)) return 0;
return totalPrice / ((maxDistance - minDistance)/ 100.0);
}
double Car::budget_cost_total()
Expand All @@ -701,7 +707,7 @@ double Car::budget_cost_total()
double Car::budget_cost()
{
//returns costs for bills per 100KM
if (maxdistance()-mindistance() ==0) return 0;
if (maxdistance() ==mindistance()) return 0;
return budget_cost_total() / ((maxdistance() - mindistance())/ 100.0);
}
double Car::budget_invest_total()
Expand All @@ -712,6 +718,7 @@ double Car::budget_invest_total()
double Car::budget_invest()
{
//returns bying costs per 100 KM
if (maxdistance()== mindistance()) return 0;
QDate today = QDate::currentDate();
unsigned int monthsused = 1;
double valuecosts;
Expand All @@ -727,14 +734,15 @@ double Car::budget_invest()
{
monthsused++;
}
if ((monthsused < _lifetime) && (_lifetime !=0))
if ((monthsused < _lifetime) && (_lifetime !=0) )
valuecosts = (_buyingprice - _sellingprice)*monthsused/_lifetime;
else valuecosts = (_buyingprice - _sellingprice);
return valuecosts / ((maxdistance() - mindistance())/ 100.0);
}
double Car::budget_tire()
{
//returns tire costs per 100km
if (maxdistance() == mindistance()) return 0;
return budget_tire_total() / ((maxdistance() - mindistance())/ 100.0);
}
double Car::budget_tire_total()
Expand Down Expand Up @@ -765,29 +773,35 @@ void Car::addNewTank(QDate date, unsigned int distance, double quantity, double
_tanklist.append(tank);
qSort(_tanklist.begin(), _tanklist.end(), sortTankByDistance);
tank->save();
emit nbtankChanged(_tanklist.count());
emit consumptionChanged(this->consumption());
emit consumptionmaxChanged(this->consumptionmax());
emit consumptionlastChanged(this->consumptionlast());
emit consumptionminChanged(this->consumptionmin());
emit fueltotalChanged(this->fueltotal());
emit maxdistanceChanged(this->maxdistance());
emit tanksChanged();
if (!db_loading)
{
emit nbtankChanged(_tanklist.count());
emit consumptionChanged(this->consumption());
emit consumptionmaxChanged(this->consumptionmax());
emit consumptionlastChanged(this->consumptionlast());
emit consumptionminChanged(this->consumptionmin());
emit fueltotalChanged(this->fueltotal());
emit maxdistanceChanged(this->maxdistance());
emit tanksChanged();
}
}

void Car::delTank(Tank *tank)
{
qDebug() << "Remove tank " << tank->id();
_tanklist.removeAll(tank);
qSort(_tanklist.begin(), _tanklist.end(), sortTankByDistance);
if (!_tanklist.empty()) qSort(_tanklist.begin(), _tanklist.end(), sortTankByDistance);
tank->remove();
emit nbtankChanged(_tanklist.count());
emit consumptionChanged(this->consumption());
emit consumptionmaxChanged(this->consumptionmax());
emit consumptionlastChanged(this->consumptionlast());
emit consumptionminChanged(this->consumptionmin());
emit maxdistanceChanged(this->maxdistance());
emit tanksChanged();
if (!db_loading)
{
emit nbtankChanged(_tanklist.count());
emit consumptionChanged(this->consumption());
emit consumptionmaxChanged(this->consumptionmax());
emit consumptionlastChanged(this->consumptionlast());
emit consumptionminChanged(this->consumptionmin());
emit maxdistanceChanged(this->maxdistance());
emit tanksChanged();
}
tank->deleteLater();
}

Expand All @@ -808,7 +822,7 @@ void Car::delFueltype(Fueltype *fueltype)
{
qDebug() << "Remove Fuel Type " << fueltype->id();
_fueltypelist.removeAll(fueltype);
qSort(_fueltypelist.begin(), _fueltypelist.end(), sortFueltypeById);
if (!_fueltypelist.empty()) qSort(_fueltypelist.begin(), _fueltypelist.end(), sortFueltypeById);
QSqlQuery query(db);
QString sql = QString("UPDATE TankList SET Fueltype = 0 WHERE fueltype=%1;").arg(fueltype->id());

Expand Down Expand Up @@ -879,7 +893,7 @@ void Car::delStation(Station *station)
{
qDebug() << "Remove Station " << station->id();
_stationlist.removeAll(station);
qSort(_stationlist.begin(), _stationlist.end(), sortStationByQuantity);
if (!_stationlist.empty()) qSort(_stationlist.begin(), _stationlist.end(), sortStationByQuantity);
QSqlQuery query(db);
QString sql = QString("UPDATE TankList SET station = 0 WHERE station=%1;").arg(station->id());

Expand Down Expand Up @@ -950,7 +964,7 @@ void Car::delCosttype(Costtype *costtype)
{
qDebug() << "Remove Cost Type " << costtype->id();
_costtypelist.removeAll(costtype);
qSort(_costtypelist.begin(), _costtypelist.end(), sortCosttypeById);
if (!_costtypelist.empty()) qSort(_costtypelist.begin(), _costtypelist.end(), sortCosttypeById);
QSqlQuery query(db);
QString sql = QString("UPDATE CostList SET costtype = 0 WHERE costtype=%1;").arg(costtype->id());

Expand Down Expand Up @@ -1019,7 +1033,7 @@ void Car::delCost(Cost *cost)
{
qDebug() << "Remove Cost " << cost->id();
_costlist.removeAll(cost);
qSort(_costlist.begin(), _costlist.end(), sortCostByDate);
if (!_costlist.empty()) qSort(_costlist.begin(), _costlist.end(), sortCostByDate);
cost->remove();
emit costsChanged();
cost->deleteLater();
Expand Down
1 change: 1 addition & 0 deletions car.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class Car : public QObject
void db_upgrade_to_2();
void db_upgrade_to_3();
void db_upgrade_to_4();
bool db_loading;

public:
QSqlDatabase db;
Expand Down
4 changes: 2 additions & 2 deletions carmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,9 @@ void CarManager::importFromMyCar(QString filename, QString name)

void CarManager::importFromFuelpad(QString filename, QString name)
{
qDebug() << "Importing from Fuelpad";
createCar(name);
selectCar(name);
filename=getenv("HOME")+QString("/")+filename;
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE","fuelpaddb");
db.setDatabaseName(filename);
Expand Down Expand Up @@ -420,6 +420,7 @@ void CarManager::importFromFuelpad(QString filename, QString name)
}
if (query.next())
t_id = query.value(0).toInt();
qDebug() << "Car id: " << t_id;
if(query.exec(QString("SELECT day,km,fill,price,service,oil,tires,insurance,other,notes FROM record WHERE carid=%1;").arg(t_id)))
{
while(query.next())
Expand Down Expand Up @@ -464,7 +465,6 @@ QString CarManager::getEnv(QString name)

QStringList CarManager::checkFuelpadDBforCars( QString name)
{
name=getenv("HOME")+QString("/")+name;
QStringList fuelpadcars;
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE","fuelpaddb");
Expand Down
Loading

0 comments on commit b80cdd8

Please sign in to comment.