VTK 觀察者&命令模式: vtkCallbackCommand

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#include <vtkSmartPointer.h>
class vtkRenderer;
class vtkImageViewer2;
class vtkObject;
class vtkCallbackCommand;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public:
    void updateMousePosInfo(const QString& mousePosInfo);
private slots:
    void openFileSlot();
private:
    // 注意必須爲靜態函數
    static void updateCoords(vtkObject* obj, unsigned long eventId, void * clientData, void * callbackData);

private:
    vtkSmartPointer<vtkRenderer>     pRenderer;
    vtkSmartPointer<vtkImageViewer2> pImageViewer;
    vtkSmartPointer<vtkCallbackCommand> pMouseCallback;
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVTKWidget.h>
#include <vtkImageViewer2.h>
#include <vtkJPEGReader.h>
#include <vtkPNGReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkCallbackCommand.h>
#include <QFileDialog>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    pImageViewer = vtkSmartPointer<vtkImageViewer2>::New();
    pRenderer     = vtkSmartPointer<vtkRenderer>::New();
    ui->qvtkWidget->GetRenderWindow()->AddRenderer(pRenderer);

    pMouseCallback = vtkSmartPointer<vtkCallbackCommand>::New();
    pMouseCallback->SetClientData(this);
    pMouseCallback->SetCallback(MainWindow::updateCoords);

    ui->qvtkWidget->GetInteractor()->AddObserver(vtkCommand::LeftButtonPressEvent, pMouseCallback);

    qDebug() << "clientData A = " << (unsigned long long)(this);
    qDebug() << "ui->qvtkWidget->GetInteractor() = " << (unsigned long long)(ui->qvtkWidget->GetInteractor());
    connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(openFileSlot()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::updateMousePosInfo(const QString& mousePosInfo)
{
    ui->textBrowser->append(mousePosInfo);
}

void MainWindow::openFileSlot()
{
#if 1
    QString selectFilePath = QFileDialog::getOpenFileName(this, QString("選擇圖像文件"), QString(""), QString("圖像(*.jpeg)"));
#else
    QString selectFilePath = QFileDialog::getOpenFileName(this, QString("選擇圖像文件"), QString(""), QString("圖像(*.png)"));
#endif
    if(selectFilePath.isEmpty())
    {
        ui->textBrowser->append("選擇圖像路徑爲空!");
        return ;
    }

#if 1
    vtkSmartPointer<vtkJPEGReader> jpegReader =  vtkSmartPointer<vtkJPEGReader>::New();
#else
    vtkSmartPointer<vtkPNGReader> jpegReader =  vtkSmartPointer<vtkPNGReader>::New();
#endif
    jpegReader->SetFileName(selectFilePath.toStdString().c_str());

    //
#if 1
    // 不調用update時,調用GetOutput()函數沒有數據
    jpegReader->Update();
    pImageViewer->SetInputData(jpegReader->GetOutput());
#else
    pImageViewer->SetInputConnection(jpegReader->GetOutputPort());
#endif
    pImageViewer->UpdateDisplayExtent();
    pImageViewer->SetRenderWindow(ui->qvtkWidget->GetRenderWindow());
    pImageViewer->SetRenderer(pRenderer);
    pImageViewer->SetupInteractor(ui->qvtkWidget->GetInteractor());

    pImageViewer->SetSliceOrientationToXY();
    pImageViewer->GetImageActor()->InterpolateOn();

    pRenderer->ResetCamera();
    pRenderer->DrawOn();
    ui->qvtkWidget->GetRenderWindow()->Render();

    ui->textBrowser->append(QString("載入圖像:") + selectFilePath + QString(" 成功 !"));
}

void MainWindow::updateCoords(vtkObject* obj, unsigned long eventId, void *clientData, void *callbackData)
{
    qDebug() << "obj           = " << (unsigned long long)(obj);
    qDebug() << "eventId       = " << eventId;
    qDebug() << "callbackData  = " << (unsigned long long)(callbackData);
    qDebug() << "clientData    = " << (unsigned long long)(clientData);

    vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::SafeDownCast(obj);

    if(iren == nullptr)
    {
        return ;
    }

    int event_pos[2];
    iren->GetEventPosition(event_pos);

    MainWindow* pMyMainWindow = static_cast<MainWindow*>(clientData);

    QString str;
    str.sprintf("鼠標座標: x=%d : y=%d", event_pos[0], event_pos[1]);
    pMyMainWindow->updateMousePosInfo(str);
}

QT UI 文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QHBoxLayout" name="horizontalLayout" stretch="2,5">
    <item>
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
       <widget class="QPushButton" name="pushButton">
        <property name="text">
         <string>選擇圖像</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QTextBrowser" name="textBrowser"/>
      </item>
     </layout>
    </item>
    <item>
     <widget class="QVTKWidget" name="qvtkWidget"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>QVTKWidget</class>
   <extends>QWidget</extends>
   <header>QVTKWidget.h</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章