Correction misprinting
[occt.git] / samples / qt / FuncDemo / src / FThread.cpp
1 #include "FThread.h"
2 #include "GraphWidget.h"
3
4 #include <TFunction_Function.hxx>
5 #include <TFunction_IFunction.hxx>
6 #include <TFunction_Driver.hxx>
7 #include <TFunction_GraphNode.hxx>
8
9 #include <TDataStd_Tick.hxx>
10 #include <TDF_ListIteratorOfLabelList.hxx>
11
12 FThread::FThread(QObject* parent):QThread(parent),thread_index(0)
13 {
14
15 }
16
17 FThread::~FThread()
18 {
19
20 }
21
22 void FThread::setIterator(const TFunction_Iterator& itr)
23 {
24     this->itr = itr;
25 }
26
27 void FThread::setLogbook(const Handle(TFunction_Logbook)& log)
28 {
29     this->log = log;
30 }
31
32 void FThread::setGraph(GraphWidget* graph)
33 {
34     this->graph = graph;
35 }
36
37 void FThread::setThreadIndex(const int thread_index)
38 {
39     this->thread_index = thread_index;
40 }
41
42 // Returns any free (not executed yet) function
43 TDF_Label FThread::getFreeFunction()
44 {
45     TDF_Label L;
46     TDF_ListIteratorOfLabelList itrl(itr.Current());
47     for (; itrl.More(); itrl.Next())
48     {
49         if (itr.GetStatus(itrl.Value()) == TFunction_ES_NotExecuted)
50         {
51             L = itrl.Value();
52             itr.SetStatus(L, TFunction_ES_Executing);
53             break;
54         }
55     }
56     return L;
57 }
58
59 void FThread::run()
60 {
61     while (itr.More())
62     {
63         // Take current function,
64         // choose one and set its status to "executing".
65         TDF_Label L;
66         for (; itr.More(); itr.Next())
67         {
68             L = getFreeFunction();
69             if (L.IsNull())
70                 ::Sleep(100);
71             else
72                 break;
73         }
74
75         // Nothing to compute? Finish.
76         if (L.IsNull())
77         {
78             graph->setFinished();
79             return;
80         }
81
82         // Check a Tick attribute - a marker of skipped for execution functions.
83         // It is used only for visual presentation of skipped (not modified) functions.
84         Handle(TDataStd_Tick) tick;
85         if (L.FindAttribute(TDataStd_Tick::GetID(), tick))
86             L.ForgetAttribute(tick);
87
88         // Execute the function
89         Handle(TFunction_Driver) D = TFunction_IFunction(L).GetDriver(thread_index);
90         const bool must = D->MustExecute(log);
91         if (must)
92         {
93             // Usage of mutex for execution of Open CASCADE code is the most stupid thing!!!
94             // But it makes the execution more reliable...
95             const int ret = D->Execute(log);
96             if (ret == 0)
97             {
98                 // Successfuly executed!
99                 itr.SetStatus(L, TFunction_ES_Succeeded);
100
101                 TDF_LabelList res;
102                 D->Results(res);
103                 TDF_ListIteratorOfLabelList itrr(res);
104                 for (; itrr.More(); itrr.Next())
105                 {
106                     log->SetImpacted(itrr.Value());
107                 }
108             }
109             else
110             {
111                 // Failed...
112                 itr.SetStatus(L, TFunction_ES_Failed);
113                 graph->setFinished();
114                 return;
115             }
116         }
117         else if (itr.GetStatus(L) == TFunction_ES_Executing)
118         {
119             itr.SetStatus(L, TFunction_ES_Succeeded);
120             TDataStd_Tick::Set(L);
121         }
122
123     }// while (More())
124
125     graph->setFinished();
126 }