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 32659 2015-04-07 20:32:34Z 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: 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),
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  CRef<objects::CScope> scope;
128  ITERATE(vector<TConstScopedObjects>, it, m_InputObjects) {
129  ITERATE(TConstScopedObjects, it2, *it) {
130  if (!scope) {
131  scope = it2->scope;
132  continue;
133  } else {
134  if (scope != it2->scope) {
135  return "This tool gives unpredictable results with objects\nfrom different projects.\n\nPlease move all objects to the same project\nand try again.";
136  }
137  }
138  }
139  }
140 
141  return "";
142 }
143 
144 /* Example - how to implement this function
145 void CAlgoToolManagerBase::x_CreateParamsPanelIfNeeded()
146 {
147  if(m_ParamsPanel == NULL) {
148  x_SelectCompatibleInputObjects();
149 
150  m_ParamsPanel = new CMergeAlignmentsParamsPanel();
151  m_ParamsPanel->Hide(); // to reduce flicker
152  m_ParamsPanel->Create(m_ParentWindow);
153  m_ParamsPanel->SetParams(&m_Params);
154 
155  m_ParamsPanel->SetRegistryPath(m_RegPath + ".ParamsPanel");
156  m_ParamsPanel->LoadSettings();
157  }
158 }
159 */
160 
162  const CTypeInfo* typeInfo, map<string, TConstScopedObjects>& results)
163 {
164  NON_CONST_ITERATE(vector<TConstScopedObjects>, it, m_InputObjects) {
165  if (!it->empty()) {
166  CAsyncObjConvert convertor(typeInfo, *it, results);
167  CAsyncCall::Execute(convertor);
168  if (!results.empty())
169  return;
170  }
171  }
172 }
173 
174 
176  const CTypeInfo* typeInfo, TConstScopedObjects& results)
177 {
178  NON_CONST_ITERATE(vector<TConstScopedObjects>, it, m_InputObjects) {
179  if (!it->empty()) {
180  CAsyncObjConvert2 convertor(typeInfo, *it, results);
181  CAsyncCall::Execute(convertor);
182  if (!results.empty())
183  return;
184  }
185  }
186 }
187 
189 {
190  set<const CScope*> scopes;
191 
192  ITERATE(vector<TConstScopedObjects>, it, m_InputObjects) {
193  ITERATE(TConstScopedObjects, it2, *it) {
194  const CScope* scope = it2->scope.GetPointerOrNull();
195  if (scope) scopes.insert(scope);
196  }
197  }
198 
199  if (scopes.size() != 1)
200  return 0;
201 
202  const CScope* scope = *scopes.begin();
203 
205  if (!srv) return 0;
206 
207  CRef<CGBWorkspace> ws = srv->GetGBWorkspace();
208  if (!ws) return 0;
209 
210  CGBDocument* doc = dynamic_cast<CGBDocument*>(ws->GetProjectFromScope(*scope));
211  if (!doc) return 0;
212 
213  ICommandProccessor* cmdProcessor = &doc->GetUndoManager();
214  if (!cmdProcessor) return 0;
215 
216  return new CWeakExecuteGuard(*cmdProcessor);
217 }
218 
220 {
221  if(m_ProjectSelPanel == NULL) {
223 
224  /// initialize project params, x_InitProjectParams can be overridden
225  // in derived classes to select projects by input
227 
229 
232  }
233 }
234 
235 
237 {
238  switch( m_State ){
239  case eInvalid:
240  return action == eNext;
241  case eParams:
242  return action == eNext;
243  case eSelectProject:
244  return action == eNext || action == eBack;
245  case eCompleted:
246  return false; // nothing left to do
247  default:
248  _ASSERT(false);
249  return false;
250  }
251 }
252 
253 
255 {
256  return m_State == eSelectProject;
257 }
258 
259 
261 {
262  return m_State == eCompleted;
263 }
264 
265 
267 {
268  bool next = action == eNext;
269 
270  if(m_State == eInvalid && next) {
271  /// initial state - display Parameters panel
272  m_State = eParams;
274  x_GetParamsPanel()->TransferDataToWindow();
275  return true;
276  } else if(m_State == eParams) {
277  if(next) {
278  /// if input is valid in Parameters panel - display Projects Selection panel
279  wxPanel& panel = *x_GetParamsPanel();
280  if (panel.Validate() && panel.TransferDataFromWindow()) {
281  try{
282  if(x_ValidateParams()) {
284  // NOTE: The following code was changed to allow prototyping
285  // of editing tools that act on existing data and do not
286  // create new data objects (and therefore do not need to
287  // select a project for new data objects.
288  // The editing tool that inherits from CAlgoToolManagerBase
289  // overrides x_CreateProjectPanelIfNeeded with a function
290  // that does nothing (and therefore does not create m_ProjectSelPanel.
291  // The assumption is that if m_ProjectSelPanel is NULL then the
292  // state should be advanced to eCompleted.
293  // There is a minor problem in that the button at the bottom of the
294  // tool window will say "Next" when it should say "Finish", as the
295  // params panel is the last step before the tool acts on the data.
296  // This has been deemed acceptable for prototyping purposes.
297  // The original code looked like this:
298 #if 0
301 #endif
302  // The new code begins here.
303  if (m_ProjectSelPanel) {
306  } else {
308  }
309  // The new code ends here.
310  return true;
311  } else return false;
312  } catch (CException& e) {
313  LOG_POST(e.ReportAll());
314  NcbiErrorBox(e.GetMsg());
315  }
316  }
317  } else {
318  m_State = eInvalid;
319  return true;
320  }
321  return false;
322  } else if(m_State == eSelectProject) {
323  if(next) {
326  return true;
327  }
328  } else {
329  m_State = eParams;
330  x_GetParamsPanel()->TransferDataToWindow();
331  return true;
332  }
333  return false;
334  }
335 
336  _ASSERT(false);
337  return false;
338 }
339 
340 
342 {
344  NON_CONST_ITERATE(vector<TConstScopedObjects>, it, m_InputObjects) {
345  if (!it->empty()) {
347  break;
348  }
349  }
350 }
351 
352 
353 /// validate parameters after Params page
354 /// override this function in derived classes
356 {
357  return true;
358 }
359 
360 
362 {
365 
367  CSelectProjectOptions options;
369 
371  CRef<CDataLoadingAppTask> task(new CDataLoadingAppTask(srv, options, *job));
372  return task.Release();
373 }
374 
376 {
377  wxPanel* current_panel = GetCurrentPanel();
378  if (!current_panel)
379  return;
380  CAlgoToolManagerParamsPanel* params_panel = dynamic_cast<CAlgoToolManagerParamsPanel*>(current_panel);
381  if(!params_panel)
382  return;
383  if (wxOK != wxMessageBox("The active page settings will be restored to their original defaults.", "Confirm", wxOK | wxCANCEL))
384  return;
385  params_panel->RestoreDefaults();
386 }
387 
389 {
390  return m_Category;
391 }
392 
393 void CAlgoToolManagerBase::x_SetInputObjects( const vector<TConstScopedObjects>& objects )
394 {
395  m_InputObjects = objects;
396 }
397 
398 string CAlgoToolManagerBase::SetInputObjects( const vector<TConstScopedObjects>& objects )
399 {
400  x_SetInputObjects( objects );
401 
402  return x_ValidateInputObjects();
403 }
404 
405 static const char* kParamsSection = ".Params";
406 
408 {
409  m_RegPath = path; // store for later use
410 
412  if(params) {
413  string params_path = m_RegPath + kParamsSection;
414  params->SetRegistryPath(params_path);
415  }
416 }
417 
418 
420 {
421  if( ! m_RegPath.empty()) {
422  string path = m_RegPath + kParamsSection;
423  // save Tool Parameters
424  CAlgoToolManagerBase* nc_this = const_cast<CAlgoToolManagerBase*>(this);
425 
426  IRegSettings* params = nc_this->x_GetParamsAsRegSetting();
427  if(params) {
428  params->SaveSettings();
429  }
430 
431  // save UI state and settings
432  CAlgoToolManagerParamsPanel* params_panel = nc_this->x_GetParamsPanel();
433  if(params_panel) {
434  params_panel->SaveSettings();
435  }
436  }
437 }
438 
439 
441 {
442  if( ! m_RegPath.empty()) {
443  string path = m_RegPath + kParamsSection;
444 
445  // restore Tool Parameters
447  if(params) {
448  params->LoadSettings();
449  }
450 
451  // restore UI state and settings
453  if(params_panel) {
454  params_panel->LoadSettings();
455  }
456  }
457 }
458 
459 
460 ////////////////////////////////////////////////////////////////////////////////
461 /// CAlgoToolManagerParamsPanel
462 
464 {
465  m_RegPath = reg_path;
466 }
467 
468 
virtual void x_InitProjectParams()
init m_ProjectParams, in particular can select target project based on the tool input ...
virtual bool DoTransition(EAction action)
Performs transition if possible and returns true, otherwise the function shall warn the user about th...
virtual string SetInputObjects(const vector< TConstScopedObjects > &input)
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:844
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
CGBDocument.
Definition: document.hpp:117
virtual void x_CreateParamsPanelIfNeeded()=0
returns / creates Parameters panel, override in derived classes see cpp file for example ...
virtual void RestoreDefaults()=0
override in derived classes
IAppTask.
Definition: app_task.hpp:82
virtual void SetRegistryPath(const string &path)=0
virtual void CleanUI()
override this function in a derived class and clean extra members
CDataLoadingAppTask - a task that executes CDataLoadingAppJob.
virtual bool x_ValidateParams()
validates user input in Parameters panel, report errors if any
virtual bool IsCompletedState()
Manager goes into "Complete" state when "Finish" button is pressed and all input data is gatherred an...
IUIObject - object that provides basic properties often required in a UI object.
Definition: ui_object.hpp:61
string m_RegPath
registry path to the settings
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
SProjectSelectorParams m_ProjectParams
CProjectService - a service providing API for operations with Workspaces and Projects.
virtual void InitUI()
override this function in a derived class and initialize extra members
size_type size() const
Definition: set.hpp:132
USING_SCOPE(objects)
virtual wxPanel * GetCurrentPanel()
Return the panel corresponding to the current state of Tool Manager.
#define NULL
Definition: ncbistd.hpp:225
static const char * kParamsSection
void SetDataLocker(ILocker *locker)
wxWindow * m_ParentWindow
a window that will serve as a parent for our panels
virtual bool CanDo(EAction action)
Indicates whether given transition is possible in the current state.
virtual void LoadSettings()=0
virtual void x_SetInputObjects(const vector< TConstScopedObjects > &input)
virtual void SetRegistryPath(const string &path)
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:837
void Execute()
Definition: async_call.cpp:197
virtual CDataLoadingAppJob * x_CreateLoadingJob()=0
factory method for creating the job that executes the tool algorithm override in derived classes ...
virtual string GetCategory()
returns the name for the ToolCategory to which the tool belongs.
CProjectSelectorPanel - a panel that allows the user to specify how the project items created by a pl...
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
const_iterator begin() const
Definition: set.hpp:135
virtual void SaveSettings() const =0
virtual bool IsFinalState()
True if Tool Manager has reached its final state, i.e.
virtual IRegSettings * x_GetParamsAsRegSetting()=0
return a pointer to Parameters object as IRegSettings interface
string m_RegPath
registry path to the settings
virtual void x_CreateProjectPanelIfNeeded()
returns / creates Project panel
bool empty() const
Definition: map.hpp:149
void SelectProjectByObjects(TConstScopedObjects &objects, CProjectService *srv)
is all objects belong to the same project - selects the project
void GetParams(SProjectSelectorParams &params) const
#define LOG_POST(message)
Definition: ncbidiag.hpp:192
virtual IAppTask * GetTask()
Once parameters are gathered and validated this function is called to produce the final Task object t...
IServiceLocator - an abstract mechanism for locating services.
Definition: service.hpp:70
void ToLoadingOptions(CSelectProjectOptions &options)
CProjectSelectorPanel * m_ProjectSelPanel
void SetProjectService(CProjectService *service)
CUndoManager & GetUndoManager()
Definition: document.hpp:161
virtual void SetRegistryPath(const string &reg_path)
CAlgoToolManagerParamsPanel.
virtual void SetServiceLocator(IServiceLocator *srv_locator)
Sets / unsets Service Locator.
CException –.
Definition: ncbiexpt.hpp:709
vector< TConstScopedObjects > m_InputObjects
original input objects, the tool needs to select a subset of objects that can serve as valid input ...
vector< SConstScopedObject > TConstScopedObjects
Definition: objects.hpp:103
CScope –.
Definition: scope.hpp:90
virtual string x_ValidateInputObjects()
virtual void SaveSettings() const =0
override in derived classes
CGBProjectHandle * GetProjectFromScope(const CScope &scope)
virtual void SetParentWindow(wxWindow *parent)
const string m_Category
defines tool category
virtual void LoadSettings()=0
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:74
CUIObject m_Descriptor
describes the Manager's UI properties
IRegSettings An interface for objects that save / restore settings using CGuiRegistry.
CProjectSelectOptions - describes how new Project Items shall be added to a workspace.
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.
#define _ASSERT
CAlgoToolManagerParamsPanel.
void x_ConvertInputObjects(const CTypeInfo *typeInfo, map< string, TConstScopedObjects > &results)
Undo/Redo interface for editing operations.
virtual void RestoreDefaults()
Restores the default options for the current panel.
void SetParams(const SProjectSelectorParams &params)
Definition: set.hpp:44
virtual const IUIObject & GetDescriptor() const
Returns the object describing this tool (UI meta data).
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:435
void NcbiErrorBox(const string &message, const string &title="Error")
specialized Message Box function for reporting critical errors
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
virtual CAlgoToolManagerParamsPanel * x_GetParamsPanel()=0
returns a pointer to the parameters panel, override in derived classes
virtual void SaveSettings() const
EState m_State
tool manager state (int the Run Tool wizard)
string ReportAll(TDiagPostFlags flags=eDPF_Trace) const
Report all exceptions.
Definition: ncbiexpt.cpp:344
CAlgoToolManagerBase This is base class for simple algorithmic tool managers.
Modified on Tue Jul 28 11:22:49 2015 by modify_doxy.py rev. 426318