Compare commits

...

2 Commits

Author SHA1 Message Date
leca b67d575645 saving output settings 2025-05-17 15:43:05 +03:00
leca 33ea7ff459 load output order from settings 2025-05-17 15:06:47 +03:00
11 changed files with 88 additions and 230 deletions

View File

@ -95,7 +95,7 @@ set(PROJECT_SOURCES
net/net.h net/net.cpp
settings/settings.h settings/settings.cpp
widgets/tablewidgetmovable.hpp widgets/tablewidgetmovable.cpp
widgets/checklistviewwidget.h widgets/checklistviewwidget.cpp
widgets/outputcolumn.h widgets/outputcolumn.cpp
widgets/outputcolumnmodel.h widgets/outputcolumnmodel.cpp

View File

@ -63,7 +63,9 @@ int main(int argc, char *argv[]) {
std::cout << QObject::tr("Using locale: ").toStdString() << lang.toStdString() << std::endl;
translator.load(":/translation/" + lang + ".qm");
if (!translator.load(":/translation/" + lang + ".qm")) {
std::cerr << "Could not load translation!!" << std::endl;
}
app.installTranslator(&translator);
#endif
MainWindow w;

View File

@ -11,51 +11,39 @@
#include <outputcolumnmodel.h>
#include "settings/settings.h"
#include "utils/utils.h"
#include <map>
OutputDialog::OutputDialog(QWidget *parent, std::vector<Check> *checks)
: QDialog(parent), ui(new Ui::OutputDialog), checks(checks),
options(OutputOptions()) {
Settings settings(get_path_relative_to_home(".local/share/checks_parser/settings.json"));
settings = new Settings(get_path_relative_to_home(".local/share/checks_parser/settings.json"));
ui->setupUi(this);
columns = new std::vector<OutputColumn>;
columns->push_back(OutputColumn(tr("Date"), ColumnType::date));
columns->push_back(OutputColumn(tr("Goods name"), ColumnType::goods_name));
columns->push_back(OutputColumn(tr("Goods price per unit"), ColumnType::goods_price_per_unit));
columns->push_back(OutputColumn(tr("Goods quantity"), ColumnType::goods_quantity));
columns->push_back(OutputColumn(tr("Goods net weight"), ColumnType::goods_net_weight));
columns->push_back(OutputColumn(tr("Goods total"), ColumnType::goods_total));
OutputColumnModel *model = new OutputColumnModel(&(*columns), this);
ui->listView->setModel(model);
// ui->tableWidget->item(0, 1)->setText(QString::fromStdString(settings.get_all_settings()["output_order"]["date"]["name"]));
// ui->tableWidget->item(0, 0)->setText(QString::number((int)settings.get_all_settings()["output_order"]["date"]["position"]));
for (unsigned short i = 0; i < 6; i ++)
columns->push_back(OutputColumn(tr("Кто здесь?"), ColumnType::date));
// ui->tableWidget->item(1, 1)->setText(QString::fromStdString(settings.get_all_settings()["output_order"]["goods_name"]["name"]));
// ui->tableWidget->item(1, 0)->setText(QString::number((int)settings.get_all_settings()["output_order"]["goods_name"]["position"]));
for (auto &column : column_names) {
std::string name = settings->get_all_settings()["output_order"][column.first]["name"];
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));
}
// ui->tableWidget->item(2, 1)->setText(QString::fromStdString(settings.get_all_settings()["output_order"]["goods_price_per_unit"]["name"]));
// ui->tableWidget->item(2, 0)->setText(QString::number((int)settings.get_all_settings()["output_order"]["goods_price_per_unit"]["position"]));
// ui->tableWidget->item(3, 1)->setText(QString::fromStdString(settings.get_all_settings()["output_order"]["goods_quantity"]["name"]));
// ui->tableWidget->item(3, 0)->setText(QString::number((int)settings.get_all_settings()["output_order"]["goods_quantity"]["position"]));
// ui->tableWidget->item(4, 1)->setText(QString::fromStdString(settings.get_all_settings()["output_order"]["goods_net_weight"]["name"]));
// ui->tableWidget->item(4, 0)->setText(QString::number((int)settings.get_all_settings()["output_order"]["goods_net_weight"]["position"]));
// ui->tableWidget->item(5, 1)->setText(QString::fromStdString(settings.get_all_settings()["output_order"]["goods_total"]["name"]));
// ui->tableWidget->item(5, 0)->setText(QString::number((int)settings.get_all_settings()["output_order"]["goods_total"]["position"]));
ui->printHeaderCheckBox->setChecked(settings.get_all_settings()["print_header"]);
ui->printTotalCheckBox->setChecked(settings.get_all_settings()["print_total"]);
ui->printHeaderCheckBox->setChecked(settings->get_all_settings()["print_header"]);
ui->printTotalCheckBox->setChecked(settings->get_all_settings()["print_total"]);
}
OutputDialog::~OutputDialog() { delete ui; }
OutputDialog::~OutputDialog() {
delete settings;
delete ui;
}
void OutputDialog::on_buttonBox_accepted() {
std::ofstream output_file(this->options.get_path());
@ -67,7 +55,6 @@ void OutputDialog::on_buttonBox_accepted() {
for (auto it = check.get_goods().begin(); it != check.get_goods().end(); it++, row_number++) {
for (int i = 0; i < columns->size(); i ++) {
OutputColumn &column = columns->at(i);
std::cout << column.get_text().toStdString() << std::endl;
switch (column.get_column_type()) {
case ColumnType::date:
if (row_number == 0) output_file << check.get_date();
@ -102,22 +89,9 @@ void OutputDialog::on_buttonBox_accepted() {
}
}
output_file.close();
save_settings();
}
// void update_settings(OutputOptions &options, ColumnType t, std::string name,
// int value) {
// Column column;
// column.type = t;
// column.name = name;
// column.position = value;
// if (value) {
// options.add_or_update_column(column);
// } else {
// options.remove_column(t);
// }
// }
void OutputDialog::on_chooseFileButton_clicked() {
QString filename = QFileDialog::getSaveFileName();
this->options.set_path(filename.toStdString());
@ -144,3 +118,13 @@ void OutputDialog::print_header(std::ofstream *output_file) {
*output_file << std::endl;
}
}
void OutputDialog::save_settings() {
for (int i = 0; i < columns->size(); i ++) {
OutputColumn &column = columns->at(i);
std::string key = find_key_by_value(column_names, column.get_column_type());
settings->get_all_settings()["output_order"][key]["name"] = column.get_text().toStdString();
settings->get_all_settings()["output_order"][key]["position"] = i + 1;
}
settings->flush();
}

View File

@ -17,6 +17,16 @@ class OutputDialog : public QDialog {
OutputOptions options;
std::vector<Check> *checks;
std::vector<OutputColumn> *columns;
Settings *settings;
const std::map<std::string, ColumnType> column_names = {
{"date", ColumnType::date},
{"goods_name", ColumnType::goods_name},
{"goods_price_per_unit", ColumnType::goods_price_per_unit},
{"goods_quantity", ColumnType::goods_quantity},
{"goods_net_weight", ColumnType::goods_net_weight},
{"goods_total", ColumnType::goods_total}
};
public:
explicit OutputDialog(QWidget *parent = nullptr, std::vector<Check> *checks = nullptr);
@ -35,6 +45,8 @@ private:
Ui::OutputDialog *ui;
void print_header(std::ofstream *output_file);
void save_settings();
};
#endif // OUTPUTDIALOG_H

View File

@ -537,38 +537,28 @@
<translation>Print header</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="24"/>
<source>Date</source>
<translation>Date</translation>
<translation type="vanished">Date</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="25"/>
<source>Goods name</source>
<translation>Goods name</translation>
<translation type="vanished">Goods name</translation>
</message>
<message>
<source>Goods price</source>
<translation type="vanished">Goods price</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="26"/>
<source>Goods price per unit</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="27"/>
<source>Goods quantity</source>
<translation>Goods quantity</translation>
<translation type="vanished">Goods quantity</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="28"/>
<source>Goods net weight</source>
<translation>Goods net weight</translation>
<translation type="vanished">Goods net weight</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="29"/>
<source>Goods total</source>
<translation>Goods total</translation>
<translation type="vanished">Goods total</translation>
</message>
<message>
<source>position</source>
@ -627,6 +617,11 @@
<source>Print total</source>
<translation>Print total</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="30"/>
<source>Кто здесь?</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>QObject</name>

View File

@ -525,33 +525,24 @@
<translation>Печатать заголовок</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="25"/>
<source>Goods name</source>
<translation>Имя товара</translation>
<translation type="vanished">Имя товара</translation>
</message>
<message>
<source>Goods price</source>
<translation type="vanished">Цена товара</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="26"/>
<source>Goods price per unit</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="27"/>
<source>Goods quantity</source>
<translation>Количество товара</translation>
<translation type="vanished">Количество товара</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="28"/>
<source>Goods net weight</source>
<translation>Масса нетто товара</translation>
<translation type="vanished">Масса нетто товара</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="29"/>
<source>Goods total</source>
<translation>Всего за товар</translation>
<translation type="vanished">Всего за товар</translation>
</message>
<message>
<source>position</source>
@ -611,8 +602,8 @@
<translation>Печатать Итого</translation>
</message>
<message>
<location filename="../outputdialog.cpp" line="24"/>
<source>Date</source>
<location filename="../outputdialog.cpp" line="30"/>
<source>Кто здесь?</source>
<translation type="unfinished"></translation>
</message>
</context>

View File

@ -1,3 +1,4 @@
#include "output/output_options.h"
#include <utils/utils.h>
#ifdef BUILD_OFD_BINARYEYE_SCAN
# include <arpa/inet.h>
@ -80,11 +81,31 @@ bool areAllSizesEqual(const std::vector<T>& v1, const std::vector<T>& v2,
return (v1.size() == v2.size() && v2.size() == v3.size() && v3.size() == v4.size());
}
// template<typename K, typename V>
// K find_key_by_value(std::map<K, V> &m, V value) {
// for (auto& entry : m) {
// if (value == entry.second) {
// return entry.first;
// }
// }
// return K();
// }
std::string find_key_by_value(const std::map<std::string, ColumnType> &m, ColumnType value) {
for (auto& entry : m) {
if (value == entry.second) {
return entry.first;
}
}
return "";
}
//ужас
template bool vector_contains_element<std::string>(const std::vector<std::string>& vector, const std::string& to_find);
template bool areAllSizesEqual(const std::vector<std::string>& v1, const std::vector<std::string>& v2,
const std::vector<std::string>& v3, const std::vector<std::string>& v4);
// template std::string find_key_by_value(std::map<std::string, ColumnType> &, ColumnType);
std::vector<std::string> split(std::string s, std::string delimiter) {
std::vector<std::string> result;
size_t pos = 0;
@ -321,3 +342,4 @@ std::vector<std::string> read_file(std::string path) {
return lines;
}
#endif // ifdef BUILD_EMAIL_MODE

View File

@ -3,13 +3,19 @@
#include <string>
#include <vector>
#include <map>
#include "../check/check.h"
#include "output/output_options.h"
std::string to_utf8(std::wstring wide_string);
std::wstring from_utf8(std::string string);
std::string get_path_relative_to_home(std::string path);
// template <typename K, typename V>
// K find_key_by_value(std::map<K, V> &m, V value);
std::string find_key_by_value(const std::map<std::string, ColumnType> &m, ColumnType value);
template <typename T>
bool vector_contains_element(const std::vector<T> &vector, const T &to_find);
template <class T>

View File

@ -1,10 +1,9 @@
#include "outputcolumnmodel.h"
#include <QObject>
#include <outputcolumn.h>
#include <QJsonObject>
#include <QMimeData>
#include <QIODevice>
#include <outputcolumn.h>
OutputColumnModel::OutputColumnModel(std::vector<OutputColumn> *columns, QObject *parent)
: columns(columns), QAbstractListModel{parent} { }

View File

@ -1,136 +0,0 @@
#include "tablewidgetmovable.hpp"
#include <QDropEvent>
#include <iostream>
#include <QTableWidget>
#include <qcoreevent.h>
#include <QDebug>
TableWidgetMovable::TableWidgetMovable(QWidget *parent) : QTableWidget(parent) { }
// void TableWidgetMovable::dropEvent(QDropEvent *event) {
// if (event->source() == this) {
// // Get the index of the row being dragged
// QModelIndex sourceIndex = currentIndex();
// if (!sourceIndex.isValid()) {
// return;
// }
// // Get the index of the target row
// QModelIndex targetIndex = indexAt(event->pos());
// if (!targetIndex.isValid()) {
// return;
// }
// int sourceRow = sourceIndex.row();
// int targetRow = targetIndex.row();
// // Swap rows
// if (sourceRow != targetRow) {
// // Store the data of the source row
// QList<QTableWidgetItem*> itemsSource;
// for (int col = 0; col < columnCount(); ++col) {
// itemsSource.append(takeItem(sourceRow, col));
// }
// QList<QTableWidgetItem*> itemsTarget;
// for (int col = 0; col < columnCount(); ++col) {
// itemsTarget.append(takeItem(targetRow, col));
// }
// // Insert the items into the target row
// for (int col = 0; col < columnCount(); ++col) {
// setItem(targetRow, col, itemsSource[col]);
// setItem(sourceRow, col, itemsTarget[col]);
// }
// // for (int col = 0; col < columnCount(); ++col) {
// // }
// // Remove the original row
// // removeRow(sourceRow < targetRow ? sourceRow : sourceRow + 1);
// }
// event->acceptProposedAction();
// } else {
// QTableWidget::dropEvent(event);
// }
// }
// TOOD: fix None of these works. WIP
// void TableWidgetMovable::dropEvent(QDropEvent *event) {
// std::cout << event->type() << std::endl;
// if(event->source() == this && event->type() == QEvent::Type::Drop) {
// int oldRow = this->selectedItems()[0]->row();
// int oldColumn = this->selectedItems()[0]->column();
// int newRow = this->indexAt(event->pos()).row();
// int newColumn = this->indexAt(event->pos()).column();
// auto oldCell = this->cellWidget(oldRow, oldColumn);
// auto newCell = this->cellWidget(newRow, newColumn);
// this->removeCellWidget(oldRow, oldColumn);
// this->removeCellWidget(newRow, newColumn);
// this->setCellWidget(newRow, newColumn, oldCell);
// this->setCellWidget(oldRow, oldColumn, newCell);
// event->accept();
// }
// }
// void TableWidgetMovable::dropEvent(QDropEvent *event) {
// std::cout << event->type() << std::endl;
// if(event->source() == this && event->type() == QEvent::Type::Drop) {
// int oldRow = this->selectedItems()[0]->row();
// int oldColumn = this->selectedItems()[0]->column();
// int newRow = this->indexAt(event->pos()).row();
// int newColumn = this->indexAt(event->pos()).column();
// std::cout << oldRow << " " << oldColumn<< " " << newRow << " " << newColumn << std::endl;
// QTableWidgetItem *from = this->itemAt(oldColumn, oldRow), *to = this->itemAt(newColumn, newRow);
// // QList<QTableWidgetItem*> selectedItems = this->selectedItems();
// if(newRow == -1) {
// std::cout << newRow << std::endl;
// // newRow = this->rowCount();
// return;
// }
// // QTableWidgetItem bufferTo, bufferFrom;
// QTableWidgetItem *bufferTo = new QTableWidgetItem("123"), *bufferFrom = new QTableWidgetItem("321");
// std::cout << to->text().toStdString() << " " << from->text().toStdString() << std::endl;
// *bufferTo = *to;
// *bufferFrom = *from;
// // this->setItem(newRow, newColumn, nullptr);
// // this->setItem(oldRow, oldColumn, nullptr);
// this->takeItem(newRow, newColumn);
// this->takeItem(oldRow, oldColumn);
// this->setItem(newRow, newColumn, from);
// // this->setItem(oldRow, oldColumn, to);
// // *to = *from;
// // *from = buffer;
// return;
// // int i;
// // for(i = 0; i < selectedItems.length()/this->columnCount(); i++)
// // this->insertRow(newRow);
// // int currentOldRow = -1;
// // int currentNewRow = newRow-1;
// // QList<int> deleteRows;
// // foreach(selectedItem, selectedItems) {
// // int column = selectedItem->column();
// // if(selectedItem->row() != currentOldRow) {
// // currentOldRow = selectedItem->row();
// // deleteRows.append(currentOldRow);
// // currentNewRow++;
// // }
// // this->takeItem(currentOldRow, column);
// // this->setItem(currentNewRow, column, selectedItem);
// // }
// // for(i = deleteRows.count()-1; i>=0; i--)
// // this->removeRow(deleteRows.at(i));
// }
// }

View File

@ -1,17 +0,0 @@
#ifndef TABLEWIDGETMOVABLE_HPP
#define TABLEWIDGETMOVABLE_HPP
#include <QObject>
#include <QTableWidget>
#include <QWidget>
class TableWidgetMovable : public QTableWidget
{
Q_OBJECT
public:
TableWidgetMovable(QWidget *parent = nullptr);
// void dropEvent(QDropEvent *event);
// void swapRows(int row1, int row2);
};
#endif // TABLEWIDGETMOVABLE_HPP