00001 //# Copyright (C) 2009 00002 //# Associated Universities, Inc. Washington DC, USA. 00003 //# 00004 //# This library is free software; you can redistribute it and/or modify it 00005 //# under the terms of the GNU Library General Public License as published by 00006 //# the Free Software Foundation; either version 2 of the License, or (at your 00007 //# option) any later version. 00008 //# 00009 //# This library is distributed in the hope that it will be useful, but WITHOUT 00010 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 //# License for more details. 00013 //# 00014 //# You should have received a copy of the GNU Library General Public License 00015 //# along with this library; if not, write to the Free Software Foundation, 00016 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00017 //# 00018 //# Correspondence concerning AIPS++ should be addressed as follows: 00019 //# Internet email: aips2-request@nrao.edu. 00020 //# Postal address: AIPS++ Project Office 00021 //# National Radio Astronomy Observatory 00022 //# 520 Edgemont Road 00023 //# Charlottesville, VA 22903-2475 USA 00024 //# 00025 00026 #ifndef THREADCONTROLLER_H_ 00027 #define THREADCONTROLLER_H_ 00028 00029 #include <plotms/Threads/ThreadCommunication.h> 00030 00031 namespace casa { 00032 00033 //# Forward declarations. 00034 class ProgressMonitor; 00035 class BackgroundThread; 00036 class PlotMSPlot; 00037 00038 // Typedefs for using post-thread methods. 00039 // <group> 00040 typedef void* PMSPTObject; 00041 typedef void PMSPTMethod(PMSPTObject, bool); 00042 // </group> 00043 00044 class ThreadController : public ThreadCommunication { 00045 public: 00046 // Constructor which takes the progress widget to use, and an optional 00047 // post-thread method that should be called when the thread is finished. 00048 ThreadController(ProgressMonitor* progress, 00049 PMSPTMethod postThreadMethod = NULL, 00050 PMSPTObject postThreadObject = NULL); 00051 00052 virtual ~ThreadController(); 00053 00054 //*********************************************************************** 00055 // Thread Communication Interface 00056 //*********************************************************************** 00057 00058 //Worker classes can store and error message. 00059 virtual void setError( const String& errorMessage ); 00060 // Allows the cache to set the status. 00061 virtual void setStatus(const String& status); 00062 virtual void setProgress(unsigned int progress); 00063 virtual void finished( ); 00064 // These signals are used to update the QtProgressWidget across different 00065 // threads. They shouldn't need to be used by other classes, even 00066 // children. 00067 // <group> 00068 virtual void initializeProgress(const String& operationName); 00069 virtual void setProgress(unsigned int progress, const String& status); 00070 virtual void finishProgress(); 00071 00072 // </group> 00073 00074 // Work classes can specify whether they support background operation, pause/resume, 00075 //and cancelling. 00076 virtual void setAllowedOperations(bool background, bool pauseResume, bool cancel); 00077 00078 //Callback for classes doing the work to check whether they should 00079 //cancel what they are doing or not. 00080 virtual bool wasCanceled() const; 00081 00082 //******************************************************************** 00083 // Thread Control 00084 //******************************************************************** 00085 00086 //Store the thread that will be doing the work 00087 void setWorkThread( BackgroundThread* workThread ); 00088 00089 // For when the user requests "background" for the thread. 00090 virtual void background(); 00091 00092 // For when the user requests "pause" for the thread. 00093 virtual void pause(); 00094 00095 // For when the user requests "resume" for the thread. 00096 virtual void resume(); 00097 00098 // For when the user requests "cancel" for the thread. 00099 virtual void cancel(); 00100 00101 // Executes the post-thread method as needed. Does nothing if a 00102 // post-thread method was not set in the constructor. 00103 virtual void postThreadMethod(); 00104 00105 //Returns the result of the background operation. 00106 bool getResult() const; 00107 00108 //Returns any error from the background operation. 00109 String getError() const; 00110 bool isErrorWarning() const; 00111 String getErrorTitle() const; 00112 PlotMSPlot* getPlot(); 00113 protected: 00114 00115 //Starts the thread 00116 void startThread(); 00117 00118 // Flag for whether thread was canceled or not. 00119 volatile bool wasCanceled_; 00120 00121 //Holds error messages 00122 String error; 00123 String errorTitle; 00124 bool errorWarning; 00125 00126 00127 00128 // Progress widget. 00129 ProgressMonitor* itsProgressWidget_; 00130 00131 // Method/Object to run when thread is finished. 00132 // <group> 00133 PMSPTMethod* itsPostThreadMethod_; 00134 PMSPTObject itsPostThreadObject_; 00135 // </group> 00136 00137 00138 private: 00139 ThreadController( const ThreadController& controller ); 00140 ThreadController operator=( const ThreadController& other ); 00141 00142 BackgroundThread* bgThread; 00143 00144 }; 00145 00146 } /* namespace casa */ 00147 #endif /* THREADCONTROLLER_H_ */