Skip to content

Commit

Permalink
HaikuDepot: Implemented showing the rating in the Rating column
Browse files Browse the repository at this point in the history
  • Loading branch information
stippi committed Aug 7, 2013
1 parent c887e3a commit 82a33bb
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/apps/haiku-depot/PackageInfoView.cpp
Expand Up @@ -553,6 +553,12 @@ class AboutView : public BView {
GetFont(&smallFont);
smallFont.SetSize(std::max(9.0f, ceilf(smallFont.Size() * 0.85f)));

// TODO: Clicking the screen shot view should open ShowImage with the
// the screen shot. This could be done by writing the screen shot to
// a temporary folder, launching ShowImage to display it, and writing
// all other screenshots associated with the package to the same folder
// so the user can use the ShowImage navigation to view the other
// screenshots.
fScreenshotView = new BitmapView("screenshot view");
fScreenshotView->SetExplicitMinSize(BSize(64.0f, 64.0f));
fScreenshotView->SetExplicitMaxSize(
Expand Down
135 changes: 129 additions & 6 deletions src/apps/haiku-depot/PackageListView.cpp
Expand Up @@ -35,6 +35,19 @@ class BBitmapStringField : public BStringField {
};


class RatingField : public BField {
public:
RatingField(float rating);
virtual ~RatingField();

void SetRating(float rating);
float Rating() const
{ return fRating; }
private:
float fRating;
};


// BColumn for PackageListView which knows how to render
// a BBitmapStringField
// TODO: Code-duplication with DriveSetup PartitionList.h
Expand All @@ -48,6 +61,7 @@ class PackageColumn : public BTitledColumn {

virtual void DrawField(BField* field, BRect rect,
BView* parent);
virtual int CompareFields(BField* field1, BField* field2);
virtual float GetPreferredWidth(BField* field,
BView* parent) const;

Expand Down Expand Up @@ -101,6 +115,38 @@ BBitmapStringField::SetBitmap(const BBitmap* bitmap)
}


// #pragma mark - RatingField


RatingField::RatingField(float rating)
:
fRating(0.0f)
{
SetRating(rating);
}


RatingField::~RatingField()
{
}


void
RatingField::SetRating(float rating)
{
if (rating < 0.0f)
rating = 0.0f;
if (rating > 5.0f)
rating = 5.0f;

if (rating == fRating)
return;

fRating = rating;
// TODO: cause a redraw?
}


// #pragma mark - PackageColumn


Expand All @@ -126,8 +172,9 @@ PackageColumn::DrawField(BField* field, BRect rect, BView* parent)
BBitmapStringField* bitmapField
= dynamic_cast<BBitmapStringField*>(field);
BStringField* stringField = dynamic_cast<BStringField*>(field);
RatingField* ratingField = dynamic_cast<RatingField*>(field);

if (bitmapField) {
if (bitmapField != NULL) {
const BBitmap* bitmap = bitmapField->Bitmap();

// figure out the placement
Expand Down Expand Up @@ -160,7 +207,7 @@ PackageColumn::DrawField(BField* field, BRect rect, BView* parent)
}

// draw the bitmap
if (bitmap) {
if (bitmap != NULL) {
parent->SetDrawingMode(B_OP_ALPHA);
parent->DrawBitmap(bitmap, BPoint(x, y));
parent->SetDrawingMode(B_OP_OVER);
Expand All @@ -169,7 +216,7 @@ PackageColumn::DrawField(BField* field, BRect rect, BView* parent)
// draw the string
DrawString(bitmapField->ClippedString(), parent, r);

} else if (stringField) {
} else if (stringField != NULL) {

float width = rect.Width() - (2 * sTextMargin);

Expand All @@ -182,10 +229,85 @@ PackageColumn::DrawField(BField* field, BRect rect, BView* parent)
}

DrawString(stringField->ClippedString(), parent, rect);

} else if (ratingField != NULL) {

const float kDefaultTextMargin = 8;

float width = rect.Width() - (2 * kDefaultTextMargin);

BString string = "★★★★★";
float stringWidth = parent->StringWidth(string);
bool drawOverlay = true;

if (width < stringWidth) {
string.SetToFormat("%.1f", ratingField->Rating());
drawOverlay = false;
stringWidth = parent->StringWidth(string);
}

switch (Alignment()) {
default:
case B_ALIGN_LEFT:
rect.left += kDefaultTextMargin;
break;
case B_ALIGN_CENTER:
rect.left = rect.left + (width - stringWidth) / 2.0f;
break;

case B_ALIGN_RIGHT:
rect.left = rect.right - (stringWidth + kDefaultTextMargin);
break;
}

rect.left = floorf(rect.left);
rect.right = rect.left + stringWidth;

if (drawOverlay)
parent->SetHighColor(0, 170, 255);

font_height fontHeight;
parent->GetFontHeight(&fontHeight);
float y = rect.top + (rect.Height()
- (fontHeight.ascent + fontHeight.descent)) / 2
+ fontHeight.ascent;

parent->DrawString(string, BPoint(rect.left, y));

if (drawOverlay) {
rect.left = ceilf(rect.left
+ (ratingField->Rating() / 5.0f) * rect.Width());

rgb_color color = parent->LowColor();
color.alpha = 190;
parent->SetHighColor(color);

parent->SetDrawingMode(B_OP_ALPHA);
parent->FillRect(rect, B_SOLID_HIGH);

}
}
}


int
PackageColumn::CompareFields(BField* field1, BField* field2)
{
RatingField* ratingField1 = dynamic_cast<RatingField*>(field1);
RatingField* ratingField2 = dynamic_cast<RatingField*>(field2);
if (ratingField1 == NULL || ratingField2 == NULL)
return Inherited::CompareFields(field1, field2);

if (ratingField1->Rating() > ratingField2->Rating())
return -1;
else if (ratingField1->Rating() < ratingField2->Rating())
return 1;

return 0;
}



float
PackageColumn::GetPreferredWidth(BField *_field, BView* parent) const
{
Expand Down Expand Up @@ -217,7 +339,8 @@ PackageColumn::GetPreferredWidth(BField *_field, BView* parent) const
bool
PackageColumn::AcceptsField(const BField* field) const
{
return dynamic_cast<const BStringField*>(field) != NULL;
return dynamic_cast<const BStringField*>(field) != NULL
|| dynamic_cast<const RatingField*>(field) != NULL;
}


Expand Down Expand Up @@ -255,8 +378,8 @@ PackageRow::PackageRow(const PackageInfo& package)
SetField(new BBitmapStringField(icon, package.Title()), kTitleColumn);

// Rating
// TODO: Method to compute and cache rating
SetField(new BStringField("n/a"), kRatingColumn);
RatingSummary summary = package.CalculateRatingSummary();
SetField(new RatingField(summary.averageRating), kRatingColumn);

// Description
SetField(new BStringField(package.ShortDescription()), kDescriptionColumn);
Expand Down

0 comments on commit 82a33bb

Please sign in to comment.