Skip to content

Commit

Permalink
make sure columnName.type is handled properly in computed columns by …
Browse files Browse the repository at this point in the history
…reducing code duplication

Also make sure the label isnt overwritten by changing the value in the case of a nonscale computed column
  • Loading branch information
JorisGoosen committed Nov 6, 2024
1 parent f47e394 commit 76b5b11
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 40 deletions.
35 changes: 10 additions & 25 deletions CommonData/dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <regex>
#include "timers.h"
#include "dataset.h"
#include "columnencoder.h"
#include "jsonutilities.h"
#include "databaseinterface.h"

Expand Down Expand Up @@ -586,30 +587,14 @@ const DatabaseInterface &DataSet::db() const

stringset DataSet::findUsedColumnNames(std::string searchThis)
{
//sort of based on rbridge_encodeColumnNamesToBase64
static std::regex nonNameChar("[^\\.A-Za-z0-9]");
std::set<std::string> columnsFound;
size_t foundPos = -1;

for(Column * column : _columns)
{
const std::string & col = column->name();

while((foundPos = searchThis.find(col, foundPos + 1)) != std::string::npos)
{
size_t foundPosEnd = foundPos + col.length();
//First check if it is a "free columnname" aka is there some space or a kind in front of it. We would not want to replace a part of another term (Imagine what happens when you use a columname such as "E" and a filter that includes the term TRUE, it does not end well..)
bool startIsFree = foundPos == 0 || std::regex_match(searchThis.substr(foundPos - 1, 1), nonNameChar);
bool endIsFree = foundPosEnd == searchThis.length() || (std::regex_match(searchThis.substr(foundPosEnd, 1), nonNameChar) && searchThis[foundPosEnd] != '('); //Check for "(" as well because maybe someone has a columnname such as rep or if or something weird like that

if(startIsFree && endIsFree)
{
columnsFound.insert(col);
searchThis.replace(foundPos, col.length(), ""); // remove the found entry
}

}
}

stringset columnsFound, columnsWithTypeFound;
ColumnEncoder::columnEncoder()->encodeRScript(searchThis, &columnsWithTypeFound);

//The found columns now also include the type, but we dont really care about that right now.
//Instead we'll make use of the encode->decode not being symmetrical (for the results to be less ugly) and dropping the type

for(const std::string & colPlusType : columnsWithTypeFound)
columnsFound.insert(ColumnEncoder::columnEncoder()->decode(ColumnEncoder::columnEncoder()->encode(colPlusType)));

return columnsFound;
}
9 changes: 5 additions & 4 deletions Desktop/analysis/analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

#include "log.h"
#include "utils.h"
#include "analysis.h"
#include <boost/bind.hpp>
#include "tempfiles.h"
#include "appinfo.h"
#include "dirs.h"
#include "analyses.h"
#include "analysisform.h"
//#include <boost/bind.hpp>
#include "utilities/qutils.h"
#include "log.h"
#include "utils.h"
#include "utilities/settings.h"
#include "gui/preferencesmodel.h"
#include "utilities/reporter.h"
#include "gui/preferencesmodel.h"
#include "results/resultsjsinterface.h"
#include "utilities/messageforwarder.h"

Analysis::Analysis(size_t id, Modules::AnalysisEntry * analysisEntry, std::string title, std::string moduleVersion, Json::Value *data) :
AnalysisBase(Analyses::analyses(), moduleVersion),
Expand Down
15 changes: 11 additions & 4 deletions Desktop/data/datasetpackage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,10 +963,17 @@ bool DataSetPackage::setLabelValue(const QModelIndex &index, const QString &newL
// if (oldorigval == dbl && newOrigVal == dbl) || (olorigval != dbl && newOrigVal != dbl) then replace both
// if neworigval == dbl and oldorigval != dbl then replace only value

if( label->originalValueAsString(false) != label->labelDisplay() || (originalValue.isDouble() && !label->originalValue().isDouble()))
aChange = label->setOriginalValue(originalValue) || aChange;
else
aChange = label->setOrigValLabel(originalValue) || aChange;
// But only if we are allowed to change both because of https://github.com/jasp-stats/INTERNAL-jasp/issues/2680 (allow editing of only value/label and disable the other one for computed columns
// which means that if this column is a computed column of scale type we are only allowed to change the label and only the value for the other types.
// so in this case this means that if it is a computed column, and of type !scale we do *not* also update the label when updating the value. Because otherwise it would override the data from the computed column...

bool dontSetLabel = label->originalValueAsString(false) != label->labelDisplay() || (originalValue.isDouble() && !label->originalValue().isDouble());

if(!dontSetLabel && column->isComputed() && column->type() != columnType::scale)
dontSetLabel = true;

if(dontSetLabel) aChange = label->setOriginalValue(originalValue) || aChange;
else aChange = label->setOrigValLabel(originalValue) || aChange;
}

column->labelsHandleAutoSort();
Expand Down
6 changes: 1 addition & 5 deletions Desktop/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
#include "gui/aboutmodel.h"
#include "models/columntypesmodel.h"
#include "gui/preferencesmodel.h"
#include "modules/dynamicmodule.h"
#include "modules/ribbonbutton.h"
#include "modules/ribbonmodelfiltered.h"
#include "modules/ribbonmodel.h"
#include "modules/upgrader/upgrader.h"
Expand All @@ -49,15 +47,13 @@
#include "results/resultsjsinterface.h"
#include "modules/ribbonmodeluncommon.h"
#include "results/resultmenumodel.h"
#include "jsonutilities.h"
#include "utilities/helpmodel.h"
#include "utilities/messageforwarder.h"
#include "utilities/reporter.h"
#include "utilities/codepageswindows.h"
#include "widgets/filemenu/filemenu.h"
#include "data/workspacemodel.h"

#include "utilities/languagemodel.h"
#include <vector>

using namespace std;

Expand Down
2 changes: 0 additions & 2 deletions QMLComponents/analysisform.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include "boundcontrols/boundcontrol.h"
#include "analysisbase.h"
#include "models/listmodel.h"
#include "models/listmodeltermsavailable.h"
#include "utilities/messageforwarder.h"
#include "utilities/qutils.h"
#include <queue>

Expand Down

0 comments on commit 76b5b11

Please sign in to comment.