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() Standard_OVERRIDE {}
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)
67 DFBrowser_SearchLine* aSearchLine = theWindow->GetTreeLevelLine()->GetSearchLine();
68 myItems.append (new DFBrowser_ThreadItemSearch(aSearchLine));
71 // =======================================================================
72 // function : ProcessApplication
74 // =======================================================================
75 void DFBrowser_Thread::ProcessApplication()
77 for (int anItemId = 0, aSize = myItems.size(); anItemId < aSize; anItemId++)
78 startThread (myItems[anItemId]);
81 // =======================================================================
82 // function : startThread
84 // =======================================================================
85 void DFBrowser_Thread::startThread (DFBrowser_ThreadItem* theItem)
87 DFBrowser_QThread* aThread = new DFBrowser_QThread (this);
88 aThread->setItem (theItem);
90 connect (aThread, SIGNAL (finished()), this, SLOT (onFinished()), Qt::QueuedConnection);
91 myStartedThreads.append (aThread);
94 // =======================================================================
95 // function : TerminateThread
97 // =======================================================================
98 void DFBrowser_Thread::TerminateThread()
100 for (int aThreadsId = 0, aCount = myStartedThreads.size(); aThreadsId < aCount; aThreadsId++)
102 QThread* aThread = myStartedThreads[aThreadsId];
103 if (aThread->isRunning())
104 aThread->terminate();
108 // =======================================================================
109 // function : onFinished
111 // =======================================================================
112 void DFBrowser_Thread::onFinished()
114 DFBrowser_QThread* aThread = (DFBrowser_QThread*)(sender());
115 if (myIsFinishProcessing)
117 // if thread send signal when other finished signal is processed
119 myPostponedItem = aThread->getItem();
123 myIsFinishProcessing = true;
126 myStartedThreads.removeAll (aThread);
127 DFBrowser_ThreadItem* anItem = aThread->getItem();
129 anItem->ApplyValues();
132 myIsFinishProcessing = false;
135 myPostponedItem->ApplyValues();