From 1507faef6eaa8b33bf40cf12a95fceba583e251b Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 24 May 2025 16:04:44 +0300 Subject: [PATCH] fixed only handling two fields of a check --- check/check.cpp | 75 ++++++++++++-------------------- check/check.h | 5 +++ goods/goods.cpp | 47 ++++++++++++++++++++ goods/goods.h | 9 ++++ main.cpp | 2 +- mainwindow.cpp | 7 ++- outputdialog.cpp | 11 +++++ translations/en_US.ts | 14 +++--- translations/ru_RU.ts | 14 +++--- widgets/checkqueuetablemodel.cpp | 64 ++++++++++++++++++--------- 10 files changed, 163 insertions(+), 85 deletions(-) diff --git a/check/check.cpp b/check/check.cpp index 4f728ec..5265532 100644 --- a/check/check.cpp +++ b/check/check.cpp @@ -1,9 +1,19 @@ #include "check.h" #include "../goods/goods.h" -#include +#include Check::Check() {} +Check::Check(std::string date, double total, OperationType type, std::string fn, std::string fd, std::string fi, std::vector goods) { + set_date(date); + set_total(total); + set_operation_type(type); + set_fn(fn); + set_fd(fd); + set_fi(fi); + set_goods(goods); +} + void Check::add_goods(Goods goods) { this->goods.push_back(goods); this->total = this->calculae_total_price(); @@ -25,50 +35,24 @@ double Check::calculae_total_price() { return total; } -std::vector& Check::get_goods() { - return goods; -} +std::vector& Check::get_goods() { return goods; } -void Check::set_fn(std::string fn) { - this->fn = fn; -} +void Check::set_fn(std::string fn) { this->fn = fn; } +void Check::set_fd(std::string fd) { this->fd = fd; } +void Check::set_fi(std::string fi) { this->fi = fi; } +std::string Check::get_date() { return date; } -void Check::set_fd(std::string fd) { - this->fd = fd; -} - -void Check::set_fi(std::string fi) { - this->fi = fi; -} - -std::string Check::get_date() { - return date; -} - -void Check::set_date(std::string date) { - this->date = date; -} - -void Check::set_operation_type(OperationType t) { - this->operation_type = t; -} - -void Check::set_total(double total){ - this->total = total; -} - -double Check::get_total() { - return total; -} +OperationType Check::get_operationType() { return operation_type; } +void Check::set_date(std::string date) { this->date = date; } +void Check::set_operation_type(OperationType t) { this->operation_type = t; } +void Check::set_total(double total) { this->total = total; } +void Check::set_goods(std::vector goods) { this->goods = goods; } +std::string Check::get_fn() { return fn; } +std::string Check::get_fd() { return fd; } +std::string Check::get_fi() { return fi; } +double Check::get_total() { return total; } bool Check::operator==(Check &c) { - // std::cout << "Comparing" << std::endl; - // std::cout << this->date << " <>" << c.date << std::endl; - // std::cout << this->fd << " <>" << c.fd << std::endl; - // std::cout << this->fi<< " <>" << c.fi << std::endl; - // std::cout << this->fn<< " <>" << c.fn << std::endl; - // std::cout << this->operation_type << " <>" << c.operation_type << std::endl; - // std::cout << this->total<< " <>" << c.total<< std::endl; return this->date == c.date && this->fd == c.fd && @@ -79,13 +63,6 @@ bool Check::operator==(Check &c) { } bool Check::operator==(const Check &c) { - // std::cout << "Comparing" << std::endl; - // std::cout << this->date << " <>" << c.date << std::endl; - // std::cout << this->fd << " <>" << c.fd << std::endl; - // std::cout << this->fi<< " <>" << c.fi << std::endl; - // std::cout << this->fn<< " <>" << c.fn << std::endl; - // std::cout << this->operation_type << " <>" << c.operation_type << std::endl; - // std::cout << this->total<< " <>" << c.total<< std::endl; return this->date == c.date && this->fd == c.fd && @@ -94,3 +71,5 @@ bool Check::operator==(const Check &c) { this->operation_type == c.operation_type && this->total == c.total; } + +Q_DECLARE_METATYPE(Check) diff --git a/check/check.h b/check/check.h index 1d97a04..ee88c51 100644 --- a/check/check.h +++ b/check/check.h @@ -1,6 +1,7 @@ #ifndef CHECK_H #define CHECK_H #include "../goods/goods.h" + #include typedef enum OperationTypes { @@ -23,6 +24,7 @@ class Check { public: Check(); + Check(std::string date, double total, OperationType type, std::string fn, std::string fd, std::string fi, std::vector goods); void add_goods(Goods); void add_goods(std::vector &goods); @@ -37,6 +39,8 @@ public: void set_operation_type(OperationType); void set_total(double); + void set_goods(std::vector); + std::string get_fn(); std::string get_fd(); std::string get_fi(); @@ -48,4 +52,5 @@ public: bool operator==(const Check &); }; + #endif // CHECK_H diff --git a/goods/goods.cpp b/goods/goods.cpp index 8857749..f5562ab 100644 --- a/goods/goods.cpp +++ b/goods/goods.cpp @@ -1,5 +1,9 @@ #include "goods.h" #include +#include +#include + +Goods::Goods() { } Goods::Goods(std::string name, double price_per_unit, std::string net_weight, double quantity) : name(name), price_per_unit(price_per_unit), @@ -19,10 +23,53 @@ double Goods::get_price_per_unit() { return this->price_per_unit; } void Goods::set_name(std::string name) { this->name = name; } +void Goods::set_name(QString name) { this->name = name.toStdString(); } + void Goods::set_quantity(double quantity) { this->quantity = quantity; } void Goods::set_net_weight(std::string net_weight) { this->net_weight = net_weight; } +void Goods::set_net_weight(QString net_weight) { this->net_weight = net_weight.toStdString(); } + void Goods::set_price_per_unit(double price_per_unit) { this->price_per_unit = price_per_unit; } + +Q_DECLARE_METATYPE(Goods) + +QDataStream &operator<<(QDataStream &in, Goods &goods) { + in << QString::fromStdString(goods.get_name()) << goods.get_quantity() << QString::fromStdString(goods.get_net_weight()) << goods.get_price_per_unit(); + return in; +} + +QDataStream &operator>>(QDataStream &out, Goods &goods) { + QString name, net_weight; + double quantity, price_per_unit; + out >> name >> quantity >> net_weight >> price_per_unit; + goods.set_name(name); + goods.set_quantity(quantity); + goods.set_net_weight(net_weight); + goods.set_price_per_unit(price_per_unit); + return out; +} + + +QDataStream &operator<<(QDataStream &stream, std::vector &goods) { + stream << (unsigned int )goods.size(); + for (Goods &g : goods) { + stream << g; + } + return stream; +} + +QDataStream &operator>>(QDataStream &stream, std::vector &goods) { + unsigned int size; + stream >> size; + for (unsigned int i = 0 ; i < size; i ++) { + Goods g = Goods(); + stream >> g; + goods.push_back(g); + } + return stream; +} + diff --git a/goods/goods.h b/goods/goods.h index ccbe3e3..85f49e0 100644 --- a/goods/goods.h +++ b/goods/goods.h @@ -2,6 +2,7 @@ #define GOODS_H #include +#include class Goods { @@ -10,6 +11,7 @@ class Goods std::string net_weight; // will contain values like "5мл" or "10г" double price_per_unit; public: + Goods(); Goods(std::string name, double quantity, std::string net_weight, double price_per_unit); double calculate_total_price(); @@ -19,9 +21,16 @@ public: double get_price_per_unit(); void set_name(std::string); + void set_name(QString); void set_quantity(double); void set_net_weight(std::string); + void set_net_weight(QString); void set_price_per_unit(double); }; +QDataStream &operator<<(QDataStream &, Goods &); +QDataStream &operator>>(QDataStream &, Goods &); + +QDataStream &operator<<(QDataStream &, std::vector &); +QDataStream &operator>>(QDataStream &, std::vector &); #endif // GOODS_H diff --git a/main.cpp b/main.cpp index dad189c..f5345f8 100644 --- a/main.cpp +++ b/main.cpp @@ -32,7 +32,7 @@ int main(int argc, char *argv[]) { curl_global_init(CURL_GLOBAL_ALL); - + qRegisterMetaType("Check"); std::string program_data_path = get_path_relative_to_home(".local/share/checks_parser"); create_directories(program_data_path); diff --git a/mainwindow.cpp b/mainwindow.cpp index 7244b56..aeba733 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -218,8 +218,10 @@ void MainWindow::on_add_new_check_button_clicked() { unsigned int newRowIndex = checks.size(); model->insertRows(newRowIndex, 1); - model->setData(model->index(newRowIndex, 0), QVariant::fromValue(new_check->get_date())); - model->setData(model->index(newRowIndex, 1), QVariant::fromValue(new_check->get_total())); + + checks.at(newRowIndex) = *new_check; + + emit model->dataChanged(model->index(newRowIndex, 0), model->index(newRowIndex, 1)); delete new_check; @@ -260,6 +262,7 @@ Check *MainWindow::parse_new_check() { check->set_fd(ui->fd_line_edit->text().toStdString()); check->set_fi(ui->fi_line_edit->text().toStdString()); check->set_operation_type(OperationType(ui->operation_type_combo_box->currentIndex() + 1)); + return check; } catch(OfdRequestException e) { if (!strcmp(e.what(), "Incorrect captcha")) { diff --git a/outputdialog.cpp b/outputdialog.cpp index b1c0502..a3dea33 100644 --- a/outputdialog.cpp +++ b/outputdialog.cpp @@ -20,6 +20,13 @@ OutputDialog::OutputDialog(QWidget *parent, std::vector *checks) ui->setupUi(this); + for (Check &c : *checks) { + std::cout << "Check: " << c.get_date() << " " << c.get_total() << std::endl; + for (Goods &g : c.get_goods()) { + std::cout << g.get_name() << " " << g.get_net_weight() << " " << g.get_price_per_unit() << " " << g.get_quantity() << std::endl; + } + } + columns = new std::vector; OutputColumnModel *model = new OutputColumnModel(columns, this); @@ -34,8 +41,12 @@ OutputDialog::OutputDialog(QWidget *parent, std::vector *checks) unsigned short position = settings->get_all_settings()["output_order"][column.first]["position"]; ColumnType type = column.second; columns->at(position - 1) = (OutputColumn(QString::fromStdString(name), type)); + } + for (unsigned short i = 0; i < 6; i ++) + emit model->dataChanged(model->index(i, 0), model->index(i, 0)); + ui->printHeaderCheckBox->setChecked(settings->get_all_settings()["print_header"]); ui->printTotalCheckBox->setChecked(settings->get_all_settings()["print_total"]); } diff --git a/translations/en_US.ts b/translations/en_US.ts index e5aeafa..1228a45 100644 --- a/translations/en_US.ts +++ b/translations/en_US.ts @@ -298,32 +298,32 @@ No checks to parse - + Captcha was not solved correctly! Captcha was not solved correctly! - + Captcha is incorrect Captcha is incorrect - + Internal server error. Please, try again later. Internal server error. Please, try again later. - + Internal server error Internal server error - + Check not found. Please, ensure correctness of entered data. Check not found. Please, ensure correctness of entered data. - + Check was not found Check was not found @@ -645,7 +645,7 @@ Print total - + Кто здесь? diff --git a/translations/ru_RU.ts b/translations/ru_RU.ts index 52576e4..11af723 100644 --- a/translations/ru_RU.ts +++ b/translations/ru_RU.ts @@ -286,32 +286,32 @@ - + Captcha was not solved correctly! Капча была решена неверно! - + Captcha is incorrect Капча введена неверно - + Internal server error. Please, try again later. Внутренняя ошибка сервера. Пожалуйста, попробуйте снова позже. - + Internal server error Внутренняя ошибка сервера - + Check not found. Please, ensure correctness of entered data. Чек не найден. Пожалуйста, убедитесь в правильности введённых данных. - + Check was not found Чек не найден @@ -629,7 +629,7 @@ Печатать Итого - + Кто здесь? diff --git a/widgets/checkqueuetablemodel.cpp b/widgets/checkqueuetablemodel.cpp index 057c820..b7cc54b 100644 --- a/widgets/checkqueuetablemodel.cpp +++ b/widgets/checkqueuetablemodel.cpp @@ -3,7 +3,7 @@ #include #include #include - +#include CheckQueueTableModel::CheckQueueTableModel(std::vector *checks, QObject *parent) : checks(checks), QAbstractTableModel{parent} {} @@ -33,13 +33,15 @@ bool CheckQueueTableModel::setData(const QModelIndex &index, const QVariant &val if (!index.isValid() || index.row() >= checks->size()) return false; unsigned int row = index.row(); + Check &c = checks->at(row); switch (index.column()) { - case 0: { - checks->at(row).set_date(value.value()); + case 0: + c.set_date(value.value()); break; - } case 1: - checks->at(row).set_total(value.value()); + case 1: + c.set_total(value.value()); break; + emit dataChanged(index, index, {role}); } return true; } @@ -51,10 +53,8 @@ QVariant CheckQueueTableModel::headerData(int section, Qt::Orientation orientati switch (section) { case 0: return tr("Date&Time"); - break; case 1: return tr("Total"); - break; } } else if (role == Qt::DisplayRole && orientation == Qt::Vertical) { return section + 1; @@ -117,9 +117,16 @@ QMimeData* CheckQueueTableModel::mimeData(const QModelIndexList &indexes) const for (const QModelIndex &i : indexes) { if (i.isValid() && i.column() == 0) { - QString date = data(i, Qt::DisplayRole).toString(); - double total = data(index(i.row(), i.column() + 1), Qt::DisplayRole).toDouble(); - stream << date << total; + Check &c = checks->at(i.row()); + QString date = QString::fromStdString(c.get_date()), + fn = QString::fromStdString(c.get_fn()), + fd = QString::fromStdString(c.get_fd()), + fi = QString::fromStdString(c.get_fi()); + double total = c.get_total(); + OperationType type = c.get_operationType(); + std::vector goods = c.get_goods(); + + stream << date << total << type << fn << fd << fi << goods; } } mimeData->setData(CheckQueueTableModel::MimeType, encodedData); @@ -138,23 +145,40 @@ bool CheckQueueTableModel::dropMimeData(const QMimeData *data, Qt::DropAction ac int rows = 0; while (!stream.atEnd()) { - QString date; + QString date, fn, fd, fi; double total; - stream >> date >> total; - Check c; - c.set_date(date.toStdString()); - c.set_total(total); + OperationType type; + std::vector goods; + + stream >> date >> total >> type >> fn >> fd >> fi >> goods; + Check c = Check( + date.toStdString(), + total, + type, + fn.toStdString(), + fd.toStdString(), + fi.toStdString(), + goods + ); newItems.push_back(c); ++rows; } insertRows(row, rows, QModelIndex()); - for (Check &c : newItems) { - QModelIndex date_index = index(row, 0, QModelIndex()); - QModelIndex total_index = index(row, 1, QModelIndex()); - setData(date_index, QVariant::fromValue(c.get_date()), Qt::EditRole); - setData(total_index, QVariant::fromValue(c.get_total()), Qt::EditRole); + for (Check item : newItems) { + // (*checks)[row] = std::move(item); + Check &c = checks->at(row); + c.set_date(item.get_date()); + c.set_total(item.get_total()); + c.set_fn(item.get_fn()); + c.set_fd(item.get_fd()); + c.set_fi(item.get_fi()); + c.set_operation_type(item.get_operationType()); + for (Goods &g : item.get_goods()) { + c.add_goods(g); + } + emit dataChanged(index(row, 0), index(row, 1)); row++; }