Mercurial > hg > syntilista
comparison src/main.cpp @ 213:131463be208b
Split the custom SQL models code into sqlmodels.cpp
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 18 Dec 2017 11:28:00 +0200 |
parents | 70317bb39f05 |
children | 8b9d55fb8988 |
comparison
equal
deleted
inserted
replaced
212:70317bb39f05 | 213:131463be208b |
---|---|
1502 } | 1502 } |
1503 | 1503 |
1504 // In case of id < 0 or errors .. | 1504 // In case of id < 0 or errors .. |
1505 clearForm(); | 1505 clearForm(); |
1506 ui->tableview_Transactions->setModel(NULL); | 1506 ui->tableview_Transactions->setModel(NULL); |
1507 } | |
1508 | |
1509 | |
1510 // | |
1511 // Custom SQL models | |
1512 // | |
1513 SLPersonSQLModel::SLPersonSQLModel(QObject *parent) : QSqlQueryModel(parent) | |
1514 { | |
1515 } | |
1516 | |
1517 | |
1518 QVariant SLPersonSQLModel::data(const QModelIndex &index, int role) const | |
1519 { | |
1520 QVariant value = QSqlQueryModel::data(index, role); | |
1521 | |
1522 if (value.isValid() && role == Qt::DisplayRole) | |
1523 { | |
1524 // Format some of the displayed values | |
1525 switch (index.column()) | |
1526 { | |
1527 case 3: | |
1528 return slMoneyValueToStr(value.toDouble()); | |
1529 | |
1530 case 4: | |
1531 return slDateTimeToStr(value.toDateTime()); | |
1532 } | |
1533 } | |
1534 | |
1535 if (index.column() == 3 && role == Qt::ForegroundRole) | |
1536 { | |
1537 // Use fancy coloring for debt | |
1538 double val = QSqlQueryModel::data(index, Qt::DisplayRole).toDouble(); | |
1539 if (val < 0) | |
1540 return QVariant::fromValue(QColor(Qt::red)); | |
1541 else | |
1542 return QVariant::fromValue(QColor(Qt::green)); | |
1543 } | |
1544 | |
1545 return value; | |
1546 } | |
1547 | |
1548 | |
1549 int SLPersonSQLModel::updatePerson(const SLPersonInfo &info) | |
1550 { | |
1551 QSqlQuery np; | |
1552 | |
1553 np.prepare(QStringLiteral("UPDATE people SET first_name=?,last_name=?,extra_info=?,updated=? WHERE id=?")); | |
1554 np.addBindValue(info.firstName); | |
1555 np.addBindValue(info.lastName); | |
1556 np.addBindValue(info.extraInfo); | |
1557 np.addBindValue(QDateTime::currentDateTimeUtc()); | |
1558 np.addBindValue(info.id); | |
1559 np.exec(); | |
1560 | |
1561 if (!slCheckAndReportSQLError("SLPersonSQLModel::updatePerson()", np.lastError())) | |
1562 return -1; | |
1563 | |
1564 QSqlDatabase::database().commit(); | |
1565 updateModel(); | |
1566 return 0; | |
1567 } | |
1568 | |
1569 | |
1570 qint64 SLPersonSQLModel::addPerson(const SLPersonInfo &info) | |
1571 { | |
1572 QSqlQuery np; | |
1573 np.prepare(QStringLiteral("INSERT INTO people (first_name,last_name,extra_info,added,updated) VALUES (?,?,?,?,?)")); | |
1574 np.addBindValue(info.firstName); | |
1575 np.addBindValue(info.lastName); | |
1576 np.addBindValue(info.extraInfo); | |
1577 np.addBindValue(QDateTime::currentDateTimeUtc()); | |
1578 np.addBindValue(QDateTime::currentDateTimeUtc()); | |
1579 np.exec(); | |
1580 | |
1581 if (!slCheckAndReportSQLError("SLPersonSQLModel::addPerson()", np.lastError())) | |
1582 return -1; | |
1583 | |
1584 QSqlDatabase::database().commit(); | |
1585 | |
1586 QVariant idp = np.lastInsertId(); | |
1587 | |
1588 updateModel(); | |
1589 return idp.isValid() ? idp.toInt() : -2; | |
1590 } | |
1591 | |
1592 | |
1593 int SLPersonSQLModel::deletePerson(qint64 id) | |
1594 { | |
1595 QSqlDatabase::database().transaction(); | |
1596 QSqlQuery del; | |
1597 | |
1598 del.prepare(QStringLiteral("DELETE FROM people WHERE id=?")); | |
1599 del.addBindValue(id); | |
1600 del.exec(); | |
1601 | |
1602 if (!slCheckAndReportSQLError("delete user", del.lastError())) | |
1603 { | |
1604 QSqlDatabase::database().rollback(); | |
1605 return -1; | |
1606 } | |
1607 | |
1608 del.prepare(QStringLiteral("DELETE FROM transactions WHERE person=?")); | |
1609 del.addBindValue(id); | |
1610 del.exec(); | |
1611 | |
1612 if (!slCheckAndReportSQLError("delete user transactions", del.lastError())) | |
1613 { | |
1614 QSqlDatabase::database().rollback(); | |
1615 return -2; | |
1616 } | |
1617 | |
1618 QSqlDatabase::database().commit(); | |
1619 updateModel(); | |
1620 return 0; | |
1621 } | |
1622 | |
1623 | |
1624 void SLPersonSQLModel::updateModel() | |
1625 { | |
1626 query().exec(); | |
1627 emit dataChanged(index(0, 0), index(rowCount(), columnCount())); | |
1628 } | |
1629 | |
1630 | |
1631 SLTransactionSQLModel::SLTransactionSQLModel(QObject *parent) : QSqlQueryModel(parent) | |
1632 { | |
1633 } | |
1634 | |
1635 | |
1636 QVariant SLTransactionSQLModel::data(const QModelIndex &index, int role) const | |
1637 { | |
1638 QVariant value = QSqlQueryModel::data(index, role); | |
1639 | |
1640 if (value.isValid() && role == Qt::DisplayRole) | |
1641 { | |
1642 // Format some of the displayed values | |
1643 switch (index.column()) | |
1644 { | |
1645 case 1: | |
1646 return slMoneyValueToStrSign(value.toDouble()); | |
1647 | |
1648 case 2: | |
1649 return slDateTimeToStr(value.toDateTime()); | |
1650 } | |
1651 } | |
1652 | |
1653 if (index.column() == 1 && role == Qt::ForegroundRole) | |
1654 { | |
1655 // Use fancy coloring for debt | |
1656 double val = QSqlQueryModel::data(index, Qt::DisplayRole).toDouble(); | |
1657 if (val < 0) | |
1658 return QVariant::fromValue(QColor(Qt::red)); | |
1659 else | |
1660 return QVariant::fromValue(QColor(Qt::green)); | |
1661 } | |
1662 | |
1663 return value; | |
1664 } | |
1665 | |
1666 | |
1667 void SLTransactionSQLModel::updateModel() | |
1668 { | |
1669 query().exec(); | |
1670 emit dataChanged(QModelIndex(), QModelIndex()); | |
1671 } | 1507 } |
1672 | 1508 |
1673 | 1509 |
1674 // | 1510 // |
1675 // About window | 1511 // About window |