File: main.cpp

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 205,916 kB
  • sloc: cpp: 2,336,565; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 178; javascript: 165; objc: 153; tcl: 59
file content (225 lines) | stat: -rw-r--r-- 7,033 bytes parent folder | download | duplicates (2)
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
74
75
76
77
78
79
80
81
82
83
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
156
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
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlContext>

#include <QtQuick/QQuickWindow>

#include <QtGui/QGuiApplication>

#include <QtCore/QPointer>
#include <QtCore/QScopedPointer>

#include <QQuickVTKItem.h>
#include <QVTKRenderWindowAdapter.h>

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkConeSource.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkRendererCollection.h>
#include <vtkSphereSource.h>

struct Presenter : QObject
{
  Q_OBJECT
  Q_PROPERTY(QStringList sources READ sources CONSTANT)
  QStringList sources() const
  {
    return QStringList{} << "Cone"
                         << "Sphere"
                         << "Capsule";
  }
};

struct MyVtkItem : QQuickVTKItem
{
  Q_OBJECT
public:
  MyVtkItem()
  {
    connect(this, &QQuickItem::widthChanged, this, &MyVtkItem::resetCamera);
    connect(this, &QQuickItem::heightChanged, this, &MyVtkItem::resetCamera);
  }

  struct Data : vtkObject
  {
    static Data* New();
    vtkTypeMacro(Data, vtkObject);

    vtkNew<vtkActor> actor;
    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkConeSource> cone;
    vtkNew<vtkSphereSource> sphere;
    vtkNew<vtkCylinderSource> capsule;
    vtkNew<vtkPolyDataMapper> mapper;
  };

  vtkUserData initializeVTK(vtkRenderWindow* renderWindow) override
  {
    vtkNew<Data> vtk;

    vtk->capsule->SetCapping(true);
    vtk->capsule->SetCapsuleCap(true);

    vtk->actor->SetMapper(vtk->mapper);

    vtk->renderer->AddActor(vtk->actor);
    vtk->renderer->SetBackground(0.5, 0.5, 0.7);
    vtk->renderer->SetBackground2(0.7, 0.7, 0.7);
    vtk->renderer->SetGradientBackground(true);

    renderWindow->AddRenderer(vtk->renderer);

    // Remember: QML can delete our underlying QSGNode (which calls this method) at any time.
    // We have to re-synchronize our Qt properties with our VTK properties at any time.
    // To this end we've added a "force" parameter to our Qt property setter which is set true
    // here in initializeVtk but is defaulted false whenever QML (or other C++ code) invokes it.
    //
    // To see QML randomly delete our QSGNode, split horizontally, then split vertically and then
    // unsplit the smallest top-most view and observe the console output.
    //
    // You'll see something like:
    //
    // clang-format off
    //qml: constructed ItemDelegate(0x1f8d43946a0, "viewBase 0") SplitView_QMLTYPE_1_QML_5(0x1f8d4395660, "splitView 0")
    //qml: constructed ItemDelegate(0x1f8d4395780, "itemBase 0")
    //qml: constructed ItemDelegate(0x1f8d4395c00, "itemBase 1")
    //qml: constructed ItemDelegate(0x1f8d4394e80, "viewBase 1") SplitView_QMLTYPE_1_QML_5(0x1f8d4394fa0, "splitView 1")
    //qml: constructed ItemDelegate(0x1f8d43950c0, "itemBase 2")
    //qml: destructed ItemDelegate(0x1f8d4395780, "itemBase 0") SplitView_QMLTYPE_1_QML_5(0x1f8d4394fa0, "splitView 1")
    //qml: destructed ItemDelegate(0x1f8d4394e80, "viewBase 1") SplitView_QMLTYPE_1_QML_5(0x1f8d4395660, "splitView 0")
    // clang-format on
    //
    // Notice that there are 2 (two) 'destructed' messages but you only unsplit once!!
    // QML deleted both "small" QSGNodes and then created a new QSGNode to fill the empty column.
    setSource(_source, true);

    // Note:  It is okay to store some non-graphical VTK objects in the QQuickVTKItem instead of the
    // vtkUserData but ONLY if they are accessed from the qml-render-thread. (i.e. only in the
    // initializeVTK, destroyingVTK or dispatch_async methods)
    vtk->renderer->GetActiveCamera()->DeepCopy(_camera);

    return vtk;
  }
  void destroyingVTK(vtkRenderWindow* renderWindow, vtkUserData userData) override
  {
    auto* vtk = Data::SafeDownCast(userData);
    _camera->DeepCopy(vtk->renderer->GetActiveCamera());
  }
  vtkNew<vtkCamera> _camera;

  void resetCamera()
  {
    dispatch_async(
      [this](vtkRenderWindow* renderWindow, vtkUserData userData)
      {
        auto* vtk = Data::SafeDownCast(userData);
        vtk->renderer->ResetCamera();
        scheduleRender();
      });
  }

  Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)
  QString source() const { return _source; }
  void setSource(QString v, bool forceVtk = false)
  {
    if (_source != v)
      emit sourceChanged((forceVtk = true, _source = v));

    if (forceVtk)
      dispatch_async(
        [this](vtkRenderWindow* renderWindow, vtkUserData userData)
        {
          auto* vtk = Data::SafeDownCast(userData);
          // clang-format off
          vtk->mapper->SetInputConnection(
                _source == "Cone"    ? vtk->cone->GetOutputPort()
              : _source == "Sphere"  ? vtk->sphere->GetOutputPort()
              : _source == "Capsule" ? vtk->capsule->GetOutputPort()
              : (qWarning() << Q_FUNC_INFO << "YIKES!! Unknown source:'" << _source << "'", nullptr));
          // clang-format on

          resetCamera();
        });
  }
  Q_SIGNAL void sourceChanged(QString);
  QString _source;

  bool event(QEvent* ev) override
  {
    switch (ev->type())
    {
      case QEvent::MouseButtonPress:
      {
        auto e = static_cast<QMouseEvent*>(ev);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        _click.reset(new QMouseEvent(e->type(), e->localPos(), e->windowPos(), e->screenPos(),
          e->button(), e->buttons(), e->modifiers(), e->source()));
#else
        _click.reset(e->clone());
#endif
        break;
      }
      case QEvent::MouseMove:
      {
        if (!_click)
          return QQuickVTKItem::event(ev);

        auto e = static_cast<QMouseEvent*>(ev);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        if ((_click->pos() - e->pos()).manhattanLength() > 5)
#else
        if ((_click->position() - e->position()).manhattanLength() > 5)
#endif
        {
          QQuickVTKItem::event(QScopedPointer<QMouseEvent>(_click.take()).get());
          return QQuickVTKItem::event(e);
        }
        break;
      }
      case QEvent::MouseButtonRelease:
      {
        if (!_click)
          return QQuickVTKItem::event(ev);
        else
          emit clicked();
        break;
      }
      default:
        break;
    }
    ev->accept();
    return true;
  }
  QScopedPointer<QMouseEvent> _click;
  Q_SIGNAL void clicked();
};
vtkStandardNewMacro(MyVtkItem::Data);

int main(int argc, char* argv[])
{
  QQuickVTKItem::setGraphicsApi();

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

  QGuiApplication app(argc, argv);
  Presenter presenter;

  qmlRegisterType<MyVtkItem>("com.vtk.example", 1, 0, "MyVtkItem");
  qmlRegisterUncreatableType<Presenter>("com.vtk.example", 1, 0, "Presenter", "!!");

  QQmlApplicationEngine engine;
  engine.rootContext()->setContextProperty("presenter", &presenter);
  engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  if (engine.rootObjects().isEmpty())
    return -1;

  return app.exec();
}

#include "main.moc"