TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicwidgetutils.cpp
Go to the documentation of this file.
1 #include "musicwidgetutils.h"
2 #include "musicwidgetheaders.h"
3 #include "musicimageutils.h"
4 #include "ttkdesktopwrapper.h"
5 
6 #include <QBitmap>
7 #include <QScrollArea>
8 #include <QStyledItemDelegate>
9 
10 void TTK::Widget::setFontSize(QWidget *widget, int size)
11 {
12  QFont font = widget->font();
13  font.setPointSize(size);
14  widget->setFont(font);
15 }
16 
17 void TTK::Widget::setFontStyle(QWidget *widget, TTK::FontStyleMode type)
18 {
19  QFont font = widget->font();
20  switch(type)
21  {
22  case TTK::FontStyleMode::Bold: font.setBold(true); break;
23  case TTK::FontStyleMode::Italic: font.setItalic(true); break;
24  case TTK::FontStyleMode::Underline: font.setUnderline(true); break;
25  case TTK::FontStyleMode::Overline: font.setOverline(true); break;
26  case TTK::FontStyleMode::StrikeOut: font.setStrikeOut(true); break;
27  case TTK::FontStyleMode::FixedPitch: font.setFixedPitch(true); break;
28  case TTK::FontStyleMode::Kerningt: font.setKerning(true); break;
29  default: break;
30  }
31  widget->setFont(font);
32 }
33 
34 QString TTK::Widget::elidedText(const QFont &font, const QString &text, Qt::TextElideMode mode, int width)
35 {
36  const QFontMetrics ftm(font);
37  return ftm.elidedText(text, mode, width);
38 }
39 
40 QString TTK::Widget::elidedTitleText(const QFont &font, const QString &text, int width)
41 {
42  const int index = text.lastIndexOf("[");
43  const QString &prefix = text.left(index);
44  const QString &suffix = text.right(text.length() - index);
45  const int offset = TTK::Widget::fontTextWidth(font, suffix);
46  return TTK::Widget::elidedText(font, prefix, Qt::ElideRight, width - offset) + suffix;
47 }
48 
49 int TTK::Widget::fontTextWidth(const QFont &font, const QString &text)
50 {
51  const QFontMetrics ftm(font);
52  return QtFontWidth(ftm, text);
53 }
54 
55 int TTK::Widget::fontTextHeight(const QFont &font)
56 {
57  const QFontMetrics ftm(font);
58  return ftm.height();
59 }
60 
62 {
63 #if TTK_QT_VERSION_CHECK(5,12,0)
64  const QList<QAction*> actions(menu->actions());
65  if(!actions.empty())
66  {
67  QAction* action(actions.front());
68  if(action->icon().isNull())
69  {
70  action->setIcon(QIcon(":/tiny/lb_transparent"));
71  }
72  }
73 #else
74  Q_UNUSED(menu);
75 #endif
76 }
77 
78 void TTK::Widget::adjustWidgetPosition(QWidget *widget)
79 {
80  const QRect &rect = TTKDesktopWrapper::screenGeometry();
81  widget->move((rect.width() - widget->width()) / 2, (rect.height() - widget->height()) / 2);
82 }
83 
84 void TTK::Widget::setTransparent(QWidget *widget, int alpha)
85 {
86  QPalette plt(widget->palette());
87  plt.setBrush(QPalette::Base, QBrush(QColor(255, 255, 255, alpha)));
88  widget->setPalette(plt);
89 }
90 
91 void TTK::Widget::setTransparent(QWidget *widget, const QColor &color)
92 {
93  QPalette plt(widget->palette());
94  plt.setBrush(QPalette::Highlight, QBrush(color));
95  widget->setPalette(plt);
96 }
97 
98 void TTK::Widget::setComboBoxText(QComboBox *widget, const QString &text)
99 {
100  if(widget->isEditable())
101  {
102  widget->setEditText(text);
103  }
104  else
105  {
106  const int index = widget->findText(text);
107  if(index > -1)
108  {
109  widget->setCurrentIndex(index);
110  }
111  }
112 }
113 
114 void TTK::Widget::widgetToRound(QWidget *widget, int ratioX, int ratioY)
115 {
116  widget->setMask(TTK::Image::generateMask(widget->rect(), ratioX, ratioY));
117 }
118 
119 void TTK::Widget::generateComboBoxFormat(QComboBox *widget, const QString &style)
120 {
121  widget->setItemDelegate(new QStyledItemDelegate(widget));
122  widget->setStyleSheet(style.isEmpty() ? TTK::UI::ComboBoxStyle01 + TTK::UI::ItemView01 : style);
123  widget->view()->setStyleSheet(TTK::UI::ScrollBarStyle01);
124 }
125 
126 void TTK::Widget::generateVScrollAreaFormat(QWidget *widget, QWidget *parent, bool background)
127 {
128  QScrollArea *area = TTKObjectCast(QScrollArea*, widget);
129  if(area)
130  {
131  const QString &style = background ? TTK::UI::ScrollBarStyle01 : TTK::UI::ScrollBarStyle03;
132  area->setWidgetResizable(true);
133  area->setFrameShape(QFrame::NoFrame);
134  area->setFrameShadow(QFrame::Plain);
135  area->setAlignment(Qt::AlignLeft);
136  area->setWidget(parent);
137  area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
138  area->setStyleSheet(style);
139  area->verticalScrollBar()->setStyleSheet(style);
140  }
141 }
142 
143 void TTK::Widget::generateHScrollAreaFormat(QWidget *widget, QWidget *parent, bool background)
144 {
145  QScrollArea *area = TTKObjectCast(QScrollArea*, widget);
146  if(area)
147  {
148  const QString &style = background ? TTK::UI::ScrollBarStyle02 : TTK::UI::ScrollBarStyle04;
149  area->setWidgetResizable(true);
150  area->setFrameShape(QFrame::NoFrame);
151  area->setFrameShadow(QFrame::Plain);
152  area->setAlignment(Qt::AlignLeft);
153  area->setWidget(parent);
154  area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
155  area->setStyleSheet(style);
156  area->horizontalScrollBar()->setStyleSheet(style);
157  }
158 }
TTK_MODULE_EXPORT void generateHScrollAreaFormat(QWidget *widget, QWidget *parent, bool background=true)
voidpf void uLong size
Definition: ioapi.h:136
static const QString ScrollBarStyle04
static const QString ItemView01
ItemView.
TTK_MODULE_EXPORT void setComboBoxText(QComboBox *widget, const QString &text)
TTK_MODULE_EXPORT void setFontStyle(QWidget *widget, TTK::FontStyleMode type)
static const QString ScrollBarStyle03
TTK_MODULE_EXPORT void setFontSize(QWidget *widget, int size)
voidpf uLong offset
Definition: ioapi.h:142
static const QString ScrollBarStyle01
ScrollBar.
#define QtFontWidth(p, t)
Font string width.
Definition: ttkqtcompat.h:31
TTK_MODULE_EXPORT QString suffix(const QString &name)
TTK_MODULE_EXPORT QBitmap generateMask(const QRect &rect, int ratioX, int ratioY)
FontStyleMode
Definition: musicobject.h:180
TTK_MODULE_EXPORT void setTransparent(QWidget *widget, int alpha)
TTK_MODULE_EXPORT void adjustWidgetPosition(QWidget *widget)
static QRect screenGeometry(int index=0)
TTK_MODULE_EXPORT QString elidedText(const QFont &font, const QString &text, Qt::TextElideMode mode, int width)
TTK_MODULE_EXPORT void generateComboBoxFormat(QComboBox *widget, const QString &style={})
static const QString ScrollBarStyle02
TTK_MODULE_EXPORT void generateVScrollAreaFormat(QWidget *widget, QWidget *parent, bool background=true)
TTK_MODULE_EXPORT int fontTextWidth(const QFont &font, const QString &text)
TTK_MODULE_EXPORT QString elidedTitleText(const QFont &font, const QString &text, int width)
static unsigned char alpha[symbols+1]
Definition: rsecc.c:50
const char int mode
Definition: ioapi.h:135
TTK_MODULE_EXPORT void widgetToRound(QWidget *widget, int ratioX, int ratioY)
TTK_MODULE_EXPORT int fontTextHeight(const QFont &font)
static const QString ComboBoxStyle01
ComboBox.
TTK_MODULE_EXPORT void adjustMenuPosition(QMenu *menu)
#define TTKObjectCast(x, y)
Definition: ttkqtglobal.h:60