pqThreadedEventSource.cxx 4.64 KB
Newer Older
1 2 3 4 5
/*=========================================================================

   Program: ParaView
   Module:    pqThreadedEventSource.cxx

6
   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
7 8 9
   All rights reserved.

   ParaView is a free software; you can redistribute it and/or modify it
10
   under the terms of the ParaView license version 1.2. 
11

12
   See License_v1.2.txt for the full ParaView license.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
   A copy of this license can be obtained by contacting
   Kitware Inc.
   28 Corporate Drive
   Clifton Park, NY 12065
   USA

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/


#include "pqThreadedEventSource.h"

#include <QMutex>
37
#include <QWaitCondition>
38 39
#include <QThread>
#include <QString>
40 41

#include "pqEventDispatcher.h"
42

43 44 45 46 47 48
class pqThreadedEventSource::pqInternal : public QThread
{
  friend class pqThreadedEventSource;
public:
  pqInternal(pqThreadedEventSource& source)
    : Source(source), 
49
      ShouldStop(0),
50
      GotEvent(0)
51 52 53 54 55 56 57 58 59 60
    {
    }
  
  virtual void run()
    {
    Source.run();
    }
  
  pqThreadedEventSource& Source;

61
  QWaitCondition WaitCondition;
62
  int Waiting;
63

64 65
  int ShouldStop;
  int GotEvent;
66 67 68
  QString CurrentObject;
  QString CurrentCommand;
  QString CurrentArgument;
69 70 71 72 73 74 75 76 77

  class ThreadHelper : public QThread
  {
    public:
      static void msleep(int msecs)
        {
        QThread::msleep(msecs);
        }
  };
78 79
};

80 81
pqThreadedEventSource::pqThreadedEventSource(QObject* p)
  : pqEventSource(p)
82 83 84 85 86 87
{
  this->Internal = new pqInternal(*this);
}

pqThreadedEventSource::~pqThreadedEventSource()
{
88 89
  // wait a second for this thread to finish, if it hasn't yet
  this->Internal->wait(1000);
90 91 92 93
  delete this->Internal;
}


94
int pqThreadedEventSource::getNextEvent(
95 96 97 98
    QString& object,
    QString& command,
    QString& arguments)
{
99 100 101

  while(!this->Internal->GotEvent)
    {
102 103
    // wait for the other thread to post an event, while
    // we keep the GUI alive.
104
    pqEventDispatcher::processEventsAndWait(100);
105
    }
106 107 108 109

  object = this->Internal->CurrentObject;
  command = this->Internal->CurrentCommand;
  arguments = this->Internal->CurrentArgument;
110
  this->Internal->GotEvent = 0;
111 112
  this->guiAcknowledge();
  
113 114
  if(object == QString::null)
    {
115 116 117 118 119
    if(arguments == "failure")
      {
      return FAILURE;
      }
    return DONE;
120 121
    }

122
  return SUCCESS;
123 124
}

125
void pqThreadedEventSource::relayEvent(QString object, QString command, QString arguments)
126
{
127 128 129
  this->Internal->CurrentObject = object;
  this->Internal->CurrentCommand = command;
  this->Internal->CurrentArgument = arguments;
130
  this->Internal->GotEvent = 1;
131 132 133
}


134
bool pqThreadedEventSource::postNextEvent(const QString& object,
135 136 137
                   const QString& command,
                   const QString& argument)
{
138 139 140 141 142
  QMetaObject::invokeMethod(this, "relayEvent", Qt::QueuedConnection, 
                            Q_ARG(QString, object),
                            Q_ARG(QString, command),
                            Q_ARG(QString, argument));

143
  return this->waitForGUI();
144 145 146 147
}

void pqThreadedEventSource::start()
{
148
  this->Internal->ShouldStop = 0;
149
  this->Internal->start(QThread::LowestPriority);
150
}
151

152 153 154
void pqThreadedEventSource::stop()
{
  this->Internal->ShouldStop = 1;
155
  this->Internal->wait();
156 157
}

158
bool pqThreadedEventSource::waitForGUI()
159
{
160
  this->Internal->Waiting = 1;
161

162 163
  while(this->Internal->Waiting == 1 &&
        this->Internal->ShouldStop == 0)
164
    {
165
    pqInternal::ThreadHelper::msleep(50);
166
    }
167
  
168
  this->Internal->Waiting = 0;
169

170 171 172 173 174
  return !this->Internal->ShouldStop;
}

void pqThreadedEventSource::guiAcknowledge()
{
175
  while(this->Internal->Waiting == 0)
176
    {
177
    pqInternal::ThreadHelper::msleep(50);
178 179
    }
  
180
  this->Internal->Waiting = 0;
181 182
}

183 184 185 186 187
void pqThreadedEventSource::msleep(int msec)
{
  pqInternal::ThreadHelper::msleep(msec);
}

188
void pqThreadedEventSource::done(int success)
189
{
190 191 192 193 194 195
  if(success == 0)
    {
    this->postNextEvent(QString(), QString(), QString());
    return;
    }
  this->postNextEvent(QString(), QString(), "failure");
196 197
}