My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***********************************************************************************************
* StarIndicator
*
* Displays stars rating with number of votes
*
* Colors are used to show the confidence we can give to the grade, according to the number of
* votes :
* - Light blue if undefined,
* - Red if less than m_nVotesLow,
* - Yellow if more than m_nVotesLow and less than m_nVotesHigh,
* - Light Green if more than m_nVotesHigh
*
* Autors : jibe
* Last modified : November 28, 2011
*
* 2do : Make better graphics
* Known bugs : Invalid memory access when choosing SetFontColor with .lay designer (only under
* linux ?)
**********************************************************************************************/

#include <CtrlLib/CtrlLib.h>

//NAMESPACE_UPP
using namespace Upp;

#include "StarIndicator.h"

#define IMAGECLASS StarsImg
#define IMAGEFILE <Controls4U/StarIndicator.iml>
#include <Draw/iml.h>

StarIndicator::StarIndicator()
:ProgressIndicator()
, m_bAlwaysShowValue(false)
, m_bVotes(true)
, m_FontColor(Black)
, m_nVotes(0)
, m_nVotesHigh(50)
, m_nVotesLow(5)
/***********************************************************************************************
* Constructor
* public
**********************************************************************************************/
{
m_bMouseInside=false;
}

StarIndicator::~StarIndicator()
/***********************************************************************************************
* Destructor
* virtual - public
**********************************************************************************************/
{
}

void StarIndicator::Layout() {
/***********************************************************************************************
* virtual - public
**********************************************************************************************/
Rect r=GetRect();
Size size=r.GetSize();
int x=size.cx;
int y=int(ceil(double(x/5)));
x=5*y;
r.SetSize(x, y);
Set(actual, total);
}

void StarIndicator::Paint(Draw& w) {
/***********************************************************************************************
* Paints the control
* virtual - public
**********************************************************************************************/
Font fnt = SansSerif();
Size sz = GetSize();
Size msz = GetMsz();
Rect r;
String pt;
int p0 = 0;
int p = pxp;
fnt.Bold();
if(total <= 0) {
int l = max(msz.cx, msz.cy) & ~7;
p0 = pxp - l / 4;
p = min(p - p0, max(msz.cx, msz.cy) - p0);
if(style->bound && p0 < 0) {
p += p0;
p0 = 0;
}
}
ChPaintEdge(w, sz, EditFieldEdge());
Rect mg = ChMargins(EditFieldEdge());
sz -= Size(mg.left + mg.right, mg.top + mg.bottom);
Rect r1, r2, r3;
r = r1 = r2 = r3 = RectC(mg.left, mg.top, sz.cx, sz.cy);
w.Clip(r1);
if(sz.cx > sz.cy) {
r1.right = r2.left = min(p, sz.cx) + mg.left + p0;
r3.right = mg.left + p0;
r3.bottom = r1.bottom;
}
else {
r2.bottom = r1.top = sz.cy - min(p, sz.cy) + mg.left + p0;
r3.bottom = mg.top + p0;
r3.right = r1.right;
}
if (m_nVotes) {
w.DrawRect(r1, Nvl(color, SColorHighlight()));
w.DrawRect(r2, SColorPaper);
w.DrawRect(r3, SColorPaper);
w.DrawImage(r,StarsImg::Get(0),Nvl(color, SColorHighlight()));
w.DrawImage(r,StarsImg::Get(1),SColorPaper);
}
if (percent) {
pt = Format("%d %%", 100 * actual / max(total, 1));
}
else {
pt = Format("%3.1f", 5. * actual / max(total, 1));
if (m_bTotal) pt += " / 5";
}
if (m_bVotes && m_nVotes > 0) {
pt = Format("%s - %d Votes", pt, m_nVotes);
}
if (m_bMouseInside || m_bAlwaysShowValue) {
Size psz = GetTextSize(pt, fnt());
sz = GetSize();
while (psz.cy < sz.cy) {
fnt.Height(fnt.GetHeight()+1);
psz = GetTextSize(pt, fnt());
}
while (psz.cx > sz.cx - 8 || psz.cy > sz.cy -4) {
fnt.Height(fnt.GetHeight()-1);
psz = GetTextSize(pt, fnt());
}
int px = (sz.cx - psz.cx) / 2;
int py = (sz.cy - psz.cy) / 2;
w.DrawText(px, py, pt, fnt(), m_FontColor);
}
w.End();
}

void StarIndicator::MouseEnter(Point p, dword keyflags)
/***********************************************************************************************
* Mouse got inside the control
* virtual - public
**********************************************************************************************/
{
m_bMouseInside = true;
}

void StarIndicator::MouseLeave()
/***********************************************************************************************
* Mouse got outside the control
* virtual - public
**********************************************************************************************/
{
m_bMouseInside = false;
Refresh();
}

void StarIndicator::Set(double n)
/***********************************************************************************************
* Sets the value
* virtual - public
**********************************************************************************************/
{
Set(fceil(n*20),100);
}

StarIndicator& StarIndicator::SetVotes(int n)
/***********************************************************************************************
* Sets the number of votes and the associated color
* virtual - public
**********************************************************************************************/
{
m_nVotes=n;
if (n<0) SetColor(LtCyan);
else if (n < m_nVotesLow) SetColor(Color(255,128,128));
else if (n < m_nVotesHigh) SetColor(Yellow);
else SetColor(LtGreen);
Refresh();
return *this;
}

//END_UPP_NAMESPACE

Change log

r4233 by koldo on Dec 2, 2011   Diff
Controls4U: Added StarIndicator and doc by
Jibe
Go to: 
Project members, sign in to write a code review

Older revisions

r3774 by koldo on Aug 20, 2011   Diff
Controls4U: Added SliderCtrlX and
StarIndicator. Removed internal
MyBufferPainter
All revisions of this file

File info

Size: 5679 bytes, 185 lines
Powered by Google Project Hosting