LidarView源码分析(四)pqReaction

LidarView中菜单,工具栏中大量使用了pqReaction的子类。

ParaView客户端依赖于一些列响应机制,这些响应机制是使用pqApplicationCore和其他核心组件来完成工作的自动化对象。要使用该响应机制,只需将pqReaction子类的一个实例附加到QAction上。然后,响应机制监控来自QAction的事件。此外,相应机制可以监控ParaView应用程序状态,以更新启用状态、标签等内容。对于QAction本身。

pqReaction是相应机制的主要基类,pqReaction的基类为QObject,即其是Qt框架下的子类,可以使用Qt的一系列功能,比如内存管理、信号槽。

pqReaction是ParaView中实现的类,其源码在ParaView源码中的Qt/ApplicationComponents文件夹中。

其头文件代码如下:

/*=========================================================================

   Program: ParaView
   Module:    pqReaction.h

   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.

========================================================================*/
#ifndef pqReaction_h
#define pqReaction_h

#include "pqApplicationComponentsModule.h"
#include <QAction>
#include <QObject>

/**
 * @defgroup Reactions ParaView Reactions
 * ParaView client relies on a collection of reactions that are autonomous
 * entities that use pqApplicationCore and other core components to get their
 * work done. To use, simply attach an instance of pqReaction subclass to a
 * QAction. The reaction then monitors events from the QAction. Additionally, the
 * reaction can monitor the ParaView application state to update things like
 * enable state, label, etc. for the QAction itself.
 */

/**
 * @ingroup Reactions
 * This is a superclass just to make it easier to collect all such reactions.
 */
class PQAPPLICATIONCOMPONENTS_EXPORT pqReaction : public QObject
{
  Q_OBJECT
  typedef QObject Superclass;

public:
  /**
   * Constructor. Parent cannot be nullptr.
   */
  pqReaction(QAction* parent, Qt::ConnectionType type = Qt::AutoConnection);
  ~pqReaction() override;

  /**
   * Provides access to the parent action.
   */
  QAction* parentAction() const { return qobject_cast<QAction*>(this->parent()); }

protected Q_SLOTS:
  /**
   * Called when the action is triggered.
   */
  virtual void onTriggered() {}

  virtual void updateEnableState() {}
  virtual void updateMasterEnableState(bool);

protected: // NOLINT(readability-redundant-access-specifiers)
  bool IsMaster;

private:
  Q_DISABLE_COPY(pqReaction)
};

#endif

其中有三个虚函数,分别为onTriggered、updateEnableState、updateMasterEnableState。

其中onTriggered在构造函数中与其相关联的QAction的triggered信号连接。

updateMasterEnableState与pqApplicationCore的updateMasterEnableState信号连接。同时该函数体中设置了this->IsMaster变量,并调用了updateEnableState函数。

pqReaction源码如下:

/*=========================================================================

   Program: ParaView
   Module:    pqReaction.cxx

   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 "pqReaction.h"

#include "pqApplicationCore.h"
#include "pqServer.h"

#include <cassert>

//-----------------------------------------------------------------------------
pqReaction::pqReaction(QAction* parentObject, Qt::ConnectionType type)
  : Superclass(parentObject)
{
  assert(parentObject != nullptr);

  QObject::connect(parentObject, SIGNAL(triggered(bool)), this, SLOT(onTriggered()), type);

  // Deal with master/slave enable/disable
  QObject::connect(pqApplicationCore::instance(), SIGNAL(updateMasterEnableState(bool)), this,
    SLOT(updateMasterEnableState(bool)));

  this->IsMaster = true;
}

//-----------------------------------------------------------------------------
pqReaction::~pqReaction() = default;

//-----------------------------------------------------------------------------
void pqReaction::updateMasterEnableState(bool isMaster)
{
  this->IsMaster = isMaster;
  updateEnableState();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值