Salome HOME
Merge branch 'origin/akl/22533'
[modules/paravis.git] / src / Plugins / TableReader / ParaViewPlugin / pqCustomPlotSettingsModel.cxx
1 // Copyright (C) 2010-2014  CEA/DEN, EDF R&D
2 //
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, or (at your option) any later version.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "pqCustomPlotSettingsModel.h"
21
22 #include "vtkSMChartRepresentationProxy.h"
23 #include "pqDataRepresentation.h"
24 #include "vtkWeakPointer.h"
25 #include "vtkSMPropertyHelper.h"
26
27 #include <QPointer>
28
29
30 class pqCustomPlotSettingsModel::pqImplementation
31 {
32 public:
33   pqImplementation()
34 {
35 }
36
37   vtkWeakPointer<vtkSMChartRepresentationProxy> RepresentationProxy;
38   QPointer<pqDataRepresentation> Representation;
39 };
40
41 pqCustomPlotSettingsModel::pqCustomPlotSettingsModel(QObject* parentObject) :
42       Superclass(parentObject), Implementation(new pqImplementation())
43 {
44 }
45
46 pqCustomPlotSettingsModel::~pqCustomPlotSettingsModel()
47 {
48   delete this->Implementation;
49 }
50
51 //-----------------------------------------------------------------------------
52 void pqCustomPlotSettingsModel::setRepresentation(pqDataRepresentation* rep)
53 {
54   Superclass::setRepresentation(rep);
55
56   if (!rep || rep == this->Implementation->Representation)
57   {
58     return;
59   }
60
61   if (this->Implementation->Representation)
62   {
63     QObject::disconnect(this->Implementation->Representation, 0, this, 0);
64   }
65
66   this->Implementation->RepresentationProxy =
67       vtkSMChartRepresentationProxy::SafeDownCast(rep->getProxy());
68   this->Implementation->Representation = rep;
69 }
70
71 //-----------------------------------------------------------------------------
72 pqDataRepresentation* pqCustomPlotSettingsModel::representation() const
73 {
74   return this->Implementation->Representation;
75 }
76
77 //-----------------------------------------------------------------------------
78 bool pqCustomPlotSettingsModel::setData(const QModelIndex &idx, const QVariant &value,
79     int role)
80 {
81   bool result = false;
82   if (idx.isValid() && idx.model() == this)
83   {
84     if (idx.column() == 1 && (role == Qt::DisplayRole || role == Qt::EditRole))
85     {
86       QString name = value.toString();
87       if (!name.isEmpty())
88       {
89         this->setSeriesLabel(idx.row(), name);
90       }
91     }
92     else if(idx.column() == 0 && role == Qt::CheckStateRole)
93     {
94       result = true;
95       int checkstate = value.toInt();
96       this->setSeriesEnabled(idx.row(), checkstate == Qt::Checked);
97     }
98   }
99   return result;
100 }
101
102 //-----------------------------------------------------------------------------
103 void pqCustomPlotSettingsModel::setSeriesEnabled(int row, bool enabled)
104 {
105   if (row >= 0 && row < this->rowCount(QModelIndex()))
106   {
107     int minRow = row;
108     int maxRow = row;
109
110     if (enabled && !(this->IgnoreUnitsModeOn))
111     {
112       QString unit = getUnit(QString(this->getSeriesName(row)));
113
114       for (int i = 0; i < rowCount(QModelIndex()); i++)
115       {
116         if (i == row)
117         {
118           continue;
119         }
120
121         bool rowChanged = false;
122         QString seriesName = QString(this->getSeriesName(i));
123         QString seriesUnit = getUnit(seriesName);
124
125         if ((seriesUnit != unit) && getSeriesEnabled(i))
126         {
127           vtkSMPropertyHelper(this->Implementation->RepresentationProxy,
128               "SeriesVisibility").SetStatus(this->getSeriesName(i), 0);
129           rowChanged = true;
130         }
131
132         if (!seriesUnit.isEmpty() && this->AutoSelectModeOn &&
133             (seriesUnit == unit) && !getSeriesEnabled(i))
134         {
135           vtkSMPropertyHelper(this->Implementation->RepresentationProxy,
136               "SeriesVisibility").SetStatus(this->getSeriesName(i), 1);
137           rowChanged = true;
138         }
139
140         if (rowChanged)
141         {
142           if (i < minRow)
143           {
144             minRow = i;
145           }
146           else if (i > maxRow)
147           {
148             maxRow = i;
149           }
150         }
151       }
152     }
153
154     vtkSMPropertyHelper(this->Implementation->RepresentationProxy,
155         "SeriesVisibility").SetStatus(this->getSeriesName(row), enabled ? 1 : 0);
156     this->Implementation->RepresentationProxy->UpdateVTKObjects();
157
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);
164   }
165 }
166
167 //-----------------------------------------------------------------------------
168 void pqCustomPlotSettingsModel::SetIgnoreUnitsModeOn(bool enabled)
169 {
170   this->IgnoreUnitsModeOn = enabled;
171
172   if (this->IgnoreUnitsModeOn)
173   {
174     this->setCheckable(0, Qt::Horizontal, true);
175     this->setCheckState(0, Qt::Horizontal, Qt::Unchecked);
176   }
177   else
178   {
179     this->setCheckState(0, Qt::Horizontal, Qt::Unchecked);
180     this->setCheckable(0, Qt::Horizontal, false);
181   }
182 }
183
184 //-----------------------------------------------------------------------------
185 void pqCustomPlotSettingsModel::SetAutoSelectModeOn(bool enabled)
186 {
187   this->AutoSelectModeOn = enabled;
188   if (enabled && !this->IgnoreUnitsModeOn)
189   {
190     for (int i = 0; i < rowCount(QModelIndex()); i++)
191     {
192       if (getSeriesEnabled(i))
193       {
194         setSeriesEnabled(i, true);
195         break;
196       }
197     }
198   }
199 }
200
201 //-----------------------------------------------------------------------------
202 QString pqCustomPlotSettingsModel::getUnit(const QString& seriesName)
203 {
204   QString unit("");
205
206   int index1 = seriesName.lastIndexOf("]");
207   if (index1 == seriesName.size() - 1)
208   {
209     int index2 = seriesName.lastIndexOf(" [");
210     if (index2 > 0)
211     {
212       int start = index2 + 2;
213       unit = seriesName.mid(index2 + 2, index1 - start);
214     }
215   }
216
217   return unit;
218 }