From 9b808a495ca59876f6d6c5553a64dde6fdaff6ce Mon Sep 17 00:00:00 2001 From: unelsson Date: Thu, 26 Jan 2023 00:05:51 +0200 Subject: [PATCH] Use std::variant and std::visit instead of function overloads, add float type search, fix QMetaType and Type conversion --- apps/opencs/view/filter/editwidget.cpp | 44 +++++++++++---------- apps/opencs/view/filter/editwidget.hpp | 31 ++++++++++++--- apps/opencs/view/filter/filterbox.cpp | 27 +++++-------- apps/opencs/view/filter/filterbox.hpp | 12 +++--- apps/opencs/view/filter/recordfilterbox.cpp | 12 +++--- apps/opencs/view/filter/recordfilterbox.hpp | 8 ++-- apps/opencs/view/world/tablesubview.cpp | 37 ++++++----------- apps/opencs/view/world/tablesubview.hpp | 5 ++- 8 files changed, 89 insertions(+), 87 deletions(-) diff --git a/apps/opencs/view/filter/editwidget.cpp b/apps/opencs/view/filter/editwidget.cpp index 8078a72486..d83563da7d 100644 --- a/apps/opencs/view/filter/editwidget.cpp +++ b/apps/opencs/view/filter/editwidget.cpp @@ -1,6 +1,7 @@ #include "editwidget.hpp" #include +#include #include #include @@ -8,6 +9,7 @@ #include #include #include +#include #include #include @@ -95,9 +97,21 @@ void CSVFilter::EditWidget::filterRowsInserted(const QModelIndex& parent, int st } void CSVFilter::EditWidget::createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action, std::string stringOrValue) + std::vector, std::vector>>& filterSource, + Qt::DropAction action) { - const unsigned count = filterSource.size(); + std::string stringOrValue = "string"; + std::vector>> newFilter; + + for (auto pair : filterSource) + { + 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)); + } + + const unsigned count = newFilter.size(); bool multipleElements = false; switch (count) // setting multipleElements; @@ -165,7 +179,7 @@ void CSVFilter::EditWidget::createFilterRequest( for (unsigned i = 0; i < count; ++i) { - ss << generateFilter(filterSource[i], stringOrValue); + ss << generateFilter(newFilter[i], stringOrValue); if (i + 1 != count) { @@ -186,7 +200,7 @@ void CSVFilter::EditWidget::createFilterRequest( ss << '!'; } - ss << generateFilter(filterSource[0], stringOrValue); + ss << generateFilter(newFilter[0], stringOrValue); if (!replaceMode) { @@ -201,21 +215,8 @@ void CSVFilter::EditWidget::createFilterRequest( } } -void CSVFilter::EditWidget::createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action) -{ - std::vector>> convertedFilter; - std::string stringOrValue = "value"; - for (auto pair : filterSource) - { - std::string a = std::to_string(pair.first); - std::vector b = pair.second; - convertedFilter.emplace_back(std::make_pair (a, b)); - } - createFilterRequest(convertedFilter, action, stringOrValue); -} - -std::string CSVFilter::EditWidget::generateFilter(std::pair>& seekedString, std::string stringOrValue) const +std::string CSVFilter::EditWidget::generateFilter( + std::pair>& seekedString, std::string stringOrValue) const { const unsigned columns = seekedString.second.size(); @@ -233,9 +234,10 @@ std::string CSVFilter::EditWidget::generateFilter(std::pair #include +#include #include #include #include +#include #include #include "../../model/filter/parser.hpp" @@ -29,6 +31,26 @@ namespace CSMWorld namespace CSVFilter { + struct FilterVisitor + { + std::pair operator()(const std::string& stringData) + { + std::string stringOrValue = "string"; + return std::make_pair(stringData, stringOrValue); + } + + std::pair operator()(const QVariant& variantData) + { + std::string stringOrValue; + QMetaType::Type dataType = static_cast(variantData.type()); + if (dataType == QMetaType::QString || dataType == QMetaType::Bool || dataType == QMetaType::Int) + stringOrValue = "string"; + if (dataType == QMetaType::Int || dataType == QMetaType::Float) + stringOrValue = "value"; + return std::make_pair(variantData.toString().toStdString(), stringOrValue); + } + }; + class EditWidget : public QLineEdit { Q_OBJECT @@ -44,17 +66,16 @@ namespace CSVFilter EditWidget(CSMWorld::Data& data, QWidget* parent = nullptr); void createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action, std::string stringOrValue = "string"); - - void createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action); + std::vector, std::vector>>& filterSource, + Qt::DropAction action); signals: void filterChanged(std::shared_ptr filter); private: - std::string generateFilter(std::pair>& seekedString, std::string stringOrValue) const; + std::string generateFilter( + std::pair>& seekedString, 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 fed5873945..ed124ce401 100644 --- a/apps/opencs/view/filter/filterbox.cpp +++ b/apps/opencs/view/filter/filterbox.cpp @@ -2,10 +2,12 @@ #include #include +#include #include #include #include +#include #include "recordfilterbox.hpp" @@ -48,21 +50,15 @@ void CSVFilter::FilterBox::dropEvent(QDropEvent* event) QModelIndex index = mime->getIndexAtDragStart(); const CSVWorld::DragRecordTable* dragTable = mime->getTableOfDragStart(); - std::string searchString = ""; - std::string searchColumn = ""; - bool isValue(false); + QVariant qData; + std::string searchColumn; if (index.isValid() && dragTable) - { - QVariant::Type dataType = dragTable->model()->data(index).type(); + { + qData = dragTable->model()->data(index); searchColumn = dragTable->model()->headerData(index.column(), Qt::Horizontal).toString().toStdString(); - Log(Debug::Warning) << "Data: " << searchString; - Log(Debug::Warning) << "Header: " << searchColumn; - Log(Debug::Warning) << "Type:" << dataType; - if (dataType == QMetaType::QString || dataType == QMetaType::Bool || dataType == QMetaType::Int) searchString = dragTable->model()->data(index).toString().toStdString(); - if (dataType == QMetaType::Int) isValue = true; } - emit recordDropped(universalIdData, event->proposedAction(), searchString, searchColumn, isValue); + emit recordDropped(universalIdData, std::make_pair(qData, searchColumn), event->proposedAction()); } void CSVFilter::FilterBox::dragEnterEvent(QDragEnterEvent* event) @@ -76,13 +72,8 @@ void CSVFilter::FilterBox::dragMoveEvent(QDragMoveEvent* event) } void CSVFilter::FilterBox::createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action) -{ - mRecordFilterBox->createFilterRequest(filterSource, action); -} - -void CSVFilter::FilterBox::createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action) + std::vector, std::vector>>& filterSource, + Qt::DropAction action) { mRecordFilterBox->createFilterRequest(filterSource, action); } diff --git a/apps/opencs/view/filter/filterbox.hpp b/apps/opencs/view/filter/filterbox.hpp index 152a97eacd..9320147cff 100644 --- a/apps/opencs/view/filter/filterbox.hpp +++ b/apps/opencs/view/filter/filterbox.hpp @@ -4,8 +4,10 @@ #include #include #include +#include #include +#include #include class QDragEnterEvent; @@ -39,10 +41,8 @@ namespace CSVFilter void setRecordFilter(const std::string& filter); void createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action); - - void createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action); + std::vector, std::vector>>& filterSource, + Qt::DropAction action); private: void dragEnterEvent(QDragEnterEvent* event) override; @@ -53,8 +53,8 @@ namespace CSVFilter signals: void recordFilterChanged(std::shared_ptr filter); - void recordDropped(std::vector& types, Qt::DropAction action, - const std::string& searchString, const std::string& searchColumn, bool isValue); + void recordDropped(std::vector& types, std::pair columnSearchData, + Qt::DropAction action); }; } diff --git a/apps/opencs/view/filter/recordfilterbox.cpp b/apps/opencs/view/filter/recordfilterbox.cpp index 610f11cd15..e37145931e 100644 --- a/apps/opencs/view/filter/recordfilterbox.cpp +++ b/apps/opencs/view/filter/recordfilterbox.cpp @@ -1,7 +1,10 @@ #include "recordfilterbox.hpp" +#include + #include #include +#include #include "editwidget.hpp" @@ -32,13 +35,8 @@ void CSVFilter::RecordFilterBox::setFilter(const std::string& filter) } void CSVFilter::RecordFilterBox::createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action) -{ - mEdit->createFilterRequest(filterSource, action); -} - -void CSVFilter::RecordFilterBox::createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action) + std::vector, std::vector>>& filterSource, + Qt::DropAction action) { mEdit->createFilterRequest(filterSource, action); } diff --git a/apps/opencs/view/filter/recordfilterbox.hpp b/apps/opencs/view/filter/recordfilterbox.hpp index fc759765c0..867952d2ad 100644 --- a/apps/opencs/view/filter/recordfilterbox.hpp +++ b/apps/opencs/view/filter/recordfilterbox.hpp @@ -4,8 +4,10 @@ #include #include #include +#include #include +#include #include namespace CSMFilter @@ -36,10 +38,8 @@ namespace CSVFilter void useFilterRequest(const std::string& idOfFilter); void createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action); - - void createFilterRequest( - std::vector>>& filterSource, Qt::DropAction action); + std::vector, std::vector>>& filterSource, + Qt::DropAction action); signals: diff --git a/apps/opencs/view/world/tablesubview.cpp b/apps/opencs/view/world/tablesubview.cpp index 4864cf6347..c456ca95c2 100644 --- a/apps/opencs/view/world/tablesubview.cpp +++ b/apps/opencs/view/world/tablesubview.cpp @@ -9,9 +9,11 @@ #include #include #include +#include #include #include +#include #include @@ -152,10 +154,10 @@ void CSVWorld::TableSubView::cloneRequest(const CSMWorld::UniversalId& toClone) emit cloneRequest(toClone.getId(), toClone.getType()); } -void CSVWorld::TableSubView::createFilterRequest(std::vector& types, Qt::DropAction action, - const std::string& searchString, const std::string& searchColumn, bool isValue) +void CSVWorld::TableSubView::createFilterRequest( + std::vector& types, std::pair columnSearchData, Qt::DropAction action) { - std::vector>> filterSource; + std::vector, std::vector>> filterSource; std::vector refIdColumns = mTable->getColumnsWithDisplay( CSMWorld::TableMimeData::convertEnums(CSMWorld::UniversalId::Type_Referenceable)); @@ -178,31 +180,18 @@ void CSVWorld::TableSubView::createFilterRequest(std::vectorcreateFilterRequest(filterSource, action); - else if (isValue) - { - try - { - std::vector>> valueFilterSource; - std::vector searchColumns; - searchColumns.emplace_back(searchColumn); - int searchValue = std::stoi(searchString); - Log(Debug::Warning) << "Debug: " << searchValue; - valueFilterSource.emplace_back(searchValue, searchColumns); - mFilterBox->createFilterRequest(valueFilterSource, action); - } - catch (...) - { - Log(Debug::Warning) << "Error in converting the filter request value to integer."; - } - } - else if (searchString != "") + else { + QVariant qData = columnSearchData.first; + std::string searchColumn = columnSearchData.second; + + std::vector, std::vector>> valueFilterSource; std::vector searchColumns; searchColumns.emplace_back(searchColumn); - filterSource.emplace_back(searchString, searchColumns); - mFilterBox->createFilterRequest(filterSource, action); + valueFilterSource.emplace_back(qData, searchColumns); + + mFilterBox->createFilterRequest(valueFilterSource, action); } - } bool CSVWorld::TableSubView::eventFilter(QObject* object, QEvent* event) diff --git a/apps/opencs/view/world/tablesubview.hpp b/apps/opencs/view/world/tablesubview.hpp index f8652483b5..b6beb08779 100644 --- a/apps/opencs/view/world/tablesubview.hpp +++ b/apps/opencs/view/world/tablesubview.hpp @@ -4,6 +4,7 @@ #include "../doc/subview.hpp" #include +#include #include #include @@ -61,8 +62,8 @@ namespace CSVWorld void editRequest(const CSMWorld::UniversalId& id, const std::string& hint); void cloneRequest(const CSMWorld::UniversalId& toClone); - void createFilterRequest(std::vector& types, Qt::DropAction action, - const std::string& searchString, const std::string& searchColumn, bool isValue); + void createFilterRequest(std::vector& types, + std::pair columnSearchData, Qt::DropAction action); void toggleOptions(); public slots: