r187: *** empty log message ***
[ctsim.git] / src / dlgreconstruct.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          dlgreconstruct.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: dlgreconstruct.cpp,v 1.1 2000/09/02 05:13:57 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 "dlgreconstruct.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 "dlgreconstruct.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(ReconstructDialog, wxDialog)
60    EVT_BUTTON(wxID_CANCEL, ReconstructDialog::OnCancel)
61    EVT_BUTTON(ID_BTN_PAUSE, ReconstructDialog::OnPause)
62    EVT_BUTTON(ID_BTN_STEP, ReconstructDialog::OnStep)
63    EVT_CLOSE(ReconstructDialog::OnClose)
64    EVT_PAINT(ReconstructDialog::OnPaint)
65 END_EVENT_TABLE()
66
67 IMPLEMENT_CLASS(ReconstructDialog, wxDialog)
68
69
70 ReconstructDialog::ReconstructDialog (Reconstructor& rReconstruct, const Projections& rProj, ImageFile& rIF, const int iTrace, wxWindow *parent)
71     : wxDialog(parent, -1, "Collect Projections"), m_rReconstructor(rReconstruct), m_rProjections(rProj), m_rImageFile(rIF), 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 (700,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
115     m_iClientX = sizeDlg.x;
116     m_iClientY = sizeDlg.y;
117     SetClientSize(sizeDlg);
118
119     m_bitmap.Create (m_iClientX, m_iClientY); // save a copy of screen
120     m_pDC = new wxMemoryDC;
121     m_pDC->SelectObject (m_bitmap);       // in memoryDC
122     m_pDC->SetFont (*wxSWISS_FONT);
123     int x, y;
124     this->GetClientSize(&x, &y);
125     m_pSGPDriver = new SGPDriver (dynamic_cast<wxDC*>(m_pDC), x, y);
126     m_pSGP = new SGP (*m_pSGPDriver);
127
128     Centre(wxCENTER_FRAME | wxBOTH);
129
130     if ( m_parentTop )
131       m_parentTop->Enable(FALSE);
132
133     Show(TRUE);
134     Enable(TRUE); // enable this window
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 ReconstructDialog::showView (int iViewNumber)
146 {
147   if ( iViewNumber < m_rProjections.nView() ) {
148     wxYield();        // update the display
149     m_iLastView = iViewNumber;
150     m_pSGP->eraseWindow();
151     if (m_iTrace >= Trace::TRACE_PLOT)
152       m_pSGP->setViewport (0, 0, 1, 1);
153     char szProgress [256];
154     snprintf (szProgress, sizeof(szProgress), "Reconstructing View %d (%.1f%%)", iViewNumber, 100 * iViewNumber / static_cast<double>(m_rProjections.nView()));
155     double wText, hText;
156     m_pSGP->setTextColor (C_LTRED, -1);
157     m_pSGP->getTextExtent (szProgress, &wText, &hText);
158     double xw_max, xw_min, yw_max, yw_min;
159     m_pSGP->getWindow (xw_min, yw_min, xw_max, yw_max);
160     m_pSGP->moveAbs ((xw_max - xw_min) / 2 + xw_min - wText / 2, yw_max - hText);
161     m_pSGP->drawText (szProgress);
162     if (m_iTrace >= Trace::TRACE_PLOT)
163       m_pSGP->setViewport (0, .1, 0.66, 1);
164     m_rReconstructor.reconstructView (iViewNumber, 1, m_pSGP);
165
166     ::wxYield();
167     m_pDC->SelectObject (wxNullBitmap);
168     Refresh();
169     m_pDC->SelectObject (m_bitmap);
170     ::wxYield();
171   }
172 }
173
174 bool
175 ReconstructDialog::reconstructView (int iViewNumber)
176 {
177   showView (iViewNumber);
178   wxYield();        // update the display
179   if (m_iTrace >= Trace::TRACE_PLOT)
180     sleep(1);
181   else {
182     m_state = Finished;    // so that we return TRUE below and 
183   }                        // that [Cancel] handler knew what to do
184   
185 #ifdef __WXMAC__
186   MacUpdateImmediately();
187 #endif
188
189   return m_state != Cancelled;
190 }
191
192
193 // EVENT HANDLERS
194
195 void ReconstructDialog::OnCancel (wxCommandEvent& event)
196 {
197   if ( m_state == Finished ) {
198     // this means that the count down is already finished and we're being
199     // shown as a modal dialog - so just let the default handler do the job
200     event.Skip();
201   } else {
202     // request to cancel was received, the next time Update() is called we
203     // will handle it
204     m_state = Cancelled;
205
206     // update the button state immediately so that the user knows that the
207     // request has been noticed
208     m_btnAbort->Disable();
209   }
210 }
211
212
213 void 
214 ReconstructDialog::OnPause (wxCommandEvent& event)
215 {
216   if ( m_state == Finished ) {
217     // this means that the count down is already finished and we're being
218     // shown as a modal dialog - so just let the default handler do the job
219     event.Skip();
220   } else {
221       if (m_state == Continue) {
222         m_state = Paused;
223         m_btnPause->SetLabel (wxString("Resume"));
224       } else if (m_state == Paused) {
225         m_state = Continue;
226         m_btnPause->SetLabel (wxString("Pause"));
227       }
228   }
229 }
230
231 void 
232 ReconstructDialog::OnStep (wxCommandEvent& event)
233 {
234   if ( m_state == Finished ) {
235     event.Skip();
236   } else {
237     if (m_state == Continue) {
238       m_state = Paused;
239       m_btnPause->SetLabel (wxString("Resume"));
240     } else if (m_state == Paused) {
241       reconstructView (m_iLastView + 1);
242     }
243   }
244 }
245
246 void ReconstructDialog::OnClose(wxCloseEvent& event)
247 {
248     if ( m_state == Uncancellable )
249       event.Veto(TRUE);    // can't close this dialog
250     else if ( m_state == Finished )
251       event.Skip(); // let the default handler close the window as we already terminated
252     else
253       m_state = Cancelled;          // next Update() will notice it
254 }
255
256 void
257 ReconstructDialog::OnPaint (wxPaintEvent& event)
258 {
259   wxPaintDC paintDC (this);
260   if (m_state == Paused) {
261     paintDC.DrawBitmap(m_bitmap, 0, 0, false);
262   }
263 }
264
265
266 /////////////////////////////////////////////////////
267 // destruction
268
269 ReconstructDialog::~ReconstructDialog()
270 {
271   if ( m_parentTop )
272     m_parentTop->Enable(TRUE);
273
274   delete m_pSGP;
275   delete m_pSGPDriver;
276   delete m_pDC;
277 }
278