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