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

Go to the SVN repository for this file.

1 /* $Id: author_names_container.cpp 41446 2018-07-31 15:24:14Z katargir $
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>
35 #include <util/line_reader.hpp>
36 
40 
44 #include <wx/scrolwin.h>
45 #include <wx/filedlg.h>
46 
49 
50 IMPLEMENT_ABSTRACT_CLASS(CAuthorNamesContainer, wxPanel )
51 
52 
53 BEGIN_EVENT_TABLE(CAuthorNamesContainer, wxPanel )
54 
56 
57 
58 void CAuthorNamesContainer::DeleteRow(wxWindow* wnd)
59 {
60  if (wnd == NULL) return;
61 
62  wxSizerItemList& itemList = m_Sizer->GetChildren();
63  wxSizerItem* auth = x_FindSingleAuthor(wnd, itemList);
64 
65  wxSizerItemList::iterator node = itemList.begin();
66  wxSizerItemList::iterator last_node = itemList.end();
67  last_node--;
68  if ((**last_node).GetWindow() == wnd)
69  return;
70  while (node != last_node)
71  {
72  if ((**node).GetWindow() == wnd)
73  {
74  (**node).DeleteWindows();
75  delete (*node);
76  node = itemList.erase(node);
77  }
78  else
79  node++;
80  }
81 
82  x_DeleteAuthor(auth);
83 }
84 
86 {
87  if (!link) return;
88  Freeze();
89  wxSizerItemList& itemList = m_Sizer->GetChildren();
90  wxSizerItemList::iterator node = itemList.begin();
91  wxSizerItemList::iterator last_node = itemList.end();
92  last_node--;
93  if ((*last_node) == link)
94  return;
95  while (node != last_node)
96  {
97  if ((*node) == link)
98  {
99  (**node).DeleteWindows();
100  delete (*node);
101  node = itemList.erase(node);
102  }
103  else
104  node++;
105  }
106  if (itemList.empty()) {
107  CRef<CAuthor> auth(new CAuthor());
108  wxWindow* row = new CSingleAuthorPanel(m_ScrolledWindow, *auth);
110  }
111  else {
112  m_ScrolledWindow->FitInside();
113  m_Sizer->Layout();
114  m_ScrolledWindow->Refresh();
115  }
116  Thaw();
117 }
118 
120 {
121  // find the row that holds the author being changed
122  int row_num = x_FindRow(link, m_Sizer->GetChildren());
123  x_MoveRowUp(row_num);
124 }
125 
126 
128 {
129  // find the row that holds the author being changed
130  int row_num = x_FindRow(link, m_Sizer->GetChildren());
131  if (row_num > -1) {
132  x_MoveRowUp(row_num + 1);
133  }
134 }
135 
136 static void s_GetAuthorNames(const string& line, CAuth_list& author_list)
137 {
138  vector<string> row_values;
139  NStr::Split(line, " ", row_values, NStr::fSplit_Tokenize);
140  if (row_values.size() == 2) {
141  CRef<CAuthor> author(new CAuthor);
142  author->SetName().SetName().SetFirst(row_values[0]);
143  author->SetName().SetName().SetLast(row_values[1]);
144  author_list.SetNames().SetStd().push_back(author);
145  }
146  else if (row_values.size() == 3) {
147  CRef<CAuthor> author(new CAuthor);
148  CName_std& std_name = author->SetName().SetName();
149  std_name.SetFirst(row_values[0]);
150  std_name.SetLast(row_values[2]);
151  const string middle_init = row_values[1];
152 
153  string initials = macro::CMacroFunction_AuthorFix::s_GetFirstNameInitials(std_name.GetFirst());
154  if (!NStr::IsBlank(middle_init)) {
155  initials.append(middle_init);
156  }
157  // don't add another period if one is already there
158  if (!NStr::EndsWith(initials, ".")) {
159  initials.append(".");
160  }
161  std_name.SetInitials(initials);
162  author_list.SetNames().SetStd().push_back(author);
163  }
164 }
165 
167 {
168  wxFileDialog file(this, wxT("Import author names from file"), m_WorkDir, wxEmptyString,
170  wxFD_OPEN | wxFD_FILE_MUST_EXIST);
171  if (file.ShowModal() == wxID_OK)
172  {
173  wxString path = file.GetPath();
174  if (!path.IsEmpty())
175  {
176  CNcbiIfstream istr(path.fn_str());
177  CRef<CAuth_list> new_auth_list(new CAuth_list);
178  bool list_of_names = true;
179  try {
180  istr >> MSerial_AsnText >> *new_auth_list;
181  list_of_names = false;
182  }
183  catch (const CException&) {}
184 
185  if (list_of_names) {
186  CNcbiIfstream fstream(path.fn_str(), ios_base::binary);
187  CRef<ILineReader> line_reader(ILineReader::New(fstream));
188  if (line_reader.Empty()) {
189  NcbiMessageBox("Could not open file.");
190  return;
191  }
192  while (!line_reader->AtEOF()) {
193  line_reader->ReadLine();
194  string str = line_reader->GetCurrentLine();
196  if (str.empty())
197  continue;
198 
200  if (NStr::FindNoCase(str, ",") != NPOS) {
201  // a comma separated author list
202  vector<string> author_names;
203  NStr::Split(str, ",", author_names, NStr::fSplit_Tokenize);
204  if (!author_names.empty()) {
205  for (auto&& it : author_names) {
206  s_GetAuthorNames(it, new_auth_list.GetObject());
207  }
208  }
209  }
210  else {
211  // each line contains one author's name
212  s_GetAuthorNames(str, new_auth_list.GetObject());
213  }
214  }
215  }
216 
217  if (!new_auth_list->SetNames().SetStd().empty()) {
218  SetAuthors(*new_auth_list);
219  }
220  }
221  }
222 }
223 
224 
USING_SCOPE(objects)
static void s_GetAuthorNames(const string &line, CAuth_list &author_list)
@Auth_list.hpp User-defined methods of the data storage class.
Definition: Auth_list.hpp:57
void x_DeleteAuthor(wxSizerItem *link)
virtual void x_MoveRowUp(int row_num)=0
wxScrolledWindow * m_ScrolledWindow
virtual int x_FindRow(wxWindow *wnd, wxSizerItemList &itemList)=0
virtual void SetAuthors(const objects::CAuth_list &auth_list)=0
virtual void x_AddRowToWindow(wxWindow *row)=0
void ShiftAuthorUp(wxWindow *link)
void ShiftAuthorDown(wxWindow *link)
CAuthor –.
Definition: Author.hpp:59
static wxString GetDialogFilter(EFileType fileType)
@Name_std.hpp User-defined methods of the data storage class.
Definition: Name_std.hpp:56
static const char * str(char *buf, int n)
Definition: stats.c:84
#define NULL
Definition: ncbistd.hpp:225
EDialogReturnValue NcbiMessageBox(const string &message, TDialogType type=eDialog_Ok, EDialogIcon icon=eIcon_Exclamation, const string &title="Error", EDialogTextMode text_mode=eRaw)
Definition: message_box.cpp:48
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
CTempString GetCurrentLine(void) const
static CRef< ILineReader > New(const string &filename)
Return a new ILineReader object corresponding to the given filename, taking "-" (but not "....
Definition: line_reader.cpp:49
void ReadLine(void)
Definition: line_reader.hpp:88
virtual bool AtEOF(void) const =0
Indicates (negatively) whether there is any more input.
TObjectType & GetObject(void)
Get object.
Definition: ncbiobj.hpp:1011
bool Empty(void) const THROWS_NONE
Check if CRef is empty – not pointing to any object, which means having a null value.
Definition: ncbiobj.hpp:719
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:439
#define kEmptyStr
Definition: ncbistr.hpp:123
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:3461
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 bool EndsWith(const CTempString str, const CTempString end, ECase use_case=eCase)
Check if a string ends with a specified suffix value.
Definition: ncbistr.hpp:5430
static bool IsBlank(const CTempString str, SIZE_TYPE pos=0)
Check if a string is blank (has no text).
Definition: ncbistr.cpp:106
#define NPOS
Definition: ncbistr.hpp:133
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
@ fSplit_Tokenize
All delimiters are merged and trimmed, to get non-empty tokens only.
Definition: ncbistr.hpp:2508
void SetName(TName &value)
Assign a value to Name data member.
Definition: Author_.cpp:81
void SetNames(TNames &value)
Assign a value to Names data member.
Definition: Auth_list_.cpp:149
void SetInitials(const TInitials &value)
Assign a value to Initials data member.
Definition: Name_std_.hpp:619
void SetLast(const TLast &value)
Assign a value to Last data member.
Definition: Name_std_.hpp:431
void SetFirst(const TFirst &value)
Assign a value to First data member.
Definition: Name_std_.hpp:478
const TFirst & GetFirst(void) const
Get the First member data.
Definition: Name_std_.hpp:469
END_EVENT_TABLE()
FILE * file
Lightweight interface for getting lines of data with minimal memory copying.
#define wxT(x)
Definition: muParser.cpp:41
#define row(bind, expected)
Definition: string_bind.c:73
Modified on Sat Apr 20 12:16:55 2024 by modify_doxy.py rev. 669887