Use std::variant and std::visit instead of function overloads, add float type search, fix QMetaType and Type conversion

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

@ -1,6 +1,7 @@
#include "editwidget.hpp"
#include <sstream>
#include <variant>
#include <QAction>
#include <QApplication>
@ -8,6 +9,7 @@
#include <QMenu>
#include <QRegularExpression>
#include <QString>
#include <QVariant>
#include <apps/opencs/model/filter/parser.hpp>
#include <apps/opencs/model/world/universalid.hpp>
@ -95,9 +97,21 @@ void CSVFilter::EditWidget::filterRowsInserted(const QModelIndex& parent, int st
}
void CSVFilter::EditWidget::createFilterRequest(
std::vector<std::pair<std::string, std::vector<std::string>>>& filterSource, Qt::DropAction action, std::string stringOrValue)
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action)
{
const unsigned count = filterSource.size();
std::string stringOrValue = "string";
std::vector<std::pair<std::string, std::vector<std::string>>> newFilter;
for (auto pair : filterSource)
{
std::string searchString = std::visit(FilterVisitor(), pair.first).first;
stringOrValue = std::visit(FilterVisitor(), pair.first).second;
std::vector<std::string> 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<std::pair<int, std::vector<std::string>>>& filterSource, Qt::DropAction action)
{
std::vector<std::pair<std::string, std::vector<std::string>>> convertedFilter;
std::string stringOrValue = "value";
for (auto pair : filterSource)
{
std::string a = std::to_string(pair.first);
std::vector<std::string> b = pair.second;
convertedFilter.emplace_back(std::make_pair (a, b));
}
createFilterRequest(convertedFilter, action, stringOrValue);
}
std::string CSVFilter::EditWidget::generateFilter(std::pair<std::string, std::vector<std::string>>& seekedString, std::string stringOrValue) const
std::string CSVFilter::EditWidget::generateFilter(
std::pair<std::string, std::vector<std::string>>& seekedString, std::string stringOrValue) const
{
const unsigned columns = seekedString.second.size();
@ -233,9 +234,10 @@ std::string CSVFilter::EditWidget::generateFilter(std::pair<std::string, std::ve
multipleColumns = true;
break;
}
std::string quotesResolved = seekedString.first;
if (stringOrValue == "string") quotesResolved = '"' + seekedString.first + '"';
if (stringOrValue == "string")
quotesResolved = '"' + seekedString.first + '"';
std::stringstream ss;
if (multipleColumns)

@ -3,10 +3,12 @@
#include <QLineEdit>
#include <QPalette>
#include <QVariant>
#include <memory>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "../../model/filter/parser.hpp"
@ -29,6 +31,26 @@ namespace CSMWorld
namespace CSVFilter
{
struct FilterVisitor
{
std::pair<std::string, std::string> operator()(const std::string& stringData)
{
std::string stringOrValue = "string";
return std::make_pair(stringData, stringOrValue);
}
std::pair<std::string, std::string> operator()(const QVariant& variantData)
{
std::string stringOrValue;
QMetaType::Type dataType = static_cast<QMetaType::Type>(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<std::pair<std::string, std::vector<std::string>>>& filterSource, Qt::DropAction action, std::string stringOrValue = "string");
void createFilterRequest(
std::vector<std::pair<int, std::vector<std::string>>>& filterSource, Qt::DropAction action);
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action);
signals:
void filterChanged(std::shared_ptr<CSMFilter::Node> filter);
private:
std::string generateFilter(std::pair<std::string, std::vector<std::string>>& seekedString, std::string stringOrValue) const;
std::string generateFilter(
std::pair<std::string, std::vector<std::string>>& seekedString, std::string stringOrValue) const;
void contextMenuEvent(QContextMenuEvent* event) override;

@ -2,10 +2,12 @@
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include <QDragEnterEvent>
#include <QHBoxLayout>
#include <QVariant>
#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<std::pair<std::string, std::vector<std::string>>>& filterSource, Qt::DropAction action)
{
mRecordFilterBox->createFilterRequest(filterSource, action);
}
void CSVFilter::FilterBox::createFilterRequest(
std::vector<std::pair<int, std::vector<std::string>>>& filterSource, Qt::DropAction action)
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action)
{
mRecordFilterBox->createFilterRequest(filterSource, action);
}

@ -4,8 +4,10 @@
#include <memory>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include <QVariant>
#include <QWidget>
class QDragEnterEvent;
@ -39,10 +41,8 @@ namespace CSVFilter
void setRecordFilter(const std::string& filter);
void createFilterRequest(
std::vector<std::pair<std::string, std::vector<std::string>>>& filterSource, Qt::DropAction action);
void createFilterRequest(
std::vector<std::pair<int, std::vector<std::string>>>& filterSource, Qt::DropAction action);
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action);
private:
void dragEnterEvent(QDragEnterEvent* event) override;
@ -53,8 +53,8 @@ namespace CSVFilter
signals:
void recordFilterChanged(std::shared_ptr<CSMFilter::Node> filter);
void recordDropped(std::vector<CSMWorld::UniversalId>& types, Qt::DropAction action,
const std::string& searchString, const std::string& searchColumn, bool isValue);
void recordDropped(std::vector<CSMWorld::UniversalId>& types, std::pair<QVariant, std::string> columnSearchData,
Qt::DropAction action);
};
}

@ -1,7 +1,10 @@
#include "recordfilterbox.hpp"
#include <variant>
#include <QHBoxLayout>
#include <QLabel>
#include <QVariant>
#include "editwidget.hpp"
@ -32,13 +35,8 @@ void CSVFilter::RecordFilterBox::setFilter(const std::string& filter)
}
void CSVFilter::RecordFilterBox::createFilterRequest(
std::vector<std::pair<std::string, std::vector<std::string>>>& filterSource, Qt::DropAction action)
{
mEdit->createFilterRequest(filterSource, action);
}
void CSVFilter::RecordFilterBox::createFilterRequest(
std::vector<std::pair<int, std::vector<std::string>>>& filterSource, Qt::DropAction action)
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action)
{
mEdit->createFilterRequest(filterSource, action);
}

@ -4,8 +4,10 @@
#include <memory>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include <QVariant>
#include <QWidget>
namespace CSMFilter
@ -36,10 +38,8 @@ namespace CSVFilter
void useFilterRequest(const std::string& idOfFilter);
void createFilterRequest(
std::vector<std::pair<std::string, std::vector<std::string>>>& filterSource, Qt::DropAction action);
void createFilterRequest(
std::vector<std::pair<int, std::vector<std::string>>>& filterSource, Qt::DropAction action);
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>>& filterSource,
Qt::DropAction action);
signals:

@ -9,9 +9,11 @@
#include <QPushButton>
#include <QScreen>
#include <QVBoxLayout>
#include <QVariant>
#include <algorithm>
#include <utility>
#include <variant>
#include <apps/opencs/view/doc/subview.hpp>
@ -152,10 +154,10 @@ void CSVWorld::TableSubView::cloneRequest(const CSMWorld::UniversalId& toClone)
emit cloneRequest(toClone.getId(), toClone.getType());
}
void CSVWorld::TableSubView::createFilterRequest(std::vector<CSMWorld::UniversalId>& types, Qt::DropAction action,
const std::string& searchString, const std::string& searchColumn, bool isValue)
void CSVWorld::TableSubView::createFilterRequest(
std::vector<CSMWorld::UniversalId>& types, std::pair<QVariant, std::string> columnSearchData, Qt::DropAction action)
{
std::vector<std::pair<std::string, std::vector<std::string>>> filterSource;
std::vector<std::pair<std::variant<std::string, QVariant>, std::vector<std::string>>> filterSource;
std::vector<std::string> refIdColumns = mTable->getColumnsWithDisplay(
CSMWorld::TableMimeData::convertEnums(CSMWorld::UniversalId::Type_Referenceable));
@ -178,31 +180,18 @@ void CSVWorld::TableSubView::createFilterRequest(std::vector<CSMWorld::Universal
if (!filterSource.empty())
mFilterBox->createFilterRequest(filterSource, action);
else if (isValue)
{
try
{
std::vector<std::pair<int, std::vector<std::string>>> valueFilterSource;
std::vector<std::string> 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::pair<std::variant<std::string, QVariant>, std::vector<std::string>>> valueFilterSource;
std::vector<std::string> 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)

@ -4,6 +4,7 @@
#include "../doc/subview.hpp"
#include <QModelIndex>
#include <QVariant>
#include <QWidget>
#include <string>
@ -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<CSMWorld::UniversalId>& types, Qt::DropAction action,
const std::string& searchString, const std::string& searchColumn, bool isValue);
void createFilterRequest(std::vector<CSMWorld::UniversalId>& types,
std::pair<QVariant, std::string> columnSearchData, Qt::DropAction action);
void toggleOptions();
public slots:

Loading…
Cancel
Save