File: DFBrowser_Thread.cxx

package info (click to toggle)
opencascade 7.3.0%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 278,376 kB
  • sloc: cpp: 1,136,010; ansic: 81,569; tcl: 14,864; cs: 5,173; java: 1,522; xml: 468; sh: 375; perl: 37; makefile: 25
file content (138 lines) | stat: -rw-r--r-- 4,355 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Created on: 2017-06-16
// Created by: Natalia ERMOLAEVA
// Copyright (c) 2017 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement. 

#include <inspector/DFBrowser_Thread.hxx>
#include <inspector/DFBrowser_ThreadItemSearch.hxx>
#include <inspector/DFBrowser_TreeLevelLine.hxx>
#include <inspector/DFBrowser_SearchLine.hxx>

#include <inspector/DFBrowser_Window.hxx>

#include <Standard_WarningsDisable.hxx>
#include <QThread>
#include <Standard_WarningsRestore.hxx>

//! \class DFBrowser_QThread
//! Internal class to cover QThread in order to process ThreadItem.
class DFBrowser_QThread : public QThread
{
public:

  //! Constructor
  DFBrowser_QThread (QObject* theParent) : QThread (theParent), myItem (0) {}

  //! Destructor
  virtual ~DFBrowser_QThread() Standard_OVERRIDE {}

  //! Sets thread item to be processed
  //! \param theItem a thread item
  void setItem (DFBrowser_ThreadItem* theItem) { myItem = theItem; }

  //! Returns the current processing thread item
  DFBrowser_ThreadItem* getItem() const { return myItem; }

protected:

  //! Starts thread item
  virtual void run() Standard_OVERRIDE
  {
    if (myItem)
      myItem->Run();
  }

private:

  DFBrowser_ThreadItem* myItem;
};

// =======================================================================
// function : Constructor
// purpose :
// =======================================================================
DFBrowser_Thread::DFBrowser_Thread (DFBrowser_Window* theWindow)
: QObject (theWindow), myPostponedItem (0), myIsFinishProcessing (false)
{
  DFBrowser_SearchLine* aSearchLine = theWindow->GetTreeLevelLine()->GetSearchLine();
  myItems.append (new DFBrowser_ThreadItemSearch(aSearchLine));
}

// =======================================================================
// function : ProcessApplication
// purpose :
// =======================================================================
void DFBrowser_Thread::ProcessApplication()
{
  for (int anItemId = 0, aSize = myItems.size(); anItemId < aSize; anItemId++)
    startThread (myItems[anItemId]);
}

// =======================================================================
// function : startThread
// purpose :
// =======================================================================
void DFBrowser_Thread::startThread (DFBrowser_ThreadItem* theItem)
{
  DFBrowser_QThread* aThread = new DFBrowser_QThread (this);
  aThread->setItem (theItem);
  aThread->start();
  connect (aThread, SIGNAL (finished()), this, SLOT (onFinished()), Qt::QueuedConnection);
  myStartedThreads.append (aThread);
}

// =======================================================================
// function : TerminateThread
// purpose :
// =======================================================================
void DFBrowser_Thread::TerminateThread()
{
  for (int aThreadsId = 0, aCount = myStartedThreads.size(); aThreadsId < aCount; aThreadsId++)
  {
    QThread* aThread = myStartedThreads[aThreadsId];
    if (aThread->isRunning())
      aThread->terminate();
  }
}

// =======================================================================
// function : onFinished
// purpose :
// =======================================================================
void DFBrowser_Thread::onFinished()
{
  DFBrowser_QThread* aThread = (DFBrowser_QThread*)(sender());
  if (myIsFinishProcessing)
  {
    // if thread send signal when other finished signal is processed
    if (aThread)
      myPostponedItem = aThread->getItem();
    return;
  }

  myIsFinishProcessing = true;
  if (aThread)
  {
    myStartedThreads.removeAll (aThread);
    DFBrowser_ThreadItem* anItem = aThread->getItem();
    if (anItem)
      anItem->ApplyValues();
  }

  myIsFinishProcessing = false;
  if (myPostponedItem)
  {
    myPostponedItem->ApplyValues();
    myPostponedItem = 0;
  }
}