xref: /haiku/src/apps/haikudepot/ui_generic/RatingView.cpp (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
1 /*
2  * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "RatingView.h"
8 
9 #include <stdio.h>
10 
11 #include <LayoutUtils.h>
12 
13 
14 RatingView::RatingView(const char* name)
15 	:
16 	BView(name, B_WILL_DRAW),
17 	fStarBitmap(501),
18 	fRating(-1.0f)
19 {
20 	SetViewColor(B_TRANSPARENT_COLOR);
21 	SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
22 }
23 
24 
25 RatingView::~RatingView()
26 {
27 }
28 
29 
30 void
31 RatingView::AttachedToWindow()
32 {
33 	BView* parent = Parent();
34 	if (parent != NULL)
35 		SetLowColor(parent->ViewColor());
36 }
37 
38 
39 void
40 RatingView::Draw(BRect updateRect)
41 {
42 	FillRect(updateRect, B_SOLID_LOW);
43 
44 	if (fRating < 0.0f)
45 		return;
46 
47 	const BBitmap* star = fStarBitmap.Bitmap(SharedBitmap::SIZE_16);
48 	if (star == NULL) {
49 		fprintf(stderr, "No star icon found in application resources.\n");
50 		return;
51 	}
52 
53 	SetDrawingMode(B_OP_OVER);
54 
55 	float x = 0;
56 	for (int i = 0; i < 5; i++) {
57 		DrawBitmap(star, BPoint(x, 0));
58 		x += 16 + 2;
59 	}
60 
61 	if (fRating >= 5.0f)
62 		return;
63 
64 	SetDrawingMode(B_OP_OVER);
65 
66 	BRect rect(Bounds());
67 	rect.right = x - 2;
68 	rect.left = ceilf(rect.left + (fRating / 5.0f) * rect.Width());
69 
70 	rgb_color color = LowColor();
71 	color.alpha = 190;
72 	SetHighColor(color);
73 
74 	SetDrawingMode(B_OP_ALPHA);
75 	FillRect(rect, B_SOLID_HIGH);
76 }
77 
78 
79 BSize
80 RatingView::MinSize()
81 {
82 	BSize size(16 * 5 + 2 * 4, 16 + 2);
83 	return BLayoutUtils::ComposeSize(ExplicitMinSize(), size);
84 }
85 
86 
87 BSize
88 RatingView::PreferredSize()
89 {
90 	return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), MinSize());
91 }
92 
93 
94 BSize
95 RatingView::MaxSize()
96 {
97 	return BLayoutUtils::ComposeSize(ExplicitMaxSize(), MinSize());
98 }
99 
100 
101 void
102 RatingView::SetRating(float rating)
103 {
104 	fRating = rating;
105 	Invalidate();
106 }
107 
108 
109 float
110 RatingView::Rating() const
111 {
112 	return fRating;
113 }
114 
115