plot: Correctly restore Y axis check states

When restoring the plot state we always checked set the use-on-y-axis
checkbox when a colour was defined for a field. This is correct most of
the times because a field doesn't have a colour as long as it's not used
in the plot. However, when you uncheck a field it keeps its colour which
starts causing issues as soon these settings are reloaded.

The solution is to not only store the colour but also the check state
independently. This changes the project files format though, so the code
needs to support loading old and new file formats.

See issue #1245.
This commit is contained in:
Martin Kleusberg
2017-12-01 12:59:42 +01:00
parent dcc0c32c39
commit 16263452c8
2 changed files with 36 additions and 15 deletions

View File

@@ -56,7 +56,7 @@ void PlotDock::updatePlot(SqliteTableModel* model, BrowseDataTableSettings* sett
// save current selected columns, so we can restore them after the update
QString sItemX; // selected X column
QMap<QString, QColor> mapItemsY; // selected Y columns with color
QMap<QString, PlotSettings> mapItemsY; // selected Y columns with color
if(keepOrResetSelection)
{
@@ -68,19 +68,13 @@ void PlotDock::updatePlot(SqliteTableModel* model, BrowseDataTableSettings* sett
sItemX = item->text(PlotColumnField);
if(item->checkState(PlotColumnY) == Qt::Checked)
mapItemsY[item->text(PlotColumnField)] = item->backgroundColor(PlotColumnY);
mapItemsY[item->text(PlotColumnField)] = PlotSettings(0, 0, item->backgroundColor(PlotColumnY), item->checkState(PlotColumnY) == Qt::Checked);
}
} else {
// Get the plot columns to select from the stored browse table information
sItemX = m_currentTableSettings->plotXAxis;
QMap<QString, PlotSettings> axesY = m_currentTableSettings->plotYAxes;
auto it = axesY.constBegin();
while(it != axesY.constEnd())
{
mapItemsY.insert(it.key(), it.value().colour);
++it;
}
mapItemsY = m_currentTableSettings->plotYAxes;
}
ui->treePlotColumns->clear();
@@ -106,11 +100,12 @@ void PlotDock::updatePlot(SqliteTableModel* model, BrowseDataTableSettings* sett
// restore previous check state
if(mapItemsY.contains(columnitem->text(PlotColumnField)))
{
columnitem->setCheckState(PlotColumnY, Qt::Checked);
columnitem->setBackgroundColor(PlotColumnY, mapItemsY[columnitem->text(PlotColumnField)]);
}
else
columnitem->setCheckState(PlotColumnY, mapItemsY[columnitem->text(PlotColumnField)].active ? Qt::Checked : Qt::Unchecked);
columnitem->setBackgroundColor(PlotColumnY, mapItemsY[columnitem->text(PlotColumnField)].colour);
} else {
columnitem->setCheckState(PlotColumnY, Qt::Unchecked);
}
if(sItemX == columnitem->text(PlotColumnField))
columnitem->setCheckState(PlotColumnX, Qt::Checked);
else
@@ -132,8 +127,8 @@ void PlotDock::updatePlot(SqliteTableModel* model, BrowseDataTableSettings* sett
// restore previous check state
if(mapItemsY.contains(columnitem->text(PlotColumnField)))
{
columnitem->setCheckState(PlotColumnY, Qt::Checked);
columnitem->setBackgroundColor(PlotColumnY, mapItemsY[columnitem->text(PlotColumnField)]);
columnitem->setCheckState(PlotColumnY, mapItemsY[columnitem->text(PlotColumnField)].active ? Qt::Checked : Qt::Unchecked);
columnitem->setBackgroundColor(PlotColumnY, mapItemsY[columnitem->text(PlotColumnField)].colour);
} else {
columnitem->setCheckState(PlotColumnY, Qt::Unchecked);
}
@@ -295,6 +290,13 @@ void PlotDock::on_treePlotColumns_itemChanged(QTreeWidgetItem* changeitem, int c
m_currentTableSettings->plotXAxis = QString();
}
} else if(column == PlotColumnY) {
// Save check state of this column
if(m_currentTableSettings)
{
PlotSettings& plot_settings = m_currentTableSettings->plotYAxes[changeitem->text(PlotColumnField)];
plot_settings.active = (changeitem->checkState(column) == Qt::Checked);
}
if(changeitem->checkState(column) == Qt::Checked)
{
// Generate a default colour if none isn't set yet
@@ -391,6 +393,7 @@ void PlotDock::on_treePlotColumns_itemDoubleClicked(QTreeWidgetItem* item, int c
if(m_currentTableSettings)
{
PlotSettings& plot_settings = m_currentTableSettings->plotYAxes[item->text(PlotColumnField)];
plot_settings.active = (item->checkState(column) == Qt::Checked);
plot_settings.colour = color;
plot_settings.lineStyle = ui->comboLineType->currentIndex();
plot_settings.pointShape = (ui->comboPointShape->currentIndex() > 0 ? (ui->comboPointShape->currentIndex()+1) : ui->comboPointShape->currentIndex());

View File

@@ -25,12 +25,27 @@ public:
int lineStyle;
int pointShape;
QColor colour;
bool active;
PlotSettings() :
lineStyle(0),
pointShape(0),
active(false)
{}
PlotSettings(int _lineStyle, int _pointShape, QColor _colour, bool _active) :
lineStyle(_lineStyle),
pointShape(_pointShape),
colour(_colour),
active(_active)
{}
friend QDataStream& operator<<(QDataStream& stream, const PlotDock::PlotSettings& object)
{
stream << object.lineStyle;
stream << object.pointShape;
stream << object.colour;
stream << object.active;
return stream;
}
@@ -40,6 +55,9 @@ public:
stream >> object.pointShape;
stream >> object.colour;
if(!stream.atEnd())
stream >> object.active;
return stream;
}
};