process.cc 9.5 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * 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 COPYRIGHT HOLDER *
 * 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.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
40 41
\*===========================================================================*/

42

Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57




//=============================================================================
//
//  CLASS Core - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================

#include "Core.hh"

58 59
#include <OpenFlipper/widgets/processManagerWidget/BlockingWidget.hh>

Jan Möbius's avatar
Jan Möbius committed
60 61 62 63
//== IMPLEMENTATION ==========================================================


// A job has been started by a plugin
64 65
void Core::slotStartJob( QString _jobId, QString _description , int _min , int _max, bool _blocking) {
  std::cerr << "StartJob: " << _jobId.toStdString() << " " << _description.toStdString() << " " << _min << " " << _max  << " " << _blocking <<std::endl;
66 67

  // Create job information
Matthias Möller's avatar
Matthias Möller committed
68 69 70 71 72 73 74 75 76
  JobInfo info;
  info.id             = _jobId;
  info.description    = _description;
  info.currentStep    =  0;
  info.minSteps       = _min;
  info.maxSteps       = _max;
  info.blocking       = _blocking;
  info.blockingWidget = 0;

77 78


Jan Möbius's avatar
Jan Möbius committed
79 80 81 82 83 84 85
  if (OpenFlipper::Options::gui()) {
    // Don't show process status in process manager
    // if blocking is enabled
    if(_blocking) {
      // Create blocking widget
      BlockingWidget* widget = new BlockingWidget(_jobId, _description,
              _min, _max);
86

Jan Möbius's avatar
Jan Möbius committed
87 88 89
      // Connect cancel button to local slot for further treatment
      connect(widget,     SIGNAL(cancelRequested(QString)),
              this,       SLOT(slotJobCancelRequested(QString)));
90

Matthias Möller's avatar
Matthias Möller committed
91
      info.blockingWidget = widget;
92

Jan Möbius's avatar
Jan Möbius committed
93 94
      int x = (coreWidget_->width()  / 2) - (widget->width()  / 2);
      int y = (coreWidget_->height() / 2) - (widget->height() / 2);
95

Jan Möbius's avatar
Jan Möbius committed
96
      widget->setGeometry( x, y, widget->width(), widget->height());
97

Jan Möbius's avatar
Jan Möbius committed
98 99
      // Show blocking widget
      widget->show();
100

Jan Möbius's avatar
Jan Möbius committed
101 102 103 104 105 106 107 108 109
    } else {
      // Create process manager window if it has not been created before
      if(!processManager_) {
          processManager_ = new ProcessManagerWidget();

          // Connect cancel buttons to local slot for further treatment
          connect(processManager_, SIGNAL(cancelJobRequested(QString)),
                  this,            SLOT(slotJobCancelRequested(QString)));
      }
110

Jan Möbius's avatar
Jan Möbius committed
111 112
      // Add new item
      processManager_->addJob(_jobId, _description, _min, _max);
113

Jan Möbius's avatar
Jan Möbius committed
114 115 116
      // Show window
      processManager_->show();
    }
117
  }
Matthias Möller's avatar
Matthias Möller committed
118 119 120

  // Add job to local job list
  currentJobs.push_back(info);
121 122 123 124 125 126
}

//-----------------------------------------------------------------------------
bool Core::getJob(QString _jobId, int& _index) {
  
  for ( int i = 0 ; i < currentJobs.size() ; ++i) {
Matthias Möller's avatar
Matthias Möller committed
127
      if ( currentJobs[i].id == _jobId ) {
128 129 130 131 132 133 134 135
        _index = i;
        return true;
      }
  }
  
  emit log(LOGERR,tr("Unable to find Job %1.").arg(_jobId));
  _index = -1;
  return false;
Jan Möbius's avatar
Jan Möbius committed
136 137 138 139 140 141
}

//-----------------------------------------------------------------------------

// A job state has been updated by a plugin
void Core::slotSetJobState(QString _jobId, int _value ) {
142 143 144
  int id;
  
  if (  getJob(_jobId, id) ) {
Matthias Möller's avatar
Matthias Möller committed
145
    currentJobs[id].currentStep = _value;
146

Jan Möbius's avatar
Jan Möbius committed
147 148
    if (!OpenFlipper::Options::gui())
        return;
149
    // Update gui
Matthias Möller's avatar
Matthias Möller committed
150
    if(!currentJobs[id].blocking)
151 152 153
    	processManager_->updateStatus(_jobId, _value);
    else {
    	BlockingWidget* w = 0;
Matthias Möller's avatar
Matthias Möller committed
154
    	w = dynamic_cast<BlockingWidget*>(currentJobs[id].blockingWidget);
155 156 157 158
    	if(w != 0) {
    		w->updateStatus(_value);
    	}
    }
159
  }
Jan Möbius's avatar
Jan Möbius committed
160 161 162 163
}

//-----------------------------------------------------------------------------

164 165 166 167 168
// A job's caption has been updated by a plugin
void Core::slotSetJobName(QString _jobId, QString _name ) {
    int id;
    
    if (  getJob(_jobId, id) ) {
Matthias Möller's avatar
Matthias Möller committed
169
        currentJobs[id].id = _name;
170

Jan Möbius's avatar
Jan Möbius committed
171 172
        if (!OpenFlipper::Options::gui())
            return;
173
        // Update gui
Matthias Möller's avatar
Matthias Möller committed
174
        if(!currentJobs[id].blocking)
175 176 177
        	processManager_->setJobName(_jobId, _name);
        else {
           	BlockingWidget* w = 0;
Matthias Möller's avatar
Matthias Möller committed
178
           	w = dynamic_cast<BlockingWidget*>(currentJobs[id].blockingWidget);
179 180 181 182
           	if(w != 0) {
           		w->setJobId(_name);
           	}
        }
183 184 185 186 187 188 189 190 191
    }
}
//-----------------------------------------------------------------------------

// A job's widget's status text has been updated by a plugin
void Core::slotSetJobDescription(QString _jobId, QString _text ) {
    int id;
    
    if (  getJob(_jobId, id) ) {
Matthias Möller's avatar
Matthias Möller committed
192
        currentJobs[id].description = _text;
193

Jan Möbius's avatar
Jan Möbius committed
194 195
        if (!OpenFlipper::Options::gui())
            return;
196
        // Update gui
Matthias Möller's avatar
Matthias Möller committed
197
        if(!currentJobs[id].blocking)
198 199 200
        	processManager_->setJobDescription(_jobId, _text);
        else {
           	BlockingWidget* w = 0;
Matthias Möller's avatar
Matthias Möller committed
201
           	w = dynamic_cast<BlockingWidget*>(currentJobs[id].blockingWidget);
202 203 204 205
            if(w != 0) {
            	w->setJobDescription(_text);
            }
        }
206 207 208 209 210
    }
}

//-----------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
211 212
// A job state has been canceled by a plugin
void Core::slotCancelJob(QString _jobId ) {
213 214 215
  int id;
  
  if (  getJob(_jobId, id) ) {
216

Jan Möbius's avatar
Jan Möbius committed
217 218
    if (OpenFlipper::Options::gui()) {
        // Update gui
Matthias Möller's avatar
Matthias Möller committed
219
        if(!currentJobs[id].blocking)
Jan Möbius's avatar
Jan Möbius committed
220 221 222
            processManager_->removeJob(_jobId);
        else {
            BlockingWidget* w = 0;
Matthias Möller's avatar
Matthias Möller committed
223
            w = dynamic_cast<BlockingWidget*>(currentJobs[id].blockingWidget);
Jan Möbius's avatar
Jan Möbius committed
224 225 226 227 228 229
            if(w != 0) {
                w->hide();
                delete w;
            }
        }
    }
230 231

	currentJobs.removeAt(id);
232
  }
Jan Möbius's avatar
Jan Möbius committed
233 234 235 236 237 238
}

//-----------------------------------------------------------------------------

// A job state has been finished by a plugin
void Core::slotFinishJob(QString _jobId ) {
239 240 241
  int id;
  
  if (  getJob(_jobId, id) ) {
242

Jan Möbius's avatar
Jan Möbius committed
243 244
    if (OpenFlipper::Options::gui()) {
        // Update gui
Matthias Möller's avatar
Matthias Möller committed
245
        if(!currentJobs[id].blocking) {
Jan Möbius's avatar
Jan Möbius committed
246 247 248 249 250 251 252
            processManager_->removeJob(_jobId);

            // Hide widget if there's no job left
            if(processManager_->getNumJobs() == 0) processManager_->hide();

        } else {
            BlockingWidget* w = 0;
Matthias Möller's avatar
Matthias Möller committed
253
            w = dynamic_cast<BlockingWidget*>(currentJobs[id].blockingWidget);
Jan Möbius's avatar
Jan Möbius committed
254 255 256 257 258 259
            if(w != 0) {
                w->hide();
                delete w;
            }
        }
    }
260

261 262 263 264
    currentJobs.removeAt(id);
  }
}

265
//-----------------------------------------------------------------------------
Jan Möbius's avatar
Jan Möbius committed
266

Mike Kremer's avatar
Mike Kremer committed
267
// A job will be canceled by user request
268
void Core::slotJobCancelRequested(QString _jobId ) {
269
    
270 271 272
  std::cerr << "Cancel requested!" << std::endl;
  
  emit jobCanceled(_jobId);
273
    // Cancel job still to be implemented...
274
	
275
}
Jan Möbius's avatar
Jan Möbius committed
276 277

//=============================================================================