Hide menu
visualization/qtquick_qml/manipulator/main.cxx

Refer to Manipulator Example

ManipulatorApplication.hxx

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2022, CADEX. All rights reserved.
//
// This file is part of the CAD Exchanger software.
//
// You may use this file under the terms of the BSD license as follows:
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 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 COPYRIGHT HOLDER 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 __ManipulatorApplication_HeaderFile
#define __ManipulatorApplication_HeaderFile
#include "../baseviewer/BaseViewerApplication.hxx"
#include <cadex/ModelPrs_ManipulatorObserver.hxx>
#include <cadex/ModelPrs_ManipulatorMode.hxx>
#include <cadex/ModelPrs_SelectionChangesObserver.hxx>
namespace cadex {
class ModelPrs_Selection;
class ModelPrs_SceneNode;
}
class ManipulatorApplication;
class SelectionChangesObserver : public cadex::ModelPrs_SelectionChangesObserver
{
public:
SelectionChangesObserver (ManipulatorApplication& theApplication);
const cadex::ModelPrs_Selection& theSelected,
const cadex::ModelPrs_Selection& theDeselected) override;
const cadex::ModelPrs_Selection& /*theSelected*/,
const cadex::ModelPrs_Selection& /*theDeselected*/) override { }
private:
ManipulatorApplication& myApplication;
};
class ManipulatorObserver : public cadex::ModelPrs_ManipulatorObserver
{
public:
ManipulatorObserver (ManipulatorApplication& theApplication);
void ModeChanged (cadex::ModelPrs_ManipulatorMode theMode, const cadex::ModelData_Direction& theDirection) override;
void StartTransformation() override;
void Transformed (const cadex::ModelData_Transformation& theLocalTrsf,
const cadex::ModelData_Transformation& theGlobalTrsf,
double theValue) override;
void StopTransformation() override;
private:
ManipulatorApplication& myApplication;
};
class ManipulatorApplication : public BaseViewerApplication
{
Q_OBJECT
public:
ManipulatorApplication();
void OnAttachedChanged (bool theValue);
void OnSelectionChanged (const cadex::ModelPrs_SceneNode& theNode);
void OnTransformationChanged (double theValue);
void OnTransformationModeChanged (cadex::ModelPrs_ManipulatorMode theMode);
void OnTransformationActiveChanged (bool theValue);
public slots:
void onInitialized();
void onResetTransformation();
void onManipulatorModeChanged (const QVariant& theMode);
protected:
void Clear() override;
protected:
ManipulatorObserver myManipulatorObserver;
SelectionChangesObserver mySelectionObserver;
};
#endif // __ManipulatorApplication_HeaderFile

ManipulatorApplication.cxx

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2022, CADEX. All rights reserved.
//
// This file is part of the CAD Exchanger software.
//
// You may use this file under the terms of the BSD license as follows:
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 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 COPYRIGHT HOLDER 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 "ManipulatorApplication.hxx"
#include <cadex/ModelPrsQtQuick_ViewPort.hxx>
#include <cadex/ModelPrs_Manipulator.hxx>
#include <cadex/ModelPrs_SceneNode.hxx>
#include <cadex/ModelPrs_Selection.hxx>
#include <cadex/ModelPrs_SelectionVisitor.hxx>
#include <cadex/ModelPrs_SelectionManager.hxx>
#include <QtQuick/QQuickWindow>
using namespace cadex;
namespace {
class SelectionVisitor : public ModelPrs_SelectionVisitor
{
public:
void Visit (const ModelPrs_SceneNode& theNode) override
{
mySceneNode = theNode;
}
void Visit (const ModelPrs_SceneNode& /*theNode*/, const std::vector<ModelData_Shape>& /*theShapes*/) override
{
}
ModelPrs_SceneNode mySceneNode;
};
} // end of unnamed namespace
SelectionChangesObserver::SelectionChangesObserver (ManipulatorApplication& theApplication)
: myApplication (theApplication)
{
}
void SelectionChangesObserver::SelectionChangedByScene (const cadex::ModelPrs_Selection& theCurrent,
const cadex::ModelPrs_Selection& /*theSelected*/,
const cadex::ModelPrs_Selection& /*theDeselected*/)
{
if (theCurrent.IsNull()) {
mySceneNode.Nullify();
} else {
SelectionVisitor aVisitor;
theCurrent.Accept (aVisitor);
mySceneNode = aVisitor.mySceneNode;
}
myApplication.OnSelectionChanged (mySceneNode);
}
ManipulatorObserver::ManipulatorObserver (ManipulatorApplication& theApplication)
: myApplication (theApplication)
{
}
void ManipulatorObserver::StartTransformation()
{
myApplication.OnTransformationActiveChanged (true);
myApplication.OnTransformationChanged (0.);
}
void ManipulatorObserver::ModeChanged (ModelPrs_ManipulatorMode theMode, const ModelData_Direction& /*theDirection*/)
{
myApplication.OnTransformationModeChanged (theMode);
}
void ManipulatorObserver::StopTransformation()
{
myApplication.OnTransformationActiveChanged (false);
}
void ManipulatorObserver::Transformed (const ModelData_Transformation& /*theLocalTrsf*/,
const ModelData_Transformation& /*theGlobalTrsf*/,
double theValue)
{
myApplication.OnTransformationChanged (theValue);
}
ManipulatorApplication::ManipulatorApplication()
: myManipulatorObserver (*this),
mySelectionObserver (*this)
{
myScene.SelectionManager().Register (mySelectionObserver);
myRoot.SetSelectionMode (ModelPrs_SM_Node);
connect (this, &BaseViewerApplication::initialized, this, &ManipulatorApplication::onInitialized);
}
void ManipulatorApplication::OnSelectionChanged (const ModelPrs_SceneNode& theNode)
{
if (theNode) {
myViewPort->Manipulator().AttachTo (theNode);
} else {
myViewPort->Manipulator().Detach();
}
OnAttachedChanged (!theNode.IsNull());
}
void ManipulatorApplication::onResetTransformation()
{
myViewPort->Manipulator().ResetTransformation();
}
void ManipulatorApplication::onManipulatorModeChanged (const QVariant& theMode)
{
auto aMode = theMode.toInt();
auto aManipulatorMode = static_cast<ModelPrs_ManipulatorMode> (aMode);
myViewPort->Manipulator().SetMode (aManipulatorMode);
}
void ManipulatorApplication::onInitialized()
{
connect (myMainWindow, SIGNAL (resetTransformation()),
this, SLOT (onResetTransformation()));
connect (myMainWindow, SIGNAL (manipulatorModeChanged (const QVariant&)),
this, SLOT (onManipulatorModeChanged (const QVariant&)));
myViewPort->Manipulator().Register (myManipulatorObserver);
}
void ManipulatorApplication::OnTransformationModeChanged (cadex::ModelPrs_ManipulatorMode theMode)
{
if (theMode != ModelPrs_MM_None) {
myMainWindow->setProperty ("transformationType", theMode);
}
}
void ManipulatorApplication::OnTransformationChanged (double theValue)
{
myMainWindow->setProperty ("transformationValue", theValue);
}
void ManipulatorApplication::OnTransformationActiveChanged (bool theValue)
{
myMainWindow->setProperty ("transformationActive", theValue);
}
void ManipulatorApplication::OnAttachedChanged (bool theValue)
{
myMainWindow->setProperty ("isAttached", theValue);
if (!theValue) {
myMainWindow->setProperty ("transformationType", ModelPrs_MM_None);
}
}
void ManipulatorApplication::Clear()
{
myViewPort->Manipulator().Detach();
OnAttachedChanged (false);
OnTransformationActiveChanged (false);
BaseViewerApplication::Clear();
}

ManipulatorWindow.qml

import QtQuick 2.12
import QtQuick.Controls 2.3
import CadEx 1.2
BaseViewerWindow {
id: root
property int transformationType: 0
property int transformationValue: 0
property bool transformationActive: false
property bool isAttached: false
signal resetTransformation()
signal manipulatorModeChanged (var theMode)
onTransformationActiveChanged: {
if (!transformationActive && transformationType == 0) {
transformationValue = 0
}
}
onTransformationTypeChanged: {
transformationValue = 0
}
enum ManipulatorModes {
None = 0x00,
Translation = 0x01,
Rotation = 0x02
}
Menu {
id: manipulatorModeMenu
title: qsTr("&Manipulator mode")
property var manipulatorModeMask: ManipulatorWindow.ManipulatorModes.Translation
| ManipulatorWindow.ManipulatorModes.Rotation
onManipulatorModeMaskChanged: {
manipulatorModeChanged (manipulatorModeMask);
}
function updateMask (theMode, checked)
{
manipulatorModeMask = checked ? manipulatorModeMask | theMode
: manipulatorModeMask & ~theMode;
}
MenuItem {
text: qsTr("&Translation")
checkable: true
checked: true
onCheckedChanged: {
manipulatorModeMenu.updateMask (ManipulatorWindow.ManipulatorModes.Translation, checked);
}
}
MenuItem {
text: qsTr("&Rotation")
checkable: true
checked: true
onCheckedChanged: {
manipulatorModeMenu.updateMask (ManipulatorWindow.ManipulatorModes.Rotation, checked)
}
}
}
onMenuCompleted: {
menuBarItem.addMenu (manipulatorModeMenu);
}
Rectangle {
id: transformationBar
radius: 3
width: 300
height: 200
anchors.margins: 10
anchors.top: parent.top
anchors.right: parent.right
color: "#CCFFFFFF"
border.color: "#55000000"
readonly property string textColor: "#BB000000"
Column {
padding: 20
spacing: 20
width: parent.width
Text {
width: parent.width - 40
horizontalAlignment: Text.AlignHCenter
font.pointSize: 14
font.bold: true
color: transformationBar.textColor
text: qsTr ("Transformation")
}
Text {
width: parent.width - 40
visible: !isAttached || (isAttached && transformationType == 0)
text: !isAttached ? qsTr ("Please select any part") : qsTr ("Transform or rotate")
font.pointSize: 13
font.italic: true
color: transformationBar.textColor
horizontalAlignment: Text.AlignHCenter
}
Text {
visible: isAttached && transformationType !=0
text: qsTr ("Type: ") + (transformationType == 1 ? qsTr ("Translation") : qsTr ("Rotation"))
font.pointSize: 13
color: transformationBar.textColor
}
Text {
visible: isAttached && transformationType !=0
text: qsTr ("Result: ") + transformationValue
font.pointSize: 13
color: transformationBar.textColor
}
Button {
width: parent.width - 40
anchors.horizontalCenter: parent.horizontalCenter
visible: isAttached && transformationType !=0
enabled: !transformationActive
text: qsTr ("Reset transformation")
onClicked: {
resetTransformation();
}
}
}
}
}

main.cxx

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2022, CADEX. All rights reserved.
//
// This file is part of the CAD Exchanger software.
//
// You may use this file under the terms of the BSD license as follows:
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 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 COPYRIGHT HOLDER 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 "ManipulatorApplication.hxx"
#include <cadex/LicenseManager_Activate.h>
#include <QtCore/QUrl>
#include <QtWidgets/QApplication>
#include "../../../cadex_license.cxx"
using namespace cadex;
int main (int argc, char *argv[])
{
auto aKey = LicenseKey::Value();
//activate the license (aKey must be defined in cadex_license.cxx)
if (!CADExLicense_Activate (aKey)) {
qCritical ("Failed to activate CAD Exchanger license.");
return 1;
}
// Must set dpi scaling before creating an application.
QCoreApplication::setAttribute (Qt::AA_EnableHighDpiScaling);
// Create QApplication because Qt.labs.platform module requires Qt Widgets library.
// For more information see https://doc.qt.io/qt-5.10/qml-qt-labs-platform-filedialog.html
QApplication app (argc, argv);
ManipulatorApplication anApp;
if (anApp.Initialize (QUrl ("qrc:/qml/ManipulatorWindow.qml"), "viewPort")) {
return app.exec();
}
return 0;
}
virtual void StopTransformation()=0
A callback is invoked when a manipulator controller is released. Before that, the manipulator updates...
Defines an observer on selection events.
Definition: ModelPrs_SelectionChangesObserver.hxx:29
bool IsNull() const
Returns true if the object is nullified.
Definition: Base_PublicObject.cxx:46
virtual void ModeChanged(ModelPrs_ManipulatorMode theMode, const ModelData_Direction &theDirection)=0
A callback is invoked when the manipulator has changed mode or the direction of translation/direction...
Defines a 3D direction.
Definition: ModelData_Direction.hxx:169
Defines a visitor of the selection object.
Definition: ModelPrs_SelectionVisitor.hxx:33
void Accept(ModelPrs_SelectionVisitor &theVisitor) const
Accepts a visitor.
Definition: ModelPrs_Selection.cxx:197
Encapsulates a container of selected scene nodes.
Definition: ModelPrs_Selection.hxx:39
Represents a node in the visual scene graph.
Definition: ModelPrs_SceneNode.hxx:43
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22
virtual void SelectionChangedByManager(const ModelPrs_Selection &theCurrent, const ModelPrs_Selection &theSelected, const ModelPrs_Selection &theDeselected)=0
The method is invoked when the source of the selection event is the manager's method call.
Base abstract class for observers registered in ModelPrs_Manipulator.
Definition: ModelPrs_ManipulatorObserver.hxx:32
virtual void SelectionChangedByScene(const ModelPrs_Selection &theCurrent, const ModelPrs_Selection &theSelected, const ModelPrs_Selection &theDeselected)=0
The method is invoked when the source of the selection event is mouse click on the scene.
virtual void StartTransformation()=0
A callback is invoked when a manipulator controller is pressed down by the mouse button....
Defines a transformation matrix.
Definition: ModelData_Transformation.hxx:32
virtual void Transformed(const ModelData_Transformation &theLocalTrsf, const ModelData_Transformation &theGlobalTrsf, double theValue)=0
A callback is invoked when the manipulator makes transformations to the graphic presentation of scene...