xref: /haiku/src/apps/haikudepot/ui_generic/RatingView.cpp (revision 1a76488fc88584bf66b9751d7fb9b6527ac20d87)
1 /*
2  * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2018-2020, 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 #include "RatingUtils.h"
16 
17 
18 RatingView::RatingView(const char* name)
19 	:
20 	BView(name, B_WILL_DRAW),
21 	fStarBlueBitmap(new SharedBitmap(RSRC_STAR_BLUE)),
22 	fStarGrayBitmap(new SharedBitmap(RSRC_STAR_GREY)),
23 	fRating(RATING_MISSING)
24 {
25 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
26 	SetLowUIColor(ViewUIColor());
27 }
28 
29 
30 RatingView::~RatingView()
31 {
32 }
33 
34 
35 void
36 RatingView::AttachedToWindow()
37 {
38 	AdoptParentColors();
39 }
40 
41 /*! This method will return a star image that can be used repeatedly in the
42     user interface in order to signify the rating given by a user.  It could
43     be grey if no rating is assigned.
44 */
45 
46 const BBitmap*
47 RatingView::StarBitmap()
48 {
49 	if (fRating < RATING_MIN)
50 		return fStarGrayBitmap->Bitmap(BITMAP_SIZE_16);
51 	return fStarBlueBitmap->Bitmap(BITMAP_SIZE_16);
52 }
53 
54 
55 void
56 RatingView::Draw(BRect updateRect)
57 {
58 	RatingUtils::Draw(this, BPoint(0, 0), fRating, StarBitmap());
59 }
60 
61 
62 BSize
63 RatingView::MinSize()
64 {
65 	BSize size(16 * 5 + 2 * 4, 16 + 2);
66 	return BLayoutUtils::ComposeSize(ExplicitMinSize(), size);
67 }
68 
69 
70 BSize
71 RatingView::PreferredSize()
72 {
73 	return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), MinSize());
74 }
75 
76 
77 BSize
78 RatingView::MaxSize()
79 {
80 	return BLayoutUtils::ComposeSize(ExplicitMaxSize(), MinSize());
81 }
82 
83 
84 void
85 RatingView::SetRating(float rating)
86 {
87 	fRating = rating;
88 	Invalidate();
89 }
90 
91 
92 float
93 RatingView::Rating() const
94 {
95 	return fRating;
96 }
97 
98