From c16b3572388da7a8987bbc09860a3377509f48c4 Mon Sep 17 00:00:00 2001 From: Ripose Date: Sat, 25 Sep 2021 18:03:59 -0700 Subject: [PATCH] subtitlewidget: fixes high cpu usage The previous implementation of SubtitleWidget had very high CPU usage for unknown reasons. Turns out that an infinite loop was created by overriding paintEvent and then doing cursor operations in there. Basically new updates were being created inside of paintEvent which would baloon over until subtitleWidget was hidden or the event queue was too large. This fixes the problem by completely ignoring the StackOverflow solution and instead doing things the "dumb" way, i.e. having one QTextEdit with a stroke in the background, and another QTextEdit overlayed on top of that QTextEdit as the foreground. This achieves a similar effect without the high CPU usage or having to override paintEvent. This new widget is called at StrokeLabel and both ProgessSlider and SubtitleWidget depend on it. --- src/gui/widgets/common/CMakeLists.txt | 15 +- src/gui/widgets/common/strokelabel.cpp | 234 ++++++++++++++++++ src/gui/widgets/common/strokelabel.h | 153 ++++++++++++ src/gui/widgets/overlay/CMakeLists.txt | 12 + src/gui/widgets/overlay/playercontrols.ui | 2 +- .../{common => overlay}/progressslider.cpp | 84 +------ .../{common => overlay}/progressslider.h | 0 src/gui/widgets/overlay/subtitlewidget.cpp | 153 +++--------- src/gui/widgets/overlay/subtitlewidget.h | 27 +- 9 files changed, 448 insertions(+), 232 deletions(-) create mode 100644 src/gui/widgets/common/strokelabel.cpp create mode 100644 src/gui/widgets/common/strokelabel.h rename src/gui/widgets/{common => overlay}/progressslider.cpp (72%) rename src/gui/widgets/{common => overlay}/progressslider.h (100%) diff --git a/src/gui/widgets/common/CMakeLists.txt b/src/gui/widgets/common/CMakeLists.txt index 56044a2d..ba74b568 100644 --- a/src/gui/widgets/common/CMakeLists.txt +++ b/src/gui/widgets/common/CMakeLists.txt @@ -8,22 +8,21 @@ target_link_libraries( ) add_library( - sliderjumpstyle - sliderjumpstyle.cpp + strokelabel + strokelabel.cpp ) target_link_libraries( - sliderjumpstyle + strokelabel Qt5::Widgets ) add_library( - progressslider - progressslider.cpp + sliderjumpstyle + sliderjumpstyle.cpp ) target_link_libraries( - progressslider + sliderjumpstyle Qt5::Widgets - Qt5::Core ) add_library( @@ -42,4 +41,4 @@ add_library( target_link_libraries( scrolldoublespinbox Qt5::Widgets -) \ No newline at end of file +) diff --git a/src/gui/widgets/common/strokelabel.cpp b/src/gui/widgets/common/strokelabel.cpp new file mode 100644 index 00000000..ba40dfbd --- /dev/null +++ b/src/gui/widgets/common/strokelabel.cpp @@ -0,0 +1,234 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2021 Ripose +// +// This file is part of Memento. +// +// Memento 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, version 2 of the License. +// +// Memento 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 Memento. If not, see . +// +//////////////////////////////////////////////////////////////////////////////// + +#include "strokelabel.h" + +#include +#include + +/* Begin Constructor/Destructor */ + +#define TRANSPARENT_COLOR QColor("#00000000") + +StrokeLabel::StrokeLabel(QWidget *parent) + : QWidget(parent), + m_backgroundText(new QTextEdit(this)), + m_foregroundText(new QTextEdit(this)), + m_textColor(palette().text().color()), + m_strokeColor(palette().window().color()), + m_strokeSize(4.0), + m_backgroundColor(TRANSPARENT_COLOR) +{ + initTextEdit(m_backgroundText); + + initTextEdit(m_foregroundText); + m_foregroundText->raise(); + + setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + setFocusPolicy(Qt::FocusPolicy::NoFocus); + fitToContents(); + updateColors(); +} + +void StrokeLabel::initTextEdit(QTextEdit *te) +{ + te->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); + te->setFocusPolicy(Qt::FocusPolicy::NoFocus); + te->setAcceptDrops(false); + te->setFrameShape(QFrame::Shape::NoFrame); + te->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + te->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + te->setLineWrapMode(QTextEdit::NoWrap); + te->setReadOnly(true); + te->setAcceptRichText(false); + te->setTextInteractionFlags(Qt::NoTextInteraction); + te->setCursor(Qt::ArrowCursor); +} + +void StrokeLabel::updateColors() +{ + /* Set the text and background color */ + QString stylesheetFormat = + "QTextEdit {" + "color: rgba(%1, %2, %3, %4);" + "background: rgba(0, 0, 0, 0);" + "}"; + m_foregroundText->setStyleSheet( + stylesheetFormat.arg(QString::number(m_textColor.red())) + .arg(QString::number(m_textColor.green())) + .arg(QString::number(m_textColor.blue())) + .arg(QString::number(m_textColor.alpha())) + ); + + stylesheetFormat = + "QTextEdit {" + "color: rgba(%1, %2, %3, %4);" + "background: rgba(%5, %6, %7, %8);" + "}"; + m_backgroundText->setStyleSheet( + stylesheetFormat.arg(QString::number(m_textColor.red())) + .arg(QString::number(m_textColor.green())) + .arg(QString::number(m_textColor.blue())) + .arg(QString::number(m_textColor.alpha())) + .arg(QString::number(m_backgroundColor.red())) + .arg(QString::number(m_backgroundColor.green())) + .arg(QString::number(m_backgroundColor.blue())) + .arg(QString::number(m_backgroundColor.alpha())) + ); + + setText(m_foregroundText->toPlainText()); +} + + +void StrokeLabel::fitToContents() +{ + m_backgroundText->updateGeometry(); + int width = m_backgroundText->document()->idealWidth() + 4; + int height = m_backgroundText->document()->size().toSize().height(); + setSize(width, height); +} + +#undef TRANSPARENT_COLOR + +/* End Constructor/Destructor */ +/* Begin Color Setters */ + +void StrokeLabel::setTextColor(const QColor &color) +{ + m_textColor = color; + updateColors(); +} + +void StrokeLabel::setStrokeColor(const QColor &color) +{ + m_strokeColor = color; + updateColors(); +} + +void StrokeLabel::setStrokeSize(double size) +{ + m_strokeSize = size; + updateColors(); +} + +void StrokeLabel::setBackgroundColor(const QColor &color) +{ + m_backgroundColor = color; + updateColors(); +} + +/* End Color Setters */ +/* Begin Font Methods */ + +QFont StrokeLabel::textFont() const +{ + return m_foregroundText->font(); +} + +void StrokeLabel::setTextFont(const QFont &f) +{ + m_foregroundText->setFont(f); + m_backgroundText->setFont(f); + fitToContents(); +} + +/* End Font Methods */ +/* Begin Text Methods */ + +void StrokeLabel::setText(const QString &text) +{ + clearText(); + QStringList subList = text.split('\n'); + for (const QString &text : subList) + { + if (text.isEmpty()) + continue; + + m_backgroundText->append(text); + m_backgroundText->setAlignment(Qt::AlignHCenter); + + m_foregroundText->append(text); + m_foregroundText->setAlignment(Qt::AlignHCenter); + } + + /* Add the stroke */ + QTextCharFormat format; + format.setTextOutline( + QPen( + m_strokeColor, + m_strokeSize, + Qt::SolidLine, + Qt::RoundCap, + Qt::RoundJoin + ) + ); + QTextCursor cursor(m_backgroundText->document()); + cursor.select(QTextCursor::Document); + cursor.mergeCharFormat(format); + + fitToContents(); +} + + +QString StrokeLabel::getText() const +{ + return m_foregroundText->toPlainText(); +} + +void StrokeLabel::clearText() +{ + m_backgroundText->clear(); + m_foregroundText->clear(); + setSize(0, 0); +} + +void StrokeLabel::selectText(int start, int length) +{ + QTextCursor q = m_foregroundText->textCursor(); + q.setPosition(start); + q.setPosition(start + length, QTextCursor::KeepAnchor); + m_foregroundText->setTextCursor(q); +} + +void StrokeLabel::deselectText() +{ + QTextCursor q = m_foregroundText->textCursor(); + q.clearSelection(); + m_foregroundText->setTextCursor(q); +} + +/* End Text Methods */ +/* Begin Helper Methods */ + +void StrokeLabel::setSize(int w, int h) +{ + m_backgroundText->setFixedSize(w, h); + m_foregroundText->setFixedSize(w, h); + setFixedSize(w, h); +} + +int StrokeLabel::getPosition(const QPoint &pos) const +{ + return m_foregroundText->document()->documentLayout()->hitTest( + pos, Qt::ExactHit + ); +} + +/* End Helper Methods */ diff --git a/src/gui/widgets/common/strokelabel.h b/src/gui/widgets/common/strokelabel.h new file mode 100644 index 00000000..9319d874 --- /dev/null +++ b/src/gui/widgets/common/strokelabel.h @@ -0,0 +1,153 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2021 Ripose +// +// This file is part of Memento. +// +// Memento 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, version 2 of the License. +// +// Memento 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 Memento. If not, see . +// +//////////////////////////////////////////////////////////////////////////////// + +#ifndef STROKELABEL_H +#define STROKELABEL_H + +#include + +class QTextEdit; + +/** + * A label that displays text with a stroke. + */ +class StrokeLabel : public QWidget +{ + Q_OBJECT + +public: + StrokeLabel(QWidget *parent = nullptr); + + /** + * Sets the color of the text. + * @param color The color to set the text to. + */ + void setTextColor(const QColor &color); + + /** + * Sets the color of the stroke. + * @param color The color of the stroke. + */ + void setStrokeColor(const QColor &color); + + /** + * Sets the size of the stroke. + * @param size The size of the stroke. + */ + void setStrokeSize(double size); + + /** + * Sets the color of the background. + * @param color The color of the background. + */ + void setBackgroundColor(const QColor &color); + + /** + * Gets the current text font. + * @return The current font. + */ + QFont textFont() const; + + /** + * Sets the current text font. + * @param f The font to make the current font. + */ + void setTextFont(const QFont &f); + + /** + * Sets the text of the label. Text is always centered. + * @param text The text to set the label to. + */ + void setText(const QString &text); + + /** + * Gets the current text of the label in plain text. + */ + QString getText() const; + + /** + * Removes all text from the label. + */ + void clearText(); + + /** + * Gets the index of the character at the current position. + * @param pos The point (on this widget) to get the text index of. + */ + int getPosition(const QPoint &pos) const; + +public Q_SLOTS: + /** + * Selects the text starting at some index for some number of characters. + * @param start The starting index to begin the selection from. + * @param length The length of the selection. + */ + void selectText(int start, int length); + + /** + * Deselects text if any is currently selected. + */ + void deselectText(); + +protected: + /** + * Resizes the label to fit the context of the current text. + */ + void fitToContents(); + +private: + /** + * Initializes a QTextEdit with all the expected common configuration. + * @param te The QTextEdit to initialize. + */ + void initTextEdit(QTextEdit *te); + + /** + * Updates the colors of the QTextEdit. + */ + void updateColors(); + + /** + * Sets the fixed size of the widget. + * @param h The height of the widget. + * @param w The width of the widget. + */ + void setSize(int h, int w); + + /* The background QTextEdit that creates the stroke effect. */ + QTextEdit *m_backgroundText; + + /* The foreground QTextEdit that creates the text */ + QTextEdit *m_foregroundText; + + /* The current color of the text */ + QColor m_textColor; + + /* The current color of the stroke */ + QColor m_strokeColor; + + /* The current size of the stroke */ + double m_strokeSize; + + /* The current background color */ + QColor m_backgroundColor; +}; + +#endif // STROKELABEL_H \ No newline at end of file diff --git a/src/gui/widgets/overlay/CMakeLists.txt b/src/gui/widgets/overlay/CMakeLists.txt index 39cf7e11..bb6cd336 100644 --- a/src/gui/widgets/overlay/CMakeLists.txt +++ b/src/gui/widgets/overlay/CMakeLists.txt @@ -1,3 +1,14 @@ +add_library( + progressslider + progressslider.cpp +) +target_link_libraries( + progressslider + strokelabel + Qt5::Widgets + Qt5::Core +) + add_library( playermenu playermenu.cpp @@ -14,6 +25,7 @@ add_library( ) target_link_libraries( subtitlewidget + strokelabel Qt5::Widgets globalmediator dictionary_db diff --git a/src/gui/widgets/overlay/playercontrols.ui b/src/gui/widgets/overlay/playercontrols.ui index 0b4627f1..29232bc5 100644 --- a/src/gui/widgets/overlay/playercontrols.ui +++ b/src/gui/widgets/overlay/playercontrols.ui @@ -294,7 +294,7 @@ ProgressSlider QSlider -
src/gui/widgets/common/progressslider.h
+
src/gui/widgets/overlay/progressslider.h
diff --git a/src/gui/widgets/common/progressslider.cpp b/src/gui/widgets/overlay/progressslider.cpp similarity index 72% rename from src/gui/widgets/common/progressslider.cpp rename to src/gui/widgets/overlay/progressslider.cpp index 8adaee79..64baa7ff 100644 --- a/src/gui/widgets/common/progressslider.cpp +++ b/src/gui/widgets/overlay/progressslider.cpp @@ -29,78 +29,8 @@ #include #include "../../../util/globalmediator.h" +#include "../common/strokelabel.h" -/* Begin StrokeLabel */ - -/** - * A widget that displays text with a stroke. - */ -class StrokeLabel : public QTextEdit -{ -public: - StrokeLabel(QWidget *parent = nullptr); - - /** - * Sets the text of the widget and adjusts the size according to the - * content. - * @param text The text to set the widget to. - */ - void setText(const QString &text); - -protected: - /** - * Adds the stroke. - * @param event The paint event. - */ - void paintEvent(QPaintEvent *event) override; -}; - -StrokeLabel::StrokeLabel(QWidget *parent) : QTextEdit(parent) -{ - setReadOnly(true); - setAutoFillBackground(false); - setFrameStyle(QFrame::NoFrame); - setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - setStyleSheet("QTextEdit { background : rgba(0, 0, 0, 0); }"); -} - -void StrokeLabel::paintEvent(QPaintEvent *event) -{ - QTextCharFormat format; - format.setTextOutline( - QPen( - parentWidget()->palette().window(), - 3.0, - Qt::SolidLine, - Qt::RoundCap, - Qt::RoundJoin - ) - ); - QTextCursor cursor(document()); - cursor.select(QTextCursor::Document); - cursor.mergeCharFormat(format); - QTextEdit::paintEvent(event); - - format = QTextCharFormat(); - format.setTextOutline(QPen(Qt::transparent)); - cursor.mergeCharFormat(format); - QTextEdit::paintEvent(event); -} - -void StrokeLabel::setText(const QString &text) -{ - setPlainText(text); - document()->adjustSize(); - int width = document()->idealWidth() + 10; - setFixedWidth(width); - int height = document()->size().toSize().height(); - setFixedHeight(height); - updateGeometry(); -} - -/* End StrokeLabel */ /* Begin Constructor/Destructor */ /** @@ -148,7 +78,8 @@ ProgressSlider::~ProgressSlider() void ProgressSlider::initTheme() { - m_labelTimecode->setPalette(palette()); + m_labelTimecode->setTextColor(palette().text().color()); + m_labelTimecode->setStrokeColor(palette().window().color()); initStylesheet(); } @@ -166,9 +97,9 @@ void ProgressSlider::initStylesheet() void ProgressSlider::showEvent(QShowEvent *event) { - QFont font = m_labelTimecode->font(); + QFont font = m_labelTimecode->textFont(); font.setPixelSize(height()); - m_labelTimecode->setFont(font); + m_labelTimecode->setTextFont(font); } void ProgressSlider::hideEvent(QHideEvent *event) @@ -181,6 +112,8 @@ void ProgressSlider::hideEvent(QHideEvent *event) void ProgressSlider::mouseMoveEvent(QMouseEvent *event) { + QSlider::mouseMoveEvent(event); + if (maximum() == 0) { QSlider::mouseMoveEvent(event); @@ -210,8 +143,6 @@ void ProgressSlider::mouseMoveEvent(QMouseEvent *event) { m_labelTimecode->show(); } - - QSlider::mouseMoveEvent(event); } #undef MOUSE_OFFSET @@ -219,6 +150,7 @@ void ProgressSlider::mouseMoveEvent(QMouseEvent *event) void ProgressSlider::paintEvent(QPaintEvent* event) { QSlider::paintEvent(event); + QRect rect = event->rect(); QPainter p(this); p.setPen(palette().window().color()); diff --git a/src/gui/widgets/common/progressslider.h b/src/gui/widgets/overlay/progressslider.h similarity index 100% rename from src/gui/widgets/common/progressslider.h rename to src/gui/widgets/overlay/progressslider.h diff --git a/src/gui/widgets/overlay/subtitlewidget.cpp b/src/gui/widgets/overlay/subtitlewidget.cpp index 39373f35..2e263df3 100644 --- a/src/gui/widgets/overlay/subtitlewidget.cpp +++ b/src/gui/widgets/overlay/subtitlewidget.cpp @@ -42,7 +42,7 @@ /* Begin Constructor/Destructor */ SubtitleWidget::SubtitleWidget(QWidget *parent) - : QTextEdit(parent), + : StrokeLabel(parent), m_dictionary(GlobalMediator::getGlobalMediator()->getDictionary()), m_currentIndex(-1), m_findDelay(new QTimer(this)), @@ -56,19 +56,9 @@ SubtitleWidget::SubtitleWidget(QWidget *parent) initTheme(); - setFixedHeight(0); - setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); - setFocusPolicy(Qt::FocusPolicy::NoFocus); - setAcceptDrops(false); - setFrameShape(QFrame::Shape::NoFrame); - setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - setLineWrapMode(QTextEdit::NoWrap); - setReadOnly(true); - setAcceptRichText(false); - setTextInteractionFlags(Qt::NoTextInteraction); - hide(); + setMouseTracking(true); setCursor(Qt::ArrowCursor); + hide(); m_findDelay->setSingleShot(true); @@ -92,7 +82,7 @@ SubtitleWidget::SubtitleWidget(QWidget *parent) ); connect( mediator, &GlobalMediator::definitionsHidden, - this, &SubtitleWidget::deselectText + this, &StrokeLabel::deselectText ); connect( mediator, &GlobalMediator::definitionsShown, @@ -178,13 +168,7 @@ void SubtitleWidget::initTheme() SETTINGS_INTERFACE_SUB_SCALE_DEFAULT ).toDouble() ); - setFont(font); - - QString stylesheetFormat = - "QTextEdit {" - "color: rgba(%1, %2, %3, %4);" - "background: rgba(%5, %6, %7, %8);" - "}"; + setTextFont(font); QColor fontColor( settings.value( @@ -192,35 +176,29 @@ void SubtitleWidget::initTheme() SETTINGS_INTERFACE_SUB_TEXT_COLOR_DEFAULT ).toString() ); + setTextColor(fontColor); + QColor bgColor( settings.value( SETTINGS_INTERFACE_SUB_BG_COLOR, SETTINGS_INTERFACE_SUB_BG_COLOR_DEFAULT ).toString() ); + setBackgroundColor(bgColor); - setStyleSheet( - stylesheetFormat.arg(QString::number(fontColor.red())) - .arg(QString::number(fontColor.green())) - .arg(QString::number(fontColor.blue())) - .arg(QString::number(fontColor.alpha())) - .arg(QString::number(bgColor.red())) - .arg(QString::number(bgColor.green())) - .arg(QString::number(bgColor.blue())) - .arg(QString::number(bgColor.alpha())) - - ); - - m_settings.strokeColor.setNamedColor( + QColor strokeColor( settings.value( SETTINGS_INTERFACE_SUB_STROKE_COLOR, SETTINGS_INTERFACE_SUB_STROKE_COLOR_DEFAULT ).toString() ); - m_settings.strokeSize = settings.value( + setStrokeColor(strokeColor); + + double strokeSize = settings.value( SETTINGS_INTERFACE_SUB_STROKE, SETTINGS_INTERFACE_SUB_STROKE_DEFAULT ).toDouble(); + setStrokeSize(strokeSize); settings.endGroup(); @@ -324,30 +302,32 @@ void SubtitleWidget::initSettings() void SubtitleWidget::showEvent(QShowEvent *event) { + StrokeLabel::showEvent(event); + if (m_settings.hideSubsWhenVisible) { Q_EMIT GlobalMediator::getGlobalMediator() ->requestSetSubtitleVisibility(false); } - QTextEdit::showEvent(event); } void SubtitleWidget::hideEvent(QHideEvent *event) { + StrokeLabel::hideEvent(event); + if (m_settings.hideSubsWhenVisible && m_settings.hideOnPlay) { Q_EMIT GlobalMediator::getGlobalMediator() ->requestSetSubtitleVisibility(true); } Q_EMIT GlobalMediator::getGlobalMediator()->subtitleHidden(); - QTextEdit::hideEvent(event); } void SubtitleWidget::mouseMoveEvent(QMouseEvent *event) { - int position = document()->documentLayout()->hitTest( - event->pos(), Qt::ExactHit - ); + StrokeLabel::mouseMoveEvent(event); + + int position = getPosition(event->pos()); if (!m_paused || position == m_currentIndex || position == -1) { return; @@ -374,50 +354,26 @@ void SubtitleWidget::mouseMoveEvent(QMouseEvent *event) void SubtitleWidget::mouseDoubleClickEvent(QMouseEvent *event) { + StrokeLabel::mouseDoubleClickEvent(event); + QApplication::clipboard()->setText(m_subtitle.rawText); } void SubtitleWidget::leaveEvent(QEvent *event) { + StrokeLabel::leaveEvent(event); + m_findDelay->stop(); m_currentIndex = -1; } void SubtitleWidget::resizeEvent(QResizeEvent *event) { - setAlignment(Qt::AlignHCenter); - if (!m_subtitle.rawText.isEmpty()) - fitToContents(); - - event->ignore(); - QTextEdit::resizeEvent(event); + StrokeLabel::resizeEvent(event); Q_EMIT GlobalMediator::getGlobalMediator()->requestDefinitionDelete(); } -void SubtitleWidget::paintEvent(QPaintEvent *event) -{ - QTextCharFormat format; - format.setTextOutline( - QPen( - m_settings.strokeColor, - m_settings.strokeSize, - Qt::SolidLine, - Qt::RoundCap, - Qt::RoundJoin - ) - ); - QTextCursor cursor(document()); - cursor.select(QTextCursor::Document); - cursor.mergeCharFormat(format); - QTextEdit::paintEvent(event); - - format = QTextCharFormat(); - format.setTextOutline(QPen(Qt::transparent)); // Potential SIGSEGV - cursor.mergeCharFormat(format); - QTextEdit::paintEvent(event); -} - /* End Event Handlers */ /* Begin General Slots */ @@ -458,7 +414,7 @@ void SubtitleWidget::findTerms() Q_EMIT GlobalMediator::getGlobalMediator()->termsChanged(terms); m_lastEmittedIndex = index; m_lastEmittedSize = terms->first()->clozeBody.size(); - m_lastEmittedSize += toPlainText() + m_lastEmittedSize += getText() .midRef(m_lastEmittedIndex, m_lastEmittedSize) .count('\n'); } @@ -472,7 +428,7 @@ void SubtitleWidget::adjustVisibility() { hide(); } - else if (toPlainText().isEmpty()) + else if (getText().isEmpty()) { hide(); } @@ -496,7 +452,7 @@ void SubtitleWidget::positionChanged(const double value) value > m_subtitle.endTime + DOUBLE_DELTA) { m_subtitle.rawText.clear(); - clear(); + clearText(); hide(); Q_EMIT GlobalMediator::getGlobalMediator()->subtitleExpired(); } @@ -516,26 +472,7 @@ void SubtitleWidget::setSubtitle(QString subtitle, } /* Add it to the text edit */ - clear(); - QStringList subList = subtitle.split('\n'); - for (const QString &text : subList) - { - if (text.isEmpty()) - continue; - - append(text); - setAlignment(Qt::AlignHCenter); - } - - /* Update Size */ - if (m_subtitle.rawText.isEmpty()) - { - setFixedSize(QSize(0, 0)); - } - else - { - fitToContents(); - } + setText(subtitle); /* Keep track of when to delete the subtitle */ m_subtitle.startTime = start + delay; @@ -547,37 +484,7 @@ void SubtitleWidget::setSubtitle(QString subtitle, void SubtitleWidget::selectText() { - QTextCursor q = textCursor(); - q.setPosition(m_lastEmittedIndex); - q.setPosition( - m_lastEmittedIndex + m_lastEmittedSize, - QTextCursor::KeepAnchor - ); - setTextCursor(q); -} - -void SubtitleWidget::deselectText() -{ - QTextCursor q = textCursor(); - q.clearSelection(); - setTextCursor(q); + StrokeLabel::selectText(m_lastEmittedIndex, m_lastEmittedSize); } /* End General Slots */ -/* Begin Helpers */ - -void SubtitleWidget::fitToContents() -{ - updateGeometry(); - int width = document()->idealWidth() + 4; - if (width > GlobalMediator::getGlobalMediator()->getPlayerWidget()->width()) - { - width = GlobalMediator::getGlobalMediator()->getPlayerWidget()->width(); - } - setFixedWidth(width); - int height = document()->size().toSize().height(); - setFixedHeight(height); - updateGeometry(); -} - -/* End Helpers */ diff --git a/src/gui/widgets/overlay/subtitlewidget.h b/src/gui/widgets/overlay/subtitlewidget.h index 3de1cbe8..497f9808 100644 --- a/src/gui/widgets/overlay/subtitlewidget.h +++ b/src/gui/widgets/overlay/subtitlewidget.h @@ -21,7 +21,7 @@ #ifndef SUBTITLEWIDGET_H #define SUBTITLEWIDGET_H -#include +#include "../common/strokelabel.h" #include #include @@ -31,7 +31,7 @@ /** * Widget used to display subtitle text and initiate searches. */ -class SubtitleWidget : public QTextEdit +class SubtitleWidget : public StrokeLabel { Q_OBJECT @@ -67,12 +67,6 @@ class SubtitleWidget : public QTextEdit */ void resizeEvent(QResizeEvent *event) override; - /** - * Applys a stroke to the text if needed. - * @param event The paint event. - */ - void paintEvent(QPaintEvent *event) override; - /** * Hides the player subtitles when visible if set. * @param event The show event, not used. @@ -133,17 +127,7 @@ private Q_SLOTS: */ void selectText(); - /** - * Deselects any currently selected text. - */ - void deselectText(); - private: - /** - * Resizes the widget to the current text. - */ - void fitToContents(); - /** * Destructor for the term list. */ @@ -220,12 +204,7 @@ private Q_SLOTS: /* The string to replace newlines with if replaceNewLines is true. */ QString replaceStr; - /* The color of the stroke. */ - QColor strokeColor; - - /* The size of the stroke. */ - double strokeSize; - + /* true if subtitles should be shown when needed, false otherwise. */ bool showSubtitles; } m_settings; };