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

Go to the SVN repository for this file.

1 /* $Id: flat_file_demo_dlg.cpp 45991 2021-01-20 18:23:43Z grichenk $
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: Roman Katargin
27  */
28 
29 
30 #include <ncbi_pch.hpp>
31 ////@begin includes
32 ////@end includes
33 
34 #include "flat_file_demo_dlg.hpp"
35 
36 #include <wx/sizer.h>
37 #include <wx/button.h>
38 #include <wx/stattext.h>
39 #include <wx/choice.h>
40 #include <wx/dcclient.h>
41 #include <wx/settings.h>
42 #include <wx/icon.h>
43 #include <wx/msgdlg.h>
44 #include <wx/filedlg.h>
45 #include <wx/utils.h>
46 
47 #include <wx/wfstream.h>
48 #include <wx/txtstrm.h>
49 
51 
52 #include <gui/objutils/objects.hpp>
53 #include <gui/objutils/label.hpp>
55 //#include <gui/widgets/text_widget/text_item.hpp>
58 
59 #include <serial/objistr.hpp>
61 
63 
64 ////@begin XPM images
65 ////@end XPM images
66 
67 
68 /*!
69  * CFlatFileDemoDlg type definition
70  */
71 
72 IMPLEMENT_DYNAMIC_CLASS( CFlatFileDemoDlg, wxDialog )
73 
74 
75 /*!
76  * CFlatFileDemoDlg event table definition
77  */
78 
79 BEGIN_EVENT_TABLE( CFlatFileDemoDlg, wxDialog )
80 
81 ////@begin CFlatFileDemoDlg event table entries
83 
85 
87 
89 
90  EVT_BUTTON( wxID_CANCEL, CFlatFileDemoDlg::OnCancelClick )
91 
92 ////@end CFlatFileDemoDlg event table entries
93 
95 
96 
97 /*!
98  * CFlatFileDemoDlg constructors
99  */
100 
102 {
103  Init();
104 }
105 
106 CFlatFileDemoDlg::CFlatFileDemoDlg( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
107 {
108  Init();
109  Create(parent, id, caption, pos, size, style);
110 }
111 
112 
113 /*!
114  * CFlatFileDemoDlg creator
115  */
116 
117 bool CFlatFileDemoDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
118 {
119 ////@begin CFlatFileDemoDlg creation
120  SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY|wxWS_EX_BLOCK_EVENTS);
121  wxDialog::Create( parent, id, caption, pos, size, style );
122 
123  CreateControls();
124  if (GetSizer())
125  {
126  GetSizer()->SetSizeHints(this);
127  }
128  Centre();
129 ////@end CFlatFileDemoDlg creation
130  return true;
131 }
132 
133 
134 /*!
135  * CFlatFileDemoDlg destructor
136  */
137 
139 {
140 ////@begin CFlatFileDemoDlg destruction
141 ////@end CFlatFileDemoDlg destruction
142 }
143 
144 
145 /*!
146  * Member initialisation
147  */
148 
150 {
151 ////@begin CFlatFileDemoDlg member initialisation
153  m_AccInput = NULL;
154 ////@end CFlatFileDemoDlg member initialisation
155 
158 
159  m_Scope.Reset(new CScope(*m_ObjMgr));
160  m_Scope->AddDefaults();
161 
162  CUser_object::GetTypeInfo();
163 }
164 
165 
166 /*!
167  * Control creation for CFlatFileDemoDlg
168  */
169 
171 {
172 ////@begin CFlatFileDemoDlg content construction
173  CFlatFileDemoDlg* itemDialog1 = this;
174 
175  wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
176  itemDialog1->SetSizer(itemBoxSizer2);
177 
178  CTextPanel* itemPanel3 = new CTextPanel( itemDialog1, ID_WINDOW, wxDefaultPosition, itemDialog1->ConvertDialogToPixels(wxSize(50, 50)), wxNO_BORDER );
179  itemBoxSizer2->Add(itemPanel3, 1, wxGROW|wxALL, 5);
180 
181  wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
182  itemBoxSizer2->Add(itemBoxSizer4, 0, wxALIGN_LEFT|wxALL, 0);
183 
184  m_LoadedStatus = new wxStaticText( itemDialog1, wxID_STATIC, _("Not loaded..."), wxDefaultPosition, wxDefaultSize, 0 );
185  itemBoxSizer4->Add(m_LoadedStatus, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
186 
187  wxStaticLine* itemStaticLine6 = new wxStaticLine( itemDialog1, wxID_STATIC, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
188  itemBoxSizer2->Add(itemStaticLine6, 0, wxGROW|wxALL, 5);
189 
190  wxBoxSizer* itemBoxSizer7 = new wxBoxSizer(wxHORIZONTAL);
191  itemBoxSizer2->Add(itemBoxSizer7, 0, wxGROW|wxALL, 0);
192 
193  wxFlexGridSizer* itemFlexGridSizer8 = new wxFlexGridSizer(0, 2, 0, 0);
194  itemBoxSizer7->Add(itemFlexGridSizer8, 1, wxALIGN_CENTER_VERTICAL|wxALL, 0);
195 
196  itemFlexGridSizer8->Add(0, 0, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
197 
198  wxButton* itemButton10 = new wxButton( itemDialog1, ID_FILE, _("Load File..."), wxDefaultPosition, wxDefaultSize, 0 );
199  itemFlexGridSizer8->Add(itemButton10, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
200 
201  m_AccInput = new wxTextCtrl( itemDialog1, ID_ACC_TEXT, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
202  m_AccInput->SetMaxLength(20);
203  m_AccInput->SetHelpText(_("Enter Accession"));
205  m_AccInput->SetToolTip(_("Enter Accession"));
206  itemFlexGridSizer8->Add(m_AccInput, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
207 
208  wxButton* itemButton12 = new wxButton( itemDialog1, ID_ACC, _("Load Accession"), wxDefaultPosition, wxDefaultSize, 0 );
209  itemFlexGridSizer8->Add(itemButton12, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
210 
211  itemFlexGridSizer8->AddGrowableCol(0);
212 
213  wxStaticLine* itemStaticLine13 = new wxStaticLine( itemDialog1, wxID_STATIC, wxDefaultPosition, wxDefaultSize, wxLI_VERTICAL );
214  itemBoxSizer7->Add(itemStaticLine13, 0, wxGROW|wxALL, 5);
215 
216  wxBoxSizer* itemBoxSizer14 = new wxBoxSizer(wxVERTICAL);
217  itemBoxSizer7->Add(itemBoxSizer14, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
218 
219  wxButton* itemButton15 = new wxButton( itemDialog1, ID_BUTTON1, _("Save Text..."), wxDefaultPosition, wxDefaultSize, 0 );
220  itemButton15->Enable(false);
221  itemBoxSizer14->Add(itemButton15, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
222 
223  wxButton* itemButton16 = new wxButton( itemDialog1, wxID_CANCEL, _("&Close"), wxDefaultPosition, wxDefaultSize, 0 );
224  itemBoxSizer14->Add(itemButton16, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
225 
226 ////@end CFlatFileDemoDlg content construction
227 
228  m_AccInput->SetValue(wxT("U49845"));
229 
230  FindWindow(ID_ACC)->SetFocus();
231  FindWindow(ID_WINDOW)->Show();
232 }
233 
234 /*!
235  * Should we show tooltips?
236  */
237 
239 {
240  return true;
241 }
242 
243 /*!
244  * Get bitmap resources
245  */
246 
247 wxBitmap CFlatFileDemoDlg::GetBitmapResource( const wxString& name )
248 {
249  // Bitmap retrieval
250 ////@begin CFlatFileDemoDlg bitmap retrieval
251  wxUnusedVar(name);
252  return wxNullBitmap;
253 ////@end CFlatFileDemoDlg bitmap retrieval
254 }
255 
256 /*!
257  * Get icon resources
258  */
259 
260 wxIcon CFlatFileDemoDlg::GetIconResource( const wxString& name )
261 {
262  // Icon retrieval
263 ////@begin CFlatFileDemoDlg icon retrieval
264  wxUnusedVar(name);
265  return wxNullIcon;
266 ////@end CFlatFileDemoDlg icon retrieval
267 }
268 
269 /*!
270  * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_ACC
271  */
272 
273 void CFlatFileDemoDlg::OnAccClick( wxCommandEvent& WXUNUSED(event) )
274 {
275  wxBusyCursor wait;
276 
277  CRef<CSeq_id> id(new CSeq_id());
278 
279  string acc = ToStdString(m_AccInput->GetValue());
280  try {
281  id->Set(acc);
282  }
283  catch (CSeqIdException&) {
284  string msg("Accession '");
285  msg += acc + " not recognized as a valid accession";
286  wxMessageBox(ToWxString(msg), _("Cannot load accesion"),
287  wxOK | wxICON_ERROR);
288  return;
289  }
290 
291  CTextPanel* textPanel = (CTextPanel*)FindWindow(ID_WINDOW);
292  SConstScopedObject obj(id, m_Scope);
294  objects.push_back(obj);
295  textPanel->InitWidget(objects);
296  FindWindow(ID_BUTTON1)->Enable();
297 
298  /*{{
299  CBioseq_Handle handle = m_Scope->GetBioseqHandle(*id);
300  if (handle) {
301  CSeq_entry_Handle se = handle.GetTopLevelEntry();
302  CConstRef<CSerialObject> so(se.GetSeq_entryCore());
303  CNcbiOfstream("c:\\tmp\\test.xml") << MSerial_Xml << *so;
304  }
305  }}*/
306 
307  string label;
309  m_LoadedStatus->SetLabel(ToWxString("Accession: " + label));
310 }
311 
312 /*!
313  * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_FILE
314  */
315 
316 void CFlatFileDemoDlg::OnLoadFileClick( wxCommandEvent& WXUNUSED(event) )
317 {
318  wxFileDialog dlg(this, _("Select a file"), _(""), _(""),
319  _("ASN files (*.asn)|*.asn|All files (*.*)|*.*"),
320  wxFD_OPEN);
321 
322  if (dlg.ShowModal() != wxID_OK)
323  return;
324 
325  wxString fileName = dlg.GetPath();
326  if (fileName.empty())
327  return;
328 
330 
331  try {
332  wxBusyCursor wait;
333  CNcbiIfstream istr(fileName.fn_str());
334  unique_ptr<CObjectIStream> os(CObjectIStream::Open(eSerial_AsnText, istr));
335  string header = os->ReadFileHeader();
336  TTypeInfo type_info = CClassTypeInfo::GetClassInfoByName(header);
337  so.Reset((CSerialObject*)type_info->Create());
338  os->ReadObject(so.GetPointer(), type_info);
339  } catch(CException& e) {
340  LOG_POST(Error << e.ReportAll());
341  string s = e.GetMsg();
342  wxMessageBox(ToWxString(s), _("Error loading file."),
343  wxOK | wxICON_ERROR);
344  return;
345  }
346 
347  CSeq_entry* seq_entry = dynamic_cast<CSeq_entry*>(so.GetPointer());
348  if (seq_entry)
349  m_Scope->AddTopLevelSeqEntry(*seq_entry);
350  CBioseq* bioseq = dynamic_cast<CBioseq*>(so.GetPointer());
351  if (bioseq)
352  m_Scope->AddBioseq(*bioseq);
353 
354  CTextPanel* textPanel = (CTextPanel*)FindWindow(ID_WINDOW);
355  SConstScopedObject obj(so, m_Scope);
357  objects.push_back(obj);
358  textPanel->InitWidget(objects);
359  FindWindow(ID_BUTTON1)->Enable();
360 
361  m_LoadedStatus->SetLabel(_("File: ") + dlg.GetFilename());
362 }
363 
364 /*!
365  * wxEVT_CLOSE_WINDOW event handler for ID_HIT_MATRIX_DEMO_DLG
366  */
367 
368 void CFlatFileDemoDlg::OnCancelClick( wxCommandEvent& WXUNUSED(event) )
369 {
370  Destroy();
371 }
372 
373 
374 /*!
375  * wxEVT_CLOSE_WINDOW event handler for ID_HIT_MATRIX_DEMO_DLG
376  */
377 
378 void CFlatFileDemoDlg::OnCloseWindow( wxCloseEvent& WXUNUSED(event) )
379 {
380  Destroy();
381 }
382 
383 /*!
384  * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON1
385  */
386 
387 void CFlatFileDemoDlg::OnSaveText( wxCommandEvent& WXUNUSED(event) )
388 {
389  static wxString filePath;
390  wxFileDialog dlg(this, _("Select a file"), _(""), filePath,
391  _("All files (*.*)|*.*"),
392  wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
393 
394  if (dlg.ShowModal() != wxID_OK)
395  return;
396 
397  filePath = dlg.GetPath();
398 
399  CTextPanel* textPanel = (CTextPanel*)FindWindow(ID_WINDOW);
400  CTextItemPanel* textItemPanel = textPanel->GetWidget();
401  ITextItem* textItem = textItemPanel->GetRootItem();
402  if (!textItem) return;
403 
404  wxFFileOutputStream stream(filePath);
405  wxTextOutputStream os(stream);
406  textItem->GetText(os, textItem->GetItemBlock(), textItemPanel->GetContext());
407 
408  if (stream.GetLastError() != wxSTREAM_NO_ERROR)
409  wxMessageBox(_("Failed to write file: \"") + filePath + _("\"."),
410  _("Write Error"), wxOK|wxICON_ERROR);
411 }
412 
CFlatFileDemoDlg()
Constructors.
~CFlatFileDemoDlg()
Destructor.
wxStaticText * m_LoadedStatus
CRef< CScope > m_Scope
void OnLoadFileClick(wxCommandEvent &event)
wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_FILE
void OnCloseWindow(wxCloseEvent &event)
wxEVT_CLOSE_WINDOW event handler for ID_HIT_MATRIX_DEMO_DLG
bool Create(wxWindow *parent, wxWindowID id=ID_HIT_MATRIX_DEMO_DLG, const wxString &caption=_("CFlatFileDemoDlg"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(400, 368), long style=wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX|wxMAXIMIZE_BOX|wxMINIMIZE_BOX)
Creation.
wxIcon GetIconResource(const wxString &name)
Retrieves icon resources.
void Init()
Initialises member variables.
void OnCancelClick(wxCommandEvent &event)
wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL
void OnAccClick(wxCommandEvent &event)
wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_ACC
void OnSaveText(wxCommandEvent &event)
wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON1
wxBitmap GetBitmapResource(const wxString &name)
Retrieves bitmap resources.
static bool ShowToolTips()
Should we show tooltips?
void CreateControls()
Creates the controls and sizers.
CRef< CObjectManager > m_ObjMgr
static TRegisterLoaderInfo RegisterInObjectManager(CObjectManager &om, CReader *reader=0, CObjectManager::EIsDefault is_default=CObjectManager::eDefault, CObjectManager::TPriority priority=CObjectManager::kPriority_NotSet)
Definition: gbloader.cpp:366
CScope –.
Definition: scope.hpp:92
CSeqIdException –.
Definition: Seq_id.hpp:969
Definition: Seq_entry.hpp:56
Base class for all serializable objects.
Definition: serialbase.hpp:150
ITextItem * GetRootItem()
CTextPanelContext * GetContext()
CTextItemPanel * GetWidget() const
Definition: text_panel.hpp:229
virtual bool InitWidget(TConstScopedObjects &objects)
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
CTextBlock GetItemBlock() const
Definition: text_item.hpp:117
virtual void GetText(wxTextOutputStream &os, const CTextBlock &block, CTextPanelContext *context)=0
#define ID_ACC
#define ID_FILE
#define _(proto)
Definition: ct_nlmzip_i.h:78
static void Init(void)
Definition: cursor6.c:76
#define ID_BUTTON1
#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
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
string ReportAll(TDiagPostFlags flags=eDPF_Exception) const
Report all exceptions.
Definition: ncbiexpt.cpp:370
static void GetLabel(const CObject &obj, string *label, ELabelType type=eDefault)
Definition: label.cpp:140
vector< SConstScopedObject > TConstScopedObjects
Definition: objects.hpp:65
@ eDefault
Definition: label.hpp:73
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
static CObjectIStream * Open(ESerialDataFormat format, CNcbiIstream &inStream, bool deleteInStream)
Create serial object reader and attach it to an input stream.
Definition: objistr.cpp:195
CBioseq_Handle AddBioseq(CBioseq &bioseq, TPriority pri=kPriority_Default, EExist action=eExist_Throw)
Add bioseq, return bioseq handle.
Definition: scope.cpp:530
static CRef< CObjectManager > GetInstance(void)
Return the existing object manager or create one.
CSeq_entry_Handle AddTopLevelSeqEntry(CSeq_entry &top_entry, TPriority pri=kPriority_Default, EExist action=eExist_Default)
Add seq_entry, default priority is higher than for defaults or loaders Add object to the score with p...
Definition: scope.cpp:522
void AddDefaults(TPriority pri=kPriority_Default)
Add default data loaders from object manager.
Definition: scope.cpp:504
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:439
static TTypeInfo GetClassInfoByName(const string &name)
Definition: classinfob.cpp:244
TObjectPtr Create(CObjectMemoryPool *memoryPool=0) const
Create object of this type on heap (can be deleted by operator delete)
static const char label[]
END_EVENT_TABLE()
#define wxT(x)
Definition: muParser.cpp:41
const struct ncbi::grid::netcache::search::fields::SIZE size
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
string ToStdString(const wxString &s)
Definition: wx_utils.hpp:161
Modified on Thu Feb 22 17:14:25 2024 by modify_doxy.py rev. 669887