~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/globalsearch/tooltipresultwidget.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2010, David Sansome <me@davidsansome.com>
 
3
 
 
4
   Clementine is free software: you can redistribute it and/or modify
 
5
   it under the terms of the GNU General Public License as published by
 
6
   the Free Software Foundation, either version 3 of the License, or
 
7
   (at your option) any later version.
 
8
 
 
9
   Clementine is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
 
16
*/
 
17
 
 
18
#include "tooltipresultwidget.h"
 
19
#include "core/logging.h"
 
20
 
 
21
#include <QPainter>
 
22
 
 
23
const int TooltipResultWidget::kBorder = 15;
 
24
const int TooltipResultWidget::kSpacing = 3;
 
25
const int TooltipResultWidget::kTrackNumSpacing = 6;
 
26
const int TooltipResultWidget::kLineHeight = 1;
 
27
const int TooltipResultWidget::kIconSize = 16;
 
28
 
 
29
TooltipResultWidget::TooltipResultWidget(const SearchProvider::Result& result,
 
30
                                         QWidget* parent)
 
31
  : QAbstractButton(parent),
 
32
    result_(result),
 
33
    kTextHeight(fontMetrics().height()),
 
34
    kTrackNoWidth(fontMetrics().width("0000")),
 
35
    bold_metrics_(fontMetrics())
 
36
{
 
37
  bold_font_ = font();
 
38
  bold_font_.setBold(true);
 
39
  bold_metrics_ = QFontMetrics(bold_font_);
 
40
 
 
41
  size_hint_ = CalculateSizeHint();
 
42
 
 
43
  setCheckable(true);
 
44
  setAutoExclusive(true);
 
45
}
 
46
 
 
47
QSize TooltipResultWidget::sizeHint() const {
 
48
  return size_hint_;
 
49
}
 
50
 
 
51
QSize TooltipResultWidget::CalculateSizeHint() const {
 
52
  int w = 0;
 
53
  int h = 0;
 
54
 
 
55
  // Title text
 
56
  h += kSpacing + kIconSize + kSpacing + kLineHeight;
 
57
  w = qMax(w, kBorder + kTrackNoWidth + kTrackNumSpacing +
 
58
              bold_metrics_.width(TitleText()) + kBorder);
 
59
 
 
60
  switch (result_.type_) {
 
61
  case globalsearch::Type_Track:
 
62
  case globalsearch::Type_Stream:
 
63
    break;
 
64
 
 
65
  case globalsearch::Type_Album:
 
66
    if (result_.album_songs_.isEmpty())
 
67
      break;
 
68
 
 
69
    // Song list
 
70
    h += kSpacing + kSpacing * (result_.album_songs_.count() - 1) +
 
71
         kTextHeight * result_.album_songs_.count();
 
72
    foreach (const Song& song, result_.album_songs_) {
 
73
      w = qMax(w, kBorder + kTrackNoWidth + kTrackNumSpacing +
 
74
                  fontMetrics().width(song.TitleWithCompilationArtist()) +
 
75
                  kBorder);
 
76
    }
 
77
 
 
78
    h += kSpacing + kLineHeight;
 
79
 
 
80
    break;
 
81
  }
 
82
 
 
83
  return QSize(w, h);
 
84
}
 
85
 
 
86
QString TooltipResultWidget::TitleText() const {
 
87
  return result_.provider_->name();
 
88
}
 
89
 
 
90
void TooltipResultWidget::paintEvent(QPaintEvent*) {
 
91
  QPainter p(this);
 
92
 
 
93
  const QColor text_color = palette().color(QPalette::BrightText);
 
94
 
 
95
  const qreal line_opacity  = 0.1 + (isChecked() ? 0.2 : 0.0);
 
96
  const qreal track_opacity = 0.1 + (isChecked() ? 0.5 : 0.0);
 
97
  const qreal text_opacity  = 0.4 + (isChecked() ? 0.5 : 0.0);
 
98
 
 
99
  int y = kSpacing;
 
100
 
 
101
  // Title text
 
102
  QRect text_rect(kBorder + kTrackNoWidth + kTrackNumSpacing, y,
 
103
                  width() - kBorder*2 - kTrackNoWidth - kTrackNumSpacing, kIconSize);
 
104
  p.setFont(bold_font_);
 
105
  p.setPen(text_color);
 
106
  p.setOpacity(text_opacity);
 
107
  p.drawText(text_rect, Qt::AlignVCenter, TitleText());
 
108
 
 
109
  // Title icon
 
110
  QRect icon_rect(text_rect.left() - kTrackNumSpacing - kIconSize, y,
 
111
                  kIconSize, kIconSize);
 
112
  p.drawPixmap(icon_rect, result_.provider_->icon().pixmap(kIconSize));
 
113
 
 
114
  // Line
 
115
  y += kIconSize + kSpacing;
 
116
  p.setOpacity(line_opacity);
 
117
  p.setPen(text_color);
 
118
  p.drawLine(0, y, width(), y);
 
119
  y += kLineHeight;
 
120
 
 
121
  switch (result_.type_) {
 
122
  case globalsearch::Type_Track:
 
123
  case globalsearch::Type_Stream:
 
124
    break;
 
125
 
 
126
  case globalsearch::Type_Album:
 
127
    if (result_.album_songs_.isEmpty())
 
128
      break;
 
129
 
 
130
    // Song list
 
131
    y += kSpacing;
 
132
 
 
133
    p.setFont(font());
 
134
 
 
135
    foreach (const Song& song, result_.album_songs_) {
 
136
      QRect number_rect(kBorder, y, kTrackNoWidth, kTextHeight);
 
137
      if (song.track() > 0) {
 
138
        // Track number
 
139
        p.setOpacity(track_opacity);
 
140
        p.drawText(number_rect, Qt::AlignRight | Qt::AlignHCenter,
 
141
                   QString::number(song.track()));
 
142
      }
 
143
 
 
144
      // Song title
 
145
      QRect title_rect(number_rect.right() + kTrackNumSpacing, y,
 
146
                       width() - number_rect.right() - kTrackNumSpacing - kBorder,
 
147
                       kTextHeight);
 
148
      p.setOpacity(text_opacity);
 
149
      p.drawText(title_rect, song.TitleWithCompilationArtist());
 
150
 
 
151
      y += kTextHeight + kSpacing;
 
152
    }
 
153
 
 
154
    // Line
 
155
    p.setOpacity(line_opacity);
 
156
    p.drawLine(0, y, width(), y);
 
157
    y += kLineHeight;
 
158
 
 
159
    break;
 
160
  }
 
161
 
 
162
  y += kSpacing;
 
163
}