added numeric value filter node
parent
537ab38985
commit
3cf60da5a7
@ -0,0 +1,71 @@
|
||||
|
||||
#include "valuenode.hpp"
|
||||
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "../world/columns.hpp"
|
||||
#include "../world/idtable.hpp"
|
||||
|
||||
CSMFilter::ValueNode::ValueNode (int columnId,
|
||||
double lower, double upper, bool min, bool max)
|
||||
: mColumnId (columnId), mLower (lower), mUpper (upper), mMin (min), mMax (max)
|
||||
{}
|
||||
|
||||
bool CSMFilter::ValueNode::test (const CSMWorld::IdTable& table, int row,
|
||||
const std::map<int, int>& columns) const
|
||||
{
|
||||
const std::map<int, int>::const_iterator iter = columns.find (mColumnId);
|
||||
|
||||
if (iter==columns.end())
|
||||
throw std::logic_error ("invalid column in test value test");
|
||||
|
||||
if (iter->second==-1)
|
||||
return true;
|
||||
|
||||
QModelIndex index = table.index (row, iter->second);
|
||||
|
||||
QVariant data = table.data (index);
|
||||
|
||||
if (data.type()!=QVariant::Double && data.type()!=QVariant::Bool && data.type()!=QVariant::Int &&
|
||||
data.type()!=QVariant::UInt)
|
||||
return false;
|
||||
|
||||
double value = data.toDouble();
|
||||
|
||||
if (mLower==mUpper && mMin && mMax)
|
||||
return value==mLower;
|
||||
|
||||
return (mMin ? value>=mLower : value>mLower) && (mMax ? value<=mUpper : value<mUpper);
|
||||
}
|
||||
|
||||
std::vector<int> CSMFilter::ValueNode::getReferencedColumns() const
|
||||
{
|
||||
return std::vector<int> (1, mColumnId);
|
||||
}
|
||||
|
||||
std::string CSMFilter::ValueNode::toString (bool numericColumns) const
|
||||
{
|
||||
std::ostringstream stream;
|
||||
|
||||
stream << "value (";
|
||||
|
||||
if (numericColumns)
|
||||
stream << mColumnId;
|
||||
else
|
||||
stream
|
||||
<< "\""
|
||||
<< CSMWorld::Columns::getName (static_cast<CSMWorld::Columns::ColumnId> (mColumnId))
|
||||
<< "\"";
|
||||
|
||||
stream << ", \"";
|
||||
|
||||
if (mLower==mUpper && mMin && mMax)
|
||||
stream << mLower;
|
||||
else
|
||||
stream << (mMin ? "[" : "(") << mLower << ", " << mUpper << (mMax ? "]" : ")");
|
||||
|
||||
stream << ")";
|
||||
|
||||
return stream.str();
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
#ifndef CSM_FILTER_VALUENODE_H
|
||||
#define CSM_FILTER_VALUENODE_H
|
||||
|
||||
#include "leafnode.hpp"
|
||||
|
||||
namespace CSMFilter
|
||||
{
|
||||
class ValueNode : public LeafNode
|
||||
{
|
||||
int mColumnId;
|
||||
std::string mText;
|
||||
double mLower;
|
||||
double mUpper;
|
||||
bool mMin;
|
||||
bool mMax;
|
||||
|
||||
public:
|
||||
|
||||
ValueNode (int columnId, double lower, double upper, bool min, bool max);
|
||||
|
||||
virtual bool test (const CSMWorld::IdTable& table, int row,
|
||||
const std::map<int, int>& columns) const;
|
||||
///< \return Can the specified table row pass through to filter?
|
||||
/// \param columns column ID to column index mapping
|
||||
|
||||
virtual std::vector<int> getReferencedColumns() const;
|
||||
///< Return a list of the IDs of the columns referenced by this node. The column mapping
|
||||
/// passed into test as columns must contain all columns listed here.
|
||||
|
||||
virtual std::string toString (bool numericColumns) const;
|
||||
///< Return a string that represents this node.
|
||||
///
|
||||
/// \param numericColumns Use numeric IDs instead of string to represent columns.
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue