Implement enum class FilterType

7220-lua-add-a-general-purpose-lexical-parser
unelsson 2 years ago
parent 2e7d41373b
commit 6b0998ff85

@ -100,15 +100,15 @@ void CSVFilter::EditWidget::filterRowsInserted(const QModelIndex& parent, int st
void CSVFilter::EditWidget::createFilterRequest(const std::vector<FilterData>& sourceFilter, Qt::DropAction action) void CSVFilter::EditWidget::createFilterRequest(const std::vector<FilterData>& sourceFilter, Qt::DropAction action)
{ {
std::string stringOrValue = "string"; FilterType filterType = FilterType::String;
std::vector<FilterData> newFilter; std::vector<FilterData> newFilter;
for (auto filterData : sourceFilter) for (auto filterData : sourceFilter)
{ {
FilterData newFilterData; FilterData newFilterData;
std::pair<std::string, std::string> pair = std::visit(FilterVisitor(), filterData.searchData); std::pair<std::string, FilterType> pair = std::visit(FilterVisitor(), filterData.searchData);
std::string searchString = pair.first; std::string searchString = pair.first;
stringOrValue = pair.second; filterType = pair.second;
std::vector<std::string> columns; std::vector<std::string> columns;
newFilterData.searchData = searchString; newFilterData.searchData = searchString;
newFilterData.columns = filterData.columns; newFilterData.columns = filterData.columns;
@ -183,7 +183,7 @@ void CSVFilter::EditWidget::createFilterRequest(const std::vector<FilterData>& s
for (unsigned i = 0; i < count; ++i) for (unsigned i = 0; i < count; ++i)
{ {
ss << generateFilter(newFilter[i], stringOrValue); ss << generateFilter(newFilter[i], filterType);
if (i + 1 != count) if (i + 1 != count)
{ {
@ -204,7 +204,7 @@ void CSVFilter::EditWidget::createFilterRequest(const std::vector<FilterData>& s
ss << '!'; ss << '!';
} }
ss << generateFilter(newFilter[0], stringOrValue); ss << generateFilter(newFilter[0], filterType);
if (!replaceMode) if (!replaceMode)
{ {
@ -219,7 +219,7 @@ void CSVFilter::EditWidget::createFilterRequest(const std::vector<FilterData>& 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(); 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."; Log(Debug::Warning) << "Generating record filter failed.";
return ""; return "";
} }
if (stringOrValue == "string") if (filterType == FilterType::String)
quotesResolved = '"' + quotesResolved + '"'; quotesResolved = '"' + quotesResolved + '"';
std::stringstream ss; std::stringstream ss;
if (multipleColumns) if (multipleColumns)
{ {
ss << "or("; ss << "or(";
for (unsigned i = 0; i < columns; ++i) for (unsigned i = 0; i < columns; ++i)
{ {
ss << stringOrValue << "(" << '"' << filterData.columns[i] << '"' << ',' << quotesResolved << ')'; ss << filterTypeName(filterType) << "(" << '"' << filterData.columns[i] << '"' << ',' << quotesResolved
<< ')';
if (i + 1 != columns) if (i + 1 != columns)
ss << ','; ss << ',';
} }
@ -263,7 +265,7 @@ std::string CSVFilter::EditWidget::generateFilter(const FilterData& filterData,
} }
else else
{ {
ss << stringOrValue << '(' << '"' << filterData.columns[0] << "\"," << quotesResolved << ")"; ss << filterTypeName(filterType) << '(' << '"' << filterData.columns[0] << "\"," << quotesResolved << ")";
} }
return ss.str(); return ss.str();

@ -33,23 +33,29 @@ namespace CSMWorld
namespace CSVFilter namespace CSVFilter
{ {
enum class FilterType
{
String,
Value
};
struct FilterVisitor struct FilterVisitor
{ {
std::pair<std::string, std::string> operator()(const std::string& stringData) std::pair<std::string, FilterType> operator()(const std::string& stringData)
{ {
std::string stringOrValue = "string"; FilterType filterType = FilterType::String;
return std::make_pair(stringData, stringOrValue); return std::make_pair(stringData, filterType);
} }
std::pair<std::string, std::string> operator()(const QVariant& variantData) std::pair<std::string, FilterType> operator()(const QVariant& variantData)
{ {
std::string stringOrValue; FilterType filterType = FilterType::String;
QMetaType::Type dataType = static_cast<QMetaType::Type>(variantData.type()); QMetaType::Type dataType = static_cast<QMetaType::Type>(variantData.type());
if (dataType == QMetaType::QString || dataType == QMetaType::Bool || dataType == QMetaType::Int) if (dataType == QMetaType::QString || dataType == QMetaType::Bool || dataType == QMetaType::Int)
stringOrValue = "string"; filterType = FilterType::String;
if (dataType == QMetaType::Int || dataType == QMetaType::Float) if (dataType == QMetaType::Int || dataType == QMetaType::Float)
stringOrValue = "value"; filterType = FilterType::Value;
return std::make_pair(variantData.toString().toStdString(), stringOrValue); return std::make_pair(variantData.toString().toStdString(), filterType);
} }
}; };
@ -74,10 +80,22 @@ namespace CSVFilter
void filterChanged(std::shared_ptr<CSMFilter::Node> filter); void filterChanged(std::shared_ptr<CSMFilter::Node> filter);
private: 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; 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: private slots:
void textChanged(const QString& text); void textChanged(const QString& text);

Loading…
Cancel
Save