1 // Copyright (C) 2010-2013 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "pqCustomPlotSettingsModel.h"
22 #include "vtkSMChartRepresentationProxy.h"
23 #include "pqDataRepresentation.h"
24 #include "vtkWeakPointer.h"
25 #include "vtkSMPropertyHelper.h"
30 class pqCustomPlotSettingsModel::pqImplementation
37 vtkWeakPointer<vtkSMChartRepresentationProxy> RepresentationProxy;
38 QPointer<pqDataRepresentation> Representation;
41 pqCustomPlotSettingsModel::pqCustomPlotSettingsModel(QObject* parentObject) :
42 Superclass(parentObject), Implementation(new pqImplementation())
46 pqCustomPlotSettingsModel::~pqCustomPlotSettingsModel()
48 delete this->Implementation;
51 //-----------------------------------------------------------------------------
52 void pqCustomPlotSettingsModel::setRepresentation(pqDataRepresentation* rep)
54 Superclass::setRepresentation(rep);
56 if (!rep || rep == this->Implementation->Representation)
61 if (this->Implementation->Representation)
63 QObject::disconnect(this->Implementation->Representation, 0, this, 0);
66 this->Implementation->RepresentationProxy =
67 vtkSMChartRepresentationProxy::SafeDownCast(rep->getProxy());
68 this->Implementation->Representation = rep;
71 //-----------------------------------------------------------------------------
72 pqDataRepresentation* pqCustomPlotSettingsModel::representation() const
74 return this->Implementation->Representation;
77 //-----------------------------------------------------------------------------
78 bool pqCustomPlotSettingsModel::setData(const QModelIndex &idx, const QVariant &value,
82 if (idx.isValid() && idx.model() == this)
84 if (idx.column() == 1 && (role == Qt::DisplayRole || role == Qt::EditRole))
86 QString name = value.toString();
89 this->setSeriesLabel(idx.row(), name);
92 else if(idx.column() == 0 && role == Qt::CheckStateRole)
95 int checkstate = value.toInt();
96 this->setSeriesEnabled(idx.row(), checkstate == Qt::Checked);
102 //-----------------------------------------------------------------------------
103 void pqCustomPlotSettingsModel::setSeriesEnabled(int row, bool enabled)
105 if (row >= 0 && row < this->rowCount(QModelIndex()))
110 if (enabled && !(this->IgnoreUnitsModeOn))
112 QString unit = getUnit(QString(this->getSeriesName(row)));
114 for (int i = 0; i < rowCount(QModelIndex()); i++)
121 bool rowChanged = false;
122 QString seriesName = QString(this->getSeriesName(i));
123 QString seriesUnit = getUnit(seriesName);
125 if ((seriesUnit != unit) && getSeriesEnabled(i))
127 vtkSMPropertyHelper(this->Implementation->RepresentationProxy,
128 "SeriesVisibility").SetStatus(this->getSeriesName(i), 0);
132 if (!seriesUnit.isEmpty() && this->AutoSelectModeOn &&
133 (seriesUnit == unit) && !getSeriesEnabled(i))
135 vtkSMPropertyHelper(this->Implementation->RepresentationProxy,
136 "SeriesVisibility").SetStatus(this->getSeriesName(i), 1);
154 vtkSMPropertyHelper(this->Implementation->RepresentationProxy,
155 "SeriesVisibility").SetStatus(this->getSeriesName(row), enabled ? 1 : 0);
156 this->Implementation->RepresentationProxy->UpdateVTKObjects();
158 this->setSeriesColor(row, this->getSeriesColor(row));
159 QModelIndex topLeft = this->createIndex(minRow, 0);
160 QModelIndex bottomRight = this->createIndex(maxRow, 0);
161 emit this->dataChanged(topLeft, bottomRight);
162 emit this->redrawChart();
163 this->updateCheckState(0, Qt::Horizontal);
167 //-----------------------------------------------------------------------------
168 void pqCustomPlotSettingsModel::SetIgnoreUnitsModeOn(bool enabled)
170 this->IgnoreUnitsModeOn = enabled;
172 if (this->IgnoreUnitsModeOn)
174 this->setCheckable(0, Qt::Horizontal, true);
175 this->setCheckState(0, Qt::Horizontal, Qt::Unchecked);
179 this->setCheckState(0, Qt::Horizontal, Qt::Unchecked);
180 this->setCheckable(0, Qt::Horizontal, false);
184 //-----------------------------------------------------------------------------
185 void pqCustomPlotSettingsModel::SetAutoSelectModeOn(bool enabled)
187 this->AutoSelectModeOn = enabled;
188 if (enabled && !this->IgnoreUnitsModeOn)
190 for (int i = 0; i < rowCount(QModelIndex()); i++)
192 if (getSeriesEnabled(i))
194 setSeriesEnabled(i, true);
201 //-----------------------------------------------------------------------------
202 QString pqCustomPlotSettingsModel::getUnit(const QString& seriesName)
206 int index1 = seriesName.lastIndexOf("]");
207 if (index1 == seriesName.size() - 1)
209 int index2 = seriesName.lastIndexOf(" [");
212 int start = index2 + 2;
213 unit = seriesName.mid(index2 + 2, index1 - start);