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

Go to the SVN repository for this file.

1 /*$Id: table_model.cpp 47739 2024-06-28 18:13:02Z ivanov $
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: Yury Voronov, Andrey Yazhuk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
36 
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 /// CwxTableModelNotice
41 
43 : source(source),
44  type(eAllDataChanged),
45  firstRow(0),
46  lastRow(0),
47  column(ALL_COLUMNS)
48 {
49 }
50 
51 
53 : source(source),
54  type(eUpdate),
55  firstRow(row),
56  lastRow(row),
57  column(ALL_COLUMNS)
58 {
59 }
60 
61 
63  int col, EType type)
64 : source(source),
65  type(type),
66  firstRow(first_row),
67  lastRow(last_row),
68  column(col)
69 {
70 }
71 
72 
73 ///////////////////////////////////////////////////////////////////////////////
74 /// CwxAbstractTableModel
77  m_ListenerList()
78 {
79 }
80 
81 
82 wxString CwxAbstractTableModel::GetColumnName( int aColIx ) const {
83 
84  wxString name;
85  for(; aColIx >= 0; aColIx = aColIx / 26 - 1 ){
86  name = wxString::Format( wxT("%c"), (char)( (char)(aColIx % 26) + 'A' ) ) + name;
87  }
88  return name;
89 }
90 
91 
92 wxString CwxAbstractTableModel::GetColumnType( int aColIx ) const {
93 
94  return GetNumRows() > 0 ? GetValueAt( 0, aColIx ).GetType() : wxString("int");
95 }
96 
97 
99 
100  // we inserted listeners in reverse order
101  // for easier notice propagation
102  m_ListenerList.push_front( &aTMEar );
103 }
104 
105 
107 
108  m_ListenerList.remove( &aTMEar );
109 }
110 
111 
113 
114  CwxTableModelNotice notice( *this );
115  x_FireTableChanged( notice );
116 }
117 
118 
120 
121  CwxTableModelNotice notice(
124  );
125  x_FireTableChanged( notice );
126 }
127 
128 
129 void CwxAbstractTableModel::x_FireRowsInserted( int aFirstRow, int aLastRow ){
130 
131  CwxTableModelNotice notice(
132  *this, aFirstRow, aLastRow,
134  );
135  x_FireTableChanged( notice );
136 }
137 
138 
139 void CwxAbstractTableModel::x_FireRowsUpdated( int aFirstRow, int aLastRow ){
140 
141  CwxTableModelNotice notice(
142  *this, aFirstRow, aLastRow,
144  );
145  x_FireTableChanged( notice );
146 }
147 
148 
149 void CwxAbstractTableModel::x_FireRowsDeleted( int aFirstRow, int aLastRow ){
150 
151  CwxTableModelNotice notice(
152  *this, aFirstRow, aLastRow,
154  );
155  x_FireTableChanged( notice );
156 }
157 
158 
159 void CwxAbstractTableModel::x_FireCellUpdated( int aRow, int aCol ){
160 
161  CwxTableModelNotice notice( *this, aRow, aRow, aCol );
162  x_FireTableChanged( notice );
163 }
164 
165 
167 
168  NON_CONST_ITERATE( list<IwxTableModelListener*>, iter, m_ListenerList ){
169  (*iter) -> TableChanged( aNote );
170  }
171 }
172 
173 
174 ///////////////////////////////////////////////////////////////////////////////
175 /// CwxTextTableModel
176 CTextTableModel::CTextTableModel(int n_cols, int n_rows)
177 {
178  Init(n_cols, n_rows);
179 }
180 
181 
182 void CTextTableModel::Init(int n_cols, int n_rows)
183 {
184  vector<wxString> names(n_cols, wxString() );
185  Init(names, n_rows);
186 }
187 
188 /*
189 void CTextTableModel::Init(const vector<string>& columns, int n_rows)
190 {
191  ITERATE( vector<string>, col_it, columns ){
192  m_ColumnNames.push_back( wxString::FromUTF8( col_it->c_str() ) );
193  }
194 
195  m_Icons.clear();
196  m_Columns.clear();
197  m_Attachments.clear();
198 
199  size_t n_cols = m_ColumnNames.size();
200 
201  _ASSERT(n_cols > 0 && n_rows >= 0);
202 
203  m_Columns.resize(n_cols);
204 
205  SetNumRows( n_rows );
206 }
207 */
208 
209 void CTextTableModel::Init(const vector<wxString>& columns, int n_rows)
210 {
211  m_ColumnNames.clear();
212  ITERATE( vector<wxString>, col_it, columns ){
213  m_ColumnNames.push_back( *col_it );
214  }
215 
216  m_Icons.clear();
217  m_Columns.clear();
218  m_Attachments.clear();
219 
220  size_t n_cols = m_ColumnNames.size();
221 
222  _ASSERT(n_cols > 0 && n_rows >= 0);
223 
224  m_Columns.resize(n_cols);
225 
226  SetNumRows( n_rows );
227 }
228 
229 void CTextTableModel::SetNumRows( int n_rows )
230 {
231  m_Icons.resize(n_rows);
232  m_Attachments.resize(n_rows);
233 
234  for( size_t i = 0; i < m_Columns.size(); i++ ) {
235  m_Columns[i].resize( n_rows );
236  }
237 }
238 
239 
240 /*
241 void CTextTableModel::SetIcon( int row, const string& icon_alias )
242 {
243  _ASSERT(row >= 0 && row < (int) m_Icons.size());
244  m_Icons[row] = wxString::FromUTF8( icon_alias.c_str() );
245 }
246 */
247 
248 void CTextTableModel::SetIcon( int row, const wxString& icon_alias )
249 {
250  _ASSERT(row >= 0 && row < (int) m_Icons.size());
251  m_Icons[row] = icon_alias;
252 }
253 
254 /*
255 void CTextTableModel::SetStringValueAt( int row, int col, const string& value )
256 {
257  _ASSERT(col >=0 && col < (int) m_Columns.size());
258 
259  TColumn& column = m_Columns[col];
260 
261  _ASSERT(row >= 0 && row < (int) column.size());
262 
263  column[row] = wxString::FromUTF8( value.c_str() );
264 }
265 */
266 
267 void CTextTableModel::SetStringValueAt( int row, int col, const wxString& value )
268 {
269  _ASSERT(col >=0 && col < (int) m_Columns.size());
270 
271  TColumn& column = m_Columns[col];
272 
273  _ASSERT(row >= 0 && row < (int) column.size());
274 
275  column[row] = value;
276 }
277 
278 void CTextTableModel::SetAttachment( int row, void* attachment )
279 {
280  _ASSERT(row >= 0 && row < (int) m_Attachments.size());
281  m_Attachments[row] = attachment;
282 }
283 
284 
286 {
287  return static_cast<int>(m_Columns.empty() ? 0 : m_Columns[0].size());
288 }
289 
290 
292 {
293  return static_cast<int>(m_Columns.size());
294 }
295 
296 
297 wxVariant CTextTableModel::GetValueAt( int row, int col ) const
298 {
299  _ASSERT(col >=0 && col < (int) m_Columns.size());
300 
301  const TColumn& column = m_Columns[col];
302 
303  _ASSERT(row >= 0 && row < (int) column.size());
304 
305  return column[row];
306 }
307 
308 
309 wxString CTextTableModel::GetColumnName(int col) const
310 {
311  _ASSERT(col >=0 && col < (int) m_ColumnNames.size());
312  //return wxString::FromUTF8( m_ColumnNames[col].c_str() ); // ToWxString
313  return m_ColumnNames[col];
314 }
315 
316 
318 {
319  return m_Attachments[row];
320 }
321 
322 
324 {
326 }
327 
329 {
331 }
332 
333 
334 
335 void CTextTableModel::FireRowsUpdated( int first_row, int last_row )
336 {
337  x_FireRowsUpdated( first_row, last_row );
338 }
339 
340 
341 
342 wxString CTextTableModel::GetImageAliasAt( int row, int col ) const
343 {
344  if( col == 0 ){
345  _ASSERT(row >= 0 && row < (int) m_Icons.size());
346 
347  return m_Icons[row];
348  } else {
349  return wxString();
350  }
351 }
352 
353 
vector< wxString > TColumn
vector< wxString > m_Icons
virtual void * GetAttachment(int row)
vector< void * > m_Attachments
virtual void Init(int n_cols, int n_rows)
void SetNumRows(int n_rows)
virtual wxString GetColumnName(int col) const
Returns a default name for the column using spreadsheet conventions: A, B, C, ...
virtual wxVariant GetValueAt(int row, int col) const
CTextTableModel(int n_cols=1, int n_rows=0)
CwxTextTableModel.
virtual int GetNumColumns() const
Returns the number of columns in the model.
vector< TColumn > m_Columns
virtual void FireDataChanged()
virtual void SetAttachment(int row, void *attachment)
virtual void SetIcon(int row, const wxString &icon_alias)
vector< wxString > m_ColumnNames
virtual void SetStringValueAt(int row, int col, const wxString &value)
virtual void FireStructureChanged()
virtual wxString GetImageAliasAt(int row, int col) const
virtual void FireRowsUpdated(int first_row, int last_row=-1)
virtual int GetNumRows() const
Returns the number of rows in the model.
virtual void x_FireTableChanged(const CwxTableModelNotice &note)
virtual void RemoveTMListener(IwxTableModelListener &aTMEar)
virtual void x_FireCellUpdated(int row, int col)
virtual wxVariant GetValueAt(int row, int col) const =0
virtual void x_FireRowsUpdated(int first_row, int last_row=-1)
virtual wxString GetColumnType(int col_idx) const
Tries to extract actual type from row 0 value if it exists.
Definition: table_model.cpp:92
virtual wxString GetColumnName(int col_idx) const
Returns a default name for the column using spreadsheet conventions: A, B, C, ...
Definition: table_model.cpp:82
virtual void x_FireStructureChanged()
virtual void x_FireRowsInserted(int first_row, int last_row=-1)
virtual void AddTMListener(IwxTableModelListener &aTMEar)
Definition: table_model.cpp:98
list< IwxTableModelListener * > m_ListenerList
List of TM Listeners Listeners are just stored, no deletion upon removal.
CwxAbstractTableModel()
CwxAbstractTableModel.
Definition: table_model.cpp:75
virtual void x_FireRowsDeleted(int first_row, int last_row=-1)
virtual int GetNumRows() const =0
Returns the number of rows in the model.
virtual void x_FireDataChanged()
CwxTableModelNotice.
Definition: table_model.hpp:86
CwxTableModelNotice(IwxTableModel &source)
CwxTableModelNotice.
Definition: table_model.cpp:42
static const int ALL_COLUMNS
Definition: table_model.hpp:88
static const int HEADER_ROW
Definition: table_model.hpp:89
IwxDecoratedTableModel.
IwxTableModelListener.
static const struct name_t names[]
static const char * column
Definition: stats.c:23
static const column_t columns[]
Definition: utf8_2.c:22
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
int i
#define wxT(x)
Definition: muParser.cpp:41
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
const CharType(& source)[N]
Definition: pointer.h:1149
Format
Definition: njn_ioutil.hpp:52
#define row(bind, expected)
Definition: string_bind.c:73
Definition: type.c:6
#define _ASSERT
Modified on Fri Sep 20 14:58:12 2024 by modify_doxy.py rev. 669887