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

Go to the SVN repository for this file.

1 /* $Id: indexer_app_mode.cpp 39528 2017-10-05 15:27:37Z katargir $
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 
32 #include "indexer_app_mode.hpp"
34 
38 
39 #include <gui/utils/extension.hpp>
41 
47 #include <gui/core/commands.hpp>
48 #include <gui/core/document.hpp>
50 
52 
53 #include <gui/objutils/label.hpp>
55 
57 
58 #include <serial/iterator.hpp>
59 
60 #include <wx/filedlg.h>
61 #include <wx/filefn.h>
62 #include <wx/msgdlg.h>
63 #include <wx/filename.h>
64 
67 
68 
69 ///////////////////////////////////////////////////////////////////////////////
70 /// class CIndexerProjectItemExtension
71 
73  public CObject,
74  public IProjectItemExtension,
75  public IExtension
76 {
77 public:
79 
80  /// @name IProjectItemExtension interface implementation
81  /// @{
82  virtual void ProjectItemCreated(objects::CProjectItem& item, IServiceLocator* service_locator);
83  virtual void ProjectItemAttached(objects::CProjectItem& /*item*/, CGBDocument& /*doc*/, IServiceLocator* /*service_locator*/) {}
84  virtual void ProjectItemDetached(objects::CProjectItem& /*item*/, CGBDocument& /*doc*/, IServiceLocator* /*service_locator*/) {}
85  /// @}
86 
87  /// @name IExtension interface implementation
88  /// @{
89  virtual string GetExtensionIdentifier() const;
90  virtual string GetExtensionLabel() const;
91  /// @}
92 };
93 
94 void CIndexerProjectItemExtension::ProjectItemCreated(objects::CProjectItem& item, IServiceLocator* service_locator)
95 {
96  // If project item is a seqsubmit, use the first seqentry to set the label
97  if (item.CanGetItem()) {
98  const CProjectItem::TItem& citem = item.GetItem();
99  if (citem.IsSubmit()) {
100  const CSeq_submit* seqSubmit = &citem.GetSubmit();
101  const CObject* obj = seqSubmit;
102  if (seqSubmit->IsEntrys() && seqSubmit->CanGetData()) {
103  const CSeq_submit::TData& data = seqSubmit->GetData();
104  if (data.IsEntrys()) {
105  const CSeq_submit::TData::TEntrys& entrys = data.GetEntrys();
106  if (!entrys.empty()) {
107  obj = entrys.front().GetPointerOrNull();
108  }
109  }
110  }
111 
112  string sLabel;
113  if (obj) {
114  CLabel::GetLabel( *obj, &sLabel, CLabel::eDefault, NULL );
115  }
116  item.SetLabel(sLabel);
117  }
118  }
119 
120  item.AddTag("open_discrepancy_dlg");
121  item.AddTag("open_editing_buttons");
122  item.AddTag("open_text_view");
123 }
124 
126 {
127  static string sid("single_project_mode_item_extension");
128  return sid;
129 }
130 
132 {
133  static string slabel("Genome Workbench Single Project mode item extension");
134  return slabel;
135 }
136 
137 ///////////////////////////////////////////////////////////////////////////////
138 /// class CIndexerAppModeExtension
139 
141 {
142 }
143 
145 {
146  static string sid("indexer_app_mode_extension");
147  return sid;
148 }
149 
151 {
152  static string slabel("Indexer application mode extension");
153  return slabel;
154 }
155 
157 {
158  return "indexer";
159 }
160 
162 {
163  if (wb) {
164  m_Workbench = wb;
166  m_Workbench->Bind(wxEVT_MENU, &CIndexerAppModeExtension::OnFileOpen, this, wxID_OPEN);
168 
171  }
172  else if (m_Workbench) {
173  m_Workbench->Unbind(wxEVT_MENU, &CIndexerAppModeExtension::OnFileOpen, this, wxID_OPEN);
174  m_Workbench = 0;
175  }
176 }
177 
178 void CIndexerAppModeExtension::OnFileOpen(wxCommandEvent& event)
179 {
180  if (!m_Workbench) return;
181 
182  long style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
185  wxFileDialog dlg(NULL, "Select ASN.1 File", wxGetCwd(), wxEmptyString, filter, style);
186 
187  if (dlg.ShowModal() != wxID_OK)
188  return;
189 
190  vector<wxString> fileNames = { dlg.GetPath() };
191  CIRef<IObjectLoader> loader(new CAsnObjectLoader(fileNames));
192 
194 
195  if (!srv->HasWorkspace())
196  srv->CreateNewWorkspace();
197 
198  CRef<CGBWorkspace> ws = srv->GetGBWorkspace();
199  if (!ws) return;
200 
201  CSelectProjectOptions options;
202  options.Set_CreateNewProject();
203 
204  CRef<CObjectLoadingTask> task(new CObjectLoadingTask(srv, *loader, options));
205  task->AddProjectItemExtra("SmartFile", string(fileNames.front().ToUTF8()));
207 }
208 
210 {
211  if (!m_Workbench) return;
212 
214 
215  CRef<CGBWorkspace> ws = srv->GetGBWorkspace();
216  if (!ws) return;
217 
218  vector<CGBDocument*> docs;
219  for (CTypeIterator<CGBProjectHandle> it(ws->SetWorkspace()); it; ++it) {
220  CGBDocument* doc = dynamic_cast<CGBDocument*>(&*it);
221  if (doc && doc->GetDataModified())
222  docs.push_back(doc);
223  }
224 
225  if (docs.empty()) {
226  srv->ResetWorkspace();
228  return;
229  }
230 
232  for (auto doc : docs) {
233  for (CTypeConstIterator<CProjectItem> it(doc->GetData()); it; ++it) {
234  const CSerialObject* so = dynamic_cast<const CSerialObject*>(it->GetObject());
235  if (!so) return;
236 
237  string smartFile;
238  CProjectItemExtra::GetStr(*it, "SmartFile", smartFile);
239  if (smartFile.empty()) continue;
240  files[so] = smartFile;
241  }
242 
243  if (!files.empty()) {
244  wxString fileNames, msg = wxT("Want to save changes to "), delim;
245  for (auto& f : files) {
246  wxFileName fname(wxString::FromUTF8(f.second.c_str()));
247  msg += delim + fname.GetFullName();
248  delim = wxT(",");
249  }
250  msg += wxT("?");
251 
252  wxMessageDialog msgBox(nullptr, msg, wxT("Gemome Workbech"), wxYES_NO | wxCANCEL | wxICON_QUESTION);
253  msgBox.SetYesNoLabels(wxID_SAVE, _("Do&n't save"));
254 
255  int result = msgBox.ShowModal();
256  if (result == wxID_CANCEL)
257  return;
258 
259  if (result == wxID_YES) {
260  for (auto f : files) {
261  string errMsg;
262  wxString fileName = wxString::FromUTF8(f.second.c_str());
263  try {
264  CNcbiOfstream os(fileName.fn_str());
265  os << MSerial_AsnText << *f.first;
266  }
267  catch (const CException& e) {
268  errMsg = e.GetMsg();
269  }
270  catch (const exception& e) {
271  errMsg = e.what();
272  }
273 
274  if (!errMsg.empty()) {
275  wxMessageBox(wxT("File: ") + fileName + wxT("\nError: ") + ToWxString(errMsg),
276  wxT("Failed to save file"), wxOK | wxICON_ERROR);
277  }
278  }
279  }
280  }
281  }
282 
283  srv->ResetWorkspace();
285 }
286 
287 
User-defined methods of the data storage class.
User-defined methods of the data storage class.
CAsnObjectLoader.
CExtensionDeclaration - static declaration helper.
static wxString GetDialogFilter(EFileType fileType)
CGBDocument.
Definition: document.hpp:113
void OnCloseWorkspace(wxCommandEvent &event)
virtual void SetWorkbench(CWorkbench *wb)
virtual string GetModeName() const
void OnFileOpen(wxCommandEvent &event)
CIndexerAppModeExtension()
class CIndexerAppModeExtension
virtual string GetExtensionLabel() const
returns a displayable label for this extension ( please capitalize the key words - "My Extension" )
virtual string GetExtensionIdentifier() const
returns the unique human-readable identifier for the extension the id should use lowercase letters se...
class CIndexerProjectItemExtension
virtual void ProjectItemCreated(objects::CProjectItem &item, IServiceLocator *service_locator)
virtual string GetExtensionLabel() const
returns a displayable label for this extension ( please capitalize the key words - "My Extension" )
virtual void ProjectItemDetached(objects::CProjectItem &, CGBDocument &, IServiceLocator *)
virtual string GetExtensionIdentifier() const
returns the unique human-readable identifier for the extension the id should use lowercase letters se...
virtual void ProjectItemAttached(objects::CProjectItem &, CGBDocument &, IServiceLocator *)
static void SetSaveFilePath(bool save)
CObject –.
Definition: ncbiobj.hpp:180
static bool GetStr(const objects::CProjectItem &pi, const string &tag, string &value)
the item we represent
CProjectService - a service providing API for operations with Workspaces and Projects.
CRef –.
Definition: ncbiobj.hpp:618
CProjectSelectOptions - describes how new Project Items shall be added to a workspace.
void Set_CreateNewProject(const string &folder=kEmptyStr)
bool IsEntrys(void) const
Definition: Seq_submit.cpp:54
Base class for all serializable objects.
Definition: serialbase.hpp:150
Template class for iteration on objects of class C (non-medifiable version)
Definition: iterator.hpp:767
Template class for iteration on objects of class C.
Definition: iterator.hpp:673
CWorkbench - default implementation of IWorkbench.
static bool CleanUp()
IExtension IExtension interface represents an abstract pluggable component.
Definition: extension.hpp:57
IServiceLocator - an abstract mechanism for locating services.
Definition: service.hpp:71
bool empty() const
Definition: map.hpp:149
Definition: map.hpp:338
@ eCmdCloseWorkspace
Definition: commands.hpp:71
#define _(proto)
Definition: ct_nlmzip_i.h:78
#define NULL
Definition: ncbistd.hpp:225
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 CAppTaskService * GetAppTaskService()
void AddTask(IAppTask &task)
Add a task to the queue.
static void GetLabel(const CObject &obj, string *label, ELabelType type=eDefault)
Definition: label.cpp:140
@ eDefault
Definition: label.hpp:73
#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
bool IsSubmit(void) const
Check if variant Submit is selected.
const TSubmit & GetSubmit(void) const
Get the variant data.
list< CRef< CSeq_entry > > TEntrys
const TEntrys & GetEntrys(void) const
Get the variant data.
const TData & GetData(void) const
Get the Data member data.
bool IsEntrys(void) const
Check if variant Entrys is selected.
bool CanGetData(void) const
Check if it is safe to call GetData method.
USING_SCOPE(objects)
#define wxT(x)
Definition: muParser.cpp:41
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
#define EXT_POINT__OPEN_PROJECT_VIEW_EXTENSION
#define EXT_POINT__PROJECT_ITEM_EXTENSION
else result
Definition: token2.c:20
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
Modified on Thu Mar 28 17:05:50 2024 by modify_doxy.py rev. 669887