14bbbdcb |
1 | // Created on: 2017-06-16 |
2 | // Created by: Natalia ERMOLAEVA |
3 | // Copyright (c) 2017 OPEN CASCADE SAS |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
12 | // |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
15 | |
0cb512c0 |
16 | #include <inspector/DFBrowser_Thread.hxx> |
17 | #include <inspector/DFBrowser_ThreadItemSearch.hxx> |
18 | #include <inspector/DFBrowser_ThreadItemUsedShapesMap.hxx> |
19 | #include <inspector/DFBrowser_TreeLevelLine.hxx> |
20 | #include <inspector/DFBrowser_SearchLine.hxx> |
14bbbdcb |
21 | |
0cb512c0 |
22 | #include <inspector/DFBrowser_Window.hxx> |
14bbbdcb |
23 | |
130eb114 |
24 | #include <Standard_WarningsDisable.hxx> |
14bbbdcb |
25 | #include <QThread> |
130eb114 |
26 | #include <Standard_WarningsRestore.hxx> |
14bbbdcb |
27 | |
28 | //! \class DFBrowser_QThread |
29 | //! Internal class to cover QThread in order to process ThreadItem. |
30 | class DFBrowser_QThread : public QThread |
31 | { |
32 | public: |
33 | |
34 | //! Constructor |
35 | DFBrowser_QThread (QObject* theParent) : QThread (theParent), myItem (0) {} |
36 | |
37 | //! Destructor |
38 | virtual ~DFBrowser_QThread() Standard_OVERRIDE {} |
39 | |
40 | //! Sets thread item to be processed |
41 | //! \param theItem a thread item |
42 | void setItem (DFBrowser_ThreadItem* theItem) { myItem = theItem; } |
43 | |
44 | //! Returns the current processing thread item |
45 | DFBrowser_ThreadItem* getItem() const { return myItem; } |
46 | |
47 | protected: |
48 | |
49 | //! Starts thread item |
50 | virtual void run() Standard_OVERRIDE |
51 | { |
52 | if (myItem) |
53 | myItem->Run(); |
54 | } |
55 | |
56 | private: |
57 | |
58 | DFBrowser_ThreadItem* myItem; |
59 | }; |
60 | |
61 | // ======================================================================= |
62 | // function : Constructor |
63 | // purpose : |
64 | // ======================================================================= |
65 | DFBrowser_Thread::DFBrowser_Thread (DFBrowser_Window* theWindow) |
66 | : QObject (theWindow), myPostponedItem (0), myIsFinishProcessing (false) |
67 | { |
68 | DFBrowser_SearchLine* aSearchLine = theWindow->GetTreeLevelLine()->GetSearchLine(); |
69 | myItems.append (new DFBrowser_ThreadItemSearch(aSearchLine)); |
70 | myItems.append (new DFBrowser_ThreadItemUsedShapesMap()); |
71 | } |
72 | |
73 | // ======================================================================= |
74 | // function : SetModule |
75 | // purpose : |
76 | // ======================================================================= |
77 | void DFBrowser_Thread::SetModule (DFBrowser_Module* theModule) |
78 | { |
79 | for (int anItemId = 0, aSize = myItems.size(); anItemId < aSize; anItemId++) |
80 | { |
81 | DFBrowser_ThreadItemUsedShapesMap* aShapesItem = dynamic_cast<DFBrowser_ThreadItemUsedShapesMap*> (myItems[anItemId]); |
82 | if (aShapesItem) |
83 | aShapesItem->SetModule (theModule); |
84 | } |
85 | } |
86 | |
87 | // ======================================================================= |
88 | // function : ProcessApplication |
89 | // purpose : |
90 | // ======================================================================= |
91 | void DFBrowser_Thread::ProcessApplication() |
92 | { |
93 | for (int anItemId = 0, aSize = myItems.size(); anItemId < aSize; anItemId++) |
94 | startThread (myItems[anItemId]); |
95 | } |
96 | |
97 | // ======================================================================= |
98 | // function : startThread |
99 | // purpose : |
100 | // ======================================================================= |
101 | void DFBrowser_Thread::startThread (DFBrowser_ThreadItem* theItem) |
102 | { |
103 | DFBrowser_QThread* aThread = new DFBrowser_QThread (this); |
104 | aThread->setItem (theItem); |
105 | aThread->start(); |
106 | connect (aThread, SIGNAL (finished()), this, SLOT (onFinished()), Qt::QueuedConnection); |
107 | myStartedThreads.append (aThread); |
108 | } |
109 | |
110 | // ======================================================================= |
111 | // function : TerminateThread |
112 | // purpose : |
113 | // ======================================================================= |
114 | void DFBrowser_Thread::TerminateThread() |
115 | { |
116 | for (int aThreadsId = 0, aCount = myStartedThreads.size(); aThreadsId < aCount; aThreadsId++) |
117 | { |
118 | QThread* aThread = myStartedThreads[aThreadsId]; |
119 | if (aThread->isRunning()) |
120 | aThread->terminate(); |
121 | } |
122 | } |
123 | |
124 | // ======================================================================= |
125 | // function : onFinished |
126 | // purpose : |
127 | // ======================================================================= |
128 | void DFBrowser_Thread::onFinished() |
129 | { |
130 | DFBrowser_QThread* aThread = (DFBrowser_QThread*)(sender()); |
131 | if (myIsFinishProcessing) |
132 | { |
133 | // if thread send signal when other finished signal is processed |
134 | if (aThread) |
135 | myPostponedItem = aThread->getItem(); |
136 | return; |
137 | } |
138 | |
139 | myIsFinishProcessing = true; |
140 | if (aThread) |
141 | { |
142 | myStartedThreads.removeAll (aThread); |
143 | DFBrowser_ThreadItem* anItem = aThread->getItem(); |
144 | if (anItem) |
145 | anItem->ApplyValues(); |
146 | } |
147 | |
148 | myIsFinishProcessing = false; |
149 | if (myPostponedItem) |
150 | { |
151 | myPostponedItem->ApplyValues(); |
152 | myPostponedItem = 0; |
153 | } |
154 | } |