1 // -*- c-basic-offset: 4 -*-
2 /** @file PanoramaAlgorithm.h
3 *
4 *  @author Ippei UKAI <ippei_ukai@mac.com>
5 *
6 *  $Id$
7 *
8 *  This is free software; you can redistribute it and/or
9 *  modify it under the terms of the GNU General Public
10 *  License as published by the Free Software Foundation; either
11 *  version 2 of the License, or (at your option) any later version.
12 *
13 *  This software is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 *  General Public License for more details.
17 *
18 *  You should have received a copy of the GNU General Public License
19 *  along with this software; if not, write to the Free Software
20 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1307, USA.
21 *
22 *  Hereby the author, Ippei UKAI, grant the license of this particular file to
23 *  be relaxed to the GNU Lesser General Public License as published by the Free
24 *  Software Foundation; either version 2 of the License, or (at your option)
25 *  any later version. Please note however that when the file is linked to or
26 *  compiled with other files in this library, the GNU General Public License as
27 *  mentioned above is likely to restrict the terms of use further.
28 *
29 */
30 
31 #ifndef _ALGORITHM_PANORAMAALGORITHM_H
32 #define _ALGORITHM_PANORAMAALGORITHM_H
33 
34 #include <hugin_shared.h>
35 #include <appbase/ProgressDisplay.h>
36 
37 
38 namespace HuginBase {
39 
40     class PanoramaData;
41 
42 
43     /**
44      *
45      */
46     class IMPEX PanoramaAlgorithm
47     {
48 
49     protected:
50         ///
PanoramaAlgorithm(PanoramaData & panorama)51         PanoramaAlgorithm(PanoramaData& panorama)
52             : o_panorama(panorama), o_successful(false)
53             { };
54 
55     public:
56         ///
~PanoramaAlgorithm()57         virtual ~PanoramaAlgorithm() {};
58 
59 
60     public:
61         /// returns true if the algorithm changes the PanoramaData.
62         virtual bool modifiesPanoramaData() const =0;
63 
64         ///
hasRunSuccessfully()65         virtual bool hasRunSuccessfully()
66         {
67             return o_successful;
68         }
69 
70         /// runs the algorithm.
run()71         virtual void run()
72         {
73             o_successful = runAlgorithm();
74         }
75 
76 #if 0
77         /// runs the algorithm.
78         template<class AlgorithmClass>
79         AlgorithmClass& runMe()
80         {
81             AlgorithmClass& THIS = static_cast<AlgorithmClass&>(*this);
82             THIS.run();
83             return THIS;
84         }
85 #endif
86 
87         /** implementation of the algorithm.
88         *   You should override with your algorithm's implementiation.
89         */
90         virtual bool runAlgorithm() =0;
91 
92         /*
93          * Here is the informal interface guidelines that you should follow when
94          * you subclass from this class:
95          *
96          *  1. You should provide [ SomeType getSomeResult() ] methods if there
97          *   is any result from the algorithm.
98          *
99          *  2. For complicated algorithms, you can have [ MyErrorEnum getError() ]
100          *   that returns error.
101          *
102          *  3. You can optionaly implement your algorithm as
103          *   [ static SomeType executeMyAlgorithm(PanoramaData& panorama, all parameters) ].
104          *
105          */
106 
107     protected:
108         PanoramaData& o_panorama;
109         bool o_successful;
110 
111     };
112 
113 
114 
115     /**
116     *
117     */
118     class IMPEX TimeConsumingPanoramaAlgorithm : public PanoramaAlgorithm
119     {
120 
121     protected:
122         /// [Warning! it keeps the reference to the panorama data!]
123         TimeConsumingPanoramaAlgorithm(PanoramaData& panorama, AppBase::ProgressDisplay* progressDisplay = NULL)
PanoramaAlgorithm(panorama)124             : PanoramaAlgorithm(panorama),
125               m_progressDisplay(progressDisplay), m_wasCancelled(false)
126         { };
127 
128     public:
129         ///
~TimeConsumingPanoramaAlgorithm()130         virtual ~TimeConsumingPanoramaAlgorithm() {};
131 
132 
133     public:
134         /*
135          * Please follow the same guideline as the PanoramaAlgorithm class, but with:
136          *
137          *  3. should now be
138          *   [ static SomeType executeMyAlgorithm(PanoramaData& panorama, ProgressDisplay* progressDisplay, all parameters) ]
139          *   instead.
140          *
141          */
142 
143 
144     // -- access to the ProgressDisplay --
145 
146     protected:
147         ///
getProgressDisplay()148         virtual AppBase::ProgressDisplay* getProgressDisplay() const
149             { return m_progressDisplay; };
150 
151         ///
hasProgressDisplay()152         virtual bool hasProgressDisplay() const
153             { return getProgressDisplay() != NULL; };
154 
155 
156     // -- cancelling process --
157 
158     public:
159         ///
wasCancelled()160         virtual bool wasCancelled() const
161             { return m_wasCancelled; };
162 
163     protected:
164         /** Call this when the algorithm is cancelled. This method sets
165          *  wasCancelled() to return true, and calls algorithmCancelled()
166          */
cancelAlgorithm()167         virtual void cancelAlgorithm()
168         {
169             m_wasCancelled = true;
170             algorithmCancelled();
171         }
172 
173         /** Called when the algorithm got cancelled;
174          *  override with cleaning up process etc.
175          *  the default implementation does nothing.
176          */
algorithmCancelled()177         virtual void algorithmCancelled() {};
178 
179 
180     // -- private variables --
181 
182     private:
183         AppBase::ProgressDisplay* m_progressDisplay;
184         bool m_wasCancelled;
185 
186     };
187 
188 
189 }; // namespace
190 #endif // _H
191