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

Go to the SVN repository for this file.

1 /* $Id: string_constraint_panel.cpp 45332 2020-07-07 16:56:10Z 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: Colleen Bollin
27  */
28 
29 
30 #include <ncbi_pch.hpp>
34 
35 #include <wx/panel.h>
36 #include <wx/sizer.h>
37 #include <wx/button.h>
38 
39 
41 
42 /*!
43  * CStringConstraintPanel type definition
44  */
45 
46 IMPLEMENT_DYNAMIC_CLASS( CStringConstraintPanel, wxPanel )
47 
48 
49 /*!
50  * CStringConstraintPanel event table definition
51  */
52 
53 BEGIN_EVENT_TABLE( CStringConstraintPanel, wxPanel )
54  EVT_BUTTON( ID_STRING_CONSTRAINT_CLEAR_BUTTON, CStringConstraintPanel::OnClear )
55  EVT_CHOICE( ID_STRING_CONSTRAINT_MATCH_CHOICE, CStringConstraintPanel::OnMatchTypeSelected )
57 
58 
59 /*!
60  * CStringConstraintPanel constructors
61  */
62 
64 {
65  Init();
66 }
67 
68 CStringConstraintPanel::CStringConstraintPanel( wxWindow* parent, bool parentAECRDlg, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, bool cap_constraints )
69  : m_ParentAECRDlg(parentAECRDlg), m_CapConstraints(cap_constraints)
70 {
71  Init();
72  Create(parent, id, pos, size, style);
73 }
74 
75 
76 /*!
77  * CStringConstraintPanel creator
78  */
79 
80 bool CStringConstraintPanel::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
81 {
82 ////@begin CStringConstraintPanel creation
83  wxPanel::Create( parent, id, pos, size, style );
84 
86  if (GetSizer())
87  {
88  GetSizer()->SetSizeHints(this);
89  }
90  Centre();
91 ////@end CStringConstraintPanel creation
93 
94  return true;
95 }
96 
97 
98 /*!
99  * CStringConstraintPanel destructor
100  */
101 
103 {
104 ////@begin CStringConstraintPanel destruction
105 ////@end CStringConstraintPanel destruction
106 }
107 
108 
109 /*!
110  * Member initialisation
111  */
112 
114 {
115 ////@begin CStringConstraintPanel member initialisation
116  m_MatchType = NULL;
117  m_MatchText = NULL;
118  m_IgnoreCase = NULL;
121  m_TextSizer = NULL;
122  m_is_choice = false;
123 ////@end CStringConstraintPanel member initialisation
124 }
125 
126 
127 /*!
128  * Control creation for CStringConstraintPanel
129  */
130 
132 {
133 ////@begin CStringConstraintPanel content construction
134  //CStringConstraintPanel* itemPanel1 = this;
135  wxPanel* parentPanel = this;
136 
137  wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
138  parentPanel->SetSizer(itemBoxSizer2);
139 
140  wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
141  itemBoxSizer2->Add(itemBoxSizer3, 1, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
142 
143  wxArrayString m_MatchTypeStrings;
144  m_MatchTypeStrings.Add(_("Contains"));
145  m_MatchTypeStrings.Add(_("Does not contain"));
146  m_MatchTypeStrings.Add(_("Equals"));
147  m_MatchTypeStrings.Add(_("Does not equal"));
148  m_MatchTypeStrings.Add(_("Starts with"));
149  m_MatchTypeStrings.Add(_("Ends with"));
150  m_MatchTypeStrings.Add(_("Is one of"));
151  m_MatchTypeStrings.Add(_("Is not one of"));
152  m_MatchTypeStrings.Add(_("Does not start with"));
153  m_MatchTypeStrings.Add(_("Does not end with"));
154  if (m_CapConstraints)
155  {
156  m_MatchTypeStrings.Add(_("All caps"));
157  m_MatchTypeStrings.Add(_("First letter of each word"));
158  m_MatchTypeStrings.Add(_("First letter of first word"));
159  }
160 
161  m_MatchType = new wxChoice( parentPanel, ID_STRING_CONSTRAINT_MATCH_CHOICE, wxDefaultPosition, wxDefaultSize, m_MatchTypeStrings, 0 );
162  m_MatchType->SetStringSelection(_("Contains"));
163  itemBoxSizer3->Add(m_MatchType, 0, wxALIGN_TOP|wxLEFT|wxRIGHT|wxBOTTOM, 5);
164 
165  m_TextSizer = new wxBoxSizer(wxVERTICAL);
166  itemBoxSizer3->Add(m_TextSizer, 0, wxALIGN_TOP, 0);
167 
168  m_MatchText = new wxTextCtrl( parentPanel, ID_STRING_CONSTRAINT_MATCH_TEXT, wxEmptyString, wxDefaultPosition, wxSize(300, -1), wxTE_MULTILINE );
169  m_TextSizer->Add(m_MatchText, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxBOTTOM, 5);
170  if (m_ParentAECRDlg) {
171  m_MatchText->SetMinSize(wxSize(170, -1));
172  //itemBoxSizer3->Layout();
173  }
174  wxArrayString choice_strings;
175  m_MatchChoice = new wxComboBox(parentPanel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(300, -1), choice_strings, 0 );
176  m_MatchChoice->Hide();
177 
178 
179  wxBoxSizer* itemBoxSizer6 = new wxBoxSizer(wxHORIZONTAL);
180  itemBoxSizer2->Add(itemBoxSizer6, 1, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
181 
182  m_IgnoreCase = new wxCheckBox( parentPanel, ID_STRING_CONSTRAINT_CHECKBOX5, _("Ignore Case"), wxDefaultPosition, wxDefaultSize, 0 );
183  m_IgnoreCase->SetValue(false);
184  itemBoxSizer6->Add(m_IgnoreCase, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
185 
186  m_IgnoreSpace = new wxCheckBox( parentPanel, ID_STRING_CONSTRAINT_CHECKBOX6, _("Ignore Space"), wxDefaultPosition, wxDefaultSize, 0 );
187  m_IgnoreSpace->SetValue(false);
188  itemBoxSizer6->Add(m_IgnoreSpace, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
189 
190  wxButton* itemButton4 = new wxButton( parentPanel, ID_STRING_CONSTRAINT_CLEAR_BUTTON, _("Clear Constraint"), wxDefaultPosition, wxDefaultSize, 0 );
191  itemBoxSizer6->Add(itemButton4, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
192 
193 
194  ////@end CStringConstraintPanel content construction
195 }
196 
197 void CStringConstraintPanel::OnClear( wxCommandEvent& event )
198 {
199  ClearValues();
200 }
201 
203 {
204  if (m_is_choice)
205  {
206  m_MatchChoice->Hide();
208  m_MatchText->Show();
209  m_is_choice = false;
210  Layout();
211  Fit();
212  }
213  m_MatchType->SetStringSelection(_("Contains"));
214  m_MatchText->SetValue(wxEmptyString);
215  m_IgnoreCase->SetValue(false);
216  m_IgnoreSpace->SetValue(false);
217 }
218 
219 void CStringConstraintPanel::SetChoices(const vector<string>& choices, const vector<string>& values)
220 {
221  wxArrayString choice_strings;
222  ITERATE(vector<string>, it, choices)
223  {
224  choice_strings.Add(ToWxString(*it));
225  }
226  m_MatchChoice->Set(choice_strings);
227 
228  _ASSERT(values.empty() || choices.size() == values.size());
230  for (size_t i = 0; i < values.size(); i++)
231  m_ChoiceToValue[choices[i]] = values[i];
232 
234 }
235 
237 {
238  if (!m_MatchChoice->IsListEmpty() && (m_MatchType->GetSelection() == eMatchType_Equals || m_MatchType->GetSelection() == eMatchType_DoesNotEqual) )
239  {
240  if (!m_is_choice)
241  {
242  m_MatchText->Hide();
244  m_MatchChoice->Show();
245  }
246  m_is_choice = true;
247  }
248  else
249  {
250  if (m_is_choice)
251  {
252  m_MatchChoice->Hide();
254  m_MatchText->Show();
255  }
256  m_is_choice = false;
257  }
258  Layout();
259  Fit();
260 
261 }
262 
263 void CStringConstraintPanel::OnMatchTypeSelected( wxCommandEvent& event )
264 {
266  event.Skip();
267 }
268 
269 /*!
270  * Should we show tooltips?
271  */
272 
274 {
275  return true;
276 }
277 
278 /*!
279  * Get bitmap resources
280  */
281 
282 wxBitmap CStringConstraintPanel::GetBitmapResource( const wxString& name )
283 {
284  // Bitmap retrieval
285 ////@begin CStringConstraintPanel bitmap retrieval
286  wxUnusedVar(name);
287  return wxNullBitmap;
288 ////@end CStringConstraintPanel bitmap retrieval
289 }
290 
291 /*!
292  * Get icon resources
293  */
294 
295 wxIcon CStringConstraintPanel::GetIconResource( const wxString& name )
296 {
297  // Icon retrieval
298 ////@begin CStringConstraintPanel icon retrieval
299  wxUnusedVar(name);
300  return wxNullIcon;
301 ////@end CStringConstraintPanel icon retrieval
302 }
303 
305 {
306  string match_text;
307  if (!m_is_choice) {
308  match_text = NStr::Replace(NStr::Replace(ToStdString(m_MatchText->GetValue()), "\r", " "), "\n", " ");
309  }
310  else {
311  match_text = NStr::Replace(NStr::Replace(ToStdString(m_MatchChoice->GetValue()), "\r", " "), "\n", " ");
312  }
313 
314  edit::CStringConstraint::EMatchType match_type = edit::CStringConstraint::eMatchType_Contains;
315  int val = m_MatchType->GetSelection();
316  bool not_present = false;
317  bool not_found = false;
318  switch(val) {
319  case eMatchType_Contains:
320  match_type = edit::CStringConstraint::eMatchType_Contains;
321  not_present = false;
322  break;
324  match_type = edit::CStringConstraint::eMatchType_Contains;
325  not_present = true;
326  break;
327  case eMatchType_Equals:
328  match_type = edit::CStringConstraint::eMatchType_Equals;
329  not_present = false;
330  break;
332  match_type = edit::CStringConstraint::eMatchType_Equals;
333  not_present = true;
334  break;
336  match_type = edit::CStringConstraint::eMatchType_StartsWith;
337  not_present = false;
338  break;
339  case eMatchType_EndsWith:
340  match_type = edit::CStringConstraint::eMatchType_EndsWith;
341  not_present = false;
342  break;
343  case eMatchType_IsOneOf:
344  match_type = edit::CStringConstraint::eMatchType_IsOneOf;
345  not_present = false;
346  break;
348  match_type = edit::CStringConstraint::eMatchType_IsOneOf;
349  not_present = true;
350  break;
352  match_type = edit::CStringConstraint::eMatchType_StartsWith;
353  not_present = true;
354  break;
356  match_type = edit::CStringConstraint::eMatchType_EndsWith;
357  not_present = true;
358  break;
359  default: not_found = true; break;
360  }
362  if (!not_found)
363  {
364  if (match_text.size() > 0 || (match_type == edit::CStringConstraint::eMatchType_Equals && not_present)) {
365  sc.Reset(new edit::CStringConstraint(match_text, match_type,
366  m_IgnoreCase->GetValue(),
367  m_IgnoreSpace->GetValue(),
368  not_present));
369  }
370  }
371  return sc;
372 }
373 
374 string CStringConstraintPanel::GetMacroStringConstraint(const string& field_name)
375 {
376  string match_text;
377  if (!m_is_choice) {
378  match_text = NStr::Replace(NStr::Replace(ToStdString(m_MatchText->GetValue()), "\r", " "), "\n", " ");
379  }
380  else {
381  match_text = x_ReplaceChoices(NStr::Replace(NStr::Replace(ToStdString(m_MatchChoice->GetValue()), "\r", " "), "\n", " "));
382  }
383 
384  // return if there is no constraint to consider
385  if (match_text.empty() && (EMatchType)(m_MatchType->GetSelection()) != eMatchType_DoesNotEqual) {
386  return kEmptyStr;
387  }
388 
389  string function;
390  const string not_string = "NOT ";
391  switch (m_MatchType->GetSelection()) {
392  case eMatchType_Contains:
393  function = macro::CMacroFunction_StringConstraints::sm_Contain;
394  break;
396  function = not_string + CTempString(macro::CMacroFunction_StringConstraints::sm_Contain);
397  break;
398  case eMatchType_Equals:
399  function = macro::CMacroFunction_StringConstraints::sm_Equal;
400  break;
402  function = not_string + CTempString(macro::CMacroFunction_StringConstraints::sm_Equal);
403  break;
405  function = macro::CMacroFunction_StringConstraints::sm_Start;
406  break;
407  case eMatchType_EndsWith:
408  function = macro::CMacroFunction_StringConstraints::sm_End;
409  break;
410  case eMatchType_IsOneOf:
411  function = macro::CMacroFunction_StringConstraints::sm_InList;
412  break;
414  function = not_string + CTempString(macro::CMacroFunction_StringConstraints::sm_InList);
415  break;
417  function = not_string + CTempString(macro::CMacroFunction_StringConstraints::sm_Start);
418  break;
420  function = not_string + CTempString(macro::CMacroFunction_StringConstraints::sm_End);
421  break;
422  default:
423  break;
424  }
425 
426  if (function.empty())
427  return kEmptyStr;
428 
429  string case_sensitive = (m_IgnoreCase->IsChecked()) ? "false" : "true";
430  string ignore_space = (m_IgnoreSpace->IsChecked()) ? "true" : "false";
431 
432  function += "(" + field_name + ", \"" + match_text + "\", " + case_sensitive + ", " + ignore_space + ")";
433  return function;
434 }
435 
437 {
438  string result = orig;
439  auto it = m_ChoiceToValue.find(orig);
440  if (it != m_ChoiceToValue.end())
441  result = it->second;
442  return result;
443 }
444 
446 {
448  if (constraint)
449  return constraint->DoesTextMatch(str);
450 
452  bool not_found = false;
453  int val = m_MatchType->GetSelection();
454  switch(val)
455  {
456  case eMatchType_Is_all_caps: s.SetIs_all_caps(true); break;
458  case eMatchType_IsFirstCap: s.SetIs_first_cap(true); break;
459  default: not_found = true;
460  }
461 
462  if (not_found)
463  return false;
464  return s.Match(str);
465 }
466 
467 
469 
wxIcon GetIconResource(const wxString &name)
Retrieves icon resources.
wxBitmap GetBitmapResource(const wxString &name)
Retrieves bitmap resources.
void OnMatchTypeSelected(wxCommandEvent &event)
void OnClear(wxCommandEvent &event)
CRef< edit::CStringConstraint > GetStringConstraint()
void SetChoices(const vector< string > &choices, const vector< string > &values)
string GetMacroStringConstraint(const string &field_name)
void Init()
Initialises member variables.
map< string, string > m_ChoiceToValue
bool Match(const string &str)
string x_ReplaceChoices(const string &orig)
static bool ShowToolTips()
Should we show tooltips?
bool Create(wxWindow *parent, wxWindowID id=ID_CSTRINGCONSTRAINTPANEL, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(400, 300), long style=wxTAB_TRAVERSAL)
Creation.
bool m_ParentAECRDlg
flag is set if parent is the AECRDlg, for having specific features
void CreateControls()
Creates the controls and sizers.
bool Match(const CMatchString &str) const
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
const_iterator end() const
Definition: map.hpp:152
void clear()
Definition: map.hpp:169
const_iterator find(const key_type &key) const
Definition: map.hpp:153
#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 Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
static string & Replace(const string &src, const string &search, const string &replace, string &dst, SIZE_TYPE start_pos=0, SIZE_TYPE max_replace=0, SIZE_TYPE *num_replace=0)
Replace occurrences of a substring within a string.
Definition: ncbistr.cpp:3314
void SetIs_first_cap(TIs_first_cap value)
Assign a value to Is_first_cap data member.
void SetIs_first_each_cap(TIs_first_each_cap value)
Assign a value to Is_first_each_cap data member.
void SetIs_all_caps(TIs_all_caps value)
Assign a value to Is_all_caps data member.
where boath are integers</td > n< td ></td > n</tr > n< tr > n< td > tse</td > n< td > optional</td > n< td > String</td > n< td class=\"description\"> TSE option controls what blob is orig
END_EVENT_TABLE()
int i
constexpr bool empty(list< Ts... >) noexcept
const struct ncbi::grid::netcache::search::fields::SIZE size
static static static wxID_ANY
#define _ASSERT
else result
Definition: token2.c:20
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
string ToStdString(const wxString &s)
Definition: wx_utils.hpp:161
Modified on Wed Jun 19 17:02:25 2024 by modify_doxy.py rev. 669887