diff --git a/apps/opencs/view/filter/editwidget.cpp b/apps/opencs/view/filter/editwidget.cpp index 46c996cee4..b447a93255 100644 --- a/apps/opencs/view/filter/editwidget.cpp +++ b/apps/opencs/view/filter/editwidget.cpp @@ -100,15 +100,15 @@ void CSVFilter::EditWidget::filterRowsInserted(const QModelIndex& parent, int st void CSVFilter::EditWidget::createFilterRequest(const std::vector& sourceFilter, Qt::DropAction action) { - std::string stringOrValue = "string"; + FilterType filterType = FilterType::String; std::vector newFilter; for (auto filterData : sourceFilter) { FilterData newFilterData; - std::pair pair = std::visit(FilterVisitor(), filterData.searchData); + std::pair pair = std::visit(FilterVisitor(), filterData.searchData); std::string searchString = pair.first; - stringOrValue = pair.second; + filterType = pair.second; std::vector columns; newFilterData.searchData = searchString; newFilterData.columns = filterData.columns; @@ -183,7 +183,7 @@ void CSVFilter::EditWidget::createFilterRequest(const std::vector& s for (unsigned i = 0; i < count; ++i) { - ss << generateFilter(newFilter[i], stringOrValue); + ss << generateFilter(newFilter[i], filterType); if (i + 1 != count) { @@ -204,7 +204,7 @@ void CSVFilter::EditWidget::createFilterRequest(const std::vector& s ss << '!'; } - ss << generateFilter(newFilter[0], stringOrValue); + ss << generateFilter(newFilter[0], filterType); if (!replaceMode) { @@ -219,7 +219,7 @@ void CSVFilter::EditWidget::createFilterRequest(const std::vector& s } } -std::string CSVFilter::EditWidget::generateFilter(const FilterData& filterData, std::string stringOrValue) const +std::string CSVFilter::EditWidget::generateFilter(const FilterData& filterData, const FilterType& filterType) const { const unsigned columns = filterData.columns.size(); @@ -246,16 +246,18 @@ std::string CSVFilter::EditWidget::generateFilter(const FilterData& filterData, Log(Debug::Warning) << "Generating record filter failed."; return ""; } - if (stringOrValue == "string") + if (filterType == FilterType::String) quotesResolved = '"' + quotesResolved + '"'; std::stringstream ss; + if (multipleColumns) { ss << "or("; for (unsigned i = 0; i < columns; ++i) { - ss << stringOrValue << "(" << '"' << filterData.columns[i] << '"' << ',' << quotesResolved << ')'; + ss << filterTypeName(filterType) << "(" << '"' << filterData.columns[i] << '"' << ',' << quotesResolved + << ')'; if (i + 1 != columns) ss << ','; } @@ -263,7 +265,7 @@ std::string CSVFilter::EditWidget::generateFilter(const FilterData& filterData, } else { - ss << stringOrValue << '(' << '"' << filterData.columns[0] << "\"," << quotesResolved << ")"; + ss << filterTypeName(filterType) << '(' << '"' << filterData.columns[0] << "\"," << quotesResolved << ")"; } return ss.str(); diff --git a/apps/opencs/view/filter/editwidget.hpp b/apps/opencs/view/filter/editwidget.hpp index 981e570f6b..7ae166283a 100644 --- a/apps/opencs/view/filter/editwidget.hpp +++ b/apps/opencs/view/filter/editwidget.hpp @@ -33,23 +33,29 @@ namespace CSMWorld namespace CSVFilter { + enum class FilterType + { + String, + Value + }; + struct FilterVisitor { - std::pair operator()(const std::string& stringData) + std::pair operator()(const std::string& stringData) { - std::string stringOrValue = "string"; - return std::make_pair(stringData, stringOrValue); + FilterType filterType = FilterType::String; + return std::make_pair(stringData, filterType); } - std::pair operator()(const QVariant& variantData) + std::pair operator()(const QVariant& variantData) { - std::string stringOrValue; + FilterType filterType = FilterType::String; QMetaType::Type dataType = static_cast(variantData.type()); if (dataType == QMetaType::QString || dataType == QMetaType::Bool || dataType == QMetaType::Int) - stringOrValue = "string"; + filterType = FilterType::String; if (dataType == QMetaType::Int || dataType == QMetaType::Float) - stringOrValue = "value"; - return std::make_pair(variantData.toString().toStdString(), stringOrValue); + filterType = FilterType::Value; + return std::make_pair(variantData.toString().toStdString(), filterType); } }; @@ -74,10 +80,22 @@ namespace CSVFilter void filterChanged(std::shared_ptr filter); private: - std::string generateFilter(const FilterData& filterData, std::string stringOrValue) const; + std::string generateFilter(const FilterData& filterData, const FilterType& filterType) const; void contextMenuEvent(QContextMenuEvent* event) override; + constexpr std::string_view filterTypeName(const FilterType& type) const + { + switch (type) + { + case FilterType::String: + return "string"; + case FilterType::Value: + return "value"; + } + return "unknown type"; + } + private slots: void textChanged(const QString& text);