add scroll bar to properties in cardinfotextwidget (#6201)

* add scroll bar to properties in cardinfotextwidget

* remove resizeevent trigger
This commit is contained in:
ebbit1q 2025-10-03 04:55:18 +02:00 committed by GitHub
parent c5b361e94d
commit a69bfb8cb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 15 deletions

View file

@ -6,32 +6,55 @@
#include <QGridLayout> #include <QGridLayout>
#include <QLabel> #include <QLabel>
#include <QScrollArea>
#include <QScrollBar>
#include <QSizePolicy>
#include <QTextEdit> #include <QTextEdit>
CardInfoTextWidget::CardInfoTextWidget(QWidget *parent) : QFrame(parent), info(nullptr) CardInfoTextWidget::CardInfoTextWidget(QWidget *parent) : QFrame(parent), info(nullptr)
{ {
nameLabel = new QLabel; propsLabel = new QLabel;
nameLabel->setOpenExternalLinks(false); propsLabel->setOpenExternalLinks(false);
nameLabel->setWordWrap(true); propsLabel->setWordWrap(true);
connect(nameLabel, SIGNAL(linkActivated(const QString &)), this, SIGNAL(linkActivated(const QString &))); connect(propsLabel, SIGNAL(linkActivated(const QString &)), this, SIGNAL(linkActivated(const QString &)));
textLabel = new QTextEdit(); textLabel = new QTextEdit();
textLabel->setReadOnly(true); textLabel->setReadOnly(true);
textLabel->setMinimumSize(35, 35);
propsScroll = new QScrollArea(this);
propsScroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
propsScroll->setWidgetResizable(true);
propsScroll->setAlignment(Qt::AlignLeft | Qt::AlignTop);
propsScroll->setContentsMargins(0, 0, 0, 0);
propsScroll->setFrameStyle(QFrame::NoFrame);
propsScroll->setWidget(propsLabel);
// blend into normal background color, note that themes may override this!
propsScroll->viewport()->setStyleSheet("QWidget{background: transparent}");
auto *grid = new QGridLayout(this); auto *grid = new QGridLayout(this);
grid->addWidget(nameLabel, 0, 0); grid->addWidget(propsScroll, 0, 0);
grid->addWidget(textLabel, 1, 0, -1, 2); grid->addWidget(textLabel, 1, 0);
grid->setRowStretch(0, 2);
grid->setRowStretch(1, 1); grid->setRowStretch(1, 1);
grid->setColumnStretch(1, 1);
retranslateUi(); retranslateUi();
} }
void CardInfoTextWidget::setTexts(const QString &propsText, const QString &textText)
{
propsScroll->setMaximumHeight(0); // reset the max height, otherwise the scrollbar will blink in and out sometimes
propsLabel->setText(propsText);
propsScroll->setMinimumWidth(propsLabel->minimumWidth() + propsScroll->verticalScrollBar()->width());
propsScroll->setMaximumHeight(propsLabel->sizeHint().height());
textLabel->setText(textText);
}
void CardInfoTextWidget::setCard(CardInfoPtr card) void CardInfoTextWidget::setCard(CardInfoPtr card)
{ {
if (card == nullptr) { if (card == nullptr) {
nameLabel->setText(""); setTexts("", "");
textLabel->setText("");
return; return;
} }
@ -61,14 +84,12 @@ void CardInfoTextWidget::setCard(CardInfoPtr card)
} }
text += "</table>"; text += "</table>";
nameLabel->setText(text); setTexts(text, card->getText());
textLabel->setText(card->getText());
} }
void CardInfoTextWidget::setInvalidCardName(const QString &cardName) void CardInfoTextWidget::setInvalidCardName(const QString &cardName)
{ {
nameLabel->setText(tr("Unknown card:") + " " + cardName); setTexts(tr("Unknown card:") + " " + cardName, "");
textLabel->setText("");
} }
void CardInfoTextWidget::retranslateUi() void CardInfoTextWidget::retranslateUi()
@ -77,5 +98,5 @@ void CardInfoTextWidget::retranslateUi()
* There's no way we can really translate the text currently being rendered. * There's no way we can really translate the text currently being rendered.
* The best we can do is invalidate the current text. * The best we can do is invalidate the current text.
*/ */
setInvalidCardName(""); setTexts("", "");
} }

View file

@ -11,6 +11,7 @@
#include <QFrame> #include <QFrame>
class QLabel; class QLabel;
class QScrollArea;
class QTextEdit; class QTextEdit;
class CardInfoTextWidget : public QFrame class CardInfoTextWidget : public QFrame
@ -18,9 +19,11 @@ class CardInfoTextWidget : public QFrame
Q_OBJECT Q_OBJECT
private: private:
QLabel *nameLabel; QLabel *propsLabel;
QScrollArea *propsScroll;
QTextEdit *textLabel; QTextEdit *textLabel;
CardInfoPtr info; CardInfoPtr info;
void setTexts(const QString &propsText, const QString &textText);
public: public:
explicit CardInfoTextWidget(QWidget *parent = nullptr); explicit CardInfoTextWidget(QWidget *parent = nullptr);