OpenASIP  2.0
ProDeExportCmd.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file ProDeExportCmd.cc
26  *
27  * Definition of ProDeExportCmd class.
28  *
29  * @author Veli-Pekka Jääskeläinen (vjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <wx/wx.h>
34 #include <wx/docview.h>
35 
36 #include "MachineCanvas.hh"
37 #include "ProDeExportCmd.hh"
38 #include "ProDeConstants.hh"
39 #include "ProDe.hh"
40 #include "MDFView.hh"
41 #include "WxConversion.hh"
42 #include "ErrorDialog.hh"
43 #include "FileSystem.hh"
44 
45 #if wxCHECK_VERSION(3, 0, 0)
46  #define wxSAVE wxFD_SAVE
47  #define wxOVERWRITE_PROMPT wxFD_OVERWRITE_PROMPT
48 #endif
49 
50 using std::string;
51 
52 /**
53  * The Constructor.
54  */
56  EditorCommand(ProDeConstants::CMD_NAME_EXPORT) {
57 
58 }
59 
60 
61 /**
62  * The Destructor.
63  */
65 
66 
67 /**
68  * Executes the command.
69  *
70  * @return Always false. Returning true here would cause problems with the
71  * wxCommandProcessor, and the command is never undoable so it's
72  * safe to return false even if the command was succesfully executed.
73  */
74 bool
76 
77  wxView* view = wxGetApp().docManager()->GetCurrentView();
78  MachineCanvas* canvas = dynamic_cast<MDFView*>(view)->canvas();
79 
80  assert(view != NULL && canvas != NULL);
81 
82  wxString message = _T("Export processor figure.");
83  wxString defaultDir = _T(".");
84  wxString defaultFile= _T("");
85 #if wxCHECK_VERSION(3, 0, 0)
86  wxString fileTypes = _T("Scalable Vector Graphics (.svg)|*.svg|");
87 #else
88  wxString fileTypes = _T("Encapsulated Postscript (.eps)|*.eps;*.epsi|");
89 #endif
90  fileTypes.Append(_T("Portable Network Graphics (.png)|*.png"));
91 
92  wxFileDialog dialog(
93  parentWindow(), message, defaultDir, defaultFile, fileTypes,
94  wxSAVE | wxOVERWRITE_PROMPT);
95 
96  if (dialog.ShowModal() == wxID_CANCEL) {
97  return false;
98  }
99 
100  std::string filename = WxConversion::toString(dialog.GetPath());
101  std::string extension = FileSystem::fileExtension(filename);
102  std::string creator = "TTA Processor Designer";
103  std::string title =
104  WxConversion::toString(view->GetDocument()->GetTitle());
105 
106 #if wxCHECK_VERSION(3, 0, 0)
107  if (extension == ".svg") {
108  if (!canvas->saveSVG(filename)) {
109 #else
110  if (extension == ".eps" || extension == ".epsi") {
111  if (!canvas->saveEPS(filename, title, creator)) {
112 #endif
113  wxString message = _T("Error saving file '");
114  message.Append(dialog.GetPath());
115  message.Append(_T("'."));
116  ErrorDialog errorDialog(parentWindow(), message);
117  errorDialog.ShowModal();
118  }
119  } else if (extension == ".png") {
120  if (!canvas->savePNG(filename)) {
121  wxString message = _T("Error saving file '");
122  message.Append(dialog.GetPath());
123  message.Append(_T("'."));
124  ErrorDialog errorDialog(parentWindow(), message);
125  errorDialog.ShowModal();
126  }
127  } else {
128  wxString message = _T("File type with extension '");
129  message.Append(WxConversion::toWxString(extension));
130  message.Append(_T("' is not supported."));
131  ErrorDialog dialog(parentWindow(), message);
132  dialog.ShowModal();
133  return false;
134  }
135 
136  return false;
137 }
138 
139 /**
140  * Returns id of this command.
141  *
142  * @return ID for this command to be used in menus and toolbars.
143  */
144 int
147 }
148 
149 
150 /**
151  * Creates and returns a new instance of this command.
152  *
153  * @return Newly created instance of this command.
154  */
157  return new ProDeExportCmd();
158 }
159 
160 
161 /**
162  * Returns true when the command is executable, false when not.
163  *
164  * This command is executable when a document is open.
165  *
166  * @return True, if a document is open.
167  */
168 bool
170  wxView* view = wxGetApp().docManager()->GetCurrentView();
171  if (view != NULL) {
172  return true;
173  }
174  return false;
175 }
ProDeExportCmd::isEnabled
virtual bool isEnabled()
Definition: ProDeExportCmd.cc:169
MachineCanvas
Definition: MachineCanvas.hh:64
ProDe.hh
FileSystem.hh
WxConversion::toWxString
static wxString toWxString(const std::string &source)
ProDeExportCmd::Do
virtual bool Do()
Definition: ProDeExportCmd.cc:75
MachineCanvas::saveEPS
bool saveEPS(const std::string &filename, const std::string &title, const std::string &creator="")
Definition: MachineCanvas.cc:617
assert
#define assert(condition)
Definition: Application.hh:86
ErrorDialog
Definition: ErrorDialog.hh:42
ProDeExportCmd.hh
ErrorDialog.hh
ProDeExportCmd::create
virtual ProDeExportCmd * create() const
Definition: ProDeExportCmd.cc:156
EditorCommand::view
wxView * view() const
Definition: EditorCommand.cc:76
FileSystem::fileExtension
static std::string fileExtension(const std::string &fileName)
Definition: FileSystem.cc:279
MachineCanvas.hh
ProDeConstants.hh
MDFView.hh
EditorCommand
Definition: EditorCommand.hh:46
MachineCanvas::savePNG
bool savePNG(const std::string &filename)
Definition: MachineCanvas.cc:648
WxConversion.hh
ProDeExportCmd::id
virtual int id() const
Definition: ProDeExportCmd.cc:145
ProDeExportCmd::ProDeExportCmd
ProDeExportCmd()
Definition: ProDeExportCmd.cc:55
ProDeConstants
Definition: ProDeConstants.hh:43
ProDeExportCmd::~ProDeExportCmd
virtual ~ProDeExportCmd()
Definition: ProDeExportCmd.cc:64
ProDeConstants::COMMAND_EXPORT
@ COMMAND_EXPORT
Definition: ProDeConstants.hh:414
WxConversion::toString
static std::string toString(const wxString &source)
ProDeExportCmd
Definition: ProDeExportCmd.hh:43
GUICommand::parentWindow
wxWindow * parentWindow() const
Definition: GUICommand.cc:75
MDFView
Definition: MDFView.hh:59