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 SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 21 SetLowUIColor(ViewUIColor()); 22 } 23 24 25 RatingView::~RatingView() 26 { 27 } 28 29 30 void 31 RatingView::AttachedToWindow() 32 { 33 AdoptParentColors(); 34 } 35 36 37 void 38 RatingView::Draw(BRect updateRect) 39 { 40 FillRect(updateRect, B_SOLID_LOW); 41 42 if (fRating < 0.0f) 43 return; 44 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; 49 } 50 51 SetDrawingMode(B_OP_OVER); 52 53 float x = 0; 54 for (int i = 0; i < 5; i++) { 55 DrawBitmap(star, BPoint(x, 0)); 56 x += 16 + 2; 57 } 58 59 if (fRating >= 5.0f) 60 return; 61 62 SetDrawingMode(B_OP_OVER); 63 64 BRect rect(Bounds()); 65 rect.right = x - 2; 66 rect.left = ceilf(rect.left + (fRating / 5.0f) * rect.Width()); 67 68 rgb_color color = LowColor(); 69 color.alpha = 190; 70 SetHighColor(color); 71 72 SetDrawingMode(B_OP_ALPHA); 73 FillRect(rect, B_SOLID_HIGH); 74 } 75 76 77 BSize 78 RatingView::MinSize() 79 { 80 BSize size(16 * 5 + 2 * 4, 16 + 2); 81 return BLayoutUtils::ComposeSize(ExplicitMinSize(), size); 82 } 83 84 85 BSize 86 RatingView::PreferredSize() 87 { 88 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), MinSize()); 89 } 90 91 92 BSize 93 RatingView::MaxSize() 94 { 95 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), MinSize()); 96 } 97 98 99 void 100 RatingView::SetRating(float rating) 101 { 102 fRating = rating; 103 Invalidate(); 104 } 105 106 107 float 108 RatingView::Rating() const 109 { 110 return fRating; 111 } 112 113