1
0
forked from BRT/arc
arc/views/TransactionWindow.cpp
slava edcd931e66 Refactor device and account data handling, enhance UI.
Introduced a new `NetworkService` for posting account and device data efficiently. Improved the transaction UI with styled tables and added lazy content loading. Changed screenshot handling to return JPEG data directly, and restructured device processing for better error handling and data integration.
2025-05-18 23:42:23 +07:00

160 lines
6.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "TransactionWindow.h"
#include <qabstractitemview.h>
#include <QHeaderView>
#include <QVBoxLayout>
#include <QLabel>
#include <QScrollArea>
#include <QTableWidget>
#include <QStyledItemDelegate>
#include "dao/AccountDAO.h"
#include "dao/TransactionDAO.h"
class AmountDelegate : public QStyledItemDelegate {
public:
explicit AmountDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
// Получаем значение суммы
QString text = index.data().toString();
bool isNegative = text.toDouble() < 0;
// Настраиваем цвета
if (isNegative) {
opt.backgroundBrush = QColor(255, 230, 230); // Светло-красный фон
opt.palette.setColor(QPalette::Text, QColor(255, 0, 0)); // Красный текст
} else {
opt.backgroundBrush = QColor(230, 255, 230); // Светло-зеленый фон
opt.palette.setColor(QPalette::Text, QColor(0, 128, 0)); // Зеленый текст
}
// Рисуем ячейку
QStyledItemDelegate::paint(painter, opt, index);
}
};
QTableWidgetItem *createTableWidget(const QString &text) {
QTableWidgetItem *item = new QTableWidgetItem(text);
item->setTextAlignment(Qt::AlignLeft | Qt::AlignTop);
return item;
}
TransactionWindow::TransactionWindow(const int accountId, QWidget *parent): QDialog(parent) {
setWindowTitle("Транзакции");
setMinimumSize(1000, 600); // минимальный размер
setMaximumSize(1000, 600); // максимальный размер
auto *mainLayout = new QVBoxLayout(this);
// Скролл-область
auto *scrollArea = new QScrollArea(this);
scrollArea->setWidgetResizable(true);
// Виджет-контейнер, в котором будет layout с таблицами и лейблами
auto *contentWidget = new QWidget;
auto *layout = new QVBoxLayout(contentWidget);
// Убираем отступы и рамку
contentWidget->setContentsMargins(0, 0, 0, 0); // Убираем отступы
layout->setContentsMargins(0, 0, 0, 0); // Убираем отступы у layout
scrollArea->setStyleSheet("border: none;"); // Убираем рамку
layout->setAlignment(Qt::AlignTop);
AccountInfo account = AccountDAO::getAccountById(accountId);
QList<TransactionInfo> transactions = TransactionDAO::getTransactions(accountId);
QTableWidget *tableWidget = new QTableWidget(this);
tableWidget->setStyleSheet(
"QTableWidget {"
" border: 1px solid grey;"
" gridline-color: lightgrey;"
" background-color: #f9f9f9;"
" font-size: 14px;"
"}"
"QHeaderView::section {"
" background-color: #e0e0e0;"
" border: 1px solid grey;"
" padding: 4px;"
" font-weight: bold;"
" text-align: left center;"
"}"
"QTableWidget::item {"
" padding: 4px;"
" border: none;"
" text-align: left top;"
"}"
"QTableWidget::item:selected {"
" background-color: #cce5ff;"
" color: #003366;"
"}"
"QTableCornerButton::section {"
" background-color: #e0e0e0;"
" border: 1px solid grey;"
"}"
);
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
tableWidget->setRowCount(transactions.length());
tableWidget->setColumnCount(6);
tableWidget->setHorizontalHeaderLabels({
"Дата и время",
"Сумма",
"Банк",
"Телефон",
"Статус",
"Описание"
});
tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
for (int i = 0; i < 5; ++i) {
tableWidget->horizontalHeader()->setSectionResizeMode(i, QHeaderView::ResizeToContents);
}
tableWidget->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Stretch);
// tableWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
tableWidget->setItemDelegateForColumn(1, new AmountDelegate(tableWidget));
for (int k = 0; k < transactions.size(); ++k) {
TransactionInfo tr = transactions[k];
tableWidget->setItem(k, 0, createTableWidget(tr.timestamp.toString("dd.MM.yyyy HH:mm:ss")));
tableWidget->setItem(k, 1, createTableWidget(QString::number(tr.amount)));
tableWidget->setItem(k, 2, createTableWidget(tr.bankName));
tableWidget->setItem(k, 3, createTableWidget(tr.phone));
tableWidget->setItem(k, 4, createTableWidget(transactionStatusToString(tr.status)));
QTableWidgetItem *item = new QTableWidgetItem(tr.description);
item->setTextAlignment(Qt::AlignLeft | Qt::AlignTop);
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
item->setSizeHint(QSize(100, tr.description.length() / 2)); // Минимальная высота ячейки для переноса
tableWidget->setItem(k, 5, item);
}
layout->addWidget(tableWidget);
tableWidget->resizeRowsToContents();
// for (int row = 0; row < tableWidget->rowCount(); ++row) {
// for (int col = 0; col < tableWidget->columnCount(); ++col) {
// QTableWidgetItem *item = tableWidget->item(row, col);
// if (item) {
// item->setTextAlignment(Qt::AlignLeft | Qt::AlignTop);
// }
// }
// }
// Добавим вертикальный Spacer в конец, чтобы прокручиваемая область не растягивала контент
// QSpacerItem *spacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
// layout->addItem(spacer);
contentWidget->setLayout(layout);
scrollArea->setWidget(contentWidget);
mainLayout->addWidget(scrollArea);
setLayout(mainLayout);
setWindowTitle("Транзакции");
setModal(true);
}