TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkitemdelegate.cpp
Go to the documentation of this file.
1 #include "ttkitemdelegate.h"
2 
3 #include <QLabel>
4 #include <QPainter>
5 #include <QCheckBox>
6 #include <QPushButton>
7 #include <QProgressBar>
8 
10  : QItemDelegate(parent),
11  m_mode(TTKAbstractItemDelegate::Null),
12  m_container(nullptr)
13 {
14 
15 }
16 
17 void TTKAbstractItemDelegate::setStyleSheet(const QString &style) const
18 {
19  if(m_container)
20  {
21  m_container->setStyleSheet(style);
22  }
23 }
24 
25 QSize TTKAbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &) const
26 {
27  QSize size = option.rect.size();
28  size.setHeight(25);
29  return size;
30 }
31 
32 void TTKAbstractItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
33 {
34 #if !TTK_QT_VERSION_CHECK(5,7,0)
35  QItemDelegate::paint(painter, option, index);
36 #else
37  drawBackground(painter, option, index);
38 #endif
39 }
40 
41 
42 
44  : TTKAbstractItemDelegate(parent),
45  m_background(false)
46 {
47  m_checkBox = new QCheckBox;
48 #ifdef Q_OS_UNIX
49  m_checkBox->setFocusPolicy(Qt::NoFocus);
50 #endif
52 }
53 
55 {
56  delete m_checkBox;
57 }
58 
59 void TTKCheckBoxItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
60 {
61  TTKAbstractItemDelegate::paint(painter, option, index);
62 
63  if((m_mode & TTKAbstractItemDelegate::TreeMode) && !index.parent().isValid())
64  {
65  drawDisplay(painter, option, option.rect, index.data(Qt::DisplayRole).toString());
66  return;
67  }
68 
69  painter->save();
70  const bool displayMode = m_mode & TTKAbstractItemDelegate::DisplayMode;
71  if(displayMode)
72  {
73  m_checkBox->resize(option.rect.size());
74  m_checkBox->setText(index.data(TTK_DISPLAY_ROLE).toString());
75  m_checkBox->setEnabled(index.data(TTK_ENABLED_ROLE).toBool());
76  }
77  else
78  {
79  const int minSize = qMin(option.rect.width(), option.rect.height());
80  m_checkBox->resize(minSize, minSize);
81  }
82 
83  const bool state = m_checkBox->isChecked();
84  m_checkBox->setChecked(TTKStaticCast(Qt::CheckState, index.data(TTK_CHECKED_ROLE).toInt()) == Qt::Checked);
85  if(m_checkBox->isEnabled() && state != m_checkBox->isChecked())
86  {
87  Q_EMIT TTKConstCast(TTKCheckBoxItemDelegate*, this)->buttonChecked();
88  }
89  painter->translate(displayMode ? 0 : (option.rect.width() - 16) / 2, 0);
90 
91  m_checkBox->render(painter, option.rect.topLeft(), QRegion(), QWidget::DrawChildren);
92  painter->restore();
93 }
94 
95 
96 
98  : TTKAbstractItemDelegate(parent)
99 {
100  m_progress = new QProgressBar;
102 }
103 
105 {
106  delete m_progress;
107 }
108 
109 void TTKProgressBarItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
110 {
111  TTKAbstractItemDelegate::paint(painter, option, index);
112 
113  if((m_mode & TTKAbstractItemDelegate::TreeMode) && !index.parent().isValid())
114  {
115  return;
116  }
117 
118  painter->save();
119  m_progress->resize(option.rect.width() - 21, option.rect.height() - 17);
120  m_progress->setValue(index.data(TTK_PROGRESS_ROLE).toInt());
121  painter->translate(10, 10);
122 
123  m_progress->render(painter, option.rect.topLeft(), QRegion(), QWidget::DrawChildren);
124  painter->restore();
125 }
126 
127 
128 
130  : TTKAbstractItemDelegate(parent)
131 {
132  m_label = new QLabel;
133  m_label->setAlignment(Qt::AlignCenter);
135 }
136 
138 {
139  delete m_label;
140 }
141 
142 void TTKLabelItemDelegate::setAlignment(Qt::Alignment alignment) const
143 {
144  m_label->setAlignment(alignment);
145 }
146 
147 void TTKLabelItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
148 {
149  TTKAbstractItemDelegate::paint(painter, option, index);
150 
151  if((m_mode & TTKAbstractItemDelegate::TreeMode) && !index.parent().isValid())
152  {
153  drawDisplay(painter, option, option.rect, index.data(Qt::DisplayRole).toString());
154  return;
155  }
156 
157  painter->save();
158  const QColor &color = index.data(Qt::ForegroundRole).value<QColor>();
159  if(color.isValid())
160  {
161  QPalette plt(m_label->palette());
162  plt.setColor(QPalette::WindowText, color);
163  m_label->setPalette(plt);
164  }
165 
166  const QString &text = index.data(TTK_DISPLAY_ROLE).toString();
167  const QFontMetrics ftm(m_label->font());
168  m_label->setText(m_mode & TTKAbstractItemDelegate::ElideMode ? ftm.elidedText(text, Qt::ElideRight, option.rect.width() - 5) : text);
169  m_label->resize(option.rect.size());
170  painter->translate(0, 0);
171 
172  m_label->render(painter, option.rect.topLeft(), QRegion(), QWidget::DrawChildren);
173  painter->restore();
174 }
175 
176 
177 
179  : TTKAbstractItemDelegate(parent)
180 {
181  m_pushButton = new QPushButton;
182  m_pushButton->setCursor(QCursor(Qt::PointingHandCursor));
183 #ifdef Q_OS_UNIX
184  m_pushButton->setFocusPolicy(Qt::NoFocus);
185 #endif
187 }
188 
190 {
191  delete m_pushButton;
192 }
193 
194 void TTKPushButtonItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
195 {
196  TTKAbstractItemDelegate::paint(painter, option, index);
197 
198  if((m_mode & TTKAbstractItemDelegate::TreeMode) && !index.parent().isValid())
199  {
200  drawDisplay(painter, option, option.rect, index.data(Qt::DisplayRole).toString());
201  return;
202  }
203 
204  painter->save();
205  m_pushButton->setText(index.data(TTK_DISPLAY_ROLE).toString());
206  m_pushButton->resize(option.rect.size() - QSize(10, 10));
207  painter->translate(5, 5);
208 
209  m_pushButton->render(painter, option.rect.topLeft(), QRegion(), QWidget::DrawChildren);
210  painter->restore();
211 }
#define TTKStaticCast(x, y)
Definition: ttkglobal.h:159
#define TTKConstCast(x, y)
Definition: ttkglobal.h:141
TTKLabelItemDelegate(QObject *parent=nullptr)
#define TTK_DISPLAY_ROLE
#define TTK_ENABLED_ROLE
#define TTK_PROGRESS_ROLE
voidpf void uLong size
Definition: ioapi.h:136
void setStyleSheet(const QString &style) const
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const overridefinal
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const overridefinal
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &) const overridefinal
void setAlignment(Qt::Alignment alignment) const
TTKCheckBoxItemDelegate(QObject *parent=nullptr)
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const overridefinal
TTKPushButtonItemDelegate(QObject *parent=nullptr)
TTKAbstractItemDelegate(QObject *parent=nullptr)
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
The class of the abstract item delegate.
TTKProgressBarItemDelegate(QObject *parent=nullptr)
The class of the checkbox item delegate.
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const overridefinal
#define TTK_CHECKED_ROLE
state
Definition: http_parser.c:279