| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/reusable/helpspoiler.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | #include "gui/reusable/plaintoolbutton.h" |
| 7 | #include "miscellaneous/application.h" |
| 8 | #include "miscellaneous/iconfactory.h" |
| 9 | #include "network-web/webfactory.h" |
| 10 | |
| 11 | #include <QGridLayout> |
| 12 | #include <QParallelAnimationGroup> |
| 13 | #include <QPropertyAnimation> |
| 14 | #include <QScrollArea> |
| 15 | #include <QTextBrowser> |
| 16 | #include <QToolButton> |
| 17 | |
| 18 | HelpSpoiler::HelpSpoiler(QWidget* parent) |
| 19 | : QWidget(parent), m_btnToggle(new QToolButton(this)), m_content(new QScrollArea(this)), |
| 20 | m_animation(new QParallelAnimationGroup(this)), m_layout(new QGridLayout(this)), m_text(new QTextBrowser(this)), |
| 21 | m_btnHelp(new PlainToolButton(this)) { |
| 22 | m_btnToggle->setStyleSheet(QSL("QToolButton { border: none; }")); |
| 23 | m_btnToggle->setToolButtonStyle(Qt::ToolButtonStyle::ToolButtonTextBesideIcon); |
| 24 | m_btnToggle->setArrowType(Qt::ArrowType::RightArrow); |
| 25 | m_btnToggle->setText(tr("View more information on this")); |
| 26 | m_btnToggle->setCheckable(true); |
| 27 | m_btnToggle->setChecked(false); |
| 28 | |
| 29 | m_content->setStyleSheet(QSL("QScrollArea { border: 1px solid %1; }").arg(palette().windowText().color().name())); |
| 30 | m_content->setSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Fixed); |
| 31 | m_content->setMaximumHeight(0); |
| 32 | m_content->setMinimumHeight(0); |
| 33 | m_content->setContentsMargins({0, 0, 0, 0}); |
| 34 | |
| 35 | m_animation->addAnimation(new QPropertyAnimation(this, QSL("minimumHeight").toLocal8Bit())); |
| 36 | m_animation->addAnimation(new QPropertyAnimation(this, QSL("maximumHeight").toLocal8Bit())); |
| 37 | m_animation->addAnimation(new QPropertyAnimation(m_content, QSL("maximumHeight").toLocal8Bit())); |
| 38 | |
| 39 | // Don't waste space. |
| 40 | m_layout->setHorizontalSpacing(0); |
| 41 | m_layout->setVerticalSpacing(0); |
| 42 | m_layout->setContentsMargins(0, 0, 0, 0); |
| 43 | |
| 44 | m_btnHelp->setPadding(0); |
| 45 | |
| 46 | m_layout->addWidget(m_btnHelp, 0, 0); |
| 47 | m_layout->addWidget(m_btnToggle, 0, 1, 1, 1, Qt::AlignmentFlag::AlignLeft); |
| 48 | m_layout->addWidget(m_content, 1, 0, 1, 2); |
| 49 | |
| 50 | connect(m_text, &QTextBrowser::anchorClicked, this, &HelpSpoiler::onAnchorClicked); |
| 51 | |
| 52 | connect(m_btnToggle, &QToolButton::clicked, [this](const bool checked) { |
| 53 | const auto collapsed_height = m_btnHelp->height(); |
| 54 | auto content_height = m_text->document()->size().height() + 22; |
| 55 | |
| 56 | for (int i = 0; i < m_animation->animationCount() - 1; i++) { |
| 57 | QPropertyAnimation* spoiler_animation = static_cast<QPropertyAnimation*>(m_animation->animationAt(i)); |
| 58 | |
| 59 | spoiler_animation->setDuration(100); |
| 60 | spoiler_animation->setStartValue(collapsed_height); |
| 61 | spoiler_animation->setEndValue(collapsed_height + content_height); |
| 62 | } |
| 63 | |
| 64 | QPropertyAnimation* content_animation = |
| 65 | static_cast<QPropertyAnimation*>(m_animation->animationAt(m_animation->animationCount() - 1)); |
| 66 | |
| 67 | content_animation->setDuration(100); |
| 68 | content_animation->setStartValue(0); |
| 69 | content_animation->setEndValue(content_height); |
| 70 | |
| 71 | m_btnToggle->setArrowType(checked ? Qt::ArrowType::DownArrow : Qt::ArrowType::RightArrow); |
| 72 | m_animation->setDirection(checked ? QAbstractAnimation::Direction::Forward |
| 73 | : QAbstractAnimation::Direction::Backward); |
| 74 | m_animation->start(); |
| 75 | }); |
| 76 | |
| 77 | m_text->viewport()->setAutoFillBackground(false); |
| 78 | m_text->setFrameShape(QFrame::Shape::NoFrame); |
| 79 | m_text->setOpenLinks(false); |
| 80 | m_text->setOpenExternalLinks(false); |
| 81 | m_text->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); |
| 82 | m_text->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); |
| 83 | m_text->setWordWrapMode(QTextOption::WrapMode::WordWrap); |
| 84 | |
| 85 | auto* content_layout = new QVBoxLayout(m_content); |
| 86 | |
| 87 | content_layout->addWidget(m_text, 1); |
| 88 | } |
| 89 | |
| 90 | void HelpSpoiler::setHelpText(const QString& title, const QString& text, bool is_warning, bool force_html) { |
| 91 | m_btnToggle->setText(title); |
| 92 | setHelpText(text, is_warning); |
| 93 | } |
| 94 | |
| 95 | void HelpSpoiler::setHelpText(const QString& text, bool is_warning, bool force_html) { |
| 96 | if (force_html) { |
| 97 | m_text->setHtml(text); |
| 98 | } |
| 99 | else { |
| 100 | m_text->setText(text); |
| 101 | } |
| 102 | |
| 103 | m_btnHelp->setIcon(is_warning ? qApp->icons()->fromTheme(QSL("dialog-warning")) |
| 104 | : qApp->icons()->fromTheme(QSL("dialog-question"))); |
| 105 | |
| 106 | m_text->document()->setDocumentMargin(0); |
| 107 | } |
| 108 | |
| 109 | void HelpSpoiler::onAnchorClicked(const QUrl& url) { |
| 110 | qApp->web()->openUrlInExternalBrowser(url.toString()); |
| 111 | } |