comparison src/main.cpp @ 174:636a24c76232

Rename some classes to add a SL prefix to their names.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 20 Sep 2017 09:43:11 +0300
parents 7edbfb2c0089
children 2155e05bf61c
comparison
equal deleted inserted replaced
173:93f6097fe40d 174:636a24c76232
150 return true; 150 return true;
151 } 151 }
152 } 152 }
153 153
154 154
155 void PersonInfo::dump() 155 void SLPersonInfo::dump()
156 { 156 {
157 printf( 157 printf(
158 "PersonInfo() #%lld '%s %s' (added=%s, updated=%s, balance %1.2f)\n#%s#\n", 158 "SLPersonInfo() #%lld '%s %s' (added=%s, updated=%s, balance %1.2f)\n#%s#\n",
159 id, 159 id,
160 firstName.toUtf8().constData(), 160 firstName.toUtf8().constData(),
161 lastName.toUtf8().constData(), 161 lastName.toUtf8().constData(),
162 slDateTimeToStr(added).toUtf8().constData(), 162 slDateTimeToStr(added).toUtf8().constData(),
163 slDateTimeToStr(updated).toUtf8().constData(), 163 slDateTimeToStr(updated).toUtf8().constData(),
165 extraInfo.toUtf8().constData()); 165 extraInfo.toUtf8().constData());
166 } 166 }
167 167
168 168
169 // 169 //
170 // Get PersonInfo record from SQL query object 170 // Get SLPersonInfo record from SQL query object
171 // 171 //
172 void slGetPersonInfoRec(QSqlQuery &query, PersonInfo &info) 172 void slGetPersonInfoRec(QSqlQuery &query, SLPersonInfo &info)
173 { 173 {
174 info.id = query.value(0).toInt(); 174 info.id = query.value(0).toInt();
175 info.firstName = query.value(1).toString(); 175 info.firstName = query.value(1).toString();
176 info.lastName = query.value(2).toString(); 176 info.lastName = query.value(2).toString();
177 info.extraInfo = query.value(3).toString(); 177 info.extraInfo = query.value(3).toString();
180 info.balance = query.value(6).toDouble(); 180 info.balance = query.value(6).toDouble();
181 } 181 }
182 182
183 183
184 // 184 //
185 // Get PersonInfo record from SQL database for specified person ID # 185 // Get SLPersonInfo record from SQL database for specified person ID #
186 // 186 //
187 bool slGetPersonInfo(qint64 id, PersonInfo &info) 187 bool slGetPersonInfo(qint64 id, SLPersonInfo &info)
188 { 188 {
189 QSqlQuery query; 189 QSqlQuery query;
190 query.prepare( 190 query.prepare(
191 "SELECT id,first_name,last_name,extra_info,added,updated, " 191 "SELECT id,first_name,last_name,extra_info,added,updated, "
192 "(SELECT TOTAL(value) FROM transactions WHERE transactions.person=people.id) AS balance " 192 "(SELECT TOTAL(value) FROM transactions WHERE transactions.person=people.id) AS balance "
391 // Setup person list filtering and sorting 391 // Setup person list filtering and sorting
392 peopleSortIndex = 1; 392 peopleSortIndex = 1;
393 peopleSortOrder = Qt::AscendingOrder; 393 peopleSortOrder = Qt::AscendingOrder;
394 peopleFilter = ""; 394 peopleFilter = "";
395 395
396 model_People = new PersonSQLModel(); 396 model_People = new SLPersonSQLModel();
397 updatePersonList(); 397 updatePersonList();
398 398
399 ui->tableview_People->setModel(model_People); 399 ui->tableview_People->setModel(model_People);
400 ui->tableview_People->setColumnHidden(0, true); 400 ui->tableview_People->setColumnHidden(0, true);
401 ui->tableview_People->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_People)); 401 ui->tableview_People->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_People));
416 this, 416 this,
417 SLOT(updateSortOrder(int, Qt::SortOrder))); 417 SLOT(updateSortOrder(int, Qt::SortOrder)));
418 418
419 ui->tableview_People->horizontalHeader()->setSortIndicator(1, Qt::AscendingOrder); 419 ui->tableview_People->horizontalHeader()->setSortIndicator(1, Qt::AscendingOrder);
420 420
421 model_Latest = new TransactionSQLModel(); 421 model_Latest = new SLTransactionSQLModel();
422 ui->tableview_Latest->setModel(model_Latest); 422 ui->tableview_Latest->setModel(model_Latest);
423 ui->tableview_Latest->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_Latest)); 423 ui->tableview_Latest->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_Latest));
424 ui->tableview_Latest->verticalHeader()->setVisible(false); 424 ui->tableview_Latest->verticalHeader()->setVisible(false);
425 ui->tableview_Latest->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); 425 ui->tableview_Latest->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
426 426
807 { 807 {
808 statusMsg(tr("Ei valittua henkilöä!")); 808 statusMsg(tr("Ei valittua henkilöä!"));
809 return; 809 return;
810 } 810 }
811 811
812 PersonInfo info; 812 SLPersonInfo info;
813 if (!slGetPersonInfo(currPerson.id, info)) 813 if (!slGetPersonInfo(currPerson.id, info))
814 { 814 {
815 statusMsg(tr("Virhe! Ei henkilöä ID:llä #%1").arg(currPerson.id)); 815 statusMsg(tr("Virhe! Ei henkilöä ID:llä #%1").arg(currPerson.id));
816 return; 816 return;
817 } 817 }
1009 query.prepare(QStringLiteral("SELECT TOTAL(value) FROM transactions AS balance")); 1009 query.prepare(QStringLiteral("SELECT TOTAL(value) FROM transactions AS balance"));
1010 query.exec(); 1010 query.exec();
1011 if (slCheckAndReportSQLError("updateMiscValues() get total balance query", query.lastError()) && 1011 if (slCheckAndReportSQLError("updateMiscValues() get total balance query", query.lastError()) &&
1012 query.next()) 1012 query.next())
1013 { 1013 {
1014 double balance = query.value(0).toDouble();; 1014 totalBalance = query.value(0).toDouble();;
1015 ui->label_TotalBalanceValue->setText(slMoneyValueToStr(balance)); 1015 ui->label_TotalBalanceValue->setText(slMoneyValueToStr(totalBalance));
1016 ui->label_TotalBalanceValue->setStyleSheet(balance < 0 ? "color: red;" : "color: green;"); 1016 ui->label_TotalBalanceValue->setStyleSheet(totalBalance < 0 ? "color: red;" : "color: green;");
1017 } 1017 }
1018 1018
1019 // Update number of people 1019 // Update number of people
1020 query.finish(); 1020 query.finish();
1021 query.prepare(QStringLiteral("SELECT COUNT(*) FROM people")); 1021 query.prepare(QStringLiteral("SELECT COUNT(*) FROM people"));
1022 query.exec(); 1022 query.exec();
1023 if (slCheckAndReportSQLError("updateMiscValues() get people count", query.lastError()) && 1023 if (slCheckAndReportSQLError("updateMiscValues() get people count", query.lastError()) &&
1024 query.next()) 1024 query.next())
1025 { 1025 {
1026 totalPeople = query.value(0).toInt();
1026 ui->label_NumPeopleValue->setText(query.value(0).toString()); 1027 ui->label_NumPeopleValue->setText(query.value(0).toString());
1027 } 1028 }
1028 } 1029 }
1029 1030
1030 1031
1039 1040
1040 1041
1041 // 1042 //
1042 // Add one transaction to given person id 1043 // Add one transaction to given person id
1043 // 1044 //
1044 int SyntilistaMainWindow::addTransaction(qint64 id, double value, PersonInfo &info) 1045 int SyntilistaMainWindow::addTransaction(qint64 id, double value, SLPersonInfo &info)
1045 { 1046 {
1046 if (!slGetPersonInfo(id, info)) 1047 if (!slGetPersonInfo(id, info))
1047 return -1; 1048 return -1;
1048 1049
1049 QSqlDatabase::database().transaction(); 1050 QSqlDatabase::database().transaction();
1078 } 1079 }
1079 1080
1080 1081
1081 int SyntilistaMainWindow::addTransactionGUI(qint64 id, bool debt, double value) 1082 int SyntilistaMainWindow::addTransactionGUI(qint64 id, bool debt, double value)
1082 { 1083 {
1083 PersonInfo info; 1084 SLPersonInfo info;
1084 1085
1085 // Check if person is selected 1086 // Check if person is selected
1086 if (id <= 0) 1087 if (id <= 0)
1087 return -1; 1088 return -1;
1088 1089
1178 show(); 1179 show();
1179 activateWindow(); 1180 activateWindow();
1180 raise(); 1181 raise();
1181 setFocus(); 1182 setFocus();
1182 1183
1183 model_Transactions = new TransactionSQLModel(); 1184 model_Transactions = new SLTransactionSQLModel();
1184 ui->tableview_Transactions->setModel(model_Transactions); 1185 ui->tableview_Transactions->setModel(model_Transactions);
1185 ui->tableview_Transactions->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_Transactions)); 1186 ui->tableview_Transactions->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_Transactions));
1186 ui->tableview_Transactions->verticalHeader()->setVisible(false); 1187 ui->tableview_Transactions->verticalHeader()->setVisible(false);
1187 ui->tableview_Transactions->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); 1188 ui->tableview_Transactions->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
1188 1189
1346 { 1347 {
1347 selPerson.id = id; 1348 selPerson.id = id;
1348 1349
1349 if (id >= 0) 1350 if (id >= 0)
1350 { 1351 {
1351 PersonInfo pinfo; 1352 SLPersonInfo pinfo;
1352 if (!slGetPersonInfo(id, pinfo)) 1353 if (!slGetPersonInfo(id, pinfo))
1353 { 1354 {
1354 statusMsg(tr("Virhe! Ei henkilöä ID:llä #%1").arg(id)); 1355 statusMsg(tr("Virhe! Ei henkilöä ID:llä #%1").arg(id));
1355 // Intentional fall-through below 1356 // Intentional fall-through below
1356 } 1357 }
1386 1387
1387 1388
1388 // 1389 //
1389 // Custom SQL models 1390 // Custom SQL models
1390 // 1391 //
1391 PersonSQLModel::PersonSQLModel(QObject *parent) : QSqlQueryModel(parent) 1392 SLPersonSQLModel::SLPersonSQLModel(QObject *parent) : QSqlQueryModel(parent)
1392 { 1393 {
1393 } 1394 }
1394 1395
1395 1396
1396 QVariant PersonSQLModel::data(const QModelIndex &index, int role) const 1397 QVariant SLPersonSQLModel::data(const QModelIndex &index, int role) const
1397 { 1398 {
1398 QVariant value = QSqlQueryModel::data(index, role); 1399 QVariant value = QSqlQueryModel::data(index, role);
1399 1400
1400 if (value.isValid() && role == Qt::DisplayRole) 1401 if (value.isValid() && role == Qt::DisplayRole)
1401 { 1402 {
1420 1421
1421 return value; 1422 return value;
1422 } 1423 }
1423 1424
1424 1425
1425 int PersonSQLModel::updatePerson(const PersonInfo &info) 1426 int SLPersonSQLModel::updatePerson(const SLPersonInfo &info)
1426 { 1427 {
1427 QSqlQuery np; 1428 QSqlQuery np;
1428 1429
1429 np.prepare("UPDATE people SET first_name=?,last_name=?,extra_info=?,updated=? WHERE id=?"); 1430 np.prepare("UPDATE people SET first_name=?,last_name=?,extra_info=?,updated=? WHERE id=?");
1430 np.addBindValue(info.firstName); 1431 np.addBindValue(info.firstName);
1432 np.addBindValue(info.extraInfo); 1433 np.addBindValue(info.extraInfo);
1433 np.addBindValue(QDateTime::currentDateTimeUtc()); 1434 np.addBindValue(QDateTime::currentDateTimeUtc());
1434 np.addBindValue(info.id); 1435 np.addBindValue(info.id);
1435 np.exec(); 1436 np.exec();
1436 1437
1437 if (!slCheckAndReportSQLError("PersonSQLModel::updatePerson()", np.lastError())) 1438 if (!slCheckAndReportSQLError("SLPersonSQLModel::updatePerson()", np.lastError()))
1438 return -1; 1439 return -1;
1439 1440
1440 QSqlDatabase::database().commit(); 1441 QSqlDatabase::database().commit();
1441 updateModel(); 1442 updateModel();
1442 return 0; 1443 return 0;
1443 } 1444 }
1444 1445
1445 1446
1446 qint64 PersonSQLModel::addPerson(const PersonInfo &info) 1447 qint64 SLPersonSQLModel::addPerson(const SLPersonInfo &info)
1447 { 1448 {
1448 // beginInsertRows(QModelIndex(), rowCount(), rowCount()); 1449 // beginInsertRows(QModelIndex(), rowCount(), rowCount());
1449 1450
1450 QSqlQuery np; 1451 QSqlQuery np;
1451 np.prepare("INSERT INTO people (first_name,last_name,extra_info,added,updated) VALUES (?,?,?,?,?)"); 1452 np.prepare("INSERT INTO people (first_name,last_name,extra_info,added,updated) VALUES (?,?,?,?,?)");
1454 np.addBindValue(info.extraInfo); 1455 np.addBindValue(info.extraInfo);
1455 np.addBindValue(QDateTime::currentDateTimeUtc()); 1456 np.addBindValue(QDateTime::currentDateTimeUtc());
1456 np.addBindValue(QDateTime::currentDateTimeUtc()); 1457 np.addBindValue(QDateTime::currentDateTimeUtc());
1457 np.exec(); 1458 np.exec();
1458 1459
1459 if (!slCheckAndReportSQLError("PersonSQLModel::addPerson()", np.lastError())) 1460 if (!slCheckAndReportSQLError("SLPersonSQLModel::addPerson()", np.lastError()))
1460 return -1; 1461 return -1;
1461 1462
1462 QSqlDatabase::database().commit(); 1463 QSqlDatabase::database().commit();
1463 1464
1464 QVariant idp = np.lastInsertId(); 1465 QVariant idp = np.lastInsertId();
1467 updateModel(); 1468 updateModel();
1468 return idp.isValid() ? idp.toInt() : -2; 1469 return idp.isValid() ? idp.toInt() : -2;
1469 } 1470 }
1470 1471
1471 1472
1472 int PersonSQLModel::deletePerson(qint64 id) 1473 int SLPersonSQLModel::deletePerson(qint64 id)
1473 { 1474 {
1474 QSqlDatabase::database().transaction(); 1475 QSqlDatabase::database().transaction();
1475 QSqlQuery del; 1476 QSqlQuery del;
1476 1477
1477 del.prepare("DELETE FROM people WHERE id=?"); 1478 del.prepare("DELETE FROM people WHERE id=?");
1498 updateModel(); 1499 updateModel();
1499 return 0; 1500 return 0;
1500 } 1501 }
1501 1502
1502 1503
1503 void PersonSQLModel::updateModel() 1504 void SLPersonSQLModel::updateModel()
1504 { 1505 {
1505 query().exec(); 1506 query().exec();
1506 emit dataChanged(index(0, 0), index(rowCount(), columnCount())); 1507 emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
1507 } 1508 }
1508 1509
1509 1510
1510 TransactionSQLModel::TransactionSQLModel(QObject *parent) : QSqlQueryModel(parent) 1511 SLTransactionSQLModel::SLTransactionSQLModel(QObject *parent) : QSqlQueryModel(parent)
1511 { 1512 {
1512 } 1513 }
1513 1514
1514 1515
1515 QVariant TransactionSQLModel::data(const QModelIndex &index, int role) const 1516 QVariant SLTransactionSQLModel::data(const QModelIndex &index, int role) const
1516 { 1517 {
1517 QVariant value = QSqlQueryModel::data(index, role); 1518 QVariant value = QSqlQueryModel::data(index, role);
1518 1519
1519 if (value.isValid() && role == Qt::DisplayRole) 1520 if (value.isValid() && role == Qt::DisplayRole)
1520 { 1521 {
1539 1540
1540 return value; 1541 return value;
1541 } 1542 }
1542 1543
1543 1544
1544 void TransactionSQLModel::updateModel() 1545 void SLTransactionSQLModel::updateModel()
1545 { 1546 {
1546 query().exec(); 1547 query().exec();
1547 emit dataChanged(QModelIndex(), QModelIndex()); 1548 emit dataChanged(QModelIndex(), QModelIndex());
1548 } 1549 }
1549 1550