pqThreadedEventSource.cxx 4.31 KB
Newer Older
Clinton Stimpson's avatar
 
Clinton Stimpson committed
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
/*=========================================================================

   Program: ParaView
   Module:    pqThreadedEventSource.cxx

   Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
   All rights reserved.

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

   See License_v1.1.txt for the full ParaView license.
   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>
Clinton Stimpson's avatar
   
Clinton Stimpson committed
37
#include <QWaitCondition>
Clinton Stimpson's avatar
 
Clinton Stimpson committed
38
39
40
#include <QThread>
#include <QString>
#include <QEventLoop>
Clinton Stimpson's avatar
   
Clinton Stimpson committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <QEvent>
#include <QApplication>

namespace
{
  class pqPlayCommandEvent : public QEvent
    {
  public:
    pqPlayCommandEvent(const QString& o, const QString& c, const QString& a)
      : QEvent(QEvent::User),
        Object(o),
        Command(c),
        Arguments(a)
    {
    }
    QString Object;
    QString Command;
    QString Arguments;
    };

}

Clinton Stimpson's avatar
 
Clinton Stimpson committed
63
64
65
66
67
68
69

class pqThreadedEventSource::pqInternal : public QThread
{
  friend class pqThreadedEventSource;
public:
  pqInternal(pqThreadedEventSource& source)
    : Source(source), 
Clinton Stimpson's avatar
   
Clinton Stimpson committed
70
      GotEvent(false)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
71
72
73
74
75
76
77
78
79
80
    {
    }
  
  virtual void run()
    {
    Source.run();
    }
  
  pqThreadedEventSource& Source;

Clinton Stimpson's avatar
   
Clinton Stimpson committed
81
  QWaitCondition WaitCondition;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
82
83
  QEventLoop Loop;

Clinton Stimpson's avatar
   
Clinton Stimpson committed
84
  bool GotEvent;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
85
86
87
88
89
  QString CurrentObject;
  QString CurrentCommand;
  QString CurrentArgument;
};

Clinton Stimpson's avatar
   
Clinton Stimpson committed
90
91
pqThreadedEventSource::pqThreadedEventSource(QObject* p)
  : pqEventSource(p)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
92
93
94
95
96
97
{
  this->Internal = new pqInternal(*this);
}

pqThreadedEventSource::~pqThreadedEventSource()
{
98
99
  // wait a second for this thread to finish, if it hasn't yet
  this->Internal->wait(1000);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
100
101
102
103
  delete this->Internal;
}


Clinton Stimpson's avatar
   
Clinton Stimpson committed
104
int pqThreadedEventSource::getNextEvent(
Clinton Stimpson's avatar
 
Clinton Stimpson committed
105
106
107
108
    QString& object,
    QString& command,
    QString& arguments)
{
Clinton Stimpson's avatar
   
Clinton Stimpson committed
109
110
111
112
113
114
  if(!this->Internal->GotEvent == true)
  {
    // wait for the other thread to post an event, while
    // we keep the GUI alive.
    this->Internal->Loop.exec();
  }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
115
116
117
118

  object = this->Internal->CurrentObject;
  command = this->Internal->CurrentCommand;
  arguments = this->Internal->CurrentArgument;
Clinton Stimpson's avatar
   
Clinton Stimpson committed
119
120
  this->Internal->GotEvent = false;
  this->Internal->WaitCondition.wakeAll();
Clinton Stimpson's avatar
 
Clinton Stimpson committed
121
122
123

  if(object == QString::null)
    {
Clinton Stimpson's avatar
   
Clinton Stimpson committed
124
125
126
127
128
    if(arguments == "failure")
      {
      return FAILURE;
      }
    return DONE;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
129
130
    }

Clinton Stimpson's avatar
   
Clinton Stimpson committed
131
  return SUCCESS;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
132
133
}

134
void pqThreadedEventSource::relayEvent(QString object, QString command, QString arguments)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
135
{
136
137
138
139
140
141
  this->Internal->CurrentObject = object;
  this->Internal->CurrentCommand = command;
  this->Internal->CurrentArgument = arguments;
  this->Internal->GotEvent = true;
  if(this->Internal->Loop.isRunning())
    {
Clinton Stimpson's avatar
   
Clinton Stimpson committed
142
    this->Internal->Loop.quit();
143
    }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
144
145
146
147
148
149
150
}


void pqThreadedEventSource::postNextEvent(const QString& object,
                   const QString& command,
                   const QString& argument)
{
Clinton Stimpson's avatar
   
Clinton Stimpson committed
151
152
  QMutex mut;
  mut.lock();
153
154
155
156
157
158
  QMetaObject::invokeMethod(this, "relayEvent", Qt::QueuedConnection, 
                            Q_ARG(QString, object),
                            Q_ARG(QString, command),
                            Q_ARG(QString, argument));

  // wait for the GUI thread to take the event and wake us up
Clinton Stimpson's avatar
   
Clinton Stimpson committed
159
  this->Internal->WaitCondition.wait(&mut);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
160
161
162
163
164
165
}

void pqThreadedEventSource::start()
{
  this->Internal->start();
}
Clinton Stimpson's avatar
   
Clinton Stimpson committed
166
167

void pqThreadedEventSource::done(int success)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
168
{
Clinton Stimpson's avatar
   
Clinton Stimpson committed
169
170
171
172
173
174
  if(success == 0)
    {
    this->postNextEvent(QString(), QString(), QString());
    return;
    }
  this->postNextEvent(QString(), QString(), "failure");
Clinton Stimpson's avatar
 
Clinton Stimpson committed
175
176
}