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

Go to the SVN repository for this file.

1 /* $Id: gradient_panel.cpp 24939 2011-12-29 14:52:50Z wuliangs $
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 
34 #include <gui/utils/rgba_color.hpp>
35 
37 
39 
40 
41 #include <wx/dc.h>
42 #include <wx/settings.h>
43 #include <wx/dcclient.h>
44 
45 
47 
48 
49 BEGIN_EVENT_TABLE(CGradientPanel, wxWindow)
50  EVT_PAINT(CGradientPanel::OnPaint)
52 
53 static const int kGradH = 16;
57 
58 
59 CGradientPanel::CGradientPanel(wxWindow* parent,
60  wxWindowID id,
61  const wxPoint& pos,
62  const wxSize& size,
63  long style,
64  const wxString& name)
65 : wxPanel(parent, id, pos, size, style, name),
66  m_Params(NULL)
67 {
68 // SetBackgroundStyle(wxBG_STYLE_CUSTOM); // do not erase it
69 
70  m_Font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
71 
72 }
73 
74 
76 {
77  int text_h = abs(m_Font.GetPixelSize().y);
78 
79  wxSize sz(200, kGradH + kTickH + text_h + 5 * kOffset);
80  CacheBestSize(sz);
81  return sz;
82 }
83 
84 
85 void CGradientPanel::OnPaint(wxPaintEvent& event)
86 {
87  wxPaintDC dc(this);
88 
89  wxRect rc = GetClientRect();
90 
91  if(m_Params && IsEnabled()) {
92  rc.Deflate(kOffset);
93 
94  int left = rc.GetLeft();
95  int top = rc.GetTop();
96  int width = rc.GetWidth();
97 
98  int right = left + width - 1;
99 
100  // draw gradient bar
101  int n = m_Params->m_Steps;
102 
103  double grad_start = m_Params->m_EnableMinGrad ? m_Params->m_MinGrad : m_Params->m_MinValue;
104  double start = min(m_Params->m_MinValue, grad_start);
106  double end = max(m_Params->m_MaxValue, grad_end);
107 
108  double norm_start = m_Params->GetColorNorm(grad_start);
109  double norm_end = m_Params->GetColorNorm(grad_end);
110  norm_start = max(0.0, norm_start);
111  norm_end = min(1.0, norm_end);
112 
113  dc.SetPen(*wxTRANSPARENT_PEN);
114 
115  double d = ((double) (norm_end - norm_start) * width) / n;
116  int grad_x = left + (int)(norm_start * width);
117  for(int i = 0; i < n ; i++) {
118  int x1 = (int) floor(d * i);
119  int x2 = (int) ceil(d * (i + 1));
120  float k = ((float) i) / (n - 1);
122 
123  wxBrush brush(ConvertColor(color));
124  dc.SetBrush(brush);
125  dc.DrawRectangle(grad_x + x1, top, x2 - x1, kGradH);
126  }
127 
128  // draw min max labels
129  wxColour cl_text = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
130  dc.SetTextForeground(cl_text);
131  dc.SetFont(m_Font);
132 
133  int tick_y = top + kGradH + kOffset;
134  int text_y = top + kGradH + kTickH + 2 * kOffset;
135 
136  int clip_left = left;
137  int clip_right = right;
138  wxAlignment align = (m_Params->m_MinValue > start) ? wxALIGN_CENTER : wxALIGN_LEFT;
139  x_DrawNumber(dc, m_Params->m_MinValue, left, right, clip_left, clip_right, text_y, align);
140 
141  align = (m_Params->m_MaxValue < end) ? wxALIGN_CENTER : wxALIGN_RIGHT;
142  clip_left = clip_right + kSpace;
143  x_DrawNumber(dc, m_Params->m_MaxValue, left, right, clip_left, clip_right, text_y, align);
144 
145  // draw tick marks
146  dc.SetPen(wxPen(cl_text));
147 
148  double value_range = m_Params->m_MaxValue - m_Params->m_MinValue;
149  double d_value = value_range / 4;
150  for( int i = 0; i < 5; i++ ) {
151  double v = m_Params->m_MinValue + i * d_value;
152  double norm = m_Params->GetColorNorm(v);
153  int line_x = left + (int) (norm * (width - 1));
154  dc.DrawLine(line_x, tick_y, line_x, tick_y + kTickH);
155  }
156  }
157 }
158 
159 // draws a label for the given value, left and right params specify limits of the
160 // drawing area
161 void CGradientPanel::x_DrawNumber(wxDC& dc, double value, int left, int right,
162  int& clip_left, int& clip_right, int y, wxAlignment align)
163 {
164  double norm = m_Params->GetColorNorm(value);
165  int x = left + (int)(norm * (right - left));
166  int text_w = 0, text_h = 0;
167 
169  dc.GetTextExtent(str, &text_w, &text_h);
170 
171  switch(align) {
172  case wxALIGN_CENTER:
173  x -= text_w / 2;
174  break;
175  case wxALIGN_RIGHT:
176  x -= text_w;
177  break;
178  default:
179  break;
180  }
181 
182  // adjust position if necessary
183  if(x + text_w > right) {
184  x = right - text_w;
185  }
186  if(x < left) {
187  x = left;
188  }
189 
190  if(x >= left && (x + text_w - 1) <= right) { // draw
191  dc.DrawText(str, x, y);
192 
193  clip_left = x;
194  clip_right = x + text_w - 1;
195  } else {
196  clip_left = clip_right = left;
197  }
198 }
199 
200 
202 {
203  m_Params = params;
204  Refresh();
205 }
206 
207 
#define static
void SetParams(const SHitColoringParams *params)
void OnPaint(wxPaintEvent &event)
void x_DrawNumber(wxDC &dc, double value, int left, int right, int &clip_left, int &clip_right, int y, wxAlignment align)
virtual wxSize DoGetBestSize() const
const SHitColoringParams * m_Params
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
static const char * str(char *buf, int n)
Definition: stats.c:84
static const int kOffset
static const int kSpace
static const int kTickH
static const int kGradH
#define NULL
Definition: ncbistd.hpp:225
static CRgbaColor Interpolate(const CRgbaColor &color1, const CRgbaColor &color2, float alpha)
Interpolate two colors.
Definition: rgba_color.cpp:444
#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
n background color
END_EVENT_TABLE()
int i
yy_size_t n
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
#define abs(a)
Definition: ncbi_heapmgr.c:130
T max(T x_, T y_)
T min(T x_, T y_)
Format
Definition: njn_ioutil.hpp:52
double GetColorNorm(double value, bool precise=true) const
const char * GetPrecisionFormat() const
CRgbaColor m_MaxColor
CRgbaColor m_MinColor
wxColour ConvertColor(const CRgbaColor &color)
Definition: wx_utils.cpp:997
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
#define const
Definition: zconf.h:232
Modified on Wed Apr 17 13:10:03 2024 by modify_doxy.py rev. 669887