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

Go to the SVN repository for this file.

1 /* $Id: legend.cpp 23948 2011-06-23 17:05:49Z kuznets $
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: Andrey Yazhuk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <gui/graph/legend.hpp>
34 #include <gui/opengl/glutils.hpp>
35 
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 /// class CLegend
40 
42  : m_Font(CGlBitmapFont::eHelvetica12),
43  m_BackColor(0.95f, 0.95f, 0.95f),
44  m_BorderColor(0.0f, 0.0f, 0.0f),
45  m_bHorz(true),
46  m_Space(10)
47 {
48 }
49 
51 {
52 }
53 
55 {
57 }
59 {
60  return m_BackColor;
61 }
62 
64 {
66 }
67 
69 {
70  return m_BorderColor;
71 }
72 
74 {
75  ILegendDataSource* pLegendDS = dynamic_cast<ILegendDataSource*>(pDS);
76  bool bOk = pLegendDS!= NULL;
77  CGraphBase::SetDataSource(bOk ? pDS : NULL);
78 
80  return bOk;
81 }
82 
84 {
85  return m_Limits;
86 }
87 
89 {
90  m_Limits.Init(0, 0, 1, 1);
91 }
92 
93 void CLegend::Render(CGlPane* pPane, TElemVector* elems)
94 {
95  if(pPane) {
96  pPane->OpenPixels();
97  try {
98  TVPRect rcVP = pPane->GetViewport();
99 
100  // fill background
101  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
103  glRecti(rcVP.Left(), rcVP.Bottom(), rcVP.Right(), rcVP.Top());
104 
105  // draw Border
106  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
108  glRecti(rcVP.Left(), rcVP.Bottom(), rcVP.Right(), rcVP.Top());
109 
110  x_RenderItems(pPane);
111  }
112  catch(CException& e){
113  ERR_POST(e.ReportAll());
114  }
115  catch(std::exception& e) {
116  ERR_POST(e.what());
117  }
118  pPane->Close();
119  }
120 }
121 
123 {
125  if (pSource) {
126  IStringArray* pLabels = pSource->GetLabelArray();
127  IColorArray* pColors = pSource->GetColorArray();
128  INumericArray* pMarkers = pSource->GetMarkerArray();
129 
130  int H = max( (int)m_Font.TextHeight(), 10);
131 
132  TVPRect rcVP = pPane->GetViewport();
133  int LeftLimit = rcVP.Left() + m_Space;
134  int RightLimit = rcVP.Right() - m_Space;
135  int BottomLimit = rcVP.Bottom() + m_Space;
136 
137  int vpX = LeftLimit;
138  int StepY = H + max(H / 2, 4);
139  int vpY = rcVP.Top() - m_Space - StepY;
140  m_BoxW = pSource->ShowMarkers() ? H * 2 : H;
141  m_BoxH = H;
142 
143  int MaxX = LeftLimit;
144 
145  size_t N = pLabels->GetSize();
146  for ( size_t i = 0; i < N; i++ ) { // iterating by items
147  string sText = pLabels->GetElem(i);
148  int W = m_BoxW + m_Space + (int)m_Font.TextWidth(sText.c_str());
149  int iMarker = pSource->ShowMarkers() ? (int) pMarkers->GetElem(i) : -1;
150 
151  if( m_bHorz) {
152  if (vpX + W > RightLimit) { // new line
153  vpY -= StepY;
154  vpX = LeftLimit;
155  }
156  x_RenderItem(vpX, vpY, sText, pColors->GetElem(i), iMarker);
157 
158  vpX += W + m_Space * 2;
159  } else {
160  x_RenderItem(vpX, vpY, sText, pColors->GetElem(i), iMarker);
161 
162  MaxX = max(MaxX, vpX + W);
163  vpY -= StepY;
164 
165  if (vpY < BottomLimit) { // new column
166  vpY = rcVP.Top() - m_Space - StepY;
167  vpX = MaxX + m_Space;
168  }
169  }
170  }
171  }
172 }
173 
174 void CLegend::x_RenderItem(int X, int Y, const string& sLabel, const CRgbaColor& Color, int iMarker)
175 {
177 
178  switch (Type) {
184  //draw line
185  glColorC(Color);
186  glBegin(GL_LINES);
187  glVertex2i(X, Y + m_BoxH / 2);
188  glVertex2i(X + m_BoxW, Y + m_BoxH / 2);
189  glEnd();
190 
191  // draw marker
192  TModelUnit MarkerSize = (m_BoxH % 2) ? m_BoxH : (m_BoxH - 1);
193  CGraphDotMarker::RenderMarker(X + m_BoxW / 2, Y + m_BoxH / 2, MarkerSize, MarkerSize, Type);
194  break;
195  }
196  default: {
197  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
198  glColorC(Color);
199  glRectd(X, Y, X + m_BoxW, Y + m_BoxH);
200 
201  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
203  glRectd(X, Y, X + m_BoxW, Y + m_BoxH);
204  }
205  } //switch
206 
207  glColorC(Color);
208  m_Font.TextOut((float)(X + m_BoxW + m_Space), (float) Y, sLabel.c_str());
209 }
210 
211 ///////////////////////////////////////////////////////////////////////////////
212 /// class CLegendDataSource
213 
215 {
217 
218  TStrAdapter* pStrAd = new TStrAdapter(m_Length);
219  AddArray(static_cast<IDataArray*>(pStrAd));
220 
221  TColorAdapter* pCAd = new TColorAdapter(m_Length);
222  AddArray(static_cast<IDataArray*>(pCAd));
223 
224  TEnumAdapter* pEnAd = new TEnumAdapter(m_Length);
225  AddArray(static_cast<IDataArray*>(pEnAd));
226 }
227 
class CGlPane
Definition: glpane.hpp:62
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
class CTypedArrayAdapter<Type, TBase>
Definition: svg.hpp:122
interface IDataArray
Definition: igraph_data.hpp:50
IGraphDataSource.
Definition: igraph.hpp:55
ILegendDataSource.
Definition: legend.hpp:51
class ITypedDataArray<Type>
Definition: igraph_data.hpp:90
#define true
Definition: bool.h:35
#define H(x, y, z)
Definition: md4.c:180
#define NULL
Definition: ncbistd.hpp:225
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
string ReportAll(TDiagPostFlags flags=eDPF_Exception) const
Report all exceptions.
Definition: ncbiexpt.cpp:370
virtual const char * what(void) const noexcept
Standard report (includes full backlog).
Definition: ncbiexpt.cpp:342
void AddArray(IDataArray *pArray)
virtual IColorArray * GetColorArray()=0
const CRgbaColor & GetBorderColor()
Definition: legend.cpp:68
virtual ~CLegend()
Definition: legend.cpp:50
const CRgbaColor & GetBackColor() const
Definition: legend.cpp:58
void SetBackColor(const CRgbaColor &Color)
Definition: legend.cpp:54
void SetBorderColor(const CRgbaColor &Color)
Definition: legend.cpp:63
int m_Space
Definition: legend.hpp:93
vector< SGraphElem * > TElemVector
Definition: igraph.hpp:72
CTypedArrayAdapter< IDataArray::eString > TStrAdapter
Definition: legend.hpp:115
CTypedArrayAdapter< IDataArray::eNumeric, int > TEnumAdapter
Definition: legend.hpp:117
bool m_bHorz
Definition: legend.hpp:92
virtual void x_RenderItems(CGlPane *pPane)
Definition: legend.cpp:122
CGlBitmapFont m_Font
Definition: legend.hpp:87
CRgbaColor m_BorderColor
Definition: legend.hpp:90
int m_BoxW
Definition: legend.hpp:94
CLegend()
class CLegend
Definition: legend.cpp:41
virtual void CreateArrays()
class CLegendDataSource
Definition: legend.cpp:214
virtual void Render(CGlPane *pViewport, TElemVector *elems=NULL)
Definition: legend.cpp:93
virtual INumericArray * GetMarkerArray()=0
CRgbaColor m_BackColor
Definition: legend.hpp:89
virtual size_t GetSize()=0
TModelRect m_Limits
Definition: igraph.hpp:121
int m_BoxH
Definition: legend.hpp:94
virtual void CalculateLimits()
Definition: legend.cpp:88
virtual void CreateArrays()
Definition: igraph_data.cpp:89
virtual const TModelRect & GetLimits() const
Definition: legend.cpp:83
virtual IStringArray * GetLabelArray()=0
virtual bool SetDataSource(IGraphDataSource *pDS)
Definition: legend.cpp:73
virtual bool ShowMarkers()=0
CTypedArrayAdapter< IDataArray::eColor > TColorAdapter
Definition: legend.hpp:116
virtual bool SetDataSource(IGraphDataSource *pDS)
Definition: igraph.cpp:61
virtual TValueType GetElem(size_t i)=0
static void RenderMarker(TModelUnit cX, TModelUnit cY, TModelUnit MarkerW, TModelUnit MarkerH, EMarkerType Type)
class CGraphDotMarker
Definition: igraph.cpp:85
ILegendDataSource * GetLegendDataSource()
Definition: legend.hpp:80
virtual void x_RenderItem(int X, int Y, const string &sLabel, const CRgbaColor &Color, int iMarker)
Definition: legend.cpp:174
GLdouble TModelUnit
Definition: gltypes.hpp:48
virtual void TextOut(const char *text) const
void Init()
Definition: glrect.hpp:62
T Top() const
Definition: glrect.hpp:84
T Bottom() const
Definition: glrect.hpp:82
bool OpenPixels()
Definition: glpane.hpp:432
T Right() const
Definition: glrect.hpp:83
TVPRect & GetViewport(void)
Definition: glpane.hpp:332
virtual TModelUnit TextHeight(void) const
compute the height of a string
virtual TModelUnit TextWidth(const char *text) const
compute the length of a null-terminated string
T Left() const
Definition: glrect.hpp:81
void glColorC(const CRgbaColor &color)
Definition: glutils.hpp:177
void Close(void)
Definition: glpane.cpp:178
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
T max(T x_, T y_)
#define Type
#define N
Definition: crc32.c:57
#define W
Definition: crc32.c:85
Modified on Tue May 28 05:49:14 2024 by modify_doxy.py rev. 669887