From c696b5c19d47c6f07c4a40b463f748db2b0938ae Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Sun, 21 Apr 2019 20:36:11 +0200 Subject: [PATCH] [license] Add show-license toggle button - Non-functional as yet - Toggles expand / collapse arrow and tooltip --- src/modules/license/LicensePage.cpp | 8 +++++ src/modules/license/LicensePage.h | 1 + src/modules/license/LicenseWidget.cpp | 50 +++++++++++++++++++++++++-- src/modules/license/LicenseWidget.h | 10 ++++-- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/modules/license/LicensePage.cpp b/src/modules/license/LicensePage.cpp index 3f16a6698..025f77284 100644 --- a/src/modules/license/LicensePage.cpp +++ b/src/modules/license/LicensePage.cpp @@ -78,6 +78,14 @@ LicenseEntry::LicenseEntry(const QVariantMap& conf) cWarning() << "License entry" << m_id << "has unknown type" << typeString << "(using 'software')"; } +bool +LicenseEntry::isLocal() const +{ + return ( m_url.scheme() == "file" ) && + ( []( const QString&& r ){ return r.endsWith( ".html" ) || r.endsWith( ".txt" ); }( m_url.toString() ) ); +} + + LicensePage::LicensePage(QWidget *parent) : QWidget( parent ) , ui( new Ui::LicensePage ) diff --git a/src/modules/license/LicensePage.h b/src/modules/license/LicensePage.h index e9bd0f696..c08676f92 100644 --- a/src/modules/license/LicensePage.h +++ b/src/modules/license/LicensePage.h @@ -54,6 +54,7 @@ struct LicenseEntry bool isValid() const { return !m_id.isEmpty(); } bool isRequired() const { return m_required; } + bool isLocal() const; QString m_id; QString m_prettyName; diff --git a/src/modules/license/LicenseWidget.cpp b/src/modules/license/LicenseWidget.cpp index 4030cbd6c..7a52f4e9e 100644 --- a/src/modules/license/LicenseWidget.cpp +++ b/src/modules/license/LicenseWidget.cpp @@ -23,6 +23,7 @@ #include #include +#include LicenseWidget::LicenseWidget( LicenseEntry entry, QWidget* parent ) @@ -30,6 +31,7 @@ LicenseWidget::LicenseWidget( LicenseEntry entry, QWidget* parent ) , m_entry( std::move( entry ) ) , m_label( new QLabel( this ) ) , m_viewLicenseLabel( new QLabel( this ) ) + , m_expandLicenseButton( nullptr ) { QPalette pal( palette() ); pal.setColor( QPalette::Background, palette().background().color().lighter( 108 ) ); @@ -47,10 +49,21 @@ LicenseWidget::LicenseWidget( LicenseEntry entry, QWidget* parent ) wiLayout->addWidget( m_label ); m_viewLicenseLabel->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ); - m_viewLicenseLabel->setOpenExternalLinks( true ); m_viewLicenseLabel->setAlignment( Qt::AlignVCenter | Qt::AlignRight ); wiLayout->addWidget( m_viewLicenseLabel ); + if ( m_entry.isLocal() ) + { + m_expandLicenseButton = new QToolButton( this ); + m_expandLicenseButton->setArrowType( Qt::DownArrow ); + wiLayout->addWidget( m_expandLicenseButton ); + + connect( m_expandLicenseButton, &QAbstractButton::clicked, this, &LicenseWidget::expandClicked ); + } + else + m_viewLicenseLabel->setOpenExternalLinks( true ); + + retranslateUi(); } @@ -103,6 +116,39 @@ void LicenseWidget::retranslateUi() } m_label->setText( productDescription ); - m_viewLicenseLabel->setText( tr( "view license agreement" ) + if ( m_entry.isLocal() ) + { + m_viewLicenseLabel->setText( tr( "Show license agreement" ) ); + updateExpandToolTip(); + } + else + m_viewLicenseLabel->setText( tr( "view license agreement" ) .arg( m_entry.m_url.toString() ) ); } + +void +LicenseWidget::expandClicked() +{ + if ( m_expandLicenseButton->arrowType() == Qt::DownArrow ) + { + m_expandLicenseButton->setArrowType( Qt::UpArrow ); + } + else + { + m_expandLicenseButton->setArrowType( Qt::DownArrow ); + } + updateExpandToolTip(); +} + +void +LicenseWidget::updateExpandToolTip() +{ + if ( !m_expandLicenseButton ) + return; + + m_expandLicenseButton->setToolTip( + ( m_expandLicenseButton->arrowType() == Qt::DownArrow ) + ? tr( "Show complete license text" ) + : tr( "Hide license text" ) + ) ; +} diff --git a/src/modules/license/LicenseWidget.h b/src/modules/license/LicenseWidget.h index e91ea23c1..ab6a91491 100644 --- a/src/modules/license/LicenseWidget.h +++ b/src/modules/license/LicenseWidget.h @@ -27,6 +27,8 @@ #include #include +class QToolButton; + class LicenseWidget : public QWidget { public: @@ -36,8 +38,12 @@ public: void retranslateUi(); private: + void expandClicked(); + void updateExpandToolTip(); + LicenseEntry m_entry; - QLabel *m_label; - QLabel *m_viewLicenseLabel; + QLabel* m_label; + QLabel* m_viewLicenseLabel; + QToolButton* m_expandLicenseButton; } ; #endif