pqIntegrationModelSurfaceHelperWidget.cxx 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/*=========================================================================

    Program: ParaView
    Module:    $RCSfile$

    Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
    All rights reserved.

    ParaView is a free software; you can redistribute it and/or modify it
    under the terms of the ParaView license version 1.2.

    See License_v1.2.txt for the full ParaView license.
    A copy of this license can be obtained by contacting
    Kitware Inc.
    28 Corporate Drive
    Clifton Park, NY 12065
    USA

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

========================================================================*/
#include "pqIntegrationModelSurfaceHelperWidget.h"

#include "vtkLagrangianSurfaceHelper.h"
#include "vtkPVCompositeDataInformation.h"
#include "vtkPVDataInformation.h"
#include "vtkSMDoubleVectorProperty.h"
#include "vtkSMInputProperty.h"
#include "vtkSMIntVectorProperty.h"
#include "vtkSMProxyProperty.h"
#include "vtkSMSourceProxy.h"
#include "vtkSMStringVectorProperty.h"
#include "vtkStringArray.h"

#include "pqExpandableTableView.h"

#include <QComboBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QHeaderView>
#include <QPushButton>
#include <QStandardItemModel>

#include <sstream>

//-----------------------------------------------------------------------------
pqIntegrationModelSurfaceHelperWidget::pqIntegrationModelSurfaceHelperWidget(
  vtkSMProxy* smproxy, vtkSMProperty* smproperty, QWidget* parentObject)
  : Superclass(smproxy, smproperty, parentObject)
{
  this->resetSurfaceWidget(true);
  this->addPropertyLink(this, "arrayToGenerate", SIGNAL(arrayToGenerateChanged()), smproperty);
}

//-----------------------------------------------------------------------------
void pqIntegrationModelSurfaceHelperWidget::resetWidget()
{
  this->resetSurfaceWidget(false);
}

//-----------------------------------------------------------------------------
void pqIntegrationModelSurfaceHelperWidget::resetSurfaceWidget(bool force)
{
luz paz's avatar
luz paz committed
74
  // Avoid resetting when it is not necessary
75 76 77 78 79 80 81 82
  if (force || this->ModelProperty->GetUncheckedProxy(0) != this->ModelPropertyValue)
  {
    // Recover the current unchecked value, ie the value selected by the user but not yet applied
    this->ModelPropertyValue = this->ModelProperty->GetUncheckedProxy(0);

    // Remove all previous layout and child widget
    qDeleteAll(this->children());

83
    if (this->ModelPropertyValue)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    {
      // Force push the model proxy property to the server
      this->ModelProperty->SetProxy(0, this->ModelPropertyValue);
      this->proxy()->UpdateProperty("IntegrationModel", true);

      // Rexecute RequestInformation pass for the surface helper to update the surface datasets
      vtkSMSourceProxy::SafeDownCast(this->proxy())->UpdatePipelineInformation();

      // Update surface default values
      this->ModelPropertyValue->UpdatePropertyInformation();

      // Recover model array names and components
      vtkSMStringVectorProperty* namesProp = vtkSMStringVectorProperty::SafeDownCast(
        this->ModelPropertyValue->GetProperty("SurfaceArrayNames"));
      vtkSMIntVectorProperty* compsProp = vtkSMIntVectorProperty::SafeDownCast(
        this->ModelPropertyValue->GetProperty("SurfaceArrayComps"));
      vtkSMStringVectorProperty* enumsProp = vtkSMStringVectorProperty::SafeDownCast(
        this->ModelPropertyValue->GetProperty("SurfaceArrayEnumValues"));
      vtkSMDoubleVectorProperty* defaultValuesProp = vtkSMDoubleVectorProperty::SafeDownCast(
        this->ModelPropertyValue->GetProperty("SurfaceArrayDefaultValues"));
      vtkSMIntVectorProperty* typesProp = vtkSMIntVectorProperty::SafeDownCast(
        this->ModelPropertyValue->GetProperty("SurfaceArrayTypes"));

      // Recover input and associated flat leaf names
      vtkSMInputProperty* inputProperty =
        vtkSMInputProperty::SafeDownCast(this->proxy()->GetProperty("SurfaceInput"));
      if (!inputProperty)
      {
        qCritical() << "No Input Property defined, cannot reset widget";
        return;
      }
      vtkSMSourceProxy* input = vtkSMSourceProxy::SafeDownCast(inputProperty->GetProxy(0));
      if (!input)
      {
        qCritical() << "No Input defined, cannot reset widget";
        return;
      }

      vtkNew<vtkStringArray> leafNames;
      pqIntegrationModelSurfaceHelperWidget::fillLeafNames(
        input->GetDataInformation(), "", leafNames.Get());
      unsigned int nLeafs = leafNames->GetNumberOfValues();

      /// Check number of components
      unsigned int nArrays = namesProp->GetNumberOfElements();
      if (nArrays != compsProp->GetNumberOfElements())
      {
        qCritical() << "not the same number of names and components: "
                    << namesProp->GetNumberOfElements()
                    << " != " << compsProp->GetNumberOfElements()
                    << ", array generation may be invalid";
      }

      // Create main grid layout
      QGridLayout* gridLayout = new QGridLayout(this);

      unsigned int enumIndex = 0;
      unsigned int defaultValuesIndex = 0;
      for (unsigned int i = 0; i < nArrays; i++)
      {
        const char* arrayName = namesProp->GetElement(i);
        const char* labelName = vtkSMProperty::CreateNewPrettyLabel(arrayName);
        int nComponents = compsProp->GetElement(i);
        int type = typesProp->GetElement(i);

        // Create a group box for each array
        QGroupBox* gb = new QGroupBox(labelName, this);
        gb->setCheckable(true);
        gb->setChecked(false);
        gb->setProperty("name", arrayName);
        gb->setProperty("type", type);
        QObject::connect(gb, SIGNAL(toggled(bool)), this, SIGNAL(arrayToGenerateChanged()));
156
        delete[] labelName;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

        // And associated layout
        QGridLayout* gbLayout = new QGridLayout(gb);
        gb->setLayout(gbLayout);

        // Create a pqExpandableView in groupbox
        pqExpandableTableView* table = new pqExpandableTableView(this);
        QStandardItemModel* model = new QStandardItemModel(nLeafs, nComponents);
        table->setModel(model);
        table->horizontalHeader()->setHighlightSections(false);
#if QT_VERSION >= 0x050000
        table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
#else
        table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
#endif
        table->horizontalHeader()->setStretchLastSection(true);
        table->horizontalHeader()->hide();
        QObject::connect(
          model, SIGNAL(itemChanged(QStandardItem*)), this, SIGNAL(arrayToGenerateChanged()));

        // Enum if necessary
        int nbOfEnum = QString(enumsProp->GetElement(enumIndex)).toInt();
        enumIndex++;

        // Add a row for each leaf
        for (unsigned int j = 0; j < nLeafs; j++)
        {
          QStandardItem* item;
          item = new QStandardItem(QString(leafNames->GetValue(j)));
          item->setFlags(item->flags() & ~Qt::ItemIsEditable); // non editable
          model->setVerticalHeaderItem(j, item);

          // Add a column for each components
          for (int k = 0; k < nComponents; k++)
          {
            item = new QStandardItem();
            model->setItem(j, k, item);
            double defaultValue = 0;
            if (defaultValuesIndex < defaultValuesProp->GetNumberOfElements())
            {
              defaultValue = defaultValuesProp->GetElement(defaultValuesIndex);
              defaultValuesIndex++;
            }
            if (nbOfEnum > 0)
            {
              QModelIndex modelIndex = model->indexFromItem(item);
              QComboBox* cmb = new QComboBox;
              QObject::connect(
                cmb, SIGNAL(currentIndexChanged(int)), this, SIGNAL(arrayToGenerateChanged()));
              cmb->addItem("None", "None");
              int defaultIndex = 0;
              for (int enumIt = 0; enumIt < nbOfEnum; enumIt++)
              {
                int enumVal = QString(enumsProp->GetElement(enumIndex + 2 * enumIt)).toInt();
                QString enumString = QString(enumsProp->GetElement(enumIndex + 2 * enumIt + 1));
                cmb->addItem(enumString, enumVal);
                if (enumVal == defaultValue)
                {
                  defaultIndex = enumIt + 1;
                }
              }
              cmb->setCurrentIndex(defaultIndex);
              table->setIndexWidget(modelIndex, cmb);
            }
            else
            {
              item->setText(QString::number(defaultValue));
            }
          }
        }
        enumIndex += 2 * nbOfEnum;
        gbLayout->addWidget(table, 0, 0);
        gridLayout->addWidget(gb, i, 0);
      }
    }
    emit(this->arrayToGenerateChanged());
  }
}

//-----------------------------------------------------------------------------
vtkStringArray* pqIntegrationModelSurfaceHelperWidget::fillLeafNames(
  vtkPVDataInformation* info, QString baseName, vtkStringArray* names)
{
  vtkPVCompositeDataInformation* cinfo = info->GetCompositeDataInformation();
  if (cinfo)
  {
    if (!cinfo->GetDataIsComposite())
    {
      names->InsertNextValue("Single DataSet");
    }
    else
    {
      // For each child
      for (unsigned int i = 0; i < cinfo->GetNumberOfChildren(); i++)
      {
        vtkPVDataInformation* tmpInfo = cinfo->GetDataInformation(i);
        if (tmpInfo)
        {
          vtkPVCompositeDataInformation* tmpCinfo = tmpInfo->GetCompositeDataInformation();
          if (tmpCinfo->GetDataIsComposite() && !tmpCinfo->GetDataIsMultiPiece())
          {
            // Recursive call if the child is multiblock
259 260
            pqIntegrationModelSurfaceHelperWidget::fillLeafNames(
              tmpInfo, baseName + QString(cinfo->GetName(i)) + QString("/"), names);
261 262 263 264 265
          }
          else
          {
            // Recover name if the child is not
            names->InsertNextValue((QString::number(names->GetNumberOfValues()) + QString(":") +
266 267
                                     baseName + cinfo->GetName(i))
                                     .toStdString());
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
          }
        }
      }
    }
  }
  return names;
}

//-----------------------------------------------------------------------------
QList<QVariant> pqIntegrationModelSurfaceHelperWidget::arrayToGenerate() const
{
  QList<QVariant> values;
  // For each group box
  foreach (QGroupBox* gb, this->findChildren<QGroupBox*>())
  {
    if (gb->isChecked())
    {
      QTableView* table = gb->findChild<QTableView*>();
      if (table)
      {
        QStandardItemModel* model = qobject_cast<QStandardItemModel*>(table->model());
        if (model)
        {
          unsigned int nLeafs = model->rowCount();
          unsigned int nComponents = model->columnCount();

          // Serial Fill up of the property
          values.push_back(gb->property("name"));
          values.push_back(gb->property("type"));
          values.push_back(nLeafs);
          values.push_back(nComponents);
          std::ostringstream str;
          for (unsigned int i = 0; i < nLeafs; i++)
          {
            for (unsigned int j = 0; j < nComponents; j++)
            {
              QComboBox* cmb = qobject_cast<QComboBox*>(table->indexWidget(model->index(i, j)));
305
              if (cmb)
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
              {
                str << cmb->itemData(cmb->currentIndex()).toString().toLatin1().data() << ";";
              }
              else
              {
                str << model->item(i, j)->text().toLatin1().data() << ";";
              }
            }
          }
          values.push_back(str.str().c_str());
        }
      }
    }
  }
  return values;
}

//-----------------------------------------------------------------------------
void pqIntegrationModelSurfaceHelperWidget::setArrayToGenerate(const QList<QVariant>& values)
{
  QGroupBox* gb;
  QList<QGroupBox*> gbs = this->findChildren<QGroupBox*>();
  foreach (gb, gbs)
  {
    gb->setChecked(false);
  }

333
  for (QList<QVariant>::const_iterator i = values.begin(); i != values.end(); i += 5)
334 335 336 337 338 339 340 341 342 343 344
  {
    // Incremental python filling value check
    // When using python, this method is called incrementally,
    // this allows to wait for the correct call
    if ((i + 4)->toString().isEmpty())
    {
      continue;
    }

    QString arrayName = i->toString();
    int type = (i + 1)->toInt();
345
    gb = nullptr;
346 347 348 349 350 351 352
    foreach (gb, gbs)
    {
      if (arrayName == gb->property("name"))
      {
        break;
      }
    }
353
    if (!gb)
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    {
      qCritical() << "Could not find group box with name" << arrayName;
      continue;
    }
    if (gb->property("type") != type)
    {
      qCritical() << "Dynamic array typing is not supported, type is ignored"
                  << gb->property("type") << " " << type;
    }
    QTableView* table = gb->findChild<QTableView*>();
    if (!table)
    {
      continue;
    }
    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(table->model());
    if (!model)
    {
      continue;
    }
    int nLeafs = (i + 2)->toInt();
    int nComponents = (i + 3)->toInt();
    QStringList dataValues = (i + 4)->toString().split(';', QString::SkipEmptyParts);
    if (dataValues.size() != nLeafs * nComponents)
    {
      qCritical() << "Unexpected number of values" << dataValues.size() << " "
                  << nLeafs * nComponents;
      continue;
    }
    gb->setChecked(true);
    for (int j = 0; j < nLeafs; j++)
    {
      for (int k = 0; k < nComponents; k++)
      {
        QComboBox* cmb = qobject_cast<QComboBox*>(table->indexWidget(model->index(j, k)));
388
        if (cmb)
389 390 391 392 393 394 395 396 397 398 399
        {
          cmb->setCurrentIndex(cmb->findData(dataValues[j * nComponents + k]));
        }
        else
        {
          model->item(j, k)->setText(dataValues[j * nComponents + k]);
        }
      }
    }
  }
}