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

Go to the SVN repository for this file.

1 /* $Id: serial_member_primitive_validators.cpp 38981 2017-07-14 18:37:03Z filippov $
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: Roman Katargin
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
36 
37 #include <serial/objectinfo.hpp>
38 #include <objects/biblio/Title.hpp>
39 
40 #include <wx/msgdlg.h>
41 #include <wx/textctrl.h>
42 #include <wx/radiobox.h>
43 #include <wx/choice.h>
44 #include <wx/msgdlg.h>
45 
47 
48 static bool CheckValueType(const CMemberInfo* memberInfo, EPrimitiveValueType type)
49 {
50  TTypeInfo memberType = memberInfo->GetTypeInfo();
51  if (memberType->GetTypeFamily() != eTypeFamilyPrimitive) {
52  wxCHECK_MSG( 0, false, wxT("Invalid member name") );
53  }
54 
55  const CPrimitiveTypeInfo* primitiveType =
57  wxCHECK_MSG( primitiveType->GetPrimitiveValueType() == type,
58  false, wxT("Invalid member name") );
59  return true;
60 }
61 
62 /*
63  * CSerialTextValidator
64  */
65 
68 {
69 }
70 
72 {
73  if( !CheckValidator() )
74  return false;
75 
76  wxTextCtrl *control = (wxTextCtrl*)m_validatorWindow;
77 
79  const CPrimitiveTypeInfo* primitiveType =
81 
82  bool optional = m_MemberInfo->Optional();
83 
84  string value;
85 
86  if (!optional || m_MemberInfo->GetSetFlagYes(&m_Object))
87  primitiveType->GetValueString(memberPtr, value);
88 
89  control->SetValue(ToWxString(value));
90 
91  return true;
92 }
93 
95 {
96  if( !CheckValidator() )
97  return false;
98 
99  wxTextCtrl *control = (wxTextCtrl*)m_validatorWindow;
100  string value = ToStdString(control->GetValue());
102 
103  bool optional = m_MemberInfo->Optional();
104 
105  if (optional && value.empty()) {
107  }
108  else {
110  const CPrimitiveTypeInfo* primitiveType =
112  primitiveType->SetValueString(memberPtr, value);
114  }
115 
116  return true;
117 }
118 
120 {
121  wxCHECK_MSG( m_MemberInfo, false, wxT("Invalid member name") );
122 
124  return false;
125 
126  wxCHECK_MSG( m_validatorWindow, false,
127  wxT("No window associated with validator") );
128  wxCHECK_MSG( m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)), false,
129  wxT("CSerialTextValidator is only for wxTextCtrl's") );
130 
131  return true;
132 }
133 
134 /*
135  * CSerialAsciiTextValidator
136  */
137 
139 {
140  if( !CheckValidator() )
141  return false;
142 
143  wxTextCtrl *control = (wxTextCtrl*)m_validatorWindow;
144  string value = ToAsciiStdString(control->GetValue());
145  // get rid of leading and trailing spaces
147  // get rid of carriage returns
148  NStr::ReplaceInPlace(value, " \n", " ");
149  NStr::ReplaceInPlace(value, "\n ", " ");
150  NStr::ReplaceInPlace(value, "\n", " ");
151 
152  bool optional = m_MemberInfo->Optional();
153 
154  if (optional && value.empty()) {
156  }
157  else {
159  const CPrimitiveTypeInfo* primitiveType =
161 
162  try
163  {
164  primitiveType->SetValueString(memberPtr, value);
165  }
166  catch(CException &e)
167  {
168  return false;
169  }
170  catch (exception &e)
171  {
172  return false;
173  }
175  }
176 
177  return true;
178 }
179 
180 /*
181  * CSerialBoolValidator
182  */
183 
186 {
187 }
188 
190 {
191  if( !CheckValidator() )
192  return false;
193 
194  wxControlWithItems *control = (wxControlWithItems*)m_validatorWindow;
195 
197  const CPrimitiveTypeInfo* primitiveType =
199 
201  bool value = primitiveType->GetValueBool(memberPtr);
202  int index = value ? 0 : 1;
203  if (control->GetCount() == 3)
204  ++index;
205  control->SetSelection(index);
206  }
207  else {
208  control->SetSelection(control->GetCount() == 3 ? 0 : 1);
209  }
210 
211  return true;
212 }
213 
215 {
216  if( !CheckValidator() )
217  return false;
218 
219  wxControlWithItems *control = (wxControlWithItems*)m_validatorWindow;
220  int index = control->GetSelection();
221 
223  const CPrimitiveTypeInfo* primitiveType =
225 
226  if (control->GetCount() == 3) {
227  if (index == 0)
229  else {
231  primitiveType->SetValueBool(memberPtr, index == 1);
232  }
233  }
234  else {
236  primitiveType->SetValueBool(memberPtr, index == 0);
237  }
238 
239  return true;
240 }
241 
243 {
244  wxCHECK_MSG( m_MemberInfo, false, wxT("Invalid member name") );
245 
247  return false;
248 
249  bool optional = m_MemberInfo->Optional();
250 
251  wxCHECK_MSG( m_validatorWindow, false,
252  wxT("No window associated with validator") );
253  wxCHECK_MSG( m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)), false,
254  wxT("CSerialBoolValidator is only for wxRadioBox's") );
255 
256  wxControlWithItems *control = (wxControlWithItems*)m_validatorWindow;
257  int count = control->GetCount();
258  wxCHECK_MSG( (optional ? count == 3 : count == 2), false,
259  wxT("CSerialBoolValidator assosiated with invalid control") );
260 
261  return true;
262 }
263 
264 /*
265  * CSerialEnumValidator
266  */
267 
270 {
271 }
272 
274 {
275  if( !CheckValidator() )
276  return false;
277 
278  bool optional = m_MemberInfo->Optional();
279 
281  const CEnumeratedTypeInfo* enumType =
283 
284  TConstObjectPtr defaultPtr = m_MemberInfo->GetDefault();
285 
286  bool valueSet = false;
287  int value;
288 
290  valueSet = true;
291  value = enumType->GetValueInt4(memberPtr);
292  }
293  else if (optional) {
294  if (defaultPtr) {
295  valueSet = true;
296  value = enumType->GetValueInt4(defaultPtr);
297  }
298  }
299 
300  int newValue = 0;
301 
302  if (valueSet) {
303  const CEnumeratedTypeValues::TValues& values = enumType->Values().GetValues();
304 
305  CEnumeratedTypeValues::TValues::const_iterator it;
306  int index = 0;
307  for (it = values.begin();it != values.end();++it, ++index) {
308  if (value == it->second)
309  break;
310  }
311 
312  if (it == values.end())
313  index = 0;
314 
315  if (optional && !defaultPtr)
316  ++index;
317 
318  newValue = index;
319  }
320 
321  wxRadioBox* radioBox = wxDynamicCast(m_validatorWindow, wxRadioBox);
322  if (radioBox) {
323  radioBox->SetSelection(newValue);
324  return true;
325  }
326 
327  wxChoice* choice = wxDynamicCast(m_validatorWindow, wxChoice);
328  if (choice) {
329  choice->SetSelection(newValue);
330  return true;
331  }
332 
333  return false;
334 }
335 
337 {
338  if( !CheckValidator() )
339  return false;
340 
341  int index = 0;
342  wxRadioBox* radioBox = wxDynamicCast(m_validatorWindow, wxRadioBox);
343  if (radioBox) {
344  index = radioBox->GetSelection();
345  } else {
346  wxChoice* choice = wxDynamicCast(m_validatorWindow, wxChoice);
347  if (choice)
348  index = choice->GetSelection();
349  else
350  return false;
351  }
352 
353  bool optional = m_MemberInfo->Optional();
354  TConstObjectPtr defaultPtr = m_MemberInfo->GetDefault();
355 
357  const CEnumeratedTypeInfo* enumType =
359 
360  if (optional) {
361  if (index == 0 && !defaultPtr)
363  else {
364  if (!defaultPtr)
365  index--;
366 
367  const CEnumeratedTypeValues::TValues& values = enumType->Values().GetValues();
368  CEnumeratedTypeValues::TValues::const_iterator it;
369  for (it = values.begin(); it != values.end() && index > 0;++it, --index);
370 
371  if (it == values.end())
372  it = values.begin();
373 
374  if (enumType->IsDefault((TConstObjectPtr)(&it->second)))
376  else {
377  enumType->SetValueInt4(memberPtr, it->second);
379  }
380  }
381  }
382  else {
384  enumType->SetValueInt4(memberPtr, index);
385  }
386 
387  return true;
388 }
389 
391 {
392  wxCHECK_MSG( m_MemberInfo, false, wxT("Invalid member name") );
393 
395  return false;
396 
397  wxCHECK_MSG( m_validatorWindow, false,
398  wxT("No window associated with validator") );
399 
400  wxRadioBox* radioBox = wxDynamicCast(m_validatorWindow, wxRadioBox);
401  wxChoice* choice = wxDynamicCast(m_validatorWindow, wxChoice);
402 
403  wxCHECK_MSG( radioBox || choice,
404  false,
405  wxT("CSerialEnumValidator is only for wxRadioBox or wxChoice") );
406 
408  const CEnumeratedTypeInfo* enumType =
410  const CEnumeratedTypeValues::TValues& values = enumType->Values().GetValues();
411 
412  bool optional = m_MemberInfo->Optional();
413 
414  size_t count = optional && m_MemberInfo->GetDefault() == 0 ?
415  values.size() + 1 : values.size();
416 
417  int ctrlCount = radioBox ? radioBox->GetCount() : choice->GetCount();
418 
419  wxCHECK_MSG( count == ctrlCount, false,
420  wxT("CSerialEnumValidator assosiated with invalid control") );
421 
422  return true;
423 }
424 
425 
426 /*
427  * CSerialTitleValidator
428  */
429 
431  : wxValidator(), m_Object(val.m_Object), m_TitleChoice(val.m_TitleChoice), m_Label(val.m_Label), m_ShowErr(val.m_ShowErr)
432 {
433  Copy(val);
434 }
435 
437 {
438  wxValidator::Copy(val);
439  m_Object = val.m_Object;
440  m_TitleChoice = val.m_TitleChoice;
441  m_Label = val.m_Label;
442  m_ShowErr = val.m_ShowErr;
443  return true;
444 }
445 
446 
448 {
449  if( !CheckValidator() )
450  return false;
451 
452  wxTextCtrl *control = (wxTextCtrl*)m_validatorWindow;
453 
454  objects::CTitle& title = dynamic_cast<objects::CTitle&>(m_Object);
455 
456  string value;
457 
458  if (title.IsSet() && !title.Set().empty()) {
459  switch (title.Set().front()->Which()) {
460  case objects::CTitle::C_E::e_Name:
461  value = title.Set().front()->GetName();
462  break;
463  case objects::CTitle::C_E::e_Tsub:
464  value = title.Set().front()->GetTsub();
465  break;
466  case objects::CTitle::C_E::e_Trans:
467  value = title.Set().front()->GetTrans();
468  break;
469  case objects::CTitle::C_E::e_Jta:
470  value = title.Set().front()->GetJta();
471  break;
472  case objects::CTitle::C_E::e_Iso_jta:
473  value = title.Set().front()->GetIso_jta();
474  break;
475  case objects::CTitle::C_E::e_Ml_jta:
476  value = title.Set().front()->GetMl_jta();
477  break;
478  case objects::CTitle::C_E::e_Coden:
479  value = title.Set().front()->GetCoden();
480  break;
481  case objects::CTitle::C_E::e_Issn:
482  value = title.Set().front()->GetIssn();
483  break;
484  case objects::CTitle::C_E::e_Abr:
485  value = title.Set().front()->GetAbr();
486  break;
487  case objects::CTitle::C_E::e_Isbn:
488  value = title.Set().front()->GetIsbn();
489  break;
490  default:
491  break;
492  }
493  }
494 
495  control->SetValue(ToWxString(value));
496 
497  return true;
498 }
499 
501 {
502  if( !CheckValidator() )
503  return false;
504 
505  wxTextCtrl *control = (wxTextCtrl*)m_validatorWindow;
506  string value = ToStdString(control->GetValue());
508 
509  objects::CTitle& title = dynamic_cast<objects::CTitle&>(m_Object);
510 
512 
513  if (title.Set().empty()) {
514  item = new objects::CTitle::C_E();
515  title.Set().push_back(item);
516  } else {
517  item = title.Set().front();
518  }
519 
520  if (NStr::IsBlank(value)) {
521  item->Reset();
522  if (m_ShowErr) {
523  wxMessageBox(NStr::IsBlank(m_Label) ? wxT("Missing Title") : ToWxString("Missing required field " + m_Label),
524  wxT("Error"), wxOK | wxICON_ERROR, (wxWindow*)(control));
525  return false;
526  }
527  } else {
528  switch (m_TitleChoice) {
529  case objects::CTitle::C_E::e_Name:
530  item->SetName(value);
531  break;
532  case objects::CTitle::C_E::e_Tsub:
533  item->SetTsub(value);
534  break;
535  case objects::CTitle::C_E::e_Trans:
536  item->SetTrans(value);
537  break;
538  case objects::CTitle::C_E::e_Jta:
539  item->SetJta(value);
540  break;
541  case objects::CTitle::C_E::e_Iso_jta:
542  item->SetIso_jta(value);
543  break;
544  case objects::CTitle::C_E::e_Ml_jta:
545  item->SetMl_jta(value);
546  break;
547  case objects::CTitle::C_E::e_Coden:
548  item->SetCoden(value);
549  break;
550  case objects::CTitle::C_E::e_Issn:
551  item->SetIssn(value);
552  break;
553  case objects::CTitle::C_E::e_Abr:
554  item->SetAbr(value);
555  break;
556  case objects::CTitle::C_E::e_Isbn:
557  item->SetIsbn(value);
558  break;
559  default:
560  break;
561  }
562  }
563 
564  return true;
565 }
566 
568 {
569  wxCHECK_MSG( m_validatorWindow, false,
570  wxT("No window associated with validator") );
571  wxCHECK_MSG( m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)), false,
572  wxT("CSerialTitleValidator is only for wxTextCtrl's") );
573 
574  return true;
575 }
576 
CSerialBoolValidator(CSerialObject &object, const string &memberName)
CSerialEnumValidator(CSerialObject &object, const string &memberName)
const CMemberInfo * m_MemberInfo
CSerialTextValidator(CSerialObject &object, const string &memberName)
CSerialTitleValidator(CSerialObject &object, objects::CTitle::C_E::E_Choice title_choice, const string &label, bool show_err=true)
bool Copy(const CSerialTitleValidator &val)
objects::CTitle::C_E::E_Choice m_TitleChoice
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
list< pair< string, TEnumValueType > > TValues
Definition: enumvalues.hpp:54
bool Optional(void) const
bool UpdateSetFlagNo(TObjectPtr object) const
set 'setFlag' to eSetNo and return true if previous value wasn't eSetNo
TConstObjectPtr GetDefault(void) const
TObjectPtr GetItemPtr(TObjectPtr object) const
void UpdateSetFlagYes(TObjectPtr object) const
set 'setFlag' to eSetYes
const TValues & GetValues(void) const
Get the list of name-value pairs.
Definition: enumvalues.hpp:98
TTypeInfo GetTypeInfo(void) const
bool GetSetFlagYes(TConstObjectPtr object) const
true if 'setFlag' is not eSetNo
void * TObjectPtr
Definition: serialdef.hpp:55
const void * TConstObjectPtr
Definition: serialdef.hpp:59
EPrimitiveValueType
Primitive value type.
Definition: serialdef.hpp:147
static const TObjectType * SafeCast(TTypeInfo type)
Definition: serialutil.hpp:76
@ ePrimitiveValueString
string|char*|const char*
Definition: serialdef.hpp:153
@ ePrimitiveValueBool
bool
Definition: serialdef.hpp:149
@ ePrimitiveValueEnum
enum
Definition: serialdef.hpp:154
@ eTypeFamilyPrimitive
Definition: serialdef.hpp:139
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
static bool IsBlank(const CTempString str, SIZE_TYPE pos=0)
Check if a string is blank (has no text).
Definition: ncbistr.cpp:106
static void TruncateSpacesInPlace(string &str, ETrunc where=eTrunc_Both)
Truncate spaces in a string (in-place)
Definition: ncbistr.cpp:3201
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:3405
EPrimitiveValueType GetPrimitiveValueType(void) const
virtual void GetValueString(TConstObjectPtr objectPtr, string &value) const
Definition: stdtypes.cpp:501
ETypeFamily GetTypeFamily(void) const
virtual void SetValueBool(TObjectPtr objectPtr, bool value) const
Definition: stdtypes.cpp:405
virtual Int4 GetValueInt4(TConstObjectPtr objectPtr) const override
Definition: enumerated.cpp:324
virtual bool GetValueBool(TConstObjectPtr objectPtr) const
Definition: stdtypes.cpp:377
virtual void SetValueInt4(TObjectPtr objectPtr, Int4 value) const override
Definition: enumerated.cpp:334
virtual bool IsDefault(TConstObjectPtr object) const override
Check, whether the object contains default value.
Definition: enumerated.cpp:297
virtual void SetValueString(TObjectPtr objectPtr, const string &value) const
Definition: stdtypes.cpp:507
const CEnumeratedTypeValues & Values(void) const
Definition: enumerated.hpp:57
#define wxT(x)
Definition: muParser.cpp:41
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
static bool CheckValueType(const CMemberInfo *memberInfo, EPrimitiveValueType type)
Definition: type.c:6
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
string ToStdString(const wxString &s)
Definition: wx_utils.hpp:161
string ToAsciiStdString(const wxString &input)
Definition: wx_utils.hpp:166
Modified on Mon Jun 17 05:08:20 2024 by modify_doxy.py rev. 669887