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.25 2001/02/22 11:05:38 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)
134 ProjectionFileDocument::OnSaveDocument(const wxString& filename)
136 if (! m_pProjectionFile->write (filename.c_str())) {
137 *theApp->getLog() << "Unable to write projection file " << filename << "\n";
140 if (theApp->getVerboseLogging())
141 *theApp->getLog() << "Wrote projection file " << filename << "\n";
146 ProjectionFileDocument::~ProjectionFileDocument()
148 for (int i = 0; i < m_vecpActiveReconstructors.size(); i++) {
149 ThreadedReconstructor* pThreadedReconstructor = m_vecpActiveReconstructors[i];
150 if (pThreadedReconstructor) {
151 pThreadedReconstructor->cancel();
155 delete m_pProjectionFile;
159 ProjectionFileDocument::addReconstructor (ThreadedReconstructor* pRecon)
161 wxCriticalSectionLocker locker (m_criticalSection);
162 m_vecpActiveReconstructors.push_back (pRecon);
166 ProjectionFileDocument::removeReconstructor (ThreadedReconstructor* pRecon)
168 wxCriticalSectionLocker locker (m_criticalSection);
169 for (int i = 0; i < m_vecpActiveReconstructors.size(); i++) {
170 if (m_vecpActiveReconstructors[i] == pRecon)
171 m_vecpActiveReconstructors[i] = NULL;
176 ProjectionFileDocument::OnOpenDocument(const wxString& filename)
178 if (! OnSaveModified())
181 if (! m_pProjectionFile->read (filename.c_str())) {
182 *theApp->getLog() << "Unable to read projection file " << filename << "\n";
183 m_bBadFileOpen = true;
186 if (theApp->getVerboseLogging())
187 *theApp->getLog() << "Read projection file " << filename << "\n";
188 SetFilename(filename, true);
191 GetFirstView()->OnUpdate (GetFirstView(), NULL);
192 m_bBadFileOpen = false;
198 ProjectionFileDocument::IsModified(void) const
200 return wxDocument::IsModified();
204 ProjectionFileDocument::Modify(bool mod)
206 wxDocument::Modify(mod);
211 ProjectionFileDocument::getView() const
213 return dynamic_cast<ProjectionFileView*>(GetFirstView());
216 // PhantomFileDocument
218 IMPLEMENT_DYNAMIC_CLASS(PhantomFileDocument, wxDocument)
221 PhantomFileDocument::OnOpenDocument(const wxString& filename)
223 if (! OnSaveModified())
226 wxString myFilename = filename;
227 if (wxFile::Exists (myFilename)) {
228 m_phantom.createFromFile (myFilename);
229 if (theApp->getVerboseLogging())
230 *theApp->getLog() << "Read phantom file " << filename << "\n";
232 myFilename.Replace (".phm", "");
233 m_phantom.createFromPhantom (myFilename);
235 m_namePhantom = myFilename;
236 SetFilename (myFilename, true);
237 if (m_phantom.fail()) {
238 *theApp->getLog() << "Failure creating phantom " << myFilename << "\n";
239 m_bBadFileOpen = true;
242 m_idPhantom = m_phantom.id();
245 GetFirstView()->OnUpdate (GetFirstView(), NULL);
246 m_bBadFileOpen = false;
252 PhantomFileDocument::OnSaveDocument(const wxString& filename)
254 if (! m_phantom.fileWrite (filename.c_str())) {
255 *theApp->getLog() << "Unable to write phantom file " << filename << "\n";
258 if (theApp->getVerboseLogging())
259 *theApp->getLog() << "Wrote phantom file " << filename << "\n";
265 PhantomFileDocument::IsModified(void) const
271 PhantomFileDocument::Modify(bool mod)
273 wxDocument::Modify(mod);
278 PhantomFileDocument::getView() const
280 return dynamic_cast<PhantomFileView*>(GetFirstView());
285 IMPLEMENT_DYNAMIC_CLASS(PlotFileDocument, wxDocument)
288 PlotFileDocument::OnSaveDocument(const wxString& filename)
290 m_namePlot = filename.c_str();
291 if (! m_plot.fileWrite (filename)) {
292 *theApp->getLog() << "Unable to write plot file " << filename << "\n";
295 if (theApp->getVerboseLogging())
296 *theApp->getLog() << "Wrote plot file " << filename << "\n";
302 PlotFileDocument::OnOpenDocument(const wxString& filename)
304 if (! OnSaveModified())
307 if (! m_plot.fileRead (filename.c_str())) {
308 *theApp->getLog() << "Unable to read plot file " << filename << "\n";
309 m_bBadFileOpen = true;
312 if (theApp->getVerboseLogging())
313 *theApp->getLog() << "Read plot file " << filename << "\n";
314 SetFilename (filename, true);
315 m_namePlot = filename.c_str();
318 GetFirstView()->OnUpdate (NULL, NULL);
319 m_bBadFileOpen = false;
326 PlotFileDocument::IsModified(void) const
328 return wxDocument::IsModified();
332 PlotFileDocument::Modify (bool mod)
334 wxDocument::Modify(mod);
338 PlotFileDocument::getView() const
340 return dynamic_cast<PlotFileView*>(GetFirstView());
343 //////////////////////////////////////////////////////////////////////////
347 //////////////////////////////////////////////////////////////////////////
349 IMPLEMENT_DYNAMIC_CLASS(TextFileDocument, wxDocument)
352 TextFileDocument::OnSaveDocument(const wxString& filename)
354 TextFileView *view = getView();
355 if (! view->getTextCtrl()->SaveFile(filename))
362 TextFileDocument::OnOpenDocument(const wxString& filename)
364 TextFileView *view = getView();
366 if (! view->getTextCtrl()->LoadFile(filename)) {
367 m_bBadFileOpen = true;
371 SetFilename (filename, true);
374 m_bBadFileOpen = false;
379 TextFileDocument::IsModified(void) const
383 TextFileView *view = getView();
386 return (wxDocument::IsModified() || view->getTextCtrl()->IsModified());
388 return wxDocument::IsModified();
393 TextFileDocument::getView() const
395 return dynamic_cast<TextFileView*>(GetFirstView());
399 TextFileDocument::getTextCtrl()
401 return dynamic_cast<TextFileView*>(GetFirstView())->getTextCtrl();
404 //////////////////////////////////////////////////////////////////////////
406 // Graph3dFileDocument
408 //////////////////////////////////////////////////////////////////////////
412 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileDocument, wxDocument)
414 Graph3dFileDocument::Graph3dFileDocument(void)
415 : m_bBadFileOpen(false), m_nVertices(0), m_pVertices(0), m_pNormals(0),m_nx(0),m_ny(0),m_array(0)
419 Graph3dFileDocument::~Graph3dFileDocument()
421 // delete [] m_pVertices;
422 // delete [] m_pNormals;
426 Graph3dFileDocument::OnSaveDocument(const wxString& filename)
433 Graph3dFileDocument::OnOpenDocument(const wxString& filename)
435 SetFilename (filename, true);
438 m_bBadFileOpen = false;
443 Graph3dFileDocument::IsModified(void) const
445 return wxDocument::IsModified();
450 Graph3dFileDocument::getView() const
452 return dynamic_cast<Graph3dFileView*>(GetFirstView());
456 Graph3dFileDocument::createFromImageFile (const ImageFile& rImageFile)
458 // delete [] m_pVertices;
459 // delete [] m_pNormals;
462 m_nx = rImageFile.nx();
463 m_ny = rImageFile.ny();
464 m_array = rImageFile.getArray();
469 #endif // wxUSE_GLCANVAS