# HG changeset patch # User Sascha Wilde # Date 1396427468 -7200 # Node ID 9d2ac9b6a5b052d75c18e36a2423e2ac596b1129 # Parent f7471604bb3113dc84180cbbd3f54e8a5797c6e7# Parent 89e8783866f8278b13eccffa588f76773de1ffba Merged diff -r f7471604bb31 -r 9d2ac9b6a5b0 ui/CMakeLists.txt --- a/ui/CMakeLists.txt Wed Apr 02 10:30:49 2014 +0200 +++ b/ui/CMakeLists.txt Wed Apr 02 10:31:08 2014 +0200 @@ -20,6 +20,7 @@ ${CMAKE_CURRENT_SOURCE_DIR}/helpdialog.cpp ${CMAKE_CURRENT_SOURCE_DIR}/aboutdialog.cpp ${CMAKE_CURRENT_SOURCE_DIR}/statusdialog.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/certificateitemdelegate.cpp ${CMAKE_CURRENT_SOURCE_DIR}/installwrapper.cpp ${CERTIFICATELIST_SOURCES} ${DOWNLOADER_SOURCES} diff -r f7471604bb31 -r 9d2ac9b6a5b0 ui/certificate.h --- a/ui/certificate.h Wed Apr 02 10:30:49 2014 +0200 +++ b/ui/certificate.h Wed Apr 02 10:31:08 2014 +0200 @@ -19,6 +19,13 @@ { public: + enum Status { + InstallNew = 1, + InstallOld, + RemoveNew, + RemoveOld + }; + /** @brief construct a certificate from a line of a certificate list. * * The first two characters of the string are expected to be diff -r f7471604bb31 -r 9d2ac9b6a5b0 ui/certificateitemdelegate.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/certificateitemdelegate.cpp Wed Apr 02 10:31:08 2014 +0200 @@ -0,0 +1,90 @@ +#include + +#include "certificate.h" +#include "certificateitemdelegate.h" + +void CertificateItemDelegate::paint(QPainter *painter, + const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + // Save the current painter. + painter->save(); + int status = index.data(Qt::UserRole + 1).toInt(); + if (status == 0) { + // This status is not known, so draw the default item. + QStyledItemDelegate::paint(painter, option, index); + painter->restore(); + return; + } + + if (status == Certificate::InstallNew) { + //Set the icon and use bold and bigger font to highlight this item. + QIcon *icon = new QIcon(":/img/list-add.png"); + QFont *font = new QFont(); + font->setBold(true); + font->setPointSize(font->pointSize() + 1); + drawItem(painter, option, index, icon, font); + } + else if (status == Certificate::InstallOld) { + // Set the icon and use the default font for this item. + QIcon *icon = new QIcon(":/img/list-add.png"); + QFont *font = new QFont(); + drawItem(painter, option, index, icon, font); + } + else if (status == Certificate::RemoveNew) { + //Set the icon and use bold and bigger font to highlight this item. + QIcon *icon = new QIcon(":/img/list-remove.png"); + QFont *font = new QFont(); + font->setBold(true); + font->setPointSize(font->pointSize() + 1); + drawItem(painter, option, index, icon, font); + } + else if (status == Certificate::RemoveOld) { + // Set the icon and use the default font for this item. + QIcon *icon = new QIcon(":/img/list-remove.png"); + QFont *font = new QFont(); + drawItem(painter, option, index, icon, font); + } + else { + // Draw the default item. + QStyledItemDelegate::paint(painter, option, index); + } + // Restore the painter to have an unmodified painter for the next draw + // action. + painter->restore(); + return; +} + +void CertificateItemDelegate::drawItem(QPainter *painter, + const QStyleOptionViewItem &option, const QModelIndex &index, + QIcon *icon, QFont *font) const +{ + // Get temporary style option to draw a checkbox only. + QStyleOptionViewItem opt = option; + // Initialize the style options with the temporary option object. + initStyleOption(&opt, index); + // Clear all text to draw the checkbox only. + opt.text.clear(); + + // Draw highlighted background. + if (option.state & QStyle::State_Selected) { + painter->fillRect(option.rect, option.palette.highlight()); + } + + // Draw the checkbox control with the temporary options. + QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter); + + // Draw the icon. + int iconSpace = 25; + if (!icon->isNull()) { + QRect rect = option.rect.adjusted(25, 0, 0, 0); + icon->paint(painter, rect, Qt::AlignVCenter|Qt::AlignLeft); + iconSpace = 50; + } + + // Draw the text using the given font. + QString text = index.data().toString(); + QRect rect = option.rect.adjusted(iconSpace, 0, 0, 0); + painter->setFont(*font); + painter->drawText(rect.left(), rect.top(), rect.width(), rect.height(), + Qt::AlignVCenter|Qt::AlignLeft, text, &rect); +} diff -r f7471604bb31 -r 9d2ac9b6a5b0 ui/certificateitemdelegate.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/certificateitemdelegate.h Wed Apr 02 10:31:08 2014 +0200 @@ -0,0 +1,43 @@ + +#ifndef CERTIFICATELISTITEM_H +#define CERTIFICATELISTITEM_H +/** + * @file certificateitemdelegate.h + * @brief Item delegate drawing custom certificate items in list views. + * + */ + +#include + +class CertificateItemDelegate : public QStyledItemDelegate +{ +Q_OBJECT + +public: + CertificateItemDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent){} + + /** + * @brief Renders the delegate using the given painter and options. + * + * @param painter The painter to draw the item. + * @param option The style options. + * @param index The model index of the item to draw. + */ + void paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const; + +private: + + /** + * @brief Draw the item using the given parameters. + * + * @param painter The painter to draw the item. + * @param option The style options. + * @param index The model index of the item to draw. + * @param icon The icon to display. + * @param font The font used to draw text. + */ + void drawItem(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index, QIcon *icon, QFont *font) const; +}; +#endif diff -r f7471604bb31 -r 9d2ac9b6a5b0 ui/mainwindow.cpp --- a/ui/mainwindow.cpp Wed Apr 02 10:30:49 2014 +0200 +++ b/ui/mainwindow.cpp Wed Apr 02 10:31:08 2014 +0200 @@ -32,6 +32,7 @@ #include "helpdialog.h" #include "aboutdialog.h" #include "statusdialog.h" +#include "certificateitemdelegate.h" MainWindow::MainWindow() { createActions(); @@ -39,6 +40,7 @@ createMenuBar(); createContent(); qRegisterMetaType("SSLConnection::ErrorCode"); + qRegisterMetaType("Certificate::Status"); connect(mTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); @@ -245,6 +247,7 @@ // The certificate list QGroupBox *certBox = new QGroupBox(tr("Managed Certificates")); certificateList = new QListWidget; + certificateList->setItemDelegate(new CertificateItemDelegate); connect(certificateList, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(showDetails(QListWidgetItem*))); certLayout->addWidget(certificateList); @@ -301,6 +304,7 @@ { qDebug() << "display certificates"; certificateList->clear(); + int i = 0; foreach (const Certificate &cert, mListToInstall.getCertificates()) { if (!cert.isValid()) { qWarning() << "Invalid certificate in list"; @@ -308,10 +312,33 @@ } QListWidgetItem* item = new QListWidgetItem(cert.shortDescription()); item->setData(Qt::UserRole, cert.details()); - QIcon *certIcon = cert.isInstallCert() ? new QIcon(":/img/list-add.png"): - new QIcon(":/img/list-remove.png"); - item->setIcon(*certIcon); + if (cert.isInstallCert()) { + // This if statements is for testing! @TODO Remove this! + if (i <= 2) { + item->setData(Qt::UserRole + 1, Certificate::InstallOld); + item->setFlags(item->flags() | Qt::ItemIsUserCheckable); + item->setCheckState(Qt::Checked); + } + else { + item->setData(Qt::UserRole + 1, Certificate::InstallNew); + item->setFlags(item->flags() | Qt::ItemIsUserCheckable); + item->setCheckState(Qt::Checked); + } + } + else { + // This if statements is for testing! @TODO Remove this! + if (i > 35) { + item->setData(Qt::UserRole + 1, Certificate::RemoveNew); + item->setFlags(item->flags() | Qt::ItemIsUserCheckable); + item->setCheckState(Qt::Checked); + } + else { + item->setData(Qt::UserRole + 1, Certificate::RemoveOld); + item->setFlags(item->flags() | Qt::ItemIsUserCheckable); + } + } certificateList->addItem(item); + i++; } }