From 851c7b891e5824f5e420f9a4052d335b7edf682b Mon Sep 17 00:00:00 2001 From: Janek Bevendorff Date: Tue, 21 Feb 2017 21:34:13 +0100 Subject: [PATCH] Show icons in vertical tab bar TODO: use the correct icons, move vertical tab bar into separate widget --- src/gui/EditWidget.cpp | 39 +++++++++++++++++ src/gui/EditWidget.h | 12 +++++- src/gui/EditWidget.ui | 57 ++++++++++++++++++++++++- src/gui/MainWindow.cpp | 2 +- src/gui/entry/EditEntryWidget_p.h | 69 +++++++++++++++++++++++++------ src/http/OptionDialog.ui | 2 +- 6 files changed, 162 insertions(+), 19 deletions(-) diff --git a/src/gui/EditWidget.cpp b/src/gui/EditWidget.cpp index ef29f0132..157848eb9 100644 --- a/src/gui/EditWidget.cpp +++ b/src/gui/EditWidget.cpp @@ -18,6 +18,8 @@ #include "EditWidget.h" #include "ui_EditWidget.h" +#include "core/FilePath.h" + EditWidget::EditWidget(QWidget* parent) : DialogyWidget(parent) , m_ui(new Ui::EditWidget()) @@ -37,6 +39,11 @@ EditWidget::EditWidget(QWidget* parent) connect(m_ui->buttonBox, SIGNAL(accepted()), SIGNAL(accepted())); connect(m_ui->buttonBox, SIGNAL(rejected()), SIGNAL(rejected())); + + connect(m_ui->scrollUp, SIGNAL(clicked()), SLOT(scrollCategoriesUp())); + connect(m_ui->scrollDown, SIGNAL(clicked()), SLOT(scrollCategoriesDown())); + connect(m_ui->categoryList->verticalScrollBar(), SIGNAL(valueChanged(int)), SLOT(updateCategoryScrollButtons())); + connect(m_ui->categoryList->verticalScrollBar(), SIGNAL(rangeChanged(int, int)), SLOT(updateCategoryScrollButtons())); } EditWidget::~EditWidget() @@ -46,6 +53,8 @@ EditWidget::~EditWidget() void EditWidget::add(const QString& labelText, QWidget* widget) { m_ui->categoryList->addItem(labelText); + QListWidgetItem* item = m_ui->categoryList->item(m_ui->categoryList->count() - 1); + item->setIcon(FilePath::instance()->icon("apps", "keepassxc")); m_ui->stackedWidget->addWidget(widget); } @@ -100,3 +109,33 @@ void EditWidget::hideMessage() m_ui->messageWidget->animatedHide(); } } + +void EditWidget::updateCategoryScrollButtons() +{ + m_ui->scrollUp->setEnabled(m_ui->categoryList->verticalScrollBar()->value() != 0); + m_ui->scrollDown->setEnabled(m_ui->categoryList->verticalScrollBar()->value() + != m_ui->categoryList->verticalScrollBar()->maximum()); + + m_ui->scrollUp->setVisible(m_ui->categoryList->verticalScrollBar()->maximum() > 0); + m_ui->scrollDown->setVisible(m_ui->scrollUp->isVisible()); +} + +void EditWidget::showEvent(QShowEvent* event) +{ + QWidget::showEvent(event); + updateCategoryScrollButtons(); +} + +void EditWidget::scrollCategoriesUp() +{ + m_ui->categoryList->verticalScrollBar()->setValue( + m_ui->categoryList->verticalScrollBar()->value() - m_ui->categoryList->verticalScrollBar()->pageStep() + ); +} + +void EditWidget::scrollCategoriesDown() +{ + m_ui->categoryList->verticalScrollBar()->setValue( + m_ui->categoryList->verticalScrollBar()->value() + m_ui->categoryList->verticalScrollBar()->pageStep() + ); +} diff --git a/src/gui/EditWidget.h b/src/gui/EditWidget.h index 6f2c8f2dc..398a04600 100644 --- a/src/gui/EditWidget.h +++ b/src/gui/EditWidget.h @@ -19,6 +19,8 @@ #define KEEPASSX_EDITWIDGET_H #include +#include +#include #include "gui/DialogyWidget.h" #include "gui/MessageWidget.h" @@ -45,11 +47,17 @@ public: void setReadOnly(bool readOnly); bool readOnly() const; -Q_SIGNALS: +protected: + void showEvent(QShowEvent* event) override; + +signals: void accepted(); void rejected(); -protected Q_SLOTS: +protected slots: + void updateCategoryScrollButtons(); + void scrollCategoriesDown(); + void scrollCategoriesUp(); void showMessage(const QString& text, MessageWidget::MessageType type); void hideMessage(); diff --git a/src/gui/EditWidget.ui b/src/gui/EditWidget.ui index 64baf7032..7ab9b143f 100644 --- a/src/gui/EditWidget.ui +++ b/src/gui/EditWidget.ui @@ -40,7 +40,58 @@ - + + + 0 + + + + + + 0 + 0 + + + + + 16777215 + 15 + + + + + + + Qt::UpArrow + + + + + + + + + + + 0 + 0 + + + + + 16777215 + 15 + + + + + + + Qt::DownArrow + + + + @@ -52,7 +103,7 @@ - + 5 @@ -82,6 +133,8 @@ categoryList + scrollUp + scrollDown diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index e9a05e5d1..7225c4c5a 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -62,7 +62,7 @@ class HttpPlugin: public ISettingsPage //delete m_service; } virtual QString name() { - return QObject::tr("Http"); + return QObject::tr("Browser Integration"); } virtual QWidget * createWidget() { OptionDialog * dlg = new OptionDialog(); diff --git a/src/gui/entry/EditEntryWidget_p.h b/src/gui/entry/EditEntryWidget_p.h index cdae8bd3d..34232d25e 100644 --- a/src/gui/entry/EditEntryWidget_p.h +++ b/src/gui/entry/EditEntryWidget_p.h @@ -22,34 +22,73 @@ #include #include #include +#include class CategoryListViewDelegate : public QStyledItemDelegate { public: - explicit CategoryListViewDelegate(QObject* parent) : QStyledItemDelegate(parent) {} + explicit CategoryListViewDelegate(QListView* parent = nullptr) + : QStyledItemDelegate(parent), m_size(96, 96) + {} - QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const +protected: + void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { - QSize size = QStyledItemDelegate::sizeHint(option, index); - size.setHeight(qMax(size.height(), 22)); - return size; + QStyleOptionViewItem opt = option; + initStyleOption(&opt, index); + + painter->save(); + + QIcon icon = opt.icon; + QSize iconSize = opt.icon.actualSize(QSize(32, 32)); + opt.icon = QIcon(); + opt.decorationAlignment = Qt::AlignHCenter | Qt::AlignVCenter; + opt.decorationPosition = QStyleOptionViewItem::Top; + + QStyle* style = opt.widget ? opt.widget->style() : QApplication::style(); + style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); + + QRect fontRect = painter->fontMetrics().boundingRect( + QRect(0, 0, m_size.width(), m_size.height()), Qt::AlignHCenter | Qt::AlignBottom | Qt::TextWordWrap, opt.text); + + int paddingTop = fontRect.height() < 30 ? 15 : 10; + int left = opt.rect.left() + opt.rect.width() / 2 - iconSize.width() / 2; + painter->drawPixmap(left, opt.rect.top() + paddingTop, icon.pixmap(iconSize)); + + painter->restore(); } + + QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override + { + Q_UNUSED(option); + Q_UNUSED(index); + return m_size; + } + +private: + QSize m_size; }; class CategoryListWidget : public QListWidget { public: - explicit CategoryListWidget(QWidget* parent = 0) : QListWidget(parent) + explicit CategoryListWidget(QWidget* parent = 0) + : QListWidget(parent) { - setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); + setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); setItemDelegate(new CategoryListViewDelegate(this)); + setMovement(QListView::Static); + setViewMode(QListWidget::IconMode); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setWordWrap(true); } - - virtual QSize sizeHint() const +protected: + QSize sizeHint() const override { QSize sizeHint = QListWidget::sizeHint(); - int width = sizeHintForColumn(0) + frameWidth() * 2 + 5; + int width = sizeHintForColumn(0) + frameWidth() * 2; if (verticalScrollBar()->isVisible()) { width += verticalScrollBar()->width(); } @@ -57,6 +96,11 @@ public: return sizeHint; } + + QSize minimumSizeHint() const override + { + return QSize(sizeHint().width(), sizeHintForRow(0) * 2); + } }; class AttributesListView : public QListView @@ -65,14 +109,13 @@ public: explicit AttributesListView(QWidget* parent = 0) : QListView(parent) { setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); - setItemDelegate(new CategoryListViewDelegate(this)); } - virtual QSize sizeHint() const + QSize sizeHint() const override { QSize sizeHint = QListView::sizeHint(); - int width = sizeHintForColumn(0) + frameWidth() * 2 + 5; + int width = sizeHintForColumn(0) + frameWidth() * 2; if (verticalScrollBar()->isVisible()) { width += verticalScrollBar()->width(); } diff --git a/src/http/OptionDialog.ui b/src/http/OptionDialog.ui index ab8047db4..d55ceecdf 100644 --- a/src/http/OptionDialog.ui +++ b/src/http/OptionDialog.ui @@ -148,7 +148,7 @@ Only entries with the same scheme (http://, https://, ftp://, ...) are returned< color: rgb(255, 0, 0); - Activate the following only, if you know what you are doing! + Activate the following only if you know what you are doing!