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

Go to the SVN repository for this file.

1 /* $Id: algo_tool_manager_base.cpp 46706 2021-09-09 19:54:51Z asztalos $
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: Andrey Yazhuk
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 
33 
36 #include <gui/core/document.hpp>
38 
40 
42 
44 
46 
48 
49 #include <wx/panel.h>
50 #include <wx/msgdlg.h>
51 
54 
55 ///////////////////////////////////////////////////////////////////////////////
56 /// CAlgoToolManagerBase
58  const string& icon_alias,
59  const string& hint,
60  const string& description,
61  const string& help_id,
62  const string& category)
63 : m_Descriptor(label, icon_alias, hint, description, help_id, "tools"),
64  m_Category(category),
65  m_SrvLocator(NULL),
66  m_ParentWindow(NULL),
67  m_State(eInvalid),
68  m_ProjectSelPanel(NULL)
69 {
70 }
71 
72 
74 {
75  m_SrvLocator = srv_locator;
76 }
77 
78 
80 {
81  m_ParentWindow = parent;
82 }
83 
84 
86 {
87  return m_Descriptor;
88 }
89 
90 
91 /// override this function in a derived class and initialize extra members
93 {
94  // initially tool panels are not displayed and so the first
95  // step of the Run Tool dialog corresponds to invalid state
96  m_State = eInvalid;
97  m_InputObjects.clear();
98 
100 }
101 
102 
103 /// override this function in a derived class and clean extra members
105 {
106  m_State = eInvalid;
107  m_InputObjects.clear();
108 
110 }
111 
112 
114 {
115  switch(m_State) {
116  case eParams:
117  return x_GetParamsPanel();
118  case eSelectProject:
119  return m_ProjectSelPanel;
120  default:
121  return NULL;
122  }
123 }
124 
126 {
127  if (!UsesSingleOMScope())
128  return "";
129 
130  CRef<objects::CScope> scope;
131  ITERATE(vector<TConstScopedObjects>, it, m_InputObjects) {
132  ITERATE(TConstScopedObjects, it2, *it) {
133  if (!scope) {
134  scope = it2->scope;
135  continue;
136  } else {
137  if (scope != it2->scope) {
138  return "This tool gives unpredictable results with objects\nfrom different projects.\n\nPlease move all objects to the same project\nand try again.";
139  }
140  }
141  }
142  }
143 
144  return "";
145 }
146 
147 /* Example - how to implement this function
148 bool CAlgoToolManagerBase::x_CreateParamsPanelIfNeeded()
149 {
150  if(m_ParamsPanel == NULL) {
151  x_SelectCompatibleInputObjects();
152 
153  m_ParamsPanel = new CMergeAlignmentsParamsPanel();
154  m_ParamsPanel->Hide(); // to reduce flicker
155  m_ParamsPanel->Create(m_ParentWindow);
156  m_ParamsPanel->SetParams(&m_Params);
157 
158  m_ParamsPanel->SetRegistryPath(m_RegPath + ".ParamsPanel");
159  m_ParamsPanel->LoadSettings();
160  }
161  return true;
162 }
163 */
164 
166  const CTypeInfo* typeInfo, map<string, TConstScopedObjects>& results)
167 {
168  NON_CONST_ITERATE(vector<TConstScopedObjects>, it, m_InputObjects) {
169  if (!it->empty()) {
170  AsyncConvertObjects(typeInfo, *it, results);
171  if (!results.empty())
172  return;
173  }
174  }
175 }
176 
177 
179  const CTypeInfo* typeInfo, TConstScopedObjects& results)
180 {
181  NON_CONST_ITERATE(vector<TConstScopedObjects>, it, m_InputObjects) {
182  if (!it->empty()) {
183  AsyncConvertObjects(typeInfo, *it, results);
184  if (!results.empty())
185  return;
186  }
187  }
188 }
189 
191 {
192  set<const CScope*> scopes;
193 
194  ITERATE(vector<TConstScopedObjects>, it, m_InputObjects) {
195  ITERATE(TConstScopedObjects, it2, *it) {
196  const CScope* scope = it2->scope.GetPointerOrNull();
197  if (scope) scopes.insert(scope);
198  }
199  }
200 
201  if (scopes.size() != 1)
202  return 0;
203 
204  const CScope* scope = *scopes.begin();
205 
207  if (!srv) return 0;
208 
209  CRef<CGBWorkspace> ws = srv->GetGBWorkspace();
210  if (!ws) return 0;
211 
212  CGBDocument* doc = dynamic_cast<CGBDocument*>(ws->GetProjectFromScope(*scope));
213  if (!doc) return 0;
214 
215  ICommandProccessor* cmdProcessor = &doc->GetUndoManager();
216  if (!cmdProcessor) return 0;
217 
218  return new CWeakExecuteGuard(*cmdProcessor);
219 }
220 
222 {
223  if (UsesSingleOMScope()) {
225  }
226  else if(m_ProjectSelPanel == NULL) {
228 
229  /// initialize project params, x_InitProjectParams can be overridden
230  // in derived classes to select projects by input
232 
234 
237  }
238 }
239 
240 
242 {
243  switch( m_State ){
244  case eInvalid:
245  return action == eNext;
246  case eParams:
247  return action == eNext;
248  case eSelectProject:
249  return action == eNext || action == eBack;
250  case eCompleted:
251  return false; // nothing left to do
252  default:
253  _ASSERT(false);
254  return false;
255  }
256 }
257 
258 
260 {
261 
263 }
264 
265 
267 {
268  return m_State == eCompleted;
269 }
270 
271 
273 {
274  bool next = action == eNext;
275 
276  if(m_State == eInvalid && next) {
278  if (!created) {
279  LOG_POST(Info << "CAlgoToolManagerBase::DoTransition() - Objects conversion was canceled or no input objects were found");
280  return false;
281  }
282 
283  /// initial state - display Parameters panel
284  m_State = eParams;
285  x_GetParamsPanel()->TransferDataToWindow();
286  return true;
287  } else if(m_State == eParams) {
288  if(next) {
289  /// if input is valid in Parameters panel - display Projects Selection panel
290  wxPanel& panel = *x_GetParamsPanel();
291  if (panel.Validate() && panel.TransferDataFromWindow()) {
292  try{
293  if(x_ValidateParams()) {
295  // NOTE: The following code was changed to allow prototyping
296  // of editing tools that act on existing data and do not
297  // create new data objects (and therefore do not need to
298  // select a project for new data objects.
299  // The editing tool that inherits from CAlgoToolManagerBase
300  // overrides x_CreateProjectPanelIfNeeded with a function
301  // that does nothing (and therefore does not create m_ProjectSelPanel.
302  // The assumption is that if m_ProjectSelPanel is NULL then the
303  // state should be advanced to eCompleted.
304  // There is a minor problem in that the button at the bottom of the
305  // tool window will say "Next" when it should say "Finish", as the
306  // params panel is the last step before the tool acts on the data.
307  // This has been deemed acceptable for prototyping purposes.
308  // The original code looked like this:
309 #if 0
312 #endif
313  // The new code begins here.
314  if (m_ProjectSelPanel) {
317  } else {
319  }
320  // The new code ends here.
321  return true;
322  } else return false;
323  } catch (CException& e) {
324  LOG_POST(e.ReportAll());
325  NcbiErrorBox(e.GetMsg());
326  }
327  }
328  } else {
329  m_State = eInvalid;
330  return true;
331  }
332  return false;
333  } else if(m_State == eSelectProject) {
334  if(next) {
337  return true;
338  }
339  } else {
340  m_State = eParams;
341  x_GetParamsPanel()->TransferDataToWindow();
342  return true;
343  }
344  return false;
345  }
346 
347  _ASSERT(false);
348  return false;
349 }
350 
351 
353 {
355  NON_CONST_ITERATE(vector<TConstScopedObjects>, it, m_InputObjects) {
356  if (!it->empty()) {
358  m_ProjectParams.m_FolderName = "Tool Results";
360  break;
361  }
362  }
363 }
364 
365 
366 /// validate parameters after Params page
367 /// override this function in derived classes
369 {
370  return true;
371 }
372 
373 
375 {
377  job->SetDataLocker(x_GetDataLocker());
378 
379  if (m_ProjectSelPanel)
381  CSelectProjectOptions options;
383 
385  CRef<CDataLoadingAppTask> task(new CDataLoadingAppTask(srv, options, *job));
386  return task.Release();
387 }
388 
390 {
391  wxPanel* current_panel = GetCurrentPanel();
392  if (!current_panel)
393  return;
394  CAlgoToolManagerParamsPanel* params_panel = dynamic_cast<CAlgoToolManagerParamsPanel*>(current_panel);
395  if(!params_panel)
396  return;
397  if (wxOK != wxMessageBox("The active page settings will be restored to their original defaults.", "Confirm", wxOK | wxCANCEL))
398  return;
399  params_panel->RestoreDefaults();
400 }
401 
403 {
404  return m_Category;
405 }
406 
407 void CAlgoToolManagerBase::x_SetInputObjects( const vector<TConstScopedObjects>& objects )
408 {
410 }
411 
412 string CAlgoToolManagerBase::SetInputObjects( const vector<TConstScopedObjects>& objects )
413 {
415 
416  return x_ValidateInputObjects();
417 }
418 
419 static const char* kParamsSection = ".Params";
420 
422 {
423  m_RegPath = path; // store for later use
424 
426  if(params) {
427  string params_path = m_RegPath + kParamsSection;
428  params->SetRegistryPath(params_path);
429  }
430 }
431 
432 
434 {
435  if( ! m_RegPath.empty()) {
436  string path = m_RegPath + kParamsSection;
437  // save Tool Parameters
438  CAlgoToolManagerBase* nc_this = const_cast<CAlgoToolManagerBase*>(this);
439 
440  IRegSettings* params = nc_this->x_GetParamsAsRegSetting();
441  if(params) {
442  params->SaveSettings();
443  }
444 
445  // save UI state and settings
446  CAlgoToolManagerParamsPanel* params_panel = nc_this->x_GetParamsPanel();
447  if(params_panel) {
448  params_panel->SaveSettings();
449  }
450  }
451 }
452 
453 
455 {
456  if( ! m_RegPath.empty()) {
457  string path = m_RegPath + kParamsSection;
458 
459  // restore Tool Parameters
461  if(params) {
462  params->LoadSettings();
463  }
464 
465  // restore UI state and settings
467  if(params_panel) {
468  params_panel->LoadSettings();
469  }
470  }
471 }
472 
473 
474 ////////////////////////////////////////////////////////////////////////////////
475 /// CAlgoToolManagerParamsPanel
476 
478 {
479  m_RegPath = reg_path;
480 }
481 
482 
USING_SCOPE(objects)
static const char * kParamsSection
void AsyncConvertObjects(const CTypeInfo *typeInfo, const TConstScopedObjects &inputObjects, map< string, TConstScopedObjects > &results)
CAlgoToolManagerBase This is base class for simple algorithmic tool managers.
virtual bool x_CreateParamsPanelIfNeeded()=0
returns / creates Parameters panel, override in derived classes see cpp file for example
virtual IRegSettings * x_GetParamsAsRegSetting()=0
return a pointer to Parameters object as IRegSettings interface
virtual string GetCategory()
returns the name for the ToolCategory to which the tool belongs.
virtual string SetInputObjects(const vector< TConstScopedObjects > &input)
sets input objects that can be used as arguments for the tool.
CUIObject m_Descriptor
describes the Manager's UI properties
virtual void x_CreateProjectPanelIfNeeded()
returns / creates Project panel
virtual string x_ValidateInputObjects()
virtual void x_SetInputObjects(const vector< TConstScopedObjects > &input)
EState m_State
tool manager state (int the Run Tool wizard)
virtual void RestoreDefaults()
Restores the default options for the current panel.
virtual bool DoTransition(EAction action)
Performs transition if possible and returns true, otherwise the function shall warn the user about th...
virtual wxPanel * GetCurrentPanel()
Return the panel corresponding to the current state of Tool Manager.
CProjectSelectorPanel * m_ProjectSelPanel
virtual void InitUI()
override this function in a derived class and initialize extra members
virtual void SetRegistryPath(const string &path)
virtual bool CanDo(EAction action)
Indicates whether given transition is possible in the current state.
virtual void SetServiceLocator(IServiceLocator *srv_locator)
Sets / unsets Service Locator.
virtual bool x_ValidateParams()
validates user input in Parameters panel, report errors if any
virtual IAppTask * GetTask()
Once parameters are gathered and validated this function is called to produce the final Task object t...
virtual void SaveSettings() const
string m_RegPath
registry path to the settings
virtual bool IsCompletedState()
Manager goes into "Complete" state when "Finish" button is pressed and all input data is gatherred an...
wxWindow * m_ParentWindow
a window that will serve as a parent for our panels
virtual CDataLoadingAppJob * x_CreateLoadingJob()=0
factory method for creating the job that executes the tool algorithm override in derived classes
virtual void CleanUI()
override this function in a derived class and clean extra members
const string m_Category
defines tool category
CAlgoToolManagerBase(const string &label, const string &icon_alias, const string &hint=kEmptyStr, const string &description=kEmptyStr, const string &help_id=kEmptyStr, const string &category="Unknown")
CAlgoToolManagerBase.
virtual void x_InitProjectParams()
init m_ProjectParams, in particular can select target project based on the tool input
void x_ConvertInputObjects(const CTypeInfo *typeInfo, map< string, TConstScopedObjects > &results)
vector< TConstScopedObjects > m_InputObjects
original input objects, the tool needs to select a subset of objects that can serve as valid input
virtual bool IsFinalState()
True if Tool Manager has reached its final state, i.e.
virtual void SetParentWindow(wxWindow *parent)
virtual const IUIObject & GetDescriptor() const
Returns the object describing this tool (UI meta data).
virtual CAlgoToolManagerParamsPanel * x_GetParamsPanel()=0
returns a pointer to the parameters panel, override in derived classes
SProjectSelectorParams m_ProjectParams
CAlgoToolManagerParamsPanel.
virtual void LoadSettings()=0
virtual void SaveSettings() const =0
override in derived classes
string m_RegPath
registry path to the settings
virtual void SetRegistryPath(const string &reg_path)
CAlgoToolManagerParamsPanel.
virtual void RestoreDefaults()=0
override in derived classes
CDataLoadingAppTask - a task that executes CDataLoadingAppJob.
CGBDocument.
Definition: document.hpp:113
CUndoManager & GetUndoManager()
Definition: document.hpp:158
CProjectSelectorPanel - a panel that allows the user to specify how the project items created by a pl...
void SetProjectService(CProjectService *service)
void GetParams(SProjectSelectorParams &params) const
void SetParams(const SProjectSelectorParams &params)
CProjectService - a service providing API for operations with Workspaces and Projects.
CScope –.
Definition: scope.hpp:92
CProjectSelectOptions - describes how new Project Items shall be added to a workspace.
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
IAppTask.
Definition: app_task.hpp:83
Undo/Redo interface for editing operations.
IRegSettings An interface for objects that save / restore settings using CGuiRegistry.
virtual void SaveSettings() const =0
virtual void LoadSettings()=0
virtual void SetRegistryPath(const string &path)=0
IServiceLocator - an abstract mechanism for locating services.
Definition: service.hpp:71
IUIObject - object that provides basic properties often required in a UI object.
Definition: ui_object.hpp:63
virtual bool UsesSingleOMScope()
bool empty() const
Definition: map.hpp:149
Definition: set.hpp:45
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
const_iterator begin() const
Definition: set.hpp:135
size_type size() const
Definition: set.hpp:132
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:56
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#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
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
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
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
void NcbiErrorBox(const string &message, const string &title="Error")
specialized Message Box function for reporting critical errors
vector< SConstScopedObject > TConstScopedObjects
Definition: objects.hpp:65
#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 const char label[]
const struct ncbi::grid::netcache::search::fields::CREATED created
void SelectProjectByObjects(TConstScopedObjects &objects, CProjectService *srv)
is all objects belong to the same project - selects the project
bool m_CreateFolder
package in a single item
void ToLoadingOptions(CSelectProjectOptions &options)
#define _ASSERT
Modified on Wed Jul 31 17:12:43 2024 by modify_doxy.py rev. 669887