Skip to content

Commit

Permalink
use set functions for deserializen etc
Browse files Browse the repository at this point in the history
  • Loading branch information
JorisGoosen committed Jan 22, 2025
1 parent c34d9ce commit fb42ce1
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 178 deletions.
48 changes: 26 additions & 22 deletions CommonData/column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ void Column::setType(columnType colType)
_type = colType;
db().columnSetType(_id, _type);
incRevision();

_emitTypeChanged();
}

bool Column::hasCustomEmptyValues() const
Expand Down Expand Up @@ -688,6 +690,8 @@ void Column::_sortLabelsByOrder()
void Column::labelsClear(bool doIncRevision)
{
db().labelsClear(_id);
for(Label * l : _labels)
delete l;
_labels.clear();
_labelByIntsIdMap.clear();
_labelByValDis.clear();
Expand Down Expand Up @@ -1199,7 +1203,10 @@ std::string Column::_getLabelDisplayStringByValue(int key, bool ignoreEmptyValue
std::string Column::getValue(size_t row, bool fancyEmptyValue, bool ignoreEmptyValue, columnType asType) const
{
if(asType == columnType::unknown)
{
asType = _type;
_emitTypeChanged();
}

if (row < rowCount())
{
Expand Down Expand Up @@ -2223,6 +2230,8 @@ void Column::deserializeLabelsForCopy(const Json::Value & labels)
beginBatchedLabelsDB();
_labelByIntsIdMap.clear();
_labelByValDis.clear();
for(Label * l : _labels)
delete l;
_labels.clear();

if (labels.isArray())
Expand All @@ -2244,6 +2253,7 @@ void Column::deserializeLabelsForRevert(const Json::Value & labels)
{
labelsTempReset();

_beginResetModel();
beginBatchedLabelsDB();

//intset updatedLbls;
Expand Down Expand Up @@ -2287,7 +2297,8 @@ void Column::deserializeLabelsForRevert(const Json::Value & labels)
missingLbls.insert(idLabel.first);*/

endBatchedLabelsDB();

_endResetModel();

/* The following is already implied by endBatchedLabelsDB because it deletes all labels first anyway
for(int id : missingLbls)
{
Expand All @@ -2313,33 +2324,25 @@ void Column::deserialize(const Json::Value &json)
std::string name = json["name"].asString(),
title = json["title"].asString();

_name = getUniqueName(name);
db().columnSetName(_id, _name);
setName(getUniqueName(name));

// If title was equal to name, then they should still stay the same if the name is changed to be unique.
_title = name == title ? _name : title;
db().columnSetTitle(_id, _title);

_description = json["description"].asString();
db().columnSetDescription(_id, _description);

_type = columnType(json["type"].asInt());
db().columnSetType(_id, _type);

_invalidated = json["invalidated"].asBool();
_codeType = computedColumnType(json["codeType"].asInt());
_rCode = json["rCode"].asString();
_error = json["error"].asString();
_analysisId = json["analysisId"].asInt();
_constructorJson = json["constructorJson"];
_autoSortByValue = json["autoSortByValue"].asBool();
setTitle( name == title ? _name : title);
setDescription( json["description"] .asString() );
setType( columnType( json["type"] .asInt()) );
setInvalidated( json["invalidated"] .asBool() );
setCodeType( computedColumnType( json["codeType"] .asInt()) );
setRCode( json["rCode"] .asString() );
setError( json["error"] .asString() );
setAnalysisId( json["analysisId"] .asInt() );
setConstructorJson( json["constructorJson"] );
setAutoSortByValue( json["autoSortByValue"] .asBool() );

db().columnSetComputedInfo(_id, _analysisId, _invalidated, _codeType, _rCode, _error, constructorJsonStr());

deserializeLabelsForCopy(json["labels"]);

_emptyValues->fromJson(json["customEmptyValues"]);


_beginResetModel();
size_t i=0;
_dbls.resize(json["dbls"].size());
for (const Json::Value& dblJson : json["dbls"])
Expand All @@ -2349,6 +2352,7 @@ void Column::deserialize(const Json::Value &json)
_ints.resize(json["ints"].size());
for (const Json::Value& intJson : json["ints"])
_ints[i++] = intJson.asInt();
_endResetModel();

assert(_ints.size() == _dbls.size());

Expand Down
4 changes: 4 additions & 0 deletions CommonData/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ class Column : public DataSetBaseNode

protected:
std::function<void()> _emitLabelFilterChanged;
std::function<void()> _emitTypeChanged;
std::function<void()> _beginResetModel;
std::function<void()> _endResetModel;


private:
DataSet * const _data;
Expand Down
3 changes: 3 additions & 0 deletions CommonData/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ bool Label::setFilterAllows(bool allowFilter)
{
_filterAllows = allowFilter;
dbUpdate();

_emitLabelFilterChanged();

return true;
}
return false;
Expand Down
3 changes: 3 additions & 0 deletions CommonData/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ friend Column;
DatabaseInterface & db();
const DatabaseInterface & db() const;

protected:
std::function<void()> _emitLabelFilterChanged;

private:

Column * _column;
Expand Down
157 changes: 13 additions & 144 deletions Desktop/data/datasetpackage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@
#include "utils.h"
#include "columnutils.h"
#include "utilities/messageforwarder.h"
#include "datasetpackagesubnodemodel.h"
#include "databaseconnectioninfo.h"
#include "utilities/settings.h"
#include "modules/ribbonmodel.h"
#include "filtermodel.h"
#include <ranges>
#include "variableinfo.h"

//Im having problems getting the proxy models to play nicely with beginRemoveRows etc
//So just reset the whole thing as that is what happens in datasetview
//#define ROUGH_RESET

DataSetPackage * DataSetPackage::_singleton = nullptr;

Expand All @@ -59,16 +53,14 @@ DataSetPackage::DataSetPackage(QObject * parent) : QObject(parent)
connect(this, &DataSetPackage::dataModeChanged, this, &DataSetPackage::onDataModeChanged);

connect(&_databaseIntervalSyncher, &QTimer::timeout, this, &DataSetPackage::synchingIntervalPassed);
connect(&_delayedRefreshTimer, &QTimer::timeout, this, &DataSetPackage::delayedRefresh);

_undoStack = new UndoStack(this);
new UndoStack(this);
}

DataSetPackage::~DataSetPackage()
{
_databaseIntervalSyncher.stop();
_delayedRefreshTimer.stop();


_singleton = nullptr;
}

Expand All @@ -82,7 +74,7 @@ void DataSetPackage::createDataSet()
connectDataSet();
setDefaultWorkspaceEmptyValues();

_dataSecf>shownFilter()->setRFilter(FilterModel::defaultRFilter());
_dataSet->shownFilter()->setRFilter(FilterModel::defaultRFilter());


}
Expand Down Expand Up @@ -359,45 +351,6 @@ std::string DataSetPackage::getComputedColumnError(size_t colIndex) const
return colIndex >= dataColumnCount() ? "" : _dataSet->columns().at(colIndex)->error();
}

void DataSetPackage::setColumnsUsedInEasyFilter(std::set<std::string> usedColumns)
{
std::set<std::string> toUpdate(usedColumns);

for(const auto & nameAndUsed : _columnNameUsedInEasyFilter)
if(nameAndUsed.second)
toUpdate.insert(nameAndUsed.first);

_columnNameUsedInEasyFilter.clear();

for(const std::string & col : usedColumns)
_columnNameUsedInEasyFilter[col] = true;

if(_dataSet)
for(const std::string & col: toUpdate)
{
int idx = findIndexByName(col);
if(idx >= 0)
notifyColumnFilterStatusChanged(idx);
}
}


bool DataSetPackage::isColumnUsedInEasyFilter(const std::string & colName) const
{
return _columnNameUsedInEasyFilter.count(colName) > 0 && _columnNameUsedInEasyFilter.at(colName);
}

void DataSetPackage::notifyColumnFilterStatusChanged(int columnIndex)
{
JASPTIMER_SCOPE(DataSetPackage::notifyColumnFilterStatusChanged);

emit columnsFilteredCountChanged();
//emit headerDataChanged(Qt::Horizontal, columnIndex, columnIndex); //this keeps crashing jasp and i dont know why
beginResetModel();
endResetModel();
}


QVariant DataSetPackage::getColumnTypesWithIcons() const
{
static QVariantList ColumnTypeAndIcons;
Expand All @@ -414,74 +367,7 @@ QVariant DataSetPackage::getColumnTypesWithIcons() const
}


bool DataSetPackage::setColumnType(int columnIndex, columnType newColumnType)
{
return setColumnTypes({columnIndex}, newColumnType);
}

bool DataSetPackage::setColumnTypes(intset columnIndexes, columnType newColumnType)
{
if (_dataSet == nullptr)
return true;

bool somethingChanged = false;

for(int columnIndex : columnIndexes)
{
Column *col = _dataSet->column(columnIndex);

if (col->type() == newColumnType)
continue;


//the only possible "fail" is when an analysis made the column and thus decides the type
//the user might bet
if(col->changeType(newColumnType) == columnTypeChangeResult::generatedFromAnalysis)
{
emit showWarning(tr("Changing column type failed"), tr("The column '%1' is generated by an analysis and its type is fixed.").arg(tq(col->name())));
continue;
}

emit columnDataTypeChanged(tq(_dataSet->column(columnIndex)->name()));
somethingChanged = true;
}

if(somethingChanged)
refreshWithDelay();

return somethingChanged;
}

void DataSetPackage::refreshWithDelay()
{
_delayedRefreshTimer.setSingleShot(true);
_delayedRefreshTimer.setInterval(100);
_delayedRefreshTimer.start();
}

void DataSetPackage::delayedRefresh()
{
refresh();
}

void DataSetPackage::refreshColumn(QString columnName)
{
beginResetModel();
endResetModel();

return;
/*
if(!_dataSet) return;
int colIndex = getColumnIndex(columnName);
if(colIndex >= 0)
{
QModelIndex p = indexForSubNode(_dataSet->dataNode());
emit dataChanged(index(0, colIndex, p), index(rowCount(p), colIndex, p));
emit headerDataChanged(Qt::Horizontal, colIndex, colIndex);
}*/
}

void DataSetPackage::columnWasOverwritten(const std::string & columnName, const std::string &)
{
Expand Down Expand Up @@ -550,33 +436,20 @@ void DataSetPackage::dbDelete()

void DataSetPackage::resetVariableTypes()
{
for (Column * col : _dataSet->columns())
{
columnType guessedType = col->resetValues(PreferencesModel::prefs()->thresholdScale());

if(guessedType != col->type() && col->changeType(guessedType) == columnTypeChangeResult::changed)
{
emit columnDataTypeChanged(tq(col->name()));
refreshWithDelay();
}
}
}

int DataSetPackage::getColIndex(QVariant colID)
{
if(colID.typeId() == QMetaType::Int || colID.typeId() == QMetaType::UInt)
return colID.typeId() == QMetaType::Int ? colID.toInt() : colID.toUInt();

else
return _dataSet->getColumnIndex(fq(colID.toString()));
if(_dataSet)
_dataSet->resetVariableTypes();
}

bool DataSetPackage::initColumnWithStrings(QVariant colId, const std::string & newName, const stringvec &values, const stringvec & labels, const std::string & title, columnType desiredType, const stringset & emptyValues)
bool DataSetPackage::initColumnWithStrings(QVariant colID, const std::string & newName, const stringvec &values, const stringvec & labels, const std::string & title, columnType desiredType, const stringset & emptyValues)
{
JASPTIMER_SCOPE(DataSetPackage::initColumnWithStrings);

int colIndex = colID.typeId() == QMetaType::Int || colID.typeId() == QMetaType::UInt
? (colID.typeId() == QMetaType::Int ? colID.toInt() : colID.toUInt())
: _dataSet->getColumnIndex(fq(colID.toString()));

return _dataSet->initColumnWithStrings(
getColIndex(colId), newName, values, labels, title, desiredType, emptyValues,
colIndex, newName, values, labels, title, desiredType, emptyValues,
Settings::value(Settings::THRESHOLD_SCALE).toInt(),
PreferencesModel::prefs()->orderByValueByDefault());
}
Expand All @@ -590,13 +463,9 @@ void DataSetPackage::initializeComputedColumns()

stringvec DataSetPackage::getColumnNames()
{
stringvec names;

if(_dataSet)
for(const Column * col : _dataSet->columns())
names.push_back(col->name());

return names;
return _dataSet->getColumnNames();
return {};
}

bool DataSetPackage::isColumnDifferentFromStringValues(const std::string & columnName, const std::string & title, const stringvec & strVals, const stringvec & strLabs, const stringset & strEmptyVals)
Expand Down
8 changes: 2 additions & 6 deletions Desktop/data/datasetpackage.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ class DataSetPackage : public QObject
void enginesReceiveNewData();
bool enginesInitializing() { return emit enginesInitializingSignal(); }

UndoStack * undoStack () { return _undoStack; }


void waitForExportResultsReady();

void beginLoadingData( bool informEngines = true);
Expand Down Expand Up @@ -184,7 +181,7 @@ class DataSetPackage : public QObject

bool isColumnInvalidated( size_t colIndex) const;

bool setColumnType(int columnIndex, columnType newColumnType);

bool setColumnTypes(intset columnIndexes, columnType newColumnType);


Expand Down Expand Up @@ -328,8 +325,7 @@ public slots:

bool _synchingData = false;

QTimer _databaseIntervalSyncher,
_delayedRefreshTimer;
QTimer _databaseIntervalSyncher;
};

#endif // FILEPACKAGE_H
Loading

0 comments on commit fb42ce1

Please sign in to comment.