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> |
0cb512c0 |
18 | #include <inspector/DFBrowser_TreeLevelLine.hxx> |
19 | #include <inspector/DFBrowser_SearchLine.hxx> |
14bbbdcb |
20 | |
0cb512c0 |
21 | #include <inspector/DFBrowser_Window.hxx> |
14bbbdcb |
22 | |
130eb114 |
23 | #include <Standard_WarningsDisable.hxx> |
14bbbdcb |
24 | #include <QThread> |
130eb114 |
25 | #include <Standard_WarningsRestore.hxx> |
14bbbdcb |
26 | |
27 | //! \class DFBrowser_QThread |
28 | //! Internal class to cover QThread in order to process ThreadItem. |
29 | class DFBrowser_QThread : public QThread |
30 | { |
31 | public: |
32 | |
33 | //! Constructor |
34 | DFBrowser_QThread (QObject* theParent) : QThread (theParent), myItem (0) {} |
35 | |
36 | //! Destructor |
7e1c1e48 |
37 | virtual ~DFBrowser_QThread() {} |
14bbbdcb |
38 | |
39 | //! Sets thread item to be processed |
40 | //! \param theItem a thread item |
41 | void setItem (DFBrowser_ThreadItem* theItem) { myItem = theItem; } |
42 | |
43 | //! Returns the current processing thread item |
44 | DFBrowser_ThreadItem* getItem() const { return myItem; } |
45 | |
46 | protected: |
47 | |
48 | //! Starts thread item |
49 | virtual void run() Standard_OVERRIDE |
50 | { |
51 | if (myItem) |
52 | myItem->Run(); |
53 | } |
54 | |
55 | private: |
56 | |
57 | DFBrowser_ThreadItem* myItem; |
58 | }; |
59 | |
60 | // ======================================================================= |
61 | // function : Constructor |
62 | // purpose : |
63 | // ======================================================================= |
64 | DFBrowser_Thread::DFBrowser_Thread (DFBrowser_Window* theWindow) |
0c44027c |
65 | : QObject (theWindow), myPostponedItem (0), myIsFinishProcessing (false), |
66 | myIsProcessPostponed (Standard_False) |
14bbbdcb |
67 | { |
68 | DFBrowser_SearchLine* aSearchLine = theWindow->GetTreeLevelLine()->GetSearchLine(); |
69 | myItems.append (new DFBrowser_ThreadItemSearch(aSearchLine)); |
14bbbdcb |
70 | } |
71 | |
72 | // ======================================================================= |
73 | // function : ProcessApplication |
74 | // purpose : |
75 | // ======================================================================= |
76 | void DFBrowser_Thread::ProcessApplication() |
77 | { |
0c44027c |
78 | if (!myStartedThreads.empty()) |
79 | { |
80 | myIsProcessPostponed = Standard_True; |
81 | return; |
82 | } |
14bbbdcb |
83 | for (int anItemId = 0, aSize = myItems.size(); anItemId < aSize; anItemId++) |
84 | startThread (myItems[anItemId]); |
85 | } |
86 | |
87 | // ======================================================================= |
88 | // function : startThread |
89 | // purpose : |
90 | // ======================================================================= |
91 | void DFBrowser_Thread::startThread (DFBrowser_ThreadItem* theItem) |
92 | { |
93 | DFBrowser_QThread* aThread = new DFBrowser_QThread (this); |
94 | aThread->setItem (theItem); |
95 | aThread->start(); |
96 | connect (aThread, SIGNAL (finished()), this, SLOT (onFinished()), Qt::QueuedConnection); |
97 | myStartedThreads.append (aThread); |
98 | } |
99 | |
100 | // ======================================================================= |
101 | // function : TerminateThread |
102 | // purpose : |
103 | // ======================================================================= |
104 | void DFBrowser_Thread::TerminateThread() |
105 | { |
106 | for (int aThreadsId = 0, aCount = myStartedThreads.size(); aThreadsId < aCount; aThreadsId++) |
107 | { |
108 | QThread* aThread = myStartedThreads[aThreadsId]; |
109 | if (aThread->isRunning()) |
110 | aThread->terminate(); |
111 | } |
112 | } |
113 | |
114 | // ======================================================================= |
115 | // function : onFinished |
116 | // purpose : |
117 | // ======================================================================= |
118 | void DFBrowser_Thread::onFinished() |
119 | { |
120 | DFBrowser_QThread* aThread = (DFBrowser_QThread*)(sender()); |
121 | if (myIsFinishProcessing) |
122 | { |
123 | // if thread send signal when other finished signal is processed |
124 | if (aThread) |
125 | myPostponedItem = aThread->getItem(); |
126 | return; |
127 | } |
128 | |
129 | myIsFinishProcessing = true; |
130 | if (aThread) |
131 | { |
132 | myStartedThreads.removeAll (aThread); |
133 | DFBrowser_ThreadItem* anItem = aThread->getItem(); |
134 | if (anItem) |
135 | anItem->ApplyValues(); |
136 | } |
137 | |
138 | myIsFinishProcessing = false; |
139 | if (myPostponedItem) |
140 | { |
141 | myPostponedItem->ApplyValues(); |
142 | myPostponedItem = 0; |
143 | } |
0c44027c |
144 | |
145 | if (myIsProcessPostponed) |
146 | { |
147 | myIsProcessPostponed = Standard_False; |
148 | ProcessApplication(); |
149 | } |
14bbbdcb |
150 | } |