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

Go to the SVN repository for this file.

1 /* $Id: object_list_widget.cpp 47739 2024-06-28 18:13:02Z ivanov $
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, Yury Voronov
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
38 
39 #include <gui/utils/clipboard.hpp>
40 
44 #include <objects/seq/Bioseq.hpp>
45 
48 #include <gui/objutils/label.hpp>
49 
50 #include <wx/artprov.h>
51 #include <wx/textbuf.h>
52 
55 
56 ///////////////////////////////////////////////////////////////////////////////
57 /// CObjectListTableModel
58 
60 {
61  if( row < 0 || row >= GetNumRows() ){
62  _ASSERT(false);
63  NCBI_THROW( CException, eUnknown, "Invalid row index" );
64  }
65 }
66 
68 {
70 
71  int num_labels = GetNumExtraColumns(); //m_ObjectList->GetNumObjectLabels();
72  int num_columns = m_ObjectList->GetNumColumns();
73  int col_delta = m_ObjectList->GetColStartIx();
74 
75  // !labels first!
76 
77  if( col_delta < 0 || col_delta >= num_labels ){
78  return col < num_labels;
79  }
80 
81  return( col-col_delta < 0 || col-col_delta >= num_columns );
82 }
83 
84 
86 {
88 
89  int num_labels = GetNumExtraColumns(); //m_ObjectList->GetNumObjectLabels();
90  int num_columns = m_ObjectList->GetNumColumns();
91  int col_delta = m_ObjectList->GetColStartIx();
92 
93  // !labels first!
94 
95  if( col_delta < 0 || col_delta >= num_labels ){
96  // label : column
97  return col < num_labels ? col : col - num_labels;
98  }
99 
100  if( col-col_delta < 0 ){
101  // label
102  return col;
103 
104  } else if( col-col_delta >= num_columns ){
105  // label
106  return col - num_columns;
107 
108  } else {
109  // column
110  return col-col_delta;
111  }
112 }
113 
115 {
116 }
117 
119 {
120  m_ObjectList.Reset( &list );
121  m_Cache.clear();
122 
124 }
125 
127 {
128  m_ObjectList.Reset( new CObjectList() );
130  m_Cache.clear();
131 
133 }
134 
136 {
137  x_Clear();
138 
140 }
141 
143 {
145  m_Cache.clear();
146 }
147 
148 // IwxTableModel virtual functions implementation
149 
151 {
152  if( m_ObjectList.IsNull() ) return 0;
153 
154  return m_ObjectList->GetNumRows();
155 }
156 
158 {
159  if( m_ObjectList.IsNull() ) return 0;
160 
161  //return m_ObjectList->GetNumObjectLabels() + m_ObjectList->GetNumColumns();
163 }
164 
165 wxString CObjectListTableModel::GetColumnType( int col ) const
166 {
167  if( !x_ColIsLabel( col ) ){
168  int ol_col = x_AdjustColToData( col );
169 
171  switch( type ){
173  return wxT("string");
175  return wxT("int");
177  return wxT("double");
178  default:
179  _ASSERT(false);
180  break;
181  }
182  }
183 
184  return wxT("string");
185 }
186 
187 wxString CObjectListTableModel::GetColumnName( int col ) const
188 {
189  int ol_col = x_AdjustColToData( col );
190 
191  return
192  x_ColIsLabel( col )
193  ? GetExtraColumnName/*m_ObjectList->GetObjectLabelName*/( ol_col )
194  : ToWxString( m_ObjectList->GetColumnName( ol_col ) )
195  ;
196 }
197 
198 wxVariant CObjectListTableModel::GetValueAt( int row, int col ) const
199 {
201 
202  int ol_col = x_AdjustColToData( col );
203 
204  if( x_ColIsLabel( col ) ){
205 
206  return GetExtraValueAt( row, ol_col );
207 
208  /*
209  const CObject* obj = m_ObjectList->GetObject( row );
210  CScope* scope = const_cast<CScope*>( m_ObjectList->GetScope( row ) );
211 
212  CLabel::ELabelType type = m_ObjectList->GetObjectLabelType( ol_col );
213 
214  string label;
215  if( type == CLabel::eUserType || type == CLabel::eUserSubtype ){
216 
217  SConstScopedObject scoped_object(*obj, *scope);
218  CIRef<IGuiObjectInfo> gui_info(
219  CreateObjectInterface<IGuiObjectInfo>(scoped_object, NULL)
220  );
221 
222  if( type == CLabel::eUserType ){
223  label = gui_info->GetType();
224 
225  } else if( type == CLabel::eUserSubtype ){
226  label = gui_info->GetSubtype();
227  }
228  }
229 
230  if( label.empty() ){
231  CLabel::GetLabel( *obj, &label, type, scope );
232  }
233 
234  return ToWxString( label );
235  */
236 
237  } else {
239  switch( type ){
241  return ToWxString( m_ObjectList->GetString(ol_col, row) );
243  return wxVariant( m_ObjectList->GetInteger(ol_col, row) );
245  return wxVariant( m_ObjectList->GetDouble(ol_col, row) );
246 
247  default:
248  _ASSERT(false);
249  break;
250  }
251  }
252 
253  static wxString sError( wxT("Error") );
254  return sError;
255 }
256 
258 {
259  if( !x_ColIsLabel( col ) ){
260  int ol_col = x_AdjustColToData( col );
261 
263  switch( type ){
265  return null;
269  return CIRef<IwxStringFormat>( new CwxDoublePrintfFormat( wxT("%f") ) );
270 
271  default:
272  _ASSERT(false);
273  break;
274  }
275  }
276 
277  return null;
278 }
279 
280 wxString CObjectListTableModel::GetImageAliasAt( int row, int col ) const
281 {
283 
284  return col == 0 ? GetImageAlias( row ) : wxString("");
285 }
286 
288 {
289  SConstScopedObject sco(
291  const_cast<CScope*>(m_ObjectList->GetScope(row)));
292 
293  CIRef<IGuiObjectInfo> gui_info(CreateObjectInterface<IGuiObjectInfo>(sco, NULL));
294 
295  return gui_info.IsNull() ? wxString() : ToWxString( gui_info->GetIcon() );
296 }
297 
298 
299 /////////// Extra columns definition //////////////////
300 
302 {
304 }
305 
306 wxString CObjectListTableModel::GetExtraColumnType( int /* col */ ) const
307 {
308  return ToWxString("string");
309 }
310 
312 {
313  return ToWxString( m_ObjectList->GetObjectLabelName( col ) );
314 }
315 
316 wxVariant CObjectListTableModel::GetExtraValueAt( int row, int col ) const
317 {
318  if( col < 0 || col >= GetNumExtraColumns() ){
319  _ASSERT(false);
320  NCBI_THROW( CException, eUnknown, "Invalid extra column index" );
321  }
322 
323  string label;
324  if (x_GetCachedLabel(row, col, label))
325  return ToWxString( label );
326 
327  const CObject* obj = m_ObjectList->GetObject( row );
328  CScope* scope = const_cast<CScope*>( m_ObjectList->GetScope( row ) );
329 
331 
332  try {
334 
335  SConstScopedObject scoped_object(obj, scope);
336  CIRef<IGuiObjectInfo> gui_info(
337  CreateObjectInterface<IGuiObjectInfo>(scoped_object, NULL)
338  );
339 
340  if (!gui_info.IsNull()) {
341  if (type == CLabel::eUserType){
342  label = gui_info->GetType();
343 
344  }
345  else if (type == CLabel::eUserSubtype){
346  label = gui_info->GetSubtype();
347  }
348  }
349  }
350  }
351  catch (const CException& err) {
352  LOG_POST(Error << "Error in CObjectListTableModel::GetExtraValueAt: " << err);
353  }
354 
355  if( label.empty() ){
356  try {
357  CLabel::GetLabel( *obj, &label, type, scope );
358  } catch (const exception&) {
359  label = "N/A";
360  }
361  }
362 
363  x_SetCachedLabel(row, col, label);
364  return ToWxString( label );
365 }
366 
367 bool CObjectListTableModel::x_GetCachedLabel(int row, int col, string& value) const
368 {
369  if ((size_t)GetNumRows() != m_Cache.size()) {
370  m_Cache = vector<vector<CachedCell> >(GetNumRows(), vector<CachedCell>(GetNumExtraColumns()));
371  return false;
372  }
373 
374  if (m_Cache[row][col].m_Initialized) {
375  value = m_Cache[row][col].m_Value;
376  return true;
377  }
378  return false;
379 }
380 
381 void CObjectListTableModel::x_SetCachedLabel(int row, int col, const string& value) const
382 {
383  if ((size_t)GetNumRows() != m_Cache.size())
384  m_Cache = vector<vector<CachedCell> >(GetNumRows(), vector<CachedCell>(GetNumExtraColumns()));
385 
386  m_Cache[row][col] = CachedCell(value);
387 }
388 
390 {
391  return null;
392 }
393 
394 
395 ///////////////////////////////////////////////////////////////////////////////
396 /// CObjectListWidget
397 //IMPLEMENT_DYNAMIC_CLASS(CObjectListWidget,wxListCtrl)
398 
399 /*
400 BEGIN_EVENT_TABLE(CObjectListWidget, CwxTableListCtrl)
401  EVT_LIST_ITEM_SELECTED( wxID_ANY, CObjectListWidget::OnSelected )
402  EVT_LIST_ITEM_DESELECTED( wxID_ANY, CObjectListWidget::OnDeselected )
403 END_EVENT_TABLE()
404 */
405 
407  wxWindow* parent, wxWindowID id,
408  const wxPoint& pos, const wxSize& size, long style
409 )
410  : CwxTableListCtrl( parent, id, pos, size, style )
411 {
413 }
414 
416 {
418 }
419 
421 {
422  GetOLTModel()->SetObjectList( list );
423 }
424 
426 {
427  if( aModel == NULL ){
428  GetOLTModel()->SetObjectList( aList );
429 
430  } else {
431  aModel->SetObjectList( aList );
432  AssignModel( aModel );
433  }
434 }
435 
437 {
438  GetOLTModel()->Clear();
439 }
440 
442 {
443  wxString clip_str;
444 
445  TConstScopedObjects objs;
446  GetSelection( objs );
447 
448  bool first_row = true;
449  ITERATE( TConstScopedObjects, obj, objs ){
450 
451  string label;
453  *obj->object, &label,
455  const_cast<CScope*>( obj->scope.GetPointer() )
456  );
457 
458  if( first_row ){
459  first_row = false;
460  } else {
461  clip_str += wxTextBuffer::GetEOL();
462  }
463  clip_str += ToWxString( label );
464  }
465 
466  if( !clip_str.IsEmpty() ){
467  CSimpleClipboard::Instance().Set( clip_str );
468  }
469 }
470 
471 
473 {
474  wxArrayInt objIxs = GetDataRowsSelected();
475 
476  for( int i = 0; i < (int)objIxs.size(); ++i ){
477  int ix = objIxs[i];
478  CObject* obj = GetOLTModel()->GetObject(ix);
479  CScope* sc = GetOLTModel()->GetScope(ix);
480 
481  objects.push_back(SConstScopedObject(obj, sc));
482  }
483 }
484 
485 
487 {
488  long style = GetWindowStyle();
489  if (disabled) {
490  style |= wxLC_SINGLE_SEL;
491  }
492  else {
493  style &= ~wxLC_SINGLE_SEL;
494  }
495  SetWindowStyle(style);
496 }
497 
498 
500 {
501  SaveTableSettings(view);
502 }
503 
504 
506 {
507  LoadTableSettings(view);
508 }
509 
511 {
512  TConstScopedObjects sel_objects;
513  GetSelection( sel_objects );
514 
515  if( sel_objects.empty() ){
516  return;
517  }
518 
519  aMenu.Append( wxID_SEPARATOR, wxT("Top Actions") );
520 
521  vector<wxEvtHandler*> handlers;
523  aMenu,
524  handlers,
526  sel_objects
527  );
528 
530 }
531 
532 void CObjectListWidget::OnSelected( wxListEvent& anEvt )
533 {
534  //LOG_POST( Info << "Selected:" << anEvt.GetIndex() );
535  anEvt.Skip();
536 }
537 
538 void CObjectListWidget::OnDeselected( wxListEvent& anEvt )
539 {
540  //LOG_POST( Info << "Deselected:" << anEvt.GetIndex() );
541  anEvt.Skip();
542 }
543 
CObjectListTableModel.
virtual int GetNumRows() const
Returns the number of rows in the model.
virtual CIRef< IwxStringFormat > GetExtraColumnFormat(int col) const
virtual wxString GetColumnName(int col) const
Returns a default name for the column using spreadsheet conventions: A, B, C, ...
CRef< CObjectList > m_ObjectList
vector< vector< CachedCell > > m_Cache
void x_SetCachedLabel(int row, int col, const string &value) const
virtual wxString GetExtraColumnName(int col) const
virtual int GetNumExtraColumns() const
void x_AssertRowValid(int row) const
CObjectListTableModel.
void SetObjects(TConstScopedObjects &objects)
objects::CScope * GetScope(int row)
CObject * GetObject(int row)
virtual wxString GetColumnType(int col) const
Tries to extract actual type from row 0 value if it exists.
virtual wxVariant GetValueAt(int row, int col) const
virtual wxVariant GetExtraValueAt(int row, int col) const
int x_AdjustColToData(int col) const
virtual CIRef< IwxStringFormat > GetColumnFormat(int col) const
void SetObjectList(CObjectList &list)
virtual wxString GetExtraColumnType(int col) const
virtual wxString GetImageAlias(int row) const
virtual int GetNumColumns() const
Returns the number of columns in the model.
bool x_GetCachedLabel(int row, int col, string &value) const
virtual wxString GetImageAliasAt(int row, int col) const
bool x_ColIsLabel(int col) const
void OnSelected(wxListEvent &anEvt)
Debug & testing methods right now.
void DisableMultipleSelection(bool disabled=true)
void SetObjects(TConstScopedObjects &objects)
void Init(CObjectList &list, CObjectListTableModel *model=new CObjectListTableModel())
Inits widget with object list and optional model.
void GetSelection(TConstScopedObjects &objects)
ISelection-style API.
void SaveSettings(CRegistryWriteView &view)
CObjectListTableModel * GetOLTModel() const
void OnDeselected(wxListEvent &anEvt)
void LoadSettings(CRegistryReadView &view)
void SetObjectList(CObjectList &list)
virtual void AppendMenuItems(wxMenu &aMenu)
CObjectList Data structure representing a list of CObjects with associated Scopes and other optional ...
Definition: object_list.hpp:63
double GetDouble(int col, int row) const
const string & GetColumnName(int col) const
int GetColStartIx() const
EColumnType GetColumnType(int col) const
void Init(TConstScopedObjects &objects)
initializers
CLabel::ELabelType GetObjectLabelType(int col) const
CObject * GetObject(int row)
access to values (row, column)
const string & GetString(int col, int row) const
objects::CScope * GetScope(int row)
string GetObjectLabelName(int col) const
int GetNumObjectLabels() const
access to a set of extra columns
int GetNumRows() const
int GetInteger(int col, int row) const
int GetNumColumns() const
access to columns (attribute, indexes)
CObject –.
Definition: ncbiobj.hpp:180
class CRegistryReadView provides a nested hierarchical view at a particular key.
Definition: reg_view.hpp:58
CScope –.
Definition: scope.hpp:92
void Set(const T &val)
static CSimpleClipboard & Instance()
virtual void x_FireStructureChanged()
virtual void x_FireDataChanged()
CwxTableListCtrl - generic wxListCtrl-based Table Control.
void AssignModel(IwxTableModel *data_model)
void LoadTableSettings(const CRegistryReadView &view, bool byName=false)
wxArrayInt GetDataRowsSelected() const
virtual void AppendMenuItems(wxMenu &aMenu)
void SaveTableSettings(CRegistryWriteView &view, bool saveSorting=true) const
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#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
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
static void GetLabel(const CObject &obj, string *label, ELabelType type=eDefault)
Definition: label.cpp:140
ELabelType
Definition: label.hpp:60
vector< SConstScopedObject > TConstScopedObjects
Definition: objects.hpp:65
@ eUserSubtype
Definition: label.hpp:64
@ eDefault
Definition: label.hpp:73
@ eUserType
Definition: label.hpp:63
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
bool IsNull(void) const THROWS_NONE
Check if pointer is null – same effect as Empty().
Definition: ncbiobj.hpp:735
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
@ fWithCommas
Use commas as thousands separator.
Definition: ncbistr.hpp:254
static const char label[]
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
#define wxT(x)
Definition: muParser.cpp:41
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
#define EXT_POINT__SCOPED_OBJECTS__CMD_CONTRIBUTOR
This Extension Point allows external components to add commands that can be applied to scoped CObject...
void AddCommandsForScopedObjects(wxMenu &aMenu, vector< wxEvtHandler * > &handlers, const string &anExtPointId, TConstScopedObjects &objects, CUICommandRegistry *aCmdReg=NULL)
Helper function for adding commands provided by contributors to the menu.
USING_SCOPE(objects)
#define row(bind, expected)
Definition: string_bind.c:73
Definition: type.c:6
#define _ASSERT
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
Modified on Fri Sep 20 14:58:09 2024 by modify_doxy.py rev. 669887