r190: *** empty log 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-2000 Kevin Rosenberg
11 **
12 **  $Id: dlgprojections.cpp,v 1.5 2000/09/04 09:24:21 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"), m_rScanner(rScanner), m_rProjections(rProj), m_rPhantom(rPhantom), m_pSGPDriver(NULL), m_pSGP(NULL), m_iTrace(iTrace), m_pDC(NULL), m_btnAbort(0), m_btnPause(0), m_btnStep(0)
72 {
73     m_state = Continue;
74     m_iLastView = -1;
75     m_parentTop = parent;
76     while ( m_parentTop && m_parentTop->GetParent() )
77         m_parentTop = m_parentTop->GetParent();
78     
79     m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
80     wxLayoutConstraints* c = new wxLayoutConstraints;
81     c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
82     c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
83
84     wxSize sizeBtn = wxButton::GetDefaultSize();
85     c->width.Absolute(sizeBtn.x);
86     c->height.Absolute(sizeBtn.y);
87
88     m_btnAbort->SetConstraints(c);
89
90     m_btnPause = new wxButton (this, ID_BTN_PAUSE, wxString("Pause"));
91     wxLayoutConstraints* cPause = new wxLayoutConstraints;
92     cPause->right.SameAs(this, wxRight, 3*LAYOUT_X_MARGIN + sizeBtn.x);
93     cPause->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
94     cPause->width.Absolute(sizeBtn.x);
95     cPause->height.Absolute(sizeBtn.y);
96     m_btnPause->SetConstraints(cPause);
97
98     m_btnStep = new wxButton (this, ID_BTN_STEP, wxString("Step"));
99     wxLayoutConstraints* cStep = new wxLayoutConstraints;
100     cStep->right.SameAs(this, wxRight, 5*LAYOUT_X_MARGIN + sizeBtn.x * 2);
101     cStep->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
102     cStep->width.Absolute(sizeBtn.x);
103     cStep->height.Absolute(sizeBtn.y);
104     m_btnStep->SetConstraints(cStep);
105
106     SetAutoLayout(TRUE);
107     Layout();
108
109     wxSize sizeDlg (500,500);
110     if (sizeDlg.x != sizeDlg.y) {
111       sizeDlg.x = max(sizeDlg.x,sizeDlg.y);
112       sizeDlg.y = max(sizeDlg.x,sizeDlg.y);
113     }
114     if (m_iTrace >= Trace::TRACE_PLOT)
115       sizeDlg.x += 250;
116
117     m_iClientX = sizeDlg.x;
118     m_iClientY = sizeDlg.y;
119     SetClientSize(sizeDlg);
120     m_bitmap.Create (m_iClientX, m_iClientY); // save a copy of screen
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_pDC = dynamic_cast<wxDC*> (new wxClientDC (this));
131     int x, y;
132     this->GetClientSize(&x, &y);
133     m_pSGPDriver = new SGPDriver (m_pDC, x, y);
134     m_pSGP = new SGP (*m_pSGPDriver);
135
136     wxYield();     // Update the display
137
138     m_pSGPDriver->idWX()->SetFont(*wxSWISS_FONT);
139 #ifdef __WXMAC__
140     MacUpdateImmediately();
141 #endif
142 }
143
144 void
145 ProjectionsDialog::showView (int iViewNumber)
146 {
147     if ( iViewNumber < m_rProjections.nView() ) {
148         ::wxYield();        // update the display
149         m_iLastView = iViewNumber;
150         if (m_iTrace >= Trace::TRACE_PLOT)
151             m_pSGP->setViewport (0, 0, 0.66, 1);
152         ::wxYield();        // update the display
153         m_rScanner.collectProjections (m_rProjections, m_rPhantom, iViewNumber, 1, true, m_iTrace, m_pSGP);
154         ::wxYield();        // update the display
155         if (m_iTrace >= Trace::TRACE_PLOT) {
156             const DetectorArray& detArray = m_rProjections.getDetectorArray (iViewNumber);
157             const DetectorValue* detValues = detArray.detValues();
158             double detPos [detArray.nDet()];
159             for (int i = 0; i < detArray.nDet(); i++)
160                 detPos[i] = i;
161             EZPlot ezplot (*m_pSGP);
162             ezplot.ezset("xporigin 0.66");
163             ezplot.ezset("yporigin 0.10");
164             ezplot.ezset("xlength  0.34");
165             ezplot.ezset("ylength  0.90");
166             ezplot.ezset("grid");
167             ezplot.ezset("box");
168             ezplot.addCurve (detValues, detPos, detArray.nDet());
169             ezplot.plot();
170         }
171     }
172 }
173
174 bool
175 ProjectionsDialog::projectView (int iViewNumber)
176 {
177   if (iViewNumber <= m_iLastView)  // already done this view
178     return true;
179
180   if (iViewNumber < m_rProjections.nView()) {
181     showView (iViewNumber);
182     wxYield();        // update the display
183     if (m_iTrace >= Trace::TRACE_PLOT) {
184       sleep(1);
185     }
186   } else {
187     m_state = Finished;    // so that we return TRUE below and 
188     // that [Cancel] handler knew what to do
189   }
190     
191 #ifdef __WXMAC__
192   MacUpdateImmediately();
193 #endif
194     
195   return m_state != Cancelled;
196 }
197
198
199 // EVENT HANDLERS
200
201 void ProjectionsDialog::OnCancel (wxCommandEvent& event)
202 {
203   if ( m_state == Finished ) {
204     // this means that the count down is already finished and we're being
205     // shown as a modal dialog - so just let the default handler do the job
206     event.Skip();
207   } else {
208     // request to cancel was received, the next time Update() is called we
209     // will handle it
210     m_state = Cancelled;
211
212     // update the button state immediately so that the user knows that the
213     // request has been noticed
214     m_btnAbort->Disable();
215   }
216 }
217
218
219 void 
220 ProjectionsDialog::OnPause (wxCommandEvent& event)
221 {
222   if ( m_state == Finished ) {
223     // this means that the count down is already finished and we're being
224     // shown as a modal dialog - so just let the default handler do the job
225     event.Skip();
226   } else {
227       if (m_state == Continue) {
228           m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
229           m_pSGP->setDC (&m_memoryDC);
230           m_memoryDC.SetFont (*wxSWISS_FONT);
231           showView (m_iLastView);
232           m_pSGP->setDC (m_pDC);
233           m_memoryDC.SelectObject(wxNullBitmap);
234           m_state = Paused;
235           m_btnPause->SetLabel (wxString("Resume"));
236       } else if (m_state == Paused) {
237           m_state = Continue;
238           m_btnPause->SetLabel (wxString("Pause"));
239       }
240   }
241 }
242
243 void 
244 ProjectionsDialog::OnStep (wxCommandEvent& event)
245 {
246   if ( m_state == Finished ) {
247     // this means that the count down is already finished and we're being
248     // shown as a modal dialog - so just let the default handler do the job
249     event.Skip();
250   } else {
251       if (m_state == Continue) {
252           m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
253           m_pSGP->setDC (&m_memoryDC);
254           m_memoryDC.SetFont (*wxSWISS_FONT);
255           m_rScanner.collectProjections (m_rProjections, m_rPhantom, m_iLastView, 1, true, m_iTrace, m_pSGP);
256           m_pSGP->setDC (m_pDC);
257           m_memoryDC.SelectObject(wxNullBitmap);
258           m_state = Paused;
259           m_btnPause->SetLabel (wxString("Resume"));
260       } else if (m_state == Paused) {
261           projectView (m_iLastView + 1);
262       }
263   }
264 }
265
266 void ProjectionsDialog::OnClose(wxCloseEvent& event)
267 {
268     if ( m_state == Uncancellable )
269       event.Veto(TRUE);    // can't close this dialog
270     else if ( m_state == Finished )
271       event.Skip(); // let the default handler close the window as we already terminated
272     else
273       m_state = Cancelled;          // next Update() will notice it
274 }
275
276 void
277 ProjectionsDialog::OnPaint (wxPaintEvent& event)
278 {
279   wxPaintDC paintDC (this);
280   if (m_state == Paused) {
281     paintDC.DrawBitmap(m_bitmap, 0, 0, false);
282   }
283 }
284
285
286 /////////////////////////////////////////////////////
287 // destruction
288
289 ProjectionsDialog::~ProjectionsDialog()
290 {
291   if ( m_parentTop )
292     m_parentTop->Enable(TRUE);
293
294   delete m_pSGP;
295   delete m_pSGPDriver;
296   delete m_pDC;
297 }
298