void Car::addNewTank(QDate date, unsigned int distance, double quantity, double price, bool full, unsigned int station, QString note) { Tank *tank = new Tank(date, distance, quantity, price, full, station, CREATE_NEW_EVENT, note, this); _tanklist.append(tank); qSort(_tanklist.begin(), _tanklist.end(), sortTankByDistance); tank->save(); emit nbtankChanged(_tanklist.count()); emit consumptionChanged(this->consumption()); 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); tank->remove(); emit nbtankChanged(_tanklist.count()); emit consumptionChanged(this->consumption()); emit maxdistanceChanged(this->maxdistance()); emit tanksChanged(); tank->deleteLater(); }
void Car::db_load() { db_loading=true; QSqlQuery query(this->db); _tanklist.clear(); _stationlist.clear(); _tirelist.clear(); _costlist.clear(); _fueltypelist.clear(); _costtypelist.clear(); _tirelist.clear(); _tiremountlist.clear(); if(query.exec("SELECT event,date(date),distance,quantity,price,full,station,fueltype,note FROM TankList, Event WHERE TankList.event == Event.id;")) { 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(); double quantity = query.value(3).toDouble(); double price = query.value(4).toDouble(); bool full = query.value(5).toBool(); unsigned int station = query.value(6).toInt(); unsigned int fueltype = query.value(7).toInt(); QString note = query.value(8).toString(); Tank *tank = new Tank(date, distance, quantity, price, full, fueltype, station, id, note, this); _tanklist.append(tank); } } else { qDebug() << query.lastError(); } if(query.exec("SELECT id,name FROM FueltypeList;")) { while(query.next()) { int id = query.value(0).toInt(); QString name = query.value(1).toString(); Fueltype *fueltype = new Fueltype(id, name, this); _fueltypelist.append(fueltype); } } if(query.exec("SELECT id,name,sum(TankList.quantity) as quantity FROM StationList LEFT JOIN TankList ON StationList.id == TankList.station GROUP BY StationList.id;")) { while(query.next()) { int id = query.value(0).toInt(); QString name = query.value(1).toString(); double quantity = query.value(2).toDouble(); Station *station = new Station(id, name, quantity, this); _stationlist.append(station); } } else { qDebug() << query.lastError(); } if(query.exec("SELECT id,name FROM CosttypeList;")) { while(query.next()) { int id = query.value(0).toInt(); QString name = query.value(1).toString(); Costtype *costtype = new Costtype(id, name, this); _costtypelist.append(costtype); } } else { qDebug() << query.lastError(); } if(query.exec("SELECT event,date,distance,costtype,cost,desc FROM CostList, Event WHERE CostList.event == Event.id;")) { while(query.next()) { int id = query.value(0).toInt(); QDate date = query.value(1).toDate(); unsigned int distance = query.value(2).toInt(); unsigned int costtype = query.value(3).toInt(); double price = query.value(4).toDouble(); QString description = query.value(5).toString(); Cost *cost = new Cost(date,distance,costtype,description,price,id,this); _costlist.append(cost); } } else { qDebug() << query.lastError(); } if(query.exec("SELECT id,buydate,trashdate,price,name,manufacturer,model,quantity FROM TireList;")) { while(query.next()) { int id = query.value(0).toInt(); QDate buydate = query.value(1).toDate(); QDate trashdate = query.value(2).toDate(); double price = query.value(3).toDouble(); QString name = query.value(4).toString(); QString manufacturer = query.value(5).toString(); QString model = query.value(6).toString(); unsigned int quantity = query.value(7).toInt(); Tire *tire = new Tire(buydate,trashdate,name,manufacturer,model,price,quantity,id,this); _tirelist.append(tire); } } else { qDebug() << query.lastError(); } // Now load tire mountings // First load all unmount events (event_umount exists in events table) if(query.exec("SELECT m.id, m.date, m.distance, u.id, u.date, u.distance, t.tire FROM TireUsage t, Event m, Event u WHERE t.event_mount == m.id AND t.event_umount==u.id;")) { while(query.next()) { int mount_id = query.value(0).toInt(); QDate mount_date = query.value(1).toDate(); unsigned int mount_distance = query.value(2).toInt(); int unmount_id = query.value(3).toInt(); QDate unmount_date = query.value(4).toDate(); unsigned int unmount_distance = query.value(5).toInt(); unsigned int tire = query.value(6).toInt(); Tiremount *tiremount = new Tiremount(mount_id,mount_date,mount_distance,unmount_id,unmount_date,unmount_distance,tire,this); _tiremountlist.append(tiremount); } } else { qDebug() << "Failed to load tiremounts: " << query.lastError(); } // Now load mounted tires if(query.exec("SELECT m.id, m.date, m.distance, t.tire FROM TireUsage t, Event m WHERE t.event_mount == m.id AND t.event_umount==0;")) { while(query.next()) { int mount_id = query.value(0).toInt(); QDate mount_date = query.value(1).toDate(); unsigned int mount_distance = query.value(2).toInt(); unsigned int tire = query.value(3).toInt(); QDate unmount_date = QDate(1900,1,1); Tiremount *tiremount = new Tiremount(mount_id,mount_date,mount_distance,0,unmount_date,0,tire,this); _tiremountlist.append(tiremount); } } else { qDebug() << "Failed to load tiremounts: " << query.lastError(); } 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()); }
void Car::db_load() { QSqlQuery query(this->db); _tanklist.clear(); _stationlist.clear(); _tirelist.clear(); _costlist.clear(); if(query.exec("SELECT event,date(date),distance,quantity,price,full,station,note FROM TankList, Event WHERE TankList.event == Event.id;")) { while(query.next()) { int id = query.value(0).toInt(); QDate date = query.value(1).toDate(); unsigned int distance = query.value(2).toInt(); double quantity = query.value(3).toDouble(); double price = query.value(4).toDouble(); bool full = query.value(5).toBool(); unsigned int station = query.value(6).toInt(); QString note = query.value(7).toString(); Tank *tank = new Tank(date, distance, quantity, price, full, station, id, note, this); _tanklist.append(tank); } emit nbtankChanged(_tanklist.count()); emit consumptionChanged(this->consumption()); emit maxdistanceChanged(this->maxdistance()); emit mindistanceChanged(this->mindistance()); } else { qDebug() << query.lastError(); } if(query.exec("SELECT id,name FROM StationList;")) { while(query.next()) { int id = query.value(0).toInt(); QString name = query.value(1).toString(); Station *station = new Station(id, name, this); _stationlist.append(station); } } else { qDebug() << query.lastError(); } if(query.exec("SELECT event,date,distance,cost,desc FROM CostList, Event WHERE CostList.event == Event.id;")) { while(query.next()) { int id = query.value(0).toInt(); QDate date = query.value(1).toDate(); unsigned int distance = query.value(2).toInt(); double price = query.value(3).toDouble(); QString description = query.value(4).toString(); Cost *cost = new Cost(date,distance,description,price,id,this); _costlist.append(cost); } } else { qDebug() << query.lastError(); } if(query.exec("SELECT id,buydate,trashdate,price,name,manufacturer,model,quantity FROM TireList;")) { while(query.next()) { int id = query.value(0).toInt(); QDate buydate = query.value(1).toDate(); QDate trashdate = query.value(2).toDate(); double price = query.value(3).toDouble(); QString name = query.value(4).toString(); QString manufacturer = query.value(5).toString(); QString model = query.value(6).toString(); unsigned int quantity = query.value(7).toInt(); Tire *tire = new Tire(buydate,trashdate,name,manufacturer,model,price,quantity,id,this); _tirelist.append(tire); } } else { qDebug() << query.lastError(); } qSort(_tanklist.begin(), _tanklist.end(), sortTankByDistance); qSort(_costlist.begin(), _costlist.end(), sortCostByDistance); qSort(_stationlist.begin(), _stationlist.end(), sortStationById); }