NCBI C++ ToolKit
wx_export_alnmulti_img_dlg.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: wx_export_alnmulti_img_dlg.cpp 44954 2020-04-27 17:57:36Z evgeniev $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Vladislav Evgeniev
27  *
28  * File Description:
29  *
30  */
31 #include <ncbi_pch.hpp>
32 ////@begin includes
33 ////@end includes
34 
36 #include <wx/filename.h>
37 #include <wx/msgdlg.h>
38 #include <wx/checkbox.h>
39 #include <wx/filedlg.h>
40 #include <wx/panel.h>
41 #include <wx/artprov.h>
42 #include <wx/valgen.h>
43 
50 #include <gui/objutils/utils.hpp>
51 #include <corelib/ncbifile.hpp>
52 
53 ////@begin XPM images
54 ////@end XPM images
55 
57 
58 IMPLEMENT_DYNAMIC_CLASS( CwxExportAlnmultiImgDlg, wxDialog )
59 
60 BEGIN_EVENT_TABLE( CwxExportAlnmultiImgDlg, wxDialog )
61 
62 ////@begin CwxExportAlnmultiImgDlg event table entries
65  EVT_BUTTON( ID_SAVE, CwxExportAlnmultiImgDlg::OnSaveClick )
66  EVT_BUTTON( ID_OPEN, CwxExportAlnmultiImgDlg::OnOpenClick )
68 ////@end CwxExportAlnmultiImgDlg event table entries
69 
71 
73 {
74  Init();
75 }
76 
78  : m_AlnMultiWidget(widget)
79  , m_OutputFormat(format)
80 {
81  Init();
82  Create(parent);
83 }
84 
85 bool CwxExportAlnmultiImgDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
86 {
87 ////@begin CwxExportAlnmultiImgDlg creation
88  SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
89  wxDialog::Create( parent, id, caption, pos, size, style );
90 
92  Centre();
93 ////@end CwxExportAlnmultiImgDlg creation
94  InvalidateBestSize();
95  Layout();
96  Fit();
98  m_FileExtension = '.' + extension;
100  wxString filename;
101  filename.Printf(_T("NCBI_MSA_%d_sequences"), m_AlnMultiWidget->GetDataSource()->GetNumRows());
102  m_FileName = filename.ToStdString();
103  wxString path = ToWxString(m_Path);
104  path += wxFileName::GetPathSeparator();
106  m_FileNameBox->SetValue(path);
107  return true;
108 }
109 
111 {
112 ////@begin CwxExportVectorImageDlg destruction
113 ////@end CwxExportVectorImageDlg destruction
114  SaveSettings();
115 }
116 
118 {
119 ////@begin CwxExportAlnmultiImgDlg member initialisation
120  m_ShowHeader = true;
121  m_SimplifiedGraphics = false;
122  m_OpenDocument = false;
123  m_Panel = NULL;
127  m_InfoSizer = NULL;
128  m_Info = NULL;
129  m_SaveButton = NULL;
130  m_OpenButton = NULL;
131 ////@end CwxExportAlnmultiImgDlg member initialisation
132  m_UserSelectedFilename = false;
133  LoadSettings();
134 }
135 
137 {
138 ////@begin CwxExportAlnmultiImgDlg content construction
139  // Generated by DialogBlocks, 27/04/2020 11:28:20 (unregistered)
140 
141  CwxExportAlnmultiImgDlg* itemDialog1 = this;
142 
143  wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
144  itemDialog1->SetSizer(itemBoxSizer2);
145 
146  m_Panel = new wxPanel( itemDialog1, ID_PANEL3, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
147  m_Panel->SetName(wxT("aaaaa"));
148  itemBoxSizer2->Add(m_Panel, 0, wxALIGN_CENTER_HORIZONTAL, 5);
149 
150  wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxVERTICAL);
151  m_Panel->SetSizer(itemBoxSizer4);
152 
153  wxBoxSizer* itemBoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
154  itemBoxSizer4->Add(itemBoxSizer5, 0, wxGROW|wxLEFT|wxRIGHT|wxTOP, 5);
155 
156  wxStaticText* itemStaticText6 = new wxStaticText( m_Panel, wxID_STATIC, _("Save As:"), wxDefaultPosition, wxSize(100, -1), wxALIGN_RIGHT );
157  itemBoxSizer5->Add(itemStaticText6, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
158 
159  m_FileNameBox = new wxTextCtrl( m_Panel, ID_TEXTCTRL5, wxEmptyString, wxDefaultPosition, wxSize(200, -1), 0 );
160  itemBoxSizer5->Add(m_FileNameBox, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
161 
162  wxBitmapButton* itemBitmapButton8 = new wxBitmapButton( m_Panel, ID_BITMAPBUTTON, itemDialog1->GetBitmapResource(wxT("menu::open")), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW );
163  itemBitmapButton8->SetHelpText(_("Select File"));
165  itemBitmapButton8->SetToolTip(_("Select File"));
166  itemBoxSizer5->Add(itemBitmapButton8, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
167 
168  wxBoxSizer* itemBoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
169  itemBoxSizer4->Add(itemBoxSizer1, 0, wxGROW|wxLEFT|wxRIGHT, 5);
170 
171  wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
172  itemBoxSizer4->Add(itemBoxSizer3, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
173 
174  m_DisplayHeaderRow = new wxCheckBox( m_Panel, ID_CHECKBOX7, _("Export header row"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT );
175  m_DisplayHeaderRow->SetValue(true);
176  m_DisplayHeaderRow->SetHelpText(_("Export the header row to the output file"));
178  m_DisplayHeaderRow->SetToolTip(_("Export the header row to the output file"));
179  itemBoxSizer3->Add(m_DisplayHeaderRow, 0, wxGROW|wxALL, 5);
180 
181  m_SaveSimplified = new wxCheckBox( m_Panel, ID_CHECKBOX6, _("Simplified graphics"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT );
182  m_SaveSimplified->SetValue(false);
183  m_SaveSimplified->SetHelpText(_("Removes gradient shading from some elements to support Adobe Illustrator"));
185  m_SaveSimplified->SetToolTip(_("Makes PDF Adobe Illustrator Compatible"));
186  itemBoxSizer3->Add(m_SaveSimplified, 0, wxGROW|wxALL, 5);
187 
188  m_InfoSizer = new wxBoxSizer(wxVERTICAL);
189  itemBoxSizer2->Add(m_InfoSizer, 0, wxGROW|wxALL, 5);
190 
191  m_Info = new wxStaticText( itemDialog1, wxID_STATIC, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
192  m_InfoSizer->Add(m_Info, 0, wxALIGN_CENTER_HORIZONTAL|wxLEFT|wxRIGHT, 5);
193 
194  itemBoxSizer2->Add(5, 1, 1, wxALIGN_CENTER_HORIZONTAL|wxALL, 1);
195 
196  wxBoxSizer* itemBoxSizer19 = new wxBoxSizer(wxHORIZONTAL);
197  itemBoxSizer2->Add(itemBoxSizer19, 0, wxALIGN_RIGHT|wxLEFT|wxRIGHT|wxBOTTOM, 5);
198 
199  m_SaveButton = new wxButton( itemDialog1, ID_SAVE, _("Save"), wxDefaultPosition, wxDefaultSize, 0 );
200  itemBoxSizer19->Add(m_SaveButton, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5);
201 
202  m_OpenButton = new wxButton( itemDialog1, ID_OPEN, _("Save && Open"), wxDefaultPosition, wxDefaultSize, 0 );
203  itemBoxSizer19->Add(m_OpenButton, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5);
204 
205  wxButton* itemButton22 = new wxButton( itemDialog1, ID_CANCEL, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
206  itemBoxSizer19->Add(itemButton22, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5);
207 
208  // Set validators
209  m_DisplayHeaderRow->SetValidator( wxGenericValidator(& m_ShowHeader) );
210  m_SaveSimplified->SetValidator( wxGenericValidator(& m_SimplifiedGraphics) );
211 ////@end CwxExportAlnmultiImgDlg content construction
212 }
213 
215 {
216  m_UserSelectedFilename = false;
217  event.Skip();
218 }
219 
220 void CwxExportAlnmultiImgDlg::OnFilepathClick( wxCommandEvent& event )
221 {
222  string full_name = ToAsciiStdString(m_FileNameBox->GetValue());
223 
224  // If full_name is just a directory
225  string path = full_name;
226  string fname;
227 
228  CFile f(ToAsciiStdString(m_FileNameBox->GetValue()));
229  // If full_name is a valid directory and (existing) file
230  if (f.IsFile()) {
231  path = f.GetDir();
232  fname = f.GetName();
233  }
234  else if (!f.IsDir()) {
235  string d = f.GetDir();
236  CDir dir(d);
237  // if file_name is valid direcotry and not-yet-created file:
238  if (dir.IsDir()) {
239  path = f.GetDir();
240  fname = f.GetName();
241  }
242  }
243 
244  wxFileDialog dlg(this, wxT("Set File Name"),
245  ToWxString(path), ToWxString(fname),
246  ToWxString(m_FileExtension), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
247 
248  if (dlg.ShowModal() != wxID_OK)
249  return;
250 
251  wxString filename = dlg.GetPath();
252  m_FileNameBox->SetValue(filename);
253  m_FileName = dlg.GetFilename();
254  m_Path = dlg.GetDirectory();
255  m_UserSelectedFilename = true;
256 }
257 
258 void CwxExportAlnmultiImgDlg::OnSaveClick(wxCommandEvent& event)
259 {
260  TransferDataFromWindow();
261 
262  // validate file path
263  wxFileName file(m_FileNameBox->GetValue());
264  wxString filename = file.GetFullName();
265  wxString n = filename.Lower();
266  size_t pos = filename.rfind(m_FileExtension);
267  if (pos != string::npos) {
268  filename = filename.SubString(0, pos - 1);
269  }
270  wxString path = file.GetPath();
271 
272  if (path.IsEmpty()) {
273  NcbiMessageBox("A directory must be specified!");
274  return;
275  }
276  else if (!wxFileName::DirExists(path)) {
277  NcbiMessageBox("The given directory doesn't exist!");
278  return;
279  }
280  else if (filename.IsEmpty()) {
281  NcbiMessageBox("A file name must be specified!");
282  return;
283  }
284 
285  m_Path = ToStdString(path);
286  m_FileName = ToStdString(filename);
287  string full_path = CDir::ConcatPath(m_Path, m_FileName + m_FileExtension);
288 
289  // Make sure directory is writable:
290  CDir dir(m_Path);
291  if (!dir.CheckAccess(CDirEntry::fWrite)) {
292  NcbiMessageBox("Error - You do not have write permission to the directory: " + m_Path);
293  return;
294  }
295 
296  if (!m_UserSelectedFilename) {
297  // Warn user if file exists. If the file was selected from the file selection
298  // dialog we do not need to do this (if not the file came from the registry and probably
299  // does already exist)
300  CFile f(full_path);
301  if (f.Exists()) {
302  if (!f.CheckAccess(CDirEntry::fWrite)) {
303  NcbiMessageBox("Error - You do not have write permission to the file: " + full_path);
304  return;
305  }
306 
307  int choice = wxMessageBox(wxT("Selected file already exists. Are you sure you want to overwrite it?"),
308  wxT("Warning"), wxYES_NO | wxICON_ERROR, this);
309  if (choice != wxYES) {
310  return;
311  }
312  }
313  }
314 
315  m_Panel->Enable(false);
316  m_OpenButton->Enable(false);
317  m_SaveButton->Enable(false);
318 
319  bool result{ false };
320  try {
321  //result = GUI_AsyncExec([this](ICanceled&) { return m_AlnMultiWidget->SaveVectorImage(m_OutputFormat, m_Path, m_FileName, m_ShowHeader, m_SimplifiedGraphics); }, wxT("Saving image..."));
323  }
324  catch (const CException &error) {
325  LOG_POST(Error << "Failed to save image: " << error);
326  }
327  if (m_OpenDocument && result) {
328  // May vary from actual name if user did not end file name with proper extension
329  wxString full_path = ToWxString(CDir::ConcatPath(m_Path, m_FileName + m_FileExtension));
330  wxLaunchDefaultApplication(full_path);
331  }
332 
333  EndModal(wxID_OK);
334 }
335 
336 void CwxExportAlnmultiImgDlg::OnOpenClick( wxCommandEvent& event )
337 {
338  m_OpenDocument = true;
339  OnSaveClick(event);
340 }
341 
342 void CwxExportAlnmultiImgDlg::OnCancelClick( wxCommandEvent& event )
343 {
344  EndModal(wxID_CANCEL);
345 }
346 
348 {
349  return true;
350 }
351 wxBitmap CwxExportAlnmultiImgDlg::GetBitmapResource( const wxString& name )
352 {
353  return wxArtProvider::GetBitmap(name);
354 }
355 wxIcon CwxExportAlnmultiImgDlg::GetIconResource( const wxString& name )
356 {
357  // Icon retrieval
358 ////@begin CwxExportAlnmultiImgDlg icon retrieval
359  wxUnusedVar(name);
360  return wxNullIcon;
361 ////@end CwxExportAlnmultiImgDlg icon retrieval
362 }
363 
364 static const string kExportAlnmultiImageBaseKey("GBPlugins.ExportAlnmultiImageDialog");
365 static const string kExportAlnmultiImagePathKey = "DocumentPath";
366 
368 {
371  string file_path = view.GetString(kExportAlnmultiImagePathKey, "");
372 
373  wxFileName file(ToWxString(file_path));
374  wxString filename = file.GetFullName();
375  wxString path = file.GetPath();
376 
377  if (!path.empty() && wxFileName::DirExists(path)) {
378  m_Path = file_path;
379  }
380  else {
381  // create a default path under user folder
382  m_Path = ToStdString(wxGetHomeDir());
383  }
384 }
385 
387 {
388  if (!m_Path.empty()) {
392  }
393 }
394 
class CAlnMultiWidget
bool SaveVectorImage(CPrintOptions::EOutputFormat format, string const &path, string const &file_name, bool show_header, bool simplified)
virtual IAlnMultiDataSource * GetDataSource()
CDir –.
Definition: ncbifile.hpp:1695
CFile –.
Definition: ncbifile.hpp:1604
CRegistryWriteView GetWriteView(const string &section)
get a read-write view at a particular level.
Definition: registry.cpp:462
static CGuiRegistry & GetInstance()
access the application-wide singleton
Definition: registry.cpp:400
CRegistryReadView GetReadView(const string &section) const
get a read-only view at a particular level.
Definition: registry.cpp:428
static const string & GetFileExtension(EOutputFormat format)
class CRegistryReadView provides a nested hierarchical view at a particular key.
Definition: reg_view.hpp:58
string GetString(const string &key, const string &default_val=kEmptyStr) const
Definition: reg_view.cpp:246
void Set(const string &key, int val)
access a named key at this level, with no recursion
Definition: reg_view.cpp:533
void OnTextctrl5TextUpdated(wxCommandEvent &event)
CPrintOptions::EOutputFormat m_OutputFormat
bool Create(wxWindow *parent, wxWindowID id=ID_CWXSAVEMULTIALNIMGDLG, const wxString &caption=_("Save Image As "), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(440, 210), long style=wxDEFAULT_DIALOG_STYLE|wxCAPTION|wxSYSTEM_MENU|wxCLOSE_BOX|wxTAB_TRAVERSAL)
void OnSaveClick(wxCommandEvent &event)
wxIcon GetIconResource(const wxString &name)
wxBitmap GetBitmapResource(const wxString &name)
void OnCancelClick(wxCommandEvent &event)
void OnFilepathClick(wxCommandEvent &event)
void OnOpenClick(wxCommandEvent &event)
bool m_UserSelectedFilename
view file after finished.
virtual TNumrow GetNumRows(void) const =0
number of rows in alignment
#define ID_BITMAPBUTTON
#define _(proto)
Definition: ct_nlmzip_i.h:78
static void Init(void)
Definition: cursor6.c:76
#define NULL
Definition: ncbistd.hpp:225
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
bool CheckAccess(TMode access_mode) const
Check access rights.
Definition: ncbifile.cpp:1720
bool IsDir(EFollowLinks follow=eFollowLinks) const
Check whether a directory entry is a directory.
Definition: ncbifile.hpp:3946
static string ConcatPath(const string &first, const string &second)
Concatenate two parts of the path for the current OS.
Definition: ncbifile.cpp:776
@ fWrite
Write permission.
Definition: ncbifile.hpp:1152
EDialogReturnValue NcbiMessageBox(const string &message, TDialogType type=eDialog_Ok, EDialogIcon icon=eIcon_Exclamation, const string &title="Error", EDialogTextMode text_mode=eRaw)
Definition: message_box.cpp:48
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string & ToUpper(string &str)
Convert string to upper case – string& version.
Definition: ncbistr.cpp:424
END_EVENT_TABLE()
FILE * file
yy_size_t n
#define wxT(x)
Definition: muParser.cpp:41
const struct ncbi::grid::netcache::search::fields::SIZE size
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
static Format format
Definition: njn_ioutil.cpp:53
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
#define ID_CANCEL
#define ID_TEXTCTRL5
else result
Definition: token2.c:20
static void SetTitle(CRef< CSeq_entry > entry, string title)
static const string kExportAlnmultiImagePathKey
static const string kExportAlnmultiImageBaseKey("GBPlugins.ExportAlnmultiImageDialog")
#define SYMBOL_CWXEXPORTALNMULTIIMGDLG_TITLE
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
string ToStdString(const wxString &s)
Definition: wx_utils.hpp:161
string ToAsciiStdString(const wxString &input)
Definition: wx_utils.hpp:166
Modified on Wed Apr 17 13:10:44 2024 by modify_doxy.py rev. 669887