Mercurial > hg > syntilista
comparison src/main.cpp @ 237:54ab3f3e28c0
Split EditPerson and ViewTransactions to separate source files.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 07 May 2018 19:19:13 +0300 |
parents | 07d837442c55 |
children | 9626307f3eeb |
comparison
equal
deleted
inserted
replaced
236:2e0fcb3d0b95 | 237:54ab3f3e28c0 |
---|---|
10 #include <QMessageBox> | 10 #include <QMessageBox> |
11 #include <QSettings> | 11 #include <QSettings> |
12 #include <QStandardPaths> | 12 #include <QStandardPaths> |
13 #include "main.h" | 13 #include "main.h" |
14 #include "ui_mainwindow.h" | 14 #include "ui_mainwindow.h" |
15 #include "ui_editperson.h" | |
16 #include "ui_aboutwindow.h" | 15 #include "ui_aboutwindow.h" |
17 #include "ui_viewtransactions.h" | |
18 #include "runguard.h" | 16 #include "runguard.h" |
19 | 17 |
20 | 18 |
21 // | 19 // |
22 // Application settings struct | 20 // Application settings struct |
1285 } | 1283 } |
1286 } | 1284 } |
1287 | 1285 |
1288 | 1286 |
1289 // | 1287 // |
1290 // Edit person dialog | |
1291 // | |
1292 EditPerson::EditPerson(QWidget *parent) : | |
1293 QDialog(parent), | |
1294 ui(new Ui::EditPerson) | |
1295 { | |
1296 ui->setupUi(this); | |
1297 | |
1298 slSetCommonStyleSheet(this); | |
1299 | |
1300 setModal(true); | |
1301 setAttribute(Qt::WA_DeleteOnClose); | |
1302 show(); | |
1303 activateWindow(); | |
1304 raise(); | |
1305 setFocus(); | |
1306 | |
1307 model_Transactions = new SLTransactionSQLModel(); | |
1308 ui->tableview_Transactions->setModel(model_Transactions); | |
1309 ui->tableview_Transactions->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_Transactions)); | |
1310 ui->tableview_Transactions->verticalHeader()->setVisible(false); | |
1311 ui->tableview_Transactions->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); | |
1312 | |
1313 ui->edit_FirstName->setMaxLength(SQL_LEN_FIRST_NAME); | |
1314 ui->edit_LastName->setMaxLength(SQL_LEN_LAST_NAME); | |
1315 | |
1316 connect( | |
1317 ui->textedit_ExtraInfo, | |
1318 SIGNAL(textChanged()), | |
1319 this, | |
1320 SLOT(on_textedit_ExtraInfo_textChanged())); | |
1321 | |
1322 validateForm(); | |
1323 } | |
1324 | |
1325 | |
1326 EditPerson::~EditPerson() | |
1327 { | |
1328 delete ui; | |
1329 delete model_Transactions; | |
1330 } | |
1331 | |
1332 | |
1333 void EditPerson::statusMsg(const QString &msg) | |
1334 { | |
1335 // Pass the status message to main window | |
1336 dynamic_cast<SyntilistaMainWindow *>(parent())->statusMsg(msg); | |
1337 } | |
1338 | |
1339 | |
1340 bool EditPerson::validateForm() | |
1341 { | |
1342 selPerson.firstName = slCleanupStr(ui->edit_FirstName->text()); | |
1343 selPerson.lastName = slCleanupStr(ui->edit_LastName->text()); | |
1344 selPerson.extraInfo = ui->textedit_ExtraInfo->document()->toPlainText(); | |
1345 bool extraInfoValid = selPerson.extraInfo.length() < SQL_LEN_EXTRA_INFO; | |
1346 | |
1347 ui->textedit_ExtraInfo->setStyleSheet(!extraInfoValid ? "background-color: red;" : NULL); | |
1348 ui->edit_FirstName->setStyleSheet(selPerson.firstName == "" ? "background-color: red;" : NULL); | |
1349 ui->edit_LastName->setStyleSheet(selPerson.lastName == "" ? "background-color: red;" : NULL); | |
1350 | |
1351 return selPerson.firstName != "" && selPerson.lastName != "" && extraInfoValid; | |
1352 } | |
1353 | |
1354 | |
1355 void EditPerson::on_button_Cancel_clicked() | |
1356 { | |
1357 close(); | |
1358 } | |
1359 | |
1360 | |
1361 void EditPerson::on_button_OK_clicked() | |
1362 { | |
1363 // | |
1364 // Check form validation | |
1365 // | |
1366 if (!validateForm()) | |
1367 { | |
1368 slErrorMsg( | |
1369 tr("Virhe!"), | |
1370 tr("Vaaditut kentät (etunimi, sukunimi) eivät ole täytetty tai lisätietojen pituus on liian suuri.")); | |
1371 | |
1372 return; | |
1373 } | |
1374 | |
1375 if (selPerson.id >= 0) | |
1376 { | |
1377 // | |
1378 // We are in update/edit person mode, thus we check if the | |
1379 // first/last name have changed and if there is someone with | |
1380 // different ID and same names. | |
1381 // | |
1382 QSqlQuery person; | |
1383 person.prepare(QStringLiteral("SELECT * FROM people WHERE id <> ? AND first_name=? AND last_name=?")); | |
1384 person.addBindValue(selPerson.id); | |
1385 person.addBindValue(selPerson.firstName); | |
1386 person.addBindValue(selPerson.lastName); | |
1387 person.exec(); | |
1388 | |
1389 slCheckAndReportSQLError("SELECT check for existing person by same name (UPDATE)", person.lastError()); | |
1390 | |
1391 if (person.next()) | |
1392 { | |
1393 // There exists another person with that name | |
1394 slErrorMsg( | |
1395 tr("Virhe!"), | |
1396 tr("Ei pysty! Samalla nimellä '%1 %2' on olemassa jo henkilö!"). | |
1397 arg(selPerson.firstName).arg(selPerson.lastName)); | |
1398 return; | |
1399 } | |
1400 | |
1401 // Allest klar, update the person data | |
1402 dynamic_cast<SyntilistaMainWindow *>(parent())->model_People->updatePerson(selPerson); | |
1403 dynamic_cast<SyntilistaMainWindow *>(parent())->setActivePerson(selPerson.id); | |
1404 | |
1405 statusMsg(tr("Päivitettiin henkilö '%1 %2' (#%3)."). | |
1406 arg(selPerson.firstName).arg(selPerson.lastName).arg(selPerson.id)); | |
1407 } | |
1408 else | |
1409 { | |
1410 // | |
1411 // We are in "add new person" mode, check if there exists | |
1412 // someone with same first+last name. | |
1413 // | |
1414 QSqlQuery person; | |
1415 person.prepare("SELECT * FROM people WHERE first_name=? AND last_name=?"); | |
1416 person.addBindValue(selPerson.firstName); | |
1417 person.addBindValue(selPerson.lastName); | |
1418 person.exec(); | |
1419 | |
1420 slCheckAndReportSQLError("SELECT check for existing person by same name (ADD)", person.lastError()); | |
1421 | |
1422 if (person.next()) | |
1423 { | |
1424 // There exists a record with same name | |
1425 slErrorMsg( | |
1426 tr("Virhe!"), | |
1427 tr("Ei pysty! Samalla nimellä '%1 %2' on olemassa jo henkilö!"). | |
1428 arg(selPerson.firstName).arg(selPerson.lastName)); | |
1429 | |
1430 return; | |
1431 } | |
1432 | |
1433 // Attempt to add a person | |
1434 qint64 nid = dynamic_cast<SyntilistaMainWindow *>(parent())->model_People->addPerson(selPerson); | |
1435 if (nid < 0) | |
1436 { | |
1437 slErrorMsg( | |
1438 tr("Virhe!"), | |
1439 tr("Tietokannan käsittelyssä tapahtui virhe (#%1)."). | |
1440 arg(nid)); | |
1441 } | |
1442 else | |
1443 { | |
1444 dynamic_cast<SyntilistaMainWindow *>(parent())->updatePersonList(); | |
1445 dynamic_cast<SyntilistaMainWindow *>(parent())->setActivePerson(nid); | |
1446 dynamic_cast<SyntilistaMainWindow *>(parent())->focusDebtEdit(); | |
1447 | |
1448 statusMsg(tr("Lisättiin uusi henkilö '%1 %2'."). | |
1449 arg(selPerson.firstName).arg(selPerson.lastName)); | |
1450 } | |
1451 } | |
1452 | |
1453 close(); | |
1454 } | |
1455 | |
1456 | |
1457 void EditPerson::on_edit_FirstName_textChanged(const QString &arg1) | |
1458 { | |
1459 (void) arg1; | |
1460 validateForm(); | |
1461 } | |
1462 | |
1463 | |
1464 void EditPerson::on_edit_LastName_textChanged(const QString &arg1) | |
1465 { | |
1466 (void) arg1; | |
1467 validateForm(); | |
1468 } | |
1469 | |
1470 | |
1471 void EditPerson::on_textedit_ExtraInfo_textChanged() | |
1472 { | |
1473 validateForm(); | |
1474 } | |
1475 | |
1476 | |
1477 void EditPerson::clearForm() | |
1478 { | |
1479 ui->edit_FirstName->clear(); | |
1480 ui->edit_LastName->clear(); | |
1481 ui->textedit_ExtraInfo->document()->clear(); | |
1482 ui->edit_FirstName->setFocus(); | |
1483 } | |
1484 | |
1485 | |
1486 // | |
1487 // Set the person to be edited | |
1488 // | |
1489 void EditPerson::setPerson(qint64 id) | |
1490 { | |
1491 selPerson.id = id; | |
1492 | |
1493 if (id >= 0) | |
1494 { | |
1495 SLPersonInfo pinfo; | |
1496 if (!slGetPersonInfo(id, pinfo)) | |
1497 { | |
1498 statusMsg(tr("Virhe! Ei henkilöä ID:llä #%1").arg(id)); | |
1499 // Intentional fall-through below | |
1500 } | |
1501 else | |
1502 { | |
1503 ui->edit_FirstName->setText(pinfo.firstName); | |
1504 ui->edit_LastName->setText(pinfo.lastName); | |
1505 ui->textedit_ExtraInfo->document()->setPlainText(pinfo.extraInfo); | |
1506 ui->label_AddedValue->setText(slDateTimeToStr(pinfo.added)); | |
1507 | |
1508 QSqlQuery query; | |
1509 query.prepare(QStringLiteral("SELECT id,value,added FROM transactions WHERE person=? ORDER BY added DESC")); | |
1510 query.addBindValue(pinfo.id); | |
1511 query.exec(); | |
1512 slCheckAndReportSQLError("SELECT transactions for tableview_Transactions", query.lastError()); | |
1513 | |
1514 model_Transactions->setQuery(query); | |
1515 | |
1516 model_Transactions->setHeaderData(0, Qt::Horizontal, tr("ID")); | |
1517 model_Transactions->setHeaderData(1, Qt::Horizontal, tr("Summa")); | |
1518 model_Transactions->setHeaderData(2, Qt::Horizontal, tr("Aika")); | |
1519 | |
1520 ui->tableview_Transactions->setModel(model_Transactions); | |
1521 ui->tableview_Transactions->setColumnHidden(0, true); | |
1522 | |
1523 return; // Ugly | |
1524 } | |
1525 } | |
1526 | |
1527 // In case of id < 0 or errors .. | |
1528 clearForm(); | |
1529 ui->tableview_Transactions->setModel(NULL); | |
1530 } | |
1531 | |
1532 | |
1533 // | |
1534 // About window | 1288 // About window |
1535 // | 1289 // |
1536 AboutWindow::AboutWindow(QWidget *parent) : | 1290 AboutWindow::AboutWindow(QWidget *parent) : |
1537 QDialog(parent), | 1291 QDialog(parent), |
1538 ui(new Ui::AboutWindow) | 1292 ui(new Ui::AboutWindow) |
1612 | 1366 |
1613 void AboutWindow::on_button_Close_clicked() | 1367 void AboutWindow::on_button_Close_clicked() |
1614 { | 1368 { |
1615 close(); | 1369 close(); |
1616 } | 1370 } |
1617 | |
1618 | |
1619 // | |
1620 // Global transactions list viewer | |
1621 // | |
1622 ViewTransactions::ViewTransactions(QWidget *parent) : | |
1623 QDialog(parent), | |
1624 ui(new Ui::ViewTransactions) | |
1625 { | |
1626 ui->setupUi(this); | |
1627 | |
1628 slSetCommonStyleSheet(this); | |
1629 | |
1630 setModal(true); | |
1631 setAttribute(Qt::WA_DeleteOnClose); | |
1632 show(); | |
1633 activateWindow(); | |
1634 raise(); | |
1635 setFocus(); | |
1636 | |
1637 model_Transactions = new SLTransactionSQLModel(); | |
1638 ui->tableview_Transactions->setModel(model_Transactions); | |
1639 ui->tableview_Transactions->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_Transactions)); | |
1640 ui->tableview_Transactions->verticalHeader()->setVisible(false); | |
1641 ui->tableview_Transactions->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); | |
1642 | |
1643 QSqlQuery query; | |
1644 query.prepare(QStringLiteral( | |
1645 "SELECT transactions.id,transactions.value,transactions.added," | |
1646 "people.last_name,people.first_name FROM transactions " | |
1647 "LEFT JOIN people ON transactions.person=people.id ORDER BY transactions.added DESC" | |
1648 )); | |
1649 | |
1650 query.exec(); | |
1651 slCheckAndReportSQLError("SELECT transactions for tableview_Transactions", query.lastError()); | |
1652 | |
1653 model_Transactions->setQuery(query); | |
1654 | |
1655 model_Transactions->setHeaderData(0, Qt::Horizontal, tr("ID")); | |
1656 model_Transactions->setHeaderData(1, Qt::Horizontal, tr("Summa")); | |
1657 model_Transactions->setHeaderData(2, Qt::Horizontal, tr("Aika")); | |
1658 model_Transactions->setHeaderData(3, Qt::Horizontal, tr("Sukunimi")); | |
1659 model_Transactions->setHeaderData(4, Qt::Horizontal, tr("Etunimi")); | |
1660 | |
1661 ui->tableview_Transactions->setModel(model_Transactions); | |
1662 ui->tableview_Transactions->setColumnHidden(0, true); | |
1663 } | |
1664 | |
1665 | |
1666 ViewTransactions::~ViewTransactions() | |
1667 { | |
1668 delete ui; | |
1669 delete model_Transactions; | |
1670 } | |
1671 | |
1672 | |
1673 void ViewTransactions::on_button_Close_clicked() | |
1674 { | |
1675 close(); | |
1676 } |