1 /*****************************************************************************
4 ** Name: dlgprojections.cpp
5 ** Purpose: Projection Collection Animation Dialog
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: August 2000
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2009 Kevin Rosenberg
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.
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.
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 ******************************************************************************/
26 #include "wx/wxprec.h"
31 #include "wx/button.h"
32 #include "wx/stattext.h"
33 #include "wx/layout.h"
36 #include "wx/settings.h"
37 #include "wx/dcclient.h"
41 #include "dlgprojections.h"
45 static const int LAYOUT_X_MARGIN = 4;
46 static const int LAYOUT_Y_MARGIN = 4;
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)
56 IMPLEMENT_CLASS(ProjectionsDialog, wxDialog)
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)
66 while ( m_parentTop && m_parentTop->GetParent() )
67 m_parentTop = m_parentTop->GetParent();
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);
74 wxSize sizeBtn = wxButton::GetDefaultSize();
75 c->width.Absolute(sizeBtn.x);
76 c->height.Absolute(sizeBtn.y);
78 m_btnAbort->SetConstraints(c);
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);
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);
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);
104 if (m_iTrace >= Trace::TRACE_PLOT)
107 m_iClientX = sizeDlg.x;
108 m_iClientY = sizeDlg.y;
109 SetClientSize(sizeDlg);
111 Centre(wxCENTER_FRAME | wxBOTH);
114 m_parentTop->Enable(FALSE);
117 Enable(TRUE); // enable this window
119 m_bitmap.Create (m_iClientX, m_iClientY); // save a copy of screen
120 m_pDC = dynamic_cast<wxDC*> (new wxClientDC (this));
122 this->GetClientSize(&x, &y);
123 m_pSGPDriver = new SGPDriver (m_pDC, x, y);
124 m_pSGP = new SGP (*m_pSGPDriver);
126 wxYield(); // Update the display
128 m_pSGP->setTextPointSize(7);
130 // MacUpdateImmediately();
135 ProjectionsDialog::showView (int iViewNumber)
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();
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++)
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());
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.);
170 m_pSGP->setViewport (0.67, 0.1, 1., 1.);
172 ezplot.plot (m_pSGP);
179 ProjectionsDialog::projectView (int iViewNumber)
181 if (iViewNumber <= m_iLastView) // already done this view
184 if (iViewNumber < m_rProjections.nView()) {
185 showView (iViewNumber);
186 wxYield(); // update the display
187 if (m_iTrace >= Trace::TRACE_PLOT) {
191 m_state = Finished; // so that we return TRUE below and
192 // that [Cancel] handler knew what to do
196 // MacUpdateImmediately();
199 return m_state != Cancelled;
205 void ProjectionsDialog::OnCancel (wxCommandEvent& event)
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
212 // request to cancel was received, the next time Update() is called we
216 // update the button state immediately so that the user knows that the
217 // request has been noticed
218 m_btnAbort->Disable();
224 ProjectionsDialog::OnPause (wxCommandEvent& event)
226 if ( m_state == Finished ) {
228 } else if (m_state == Continue) {
229 m_memoryDC.SelectObject (m_bitmap); // in memoryDC
230 m_pSGP->setDC (&m_memoryDC);
231 showView (m_iLastView);
233 m_btnPause->SetLabel (_T("Resume"));
234 m_pSGP->setDC (m_pDC);
235 m_memoryDC.SelectObject(wxNullBitmap);
236 } else if (m_state == Paused) {
238 m_btnPause->SetLabel (_T("Pause"));
243 ProjectionsDialog::OnStep (wxCommandEvent& event)
245 if ( m_state == Finished ) {
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);
253 m_btnPause->SetLabel (_T("Resume"));
254 m_pSGP->setDC (m_pDC);
255 m_memoryDC.SelectObject(wxNullBitmap);
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);
267 void ProjectionsDialog::OnClose(wxCloseEvent& event)
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
274 m_state = Cancelled; // next Update() will notice it
278 ProjectionsDialog::OnPaint (wxPaintEvent& event)
280 wxPaintDC paintDC (this);
281 if (m_state == Paused) {
282 paintDC.DrawBitmap (m_bitmap, 0, 0, false);
287 /////////////////////////////////////////////////////
290 ProjectionsDialog::~ProjectionsDialog()
293 m_parentTop->Enable(TRUE);