1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <inspector/DFBrowser_Thread.hxx>
17 #include <inspector/DFBrowser_ThreadItemSearch.hxx>
18 #include <inspector/DFBrowser_TreeLevelLine.hxx>
19 #include <inspector/DFBrowser_SearchLine.hxx>
21 #include <inspector/DFBrowser_Window.hxx>
23 #include <Standard_WarningsDisable.hxx>
25 #include <Standard_WarningsRestore.hxx>
27 //! \class DFBrowser_QThread
28 //! Internal class to cover QThread in order to process ThreadItem.
29 class DFBrowser_QThread : public QThread
34 DFBrowser_QThread (QObject* theParent) : QThread (theParent), myItem (0) {}
37 virtual ~DFBrowser_QThread() {}
39 //! Sets thread item to be processed
40 //! \param theItem a thread item
41 void setItem (DFBrowser_ThreadItem* theItem) { myItem = theItem; }
43 //! Returns the current processing thread item
44 DFBrowser_ThreadItem* getItem() const { return myItem; }
48 //! Starts thread item
49 virtual void run() Standard_OVERRIDE
57 DFBrowser_ThreadItem* myItem;
60 // =======================================================================
61 // function : Constructor
63 // =======================================================================
64 DFBrowser_Thread::DFBrowser_Thread (DFBrowser_Window* theWindow)
65 : QObject (theWindow), myPostponedItem (0), myIsFinishProcessing (false),
66 myIsProcessPostponed (Standard_False)
68 DFBrowser_SearchLine* aSearchLine = theWindow->GetTreeLevelLine()->GetSearchLine();
69 myItems.append (new DFBrowser_ThreadItemSearch(aSearchLine));
72 // =======================================================================
73 // function : ProcessApplication
75 // =======================================================================
76 void DFBrowser_Thread::ProcessApplication()
78 if (!myStartedThreads.empty())
80 myIsProcessPostponed = Standard_True;
83 for (int anItemId = 0, aSize = myItems.size(); anItemId < aSize; anItemId++)
84 startThread (myItems[anItemId]);
87 // =======================================================================
88 // function : startThread
90 // =======================================================================
91 void DFBrowser_Thread::startThread (DFBrowser_ThreadItem* theItem)
93 DFBrowser_QThread* aThread = new DFBrowser_QThread (this);
94 aThread->setItem (theItem);
96 connect (aThread, SIGNAL (finished()), this, SLOT (onFinished()), Qt::QueuedConnection);
97 myStartedThreads.append (aThread);
100 // =======================================================================
101 // function : TerminateThread
103 // =======================================================================
104 void DFBrowser_Thread::TerminateThread()
106 for (int aThreadsId = 0, aCount = myStartedThreads.size(); aThreadsId < aCount; aThreadsId++)
108 QThread* aThread = myStartedThreads[aThreadsId];
109 if (aThread->isRunning())
110 aThread->terminate();
114 // =======================================================================
115 // function : onFinished
117 // =======================================================================
118 void DFBrowser_Thread::onFinished()
120 DFBrowser_QThread* aThread = (DFBrowser_QThread*)(sender());
121 if (myIsFinishProcessing)
123 // if thread send signal when other finished signal is processed
125 myPostponedItem = aThread->getItem();
129 myIsFinishProcessing = true;
132 myStartedThreads.removeAll (aThread);
133 DFBrowser_ThreadItem* anItem = aThread->getItem();
135 anItem->ApplyValues();
138 myIsFinishProcessing = false;
141 myPostponedItem->ApplyValues();
145 if (myIsProcessPostponed)
147 myIsProcessPostponed = Standard_False;
148 ProcessApplication();