NCBI C++ ToolKit
alnmulti_model.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_WIDGETS_ALNMULTI___ALNMULTI_MODEL__HPP
2 #define GUI_WIDGETS_ALNMULTI___ALNMULTI_MODEL__HPP
3 
4 /* $Id: alnmulti_model.hpp 45626 2020-10-02 14:37:14Z shkeda $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Andrey Yazhuk
30  *
31  * File Description:
32  * CAlnMultiModel - representation of alignment for rendering.
33  */
34 
35 #include <corelib/ncbiobj.hpp>
36 
37 #include <util/range_coll.hpp>
38 
40 
44 
46 
48 
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// class CAlnMultiModel - represents a visual model of an alignment. Model is
53 /// used by Renderer to draw alignment in a widget or to generate an image
54 /// (for example in a CGI).
55 ///
56 /// Model presents an alignment as a set of graphical areas corresponding to the
57 /// alignment rows. Each area is represented by an instance if IAlignRow-derived
58 /// class and has attributes such as "visible" and "selected". Order of areas on
59 /// screen (lines) may be different from order of rows in the alignment.
60 /// Model holds the data necessary for rendering IAlignRow-derived objects
61 /// represnting rows, row attaributes and indexes facilitating operations with
62 /// Model (line <-> row, row_num <-> row).
63 
65  : public IAlnMultiRendererContext, // context for Renderer
66  public TNumrowModelImpl, // ISelListModel implementation
68 {
69 public:
70  typedef int TLine;
71  typedef list<objects::CSeq_loc*> TPSeqLocList;
74 
76 
77 public:
79  virtual ~CAlnMultiModel();
80 
81  enum {
82  kMasterRowIndex = -1000 ///< dummy master row index used in selection logic
83  };
84 
85  // ISelListModel interface implementation
86  virtual void SLM_SelectAll(bool bSelect);
87  virtual void SLM_SelectSingleItem(TIndex index);
88  virtual void SLM_InvertSingleItem(TIndex index);
89 
90  virtual void Init();
91  virtual void SetDataSource(IAlnMultiDataSource* p_ds);
92  virtual void SetListener(CScoreCache::IListener* listener);
93 
94  virtual void SetStyleCatalog(const CRowStyleCatalog* catalog);
95  virtual CWidgetDisplayStyle* GetDisplayStyle();
96 
98 
99  IAlignRow* GetRow(TNumrow row);
100  const IAlignRow* GetRow(TNumrow row) const;
101  /// Get number of rows registered in model
102  const size_t GetNumRows() const;
103 
104  bool IsRowVisible(TNumrow row) const;
105  bool IsRowSelected(TNumrow row) const;
106 
107  /// returns a combination of IAlignRow::EState flags
108  int GetRowState(TNumrow row) const;
109 
110  inline void GetSelectedRows(vector<TNumrow>& rows);
111 
112  virtual void CreateRows(void);
113  virtual void ClearRows(void);
114 
115  /// Creates consenus row if it is not already created
116  virtual void CreateConsensus(void);
117 
118  void SetPortLimits(void);
119 
120  /// changes Master (Anchor) and performs necessary updates
121  virtual bool CanChangeMasterRow() const;
122  virtual bool SetMasterRow(TNumrow new_row);
123 
124  /// @name Sorting and Reordering API
125  /// {
126 
127  /// enables auto sorting but does not perform any updates
128  virtual void EnableAutoSorting(bool b_en);
129 
130  /// enables auto sorting of rows, sorts rows using x_SortRows() and
131  /// performs necessary updates
132  virtual void UpdateSortOrder(void);
133 
134  /// sorts visible rows by specified column, return "false" if sorting is
135  /// not supported (nothin happened)
136  virtual bool SortByColumn(TColumnType type, bool ascending);
137 
138  /// returns sorting state for the specified column
139  virtual TSortState GetColumnSortState(TColumnType type);
140 
141  /// returns mask of shown elements - header, ruler, master row, alignment, track
142  /// matrix
143  virtual int GetShownElements();
144 
145  virtual void SetShownElements(int shown_mask);
146 
147  /// disables auto sorting, reorders rows in alignment so that rows from
148  /// the given vector appear in the specified order starting from "pos"
149  /// position
150  virtual void SetRowOrder(const vector<TNumrow>& rows, int pos = 0);
151  /// @}
152 
153 
154  /// makes specified rows visible/hidden, if b_invert_others == "true"
155  /// then visibility of all rows not in "rows" will be changed to ! b_visible
156  void SetVisible(vector<TNumrow> rows, bool b_visible,
157  bool b_invert_others = false);
158  void ShowAll();
159 
160  virtual void UpdateOnStyleChanged(void);
161 
162  /// updates model after row height changed
163  virtual void UpdateOnRowHChanged(void);
164 
165  virtual void UpdateOnScoringChanged(const TSeqRange& range = TSeqRange::GetEmpty());
166 
167  virtual void UpdateOnDataChanged();
168 
169  /// @name IAlnMultiRendererContext implementation
170  /// @{
171  virtual const CGlPane& GetAlignPort() const;
172  virtual bool IsRendererFocused();
173  /// NUmber of visible lines
174  virtual TNumrow GetLinesCount() const;
175  virtual int GetFocusedItemIndex() const;
176  virtual bool IsItemSelected(int index) const;
177 
178  virtual IAlignRow* GetMasterRow();
179  virtual IAlignRow* GetConsensusRow();
180 
181  virtual IAlignRow* GetRowByLine(TLine line);
182  virtual int GetLineByRowNum(TNumrow row) const;
183  virtual int GetLineByModelY(int Y) const;
184  virtual int GetLinePosY(TLine line) const;
185  virtual int GetLineHeight(TLine line) const;
186  /// @}
187 
188  /// @name Scoring Methods management functions
189  /// @{
190  typedef list<CIRef<IScoringMethod> > TMethods;
191 
192  /// adds scoring method; model assumes ownership of the method, it will be
193  /// deleted automatically
194  virtual bool AddScoringMethod(IScoringMethod* method);
195  virtual const TMethods& GetMethods();
196 
197  virtual void EnableBackgroundProcessing(bool en);
198 
199  virtual const IScoringMethod* GetCurrentMethod() const;
200  virtual IScoringMethod* GetCurrentMethod();
201 
202  virtual bool SetCurrentMethod(const string& name, const TSeqRange& range = TSeqRange::GetEmpty());
203  virtual void ResetCurrentMethod();
204 
205  virtual string GetDefaultMethod(void);
206  virtual void SetDefaultMethod(const string& method_name);
207  /// @}
208 
209  /// @name CScoreCache::IListener
210  /// @{
211  virtual void OnScoringProgress(float progress, const string& msg);
212  virtual void OnScoringFinished();
213  /// @}
214 
215 protected:
216  /// SRowRecord is a record representing a single alignment row
217  struct SRowRecord
218  {
219  CIRef<IAlignRow> m_Row; ///< row object ptr
220  TLine m_Line; ///< visual line index
221 
222  public:
223  SRowRecord(IAlignRow* p_row = 0, TLine line = -1)
224  : m_Row(p_row), m_Line(line)
225  {}
226  SRowRecord(const SRowRecord& proto)
227  : m_Row(proto.m_Row), m_Line(proto.m_Line)
228  {}
229 
230  bool IsVisible() const;
231  bool IsSelected() const;
232 
233  static bool PRecLineLess(const SRowRecord* rec1,
234  const SRowRecord* rec2);
235  };
236 
237 protected:
238  inline SRowRecord& x_GetRecordByRow(TNumrow row);
239  inline const SRowRecord& x_GetRecordByRow(TNumrow row) const;
240 
241  virtual void x_CreateDisplayStyle();
242 
243  /// factory method for creating IAlignRow instances
244  virtual IAlignRow* x_CreateRow(TNumrow row);
245 
246  virtual void x_UpdateMinScale();
247  virtual void x_UpdateOnCoordsChanged();
248  virtual void x_UpdateOnVisibilityChanged(void);
249 
250  virtual bool x_UsingAutoSort() const;
251 
252  /// This is a sorting callback automatically invoked by CAlnMultiModel when
253  /// necessary. Override this function in derived classes - default
254  /// implementation doesn't do anything.
255  /// x_SortRows() should rearrange eleemnts of m_vVisibleRows and return "true"
256  /// if any changes have been made - this will force necessary updates.
257  virtual bool x_SortRows(void);
258 
259  /// performs sorting by invoking x_SortRows() and updates line numbers
260  virtual void x_DoUpdateRowsOrder(void);
261 
262  /// recalculates vertical coordinates of row objects
263  virtual void x_DoUpdateRowPositions(void);
264 
265 
266  void x_DoUpdateRowLines(void); /// updates line numbers in SRowRecords
267 
268  /// @TNumrowModelImpl extension
269  /// @{
270  virtual void x_SelectItem(TIndex index, bool b_sel);
271  virtual bool x_IsItemSelected(TIndex index) const;
272 
273  virtual TIndex x_GetItemsCount() const;
274  virtual TItem x_GetItem(TIndex index) const;
275  virtual void x_SetEntries(const TEntryVector& v_entries);
276  virtual TIndex x_GetItemIndex(const TItem& item);
277 
278  virtual void x_InsertItem(TIndex index, const TItemEntry& entry);
279  virtual void x_CompleteInsertion();
280  virtual void x_MarkItemForErase(TIndex index);
281  virtual void x_EraseMarkedItems(void);
282  virtual void x_ClearItems(void);
283  /// @}
284 
285  IAlignRow* x_GetRowByLine(int Index);
286  const IAlignRow* x_GetRowByLine(int Index) const;
287 
288 protected:
289  typedef vector<SRowRecord*> TRowRecVector;
290  typedef vector<SRowRecord> TRowToRecMap;
291 
294 
297 
298  TRowToRecMap m_RowToRec; ///< map storing records for all rows
299 
300  CIRef<IAlignRow> m_MasterRow; ///< IAligRow corresponding to Anchor
301  CIRef<IAlignRow> m_DataConsensusRow; ///< IAligRow corresponding to consensus in the data
302  TRowRecVector m_vVisibleRows; ///< line number SRowRecord* for visible rows
303  vector<int> m_vVisibleRowPos; ///< bottom positions for visible rows[line]
304 
306 
307  /// sorting support
308  bool m_AutoSort; ///< enables automating sorting of rows using x_SortRows() callback
311 
312  /// scoring support
314  TMethods m_Methods; ///< registered scoring methods
316 
318 
319  // Alternative views support
321 
322 private:
324 
325 private:
326  /// forbidden
329 };
330 
331 
332 ////////////////////////////////////////////////////////////////////////////////
333 /// CAlnMultiModelFactory - a factory to separate creation of the model from
334 /// widget. Implement this interface is you need to use a custom model with a
335 /// standard widget.
337 {
338 public:
340 
341  virtual CAlnMultiModel* CreateModel() = 0;
342 };
343 
344 
345 ////////////////////////////////////////////////////////////////////////////////
346 /// CPairSorter
347 template <typename TKey, typename TValue> class CPairSorter
348 {
349 public:
350  typedef pair<TKey, TValue> TPair;
351  typedef vector<TPair> TPairVector;
352 
353  CPairSorter(bool ascending) : m_Ascending(ascending) {}
354 
356  {
357  if (m_Ascending)
358  std::sort(data.begin(), data.end(), [](const TPair& p1, const TPair& p2) {
359  return p1.second < p2.second;
360  });
361  else
362  std::sort(data.begin(), data.end(), [](const TPair& p1, const TPair& p2) {
363  return p1.second > p2.second;
364  });
365 
366  }
367 
368 protected:
370 };
371 
372 
373 ////////////////////////////////////////////////////////////////////////////////
374 /// CAlnMultiModel inline functions
375 
377 {
378  _ASSERT(row >= 0 && (size_t) row < m_RowToRec.size());
379  return m_RowToRec[row].m_Row;
380 }
381 
383 {
384  _ASSERT(row >= 0 && (size_t) row < m_RowToRec.size());
385  return m_RowToRec[row].m_Row;
386 }
387 
389 {
390  _ASSERT(row >= 0 && (size_t) row < m_RowToRec.size());
391  return m_RowToRec[row].IsVisible();
392 }
393 
395 {
396  _ASSERT(row >= 0 && (size_t) row < m_RowToRec.size());
397  return m_RowToRec[row].IsSelected();
398 }
399 
401 {
402  _ASSERT(row >= 0 && (size_t) row < m_RowToRec.size());
403  return m_RowToRec[row];
404 }
405 
407 {
408  _ASSERT(row >= 0 && (size_t) row < m_RowToRec.size());
409  return m_RowToRec[row];
410 }
411 
413 {
414  int state = m_Row->GetRowState();
415  return (state & IAlignRow::fItemHidden) == 0;
416 }
417 
419 {
420  int state = m_Row->GetRowState();
421  return (state & IAlignRow::fItemSelected) != 0;
422 }
423 
424 inline void CAlnMultiModel::GetSelectedRows(vector<TNumrow>& rows)
425 {
426  SLM_GetSelectedItems(rows);
427 }
428 
429 inline const size_t CAlnMultiModel::GetNumRows() const
430 {
431  return m_RowToRec.size();
432 }
433 
434 
436 
437 
438 #endif // GUI_WIDGETS_ALNMULTI___ALNMULTI_MODEL__HPP
CSelListModelImpl< IAlnMultiDataSource::TNumrow > TNumrowModelImpl
CAlnMultiModelFactory - a factory to separate creation of the model from widget.
virtual CAlnMultiModel * CreateModel()=0
class CAlnMultiModel - represents a visual model of an alignment.
CAlnMultiModel(const CAlnMultiModel &)
forbidden
SRowRecord & x_GetRecordByRow(TNumrow row)
CScoreCache::IListener * m_Listener
void GetSelectedRows(vector< TNumrow > &rows)
const CRowStyleCatalog * m_StyleCatalog
list< objects::CSeq_loc * > TPSeqLocList
TSortState m_SortState
TRowRecVector m_vVisibleRows
line number SRowRecord* for visible rows
bool m_EnableScoring
scoring support
IAlignRow * GetRow(TNumrow row)
CAlnMultiModel inline functions.
CIRef< IAlignRow > m_DataConsensusRow
IAligRow corresponding to consensus in the data.
CAlnMultiModel & operator=(const CAlnMultiModel &)
CRef< CScoreCache > m_ScoreCache
const size_t GetNumRows() const
Get number of rows registered in model.
vector< SRowRecord > TRowToRecMap
CRangeCollection< TSeqPos > TRangeColl
TColumnType m_SortColumn
list< CIRef< IScoringMethod > > TMethods
CRef< CConsensusRow > m_ConsensusRow
vector< int > m_vVisibleRowPos
bottom positions for visible rows[line]
CWidgetDisplayStyle * m_Style
IAlnExplorer::ESortState TSortState
TMethods m_Methods
registered scoring methods
TRowToRecMap m_RowToRec
map storing records for all rows
bool IsRowSelected(TNumrow row) const
bool IsRowVisible(TNumrow row) const
IAlignRow::TColumnType TColumnType
bool m_AutoSort
sorting support
CIRef< IAlignRow > m_MasterRow
IAligRow corresponding to Anchor.
vector< SRowRecord * > TRowRecVector
IAlnMultiDataSource * m_DataSource
class CGlPane
Definition: glpane.hpp:62
CPairSorter.
CPairSorter(bool ascending)
vector< TPair > TPairVector
pair< TKey, TValue > TPair
void Sort(TPairVector &data)
CRowStyleCatalog is a collection of CRowDisplayStyle-s assigned to rows in alignment.
virtual void OnScoringFinished()=0
virtual void OnScoringProgress(float progress, const string &msg)=0
void SetListener(IListener *listener)
Definition: aln_scoring.cpp:91
CSelListModelImpl provides a defult implementation of ISelListModel.
virtual TItem x_GetItem(TIndex index) const =0
virtual void SLM_GetSelectedItems(TItemVector &items) const
virtual void x_CompleteInsertion()=0
performs update after all items have been inserted
virtual bool x_IsItemSelected(TIndex index) const =0
virtual void SLM_SelectAll(bool bSelect=true)
virtual TIndex x_GetItemIndex(const TItem &item)=0
virtual void x_SelectItem(TIndex index, bool b_sel)=0
virtual TIndex x_GetItemsCount() const =0
virtual void x_SetEntries(const TEntryVector &v_entries)=0
virtual void SLM_InvertSingleItem(TIndex index)
virtual void x_ClearItems()=0
virtual void x_EraseMarkedItems()=0
deletes all marked items in a single pass, performs neccessary updates
virtual void SLM_SelectSingleItem(TIndex index)
virtual void x_InsertItem(TIndex index, const TItemEntry &entry)=0
inserts item, but does not updates all data structures
virtual void x_MarkItemForErase(TIndex index)=0
mark item for deletion
CWidgetDisplayStyle is a collection of display properties common for all rows in the CAlnMultiWidget.
Interface IAlignRow - abstracts row rendering in Multiple Alignment Widget.
Definition: ialign_row.hpp:67
int TColumnType
Definition: ialign_row.hpp:70
IAlnMultiDataSource - interface to a data source representing an abstract multiple alignment.
IAlnMultiRendererContext - this interface represents context in which CAlnMultiRenderer lives.
virtual IAlignRow * GetConsensusRow()=0
virtual int GetLineByRowNum(TNumrow row) const =0
virtual int GetShownElements()=0
virtual bool IsRendererFocused()=0
virtual bool IsItemSelected(int index) const =0
virtual const CGlPane & GetAlignPort() const =0
virtual bool SortByColumn(TColumnType type, bool ascending)=0
virtual int GetFocusedItemIndex() const =0
virtual IAlignRow * GetRowByLine(int index)=0
all Y coordinates are OpenGL Viewport coordinates (not widget coords) "index" is a line index (not ro...
virtual int GetLinePosY(int index) const =0
IAlnExplorer::TNumrow TNumrow
virtual int GetLineHeight(int index) const =0
virtual IAlnExplorer::ESortState GetColumnSortState(TColumnType type)=0
virtual IAlignRow * GetMasterRow()=0
virtual int GetLineByModelY(int y) const =0
virtual TNumrow GetLinesCount() const =0
IScoringMethod represents an abstract algorithm for calculating alignment scores and assigning colors...
static void Init(void)
Definition: cursor6.c:76
static const char * catalog
Definition: stats.c:19
char data[12]
Definition: iconv.c:80
static TThisType GetEmpty(void)
Definition: range.hpp:306
#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 NCBI_GUIWIDGETS_ALNMULTIPLE_EXPORT
Definition: gui_export.h:520
CMapControl::TIndex TIndex
range(_Ty, _Ty) -> range< _Ty >
constexpr auto sort(_Init &&init)
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
#define row(bind, expected)
Definition: string_bind.c:73
SRowRecord is a record representing a single alignment row.
CIRef< IAlignRow > m_Row
row object ptr
SRowRecord(IAlignRow *p_row=0, TLine line=-1)
SRowRecord(const SRowRecord &proto)
TLine m_Line
visual line index
Definition: type.c:6
#define _ASSERT
Modified on Thu Jun 13 17:27:01 2024 by modify_doxy.py rev. 669887