diff ui/certificatelistwidget.cpp @ 743:a467204a35f5

Rework User Interface. Certificate lists are now plain lists of widgets and no longer a ListWidget. Details are no longer shown extra but included in the list. List entries are somewhat larger accordingly.
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 04 Jul 2014 16:15:59 +0200
parents 3d669400104a
children 9bfaced5cf59
line wrap: on
line diff
--- a/ui/certificatelistwidget.cpp	Fri Jul 04 16:13:40 2014 +0200
+++ b/ui/certificatelistwidget.cpp	Fri Jul 04 16:15:59 2014 +0200
@@ -8,63 +8,16 @@
 #include "certificatelistwidget.h"
 #include <QDebug>
 #include <QVBoxLayout>
-#include <QHBoxLayout>
 #include <QGroupBox>
 #include <QLabel>
+#include <QApplication>
 
 #include "certificateitemwidget.h"
 
 CertificateListWidget::CertificateListWidget(QWidget *parent, Qt::WindowFlags flags) :
     QWidget(parent, flags)
 {
-    setupGUI();
-}
-
-void CertificateListWidget::setupGUI()
-{
-    QHBoxLayout *mainLayout = new QHBoxLayout;
-    QVBoxLayout *detailMainLayout = new QVBoxLayout;
-    mCertificateList = new QListWidget;
-    mCertificateList->setFixedWidth(250);
-    connect(mCertificateList,
-        SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
-        this,
-        SLOT(updateDetails(QListWidgetItem*)));
-
-    QHBoxLayout *detailLayout = new QHBoxLayout;
-    QVBoxLayout *detailLabelLayout = new QVBoxLayout;
-    QVBoxLayout *detailContentLayout = new QVBoxLayout;
-    QGroupBox *detailBox = new QGroupBox(tr("Details"));
-    QLabel *subjectCN = new QLabel(tr("Subject Common Name:"));
-    QLabel *subjectOU = new QLabel(tr("Subject Organisation:"));
-    QLabel *validFrom = new QLabel(tr("Valid from:"));
-    QLabel *validTo = new QLabel(tr("Valid to:"));
-    QLabel *fingerprint = new QLabel(tr("Fingerprint (SHA1):"));
-    detailLabelLayout->addWidget(subjectCN);
-    detailLabelLayout->addWidget(subjectOU);
-    detailLabelLayout->addWidget(validFrom);
-    detailLabelLayout->addWidget(validTo);
-    detailLabelLayout->addWidget(fingerprint);
-    mSubjectCN = new QLabel(tr(""));
-    mSubjectO = new QLabel(tr(""));
-    mValidFrom = new QLabel(tr(""));
-    mValidTo = new QLabel(tr(""));
-    mFingerprint = new QLabel(tr(""));
-    mFingerprint->setFont(QFont("DejaVu Sans Mono"));
-    detailContentLayout->addWidget(mSubjectCN);
-    detailContentLayout->addWidget(mSubjectO);
-    detailContentLayout->addWidget(mValidFrom);
-    detailContentLayout->addWidget(mValidTo);
-    detailContentLayout->addWidget(mFingerprint);
-    detailLayout->addLayout(detailLabelLayout);
-    detailLayout->addLayout(detailContentLayout);
-    detailBox->setLayout(detailLayout);
-    detailMainLayout->addWidget(detailBox);
-    detailMainLayout->addStretch(1);
-
-    mainLayout->addWidget(mCertificateList);
-    mainLayout->addLayout(detailMainLayout);
-    this->setLayout(mainLayout);
+    setLayout(&mLayout);
 }
 
 void CertificateListWidget::addCertificate(
@@ -74,34 +27,25 @@
     const QString &installLabel,
     const QString &removeLabel)
 {
-    QListWidgetItem* item = new QListWidgetItem(mCertificateList);
-    item->setData(Qt::UserRole,
-        QVariant::fromValue(certificate));
-    mCertificateList->addItem(item);
     CertificateItemWidget *widget =
-        new CertificateItemWidget(mCertificateList, certificate, state, editable,
+        new CertificateItemWidget(this, certificate, state, editable,
                 installLabel, removeLabel);
     connect(widget, SIGNAL(stateChanged(bool, const Certificate&)),
         this, SLOT(certStateChanged(bool, const Certificate&)));
-    item->setSizeHint(widget->minimumSizeHint());
-    mCertificateList->setItemWidget(item, widget);
+
+    mCertificateWidgets << widget;
+    mLayout.addWidget(widget);
     emit certListChanged(-1);
 }
 
-void CertificateListWidget::addCertificates(const QList<Certificate> &list)
-{
-
-}
-
 void CertificateListWidget::removeCertificate(const Certificate &cert)
 {
-    for (int i = 0; i < mCertificateList->count(); i++) {
-        QListWidgetItem *item = mCertificateList->item(i);
-        CertificateItemWidget *itemWidget =
-            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
-        if (itemWidget->certificate() == cert) {
-            QListWidgetItem* item = mCertificateList->takeItem(i);
-            delete(item);
+    for (int i = 0; i < mCertificateWidgets.size(); i++) {
+        if (mCertificateWidgets[i]->certificate() == cert) {
+            mLayout.removeWidget(mCertificateWidgets[i]);
+            delete(mCertificateWidgets[i]);
+            mCertificateWidgets.removeAt(i);
+            break;
         }
     }
     emit certListChanged(-1);
@@ -109,29 +53,27 @@
 
 void CertificateListWidget::clear()
 {
-    mCertificateList->clear();
+    foreach (CertificateItemWidget * item, mCertificateWidgets) {
+        mLayout.removeWidget(item);
+        delete item;
+    }
+    mCertificateWidgets.clear();
 }
 
 QStringList CertificateListWidget::certificates()
 {
     QStringList list;
-    for (int i = 0; i < mCertificateList->count(); i++) {
-        QListWidgetItem *item = mCertificateList->item(i);
-        CertificateItemWidget *itemWidget =
-            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
-        list <<  itemWidget->certificate().base64Line();
+    foreach (CertificateItemWidget * item, mCertificateWidgets) {
+        list <<  item->certificate().base64Line();
     }
     return list;
 }
 
 QStringList CertificateListWidget::selectedCertificates() {
     QStringList list;
-    for (int i = 0; i < mCertificateList->count(); i++) {
-        QListWidgetItem *item = mCertificateList->item(i);
-        CertificateItemWidget *itemWidget =
-            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
-        if (itemWidget->state()) {
-            list << itemWidget->certificate().base64Line();
+    foreach (CertificateItemWidget * item, mCertificateWidgets) {
+        if (item->state()) {
+            list << item->certificate().base64Line();
         }
     }
     return list;
@@ -139,12 +81,9 @@
 
 QStringList CertificateListWidget::unselectedCertificates() {
     QStringList list;
-    for (int i = 0; i < mCertificateList->count(); i++) {
-        QListWidgetItem *item = mCertificateList->item(i);
-        CertificateItemWidget *itemWidget =
-            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
-        if (!itemWidget->state()) {
-            list << itemWidget->certificate().base64Line();
+    foreach (CertificateItemWidget * item, mCertificateWidgets) {
+        if (!item->state()) {
+            list << item->certificate().base64Line();
         }
     }
     return list;
@@ -153,65 +92,34 @@
 QList<Certificate> CertificateListWidget::certificateList()
 {
     QList<Certificate> list;
-    for (int i = 0; i < mCertificateList->count(); i++) {
-        QListWidgetItem *item = mCertificateList->item(i);
-        CertificateItemWidget *itemWidget =
-            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
-        list << itemWidget->certificate();
+    foreach (CertificateItemWidget * item, mCertificateWidgets) {
+        list <<  item->certificate();
     }
-
     return list;
 }
 
 void CertificateListWidget::setCertState(bool state, const Certificate &cert)
 {
-    for (int i = 0; i < mCertificateList->count(); i++) {
-        QListWidgetItem *item = mCertificateList->item(i);
-        CertificateItemWidget *itemWidget =
-            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
-        if (itemWidget->certificate() == cert &&
-            itemWidget->state() != state) {
-            itemWidget->setState(state);
+    foreach (CertificateItemWidget * item, mCertificateWidgets) {
+        if (item->certificate() == cert &&
+            item->state() != state) {
+            item->setState(state);
         }
     }
 }
 
-void CertificateListWidget::updateDetails(QListWidgetItem *item)
-{
-    if (item == NULL) {
-        return;
-    }
-    Certificate cert = item->data(Qt::UserRole).value<Certificate>();
-    mSubjectCN->setText(cert.subjectCN());
-    mSubjectO->setText(cert.subjectO());
-    mValidFrom->setText(cert.validFrom().toString());
-    mValidTo->setText(cert.validTo().toString());
-    mFingerprint->setText(cert.fingerprint());
-}
 
 void CertificateListWidget::certStateChanged(bool state, const Certificate &cert)
 {
-    int selected = 0;
-    for (int i = 0; i < mCertificateList->count(); i++) {
-        QListWidgetItem *item = mCertificateList->item(i);
-        CertificateItemWidget *itemWidget =
-            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
-        if (itemWidget->state()) {
-            selected++;
-        }
-    }
-    emit certListChanged(selected);
+    emit certListChanged(-1);
     emit certChanged(state, cert);
 }
 
 int CertificateListWidget::selectedCertCount()
 {
     int selected = 0;
-    for (int i = 0; i < mCertificateList->count(); i++) {
-        QListWidgetItem *item = mCertificateList->item(i);
-        CertificateItemWidget *itemWidget =
-            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
-        if (itemWidget->state()) {
+    foreach (CertificateItemWidget * item, mCertificateWidgets) {
+        if (item->state()) {
             selected++;
         }
     }
@@ -220,23 +128,10 @@
 
 bool CertificateListWidget::contains(const Certificate &cert)
 {
-    for (int i = 0; i < mCertificateList->count(); i++) {
-        QListWidgetItem *item = mCertificateList->item(i);
-        CertificateItemWidget *itemWidget =
-            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
-        if (itemWidget->certificate() == cert) {
+    foreach (CertificateItemWidget * item, mCertificateWidgets) {
+        if (item->certificate() == cert) {
             return true;
         }
     }
     return false;
 }
-
-void CertificateListWidget::setSelected(int index)
-{
-    if (index <= 0) {
-        index = 0;
-    }
-    mCertificateList->setFocus();
-    mCertificateList->item(index)->setSelected(true);
-    mCertificateList->setCurrentRow(index);
-}

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