Back to index

salome-paravis  6.5.0
pqCustomPlotSettingsModel.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "pqCustomPlotSettingsModel.h"
00021 
00022 #include "vtkSMChartRepresentationProxy.h"
00023 #include "pqDataRepresentation.h"
00024 #include "vtkWeakPointer.h"
00025 #include "vtkSMPropertyHelper.h"
00026 
00027 #include <QPointer>
00028 
00029 
00030 class pqCustomPlotSettingsModel::pqImplementation
00031 {
00032 public:
00033   pqImplementation()
00034   {
00035   }
00036 
00037   vtkWeakPointer<vtkSMChartRepresentationProxy> RepresentationProxy;
00038   QPointer<pqDataRepresentation> Representation;
00039 };
00040 
00041 pqCustomPlotSettingsModel::pqCustomPlotSettingsModel(QObject* parentObject) :
00042   Superclass(parentObject), Implementation(new pqImplementation())
00043 {
00044 }
00045 
00046 pqCustomPlotSettingsModel::~pqCustomPlotSettingsModel()
00047 {
00048   delete this->Implementation;
00049 }
00050 
00051 //-----------------------------------------------------------------------------
00052 void pqCustomPlotSettingsModel::setRepresentation(pqDataRepresentation* rep)
00053 {
00054   Superclass::setRepresentation(rep);
00055 
00056   if (!rep || rep == this->Implementation->Representation)
00057     {
00058     return;
00059     }
00060 
00061   if (this->Implementation->Representation)
00062     {
00063     QObject::disconnect(this->Implementation->Representation, 0, this, 0);
00064     }
00065 
00066   this->Implementation->RepresentationProxy =
00067     vtkSMChartRepresentationProxy::SafeDownCast(rep->getProxy());
00068   this->Implementation->Representation = rep;
00069 }
00070 
00071 //-----------------------------------------------------------------------------
00072 pqDataRepresentation* pqCustomPlotSettingsModel::representation() const
00073 {
00074   return this->Implementation->Representation;
00075 }
00076 
00077 //-----------------------------------------------------------------------------
00078 bool pqCustomPlotSettingsModel::setData(const QModelIndex &idx, const QVariant &value,
00079                                    int role)
00080 {
00081   bool result = false;
00082   if (idx.isValid() && idx.model() == this)
00083     {
00084     if (idx.column() == 1 && (role == Qt::DisplayRole || role == Qt::EditRole))
00085       {
00086       QString name = value.toString();
00087       if (!name.isEmpty())
00088         {
00089         this->setSeriesLabel(idx.row(), name);
00090         }
00091       }
00092     else if(idx.column() == 0 && role == Qt::CheckStateRole)
00093       {
00094       result = true;
00095       int checkstate = value.toInt();
00096       this->setSeriesEnabled(idx.row(), checkstate == Qt::Checked);
00097       }
00098     }
00099   return result;
00100 }
00101 
00102 //-----------------------------------------------------------------------------
00103 void pqCustomPlotSettingsModel::setSeriesEnabled(int row, bool enabled)
00104 {
00105   if (row >= 0 && row < this->rowCount(QModelIndex()))
00106     {
00107       int minRow = row;
00108       int maxRow = row;
00109       
00110       if (enabled && !(this->IgnoreUnitsModeOn)) 
00111        {
00112          QString unit = getUnit(QString(this->getSeriesName(row)));
00113 
00114          for (int i = 0; i < rowCount(QModelIndex()); i++)
00115            {
00116              if (i == row)
00117               {
00118                 continue;
00119               }
00120 
00121              bool rowChanged = false;
00122              QString seriesName = QString(this->getSeriesName(i));
00123              QString seriesUnit = getUnit(seriesName);
00124 
00125              if ((seriesUnit != unit) && getSeriesEnabled(i))
00126               {
00127                 vtkSMPropertyHelper(this->Implementation->RepresentationProxy,
00128                 "SeriesVisibility").SetStatus(this->getSeriesName(i), 0);
00129                 rowChanged = true;
00130               }
00131              
00132              if (!seriesUnit.isEmpty() && this->AutoSelectModeOn &&
00133                 (seriesUnit == unit) && !getSeriesEnabled(i))
00134               {
00135                 vtkSMPropertyHelper(this->Implementation->RepresentationProxy,
00136                 "SeriesVisibility").SetStatus(this->getSeriesName(i), 1);
00137                 rowChanged = true;
00138               }
00139              
00140              if (rowChanged)
00141               {
00142                 if (i < minRow)
00143                   {
00144                     minRow = i;
00145                   }
00146                 else if (i > maxRow)
00147                   {
00148                     maxRow = i;
00149                   }
00150               }
00151            }
00152        }
00153       
00154       vtkSMPropertyHelper(this->Implementation->RepresentationProxy,
00155       "SeriesVisibility").SetStatus(this->getSeriesName(row), enabled ? 1 : 0);
00156       this->Implementation->RepresentationProxy->UpdateVTKObjects();
00157     
00158       this->setSeriesColor(row, this->getSeriesColor(row));
00159       QModelIndex topLeft = this->createIndex(minRow, 0);
00160       QModelIndex bottomRight = this->createIndex(maxRow, 0);
00161       emit this->dataChanged(topLeft, bottomRight);
00162       emit this->redrawChart();
00163       this->updateCheckState(0, Qt::Horizontal);
00164     }
00165 }
00166 
00167 //-----------------------------------------------------------------------------
00168 void pqCustomPlotSettingsModel::SetIgnoreUnitsModeOn(bool enabled)
00169 {
00170   this->IgnoreUnitsModeOn = enabled;
00171 
00172   if (this->IgnoreUnitsModeOn)
00173     {
00174       this->setCheckable(0, Qt::Horizontal, true);
00175       this->setCheckState(0, Qt::Horizontal, Qt::Unchecked);
00176     }
00177   else
00178     {
00179       this->setCheckState(0, Qt::Horizontal, Qt::Unchecked);
00180       this->setCheckable(0, Qt::Horizontal, false);
00181     }
00182 }
00183 
00184 //-----------------------------------------------------------------------------
00185 void pqCustomPlotSettingsModel::SetAutoSelectModeOn(bool enabled)
00186 {
00187   this->AutoSelectModeOn = enabled;
00188   if (enabled && !this->IgnoreUnitsModeOn)
00189     {
00190       for (int i = 0; i < rowCount(QModelIndex()); i++)
00191        {
00192          if (getSeriesEnabled(i))
00193            {
00194              setSeriesEnabled(i, true);
00195              break;
00196            }
00197        }
00198     }
00199 }
00200 
00201 //-----------------------------------------------------------------------------
00202 QString pqCustomPlotSettingsModel::getUnit(const QString& seriesName)
00203 {
00204   QString unit("");
00205   
00206   int index1 = seriesName.lastIndexOf("]");
00207   if (index1 == seriesName.size() - 1)
00208     {
00209       int index2 = seriesName.lastIndexOf(" [");
00210       if (index2 > 0)
00211        {
00212          int start = index2 + 2;
00213          unit = seriesName.mid(index2 + 2, index1 - start);
00214        }
00215     }
00216 
00217   return unit;
00218 }