Skip to content

Commit

Permalink
Remove Filter object from QML Components
Browse files Browse the repository at this point in the history
Remove the CommonData to the QML Comnponent.
Only the Audit analyses use the CommonData objects (via the Filter object), but this leads to unexpected error: the sqlite library is added in the translation unit of the QML Component library.
Furthermore, n order to use the Filter object in the QML Component the whole DataSet object was exposed to the QML Component, which is completely inconsistent with the Provider/Consumer pattern used in the VariableInfo.
So in order to still be able to use the Filter object in the Audit analyses, callback functions are used instead, which are set in the DesktopCommunicator object.
  • Loading branch information
boutinb committed Jan 13, 2025
1 parent f54810c commit 1e6a183
Show file tree
Hide file tree
Showing 21 changed files with 208 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Common/jaspColumnEncoder
Submodule jaspColumnEncoder updated 2 files
+19 −0 utils.cpp
+3 −0 utils.h
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions CommonData/column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ stringvec Column::nonFilteredLevels()
levels.insert(label->label());
}
else if(!isEmptyValue(_dbls[r]))
levels.insert(ColumnUtils::doubleToString(_dbls[r]));
levels.insert(Utils::doubleToString(_dbls[r]));
}

// Use the right label order
Expand Down Expand Up @@ -1164,7 +1164,7 @@ std::string Column::doubleToDisplayString(double dbl, bool fancyEmptyValue, bool
ignoreEmptyValue = ignoreEmptyValue && !std::isnan(dbl);

if (isEmptyValue(dbl) && !ignoreEmptyValue) return fancyEmptyValue ? EmptyValues::displayString() : "";
else return ColumnUtils::doubleToString(dbl);
else return Utils::doubleToString(dbl);
}

std::string Column::operator[](size_t row)
Expand Down Expand Up @@ -1416,7 +1416,7 @@ bool Column::replaceDoubleLabelFromRowWithDouble(size_t row, double dbl)
dblsRef = dbl;

_labelsTempDbls[row] = dbl;
_labelsTemp[row] = ColumnUtils::doubleToString(dbl);
_labelsTemp[row] = Utils::doubleToString(dbl);

return true;
}
Expand Down Expand Up @@ -1580,7 +1580,7 @@ bool Column::setValue(size_t row, const std::string & value, const std::string &

if(justAValue && !newLabel && itsADouble)
{
const std::string valueDbl = ColumnUtils::doubleToString(newDoubleToSet);
const std::string valueDbl = Utils::doubleToString(newDoubleToSet);
newLabel = labelByValue(valueDbl);
newLabel = newLabel ? newLabel : labelByValueAndDisplay(valueDbl, valueDbl);
}
Expand Down
20 changes: 0 additions & 20 deletions CommonData/columnutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,26 +210,6 @@ std::string ColumnUtils::deEuropeaniseForImport(std::string value)
return value;
}

std::string ColumnUtils::doubleToStringMaxPrec(double dbl)
{
constexpr auto max_precision{std::numeric_limits<long double>::digits10 + 1};
return doubleToString(dbl, max_precision);
}

std::string ColumnUtils::doubleToString(double dbl, int precision)
{
JASPTIMER_SCOPE(ColumnUtils::doubleToString);

if (dbl > std::numeric_limits<double>::max()) return "";
if (dbl < std::numeric_limits<double>::lowest()) return "-∞";

std::stringstream conv; //Use this instead of std::to_string to make sure there are no trailing zeroes (and to get full precision)
conv << std::setprecision(precision);
conv << dbl;
return conv.str();
}


// hex should be 4 hexadecimals characters
std::string ColumnUtils::_convertEscapedUnicodeToUTF8(std::string hex)
{
Expand Down
3 changes: 0 additions & 3 deletions CommonData/columnutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ class ColumnUtils

static void convertEscapedUnicodeToUTF8( std::string & inputStr);
static std::string deEuropeaniseForImport( std::string value); //Convert a string to a double with a dot for a separator

static std::string doubleToString( double dbl, int precision = 10);
static std::string doubleToStringMaxPrec( double dbl);

static bool convertVecToInt( const stringvec & values, intvec & intValues, intset & uniqueValues);
static bool convertVecToDouble( const stringvec & values, doublevec & doubleValues);
Expand Down
11 changes: 11 additions & 0 deletions CommonData/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "dataset.h"
#include "databaseinterface.h"

std::map<std::string, Filter*> _filterMap;

Filter::Filter(DataSet * data)
: DataSetBaseNode(dataSetBaseNodeType::filter, data), _data(data)
{ }
Expand All @@ -15,6 +17,8 @@ Filter::Filter(DataSet * data, const std::string & name, bool createIfMissing)
if(db().filterGetId(_name) > -1) dbLoad();
else if(createIfMissing) dbCreate();
else throw std::runtime_error("Filter by name '" + _name + "' but it doesnt exist and createIfMissing=false!\nAre you sure this filter should exist?");

_filterMap[name] = this;
}

void Filter::dbCreate()
Expand Down Expand Up @@ -136,6 +140,8 @@ void Filter::dbDelete()

db().filterDelete(_id);
_id = -1;

_filterMap.erase(_name);
}

void Filter::incRevision()
Expand Down Expand Up @@ -175,6 +181,11 @@ bool Filter::filterNameIsFree(const std::string &filterName)
return -1 == DatabaseInterface::singleton()->filterGetId(filterName);
}

Filter* Filter::getFilterFromName(const std::string & filterName)
{
return _filterMap[filterName];
}

void Filter::reset()
{
if(!_data->writeBatchedToDB())
Expand Down
23 changes: 12 additions & 11 deletions CommonData/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ class Filter : public DataSetBaseNode
Filter(DataSet * data);
Filter(DataSet * data, const std::string & name, bool createIfMissing = true);

DataSet * data() const { return _data; }
int id() const { return _id; }
const std::string & name() const { return _name; }
const std::string & rFilter() const { return _rFilter; }
const std::string & generatedFilter() const { return _generatedFilter; }
const std::string & constructorJson() const { return _constructorJson; }
const std::string & constructorR() const { return _constructorR; }
const std::string & errorMsg() const { return _errorMsg; }
const std::vector<bool> & filtered() const { return _filtered; }
int filteredRowCount() const { return _filteredRowCount; }
DataSet * data() const { return _data; }
int id() const { return _id; }
const std::string & name() const { return _name; }
const std::string & rFilter() const { return _rFilter; }
const std::string & generatedFilter() const { return _generatedFilter; }
const std::string & constructorJson() const { return _constructorJson; }
const std::string & constructorR() const { return _constructorR; }
const std::string & errorMsg() const { return _errorMsg; }
const std::vector<bool> & filtered() const { return _filtered; }
int filteredRowCount() const { return _filteredRowCount; }

void setRFilter( const std::string & rFilter) { _rFilter = rFilter; dbUpdate(); }
void setGeneratedFilter( const std::string & generatedFilter) { _generatedFilter = generatedFilter; dbUpdate(); }
Expand All @@ -52,10 +52,11 @@ class Filter : public DataSetBaseNode
void dbLoad();
bool dbLoadResultAndError(); ///< Loads (updated) filtervalues from database and the (possible) error msg, returns true if an error is set
void dbDelete();
void incRevision() override;
void incRevision() override;
bool checkForUpdates();

static bool filterNameIsFree(const std::string & filterName);
static Filter* getFilterFromName(const std::string & filterName);

void reset();

Expand Down
1 change: 0 additions & 1 deletion Desktop/data/columnsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ QVariant ColumnsModel::provideInfo(VariableInfo::InfoType info, const QString& c
case VariableInfo::PreviewScale: return QTransposeProxyModel::headerData(colIndex, Qt::Vertical, int(DataSetPackage::specialRoles::previewScale));
case VariableInfo::PreviewOrdinal: return QTransposeProxyModel::headerData(colIndex, Qt::Vertical, int(DataSetPackage::specialRoles::previewOrdinal));
case VariableInfo::PreviewNominal: return QTransposeProxyModel::headerData(colIndex, Qt::Vertical, int(DataSetPackage::specialRoles::previewNominal));
case VariableInfo::DataSetPointer: return QVariant::fromValue<void*>(DataSetPackage::pkg()->dataSet());
}
}
catch(std::exception & e)
Expand Down
2 changes: 1 addition & 1 deletion Desktop/data/importers/jaspimporterold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void JASPImporterOld::loadDataArchive_1_00(const std::string &path, std::functio

if (isScalar)
{
values.push_back(ColumnUtils::doubleToString(*reinterpret_cast<double*>(buff)));
values.push_back(Utils::doubleToString(*reinterpret_cast<double*>(buff)));
labels.push_back(values.back());
}
else
Expand Down
6 changes: 3 additions & 3 deletions Desktop/data/importers/readstat/readstatimportcolumn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ std::string ReadStatImportColumn::readstatValueToString(const readstat_value_t &
case READSTAT_TYPE_INT8: return std::to_string (int( readstat_int8_value(value)) );
case READSTAT_TYPE_INT16: return std::to_string (int( readstat_int16_value(value)) );
case READSTAT_TYPE_INT32: return std::to_string (int( readstat_int32_value(value)) );
case READSTAT_TYPE_FLOAT: return ColumnUtils::doubleToStringMaxPrec ( readstat_float_value(value) );
case READSTAT_TYPE_DOUBLE: return ColumnUtils::doubleToStringMaxPrec ( readstat_double_value(value) );
case READSTAT_TYPE_FLOAT: return Utils::doubleToStringMaxPrec ( readstat_float_value(value) );
case READSTAT_TYPE_DOUBLE: return Utils::doubleToStringMaxPrec ( readstat_double_value(value) );
case READSTAT_TYPE_STRING_REF: throw std::runtime_error("File contains string references and we do not support this.");
}

Expand All @@ -69,7 +69,7 @@ const stringvec &ReadStatImportColumn::labels() const
void ReadStatImportColumn::addValue(const readstat_value_t & value)
{
bool setMiss = readstat_value_is_tagged_missing(value) || (_readstatVariable && readstat_value_is_defined_missing(value, _readstatVariable));
std::string valStr = ColumnUtils::doubleToString(EmptyValues::missingValueDouble);
std::string valStr = Utils::doubleToString(EmptyValues::missingValueDouble);

if(readstat_value_is_tagged_missing(value)) //This is from sas/stata and actual value is NaN but there is a tag. So we use that as a value, this will be converted to NaN later anyway
{
Expand Down
31 changes: 31 additions & 0 deletions Desktop/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,37 @@ void MainWindow::makeConnections()
Column::setAutoSortByValuesByDefault(PreferencesModel::prefs()->orderByValueByDefault());

auto * dCSingleton = DesktopCommunicator::singleton();
// For Audit analyses, the Filter object is needed. As the QML objects should not have direct access to the data objects, the needed functions are set as callbacks.
dCSingleton->setFilterFunctions(
[](const std::string& name) {
new Filter(DataSetPackage::pkg()->dataSet(), name, true);
},
[](const std::string& name) {
Filter* filter = Filter::getFilterFromName(name);
if (filter)
filter->dbDelete();
},
[](const std::string& name, const std::string& rFilter) {
Filter* filter = Filter::getFilterFromName(name);
if (filter)
filter->setRFilter(rFilter);
},
[](const std::string& name) {
Filter* filter = Filter::getFilterFromName(name);
return filter ? filter->checkForUpdates() : false;
},
[](const std::string& name) {
Filter* filter = Filter::getFilterFromName(name);
return filter ? filter->filtered() : std::vector<bool>();
},
[](const std::string& name) {
Filter* filter = Filter::getFilterFromName(name);
return filter ? filter->filteredRowCount() : 0;
},
[](const std::string& name) {
return Filter::filterNameIsFree(name);
}
);

//Needed to allow for a hard split between Desktop/QMLComps:
connect(_preferences, &PreferencesModel::uiScaleChanged, dCSingleton, &DesktopCommunicator::uiScaleChanged );
Expand Down
2 changes: 0 additions & 2 deletions QMLComponents/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ target_include_directories(
PUBLIC # JASP
${PROJECT_SOURCE_DIR}/Common
${PROJECT_SOURCE_DIR}/Common/jaspColumnEncoder
${PROJECT_SOURCE_DIR}/CommonData
)


target_link_libraries(
QMLComponents
PUBLIC
Common
CommonData
Qt::Core
Qt::Gui
Qt::Widgets
Expand Down
2 changes: 0 additions & 2 deletions QMLComponents/controls/textinputbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#include "textinputbase.h"
#include "analysisform.h"
#include "utils.h"
#include "columnutils.h"

using namespace std;

Expand Down
Loading

0 comments on commit 1e6a183

Please sign in to comment.