Applied initial patches for wx2.8 compatibility
[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$
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 #include "wx/wxprec.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/utils.h"
32 #include "wx/frame.h"
33 #include "wx/button.h"
34 #include "wx/stattext.h"
35 #include "wx/layout.h"
36 #include "wx/event.h"
37 #include "wx/intl.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
40 #include "wx/timer.h"
41 #endif
42
43 #include "dlgprojections.h"
44 #include "ct.h"
45
46
47 static const int LAYOUT_X_MARGIN = 4;
48 static const int LAYOUT_Y_MARGIN = 4;
49
50 BEGIN_EVENT_TABLE(ProjectionsDialog, wxDialog)
51   EVT_BUTTON(wxID_CANCEL, ProjectionsDialog::OnCancel)
52   EVT_BUTTON(ProjectionsDialog::ID_BTN_PAUSE, ProjectionsDialog::OnPause)
53   EVT_BUTTON(ProjectionsDialog::ID_BTN_STEP, ProjectionsDialog::OnStep)
54   EVT_CLOSE(ProjectionsDialog::OnClose)
55   EVT_PAINT(ProjectionsDialog::OnPaint)
56   END_EVENT_TABLE()
57
58   IMPLEMENT_CLASS(ProjectionsDialog, wxDialog)
59
60
61   ProjectionsDialog::ProjectionsDialog (Scanner& rScanner, Projections& rProj, const Phantom& rPhantom, const int iTrace, wxWindow *parent)
62 : wxDialog(parent, -1, _T("Collect Projections"), wxDefaultPosition), m_rScanner(rScanner), m_rProjections(rProj), m_rPhantom(rPhantom),
63       m_pSGPDriver(NULL), m_pSGP(NULL), m_iTrace(iTrace), m_pDC(NULL), m_btnAbort(0), m_btnPause(0), m_btnStep(0)
64 {
65   m_state = Continue;
66   m_iLastView = -1;
67   m_parentTop = parent;
68   while ( m_parentTop && m_parentTop->GetParent() )
69     m_parentTop = m_parentTop->GetParent();
70
71   m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
72   wxLayoutConstraints* c = new wxLayoutConstraints;
73   c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
74   c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
75
76   wxSize sizeBtn = wxButton::GetDefaultSize();
77   c->width.Absolute(sizeBtn.x);
78   c->height.Absolute(sizeBtn.y);
79
80   m_btnAbort->SetConstraints(c);
81
82   m_btnPause = new wxButton (this, ID_BTN_PAUSE, _T("Pause"));
83   wxLayoutConstraints* cPause = new wxLayoutConstraints;
84   cPause->right.SameAs(this, wxRight, 3*LAYOUT_X_MARGIN + sizeBtn.x);
85   cPause->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
86   cPause->width.Absolute(sizeBtn.x);
87   cPause->height.Absolute(sizeBtn.y);
88   m_btnPause->SetConstraints(cPause);
89
90   m_btnStep = new wxButton (this, ID_BTN_STEP, _T("Step"));
91   wxLayoutConstraints* cStep = new wxLayoutConstraints;
92   cStep->right.SameAs(this, wxRight, 5*LAYOUT_X_MARGIN + sizeBtn.x * 2);
93   cStep->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
94   cStep->width.Absolute(sizeBtn.x);
95   cStep->height.Absolute(sizeBtn.y);
96   m_btnStep->SetConstraints(cStep);
97
98   SetAutoLayout(TRUE);
99   Layout();
100
101   wxSize sizeDlg (500,500);
102   if (sizeDlg.x != sizeDlg.y) {
103     sizeDlg.x = imax(sizeDlg.x,sizeDlg.y);
104     sizeDlg.y = imax(sizeDlg.x,sizeDlg.y);
105   }
106   if (m_iTrace >= Trace::TRACE_PLOT)
107     sizeDlg.x += 250;
108
109   m_iClientX = sizeDlg.x;
110   m_iClientY = sizeDlg.y;
111   SetClientSize(sizeDlg);
112
113   Centre(wxCENTER_FRAME | wxBOTH);
114
115   if ( m_parentTop )
116     m_parentTop->Enable(FALSE);
117
118   Show(TRUE);
119   Enable(TRUE); // enable this window
120
121   m_bitmap.Create (m_iClientX, m_iClientY); // save a copy of screen
122   m_pDC = dynamic_cast<wxDC*> (new wxClientDC (this));
123   int x, y;
124   this->GetClientSize(&x, &y);
125   m_pSGPDriver = new SGPDriver (m_pDC, x, y);
126   m_pSGP = new SGP (*m_pSGPDriver);
127
128   wxYield();     // Update the display
129
130   m_pSGP->setTextPointSize(10);
131 #ifdef __WXMAC__
132   MacUpdateImmediately();
133 #endif
134 }
135
136 void
137 ProjectionsDialog::showView (int iViewNumber)
138 {
139   if ( iViewNumber < m_rProjections.nView() ) {
140     m_iLastView = iViewNumber;
141     ::wxYield();        // update the display
142     m_pSGP->eraseWindow();
143     m_btnPause->Refresh();
144     m_btnStep->Refresh();
145     m_btnAbort->Refresh();
146
147     if (m_iTrace >= Trace::TRACE_PLOT)
148       m_pSGP->setViewport (0, 0, 0.66, 1);
149     ::wxYield();        // update the display
150     m_rScanner.collectProjections (m_rProjections, m_rPhantom, iViewNumber, 1, m_rScanner.offsetView(), true, m_iTrace, m_pSGP);
151     ::wxYield();        // update the display
152     if (m_iTrace >= Trace::TRACE_PLOT) {
153       const DetectorArray& detArray = m_rProjections.getDetectorArray (iViewNumber);
154       const DetectorValue* detValues = detArray.detValues();
155       double* detPos = new double [detArray.nDet()];
156       for (int i = 0; i < detArray.nDet(); i++)
157         detPos[i] = i;
158       EZPlot ezplot;
159       ezplot.ezset ("grid");
160       ezplot.ezset ("box");
161       ezplot.ezset ("yticks left");
162       ezplot.ezset ("xticks major 5");
163       ezplot.ezset ("yticks major 10");
164       ezplot.addCurve (detValues, detPos, detArray.nDet());
165 #if 1
166       ezplot.ezset ("xporigin 0.67");
167       ezplot.ezset ("yporigin 0.10");
168       ezplot.ezset ("xlength  0.33");
169       ezplot.ezset ("ylength  0.90");
170       m_pSGP->setViewport (0., 0., 1., 1.);
171 #else
172       m_pSGP->setViewport (0.67, 0.1, 1., 1.);
173 #endif
174       ezplot.plot (m_pSGP);
175       delete detPos;
176     }
177   }
178 }
179
180 bool
181 ProjectionsDialog::projectView (int iViewNumber)
182 {
183   if (iViewNumber <= m_iLastView)  // already done this view
184     return true;
185
186   if (iViewNumber < m_rProjections.nView()) {
187     showView (iViewNumber);
188     wxYield();        // update the display
189     if (m_iTrace >= Trace::TRACE_PLOT) {
190       ::wxMilliSleep(500);
191     }
192   } else {
193     m_state = Finished;    // so that we return TRUE below and
194     // that [Cancel] handler knew what to do
195   }
196
197 #ifdef __WXMAC__
198   MacUpdateImmediately();
199 #endif
200
201   return m_state != Cancelled;
202 }
203
204
205 // EVENT HANDLERS
206
207 void ProjectionsDialog::OnCancel (wxCommandEvent& event)
208 {
209   if ( m_state == Finished ) {
210     // this means that the count down is already finished and we're being
211     // shown as a modal dialog - so just let the default handler do the job
212     event.Skip();
213   } else {
214     // request to cancel was received, the next time Update() is called we
215     // will handle it
216     m_state = Cancelled;
217
218     // update the button state immediately so that the user knows that the
219     // request has been noticed
220     m_btnAbort->Disable();
221   }
222 }
223
224
225 void
226 ProjectionsDialog::OnPause (wxCommandEvent& event)
227 {
228   if ( m_state == Finished ) {
229     event.Skip();
230   } else if (m_state == Continue) {
231     m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
232     m_pSGP->setDC (&m_memoryDC);
233     showView (m_iLastView);
234     m_state = Paused;
235     m_btnPause->SetLabel (_T("Resume"));
236     m_pSGP->setDC (m_pDC);
237     m_memoryDC.SelectObject(wxNullBitmap);
238   } else if (m_state == Paused) {
239     m_state = Continue;
240     m_btnPause->SetLabel (_T("Pause"));
241   }
242 }
243
244 void
245 ProjectionsDialog::OnStep (wxCommandEvent& event)
246 {
247   if ( m_state == Finished ) {
248     event.Skip();
249   } else if (m_state == Continue) {
250     m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
251     m_pSGP->setDC (&m_memoryDC);
252     showView (m_iLastView);
253     // m_rScanner.collectProjections (m_rProjections, m_rPhantom, m_iLastView, 1, true, m_iTrace, m_pSGP);
254     m_state = Paused;
255     m_btnPause->SetLabel (_T("Resume"));
256     m_pSGP->setDC (m_pDC);
257     m_memoryDC.SelectObject(wxNullBitmap);
258     Refresh();
259   } else if (m_state == Paused) {
260     m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
261     m_pSGP->setDC (&m_memoryDC);
262     projectView (m_iLastView + 1);
263     m_pSGP->setDC (m_pDC);
264     m_memoryDC.SelectObject(wxNullBitmap);
265     Refresh();
266   }
267 }
268
269 void ProjectionsDialog::OnClose(wxCloseEvent& event)
270 {
271   if ( m_state == Uncancellable )
272     event.Veto(TRUE);    // can't close this dialog
273   else if ( m_state == Finished )
274     event.Skip(); // let the default handler close the window as we already terminated
275   else
276     m_state = Cancelled;          // next Update() will notice it
277 }
278
279 void
280 ProjectionsDialog::OnPaint (wxPaintEvent& event)
281 {
282   wxPaintDC paintDC (this);
283   if (m_state == Paused) {
284     paintDC.DrawBitmap (m_bitmap, 0, 0, false);
285   }
286 }
287
288
289 /////////////////////////////////////////////////////
290 // destruction
291
292 ProjectionsDialog::~ProjectionsDialog()
293 {
294   if ( m_parentTop )
295     m_parentTop->Enable(TRUE);
296
297   delete m_pSGP;
298   delete m_pSGPDriver;
299   delete m_pDC;
300 }
301