Skip to content

Commit

Permalink
0029750: Samples - function arrows are not updated by moving a node i…
Browse files Browse the repository at this point in the history
…n FuncDemo qt sample

The visual links between functions are recovered.
The demo is synchronized with a corresponding demo from Qt (qt486-vc10-32\examples\graphicsview\elasticnodes). It may be successfully compiled by any further versions of Qt including 5.10.1
Also, because Open CASCADE (and OCAF in particular) is improved for usage in multi-threading mode, usage of mutexes is added in this sample (for access to the sharing TNaming_UsedShapes attribute, for example).
  • Loading branch information
VladislavVitalievichRomashko authored and bugmaster committed Dec 25, 2020
1 parent 4104614 commit 894133a
Show file tree
Hide file tree
Showing 21 changed files with 389 additions and 82 deletions.
2 changes: 1 addition & 1 deletion samples/qt/FuncDemo/custom.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@echo off
rem Define QTDIR variable
set QTDIR=
set QTDIR=%PRODUCTS_PATH%/qt486-vc10-32
7 changes: 6 additions & 1 deletion samples/qt/FuncDemo/src/BaseDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ void BaseDriver::Arguments(TDF_LabelList& args) const
}
}


// Returns the results of the function
void BaseDriver::Results(TDF_LabelList& res) const
{
Expand All @@ -50,6 +49,12 @@ void BaseDriver::Results(TDF_LabelList& res) const
}
}

// Sets a mutex for execution of the driver.
void BaseDriver::SetMutex(Standard_Mutex* pmutex)
{
myMutex = pmutex;
}

// Execution.
Standard_Integer BaseDriver::Execute(Handle(TFunction_Logbook)& ) const
{
Expand Down
11 changes: 7 additions & 4 deletions samples/qt/FuncDemo/src/BaseDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
#if !defined(_BASEDRIVER_H_)
#define _BASEDRIVER_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <Standard_DefineHandle.hxx>
#include <TFunction_Driver.hxx>
#include <TFunction_Logbook.hxx>
#include <TDF_LabelList.hxx>
#include <Standard_Mutex.hxx>

DEFINE_STANDARD_HANDLE(BaseDriver, TFunction_Driver)

Expand All @@ -30,10 +27,16 @@ class BaseDriver : public TFunction_Driver
// Returns the results of the function
virtual void Results(TDF_LabelList& res) const;

// Sets a mutex for execution of the driver.
void SetMutex(Standard_Mutex* pmutex);

// Execution.
virtual Standard_Integer Execute(Handle(TFunction_Logbook)& log) const;

DEFINE_STANDARD_RTTIEXT(BaseDriver, TFunction_Driver)

protected:
Standard_Mutex* myMutex;
};

#endif // !defined(_BASEDRIVER_H_)
4 changes: 0 additions & 4 deletions samples/qt/FuncDemo/src/CircleDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#if !defined(_CIRCLEDRIVER_H_)
#define _CIRCLEDRIVER_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "BaseDriver.h"

#include <Standard_DefineHandle.hxx>
Expand Down
4 changes: 0 additions & 4 deletions samples/qt/FuncDemo/src/ConeDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#if !defined(_CONEDRIVER_H_)
#define _CONEDRIVER_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "BaseDriver.h"

#include <Standard_DefineHandle.hxx>
Expand Down
4 changes: 0 additions & 4 deletions samples/qt/FuncDemo/src/CylinderDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#if !defined(_CYLINDERDRIVER_H_)
#define _CYLINDERDRIVER_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "BaseDriver.h"

#include <Standard_DefineHandle.hxx>
Expand Down
16 changes: 13 additions & 3 deletions samples/qt/FuncDemo/src/FThread.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "FThread.h"
#include "graphwidget.h"
#include "BaseDriver.h"

#include <TFunction_Function.hxx>
#include <TFunction_IFunction.hxx>
Expand Down Expand Up @@ -39,6 +40,11 @@ void FThread::setThreadIndex(const int theIndex)
this->thread_index = theIndex;
}

void FThread::setMutex(Standard_Mutex* pmutex)
{
this->pmutex = pmutex;
}

// Returns any free (not executed yet) function
TDF_Label FThread::getFreeFunction()
{
Expand Down Expand Up @@ -94,12 +100,16 @@ void FThread::run()
const bool must = D->MustExecute(log);
if (must)
{
// Usage of mutex for execution of Open CASCADE code is the most stupid thing!!!
// But it makes the execution more reliable...
// Usage of mutex for execution of Open CASCADE code.
// It makes the execution more reliable...
if (!Handle(BaseDriver)::DownCast(D).IsNull())
Handle(BaseDriver)::DownCast(D)->SetMutex(pmutex);

// Execute the driver.
const int ret = D->Execute(log);
if (ret == 0)
{
// Successfully executed!
// Successfuly executed!
itr.SetStatus(L, TFunction_ES_Succeeded);

TDF_LabelList res;
Expand Down
3 changes: 3 additions & 0 deletions samples/qt/FuncDemo/src/FThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <TFunction_Logbook.hxx>
#include <TFunction_Iterator.hxx>
#include <TFunction_Driver.hxx>
#include <Standard_Mutex.hxx>

class GraphWidget; // shows graphically execution of functions

Expand All @@ -25,6 +26,7 @@ class FThread : public QThread
void setLogbook(const Handle(TFunction_Logbook)& ); // to set logbook with modifications
void setGraph(GraphWidget* ); // to change color of a graph circle
void setThreadIndex(const int ); // to set the index of the thread
void setMutex(Standard_Mutex* );

protected:

Expand All @@ -35,6 +37,7 @@ class FThread : public QThread

TFunction_Iterator itr;
Handle(TFunction_Logbook) log;
Standard_Mutex* pmutex;
int thread_index;

GraphWidget* graph;
Expand Down
6 changes: 6 additions & 0 deletions samples/qt/FuncDemo/src/PointDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ Standard_Integer PointDriver::Execute(Handle(TFunction_Logbook)& log) const
// Make the result
TopoDS_Vertex V = BRepBuilderAPI_MakeVertex(gp_Pnt(x, y, z));

if (myMutex)
myMutex->Lock();

// Set the result
TNaming_Builder B(Label());
B.Generated(V);

if (myMutex)
myMutex->Unlock();

return BaseDriver::Execute(log);
}
4 changes: 0 additions & 4 deletions samples/qt/FuncDemo/src/PointDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#if !defined(_PointDRIVER_H_)
#define _PointDRIVER_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "BaseDriver.h"

#include <Standard_DefineHandle.hxx>
Expand Down
4 changes: 0 additions & 4 deletions samples/qt/FuncDemo/src/PrismDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#if !defined(_PRISMDRIVER_H_)
#define _PRISMDRIVER_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "BaseDriver.h"

#include <Standard_DefineHandle.hxx>
Expand Down
6 changes: 5 additions & 1 deletion samples/qt/FuncDemo/src/ShapeSaverDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ Standard_Integer ShapeSaverDriver::Execute(Handle(TFunction_Logbook)& log) const
}
}

//BRepTools::Write(C, "result.brep");
if (myMutex)
myMutex->Lock();

TNaming_Builder Bui(Label());
Bui.Generated(C);

if (myMutex)
myMutex->Unlock();

return BaseDriver::Execute(log);
}
4 changes: 0 additions & 4 deletions samples/qt/FuncDemo/src/ShapeSaverDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#if !defined(_SHAPESAVERDRIVER_H_)
#define _SHAPESAVERDRIVER_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "BaseDriver.h"

#include <Standard_DefineHandle.hxx>
Expand Down
4 changes: 0 additions & 4 deletions samples/qt/FuncDemo/src/SimpleDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#if !defined(_SIMPLEDRIVER_H_)
#define _SIMPLEDRIVER_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <Standard_DefineHandle.hxx>
#include <TFunction_Driver.hxx>
#include <TFunction_Logbook.hxx>
Expand Down
14 changes: 14 additions & 0 deletions samples/qt/FuncDemo/src/edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
static const double Pi = 3.14159265358979323846264338327950288419717;
static double TwoPi = 2.0 * Pi;

//! [0]
Edge::Edge(Node *sourceNode, Node *destNode)
: arrowSize(10)
{
Expand All @@ -60,7 +61,9 @@ Edge::Edge(Node *sourceNode, Node *destNode)
dest->addEdge(this);
adjust();
}
//! [0]

//! [1]
Node *Edge::sourceNode() const
{
return source;
Expand All @@ -70,7 +73,9 @@ Node *Edge::destNode() const
{
return dest;
}
//! [1]

//! [2]
void Edge::adjust()
{
if (!source || !dest)
Expand All @@ -89,7 +94,9 @@ void Edge::adjust()
sourcePoint = destPoint = line.p1();
}
}
//! [2]

//! [3]
QRectF Edge::boundingRect() const
{
if (!source || !dest)
Expand All @@ -103,7 +110,9 @@ QRectF Edge::boundingRect() const
.normalized()
.adjusted(-extra, -extra, extra, extra);
}
//! [3]

//! [4]
void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
if (!source || !dest)
Expand All @@ -112,11 +121,15 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
QLineF line(sourcePoint, destPoint);
if (qFuzzyCompare(line.length(), qreal(0.)))
return;
//! [4]

//! [5]
// Draw the line itself
painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter->drawLine(line);
//! [5]

//! [6]
// Draw the arrows
double angle = ::acos(line.dx() / line.length());
if (line.dy() >= 0)
Expand All @@ -135,3 +148,4 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
}
//! [6]
3 changes: 2 additions & 1 deletion samples/qt/FuncDemo/src/edge.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@

class Node;

//! [0]
class Edge : public QGraphicsItem
{
public:
Edge(Node *sourceNode, Node *destNode);

Node *sourceNode() const;

Node *destNode() const;

void adjust();
Expand All @@ -72,5 +72,6 @@ class Edge : public QGraphicsItem
QPointF destPoint;
qreal arrowSize;
};
//! [0]

#endif
Loading

0 comments on commit 894133a

Please sign in to comment.