From 2fac4d0e400a4aafc486ca2848087e3905bf0db0 Mon Sep 17 00:00:00 2001 From: unelsson Date: Sun, 29 Jan 2023 17:25:25 +0200 Subject: [PATCH] Use struct with sensible member names --- apps/opencs/view/filter/editwidget.cpp | 37 ++++++++++++--------- apps/opencs/view/filter/editwidget.hpp | 9 +++-- apps/opencs/view/filter/filterbox.cpp | 7 ++-- apps/opencs/view/filter/filterbox.hpp | 6 ++-- apps/opencs/view/filter/filterdata.hpp | 21 ++++++++++++ apps/opencs/view/filter/recordfilterbox.cpp | 7 ++-- apps/opencs/view/filter/recordfilterbox.hpp | 6 ++-- apps/opencs/view/world/tablesubview.cpp | 31 +++++++++++------ 8 files changed, 79 insertions(+), 45 deletions(-) create mode 100644 apps/opencs/view/filter/filterdata.hpp diff --git a/apps/opencs/view/filter/editwidget.cpp b/apps/opencs/view/filter/editwidget.cpp index d83563da7d..d3b4af4e3c 100644 --- a/apps/opencs/view/filter/editwidget.cpp +++ b/apps/opencs/view/filter/editwidget.cpp @@ -11,6 +11,8 @@ #include #include +#include "filterdata.hpp" + #include #include @@ -96,19 +98,21 @@ void CSVFilter::EditWidget::filterRowsInserted(const QModelIndex& parent, int st textChanged(text()); } -void CSVFilter::EditWidget::createFilterRequest( - std::vector, std::vector>>& filterSource, - Qt::DropAction action) +void CSVFilter::EditWidget::createFilterRequest(const std::vector& sourceFilter, Qt::DropAction action) { std::string stringOrValue = "string"; - std::vector>> newFilter; + std::vector newFilter; - for (auto pair : filterSource) + for (auto filterData : sourceFilter) { - std::string searchString = std::visit(FilterVisitor(), pair.first).first; - stringOrValue = std::visit(FilterVisitor(), pair.first).second; - std::vector column = pair.second; - newFilter.emplace_back(std::make_pair(searchString, column)); + FilterData newFilterData; + std::pair pair = std::visit(FilterVisitor(), filterData.searchData); + std::string searchString = pair.first; + stringOrValue = pair.second; + std::vector columns; + newFilterData.searchData = searchString; + newFilterData.columns = filterData.columns; + newFilter.emplace_back(newFilterData); } const unsigned count = newFilter.size(); @@ -215,10 +219,9 @@ void CSVFilter::EditWidget::createFilterRequest( } } -std::string CSVFilter::EditWidget::generateFilter( - std::pair>& seekedString, std::string stringOrValue) const +std::string CSVFilter::EditWidget::generateFilter(const FilterData& filterData, std::string stringOrValue) const { - const unsigned columns = seekedString.second.size(); + const unsigned columns = filterData.columns.size(); bool multipleColumns = false; switch (columns) @@ -235,9 +238,11 @@ std::string CSVFilter::EditWidget::generateFilter( break; } - std::string quotesResolved = seekedString.first; + std::string quotesResolved; + if (std::get_if(&filterData.searchData)) + quotesResolved = *(std::get_if(&filterData.searchData)); if (stringOrValue == "string") - quotesResolved = '"' + seekedString.first + '"'; + quotesResolved = '"' + std::get(filterData.searchData) + '"'; std::stringstream ss; if (multipleColumns) @@ -245,7 +250,7 @@ std::string CSVFilter::EditWidget::generateFilter( ss << "or("; for (unsigned i = 0; i < columns; ++i) { - ss << stringOrValue << "(" << '"' << seekedString.second[i] << '"' << ',' << quotesResolved << ')'; + ss << stringOrValue << "(" << '"' << filterData.columns[i] << '"' << ',' << quotesResolved << ')'; if (i + 1 != columns) ss << ','; } @@ -253,7 +258,7 @@ std::string CSVFilter::EditWidget::generateFilter( } else { - ss << stringOrValue << '(' << '"' << seekedString.second[0] << "\"," << quotesResolved << ")"; + ss << stringOrValue << '(' << '"' << filterData.columns[0] << "\"," << quotesResolved << ")"; } return ss.str(); diff --git a/apps/opencs/view/filter/editwidget.hpp b/apps/opencs/view/filter/editwidget.hpp index 52e3f28426..981e570f6b 100644 --- a/apps/opencs/view/filter/editwidget.hpp +++ b/apps/opencs/view/filter/editwidget.hpp @@ -11,6 +11,8 @@ #include #include +#include "filterdata.hpp" + #include "../../model/filter/parser.hpp" class QModelIndex; @@ -65,17 +67,14 @@ namespace CSVFilter public: EditWidget(CSMWorld::Data& data, QWidget* parent = nullptr); - void createFilterRequest( - std::vector, std::vector>>& filterSource, - Qt::DropAction action); + void createFilterRequest(const std::vector& sourceFilter, Qt::DropAction action); signals: void filterChanged(std::shared_ptr filter); private: - std::string generateFilter( - std::pair>& seekedString, std::string stringOrValue) const; + std::string generateFilter(const FilterData& filterData, std::string stringOrValue) const; void contextMenuEvent(QContextMenuEvent* event) override; diff --git a/apps/opencs/view/filter/filterbox.cpp b/apps/opencs/view/filter/filterbox.cpp index ed124ce401..10ba95a55c 100644 --- a/apps/opencs/view/filter/filterbox.cpp +++ b/apps/opencs/view/filter/filterbox.cpp @@ -9,6 +9,7 @@ #include #include +#include "filterdata.hpp" #include "recordfilterbox.hpp" #include @@ -71,9 +72,7 @@ void CSVFilter::FilterBox::dragMoveEvent(QDragMoveEvent* event) event->accept(); } -void CSVFilter::FilterBox::createFilterRequest( - std::vector, std::vector>>& filterSource, - Qt::DropAction action) +void CSVFilter::FilterBox::createFilterRequest(const std::vector& sourceFilter, Qt::DropAction action) { - mRecordFilterBox->createFilterRequest(filterSource, action); + mRecordFilterBox->createFilterRequest(sourceFilter, action); } diff --git a/apps/opencs/view/filter/filterbox.hpp b/apps/opencs/view/filter/filterbox.hpp index 9320147cff..3dcd6ca7e1 100644 --- a/apps/opencs/view/filter/filterbox.hpp +++ b/apps/opencs/view/filter/filterbox.hpp @@ -10,6 +10,8 @@ #include #include +#include "filterdata.hpp" + class QDragEnterEvent; class QDragMoveEvent; class QDropEvent; @@ -40,9 +42,7 @@ namespace CSVFilter void setRecordFilter(const std::string& filter); - void createFilterRequest( - std::vector, std::vector>>& filterSource, - Qt::DropAction action); + void createFilterRequest(const std::vector& sourceFilter, Qt::DropAction action); private: void dragEnterEvent(QDragEnterEvent* event) override; diff --git a/apps/opencs/view/filter/filterdata.hpp b/apps/opencs/view/filter/filterdata.hpp new file mode 100644 index 0000000000..4be6499867 --- /dev/null +++ b/apps/opencs/view/filter/filterdata.hpp @@ -0,0 +1,21 @@ +#ifndef FILTERDATA_HPP +#define FILTERDATA_HPP + +#include +#include +#include + +#include + +namespace CSVFilter +{ + + struct FilterData + { + std::variant searchData; + std::vector columns; + }; + +} + +#endif // FILTERDATA_HPP diff --git a/apps/opencs/view/filter/recordfilterbox.cpp b/apps/opencs/view/filter/recordfilterbox.cpp index e37145931e..deed76bfb8 100644 --- a/apps/opencs/view/filter/recordfilterbox.cpp +++ b/apps/opencs/view/filter/recordfilterbox.cpp @@ -7,6 +7,7 @@ #include #include "editwidget.hpp" +#include "filterdata.hpp" CSVFilter::RecordFilterBox::RecordFilterBox(CSMWorld::Data& data, QWidget* parent) : QWidget(parent) @@ -34,9 +35,7 @@ void CSVFilter::RecordFilterBox::setFilter(const std::string& filter) mEdit->setText(QString::fromUtf8(filter.c_str())); } -void CSVFilter::RecordFilterBox::createFilterRequest( - std::vector, std::vector>>& filterSource, - Qt::DropAction action) +void CSVFilter::RecordFilterBox::createFilterRequest(const std::vector& sourceFilter, Qt::DropAction action) { - mEdit->createFilterRequest(filterSource, action); + mEdit->createFilterRequest(sourceFilter, action); } diff --git a/apps/opencs/view/filter/recordfilterbox.hpp b/apps/opencs/view/filter/recordfilterbox.hpp index 867952d2ad..d7fdb5cbac 100644 --- a/apps/opencs/view/filter/recordfilterbox.hpp +++ b/apps/opencs/view/filter/recordfilterbox.hpp @@ -10,6 +10,8 @@ #include #include +#include "filterdata.hpp" + namespace CSMFilter { class Node; @@ -37,9 +39,7 @@ namespace CSVFilter void useFilterRequest(const std::string& idOfFilter); - void createFilterRequest( - std::vector, std::vector>>& filterSource, - Qt::DropAction action); + void createFilterRequest(const std::vector& sourceFilter, Qt::DropAction action); signals: diff --git a/apps/opencs/view/world/tablesubview.cpp b/apps/opencs/view/world/tablesubview.cpp index c456ca95c2..8646ba16a3 100644 --- a/apps/opencs/view/world/tablesubview.cpp +++ b/apps/opencs/view/world/tablesubview.cpp @@ -24,6 +24,7 @@ #include "../doc/sizehint.hpp" #include "../filter/filterbox.hpp" +#include "../filter/filterdata.hpp" #include "table.hpp" #include "tablebottombox.hpp" @@ -157,8 +158,7 @@ void CSVWorld::TableSubView::cloneRequest(const CSMWorld::UniversalId& toClone) void CSVWorld::TableSubView::createFilterRequest( std::vector& types, std::pair columnSearchData, Qt::DropAction action) { - std::vector, std::vector>> filterSource; - + std::vector sourceFilter; std::vector refIdColumns = mTable->getColumnsWithDisplay( CSMWorld::TableMimeData::convertEnums(CSMWorld::UniversalId::Type_Referenceable)); bool hasRefIdDisplay = !refIdColumns.empty(); @@ -169,28 +169,39 @@ void CSVWorld::TableSubView::createFilterRequest( std::vector col = mTable->getColumnsWithDisplay(CSMWorld::TableMimeData::convertEnums(type)); if (!col.empty()) { - filterSource.emplace_back(it->getId(), col); + CSVFilter::FilterData filterData; + filterData.searchData = it->getId(); + filterData.columns = col; + sourceFilter.emplace_back(filterData); } if (hasRefIdDisplay && CSMWorld::TableMimeData::isReferencable(type)) { - filterSource.emplace_back(it->getId(), refIdColumns); + CSVFilter::FilterData filterData; + filterData.searchData = it->getId(); + filterData.columns = refIdColumns; + sourceFilter.emplace_back(filterData); } } - if (!filterSource.empty()) - mFilterBox->createFilterRequest(filterSource, action); + if (!sourceFilter.empty()) + mFilterBox->createFilterRequest(sourceFilter, action); else { + std::vector sourceFilterByValue; + QVariant qData = columnSearchData.first; std::string searchColumn = columnSearchData.second; - - std::vector, std::vector>> valueFilterSource; std::vector searchColumns; searchColumns.emplace_back(searchColumn); - valueFilterSource.emplace_back(qData, searchColumns); - mFilterBox->createFilterRequest(valueFilterSource, action); + CSVFilter::FilterData filterData; + filterData.searchData = qData; + filterData.columns = searchColumns; + + sourceFilterByValue.emplace_back(filterData); + + mFilterBox->createFilterRequest(sourceFilterByValue, action); } }