1 /* 2 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>. 3 * Copyright 2018-2024, 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 #include "SharedIcons.h" 17 18 19 RatingView::RatingView(const char* name) 20 : 21 BView(name, B_WILL_DRAW), 22 fRating(RATING_MISSING) 23 { 24 SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 25 SetLowUIColor(ViewUIColor()); 26 } 27 28 29 RatingView::~RatingView() 30 { 31 } 32 33 34 void 35 RatingView::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 45 const BBitmap* 46 RatingView::StarBitmap() 47 { 48 if (fRating < RATING_MIN) 49 return SharedIcons::IconStarGrey16Scaled()->Bitmap(); 50 return SharedIcons::IconStarBlue16Scaled()->Bitmap(); 51 } 52 53 54 void 55 RatingView::Draw(BRect updateRect) 56 { 57 RatingUtils::Draw(this, BPoint(0, 0), fRating, StarBitmap()); 58 } 59 60 61 BSize 62 RatingView::MinSize() 63 { 64 BSize size(16 * 5 + 2 * 4, 16 + 2); 65 return BLayoutUtils::ComposeSize(ExplicitMinSize(), size); 66 } 67 68 69 BSize 70 RatingView::PreferredSize() 71 { 72 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), MinSize()); 73 } 74 75 76 BSize 77 RatingView::MaxSize() 78 { 79 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), MinSize()); 80 } 81 82 83 void 84 RatingView::SetRating(float rating) 85 { 86 fRating = rating; 87 Invalidate(); 88 } 89 90 91 float 92 RatingView::Rating() const 93 { 94 return fRating; 95 } 96 97