From f32da712e826a594cd5b0c6cabdad3bc27a7397d Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 17 May 2025 14:08:30 +0300 Subject: [PATCH] full rework of the output order --- CMakeLists.txt | 2 + main.cpp | 1 + output/output_options.cpp | 43 +------- output/output_options.h | 26 ++--- outputdialog.cpp | 162 +++++++++++++++--------------- outputdialog.h | 4 + scenes/outputdialog.ui | 171 ++++---------------------------- translations/en_US.ts | 66 ++++++------ translations/ru_RU.ts | 74 ++++++-------- widgets/checklistviewwidget.cpp | 2 - widgets/outputcolumn.cpp | 17 ++++ widgets/outputcolumn.h | 23 +++++ widgets/outputcolumnmodel.cpp | 151 ++++++++++++++++++++++++++++ widgets/outputcolumnmodel.h | 37 +++++++ 14 files changed, 407 insertions(+), 372 deletions(-) create mode 100644 widgets/outputcolumn.cpp create mode 100644 widgets/outputcolumn.h create mode 100644 widgets/outputcolumnmodel.cpp create mode 100644 widgets/outputcolumnmodel.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 820f89d..36882b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,6 +97,8 @@ set(PROJECT_SOURCES widgets/tablewidgetmovable.hpp widgets/tablewidgetmovable.cpp widgets/checklistviewwidget.h widgets/checklistviewwidget.cpp + widgets/outputcolumn.h widgets/outputcolumn.cpp + widgets/outputcolumnmodel.h widgets/outputcolumnmodel.cpp ${TRANSLATION_SOURCES} ) diff --git a/main.cpp b/main.cpp index 35c683f..f8880b7 100644 --- a/main.cpp +++ b/main.cpp @@ -33,6 +33,7 @@ int main(int argc, char *argv[]) { curl_global_init(CURL_GLOBAL_ALL); + std::string program_data_path = get_path_relative_to_home(".local/share/checks_parser"); create_directories(program_data_path); diff --git a/output/output_options.cpp b/output/output_options.cpp index 5a98394..cb12237 100644 --- a/output/output_options.cpp +++ b/output/output_options.cpp @@ -2,48 +2,6 @@ OutputOptions::OutputOptions() {} -void OutputOptions::add_or_update_column(Column &column) { - if (column_exist(column.type)) { - update_column(column, find_column(column.type)); - } else { - this->order.push_back(column); - } -} - -void OutputOptions::update_column(Column &column, unsigned short index) { - this->order[index] = column; -} - -void OutputOptions::remove_column(unsigned short index) { - this->order.erase(this->order.begin() + index); -} -void OutputOptions::remove_column(ColumnType t) { - this->order.erase(this->order.begin() + find_column(t)); -} - -unsigned short OutputOptions::find_column(ColumnType t) { - for (unsigned short i = 0; i < this->order.size(); i++) { - if (this->order[i].type == t) - return i; - } - - return -1; -} - -bool OutputOptions::column_exist(ColumnType t) { - for (unsigned short i = 0; i < this->order.size(); i++) { - if (this->order[i].type == t) - return true; - } - return false; -} - -Column &OutputOptions::get_column(ColumnType t) { - return this->order[find_column(t)]; -} - -std::vector &OutputOptions::get_columns() { return this->order; } - void OutputOptions::set_print_header(bool value) { this->print_header = value; } bool OutputOptions::get_print_header() { return this->print_header; } @@ -52,3 +10,4 @@ bool OutputOptions::get_print_total() { return this->print_total; } void OutputOptions::set_path(std::string path) { this->path = path; } std::string &OutputOptions::get_path() { return this->path; } + diff --git a/output/output_options.h b/output/output_options.h index dd169b0..81ff2a5 100644 --- a/output/output_options.h +++ b/output/output_options.h @@ -14,7 +14,7 @@ #include "../net/net.h" #include "../settings/settings.h" -enum class ColumnType { +enum ColumnType { date, goods_name, goods_price_per_unit, @@ -23,32 +23,26 @@ enum class ColumnType { goods_total }; -struct Column { // Example: - ColumnType type; // goods_name - std::string name; // "Товар" - unsigned int position; // "0" <-- 0 = "A", 1 = "B", etc.. column letter in - // table processor (i.e. excel or libreoffice) -} typedef Column; +// Q_DECLARE_METATYPE(ColumnType); + +// struct Column { // Example: +// ColumnType type; // goods_name +// std::string name; // "Товар" +// unsigned int position; // "0" <-- 0 = "A", 1 = "B", etc.. column letter in +// // table processor (i.e. excel or libreoffice) +// } typedef Column; class OutputOptions { - - std::vector order; bool print_header; bool print_total; std::string path; public: OutputOptions(); - - void add_or_update_column(Column &); - void update_column(Column&, unsigned short); void remove_column(unsigned short); void remove_column(ColumnType); unsigned short find_column(ColumnType); bool column_exist(ColumnType); - Column& get_column(ColumnType); - std::vector& get_columns(); - void set_print_header(bool); bool get_print_header(); @@ -60,3 +54,5 @@ public: }; #endif // OUTPUT_OPTIONS_H + + diff --git a/outputdialog.cpp b/outputdialog.cpp index b78c1f6..1c1f7db 100644 --- a/outputdialog.cpp +++ b/outputdialog.cpp @@ -3,8 +3,12 @@ #include "output/output_options.h" #include "ui_outputdialog.h" #include +#include #include +#include #include +#include +#include #include "settings/settings.h" #include "utils/utils.h" @@ -15,29 +19,36 @@ OutputDialog::OutputDialog(QWidget *parent, std::vector *checks) ui->setupUi(this); - ui->tableWidget->resizeColumnsToContents(); - ui->tableWidget->verticalHeader()->setSectionsMovable(true); - ui->tableWidget->verticalHeader()->setDragDropOverwriteMode(false); - ui->tableWidget->verticalHeader()->setDragEnabled(true); - ui->tableWidget->verticalHeader()->setDragDropMode(QAbstractItemView::InternalMove); + columns = new std::vector; - 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"])); + 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)); - 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"])); + OutputColumnModel *model = new OutputColumnModel(&(*columns), this); - 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->listView->setModel(model); - 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(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"])); - 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(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"])); - 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->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"]); @@ -46,65 +57,39 @@ OutputDialog::OutputDialog(QWidget *parent, std::vector *checks) OutputDialog::~OutputDialog() { delete ui; } -bool compare_position(Column &c1, Column &c2) { - return c1.position < c2.position; -} - void OutputDialog::on_buttonBox_accepted() { std::ofstream output_file(this->options.get_path()); - for (int i = 0; i < ui->tableWidget->rowCount(); i++) { - int position = ui->tableWidget->item(i, 0)->text().toInt(); - std::string name = ui->tableWidget->item(i, 1)->text().toStdString(); + print_header(&output_file); - Column c; - c.type = static_cast(i); - c.position = position; - c.name = name; - - this->options.add_or_update_column(c); - } - - std::sort(this->options.get_columns().begin(), - this->options.get_columns().end(), compare_position); - - if (options.get_print_header()) { - // output_file << "date," - for (auto &column : this->options.get_columns()) { - output_file << column.name - << (column.position == this->options.get_columns().size() - ? "" - : ","); - } - output_file << std::endl; - } for (Check &check : *checks) { - int i = 0; - // auto &goods : check.get_goods() - for (auto it = check.get_goods().begin(); it != check.get_goods().end(); i++, it++) { - for (auto &column : this->options.get_columns()) { - switch (column.type) { - case ColumnType::date: - if (i == 0) output_file << check.get_date(); - break; - case ColumnType::goods_name: - output_file << it->get_name(); - break; - case ColumnType::goods_price_per_unit: - output_file << std::fixed << std::setprecision(2) << it->get_price_per_unit(); - break; - case ColumnType::goods_quantity: - output_file << std::fixed << std::setprecision(2) << it->get_quantity(); - break; - case ColumnType::goods_net_weight: - output_file << it->get_net_weight(); - break; - case ColumnType::goods_total: - output_file << std::fixed << std::setprecision(2) << it->calculate_total_price(); - break; - } + int row_number = 0; + 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(); + break; + case ColumnType::goods_name: + output_file << it->get_name(); + break; + case ColumnType::goods_price_per_unit: + output_file << std::fixed << std::setprecision(2) << it->get_price_per_unit(); + break; + case ColumnType::goods_quantity: + output_file << std::fixed << std::setprecision(2) << it->get_quantity(); + break; + case ColumnType::goods_net_weight: + output_file << it->get_net_weight(); + break; + case ColumnType::goods_total: + output_file << std::fixed << std::setprecision(2) << it->calculate_total_price(); + break; + } - if (column.position != this->options.get_columns().size()) { + if (i < columns->size() - 1) { output_file << ","; } else { output_file << "\n"; @@ -119,19 +104,19 @@ void OutputDialog::on_buttonBox_accepted() { output_file.close(); } -void update_settings(OutputOptions &options, ColumnType t, std::string name, - int value) { - Column column; - column.type = t; - column.name = name; - column.position = value; +// 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); - } -} +// if (value) { +// options.add_or_update_column(column); +// } else { +// options.remove_column(t); +// } +// } void OutputDialog::on_chooseFileButton_clicked() { QString filename = QFileDialog::getSaveFileName(); @@ -146,3 +131,16 @@ void OutputDialog::on_printHeaderCheckBox_stateChanged(int value) { void OutputDialog::on_printTotalCheckBox_stateChanged(int value) { this->options.set_print_total(value); } + +void OutputDialog::print_header(std::ofstream *output_file) { + if (options.get_print_header()) { + for (unsigned int i = 0; i < columns->size(); i ++) { + OutputColumn column = columns->at(i); + (*output_file) << column.get_text().toStdString() + << (i == columns->size() - 1 + ? "" + : ","); + } + *output_file << std::endl; + } +} diff --git a/outputdialog.h b/outputdialog.h index 757b1d4..4a32ea5 100644 --- a/outputdialog.h +++ b/outputdialog.h @@ -5,6 +5,7 @@ #include "output/output_options.h" #include #include +#include namespace Ui { class OutputDialog; @@ -15,6 +16,7 @@ class OutputDialog : public QDialog { OutputOptions options; std::vector *checks; + std::vector *columns; public: explicit OutputDialog(QWidget *parent = nullptr, std::vector *checks = nullptr); @@ -31,6 +33,8 @@ private slots: private: Ui::OutputDialog *ui; + + void print_header(std::ofstream *output_file); }; #endif // OUTPUTDIALOG_H diff --git a/scenes/outputdialog.ui b/scenes/outputdialog.ui index dc83715..c0bb2f4 100644 --- a/scenes/outputdialog.ui +++ b/scenes/outputdialog.ui @@ -21,20 +21,6 @@ - - - - Choose - - - - - - - Print header - - - @@ -42,30 +28,15 @@ - - - - Qt::Orientation::Horizontal - - - QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok + + + + Choose - - - - 0 - 0 - - - - true - - - QAbstractScrollArea::SizeAdjustPolicy::AdjustToContentsOnFirstShow - + true @@ -76,137 +47,35 @@ QAbstractItemView::DragDropMode::InternalMove - Qt::DropAction::MoveAction + Qt::DropAction::TargetMoveAction true - - QAbstractItemView::SelectionMode::SingleSelection - QAbstractItemView::SelectionBehavior::SelectRows - - Qt::PenStyle::SolidLine + + + + + + Qt::Orientation::Horizontal - - false + + QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok - - true + + + + + + Print header - - - Date - - - - - Goods name - - - - - Goods price - - - - - Goods quantity - - - - - Goods net weight - - - - - Goods total - - - - - position - - - - - name - - - - - 1 - - - - - Date - - - - - 2 - - - - - Name - - - - - 3 - - - - - Price - - - - - 4 - - - - - Quantity - - - - - 5 - - - - - Net weight - - - - - 6 - - - - - Total price - - - - - TableWidgetMovable - QTableWidget -
widgets/tablewidgetmovable.hpp
-
-
diff --git a/translations/en_US.ts b/translations/en_US.ts index c4af473..702febe 100644 --- a/translations/en_US.ts +++ b/translations/en_US.ts @@ -527,113 +527,103 @@ Path to export: - + Choose Choose - + Print header Print header - - + Date Date - + Goods name Goods name - Goods price - Goods price + Goods price - + + Goods price per unit + + + + Goods quantity Goods quantity - + Goods net weight Goods net weight - + Goods total Goods total - position - position + position - name - name + name - 1 - 1 + 1 - Name - Name + Name - 2 - 2 + 2 - Price - Price + Price - 3 - 3 + 3 - Quantity - Quantity + Quantity - 4 - 4 + 4 - Net weight - Net Weight + Net Weight - 5 - 5 + 5 - 6 - 6 + 6 - Total price - Total price + Total price - + Print total Print total @@ -641,7 +631,7 @@ QObject - + Using locale: Using locale: diff --git a/translations/ru_RU.ts b/translations/ru_RU.ts index 46fc56b..5aa2014 100644 --- a/translations/ru_RU.ts +++ b/translations/ru_RU.ts @@ -515,121 +515,111 @@ Путь для экспорта: - + Choose Выбрать - + Print header Печатать заголовок - - - Date - - - - + Goods name Имя товара - Goods price - Цена товара + Цена товара - + + Goods price per unit + + + + Goods quantity Количество товара - + Goods net weight Масса нетто товара - + Goods total Всего за товар - position - позиция + позиция - name - алиас + алиас - 1 - 1 + 1 - Name - Имя + Имя - 2 - 2 + 2 - Price - Цена + Цена - 3 - 3 + 3 - Quantity - Количество + Количество - 4 - 4 + 4 - Net weight - Масса нетто + Масса нетто - 5 - 5 + 5 - 6 - 6 + 6 - Total price - Всего + Всего - + Print total Печатать Итого + + + Date + + QObject - + Using locale: Использую локаль: diff --git a/widgets/checklistviewwidget.cpp b/widgets/checklistviewwidget.cpp index 0ca4de6..7a01382 100644 --- a/widgets/checklistviewwidget.cpp +++ b/widgets/checklistviewwidget.cpp @@ -8,8 +8,6 @@ CheckListViewWidget::CheckListViewWidget(QWidget *parent, Check check) : QWidget(parent), check(check) { mw = (MainWindow*) parent; - - std::cout << "I was created with check with date " << check.get_date() << std::endl; QHBoxLayout *layout = new QHBoxLayout; QLabel *date_label = new QLabel(QString::fromStdString(check.get_date())); diff --git a/widgets/outputcolumn.cpp b/widgets/outputcolumn.cpp new file mode 100644 index 0000000..6987199 --- /dev/null +++ b/widgets/outputcolumn.cpp @@ -0,0 +1,17 @@ +#include "outputcolumn.h" + +#include + +OutputColumn::OutputColumn() { } + +OutputColumn::OutputColumn(QString text, ColumnType type) + : text(text), type(type) { } + +void OutputColumn::set_column_type(ColumnType type) { this->type = type; } +ColumnType OutputColumn::get_column_type() { return type; } + +void OutputColumn::set_text(const QString &text) { this->text = text; } +QString OutputColumn::get_text() { return text; } + + +Q_DECLARE_METATYPE(OutputColumn) diff --git a/widgets/outputcolumn.h b/widgets/outputcolumn.h new file mode 100644 index 0000000..b183289 --- /dev/null +++ b/widgets/outputcolumn.h @@ -0,0 +1,23 @@ +#ifndef OUTPUTCOLUMN_H +#define OUTPUTCOLUMN_H + +#include "output/output_options.h" +#include +#include + +class OutputColumn +{ + QString text; + ColumnType type; +public: + OutputColumn(); + OutputColumn(QString text, ColumnType type); + + void set_column_type(ColumnType type); + ColumnType get_column_type(); + + void set_text(const QString &text); + QString get_text(); +}; + +#endif // OUTPUTCOLUMN_H diff --git a/widgets/outputcolumnmodel.cpp b/widgets/outputcolumnmodel.cpp new file mode 100644 index 0000000..0afc80e --- /dev/null +++ b/widgets/outputcolumnmodel.cpp @@ -0,0 +1,151 @@ +#include "outputcolumnmodel.h" + +#include +#include +#include +#include +#include + +OutputColumnModel::OutputColumnModel(std::vector *columns, QObject *parent) + : columns(columns), QAbstractListModel{parent} { } + +int OutputColumnModel::rowCount(const QModelIndex &parent) const { + return columns->size(); +} + +QVariant OutputColumnModel::data(const QModelIndex &index, int role) const { + if (!index.isValid() || index.row() >= columns->size()) + return QVariant(); + + if (role == Qt::DisplayRole) + return QVariant::fromValue(columns->at(index.row()).get_text()); + else if (role == 0x0101) + return QVariant::fromValue(columns->at(index.row()).get_column_type()); + + return QVariant(); +} + +QVariant OutputColumnModel::headerData(int section, Qt::Orientation orientation, int role) const { + if (role != Qt::DisplayRole) + return QVariant(); + + if (orientation == Qt::Horizontal) + return QObject::tr("Column type alias"); + else + return QStringLiteral("Position %1").arg(section); +} + +bool OutputColumnModel::setData(const QModelIndex &index, const QVariant &value, int role) { + if (index.isValid()) { + OutputColumn &column = columns->at(index.row()); + if (role == 0x102) { + OutputColumn data = value.value(); + column.set_text(data.get_text()); + column.set_column_type(data.get_column_type()); + } else { + QString data = value.value(); + column.set_text(data); + } + + (*columns)[index.row()] = column; + emit dataChanged(index, index, {role}); + return true; + } + return false; +} + +Qt::DropActions OutputColumnModel::supportedDropActions() const { + return Qt::MoveAction; +} + +bool OutputColumnModel::insertRows(int position, int rows, const QModelIndex &index) { + beginInsertRows(QModelIndex(), position, position+rows-1); + + for (int row = 0; row < rows; ++row) + columns->emplace(columns->begin() + position, OutputColumn("Чё зыришь глаза пузыришь?", ColumnType::date)); + + endInsertRows(); + return true; +} + +bool OutputColumnModel::removeRows(int position, int rows, const QModelIndex &parent) { + beginRemoveRows(QModelIndex(), position, position+rows-1); + + for (int row = 0; row < rows; ++row) + columns->erase(std::next(columns->begin(), position)); + + endRemoveRows(); + return true; +} + +QStringList OutputColumnModel::mimeTypes() const { + QStringList types; + types << OutputColumnModel::MimeType; + return types; +} + +bool OutputColumnModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &) { + if ( action != Qt::MoveAction || !data->hasFormat(OutputColumnModel::MimeType)) + return false; + return true; +} + +Qt::ItemFlags OutputColumnModel::flags (const QModelIndex & index) const { + Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index); + + if (index.isValid()) + return Qt::ItemIsDragEnabled | Qt::ItemIsEditable | defaultFlags; + else + return Qt::ItemIsDropEnabled | defaultFlags; +} + +QMimeData* OutputColumnModel::mimeData(const QModelIndexList &indexes) const { + QMimeData* mimeData = new QMimeData; + QByteArray encodedData; + + QDataStream stream(&encodedData, QIODevice::WriteOnly); + + for (const QModelIndex &index : indexes) { + if (index.isValid()) { + QString text = data(index, Qt::DisplayRole).toString(); + ColumnType type = ColumnType(data(index, 0x0101).toInt()); + stream << text << type; + } + } + mimeData->setData(OutputColumnModel::MimeType, encodedData); + return mimeData; +} + +bool OutputColumnModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { + if (!canDropMimeData(data, action, row, column, parent)) + return false; + + if (action == Qt::IgnoreAction) + return true; + else if (action != Qt::MoveAction) + return false; + + QByteArray encodedData = data->data(OutputColumnModel::MimeType); + QDataStream stream(&encodedData, QIODevice::ReadOnly); + std::vector newItems; + int rows = 0; + + while (!stream.atEnd()) { + QString text; + ColumnType type; + stream >> text >> type; + newItems.push_back(OutputColumn(text, type)); + ++rows; + } + + insertRows(row, rows, QModelIndex()); + for (const OutputColumn &column : newItems) + { + QModelIndex idx = index(row, 0, QModelIndex()); + setData(idx, QVariant::fromValue(column), 0x102); + row++; + } + + return true; +} + diff --git a/widgets/outputcolumnmodel.h b/widgets/outputcolumnmodel.h new file mode 100644 index 0000000..23ad8be --- /dev/null +++ b/widgets/outputcolumnmodel.h @@ -0,0 +1,37 @@ +#ifndef OUTPUTCOLUMNMODEL_H +#define OUTPUTCOLUMNMODEL_H + +#include +#include +#include + +class OutputColumnModel : public QAbstractListModel +{ + Q_OBJECT + static constexpr const char* MimeType = "application/output.column.model"; + std::vector *columns; + +public: + OutputColumnModel(std::vector *columns, QObject *parent = nullptr); + + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role) const override; + QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const override; + + Qt::ItemFlags flags (const QModelIndex& index) const override; + bool setData(const QModelIndex &index, const QVariant &value, + int role = Qt::EditRole) override; + + Qt::DropActions supportedDropActions() const override; + + bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) override; + bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex()) override; + + QStringList mimeTypes() const override; + bool canDropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex&); + QMimeData* mimeData(const QModelIndexList &indexes) const; + bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); +}; + +#endif // OUTPUTCOLUMNMODEL_H