ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
modeltovtktableconverter.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <vtkTable.h>
11 
12 #include <QDebug>
13 
14 #include "tablemodel.h"
15 #include "utils.h"
16 #include "vtkutils.h"
17 
18 namespace VtkUtils {
20  : m_model(model) {}
21 
22 void ModelToVtkTableConverter::setLabels(const QStringList& labels) {
23  m_labels = labels;
24 }
25 
26 QStringList ModelToVtkTableConverter::labels() const { return m_labels; }
27 
29  if (!m_model) {
30  qDebug() << "ModelToVtkTableConverter::run : null model.";
31  emit finished();
32  return;
33  }
34 
35  vtkInitOnce(&m_table);
36  m_table->Initialize();
37 
38  int rows = m_model->rowCount();
39  int cols = m_model->columnCount();
40 
41  QString columnName;
42  for (int col = 0; col < cols; ++col) {
43  columnName = m_labels.size() > col ? m_labels.at(col)
44  : QString("column#%1").arg(col + 1);
45  vtkSmartPointer<vtkDoubleArray> arr = vtkDoubleArray::New();
46  arr->SetName(columnName.toUtf8().data());
47  m_table->AddColumn(arr);
48  }
49  // the order matters , first you add columns then you set the rows of
50  // columns
51  m_table->SetNumberOfRows(rows);
52 
53  for (int r = 0; r < rows; ++r)
54  for (int c = 0; c < cols; ++c)
55  m_table->SetValue(r, c, m_model->data(r, c));
56 
57  emit finished();
58 }
59 
60 vtkTable* ModelToVtkTableConverter::table() const { return m_table; }
61 
62 } // namespace VtkUtils
void setLabels(const QStringList &labels)
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: tablemodel.cpp:187
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: tablemodel.cpp:182
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: tablemodel.cpp:213
void vtkInitOnce(T **obj)
Definition: vtkutils.h:44