今天又来搬运代码了,这周是入职的第三周了,领导给安排的任务,后面要开始搞任务了。今天搬运的代码为官方实例MuliView。
文章目录
#ifndef MULTIVIEW_H #define MULTIVIEW_H #include <QWidget> #include "QVTKOpenGWidget.h" //新版本,旧版QVTKWidget #include "vtkAutoInit.h" #include "vtkAnnotationLink.h" #include "vtkCommand.h" #include "vtkDataRepresentation.h" #include "vtkDataSetAttributes.h" #include "vtkGraphLayoutView.h" #include "vtkMutableDirectedGraph.h" #include "vtkRandomGraphSource.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkStringArray.h" #include "vtkTree.h" #include "vtkViewTheme.h" #include "vector" class ViewUpdater :public vtkCommand { public: static ViewUpdater* New() { return new ViewUpdater; } void AddView(vtkView *view) { this->Views.push_back(view); view->AddObserver(vtkCommand::SelectionChangedEvent,this); } void Execute(vtkObject*,unsigned long,void*)override { for(unsigned int i = 0;i<this->Views.size();i++) { this->Views[i]->Update(); } } private: ViewUpdater() = default; ~ViewUpdater() override = default; std::vector<vtkView*> Views; }; namespace Ui { class MultiView; } class MultiView : public QWidget { Q_OBJECT public: explicit MultiView(QWidget *parent = 0); ~MultiView(); private: Ui::MultiView *ui; vtkMutableDirectedGraph *graph = nullptr; vtkStringArray* labels = nullptr; vtkTree* tree = nullptr; vtkGraphLayoutView* view = nullptr; vtkGraphLayoutView* view2 = nullptr; vtkAnnotationLink* link = nullptr; ViewUpdater* update = nullptr; }; #endif // MULTIVIEW_H
2.2 multiview.cpp
#include "multiview.h" #include "ui_multiview.h" MultiView::MultiView(QWidget *parent) : QWidget(parent), ui(new Ui::MultiView) { ui->setupUi(this); graph = vtkMutableDirectedGraph::New(); vtkIdType a = graph->AddVertex(); vtkIdType b = graph->AddChild(a); vtkIdType c = graph->AddChild(a); vtkIdType d = graph->AddChild(b); vtkIdType e = graph->AddChild(c); vtkIdType f = graph->AddChild(c); labels = vtkStringArray::New(); labels->SetName("Label"); labels->InsertValue(a, "a"); labels->InsertValue(b, "b"); labels->InsertValue(c, "c"); labels->InsertValue(d, "d"); labels->InsertValue(e, "e"); labels->InsertValue(f, "f"); graph->GetVertexData()->AddArray(labels); tree = vtkTree::New(); bool validTree = tree->CheckedShallowCopy(graph); if (!validTree) { std::cout << "ERROR: Invalid tree" << std::endl; graph->Delete(); labels->Delete(); tree->Delete(); } view = vtkGraphLayoutView::New(); view->SetRenderWindow(ui->widget->GetRenderWindow()); vtkDataRepresentation* rep =view->SetRepresentationFromInput(tree); vtkViewTheme* theme = vtkViewTheme::CreateMellowTheme(); view->ApplyViewTheme(theme); view->SetVertexColorArrayName("VertexDegree"); view->SetColorVertices(true); view->SetVertexLabelArrayName("Label"); view->SetVertexLabelVisibility(true); view2 = vtkGraphLayoutView::New(); vtkDataRepresentation* rep2 =view2->SetRepresentationFromInput(tree); view2->SetVertexLabelArrayName("Label"); view2->SetVertexLabelVisibility(true); link = vtkAnnotationLink::New(); rep->SetAnnotationLink(link); rep2->SetAnnotationLink(link); update = ViewUpdater::New(); update->AddView(view); update->AddView(view2); } MultiView::~MultiView() { delete ui; }