Skip to content

Commit e157c51

Browse files
committed
SubtitleDurationDelegate
1 parent 8edb08b commit e157c51

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/mainwindow.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,57 +35,59 @@
3535
#include <QThread>
3636
#include <QWidget>
3737

38-
3938
#include "configeditor.h"
4039
#include "mainwindow.h"
4140
#include "player.h"
4241
#include "shortcuteditor.h"
4342
#include "ui_mainwindow.h"
4443
#include "wizard.h"
4544

46-
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
4745
/**
4846
* A small delegate class to allow rich text rendering in main table cells.
4947
*/
5048
class SubtitleTextDelegate : public QStyledItemDelegate {
5149
void paint(QPainter *painter, const QStyleOptionViewItem &option,
52-
const QModelIndex &index) const {
50+
const QModelIndex &index) const override {
5351
QTextDocument document;
5452
QVariant value = index.data(Qt::DisplayRole);
53+
5554
// Draw background with cell style
56-
QStyleOptionViewItemV4 options = option;
57-
initStyleOption(&options, index);
58-
options.text = "";
59-
options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options,
60-
painter);
55+
QStyleOptionViewItem opt(option);
56+
initStyleOption(&opt, index);
57+
opt.text = QString(); // remove default text drawing
58+
59+
if (opt.widget)
60+
opt.widget->style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
61+
6162
// Render rich text
6263
if (value.isValid() && !value.isNull()) {
63-
QString html(value.toString());
64-
// Bold if selected
64+
QString html = value.toString();
6565
if (index.data(Qt::UserRole).toBool())
6666
html = QString("<b>%1</b>").arg(html);
6767
document.setHtml(html);
68+
painter->save();
6869
painter->translate(option.rect.topLeft());
6970
document.drawContents(painter);
70-
painter->translate(-option.rect.topLeft());
71+
painter->restore();
7172
}
72-
// Draw icon on the right
73-
if (!index.data(Qt::DecorationRole).isNull())
74-
options.widget->style()->drawItemPixmap(
73+
74+
if (!index.data(Qt::DecorationRole).isNull()) {
75+
QPixmap icon(index.data(Qt::DecorationRole).toString());
76+
opt.widget->style()->drawItemPixmap(
7577
painter, option.rect.translated(QPoint(-5, 0)),
76-
Qt::AlignRight | Qt::AlignVCenter,
77-
QPixmap(index.data(Qt::DecorationRole).toString()));
78+
Qt::AlignRight | Qt::AlignVCenter, icon);
79+
}
7880
}
7981
};
8082

8183
class SubtitleDurationDelegate : public QStyledItemDelegate {
8284
void paint(QPainter *painter, const QStyleOptionViewItem &option,
83-
const QModelIndex &index) const {
85+
const QModelIndex &index) const override {
8486
QStyledItemDelegate::paint(painter, option, index);
85-
// Progression is stored in cell user data
8687
qreal progression = index.data(Qt::UserRole).toReal();
8788
if (progression == 0.0)
8889
return;
90+
8991
// Paint progression of subtitle
9092
painter->save();
9193
QPen pen(option.palette.highlightedText().color());
@@ -94,18 +96,19 @@ class SubtitleDurationDelegate : public QStyledItemDelegate {
9496
QPoint startLine(option.rect.bottomLeft());
9597
startLine.setX(startLine.x() + int(option.rect.width() * progression) - 1);
9698
QPoint endLine = option.rect.bottomRight();
99+
97100
if (progression < 0) {
98101
pen.setColor(option.palette.text().color());
99102
startLine = option.rect.bottomLeft();
100103
endLine = option.rect.bottomRight();
101104
endLine.setX(endLine.x() + int(option.rect.width() * progression) + 1);
102105
}
106+
103107
painter->setPen(pen);
104108
painter->drawLine(startLine, endLine);
105109
painter->restore();
106110
}
107111
};
108-
#endif
109112

110113
MainWindow::MainWindow(QWidget *parent)
111114
: QMainWindow(parent), ui(new Ui::MainWindow), m_script(nullptr),
@@ -117,14 +120,14 @@ MainWindow::MainWindow(QWidget *parent)
117120
m_scriptProperties(new QLabel(this)), m_countDown(new QLabel(this)) {
118121
ui->setupUi(this);
119122
m_defaultPalette = qApp->palette();
120-
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
123+
121124
ui->tableWidget->setItemDelegateForColumn(COLUMN_START,
122125
new SubtitleDurationDelegate());
123126
ui->tableWidget->setItemDelegateForColumn(COLUMN_END,
124127
new SubtitleDurationDelegate());
125128
ui->tableWidget->setItemDelegateForColumn(COLUMN_TEXT,
126129
new SubtitleTextDelegate());
127-
#endif
130+
128131
ui->tableWidget->hideColumn(COLUMN_COMMENTS);
129132

130133
ui->tableWidget->installEventFilter(this);

0 commit comments

Comments
 (0)