view ui/certificateitemwidget.cpp @ 1219:cc86f69d6ac3

(issue115) icon cert-is-installed-bad with larger sign.
author Bernhard Reiter <bernhard@intevation.de>
date Wed, 24 Sep 2014 11:35:20 +0200
parents 6594e8e63a25
children ad4fc3649ffb
line wrap: on
line source
/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU GPL (v>=2)
 * and comes with ABSOLUTELY NO WARRANTY!
 * See LICENSE.txt for details.
 */
#include "certificateitemwidget.h"

#include <QHBoxLayout>
#include <QDebug>
#include <QStyleFactory>
#include <QToolButton>

CertificateItemWidget::CertificateItemWidget(QWidget *parent,
                                             const Certificate &cert,
                                             bool state,
                                             QToolButton *btn) :
    QWidget(parent),
    mButton(btn)
{
    mCertificate = cert;
    mOriginalState = state;
    btn->setCheckable(true);
    btn->setStyleSheet("QToolButton:Checked{"
         "border: 1px solid #8f8f91;"
         "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
                                           "stop: 0 #f6f7fa, stop: 1 #dadbde);"
    "}"
    );
    setState(state);
    setupGUI();
}

void CertificateItemWidget::setupGUI()
{
    mLabel = new QLabel;

    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);

    const QString validity = tr("Validity: %1 until %2").arg(
            QLocale::system().toString(mCertificate.validFrom().date(), QLocale::ShortFormat)).arg(
            QLocale::system().toString(mCertificate.validTo().date(), QLocale::ShortFormat));
    const QString fpstring = tr("Fingerprint (SHA1): <code>%1</code>").arg(mCertificate.fingerprint());
    mLabel->setText(QString::fromLatin1("<big><b>%1</b></big><br/>%2<br/>%3<br/>%4").arg
        (mCertificate.subjectCN()).arg(mCertificate.subjectO()).arg(validity).arg
        (fpstring));
    mLabel->setTextFormat(Qt::RichText);

    mLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

    mLabel->setTextInteractionFlags(
        Qt::TextSelectableByMouse |
        Qt::TextSelectableByKeyboard);
    mButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    connect(mButton, SIGNAL(toggled (bool)),
        this, SLOT(currentStateChanged(bool)));

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(mButton);
    mButton->setFixedSize(64, 64);
    mButton->setIconSize(QSize(48, 48));
    layout->addWidget(mLabel);
    this->setLayout(layout);
}

bool CertificateItemWidget::state()
{
    if (!mButton->isEnabled()) {
        return true;
    }

    return !mButton->isChecked();
}

void CertificateItemWidget::setState(bool state)
{
    /* The internal state we get here is inverted for Ui reasons the logic
     * is if a certificate is selected for installation the button
     * is disabled (as this is the default) Only those that are
     * unselected have the enabled button. */
    mButton->blockSignals(true); // code did this and not the user
    mButton->setChecked(!state);
    mButton->blockSignals(false);
    if (mButton->isEnabled()) {
        mButton->setToolTip(mButton->property(!state ? "ToolTip_On" :
                    "ToolTip_Off").toString());
    }
}

Certificate CertificateItemWidget::certificate()
{
    return mCertificate;
}

void CertificateItemWidget::currentStateChanged(bool state)
{
    mButton->setToolTip(mButton->property(state ? "ToolTip_On" :
                "ToolTip_Off").toString());
    emit stateChanged(state, mCertificate);
}

http://wald.intevation.org/projects/trustbridge/