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

Go to the SVN repository for this file.

1 /* $Id: item_selection_panel.cpp 42573 2019-03-21 17:53:20Z shkeda $
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:
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
35 
38 
40 
41 
42 #include <wx/sizer.h>
43 #include <wx/stattext.h>
44 #include <wx/textctrl.h>
45 #include <wx/srchctrl.h>
46 #include <wx/button.h>
47 #include <wx/icon.h>
48 
49 ////@begin includes
50 ////@end includes
51 
53 
54 IMPLEMENT_DYNAMIC_CLASS( CItemSelectionPanel, wxPanel )
55 
56 BEGIN_EVENT_TABLE( CItemSelectionPanel, wxPanel )
57 ////@begin CItemSelectionPanel event table entries
58  EVT_KEY_DOWN( CItemSelectionPanel::OnKeyDown )
59 
60  EVT_TEXT( ID_SEARCH, CItemSelectionPanel::OnSearchUpdated )
61 
62  EVT_BUTTON( ID_RESET, CItemSelectionPanel::OnResetClick )
63 
64 ////@end CItemSelectionPanel event table entries
67 
68 
70 {
71  Init();
72 }
73 
74 
75 CItemSelectionPanel::CItemSelectionPanel( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
76 {
77  Init();
78  Create(parent, id, pos, size, style);
79 }
80 
81 bool CItemSelectionPanel::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
82 {
83 ////@begin CItemSelectionPanel creation
84  wxPanel::Create( parent, id, pos, size, style );
85 
87  if (GetSizer())
88  {
89  GetSizer()->SetSizeHints(this);
90  }
91  Centre();
92 ////@end CItemSelectionPanel creation
93  return true;
94 }
95 
96 
98 {
99 ////@begin CItemSelectionPanel destruction
100 ////@end CItemSelectionPanel destruction
101 }
102 
103 
105 {
106 ////@begin CItemSelectionPanel member initialisation
107  m_SearchCtrl = NULL;
108  m_ResetBtn = NULL;
109  m_StatusText = NULL;
110  m_Splitter = NULL;
111 ////@end CItemSelectionPanel member initialisation
112 }
113 
114 
116 {
117 ////@begin CItemSelectionPanel content construction
118  CItemSelectionPanel* itemPanel1 = this;
119 
120  wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
121  itemPanel1->SetSizer(itemBoxSizer2);
122 
123  wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
124  itemBoxSizer2->Add(itemBoxSizer3, 0, wxGROW|wxLEFT|wxRIGHT, 5);
125 
126  wxStaticText* itemStaticText4 = new wxStaticText( itemPanel1, wxID_STATIC, _("Filter:"), wxDefaultPosition, wxDefaultSize, 0 );
127  itemBoxSizer3->Add(itemStaticText4, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
128 
129  m_SearchCtrl = new wxTextCtrl( itemPanel1, ID_SEARCH, wxT(""), wxDefaultPosition, wxSize(itemPanel1->ConvertDialogToPixels(wxSize(120, -1)).x, -1), 0 );
130  itemBoxSizer3->Add(m_SearchCtrl, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);
131 
132  m_ResetBtn = new wxButton( itemPanel1, ID_RESET, _("Reset"), wxDefaultPosition, wxDefaultSize, 0 );
133  itemBoxSizer3->Add(m_ResetBtn, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
134 
135  m_StatusText = new wxStaticText( itemPanel1, ID_STATUS, _("Filter:"), wxDefaultPosition, wxDefaultSize, 0 );
136  itemBoxSizer3->Add(m_StatusText, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);
137 
138  m_Splitter = new CSplitter( itemPanel1, ID_SPLITTER, wxDefaultPosition, wxSize(50, 360), 0 );
139  itemBoxSizer2->Add(m_Splitter, 1, wxGROW|wxALL, 5);
140 
141 ////@end CItemSelectionPanel content construction
142  int sizes[3] = { 300, 60, -1 };
145 
146  m_MapWidget = new CGroupMapWidget( m_Splitter, ID_MAP_WIDGET, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
147 
148  m_DescrCtrl = new wxTextCtrl( m_Splitter, IDDESCR, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
149 
152 
153  CMapControl* map_control = m_MapWidget->GetMapControl();
154  CMapControl::SProperties& props = map_control->GetProperties();
155  props.m_ColumnWidth = 240;
156  map_control->SetFocus();
157 }
158 
159 
161 {
163 }
164 
166 
167 void CItemSelectionPanel::SetItems(vector<TItemRef>& Items)
168 {
169  m_AllItems = Items;
171  x_FilterItems("");
172 }
173 
174 
175 // populates m_MapWidget with items for a set of objects in m_Items
177 {
179 
180  for( size_t i = 0; i < m_Items.size(); i++ ) {
181  TItemRef Item = m_Items[i];
182  x_AddItem(grp_descrs, *Item);
183  }
184 
185  // add number of items to the group name
186  string s;
187  for( size_t i = 0; i < grp_descrs.size(); i++ ) {
188  CGroupMapWidget::SGroupDescr& descr = grp_descrs[i];
189 
190  vector<string>::const_iterator it =
191  find(m_CollapsedGroups.begin(), m_CollapsedGroups.end(), descr.m_Label);
192  if (it != m_CollapsedGroups.end())
193  descr.m_Expanded = false;
194 
195  size_t n = descr.m_Items.size();
196  if(n == 0) {
197  s = " ( empty )";
198  } else if(n == 1) {
199  s = " ( 1 " + m_ItemTypeLabel + " )";
200  } else {
201  s = " ( " + NStr::SizetToString(n) + " " + m_ItemTypeLabel + "s )";
202  }
203  descr.m_Label += s;
204  }
205 
206  m_CollapsedGroups.clear();
207 
208  m_MapWidget->Init(grp_descrs);
209 }
210 
211 
212 // selected Items from m_AllItems that match the given query and
213 // place them in m_Items
214 
216 {
217  bool reset = query.empty();
218 
219  if(reset) {
221  } else {
222  m_Items.clear();
223 
224  for( size_t i = 0; i < m_AllItems.size(); i++ ) {
225  TItemRef item = m_AllItems[i];
226  const IUIObject& ui_obj = item->GetDescriptor();
227 
228  const string& label = ui_obj.GetLabel();
229  if(NStr::FindNoCase(label, query) != string::npos) {
230  m_Items.push_back(item);
231  continue;
232  }
233 
234  const string& desc = ui_obj.GetDescription();
235  if(NStr::FindNoCase(desc, query) != string::npos) {
236  m_Items.push_back(item);
237  continue;
238  }
239  }
240  }
241 
243 
244  // Update Filter status text
245  string status = reset ? "All " : "Filtered - ";
246  size_t n = m_Items.size();
247 
248  if( ! reset && n == 0) {
249  status += "no matches";
250  } else {
251  status += NStr::SizetToString(n);
252  status += " " + m_ItemTypeLabel + (n != 1 ? "s are" : " is");
253  status += " shown";
254  }
255 
256  m_StatusText->SetLabel(ToWxString(status));
257 }
258 
259 
261  IItemWithDescription& item)
262 {
263  string group = item.GetCategory();
264 
266  const IUIObject& ui_obj = item.GetDescriptor();
267  descr.m_Label = ui_obj.GetLabel();
268  descr.m_Description = ui_obj.GetDescription();
269  descr.m_IconAlias = ui_obj.GetIconAlias();
270  descr.m_ShowDefalt = item.ShowDefault();
271 
272  TGroupDescr * grp_descr = NULL;
273  // find group
274  for( size_t i = 0; i < descrs.size(); i++ ) {
275  if(descrs[i].m_Label == group) {
276  grp_descr = &descrs[i];
277  break;
278  }
279  }
280 
281  if(grp_descr == NULL) { // create a new group
282  descrs.push_back(TGroupDescr(group));
283  grp_descr = &descrs.back();
284  }
285 
286  grp_descr->m_Items.push_back(descr);
287 }
288 
289 
291 {
292  for( size_t i = 0; i < m_Items.size(); i++ ) {
293  TItemRef item = m_Items[i];
294  const IUIObject& ui_obj = item->GetDescriptor();
295  if(ui_obj.GetLabel() == label) {
296  return i;
297  }
298  }
299  return (size_t) -1;
300 }
301 
302 
303 
304 void CItemSelectionPanel::OnSearchUpdated( wxCommandEvent& event )
305 {
306  wxString s_val = m_SearchCtrl->GetValue();
307 
308  string query = ToStdString(s_val);
310 }
311 
312 
313 void CItemSelectionPanel::OnResetClick( wxCommandEvent& event )
314 {
315  m_SearchCtrl->SetValue(wxT(""));
316 }
317 
318 
320 {
321  TItemRef item;
322 
323  string label = m_MapWidget->GetSelectedItem();
324  size_t index = x_FindItemByLabel(label);
325  if(index != (size_t) -1) {
326  item = m_Items[index];
327  }
328  return item;
329 }
330 
331 void CItemSelectionPanel::OnItemSelected(wxCommandEvent& event)
332 {
333  string descr;
334 
336  if (curItem) {
337  descr = curItem->GetFullDescr();
338  }
339 
340  m_DescrCtrl->SetValue(ToWxString(descr));
341 }
342 
343 
344 void CItemSelectionPanel::SetRegistryPath(const string& path)
345 {
346  m_RegPath = path; // store for later use
347 }
348 
349 
350 static const char* kSplitterWidthsTag = "SplitterWidths";
351 static const char* kCollapsedGroupsTag = "CollapsedGroups";
352 
354 {
355  if( ! m_RegPath.empty()) {
357  CRegistryWriteView view = gui_reg.GetWriteView(m_RegPath);
358 
359  // save splitter configuration
360  vector<int> heights;
361  m_Splitter->GetHeights(heights);
362  view.Set(kSplitterWidthsTag, heights);
363 
364  vector<string> groups, collapsed;
365  m_MapWidget->GetGroupLabels(groups);
366  ITERATE(vector<string>, it, groups) {
367  if (!m_MapWidget->IsExpanded(*it)) {
368  string::size_type pos = it->find(" (");
369  string str = (pos == string::npos) ? *it : it->substr(0, pos);
370  collapsed.push_back(str);
371  }
372  }
373 
374  view.Set(kCollapsedGroupsTag, collapsed);
375  }
376 }
377 
378 
380 {
381  if( ! m_RegPath.empty()) {
383  CRegistryReadView view = gui_reg.GetReadView(m_RegPath);
384 
385  // load splitter configuration
386  vector<int> heights;
387  view.GetIntVec(kSplitterWidthsTag, heights);
388  if(heights.size() == 2) {
389  m_Splitter->SetHeights(heights);
390  }
391 
393  }
394 }
395 
396 
398 {
399  return true;
400 }
401 
402 
403 wxBitmap CItemSelectionPanel::GetBitmapResource( const wxString& name )
404 {
405  // Bitmap retrieval
406 ////@begin CItemSelectionPanel bitmap retrieval
407  wxUnusedVar(name);
408  return wxNullBitmap;
409 ////@end CItemSelectionPanel bitmap retrieval
410 }
411 
412 
413 wxIcon CItemSelectionPanel::GetIconResource( const wxString& name )
414 {
415  // Icon retrieval
416 ////@begin CItemSelectionPanel icon retrieval
417  wxUnusedVar(name);
418  return wxNullIcon;
419 ////@end CItemSelectionPanel icon retrieval
420 }
421 
422 void CItemSelectionPanel::OnKeyDown(wxKeyEvent& event)
423 {
424  int code = event.GetKeyCode();
425  if(event.ControlDown() && (code == 'F' || code == 'f')) {
426  m_SearchCtrl->SetFocus();
427  } else {
428  event.Skip();
429  }
430 
431 }
432 
434 
435 
CConstRef –.
Definition: ncbiobj.hpp:1266
CGroupMapWidget.
CConstRef< CGroupItem > GetCurrentItem() const
vector< SGroupDescr > TGroupDescrVector
virtual string GetSelectedItem() const
virtual CMapControl * GetMapControl()
virtual void Init(const TGroupDescrVector &groups)
virtual void GetGroupLabels(vector< string > &groups) const
virtual bool IsExpanded(const string &group) const
CRegistryWriteView GetWriteView(const string &section)
get a read-write view at a particular level.
Definition: registry.cpp:462
static CGuiRegistry & GetInstance()
access the application-wide singleton
Definition: registry.cpp:400
CRegistryReadView GetReadView(const string &section) const
get a read-only view at a particular level.
Definition: registry.cpp:428
CItemSelectionPanel.
CMapControl owns all its items.
Definition: map_control.hpp:67
SProperties & GetProperties()
class CRegistryReadView provides a nested hierarchical view at a particular key.
Definition: reg_view.hpp:58
void GetIntVec(const string &key, vector< int > &val) const
Definition: reg_view.cpp:252
void GetStringVec(const string &key, vector< string > &val) const
Definition: reg_view.cpp:263
void Set(const string &key, int val)
access a named key at this level, with no recursion
Definition: reg_view.cpp:533
CSplitter - container with resizable separators.
Definition: splitter.hpp:54
void SetResizableCell(int col, int row)
Definition: splitter.cpp:321
bool InsertToCell(wxWindow *child, int col, int row)
If cell [col, row] exists and vacant - adds widget to the container.
Definition: splitter.cpp:374
void GetHeights(TPosVector &heights) const
Definition: splitter.cpp:338
void SetHeights(const TPosVector &heights)
Definition: splitter.cpp:344
virtual void Split(ESplitType type)
Creates a splitter with a single cell.
Definition: splitter.cpp:147
IUIObject - object that provides basic properties often required in a UI object.
Definition: ui_object.hpp:63
#define _(proto)
Definition: ct_nlmzip_i.h:78
static void Init(void)
Definition: cursor6.c:76
static const char * str(char *buf, int n)
Definition: stats.c:84
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NULL
Definition: ncbistd.hpp:225
void x_AddItem(CGroupMapWidget::TGroupDescrVector &descrs, IItemWithDescription &manager)
void OnKeyDown(wxKeyEvent &event)
wxIcon GetIconResource(const wxString &name)
vector< TItemRef > m_Items
void SetItemTypeLabel(const string &label)
wxBitmap GetBitmapResource(const wxString &name)
virtual void SetRegistryPath(const string &path)
void OnResetClick(wxCommandEvent &event)
virtual string GetCategory()=0
virtual void SaveSettings() const
void SetItems(vector< TItemRef > &items)
void x_FilterItems(const string &query)
void OnItemSelected(wxCommandEvent &event)
virtual bool ShowDefault() const =0
vector< TItemRef > m_AllItems
vector< string > m_CollapsedGroups
CGroupMapWidget * m_MapWidget
bool Create(wxWindow *parent, wxWindowID id=ID_CITEMSELECTIONPANEL, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxNO_BORDER|wxCLIP_CHILDREN|wxTAB_TRAVERSAL)
void OnSearchUpdated(wxCommandEvent &event)
virtual const IUIObject & GetDescriptor() const =0
size_t x_FindItemByLabel(const string &label)
virtual const string & GetIconAlias() const =0
virtual const string & GetLabel() const =0
virtual const string & GetDescription() const =0
#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 string SizetToString(size_t value, TNumToStringFlags flags=0, int base=10)
Convert size_t to string.
Definition: ncbistr.cpp:2751
static SIZE_TYPE FindNoCase(const CTempString str, const CTempString pattern, SIZE_TYPE start, SIZE_TYPE end, EOccurrence which=eFirst)
Find the pattern in the specified range of a string using a case insensitive search.
Definition: ncbistr.cpp:2993
static const char label[]
static const char * kCollapsedGroupsTag
static const char * kSplitterWidthsTag
CGroupMapWidget::SGroupDescr TGroupDescr
END_EVENT_TABLE()
int i
yy_size_t n
#define wxT(x)
Definition: muParser.cpp:41
const struct ncbi::grid::netcache::search::fields::SIZE size
static static static wxID_ANY
static string query
Definition: inftrees.h:24
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
string ToStdString(const wxString &s)
Definition: wx_utils.hpp:161
Modified on Wed May 01 14:18:08 2024 by modify_doxy.py rev. 669887