@@ -18,11 +18,11 @@ void Column::setAutoSortByValuesByDefault(bool autoSort)
1818}
1919
2020Column::Column (DataSet * data, int id)
21- : DataSetBaseNode(dataSetBaseNodeType::column, data-> dataNode () ),
21+ : DataSetBaseNode(dataSetBaseNodeType::column, data),
2222 _data( data),
2323 _id( id),
2424 _emptyValues( new EmptyValues(data->emptyValues ())),
25- _doubleDummy( new Label( this )),
25+ _doubleDummy( _createLabel( )),
2626 _autoSortByValue( _autoSortByValuesByDefault)
2727{
2828 if (_id != -1 )
@@ -169,6 +169,8 @@ void Column::setType(columnType colType)
169169 _type = colType;
170170 db ().columnSetType (_id, _type);
171171 incRevision ();
172+
173+ _emitTypeChanged ();
172174}
173175
174176bool Column::hasCustomEmptyValues () const
@@ -601,9 +603,9 @@ bool Column::overwriteDataAndType(stringvec data, columnType colType)
601603
602604 if (data.size () != _data->rowCount ())
603605 {
604- if (data.size () == _data->filter ()->filteredRowCount ())
606+ if (data.size () == _data->shownFilter ()->filteredRowCount ())
605607 {
606- const boolvec & filtered = _data->filter ()->filtered ();
608+ const boolvec & filtered = _data->shownFilter ()->filtered ();
607609 stringvec newData;
608610 newData . reserve (filtered.size ());
609611
@@ -688,6 +690,8 @@ void Column::_sortLabelsByOrder()
688690void Column::labelsClear (bool doIncRevision)
689691{
690692 db ().labelsClear (_id);
693+ for (Label * l : _labels)
694+ delete l;
691695 _labels.clear ();
692696 _labelByIntsIdMap.clear ();
693697 _labelByValDis.clear ();
@@ -762,6 +766,22 @@ int Column::_labelMapIt(Label * label)
762766 return label->intsId ();
763767}
764768
769+ Label * Column::_createLabel ()
770+ {
771+ return new Label (this );
772+ }
773+
774+ Label * Column::_createLabel (int value)
775+ {
776+ return new Label (this , value);
777+ }
778+
779+ Label * Column::_createLabel (const std::string &label, int value, bool filterAllows, const std::string &description, const Json::Value &originalValue, int order, int id)
780+ {
781+ return new Label (this , label, value, filterAllows, description, originalValue, order, id);
782+ }
783+
784+
765785int Column::labelsAdd (int value, const std::string & display, bool filterAllows, const std::string & description, const Json::Value & originalValue, int order, int id)
766786{
767787 JASPTIMER_SCOPE (Column::labelsAdd lotsa arg);
@@ -771,8 +791,11 @@ int Column::labelsAdd(int value, const std::string & display, bool filterAllows,
771791 if (_labelByValDis.count (valDisplay))
772792 return _labelByValDis.at (valDisplay)->intsId ();
773793
774- Label * label = new Label ( this , display, value, filterAllows, description, originalValue, order, id);
794+ Label * label = _createLabel ( display, value, filterAllows, description, originalValue, order, id);
775795 _labels.push_back (label);
796+
797+ if (hasLabelFilter ())
798+ _emitLabelFilterChanged ();
776799
777800 return _labelMapIt (label);
778801}
@@ -797,7 +820,7 @@ int Column::labelsSet(int labelIndex, int value, const std::string &display, boo
797820
798821 if (_labels.size () < labelIndex+1 )
799822 _labels . resize (labelIndex+1 );
800- _labels[labelIndex] = new Label ( this , display, value, filterAllows, description, originalValue, order, id);
823+ _labels[labelIndex] = _createLabel ( display, value, filterAllows, description, originalValue, order, id);
801824 label = _labels[labelIndex];
802825 }
803826
@@ -952,14 +975,24 @@ int Column::labelsTempCount()
952975 return _labelsTemp.size ();
953976}
954977
978+ int Column::labelsTempCount () const
979+ {
980+ #ifdef JASP_DEBUG
981+ if (_revision != _labelsTempRevision)
982+ throw std::runtime_error (" labelsTempCount() const was not initialized" );
983+ #endif
984+
985+ return _labelsTemp.size ();
986+ }
987+
955988int Column::nonFilteredNumericsCount ()
956989{
957990 if (_nonFilteredNumericsCount == -1 )
958991 {
959992 doubleset numerics;
960993
961994 for (size_t r=0 ; r<_data->rowCount (); r++)
962- if (_data->filter ()->filtered ()[r] && !isEmptyValue (_dbls[r]))
995+ if (_data->shownFilter ()->filtered ()[r] && !isEmptyValue (_dbls[r]))
963996 numerics.insert (_dbls[r]);
964997
965998 _nonFilteredNumericsCount = numerics.size ();
@@ -968,31 +1001,57 @@ int Column::nonFilteredNumericsCount()
9681001 return _nonFilteredNumericsCount;
9691002}
9701003
1004+ int Column::nonFilteredNumericsCount () const
1005+ {
1006+ #ifdef JASP_DEBUG
1007+ if (_nonFilteredNumericsCount == -1 )
1008+ throw std::runtime_error (" nonFilteredNumericsCount() const was not initialized" );
1009+ #endif
1010+
1011+ return _nonFilteredNumericsCount;
1012+ }
1013+
9711014stringvec Column::nonFilteredLevels ()
9721015{
9731016 if (_nonFilteredLevels.empty ())
9741017 {
975- stringset levels;
1018+ stringset levels;
1019+ intset collected;
9761020 for (size_t r=0 ; r<_data->rowCount (); r++)
977- if (_data->filter ()->filtered ()[r])
1021+ if (_data->shownFilter ()->filtered ()[r])
9781022 {
9791023 if (_ints[r] != Label::DOUBLE_LABEL_VALUE)
9801024 {
981- Label * label = labelByIntsId (_ints[r]);
982- if (label && !label->isEmptyValue ())
983- levels.insert (label->label ());
1025+ if (!collected.count (_ints[r]))
1026+ {
1027+ Label * label = labelByIntsId (_ints[r]);
1028+ if (label && !label->isEmptyValue ())
1029+ levels.insert (label->label ());
1030+
1031+ collected.insert (_ints[r]);
1032+ }
9841033 }
9851034 else if (!isEmptyValue (_dbls[r]))
9861035 levels.insert (ColumnUtils::doubleToString (_dbls[r]));
9871036 }
9881037
9891038 // Use the right label order
9901039 for (std::string& label : _labelsTemp)
991- if (levels.find (label) != levels. end ( ))
1040+ if (levels.count (label))
9921041 _nonFilteredLevels.push_back (label);
9931042 }
9941043
995- return _nonFilteredLevels;
1044+ return _nonFilteredLevels;
1045+ }
1046+
1047+ stringvec Column::nonFilteredLevels () const
1048+ {
1049+ #ifdef JASP_DEBUG
1050+ if (_nonFilteredLevels.empty ())
1051+ throw std::runtime_error (" nonFilteredLevels() const was not initialized" );
1052+ #endif
1053+
1054+ return _nonFilteredLevels;
9961055}
9971056
9981057void Column::nonFilteredCountersReset ()
@@ -1011,7 +1070,15 @@ int Column::labelsTempNumerics()
10111070const stringvec &Column::labelsTemp ()
10121071{
10131072 labelsTempCount (); // generate the list if need be
1014-
1073+
1074+ return _labelsTemp;
1075+ }
1076+
1077+
1078+ const stringvec &Column::labelsTemp () const
1079+ {
1080+ labelsTempCount (); // wont generate the list if need be, instead it will crash on debug if its not yet generated
1081+
10151082 return _labelsTemp;
10161083}
10171084
@@ -1023,6 +1090,14 @@ std::string Column::labelsTempDisplay(size_t tempLabelIndex)
10231090 return _labelsTemp[tempLabelIndex];
10241091}
10251092
1093+ std::string Column::labelsTempDisplay (size_t tempLabelIndex) const
1094+ {
1095+ if (labelsTempCount () <= tempLabelIndex)
1096+ return " " ;
1097+
1098+ return _labelsTemp[tempLabelIndex];
1099+ }
1100+
10261101int Column::labelIndexNonEmpty (Label *label) const
10271102{
10281103 return !_labelNonEmptyIndexByLabel.count (label) ? -1 : _labelNonEmptyIndexByLabel.at (label);
@@ -1059,6 +1134,21 @@ std::string Column::labelsTempValue(size_t tempLabelIndex, bool fancyEmptyValue)
10591134 return doubleToDisplayString (_labelsTempDbls[tempLabelIndex], fancyEmptyValue);
10601135}
10611136
1137+ std::string Column::labelsTempValue (size_t tempLabelIndex, bool fancyEmptyValue) const
1138+ {
1139+ if (labelsTempCount () <= tempLabelIndex)
1140+ return " " ;
1141+
1142+ Label * label = labelByIndexNotEmpty (tempLabelIndex);
1143+
1144+ if (label)
1145+ return label->originalValueAsString (fancyEmptyValue);
1146+
1147+ // So its not from a Label, this means its from _dbls
1148+ // So that means the display value is actually the same as the value so:
1149+ return doubleToDisplayString (_labelsTempDbls[tempLabelIndex], fancyEmptyValue);
1150+ }
1151+
10621152double Column::labelsTempValueDouble (size_t tempLabelIndex)
10631153{
10641154 if (labelsTempCount () <= tempLabelIndex)
@@ -1113,7 +1203,10 @@ std::string Column::_getLabelDisplayStringByValue(int key, bool ignoreEmptyValue
11131203std::string Column::getValue (size_t row, bool fancyEmptyValue, bool ignoreEmptyValue, columnType asType) const
11141204{
11151205 if (asType == columnType::unknown)
1206+ {
11161207 asType = _type;
1208+ _emitTypeChanged ();
1209+ }
11171210
11181211 if (row < rowCount ())
11191212 {
@@ -1564,7 +1657,7 @@ bool Column::setValue(size_t row, const std::string & value, const std::string &
15641657{
15651658 JASPTIMER_SCOPE (Column::setValue (size_t row, const std::string & value, const std::string & label, writeToDB));
15661659
1567- // If value != "" and label == "" that means we got copy pasted stuff in the viewer. And we just dont have labels, but we can treat it like we are editing
1660+ // If value != "" and label == "" that means we got copy pasted stuff in the viewer, and we just dont have labels, but we can treat it like we are editing.
15681661 // if both are "" we just want to clear the cell
15691662 // the assumption is that this is not direct user-input, but internal jasp stuff.
15701663 if (value == " " && label == " " )
@@ -1958,7 +2051,7 @@ bool Column::allLabelsPassFilter() const
19582051 return true ;
19592052}
19602053
1961- bool Column::hasFilter () const
2054+ bool Column::hasLabelFilter () const
19622055{
19632056 return !allLabelsPassFilter ();
19642057}
@@ -2137,6 +2230,8 @@ void Column::deserializeLabelsForCopy(const Json::Value & labels)
21372230 beginBatchedLabelsDB ();
21382231 _labelByIntsIdMap.clear ();
21392232 _labelByValDis.clear ();
2233+ for (Label * l : _labels)
2234+ delete l;
21402235 _labels.clear ();
21412236
21422237 if (labels.isArray ())
@@ -2158,6 +2253,7 @@ void Column::deserializeLabelsForRevert(const Json::Value & labels)
21582253{
21592254 labelsTempReset ();
21602255
2256+ _beginResetModel ();
21612257 beginBatchedLabelsDB ();
21622258
21632259 // intset updatedLbls;
@@ -2201,7 +2297,8 @@ void Column::deserializeLabelsForRevert(const Json::Value & labels)
22012297 missingLbls.insert(idLabel.first);*/
22022298
22032299 endBatchedLabelsDB ();
2204-
2300+ _endResetModel ();
2301+
22052302 /* The following is already implied by endBatchedLabelsDB because it deletes all labels first anyway
22062303 for(int id : missingLbls)
22072304 {
@@ -2227,33 +2324,25 @@ void Column::deserialize(const Json::Value &json)
22272324 std::string name = json[" name" ].asString (),
22282325 title = json[" title" ].asString ();
22292326
2230- _name = getUniqueName (name);
2231- db ().columnSetName (_id, _name);
2327+ setName (getUniqueName (name));
22322328
22332329 // If title was equal to name, then they should still stay the same if the name is changed to be unique.
2234- _title = name == title ? _name : title;
2235- db ().columnSetTitle (_id, _title);
2330+ setTitle ( name == title ? _name : title);
2331+ setDescription ( json[" description" ] .asString () );
2332+ setType ( columnType ( json[" type" ] .asInt ()) );
2333+ setInvalidated ( json[" invalidated" ] .asBool () );
2334+ setCodeType ( computedColumnType ( json[" codeType" ] .asInt ()) );
2335+ setRCode ( json[" rCode" ] .asString () );
2336+ setError ( json[" error" ] .asString () );
2337+ setAnalysisId ( json[" analysisId" ] .asInt () );
2338+ setConstructorJson ( json[" constructorJson" ] );
2339+ setAutoSortByValue ( json[" autoSortByValue" ] .asBool () );
22362340
2237- _description = json[" description" ].asString ();
2238- db ().columnSetDescription (_id, _description);
2239-
2240- _type = columnType (json[" type" ].asInt ());
2241- db ().columnSetType (_id, _type);
2242-
2243- _invalidated = json[" invalidated" ].asBool ();
2244- _codeType = computedColumnType (json[" codeType" ].asInt ());
2245- _rCode = json[" rCode" ].asString ();
2246- _error = json[" error" ].asString ();
2247- _analysisId = json[" analysisId" ].asInt ();
2248- _constructorJson = json[" constructorJson" ];
2249- _autoSortByValue = json[" autoSortByValue" ].asBool ();
2250-
2251- db ().columnSetComputedInfo (_id, _analysisId, _invalidated, _codeType, _rCode, _error, constructorJsonStr ());
2252-
22532341 deserializeLabelsForCopy (json[" labels" ]);
22542342
22552343 _emptyValues->fromJson (json[" customEmptyValues" ]);
2256-
2344+
2345+ _beginResetModel ();
22572346 size_t i=0 ;
22582347 _dbls.resize (json[" dbls" ].size ());
22592348 for (const Json::Value& dblJson : json[" dbls" ])
@@ -2263,6 +2352,7 @@ void Column::deserialize(const Json::Value &json)
22632352 _ints.resize (json[" ints" ].size ());
22642353 for (const Json::Value& intJson : json[" ints" ])
22652354 _ints[i++] = intJson.asInt ();
2355+ _endResetModel ();
22662356
22672357 assert (_ints.size () == _dbls.size ());
22682358
0 commit comments