Use struct with sensible member names

7220-lua-add-a-general-purpose-lexical-parser
unelsson 2 years ago
parent 9b808a495c
commit 2fac4d0e40

@ -11,6 +11,8 @@
#include <QString> #include <QString>
#include <QVariant> #include <QVariant>
#include "filterdata.hpp"
#include <apps/opencs/model/filter/parser.hpp> #include <apps/opencs/model/filter/parser.hpp>
#include <apps/opencs/model/world/universalid.hpp> #include <apps/opencs/model/world/universalid.hpp>
@ -96,19 +98,21 @@ void CSVFilter::EditWidget::filterRowsInserted(const QModelIndex& parent, int st
textChanged(text()); textChanged(text());
} }
void CSVFilter::EditWidget::createFilterRequest( void CSVFilter::EditWidget::createFilterRequest(const std::vector<FilterData>& sourceFilter, Qt::DropAction action)
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action)
{ {
std::string stringOrValue = "string"; std::string stringOrValue = "string";
std::vector<std::pair<std::string, std::vector<std::string>>> newFilter; std::vector<FilterData> newFilter;
for (auto pair : filterSource) for (auto filterData : sourceFilter)
{ {
std::string searchString = std::visit(FilterVisitor(), pair.first).first; FilterData newFilterData;
stringOrValue = std::visit(FilterVisitor(), pair.first).second; std::pair<std::string, std::string> pair = std::visit(FilterVisitor(), filterData.searchData);
std::vector<std::string> column = pair.second; std::string searchString = pair.first;
newFilter.emplace_back(std::make_pair(searchString, column)); stringOrValue = pair.second;
std::vector<std::string> columns;
newFilterData.searchData = searchString;
newFilterData.columns = filterData.columns;
newFilter.emplace_back(newFilterData);
} }
const unsigned count = newFilter.size(); const unsigned count = newFilter.size();
@ -215,10 +219,9 @@ void CSVFilter::EditWidget::createFilterRequest(
} }
} }
std::string CSVFilter::EditWidget::generateFilter( std::string CSVFilter::EditWidget::generateFilter(const FilterData& filterData, std::string stringOrValue) const
std::pair<std::string, std::vector<std::string>>& seekedString, std::string stringOrValue) const
{ {
const unsigned columns = seekedString.second.size(); const unsigned columns = filterData.columns.size();
bool multipleColumns = false; bool multipleColumns = false;
switch (columns) switch (columns)
@ -235,9 +238,11 @@ std::string CSVFilter::EditWidget::generateFilter(
break; break;
} }
std::string quotesResolved = seekedString.first; std::string quotesResolved;
if (std::get_if<std::string>(&filterData.searchData))
quotesResolved = *(std::get_if<std::string>(&filterData.searchData));
if (stringOrValue == "string") if (stringOrValue == "string")
quotesResolved = '"' + seekedString.first + '"'; quotesResolved = '"' + std::get<std::string>(filterData.searchData) + '"';
std::stringstream ss; std::stringstream ss;
if (multipleColumns) if (multipleColumns)
@ -245,7 +250,7 @@ std::string CSVFilter::EditWidget::generateFilter(
ss << "or("; ss << "or(";
for (unsigned i = 0; i < columns; ++i) for (unsigned i = 0; i < columns; ++i)
{ {
ss << stringOrValue << "(" << '"' << seekedString.second[i] << '"' << ',' << quotesResolved << ')'; ss << stringOrValue << "(" << '"' << filterData.columns[i] << '"' << ',' << quotesResolved << ')';
if (i + 1 != columns) if (i + 1 != columns)
ss << ','; ss << ',';
} }
@ -253,7 +258,7 @@ std::string CSVFilter::EditWidget::generateFilter(
} }
else else
{ {
ss << stringOrValue << '(' << '"' << seekedString.second[0] << "\"," << quotesResolved << ")"; ss << stringOrValue << '(' << '"' << filterData.columns[0] << "\"," << quotesResolved << ")";
} }
return ss.str(); return ss.str();

@ -11,6 +11,8 @@
#include <variant> #include <variant>
#include <vector> #include <vector>
#include "filterdata.hpp"
#include "../../model/filter/parser.hpp" #include "../../model/filter/parser.hpp"
class QModelIndex; class QModelIndex;
@ -65,17 +67,14 @@ namespace CSVFilter
public: public:
EditWidget(CSMWorld::Data& data, QWidget* parent = nullptr); EditWidget(CSMWorld::Data& data, QWidget* parent = nullptr);
void createFilterRequest( void createFilterRequest(const std::vector<FilterData>& sourceFilter, Qt::DropAction action);
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action);
signals: signals:
void filterChanged(std::shared_ptr<CSMFilter::Node> filter); void filterChanged(std::shared_ptr<CSMFilter::Node> filter);
private: private:
std::string generateFilter( std::string generateFilter(const FilterData& filterData, std::string stringOrValue) const;
std::pair<std::string, std::vector<std::string>>& seekedString, std::string stringOrValue) const;
void contextMenuEvent(QContextMenuEvent* event) override; void contextMenuEvent(QContextMenuEvent* event) override;

@ -9,6 +9,7 @@
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QVariant> #include <QVariant>
#include "filterdata.hpp"
#include "recordfilterbox.hpp" #include "recordfilterbox.hpp"
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
@ -71,9 +72,7 @@ void CSVFilter::FilterBox::dragMoveEvent(QDragMoveEvent* event)
event->accept(); event->accept();
} }
void CSVFilter::FilterBox::createFilterRequest( void CSVFilter::FilterBox::createFilterRequest(const std::vector<FilterData>& sourceFilter, Qt::DropAction action)
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action)
{ {
mRecordFilterBox->createFilterRequest(filterSource, action); mRecordFilterBox->createFilterRequest(sourceFilter, action);
} }

@ -10,6 +10,8 @@
#include <QVariant> #include <QVariant>
#include <QWidget> #include <QWidget>
#include "filterdata.hpp"
class QDragEnterEvent; class QDragEnterEvent;
class QDragMoveEvent; class QDragMoveEvent;
class QDropEvent; class QDropEvent;
@ -40,9 +42,7 @@ namespace CSVFilter
void setRecordFilter(const std::string& filter); void setRecordFilter(const std::string& filter);
void createFilterRequest( void createFilterRequest(const std::vector<FilterData>& sourceFilter, Qt::DropAction action);
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action);
private: private:
void dragEnterEvent(QDragEnterEvent* event) override; void dragEnterEvent(QDragEnterEvent* event) override;

@ -0,0 +1,21 @@
#ifndef FILTERDATA_HPP
#define FILTERDATA_HPP
#include <string>
#include <variant>
#include <vector>
#include <QVariant>
namespace CSVFilter
{
struct FilterData
{
std::variant<std::string, QVariant> searchData;
std::vector<std::string> columns;
};
}
#endif // FILTERDATA_HPP

@ -7,6 +7,7 @@
#include <QVariant> #include <QVariant>
#include "editwidget.hpp" #include "editwidget.hpp"
#include "filterdata.hpp"
CSVFilter::RecordFilterBox::RecordFilterBox(CSMWorld::Data& data, QWidget* parent) CSVFilter::RecordFilterBox::RecordFilterBox(CSMWorld::Data& data, QWidget* parent)
: QWidget(parent) : QWidget(parent)
@ -34,9 +35,7 @@ void CSVFilter::RecordFilterBox::setFilter(const std::string& filter)
mEdit->setText(QString::fromUtf8(filter.c_str())); mEdit->setText(QString::fromUtf8(filter.c_str()));
} }
void CSVFilter::RecordFilterBox::createFilterRequest( void CSVFilter::RecordFilterBox::createFilterRequest(const std::vector<FilterData>& sourceFilter, Qt::DropAction action)
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action)
{ {
mEdit->createFilterRequest(filterSource, action); mEdit->createFilterRequest(sourceFilter, action);
} }

@ -10,6 +10,8 @@
#include <QVariant> #include <QVariant>
#include <QWidget> #include <QWidget>
#include "filterdata.hpp"
namespace CSMFilter namespace CSMFilter
{ {
class Node; class Node;
@ -37,9 +39,7 @@ namespace CSVFilter
void useFilterRequest(const std::string& idOfFilter); void useFilterRequest(const std::string& idOfFilter);
void createFilterRequest( void createFilterRequest(const std::vector<FilterData>& sourceFilter, Qt::DropAction action);
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action);
signals: signals:

@ -24,6 +24,7 @@
#include "../doc/sizehint.hpp" #include "../doc/sizehint.hpp"
#include "../filter/filterbox.hpp" #include "../filter/filterbox.hpp"
#include "../filter/filterdata.hpp"
#include "table.hpp" #include "table.hpp"
#include "tablebottombox.hpp" #include "tablebottombox.hpp"
@ -157,8 +158,7 @@ void CSVWorld::TableSubView::cloneRequest(const CSMWorld::UniversalId& toClone)
void CSVWorld::TableSubView::createFilterRequest( void CSVWorld::TableSubView::createFilterRequest(
std::vector<CSMWorld::UniversalId>& types, std::pair<QVariant, std::string> columnSearchData, Qt::DropAction action) std::vector<CSMWorld::UniversalId>& types, std::pair<QVariant, std::string> columnSearchData, Qt::DropAction action)
{ {
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>> filterSource; std::vector<CSVFilter::FilterData> sourceFilter;
std::vector<std::string> refIdColumns = mTable->getColumnsWithDisplay( std::vector<std::string> refIdColumns = mTable->getColumnsWithDisplay(
CSMWorld::TableMimeData::convertEnums(CSMWorld::UniversalId::Type_Referenceable)); CSMWorld::TableMimeData::convertEnums(CSMWorld::UniversalId::Type_Referenceable));
bool hasRefIdDisplay = !refIdColumns.empty(); bool hasRefIdDisplay = !refIdColumns.empty();
@ -169,28 +169,39 @@ void CSVWorld::TableSubView::createFilterRequest(
std::vector<std::string> col = mTable->getColumnsWithDisplay(CSMWorld::TableMimeData::convertEnums(type)); std::vector<std::string> col = mTable->getColumnsWithDisplay(CSMWorld::TableMimeData::convertEnums(type));
if (!col.empty()) 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)) 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()) if (!sourceFilter.empty())
mFilterBox->createFilterRequest(filterSource, action); mFilterBox->createFilterRequest(sourceFilter, action);
else else
{ {
std::vector<CSVFilter::FilterData> sourceFilterByValue;
QVariant qData = columnSearchData.first; QVariant qData = columnSearchData.first;
std::string searchColumn = columnSearchData.second; std::string searchColumn = columnSearchData.second;
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>> valueFilterSource;
std::vector<std::string> searchColumns; std::vector<std::string> searchColumns;
searchColumns.emplace_back(searchColumn); 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);
} }
} }

Loading…
Cancel
Save