MorphoGraphX  2.0-1-227
Process.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef PROCESS_HPP
12 #define PROCESS_HPP
13 
20 #include <Config.hpp>
21 #include <GL.hpp>
22 
23 #include <QIcon>
24 #include <QSharedPointer>
25 #include <QStringList>
26 #include <QTextStream>
27 
28 #include <MGXCamera.hpp>
29 #include <ProcessParms.hpp>
30 #include <Forall.hpp>
31 #include <Vector.hpp>
32 #include <Attributes.hpp>
33 #include <Information.hpp>
34 #include <Clip.hpp>
35 #include <CuttingSurface.hpp>
36 #include <Mesh.hpp>
37 #include <Stack.hpp>
38 #include <Store.hpp>
39 
40 #include <algorithm>
41 #include <iostream>
42 #include <typeinfo>
43 #include <tr1/memory>
44 
45 class QWidget;
46 class MorphoGraphX;
47 
57 #define PROCESS_VERSION 1380
58 
103 namespace mgx
104 {
189  #ifndef DOXYGEN
190  enum SystemCommand { UPDATE_VIEWER, LOAD_VIEW, SAVE_VIEW, SET_CURRENT_STACK,
191  TAKE_SNAPSHOT, UPDATE_STATE, SET_STATUS };
192 
193  class PrivateProcess;
194  class SetupProcess;
195  #endif
196 
204  class mgx_EXPORT UserCancelException : public std::exception {
205  public:
206  UserCancelException() : std::exception() {}
207 
208  const char* what() const throw() {
209  return "Process canceled by user.";
210  }
211  };
212 
219  class mgx_EXPORT Process : public QObject, public virtual ProcessParms
220  {
221  Q_OBJECT
222  friend class SetupProcess;
223  public:
224  static unsigned int processVersion;
225 
226  typedef std::vector<Stack*>::iterator stack_iterator;
227  typedef std::vector<Stack*>::const_iterator const_stack_iterator;
228 
229  typedef std::vector<Mesh*>::iterator mesh_iterator;
230  typedef std::vector<Mesh*>::const_iterator const_mesh_iterator;
231 
232  private:
236  Process();
237  public:
244  Process(const Process& p);
245  public:
249  virtual ~Process() {}
250 
259  void actingFile(const QString& filename, bool project_file = false);
260 
264  QString actingFile() const;
265 
269  QString pythonCall(const QStringList &parms) const;
270 
274  int stackCount() const;
278  Stack* stack(int i);
282  Stack* currentStack();
286  Stack* otherStack();
290  int currentStackId() const;
294  int otherStackId() const;
298  void setCurrentStackId(int i);
299  bool setCurrentStack(int id, const QString &store);
300 
301  //bool resetProject();
302 
306  bool takeSnapshot(QString filename, float overSampling = 1.0f, int width = 0, int height = 0, int quality = 95,
307  bool expand_frustum = false);
311  bool setStatus(const QString &msg, bool alsoPrint = true);
312 
316  bool systemCommand(int command, const QStringList &parms);
317 
321  MGXCamera *camera();
322 
323  //
325  std::pair<stack_iterator, stack_iterator> stacks();
327  std::pair<const_stack_iterator, const_stack_iterator> stacks() const;
328 
332  Stack* addStack();
333 
339  bool deleteStack(int i);
340 
344  int meshCount() const;
348  Mesh* mesh(int i);
352  Mesh* currentMesh();
356  Mesh* otherMesh();
360  int currentMeshId() const;
364  int otherMeshId() const;
368  void setCurrentMeshId(int i);
369 
371  std::pair<mesh_iterator, mesh_iterator> meshes();
373  std::pair<const_mesh_iterator, const_mesh_iterator> meshes() const;
374 
378  Mesh* addMesh(const Stack* stack);
379 
383  bool deleteMesh(int i);
384 
388  int selectedLabel() const;
389 
393  void setSelectedLabel(int label);
394 
398  float globalBrightness();
399 
403  float globalContrast();
404 
410  void setGlobalBrightness(float value);
411 
417  void setGlobalContrast(float value);
418 
422  bool meshSelection() const;
426  bool lineBorderSelection() const;
427 
431  bool updateState();
435  bool updateViewer();
439  bool loadView(const QString &fileName);
443  bool saveView(const QString &fileName);
444 
448  bool setErrorMessage(const QString& str);
449 
453  QString errorMessage() const;
454 
458  void setWarningMessage(const QString& str);
462  QString warningMessage() const;
463 
467  void userCancel() const { throw UserCancelException(); }
468 
473  const QString& file() const;
474 
478  Clip *clip1();
482  Clip *clip2();
486  Clip *clip3();
487 
491  const Clip *clip1() const;
495  const Clip *clip2() const;
499  const Clip *clip3() const;
500 
504  CuttingSurface *cuttingSurface();
505 
509  const CuttingSurface *cuttingSurface() const;
510 
512  //\name Methods needed to launch other processes
513 
517  Process *makeProcess(const QString &processName);
518 
522  template <typename ProcessT>
523  bool getProcess(const QString &processName, ProcessT* &p)
524  {
525  p = dynamic_cast<ProcessT*>(makeProcess(processName));
526  if(p)
527  return true;
528  else
529  return false;
530  }
531 
535  bool setCurrentParms();
536 
540  enum ProcessAction { PROCESS_RUN, PROCESS_STEP, PROCESS_REWIND } ;
541 
548  bool runProcess(Process &proc, QStringList &parms) throw();
549 
556  bool runProcess(const QString &processName, QStringList &parms) throw();
557 
559 
560  // @{ Functions to be defined by the user
561 
572  virtual bool initialize(QWidget * /*parent*/) { return true; }
573 
579  virtual bool run() { return true; }
580 
587  virtual bool finalize(QWidget * /*parent*/) { return true; }
588 
589  // @}
590 
591  static std::vector<Colorf> &LabelColors;
592 
594  //
595  //\name Methods to get and check elements
596 
601  STACK_ANY = 0,
602  STACK_NON_EMPTY = 0x01,
603  STACK_VISIBLE = 0x02,
604  STACK_EMPTY = 0x04,
605 
606  STACK_SCALED = 0x08,
607  STACK_TRANSFORMED = 0x10,
608  STACK_NON_SCALED = 0x20,
609  STACK_NON_TRANSFORMED = 0x40,
610  };
611 
616  STORE_ANY = 0,
617  STORE_NON_EMPTY = 0x0001,
618  STORE_VISIBLE = 0x0002,
619  STORE_EMPTY = 0x0004,
620 
621  STORE_LABEL = 0x0008,
622  STORE_NON_LABEL = 0x0010,
623 
624  STORE_SCALED = 0x0020,
625  STORE_TRANSFORMED = 0x0040,
626  STORE_NON_SCALED = 0x0080,
627  STORE_NON_TRANSFORMED = 0x0100,
628 
629  STORE_WORK = 0x1000,
630  STORE_MAIN = 0x2000
631  };
632 
637  MESH_ANY = 0,
638  MESH_NON_EMPTY = 0x000001,
639  MESH_VISIBLE = 0x000002,
640 
641  MESH_HEAT = 0x000004,
642  MESH_LABEL = 0x000008,
643  MESH_NORMAL = 0x000010,
644 
645  MESH_SIGNAL = 0x000020,
646  MESH_TEXTURE = 0x000040,
647  MESH_IMAGE = 0x000080,
648 
649  MESH_SHOW_MESH = 0x000100,
650  MESH_SHOW_SURF = 0x000200,
651 
652  MESH_ALL = 0x000400,
653  MESH_BORDER = 0x000800,
654  MESH_CELLMAP = 0x001000,
655 
656  MESH_CELLS = 0x002000,
657  MESH_IMG_TEX = 0x004000,
658  MESH_SCALED = 0x008000,
659  MESH_TRANSFORMED = 0x010000,
660 
661  MESH_EMPTY = 0x020000,
662  MESH_NON_CELLS = 0x040000,
663  MESH_NON_IMG_TEX = 0x080000,
664  MESH_NON_SCALED = 0x100000,
665  MESH_NON_TRANSFORMED = 0x200000,
666 
667  MESH_USE_PARENTS = 0x400000,
668  };
669 
673  enum CheckType {
674  CHECK_STACK = 0,
675  CHECK_STORE = 1,
676  CHECK_MESH = 2
677  };
678 
679  enum CheckWhich {
680  CHECK_CURRENT = -1
681  };
682 
688  class mgx_EXPORT CheckState {
689  public:
690  CheckState(const CheckState& copy);
691  CheckState(Process* process);
692 
696  CheckState& store(int checks = STORE_ANY, int which = CHECK_CURRENT);
700  CheckState& stack(int checks = STACK_ANY, int which = CHECK_CURRENT);
704  CheckState& mesh(int checks = MESH_ANY, int which = CHECK_CURRENT);
705 
709  operator bool();
710 
711  protected:
712  void setError();
713 
714  struct ProcessReqs {
715  int type;
716  int checks;
717  int which;
718  };
719 
720  QList<ProcessReqs> reqs;
722  };
723 
724  friend class Process::CheckState;
725 
745  CheckState checkState();
747 
748  PrivateProcess* p;
749 
750  protected:
752 
757  bool stackCheck(int checks, int which);
761  bool storeCheck(int checks, int which);
765  bool meshCheck(int checks, int which);
766 
770  QString stackError(int checks, int which);
774  QString storeError(int checks, int which);
778  QString meshError(int checks, int which);
780  signals:
781  void systemCommandGui(mgx::Process *, int, QStringList);
782  void systemCommandProcess(mgx::Process *, int, QStringList);
783  };
784 
785  #ifndef DOXYGEN
786 
787  //struct ProcessFactory
788  struct mgx_EXPORT ProcessFactory
789  {
790  ProcessFactory() {}
791  virtual ~ProcessFactory() {}
792 
793  virtual Process* operator()(const Process& process) = 0;
794  virtual void reload() = 0;
795  };
796 
797  //# ifdef WIN32
798  //struct mgx_EXPORT ProcessFactory;
799  //# endif
800 
801  template<typename ProcessName> struct ClassProcessFactory : public ProcessFactory
802  {
803  ClassProcessFactory() : ProcessFactory(), pPtr(0) {}
804 
805  virtual Process* operator()(const Process& process)
806  {
807  // Recycle the process object to allows variable persistence
808  if(!pPtr)
809  pPtr = new ProcessName(process);
810  return pPtr;
811  }
812  void reload()
813  {
814  // Recycle the process object to allows variable persistence
815  if(pPtr) {
816  delete pPtr;
817  pPtr = 0;
818  }
819  }
820  virtual ~ClassProcessFactory()
821  {
822  if(pPtr)
823  delete pPtr;
824  }
825 
826  ProcessName *pPtr;
827  };
828 
829  // Smart pointer to factory
830  typedef std::tr1::shared_ptr<ProcessFactory> ProcessFactoryPtr;
831 
832  struct mgx_EXPORT Registration {
833  typedef QList<ProcessFactoryPtr> factoryList;
834 
835  Registration(ProcessFactoryPtr f, const char* class_name, unsigned int compiledVersion);
836  ~Registration();
837  static factoryList& processFactories();
838 
839  ProcessFactoryPtr factory;
840  const char* classname;
841  };
842 
850  inline QList<ProcessFactoryPtr>& processFactories()
851  {
852  return Registration::processFactories();
853  }
854  #endif
855 
863  #define REGISTER_PROCESS(ClassName) \
864  mgx::Registration ClassName ## registration( \
865  mgx::ProcessFactoryPtr(new mgx::ClassProcessFactory<ClassName>()), \
866  typeid(ClassName).name(), PROCESS_VERSION)
867 
873  struct mgx_EXPORT ProcessDefinition
874  {
875  QString name;
876  QString description;
877  QStringList parmNames;
878  QStringList parmDescs;
879  QStringList parms;
880  QIcon icon;
882 
892  ProcessFactoryPtr factory;
893  };
894 
895  #ifndef DOXYGEN
896  typedef QMap<QString, ProcessDefinition> processesMap_t;
897 
898  mgx_EXPORT extern processesMap_t processes;
899  #endif
900 
903 
906  mgx_EXPORT ProcessDefinition* getProcessDefinition(const QString& processName);
907 
909  mgx_EXPORT bool getLastParms(const Process& proc, QStringList& parms);
911  mgx_EXPORT bool getDefaultParms(const Process& proc, QStringList& parms);
913  mgx_EXPORT bool saveDefaultParms(const Process& proc, const QStringList &parms);
915  mgx_EXPORT bool checkProcessParms(const Process& proc, const QStringList &parms, size_t* nbParms = 0);
917  mgx_EXPORT bool getLastParms(const QString& processName, QStringList& parms);
919  mgx_EXPORT bool getDefaultParms(const QString& processName, QStringList& parms);
921  mgx_EXPORT bool saveDefaultParms(const QString& processName, const QStringList &parms);
923  mgx_EXPORT bool checkProcessParms(const QString& processName, const QStringList &parms,
924  size_t* nbParms = 0);
926  mgx_EXPORT ProcessDefinition* getProcessDefinition(const QString& processThread, const QString& processName);
928  mgx_EXPORT QStringList listProcesses();
930  mgx_EXPORT bool getProcessText(const QString &text, QString &tab, QString &folder, QString &name);
931 
935  mgx_EXPORT bool validProcessName(const QString& processName);
936 
938  mgx_EXPORT bool stringToBool(const QString& string);
940  mgx_EXPORT bool stringToWorkStore(const QString& string);
942  mgx_EXPORT bool stringToMainStore(const QString& string);
943 
945  inline QString boolToString(bool b) { return (b ? "Yes" : "No"); }
946 
948 }
949 #endif
mgx::Process::CheckState::ProcessReqs::which
int which
Definition: Process.hpp:717
mgx::ProcessDefinition::factory
ProcessFactoryPtr factory
Process factory.
Definition: Process.hpp:892
Attributes.hpp
mgx::Process::stack_iterator
std::vector< Stack * >::iterator stack_iterator
Definition: Process.hpp:226
mgx::Process::processVersion
static unsigned int processVersion
Definition: Process.hpp:224
Vector.hpp
mgx::Process::initialize
virtual bool initialize(QWidget *)
Initialize the process.
Definition: Process.hpp:572
GL.hpp
mgx::getProcessDefinition
mgx_EXPORT ProcessDefinition * getProcessDefinition(const QString &processName)
Retrieves the process definition from the name of the process.
mgx::ProcessDefinition::parms
QStringList parms
List of parameters/parameter defaults.
Definition: Process.hpp:879
mgx::checkProcessParms
mgx_EXPORT bool checkProcessParms(const Process &proc, const QStringList &parms, size_t *nbParms=0)
Check if the parameters have enough defaults.
Information.hpp
mgx::Process::getProcess
bool getProcess(const QString &processName, ProcessT *&p)
Gets process.
Definition: Process.hpp:523
mgx::Process::ProcessAction
ProcessAction
Enumeration for process flow control.
Definition: Process.hpp:540
mgx::getLastParms
mgx_EXPORT bool getLastParms(const Process &proc, QStringList &parms)
Get the parameters for a given process.
mgx::validProcessName
mgx_EXPORT bool validProcessName(const QString &processName)
Check if the processName exist in the list of processes.
mgx::Process::CheckWhich
CheckWhich
Definition: Process.hpp:679
MGXCamera.hpp
mgx::Process::p
PrivateProcess * p
Definition: Process.hpp:748
mgx::Process::MeshCheckType
MeshCheckType
Enumeration for the bitfield that identifies mesh properties.
Definition: Process.hpp:636
mgx::stringToWorkStore
mgx_EXPORT bool stringToWorkStore(const QString &string)
Returns true if string correspond to the work store, false otherwise.
mgx::Stack
Definition: Stack.hpp:33
Mesh.hpp
mgx::CuttingSurface
Definition: CuttingSurface.hpp:27
mgx::Process::LabelColors
static std::vector< Colorf > & LabelColors
Definition: Process.hpp:591
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::Clip
Definition: Clip.hpp:20
mgx::Process::~Process
virtual ~Process()
Virtual destructor.
Definition: Process.hpp:249
mgx::ParmChoiceList
QList< QStringList > ParmChoiceList
Type of the dictionary giving the list of possible strings for each argument.
Definition: ProcessParms.hpp:31
ProcessParms.hpp
mgx::Process
Definition: Process.hpp:219
mgx::Process::userCancel
void userCancel() const
Throw an exception informing the system that the user canceled the current process.
Definition: Process.hpp:467
mgx::stringToBool
mgx_EXPORT bool stringToBool(const QString &string)
Helper function converting a string into a boolean.
mgx::Process::CheckState
Definition: Process.hpp:688
mgx::Process::const_stack_iterator
std::vector< Stack * >::const_iterator const_stack_iterator
Definition: Process.hpp:227
mgx::ProcessDefinition::icon
QIcon icon
Icon of the process.
Definition: Process.hpp:880
mgx::Process::mesh_iterator
std::vector< Mesh * >::iterator mesh_iterator
Definition: Process.hpp:229
mgx::Process::CheckState::ProcessReqs
Definition: Process.hpp:714
mgx::Process::const_mesh_iterator
std::vector< Mesh * >::const_iterator const_mesh_iterator
Definition: Process.hpp:230
Clip.hpp
mgx::stringToMainStore
mgx_EXPORT bool stringToMainStore(const QString &string)
Returns true if string correspond to the main store, false otherwise.
mgx::ProcessDefinition::parmChoice
ParmChoiceList parmChoice
List of choices for parameters.
Definition: Process.hpp:881
mgx::ProcessDefinition::parmNames
QStringList parmNames
List of named parameters of the process.
Definition: Process.hpp:877
MGXCamera
Definition: MGXCamera.hpp:34
mgx::UserCancelException::what
const char * what() const
Definition: Process.hpp:208
mgx::UserCancelException::UserCancelException
UserCancelException()
Definition: Process.hpp:206
mgx::Mesh
Definition: Mesh.hpp:54
mgx::boolToString
QString boolToString(bool b)
Helper function converting a boolean into a string.
Definition: Process.hpp:945
MorphoGraphX
Definition: MorphoGraphX.hpp:51
mgx::ProcessDefinition::process_t
Process process_t
Definition: Process.hpp:888
CuttingSurface.hpp
mgx::Process::CheckState::process
Process * process
Definition: Process.hpp:721
mgx::Process::StackCheckType
StackCheckType
Enumeration for the bitfield that identifies stack properties.
Definition: Process.hpp:600
mgx::Process::finalize
virtual bool finalize(QWidget *)
This is an optional method that is called from the main thread before the process is run.
Definition: Process.hpp:587
mgx::ProcessDefinition::name
QString name
Name of the process.
Definition: Process.hpp:875
mgx::ProcessParms
Definition: ProcessParms.hpp:33
mgx::Process::CheckType
CheckType
Enumeration of the type of checks that can be performed.
Definition: Process.hpp:673
mgx::ProcessDefinition::parmDescs
QStringList parmDescs
List of descriptions of named parameters.
Definition: Process.hpp:878
mgx::UserCancelException
Definition: Process.hpp:204
mgx::Process::CheckState::ProcessReqs::checks
int checks
Definition: Process.hpp:716
mgx::ProcessDefinition
Definition: Process.hpp:873
mgx::Process::run
virtual bool run()
Runs the process.
Definition: Process.hpp:579
mgx::getDefaultParms
mgx_EXPORT bool getDefaultParms(const Process &proc, QStringList &parms)
Get the default parameters for a given process (i.e. the ones defined by the process)
mgx::Process::CheckState::reqs
QList< ProcessReqs > reqs
Definition: Process.hpp:720
mgx::getProcessText
mgx_EXPORT bool getProcessText(const QString &text, QString &tab, QString &folder, QString &name)
Split process name into Tab/Folder/Name.
mgx::saveDefaultParms
mgx_EXPORT bool saveDefaultParms(const Process &proc, const QStringList &parms)
Save the default parameters in memory.
Store.hpp
mgx::Information::setStatus
mgx_EXPORT void setStatus(const QString &text, bool alsoPrint=true)
mgx::ProcessDefinition::description
QString description
Description of the process.
Definition: Process.hpp:876
mgx::listProcesses
mgx_EXPORT QStringList listProcesses()
Returns the list of names of the processes.
Stack.hpp
Forall.hpp
mgx::Process::CheckState::ProcessReqs::type
int type
Definition: Process.hpp:715
mgx::Process::StoreCheckType
StoreCheckType
Enumeration for the bitfield that identifies store properties.
Definition: Process.hpp:615