1 /*****************************************************************************
5 ** Purpose: Document routines for CTSim program
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: July 2000
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2001 Kevin Rosenberg
12 ** $Id: docs.cpp,v 1.26 2001/02/23 18:56:56 kevin Exp $
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.
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.
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 ******************************************************************************/
29 // #pragma implementation
32 #include "wx/wxprec.h"
37 #include "wx/txtstrm.h"
39 #include "wx/thread.h"
41 #if !wxUSE_DOC_VIEW_ARCHITECTURE
42 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
49 #include "threadrecon.h"
54 IMPLEMENT_DYNAMIC_CLASS(ImageFileDocument, wxDocument)
56 bool ImageFileDocument::OnSaveDocument(const wxString& filename)
58 if (! m_pImageFile->fileWrite (filename)) {
59 *theApp->getLog() << "Unable to write image file " << filename << "\n";
62 if (theApp->getVerboseLogging())
63 *theApp->getLog() << "Wrote image file " << filename << "\n";
68 bool ImageFileDocument::OnOpenDocument(const wxString& filename)
70 if (! OnSaveModified())
73 if (! m_pImageFile->fileRead (filename.c_str())) {
74 *theApp->getLog() << "Unable to read image file " << filename << "\n";
75 m_bBadFileOpen = true;
78 if (theApp->getVerboseLogging())
79 *theApp->getLog() << "Read image file " << filename << "\n";
80 SetFilename(filename, true);
83 getView()->OnUpdate (getView(), NULL);
84 m_bBadFileOpen = false;
90 ImageFileDocument::IsModified(void) const
92 return wxDocument::IsModified();
96 ImageFileDocument::Modify(bool mod)
98 wxDocument::Modify(mod);
102 ImageFileDocument::getView() const
104 return dynamic_cast<ImageFileView*>(GetFirstView());
109 ImageFileDocument::Revert ()
112 wxString msg ("Revert to saved ");
113 msg += GetFilename();
115 wxMessageDialog dialog (getView()->getFrame(), msg, "Are you sure?", wxYES_NO | wxNO_DEFAULT);
116 if (dialog.ShowModal() == wxID_YES) {
117 if (theApp->getVerboseLogging())
118 *theApp->getLog() << "Reverting to saved " << GetFilename() << "\n";
120 OnOpenDocument (GetFilename());
123 getView()->OnUpdate (getView(), NULL);
129 // ProjectionFileDocument
131 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileDocument, wxDocument)
132 BEGIN_EVENT_TABLE(ProjectionFileDocument, wxDocument)
133 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_ADD, ProjectionFileDocument::OnAddBackground)
134 EVT_MENU(BackgroundSupervisor::MSG_BACKGROUND_SUPERVISOR_REMOVE, ProjectionFileDocument::OnRemoveBackground)
138 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
140 if (! m_pProjectionFile->write (filename.c_str())) {
141 *theApp->getLog() << "Unable to write projection file " << filename << "\n";
144 if (theApp->getVerboseLogging())
145 *theApp->getLog() << "Wrote projection file " << filename << "\n";
150 ProjectionFileDocument::~ProjectionFileDocument()
152 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
153 i != m_vecpBackgroundSupervisors.end(); i++) {
157 m_vecpBackgroundSupervisors.clear();
158 delete m_pProjectionFile;
162 ProjectionFileDocument::OnAddBackground (wxCommandEvent& event)
167 ProjectionFileDocument::OnRemoveBackground (wxCommandEvent& event)
172 ProjectionFileDocument::addReconstructor (BackgroundSupervisor* pRecon)
174 wxCriticalSectionLocker locker (m_criticalSection);
175 m_vecpBackgroundSupervisors.push_back (pRecon);
179 ProjectionFileDocument::removeReconstructor (BackgroundSupervisor* pRecon)
181 wxCriticalSectionLocker locker (m_criticalSection);
183 for (BackgroundContainer::iterator i = m_vecpBackgroundSupervisors.begin();
184 i != m_vecpBackgroundSupervisors.end();
187 m_vecpBackgroundSupervisors.erase(i);
192 sys_error (ERR_SEVERE, "Could not find background task [ProjectionFileDocument::removeBackgroundTask");
196 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
198 if (! OnSaveModified())
201 if (! m_pProjectionFile->read (filename.c_str())) {
202 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
203 m_bBadFileOpen = true;
206 if (theApp->getVerboseLogging())
207 *theApp->getLog() << "Read projection file " << filename << "\n";
208 SetFilename(filename, true);
211 GetFirstView()->OnUpdate (GetFirstView(), NULL);
212 m_bBadFileOpen = false;
218 ProjectionFileDocument::IsModified(void) const
220 return wxDocument::IsModified();
224 ProjectionFileDocument::Modify(bool mod)
226 wxDocument::Modify(mod);
231 ProjectionFileDocument::getView() const
233 return dynamic_cast<ProjectionFileView*>(GetFirstView());
236 // PhantomFileDocument
238 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
241 PhantomFileDocument::OnOpenDocument(const wxString& filename)
243 if (! OnSaveModified())
246 wxString myFilename = filename;
247 if (wxFile::Exists (myFilename)) {
248 m_phantom.createFromFile (myFilename);
249 if (theApp->getVerboseLogging())
250 *theApp->getLog() << "Read phantom file " << filename << "\n";
252 myFilename.Replace (".phm", "");
253 m_phantom.createFromPhantom (myFilename);
255 m_namePhantom = myFilename;
256 SetFilename (myFilename, true);
257 if (m_phantom.fail()) {
258 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
259 m_bBadFileOpen = true;
262 m_idPhantom = m_phantom.id();
265 GetFirstView()->OnUpdate (GetFirstView(), NULL);
266 m_bBadFileOpen = false;
272 PhantomFileDocument::OnSaveDocument(const wxString& filename)
274 if (! m_phantom.fileWrite (filename.c_str())) {
275 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
278 if (theApp->getVerboseLogging())
279 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
285 PhantomFileDocument::IsModified(void) const
291 PhantomFileDocument::Modify(bool mod)
293 wxDocument::Modify(mod);
298 PhantomFileDocument::getView() const
300 return dynamic_cast<PhantomFileView*>(GetFirstView());
305 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
308 PlotFileDocument::OnSaveDocument(const wxString& filename)
310 m_namePlot = filename.c_str();
311 if (! m_plot.fileWrite (filename)) {
312 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
315 if (theApp->getVerboseLogging())
316 *theApp->getLog() << "Wrote plot file " << filename << "\n";
322 PlotFileDocument::OnOpenDocument(const wxString& filename)
324 if (! OnSaveModified())
327 if (! m_plot.fileRead (filename.c_str())) {
328 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
329 m_bBadFileOpen = true;
332 if (theApp->getVerboseLogging())
333 *theApp->getLog() << "Read plot file " << filename << "\n";
334 SetFilename (filename, true);
335 m_namePlot = filename.c_str();
338 GetFirstView()->OnUpdate (NULL, NULL);
339 m_bBadFileOpen = false;
346 PlotFileDocument::IsModified(void) const
348 return wxDocument::IsModified();
352 PlotFileDocument::Modify (bool mod)
354 wxDocument::Modify(mod);
358 PlotFileDocument::getView() const
360 return dynamic_cast<PlotFileView*>(GetFirstView());
363 //////////////////////////////////////////////////////////////////////////
367 //////////////////////////////////////////////////////////////////////////
369 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
372 TextFileDocument::OnSaveDocument(const wxString& filename)
374 TextFileView *view = getView();
375 if (! view->getTextCtrl()->SaveFile(filename))
382 TextFileDocument::OnOpenDocument(const wxString& filename)
384 TextFileView *view = getView();
386 if (! view->getTextCtrl()->LoadFile(filename)) {
387 m_bBadFileOpen = true;
391 SetFilename (filename, true);
394 m_bBadFileOpen = false;
399 TextFileDocument::IsModified(void) const
403 TextFileView *view = getView();
406 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
408 return wxDocument::IsModified();
413 TextFileDocument::getView() const
415 return dynamic_cast<TextFileView*>(GetFirstView());
419 TextFileDocument::getTextCtrl()
421 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
424 //////////////////////////////////////////////////////////////////////////
426 // Graph3dFileDocument
428 //////////////////////////////////////////////////////////////////////////
432 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
434 Graph3dFileDocument::Graph3dFileDocument(void)
435 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
439 Graph3dFileDocument::~Graph3dFileDocument()
441 // delete [] m_pVertices;
442 // delete [] m_pNormals;
446 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
453 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
455 SetFilename (filename, true);
458 m_bBadFileOpen = false;
463 Graph3dFileDocument::IsModified(void) const
465 return wxDocument::IsModified();
470 Graph3dFileDocument::getView() const
472 return dynamic_cast<Graph3dFileView*>(GetFirstView());
476 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
478 // delete [] m_pVertices;
479 // delete [] m_pNormals;
482 m_nx = rImageFile.nx();
483 m_ny = rImageFile.ny();
484 m_array = rImageFile.getArray();
489 #endif // wxUSE_GLCANVAS