Parent: [85ffd4] (diff)

Child: [707811] (diff)

Download this file

PlaylistItemDelegate.cpp    167 lines (122 with data), 4.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* PlaylistItemDelegate.cpp */
/* Copyright (C) 2012 Lucio Carreras
*
* This file is part of sayonara player
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QObject>
#include <QLabel>
#include <QListView>
#include <QPainter>
#include "GUI/playlist/delegate/PlaylistItemDelegate.h"
#include "GUI/playlist/entry/GUI_PlaylistEntryBig.h"
#include "GUI/playlist/entry/GUI_PlaylistEntrySmall.h"
static QString get_fg_color(int val_bg){
if(val_bg > 160)
return QString(" color: #202020; ");
else
return QString(" color: #D8D8D8 ");
}
PlaylistItemDelegate::PlaylistItemDelegate(QListView* parent, bool compact){
if(compact){
_row_height = 20;
_pl_entry = new GUI_PlaylistEntrySmall();
}
else{
_row_height = 31;
_pl_entry = new GUI_PlaylistEntryBig();
}
_parent = parent;
_rendered_items = 0;
}
PlaylistItemDelegate::~PlaylistItemDelegate(){
delete _pl_entry;
_row_height = 0;
}
void PlaylistItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const {
if(!index.isValid()) return;
QRect rect(option.rect);
_pl_entry->setMaximumSize(_max_width, _row_height);
_pl_entry->setMinimumSize(_max_width, _row_height);
_pl_entry->resize(_max_width, _row_height);
QVariant mdVariant = index.model()->data(index, Qt::WhatsThisRole);
MetaData md;
if( !MetaData::fromVariant(mdVariant, md) ) return;
_pl_entry->setContent(md, index.row() +1 );
QString style;
QPalette palette = _parent->palette();
QColor col_background = palette.color(QPalette::Active, QPalette::Background);
QColor col_highlight = palette.color(QPalette::Active, QPalette::Highlight);
QColor col_highlight_lighter = col_highlight.darker(140);
int highlight_val = col_highlight.lightness();
int playing_val = col_highlight_lighter.lightness();
int background_val = col_background.lightness();
if(md.pl_playing){
style = QString("background-color: ") +
col_highlight_lighter.name() + "; " +
get_fg_color(playing_val);
}
else if(md.is_disabled){
style = QString("color: #A0A0A0; background-color: transparent;");
}
else if(!md.pl_selected){
style = QString("background-color: transparent; ") +
get_fg_color(background_val);
}
// standard selected
else{
style = QString("background-color: ") +
col_highlight.name() + ";" +
get_fg_color(highlight_val);
}
int y = rect.topLeft().y() + _pl_entry->height() -1;
_pl_entry->setStyleSheet(style);
if(md.is_disabled) _pl_entry->setDisabled(true);
painter->save();
painter->translate(0, 0);
_pl_entry->render(painter, rect.topLeft() );
if(md.pl_dragged) {
painter->drawLine(QLine(0, y, _max_width, y));
}
painter->restore();
}
QSize PlaylistItemDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
return QSize(_max_width, _row_height);
}
void PlaylistItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
Q_UNUSED(editor);
Q_UNUSED(index);
}
void PlaylistItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
Q_UNUSED(editor);
Q_UNUSED(index);
Q_UNUSED(model);
}
QWidget* PlaylistItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(parent);
Q_UNUSED(option);
Q_UNUSED(index);
return 0;
}
void PlaylistItemDelegate::setMaxWidth(int w){
_max_width = w;
}
int PlaylistItemDelegate::rowHeight(){
return _row_height;
}