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

Go to the SVN repository for this file.

1 /* $Id: smart_file_text_toolbar.cpp 47479 2023-05-02 13:24:02Z ucko $
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 
33 
35 
37 #include <gui/core/document.hpp>
42 
45 
46 #include <objmgr/scope.hpp>
47 #include <objmgr/bioseq_handle.hpp>
48 
51 
56 
67 
68 #include <wx/aui/aui.h>
69 #include <wx/button.h>
70 #include <wx/filedlg.h>
71 #include <wx/msgdlg.h>
72 #include <wx/filename.h>
73 
74 #include <serial/iterator.hpp>
75 
78 
79 ///////////////////////////////////////////////////////////////////////////////
80 /// class CSmartFileTextToolbar
81 
83  IProjectView& view,
84  IServiceLocator* service_locator,
85  SConstScopedObject validateObj,
86  const wxString& fullPathName)
87  : wxAuiToolBar(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxAUI_TB_HORZ_LAYOUT|wxBORDER_NONE),
88  m_View(view), m_ServiceLocator(service_locator), m_ValidateObj(validateObj), m_JobId(CAppJobDispatcher::eInvalidJobID), m_ValidationInProgress(false),
89  m_EventProxy(new CEventProxy(this))
90 {
91 
92  m_MegaButton = dynamic_cast<wxButton*>(AddControl(new wxButton(this, ID_TB_MegaButton, "Mega Report", wxDefaultPosition, wxDefaultSize, 0))->GetWindow());
93  m_ValidateButton = dynamic_cast<wxButton*>(AddControl(new wxButton(this, ID_TB_ValidateButton, "Validate", wxDefaultPosition, wxDefaultSize, 0))->GetWindow());
94  m_SaveButton = dynamic_cast<wxButton*>(AddControl(new wxButton(this, ID_TB_SaveButton, "Save", wxDefaultPosition, wxDefaultSize, 0))->GetWindow());
95  m_SaveAsButton = dynamic_cast<wxButton*>(AddControl(new wxButton(this, ID_TB_SaveAsButton, "SaveAs", wxDefaultPosition, wxDefaultSize, 0))->GetWindow());
96  m_CloseButton = dynamic_cast<wxButton*>(AddControl(new wxButton(this, ID_TB_CloseButton, "Close", wxDefaultPosition, wxDefaultSize, 0))->GetWindow());
97 
98  // Initialize path and name from fullPathName parameter
99  wxFileName file(fullPathName);
100  m_FilePath = file.GetPath();
101  m_FileName = file.GetFullName();
102 }
103 
105 {
106  m_EventProxy->ResetToolbar();
107 
110 }
111 
112 BEGIN_EVENT_TABLE( CSmartFileTextToolbar, wxAuiToolBar )
118 
119  EVT_BUTTON(ID_TB_MegaButton, CSmartFileTextToolbar::OnMegaBtn)
120  EVT_BUTTON(ID_TB_ValidateButton, CSmartFileTextToolbar::OnValidateBtn)
121  EVT_BUTTON(ID_TB_SaveButton, CSmartFileTextToolbar::OnSaveBtn)
122  EVT_BUTTON(ID_TB_SaveAsButton, CSmartFileTextToolbar::OnSaveAsBtn)
123  EVT_BUTTON(ID_TB_CloseButton, CSmartFileTextToolbar::OnCloseBtn)
125 
127  ON_EVENT(CAppJobNotification, CAppJobNotification::eStateChanged, &CSmartFileTextToolbar::CEventProxy::OnJobNotification)
129 
130 void CSmartFileTextToolbar::CEventProxy::OnJobNotification(CEvent* evt)
131 {
132  if (m_Toolbar) m_Toolbar->OnJobNotification(evt);
133 }
134 
135 void CSmartFileTextToolbar::OnUpdateValidateBtn(wxUpdateUIEvent& event)
136 {
137  event.Enable(m_ValidateObj.object && !m_ValidationInProgress);
138 }
139 
140 void CSmartFileTextToolbar::OnUpdateSaveBtn(wxUpdateUIEvent& event)
141 {
142  event.Enable((!m_FilePath.IsEmpty() || !m_FileName.IsEmpty())
144 }
145 
146 void CSmartFileTextToolbar::OnUpdateSaveAsBtn(wxUpdateUIEvent& event)
147 {
148  event.Enable(!m_ValidationInProgress);
149 }
150 
151 void CSmartFileTextToolbar::OnUpdateCloseBtn(wxUpdateUIEvent& event)
152 {
153  event.Enable(!m_ValidationInProgress);
154 }
155 
156 static const string sValidateViewType("Validate Table View");
157 
159 {
160  RunValidateView();
161 }
162 
163 void CSmartFileTextToolbar::OnMegaBtn(wxCommandEvent&)
164 {
166 }
167 
169 {
170  CRef<CUser_object> params(new CUser_object());
173 
174  wxWindow* main_window = NULL;
175  CWorkbench* wb_frame = dynamic_cast<CWorkbench*>(m_ServiceLocator);
176  if (wb_frame)
177  main_window = wb_frame->GetMainWindow();
178 
179  CValidateFrame::GetInstance(main_window, wb_frame, params);
180 }
181 
182 void CSmartFileTextToolbar::OnSaveBtn(wxCommandEvent&)
183 {
184  CTextPanel* textPanel = dynamic_cast<CTextPanel*>(m_View.GetWindow());
185  if (!textPanel) return;
186 
187  const CSerialObject* so = dynamic_cast<const CSerialObject*>(textPanel->GetOrigObject());
188  if (!so) return;
189 
190  if (!m_FilePath.IsEmpty() && !m_FileName.IsEmpty()) {
191  wxFileName fname(m_FilePath, m_FileName);
192  x_Save(*so, fname.GetFullPath());
193 
195  if (!projectService) return;
196  CRef<CGBWorkspace> ws = projectService->GetGBWorkspace();
197  if (!ws) return;
198  CGBDocument* doc = dynamic_cast<CGBDocument*>(ws->GetProjectFromId(m_View.GetProjectId()));
199  if (doc) doc->SetDataModified(false);
200  }
201 }
202 
203 void CSmartFileTextToolbar::OnSaveAsBtn(wxCommandEvent& event)
204 {
205  CTextPanel* textPanel = dynamic_cast<CTextPanel*>(m_View.GetWindow());
206  if (!textPanel) return;
207 
208  const CSerialObject* so = dynamic_cast<const CSerialObject*>(textPanel->GetOrigObject());
209  if (!so) return;
210 
211  wxString filePath = x_AskForFileName();
212  if (!filePath.IsEmpty()) {
213  x_Save(*so, filePath);
214 
215  wxFileName file(filePath);
216  m_FilePath = file.GetPath();
217  m_FileName = file.GetFullName();
218  }
219 }
220 
222 {
223  string path;
224 
226  if (!projectService) return path;
227  if (!projectService->HasWorkspace())
228  projectService->CreateNewWorkspace();
229 
230  CRef<CGBWorkspace> ws = projectService->GetGBWorkspace();
231  CGBDocument* doc = dynamic_cast<CGBDocument*>(ws->GetProjectFromId(projid));
232  if (!doc) return path;
233 
234  CProjectFolder& data_folder = doc->SetData();
235  if (data_folder.CanGetItems()) {
236  ITERATE(CProjectFolder::TItems, bit, data_folder.GetItems()) {
237  int pitemId = (*bit)->GetId();
238  CProjectItem* item = data_folder.FindProjectItemById(pitemId);
239  string filePath;
240  if (item) {
241  CProjectItemExtra::GetStr(*item, "OriginalPath", filePath);
242  wxFileName fullpath(filePath);
243  path = fullpath.GetPath();
244  break;
245  }
246  }
247  }
248 
249  return path;
250 }
251 
252 void CSmartFileTextToolbar::OnCloseBtn(wxCommandEvent& event)
253 {
254  auto projectId = m_View.GetProjectId();
255  if (projectId < 0)
256  return;
257 // CProjectTask::TProjectIdVector toRemove(1, projectId);
258 // CProjectTask::RemoveProjects(m_ServiceLocator, toRemove, false);
260  CRef<CGBWorkspace> ws = service->GetGBWorkspace();
261  if (!ws)
262  return;
263  CRef<CGBDocument> doc(dynamic_cast<CGBDocument*>(ws->GetProjectFromId(projectId)));
264  if (!doc)
265  return;
266  if (doc->IsLoading())
267  doc->CancelLoading();
268  service->RemoveProject(*doc);
269 
270  for (CTypeIterator<CGBProjectHandle> it(ws->SetWorkspace()); it; ++it) {
271  CGBDocument* doc = dynamic_cast<CGBDocument*>(&*it);
272  if (!doc) continue;
273  return;
274  }
276 }
277 
279 {
280  wxString cdir = wxGetCwd();
281  if (!m_FilePath.IsEmpty()) {
282  cdir = m_FilePath;
283  }
284 
285  wxFileDialog dlg(this, wxT("Select a file"), cdir, wxT(""),
288  wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
289  if (dlg.ShowModal() != wxID_OK)
290  return wxEmptyString;
291 
292  return dlg.GetPath();
293 }
294 
295 void CSmartFileTextToolbar::x_Save(const CSerialObject& so, const wxString& fileName)
296 {
297  string errMsg;
298  try {
299  CNcbiOfstream os(fileName.fn_str());
300  os << MSerial_AsnText << so;
301  }
302  catch (const CException& e) {
303  errMsg = e.GetMsg();
304  }
305  catch (const exception& e) {
306  errMsg = e.what();
307  }
308 
309  if (!errMsg.empty()) {
310  wxMessageBox(wxT("File: ") + fileName + wxT("\nError: ") + ToWxString(errMsg),
311  wxT("Failed to save file"), wxOK | wxICON_ERROR);
312  }
313 }
314 
316 {
318  if (!projectService) return;
319 
320 
321  CRef<CGBWorkspace> ws = projectService->GetGBWorkspace();
322  if (!ws) return;
323 
324  CGBDocument* doc = dynamic_cast<CGBDocument*>(ws->GetProjectFromId(m_View.GetProjectId()));
325  if (!doc) return;
326 
327  string errMsg;
328  try {
329  doc->Save(doc->GetFileName());
330  } catch (const CException& ex) {
331  errMsg = ex.GetMsg();
332  } catch (const exception& ex) {
333  errMsg = ex.what();
334  }
335  if (!errMsg.empty()) {
336  wxMessageBox(errMsg, wxT("Smart client communication error"), wxOK|wxICON_ERROR);
337  return;
338  }
339 }
340 
342 {
343  CAppJobNotification* notn = dynamic_cast<CAppJobNotification*>( evt );
344  _ASSERT(notn);
345  if (notn->GetJobID() != m_JobId) return;
346  m_ValidationInProgress = false;
347  m_MegaButton->Enable();
348  m_ValidateButton->Enable();
349  m_SaveButton->Enable();
350  m_SaveAsButton->Enable();
351  m_CloseButton->Enable();
352 
353  switch (notn->GetState())
354  {
355  case IAppJob::eCompleted:
356  {
357  CRef<CObject> result = notn->GetResult();
359 
360  if (errs && errs->GetData().size() != 0)
361  {
363  if (table)
364  {
365  size_t info = table->CountBySeverity(eDiag_Info);
366  size_t warn = table->CountBySeverity(eDiag_Warning);
367  size_t error = table->CountBySeverity(eDiag_Error);
368  size_t reject = table->CountBySeverity(eDiag_Critical);
369  if (info != 0 || warn != 0 || error != 0 || reject != 0)
370  {
371  string msg = "INFO: " + NStr::NumericToString(info) + " WARNING: " + NStr::NumericToString(warn) +
372  " ERROR: " + NStr::NumericToString(error) + " REJECT: " + NStr::NumericToString(reject);
373  wxMessageDialog dlg(this, wxT("Submission failed validation tests with:\n") + ToWxString(msg), _("Validation failed"), wxYES_NO | wxCANCEL | wxICON_QUESTION);
374  dlg.SetYesNoLabels(_("Continue"), _("Review errors"));
375  switch (dlg.ShowModal())
376  {
377  case wxID_YES : break;
378  case wxID_NO : {m_JobId = CAppJobDispatcher::eInvalidJobID; RunValidateView(); return;} break;
379  case wxID_CANCEL : {m_JobId = CAppJobDispatcher::eInvalidJobID; return;} break;
380  default : break;
381  }
382  }
383  }
384  }
385  break;
386  }
387  case IAppJob::eCanceled:
388  break;
389  case IAppJob::eFailed:
390  {
392  string err_msg = "Failed: ";
393  if (error)
394  err_msg += error->GetText();
395  else
396  err_msg += "Unknown fatal error";
397 
398  wxMessageDialog dlg(this, ToWxString(err_msg), _("Validation failed"), wxOK| wxCANCEL |wxICON_ERROR);
399  dlg.SetOKLabel(_("Continue"));
400  if (dlg.ShowModal() != wxID_OK)
401  {
403  return;
404  }
405  }
406  break;
407  default:
408  break;
409  }
410 
412  ReallyDone();
413 }
414 
416 
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
EVT_UPDATE_UI(eCmdAlnShowMethodsDlg, CAlnMultiWidget::OnUpdateShowMethodDlg) EVT_UPDATE_UI(eCmdMethodProperties
CAppJobDispatcher.
CAppJobNotification Notification send by CAppJobEventTranslator.
static CDiscrepancyDlg * OpenDiscrepancyDlg(EReportType type, CScope &scope, IWorkbench *workbench, const wxString &workDir=wxEmptyString)
CEventHandler.
CEvent - generic event implementation TODO TODO - Attachments.
Definition: event.hpp:86
static wxString GetDialogFilter(EFileType fileType)
CGBDocument.
Definition: document.hpp:113
void Save(const wxString &abs_path)
Definition: document.cpp:503
void CancelLoading()
Definition: document.cpp:269
wxString GetFileName() const
Definition: document.cpp:132
bool IsLoading() const
Definition: document.cpp:264
CObjectFor –.
Definition: ncbiobj.hpp:2335
CProjectItem * FindProjectItemById(CProjectItem::TId id)
static bool GetStr(const objects::CProjectItem &pi, const string &tag, string &value)
CProjectService - a service providing API for operations with Workspaces and Projects.
void RemoveProject(CGBDocument &doc)
CRef< objects::CGBWorkspace > GetGBWorkspace()
Base class for all serializable objects.
Definition: serialbase.hpp:150
class CSmartFileTextToolbar
void OnMegaBtn(wxCommandEvent &)
void x_Save(const CSerialObject &so, const wxString &fileName)
string x_GetProject_Path(int projid)
CSmartFileTextToolbar(wxWindow *parent, IProjectView &view, IServiceLocator *service_locator, SConstScopedObject validateObj, const wxString &fullPathName)
class CSmartFileTextToolbar
void OnSaveBtn(wxCommandEvent &)
void OnSaveAsBtn(wxCommandEvent &)
CRef< CEventProxy > m_EventProxy
void OnUpdateSaveBtn(wxUpdateUIEvent &event)
void OnUpdateValidateBtn(wxUpdateUIEvent &event)
void OnValidateBtn(wxCommandEvent &)
void OnUpdateSaveAsBtn(wxUpdateUIEvent &event)
void OnCloseBtn(wxCommandEvent &)
void OnUpdateCloseBtn(wxUpdateUIEvent &event)
static void SetDoInference(objects::CUser_object &params, bool val)
static void SetDoAlignment(objects::CUser_object &params, bool val)
virtual const CObject * GetOrigObject() const
Template class for iteration on objects of class C.
Definition: iterator.hpp:673
static void GetInstance(wxWindow *parent, IWorkbench *workbench, CRef< objects::CUser_object > params, wxWindowID id=17000, const wxString &caption=_("Validation Errors"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(900, 600), long style=wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX|wxMINIMIZE_BOX|wxMAXIMIZE_BOX|wxTAB_TRAVERSAL)
CWorkbench - default implementation of IWorkbench.
static bool CleanUp()
class IProjectView defines the abstract interface for views observing projects.
virtual TProjectId GetProjectId() const =0
IServiceLocator - an abstract mechanism for locating services.
Definition: service.hpp:71
virtual wxWindow * GetWindow()=0
returns a pointer to the wxWindow representing the client
#define _(proto)
Definition: ct_nlmzip_i.h:78
#define false
Definition: bool.h:36
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NULL
Definition: ncbistd.hpp:225
@ eDiag_Info
Informational message.
Definition: ncbidiag.hpp:651
@ eDiag_Error
Error message.
Definition: ncbidiag.hpp:653
@ eDiag_Warning
Warning message.
Definition: ncbidiag.hpp:652
@ eDiag_Critical
Critical error message.
Definition: ncbidiag.hpp:654
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
virtual const char * what(void) const noexcept
Standard report (includes full backlog).
Definition: ncbiexpt.cpp:342
CIRef< T > GetServiceByType()
retrieves a typed reference to a service, the name of C++ type is used as the name of the service.
Definition: service.hpp:91
virtual wxFrame * GetMainWindow()
returns a pointer to the main application frame window
CConstIRef< IAppJobError > GetError() const
returns non-null pointer only if job Failed
CRef< objects::CScope > scope
Definition: objects.hpp:53
static CAppJobDispatcher & GetInstance()
CRef< CObject > GetResult() const
returns non-null pointer only if Completed or Running and has temporary results available
bool DeleteJob(TJobID job_id)
when a Job is deleted the listener is not notified
#define ON_EVENT(type, id, handler)
#define END_EVENT_MAP()
Ends definition of Command Map.
CConstRef< CObject > object
Definition: objects.hpp:52
#define BEGIN_EVENT_MAP(thisClass, baseClass)
Begins definition of Command Map for CEventHandler-derived class.
TJobState GetState() const
@ eCanceled
Definition: app_job.hpp:91
@ eCompleted
Definition: app_job.hpp:89
@ eFailed
Definition: app_job.hpp:90
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
#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::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:500
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
list< CRef< CProjectItem > > TItems
bool CanGetItems(void) const
Check if it is safe to call GetItems method.
const TItems & GetItems(void) const
Get the Items member data.
<!DOCTYPE HTML >< html > n< header > n< title > PubSeq Gateway Help Page</title > n< style > n table
END_EVENT_TABLE()
FILE * file
static MDB_envinfo info
Definition: mdb_load.c:37
#define wxT(x)
Definition: muParser.cpp:41
static static static wxID_ANY
USING_SCOPE(objects)
static const string sValidateViewType("Validate Table View")
#define _ASSERT
else result
Definition: token2.c:20
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
Modified on Mon May 20 04:59:32 2024 by modify_doxy.py rev. 669887