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

Go to the SVN repository for this file.

1 /* $Id: word_substitute_dlg.cpp 46724 2021-09-14 20:14:57Z 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: Igor Filippov
27  *
28  * File Description:
29  *
30  */
31 #include <ncbi_pch.hpp>
35 
36 #include <wx/sizer.h>
37 #include <wx/stattext.h>
38 #include <wx/statline.h>
39 #include <wx/listbook.h>
40 #include <wx/checkbox.h>
41 #include <wx/checklst.h>
42 #include <wx/panel.h>
43 #include <wx/textctrl.h>
44 #include <wx/button.h>
45 
48 
49 IMPLEMENT_DYNAMIC_CLASS( CWordSubstitutionDlg, wxFrame )
50 
51 BEGIN_EVENT_TABLE( CWordSubstitutionDlg, wxFrame )
52 
53 ////@begin CWordSubstitutionDlg event table entries
54  EVT_BUTTON( wxID_OK, CWordSubstitutionDlg::OnAccept )
55  EVT_BUTTON( wxID_CANCEL, CWordSubstitutionDlg::OnCancel )
58 ////@end CWordSubstitutionDlg event table entries
59 
61 
63 {
64  Init();
65 }
66 
67 CWordSubstitutionDlg::CWordSubstitutionDlg( wxWindow* parent, CRef<objects::CWord_substitution_set> word_subst, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
68 {
69  Init();
70  Create(parent, id, caption, pos, size, style);
71  InitSubstitutions(word_subst);
72 }
73 
74 bool CWordSubstitutionDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
75 {
76 ////@begin CWordSubstitutionDlg creation
77  SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
78  wxFrame::Create( parent, id, caption, pos, size, style );
79 
81  if (GetSizer())
82  {
83  GetSizer()->SetSizeHints(this);
84  }
85  Centre(wxBOTH|wxCENTRE_ON_SCREEN);
86 ////@end CWordSubstitutionDlg creation
87  return true;
88 }
89 
91 {
92 ////@begin CWordSubstitutionDlg destruction
93 ////@end CWordSubstitutionDlg destruction
94 }
95 
97 {
98 ////@begin CWordSubstitutionDlg member initialisation
99  m_Pattern = NULL;
100  m_Subst = NULL;
101  m_IgnoreCase = NULL;
102  m_WholeWord = NULL;
104 ////@end CWordSubstitutionDlg member initialisation
105 }
106 
108 {
109  wxBoxSizer* itemBoxSizer1 = new wxBoxSizer(wxVERTICAL);
110  SetSizer(itemBoxSizer1);
111 
112  wxPanel* itemCBulkCmdDlg1 = new wxPanel(this, wxID_ANY);
113  itemBoxSizer1->Add(itemCBulkCmdDlg1, 1, wxGROW, 0);
114 
115  wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
116  itemCBulkCmdDlg1->SetSizer(itemBoxSizer2);
117 
118  wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
119  itemBoxSizer2->Add(itemBoxSizer4, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
120 
121  wxStaticText* itemStaticText5 = new wxStaticText( itemCBulkCmdDlg1, wxID_STATIC, _("Pattern Word"), wxDefaultPosition, wxDefaultSize, 0 );
122  itemBoxSizer4->Add(itemStaticText5, 0, wxALIGN_LEFT|wxALL, 5);
123 
124  m_Pattern = new wxTextCtrl(itemCBulkCmdDlg1, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(250, -1), 0);
125  itemBoxSizer4->Add(m_Pattern, 0, wxALIGN_LEFT|wxALL, 5);
126 
127  m_Subst = new wxTextCtrl( itemCBulkCmdDlg1, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(250, 90), wxTE_MULTILINE );
128  itemBoxSizer2->Add(m_Subst, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
129 
130  wxBoxSizer* itemBoxSizer12 = new wxBoxSizer(wxHORIZONTAL);
131  itemBoxSizer2->Add(itemBoxSizer12, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
132 
133  m_IgnoreCase = new wxCheckBox( itemCBulkCmdDlg1, wxID_ANY, _("Ignore Case"), wxDefaultPosition, wxDefaultSize, 0 );
134  m_IgnoreCase->SetValue(true);
135  itemBoxSizer12->Add(m_IgnoreCase, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
136 
137  m_WholeWord = new wxCheckBox( itemCBulkCmdDlg1, wxID_ANY, _("Whole Word"), wxDefaultPosition, wxDefaultSize, 0 );
138  m_WholeWord->SetValue(false);
139  itemBoxSizer12->Add(m_WholeWord, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
140 
141  wxButton* itemButton13 = new wxButton( itemCBulkCmdDlg1, ID_WORDSUBST_ADD, _("Add Word Substitution"), wxDefaultPosition, wxDefaultSize, 0 );
142  itemBoxSizer2->Add(itemButton13, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
143 
144  wxStaticLine *line1 = new wxStaticLine (itemCBulkCmdDlg1, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL);
145  itemBoxSizer2->Add(line1, 0, wxALIGN_CENTER_HORIZONTAL|wxALL|wxSHAPED, 5);
146 
147  m_CheckListBox = new wxCheckListBox(itemCBulkCmdDlg1, wxID_ANY, wxDefaultPosition, wxDefaultSize);
148  itemBoxSizer2->Add(m_CheckListBox, 1, wxGROW|wxALL, 5);
149 
150  wxButton* itemButton14 = new wxButton( itemCBulkCmdDlg1, ID_WORDSUBST_REMOVE, _("Remove Word Substitution"), wxDefaultPosition, wxDefaultSize, 0 );
151  itemBoxSizer2->Add(itemButton14, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
152 
153  wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
154  itemBoxSizer2->Add(itemBoxSizer3, 0, wxALIGN_CENTER_HORIZONTAL, 0);
155 
156  wxButton* itemButton15 = new wxButton( itemCBulkCmdDlg1, wxID_OK, _("Accept"), wxDefaultPosition, wxDefaultSize, 0 );
157  itemBoxSizer3->Add(itemButton15, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
158 
159  wxButton* itemButton16 = new wxButton( itemCBulkCmdDlg1, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
160  itemBoxSizer3->Add(itemButton16, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
161 
162 }
163 
165 {
166  return true;
167 }
168 wxBitmap CWordSubstitutionDlg::GetBitmapResource( const wxString& name )
169 {
170  // Bitmap retrieval
171 ////@begin CWordSubstitutionDlg bitmap retrieval
172  wxUnusedVar(name);
173  return wxNullBitmap;
174 ////@end CWordSubstitutionDlg bitmap retrieval
175 }
176 wxIcon CWordSubstitutionDlg::GetIconResource( const wxString& name )
177 {
178  // Icon retrieval
179 ////@begin CWordSubstitutionDlg icon retrieval
180  wxUnusedVar(name);
181  return wxNullIcon;
182 ////@end CWordSubstitutionDlg icon retrieval
183 }
184 
185 void CWordSubstitutionDlg::OnCancel( wxCommandEvent& event )
186 {
187  Close();
188 }
189 
190 void CWordSubstitutionDlg::OnAccept( wxCommandEvent& event )
191 {
192  CRef<objects::CWord_substitution_set> word_subst(new objects::CWord_substitution_set);
193  for (auto item : m_label_to_subst)
194  {
195  CRef<CWord_substitution> ws = item.second;
196  word_subst->Set().push_back(ws);
197  }
198 
199  wxWindow* parent = GetParent();
200  CWordSubstitutionSetConsumer* panel = dynamic_cast<CWordSubstitutionSetConsumer*>(parent);
201  panel->AddWordSubstSet(word_subst);
202  Close();
203 }
204 
205 void CWordSubstitutionDlg::OnAdd( wxCommandEvent& event )
206 {
207  string pattern = NStr::TruncateSpaces(m_Pattern->GetValue().ToStdString());
208  string syn = NStr::TruncateSpaces(m_Subst->GetValue().ToStdString());
209  if (pattern.empty() || syn.empty())
210  return;
211 
213  ws->SetWord(pattern);
214 
215  vector<string> lines;
216  NStr::Split(syn, "\n", lines);
217 
218  for (auto& l : lines)
219  {
220  NStr::ReplaceInPlace(l, "\r", "");
221  string str = NStr::TruncateSpaces(l);
222  if (!str.empty())
223  ws->SetSynonyms().push_back(str);
224  }
225 
226  ws->SetCase_sensitive(!m_IgnoreCase->GetValue());
227  ws->SetWhole_word(m_WholeWord->GetValue());
228 
229  string label = GetDescription(ws);
230  if (!label.empty() && m_label_to_subst.find(label) == m_label_to_subst.end())
231  {
232  m_label_to_subst[label] = ws;
233  m_CheckListBox->Append(label);
234  Refresh();
235  }
236 }
237 
238 void CWordSubstitutionDlg::OnRemove( wxCommandEvent& event )
239 {
240  Freeze();
241  wxArrayInt checked_items;
242  m_CheckListBox->GetCheckedItems(checked_items);
243  set<int> to_delete;
244  for (size_t i = 0; i < checked_items.GetCount(); i++)
245  {
246  int j = checked_items.Item(i);
247  string label = m_CheckListBox->GetString(j).ToStdString();
249  to_delete.insert(j);
250  }
251  for (auto rit=to_delete.rbegin(); rit != to_delete.rend(); ++rit)
252  {
253  m_CheckListBox->Delete(*rit);
254  }
255  Thaw();
256  Refresh();
257 }
258 
260 {
261  string str;
262  if (ws && ws->IsSetWord() && ws->IsSetSynonyms() && !ws->GetSynonyms().empty())
263  {
264  str = "allow " + ws->GetWord() + " to be replaced by " + NStr::Join(ws->GetSynonyms(), ", ");
265 
266  if (ws->IsSetCase_sensitive() && ws->GetCase_sensitive())
267  str += ", case-sensitive";
268  if (ws->IsSetWhole_word() && ws->GetWhole_word())
269  str += ", whole word";
270  }
271  return str;
272 }
273 
275 {
276  if (word_subst && word_subst->IsSet())
277  {
278  for (auto ws : word_subst->Set())
279  {
280  string label = GetDescription(ws);
281  m_label_to_subst[label] = ws;
282  m_CheckListBox->Append(label);
283  }
284  }
285  Refresh();
286 }
287 
288 
User-defined methods of the data storage class.
void OnRemove(wxCommandEvent &event)
map< string, CRef< objects::CWord_substitution > > m_label_to_subst
wxCheckListBox * m_CheckListBox
void InitSubstitutions(CRef< objects::CWord_substitution_set > word_subst)
static string GetDescription(CRef< objects::CWord_substitution > ws)
void OnCancel(wxCommandEvent &event)
wxBitmap GetBitmapResource(const wxString &name)
wxIcon GetIconResource(const wxString &name)
void OnAccept(wxCommandEvent &event)
bool Create(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &caption=_("Word Substitution List"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX|wxTAB_TRAVERSAL)
void OnAdd(wxCommandEvent &event)
virtual void AddWordSubstSet(CRef< objects::CWord_substitution_set > word_subst)=0
void erase(iterator pos)
Definition: map.hpp:167
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
#define _(proto)
Definition: ct_nlmzip_i.h:78
static void Init(void)
Definition: cursor6.c:76
#define NULL
Definition: ncbistd.hpp:225
#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 list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3457
static string Join(const TContainer &arr, const CTempString &delim)
Join strings using the specified delimiter.
Definition: ncbistr.hpp:2697
static string & ReplaceInPlace(string &src, const string &search, const string &replace, 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:3401
static string TruncateSpaces(const string &str, ETrunc where=eTrunc_Both)
Truncate spaces in a string.
Definition: ncbistr.cpp:3182
static const char label[]
END_EVENT_TABLE()
int i
const struct ncbi::grid::netcache::search::fields::SIZE size
static static static wxID_ANY
static const char * str(char *buf, int n)
Definition: stats.c:84
static char line1[1024 *16]
Definition: t0016.c:98
USING_SCOPE(objects)
#define ID_WORDSUBST_REMOVE
#define ID_WORDSUBST_ADD
Modified on Tue Dec 05 02:16:26 2023 by modify_doxy.py rev. 669887