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