r460: no message
[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.20 2001/01/28 19:10:18 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.addCurve (detValues, detPos, detArray.nDet());
172 #if 1
173       ezplot.ezset ("xporigin 0.67");
174       ezplot.ezset ("yporigin 0.10");
175       ezplot.ezset ("xlength  0.33");
176       ezplot.ezset ("ylength  0.90");
177                         m_pSGP->setViewport (0., 0., 1., 1.);
178 #else
179                         m_pSGP->setViewport (0.67, 0.1, 1., 1.);
180 #endif
181                         ezplot.plot (m_pSGP);
182                         delete detPos;
183                 }
184     }
185 }
186
187 bool
188 ProjectionsDialog::projectView (int iViewNumber)
189 {
190         if (iViewNumber <= m_iLastView)  // already done this view
191                 return true;
192         
193         if (iViewNumber < m_rProjections.nView()) {
194                 showView (iViewNumber);
195                 wxYield();        // update the display
196                 if (m_iTrace >= Trace::TRACE_PLOT) {
197                         ::wxUsleep(500);
198                 }
199         } else {
200                 m_state = Finished;    // so that we return TRUE below and 
201                 // that [Cancel] handler knew what to do
202         }
203     
204 #ifdef __WXMAC__
205         MacUpdateImmediately();
206 #endif
207     
208         return m_state != Cancelled;
209 }
210
211
212 // EVENT HANDLERS
213
214 void ProjectionsDialog::OnCancel (wxCommandEvent& event)
215 {
216         if ( m_state == Finished ) {
217                 // this means that the count down is already finished and we're being
218                 // shown as a modal dialog - so just let the default handler do the job
219                 event.Skip();
220         } else {
221                 // request to cancel was received, the next time Update() is called we
222                 // will handle it
223                 m_state = Cancelled;
224                 
225                 // update the button state immediately so that the user knows that the
226                 // request has been noticed
227                 m_btnAbort->Disable();
228         }
229 }
230
231
232 void 
233 ProjectionsDialog::OnPause (wxCommandEvent& event)
234 {
235         if ( m_state == Finished ) {
236                 event.Skip();
237         } else if (m_state == Continue) {
238                 m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
239                 m_pSGP->setDC (&m_memoryDC);
240                 showView (m_iLastView);
241                 m_state = Paused;
242                 m_btnPause->SetLabel (wxString("Resume"));
243                 m_pSGP->setDC (m_pDC);
244                 m_memoryDC.SelectObject(wxNullBitmap);
245         } else if (m_state == Paused) {
246                 m_state = Continue;
247                 m_btnPause->SetLabel (wxString("Pause"));
248         }
249 }
250
251 void 
252 ProjectionsDialog::OnStep (wxCommandEvent& event)
253 {
254         if ( m_state == Finished ) {
255                 event.Skip();
256         } else if (m_state == Continue) {
257                 m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
258                 m_pSGP->setDC (&m_memoryDC);
259                 showView (m_iLastView);
260                 // m_rScanner.collectProjections (m_rProjections, m_rPhantom, m_iLastView, 1, true, m_iTrace, m_pSGP);
261                 m_state = Paused;
262                 m_btnPause->SetLabel (wxString("Resume"));
263                 m_pSGP->setDC (m_pDC);
264                 m_memoryDC.SelectObject(wxNullBitmap);
265                 Refresh();
266         } else if (m_state == Paused) {
267                 m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
268                 m_pSGP->setDC (&m_memoryDC);
269                 projectView (m_iLastView + 1);
270                 m_pSGP->setDC (m_pDC);
271                 m_memoryDC.SelectObject(wxNullBitmap);
272                 Refresh();
273         }
274 }
275
276 void ProjectionsDialog::OnClose(wxCloseEvent& event)
277 {
278     if ( m_state == Uncancellable )
279                 event.Veto(TRUE);    // can't close this dialog
280     else if ( m_state == Finished )
281                 event.Skip(); // let the default handler close the window as we already terminated
282     else
283                 m_state = Cancelled;          // next Update() will notice it
284 }
285
286 void
287 ProjectionsDialog::OnPaint (wxPaintEvent& event)
288 {
289         wxPaintDC paintDC (this);
290         if (m_state == Paused) {
291                 paintDC.DrawBitmap (m_bitmap, 0, 0, false);
292         }
293 }
294
295
296 /////////////////////////////////////////////////////
297 // destruction
298
299 ProjectionsDialog::~ProjectionsDialog()
300 {
301         if ( m_parentTop )
302                 m_parentTop->Enable(TRUE);
303         
304         delete m_pSGP;
305         delete m_pSGPDriver;
306         delete m_pDC;
307 }
308