r471: Fixed shutdown crash, added Revert
[ctsim.git] / src / dlgprojections.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          dlgprojections.cpp
5 **   Purpose:       Projection Collection Animation Dialog
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  August 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (c) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: dlgprojections.cpp,v 1.22 2001/01/30 05:05:41 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #ifdef __GNUG__
29 #pragma implementation "dlgprojections.h"
30 #endif
31
32 // For compilers that support precompilation, includes "wx.h".
33 #include "wx/wxprec.h"
34
35 #ifdef __BORLANDC__
36 #pragma hdrstop
37 #endif
38
39 #ifndef WX_PRECOMP
40 #include "wx/utils.h"
41 #include "wx/frame.h"
42 #include "wx/button.h"
43 #include "wx/stattext.h"
44 #include "wx/layout.h"
45 #include "wx/event.h"
46 #include "wx/intl.h"
47 #include "wx/settings.h"
48 #include "wx/dcclient.h"
49 #include "wx/timer.h"
50 #endif
51
52 #include "dlgprojections.h"
53 #include "ct.h"
54
55
56 static const int LAYOUT_X_MARGIN = 4;
57 static const int LAYOUT_Y_MARGIN = 4;
58
59 BEGIN_EVENT_TABLE(ProjectionsDialog, wxDialog)
60 EVT_BUTTON(wxID_CANCEL, ProjectionsDialog::OnCancel)
61 EVT_BUTTON(ProjectionsDialog::ID_BTN_PAUSE, ProjectionsDialog::OnPause)
62 EVT_BUTTON(ProjectionsDialog::ID_BTN_STEP, ProjectionsDialog::OnStep)
63 EVT_CLOSE(ProjectionsDialog::OnClose)
64 EVT_PAINT(ProjectionsDialog::OnPaint)
65 END_EVENT_TABLE()
66
67 IMPLEMENT_CLASS(ProjectionsDialog, wxDialog)
68
69
70 ProjectionsDialog::ProjectionsDialog (Scanner& rScanner, Projections& rProj, const Phantom& rPhantom, const int iTrace, wxWindow *parent)
71 : wxDialog(parent, -1, "Collect Projections", wxDefaultPosition), m_rScanner(rScanner), m_rProjections(rProj), m_rPhantom(rPhantom), 
72   m_pSGPDriver(NULL), m_pSGP(NULL), m_iTrace(iTrace), m_pDC(NULL), m_btnAbort(0), m_btnPause(0), m_btnStep(0)
73 {
74     m_state = Continue;
75     m_iLastView = -1;
76     m_parentTop = parent;
77     while ( m_parentTop && m_parentTop->GetParent() )
78         m_parentTop = m_parentTop->GetParent();
79     
80     m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
81     wxLayoutConstraints* c = new wxLayoutConstraints;
82     c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
83     c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
84         
85     wxSize sizeBtn = wxButton::GetDefaultSize();
86     c->width.Absolute(sizeBtn.x);
87     c->height.Absolute(sizeBtn.y);
88         
89     m_btnAbort->SetConstraints(c);
90         
91     m_btnPause = new wxButton (this, ID_BTN_PAUSE, wxString("Pause"));
92     wxLayoutConstraints* cPause = new wxLayoutConstraints;
93     cPause->right.SameAs(this, wxRight, 3*LAYOUT_X_MARGIN + sizeBtn.x);
94     cPause->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
95     cPause->width.Absolute(sizeBtn.x);
96     cPause->height.Absolute(sizeBtn.y);
97     m_btnPause->SetConstraints(cPause);
98         
99     m_btnStep = new wxButton (this, ID_BTN_STEP, wxString("Step"));
100     wxLayoutConstraints* cStep = new wxLayoutConstraints;
101     cStep->right.SameAs(this, wxRight, 5*LAYOUT_X_MARGIN + sizeBtn.x * 2);
102     cStep->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
103     cStep->width.Absolute(sizeBtn.x);
104     cStep->height.Absolute(sizeBtn.y);
105     m_btnStep->SetConstraints(cStep);
106         
107     SetAutoLayout(TRUE);
108     Layout();
109         
110     wxSize sizeDlg (500,500);
111     if (sizeDlg.x != sizeDlg.y) {
112                 sizeDlg.x = max(sizeDlg.x,sizeDlg.y);
113                 sizeDlg.y = max(sizeDlg.x,sizeDlg.y);
114     }
115     if (m_iTrace >= Trace::TRACE_PLOT)
116                 sizeDlg.x += 250;
117         
118     m_iClientX = sizeDlg.x;
119     m_iClientY = sizeDlg.y;
120     SetClientSize(sizeDlg);
121         
122     Centre(wxCENTER_FRAME | wxBOTH);
123         
124     if ( m_parentTop )
125                 m_parentTop->Enable(FALSE);
126         
127     Show(TRUE);
128     Enable(TRUE); // enable this window
129         
130     m_bitmap.Create (m_iClientX, m_iClientY); // save a copy of screen
131     m_pDC = dynamic_cast<wxDC*> (new wxClientDC (this));
132     int x, y;
133     this->GetClientSize(&x, &y);
134     m_pSGPDriver = new SGPDriver (m_pDC, x, y);
135     m_pSGP = new SGP (*m_pSGPDriver);
136         
137     wxYield();     // Update the display
138         
139     m_pSGP->setTextPointSize(10);
140 #ifdef __WXMAC__
141     MacUpdateImmediately();
142 #endif
143 }
144
145 void
146 ProjectionsDialog::showView (int iViewNumber)
147 {
148     if ( iViewNumber < m_rProjections.nView() ) {
149                 m_iLastView = iViewNumber;
150                 ::wxYield();        // update the display
151                 m_pSGP->eraseWindow();
152                 m_btnPause->Refresh();
153                 m_btnStep->Refresh();
154                 m_btnAbort->Refresh();
155
156                 if (m_iTrace >= Trace::TRACE_PLOT)
157                         m_pSGP->setViewport (0, 0, 0.66, 1);
158                 ::wxYield();        // update the display
159                 m_rScanner.collectProjections (m_rProjections, m_rPhantom, iViewNumber, 1, true, m_iTrace, m_pSGP);
160                 ::wxYield();        // update the display
161                 if (m_iTrace >= Trace::TRACE_PLOT) {
162                         const DetectorArray& detArray = m_rProjections.getDetectorArray (iViewNumber);
163                         const DetectorValue* detValues = detArray.detValues();
164                         double* detPos = new double [detArray.nDet()];
165                         for (int i = 0; i < detArray.nDet(); i++)
166                                 detPos[i] = i;
167                         EZPlot ezplot;
168                         ezplot.ezset ("grid");
169                         ezplot.ezset ("box");
170                         ezplot.ezset ("yticks left");
171                         ezplot.ezset ("xticks major 5");
172                         ezplot.ezset ("yticks major 10");
173                         ezplot.addCurve (detValues, detPos, detArray.nDet());
174 #if 1
175       ezplot.ezset ("xporigin 0.67");
176       ezplot.ezset ("yporigin 0.10");
177       ezplot.ezset ("xlength  0.33");
178       ezplot.ezset ("ylength  0.90");
179                         m_pSGP->setViewport (0., 0., 1., 1.);
180 #else
181                         m_pSGP->setViewport (0.67, 0.1, 1., 1.);
182 #endif
183                         ezplot.plot (m_pSGP);
184                         delete detPos;
185                 }
186     }
187 }
188
189 bool
190 ProjectionsDialog::projectView (int iViewNumber)
191 {
192         if (iViewNumber <= m_iLastView)  // already done this view
193                 return true;
194         
195         if (iViewNumber < m_rProjections.nView()) {
196                 showView (iViewNumber);
197                 wxYield();        // update the display
198                 if (m_iTrace >= Trace::TRACE_PLOT) {
199                         ::wxUsleep(500);
200                 }
201         } else {
202                 m_state = Finished;    // so that we return TRUE below and 
203                 // that [Cancel] handler knew what to do
204         }
205     
206 #ifdef __WXMAC__
207         MacUpdateImmediately();
208 #endif
209     
210         return m_state != Cancelled;
211 }
212
213
214 // EVENT HANDLERS
215
216 void ProjectionsDialog::OnCancel (wxCommandEvent& event)
217 {
218         if ( m_state == Finished ) {
219                 // this means that the count down is already finished and we're being
220                 // shown as a modal dialog - so just let the default handler do the job
221                 event.Skip();
222         } else {
223                 // request to cancel was received, the next time Update() is called we
224                 // will handle it
225                 m_state = Cancelled;
226                 
227                 // update the button state immediately so that the user knows that the
228                 // request has been noticed
229                 m_btnAbort->Disable();
230         }
231 }
232
233
234 void 
235 ProjectionsDialog::OnPause (wxCommandEvent& event)
236 {
237         if ( m_state == Finished ) {
238                 event.Skip();
239         } else if (m_state == Continue) {
240                 m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
241                 m_pSGP->setDC (&m_memoryDC);
242                 showView (m_iLastView);
243                 m_state = Paused;
244                 m_btnPause->SetLabel (wxString("Resume"));
245                 m_pSGP->setDC (m_pDC);
246                 m_memoryDC.SelectObject(wxNullBitmap);
247         } else if (m_state == Paused) {
248                 m_state = Continue;
249                 m_btnPause->SetLabel (wxString("Pause"));
250         }
251 }
252
253 void 
254 ProjectionsDialog::OnStep (wxCommandEvent& event)
255 {
256         if ( m_state == Finished ) {
257                 event.Skip();
258         } else if (m_state == Continue) {
259                 m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
260                 m_pSGP->setDC (&m_memoryDC);
261                 showView (m_iLastView);
262                 // m_rScanner.collectProjections (m_rProjections, m_rPhantom, m_iLastView, 1, true, m_iTrace, m_pSGP);
263                 m_state = Paused;
264                 m_btnPause->SetLabel (wxString("Resume"));
265                 m_pSGP->setDC (m_pDC);
266                 m_memoryDC.SelectObject(wxNullBitmap);
267                 Refresh();
268         } else if (m_state == Paused) {
269                 m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
270                 m_pSGP->setDC (&m_memoryDC);
271                 projectView (m_iLastView + 1);
272                 m_pSGP->setDC (m_pDC);
273                 m_memoryDC.SelectObject(wxNullBitmap);
274                 Refresh();
275         }
276 }
277
278 void ProjectionsDialog::OnClose(wxCloseEvent& event)
279 {
280     if ( m_state == Uncancellable )
281                 event.Veto(TRUE);    // can't close this dialog
282     else if ( m_state == Finished )
283                 event.Skip(); // let the default handler close the window as we already terminated
284     else
285                 m_state = Cancelled;          // next Update() will notice it
286 }
287
288 void
289 ProjectionsDialog::OnPaint (wxPaintEvent& event)
290 {
291         wxPaintDC paintDC (this);
292         if (m_state == Paused) {
293                 paintDC.DrawBitmap (m_bitmap, 0, 0, false);
294         }
295 }
296
297
298 /////////////////////////////////////////////////////
299 // destruction
300
301 ProjectionsDialog::~ProjectionsDialog()
302 {
303         if ( m_parentTop )
304                 m_parentTop->Enable(TRUE);
305         
306         delete m_pSGP;
307         delete m_pSGPDriver;
308         delete m_pDC;
309 }
310