1/*
2 * Copyright 2013-2014, Stephan A��mus <superstippi@gmx.de>.
3 * Copyright 2018, Andrew Lindesay <apl@lindesay.co.nz>.
4 * All rights reserved. Distributed under the terms of the MIT License.
5 */
6
7
8#include "RatingView.h"
9
10#include <stdio.h>
11
12#include <LayoutUtils.h>
13
14#include "HaikuDepotConstants.h"
15
16
17RatingView::RatingView(const char* name)
18	:
19	BView(name, B_WILL_DRAW),
20	fStarBlueBitmap(RSRC_STAR_BLUE),
21	fStarGrayBitmap(RSRC_STAR_GREY),
22	fRating(RATING_MISSING)
23{
24	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
25	SetLowUIColor(ViewUIColor());
26}
27
28
29RatingView::~RatingView()
30{
31}
32
33
34void
35RatingView::AttachedToWindow()
36{
37	AdoptParentColors();
38}
39
40/*! This method will return a star image that can be used repeatedly in the
41    user interface in order to signify the rating given by a user.  It could
42    be grey if no rating is assigned.
43*/
44
45const BBitmap*
46RatingView::StarBitmap()
47{
48	if (fRating < RATING_MIN)
49		return fStarGrayBitmap.Bitmap(SharedBitmap::SIZE_16);
50	return fStarBlueBitmap.Bitmap(SharedBitmap::SIZE_16);
51}
52
53
54void
55RatingView::Draw(BRect updateRect)
56{
57	FillRect(updateRect, B_SOLID_LOW);
58	const BBitmap* star = StarBitmap();
59
60	if (star == NULL) {
61		fprintf(stderr, "No star icon found in application resources.\n");
62		return;
63	}
64
65	SetDrawingMode(B_OP_OVER);
66
67	float x = 0;
68	for (int i = 0; i < 5; i++) {
69		DrawBitmap(star, BPoint(x, 0));
70		x += 16 + 2;
71	}
72
73	if (fRating >= RATING_MIN && fRating < 5.0f) {
74		SetDrawingMode(B_OP_OVER);
75
76		BRect rect(Bounds());
77		rect.right = x - 2;
78		rect.left = ceilf(rect.left + (fRating / 5.0f) * rect.Width());
79
80		rgb_color color = LowColor();
81		color.alpha = 190;
82		SetHighColor(color);
83
84		SetDrawingMode(B_OP_ALPHA);
85		FillRect(rect, B_SOLID_HIGH);
86	}
87}
88
89
90BSize
91RatingView::MinSize()
92{
93	BSize size(16 * 5 + 2 * 4, 16 + 2);
94	return BLayoutUtils::ComposeSize(ExplicitMinSize(), size);
95}
96
97
98BSize
99RatingView::PreferredSize()
100{
101	return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), MinSize());
102}
103
104
105BSize
106RatingView::MaxSize()
107{
108	return BLayoutUtils::ComposeSize(ExplicitMaxSize(), MinSize());
109}
110
111
112void
113RatingView::SetRating(float rating)
114{
115	fRating = rating;
116	Invalidate();
117}
118
119
120float
121RatingView::Rating() const
122{
123	return fRating;
124}
125
126