repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / haikudepot / ui_generic / RatingView.cpp
blobbd733103c073b1212a96d4f0a089828900e93ab0
1 /*
2 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
7 #include "RatingView.h"
9 #include <stdio.h>
11 #include <LayoutUtils.h>
14 RatingView::RatingView(const char* name)
16 BView(name, B_WILL_DRAW),
17 fStarBitmap(501),
18 fRating(-1.0f)
20 SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
21 SetLowUIColor(ViewUIColor());
25 RatingView::~RatingView()
30 void
31 RatingView::AttachedToWindow()
33 AdoptParentColors();
37 void
38 RatingView::Draw(BRect updateRect)
40 FillRect(updateRect, B_SOLID_LOW);
42 if (fRating < 0.0f)
43 return;
45 const BBitmap* star = fStarBitmap.Bitmap(SharedBitmap::SIZE_16);
46 if (star == NULL) {
47 fprintf(stderr, "No star icon found in application resources.\n");
48 return;
51 SetDrawingMode(B_OP_OVER);
53 float x = 0;
54 for (int i = 0; i < 5; i++) {
55 DrawBitmap(star, BPoint(x, 0));
56 x += 16 + 2;
59 if (fRating >= 5.0f)
60 return;
62 SetDrawingMode(B_OP_OVER);
64 BRect rect(Bounds());
65 rect.right = x - 2;
66 rect.left = ceilf(rect.left + (fRating / 5.0f) * rect.Width());
68 rgb_color color = LowColor();
69 color.alpha = 190;
70 SetHighColor(color);
72 SetDrawingMode(B_OP_ALPHA);
73 FillRect(rect, B_SOLID_HIGH);
77 BSize
78 RatingView::MinSize()
80 BSize size(16 * 5 + 2 * 4, 16 + 2);
81 return BLayoutUtils::ComposeSize(ExplicitMinSize(), size);
85 BSize
86 RatingView::PreferredSize()
88 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), MinSize());
92 BSize
93 RatingView::MaxSize()
95 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), MinSize());
99 void
100 RatingView::SetRating(float rating)
102 fRating = rating;
103 Invalidate();
107 float
108 RatingView::Rating() const
110 return fRating;