File indexing completed on 2022-11-22 13:00:59

0001 /*
0002     SPDX-FileCopyrightText: 2008 Daniel Laidig <d.laidig@gmx.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "buttondelegate.h"
0007 #include "dashboard.h"
0008 
0009 #include <KMessageBox>
0010 
0011 #include <KLocalizedString>
0012 #include <QAbstractItemDelegate>
0013 #include <QAbstractItemView>
0014 #include <QApplication>
0015 #include <QPainter>
0016 #include <QStandardItemModel>
0017 #include <QToolButton>
0018 #include <QUrl>
0019 
0020 const int margin = 5;
0021 const int iconSize = 22;
0022 
0023 ButtonDelegate::ButtonDelegate(QAbstractItemView *itemView, Dashboard *parent)
0024     : QStyledItemDelegate(itemView)
0025     , m_rightMargin(0)
0026     , m_buttonHeight(0)
0027     , m_dashboard(parent)
0028     , m_itemView(itemView)
0029 {
0030     m_editButton = new QToolButton(itemView->viewport());
0031     m_editButton->setIcon(QIcon::fromTheme(QStringLiteral("document-edit")));
0032     m_editButton->setToolTip(i18n("Open this vocabulary collection in the editor"));
0033     m_editButton->resize(m_editButton->sizeHint());
0034     m_editButton->setAutoRaise(true);
0035     m_editButton->hide();
0036     m_rightMargin = m_editButton->sizeHint().width() + margin;
0037     m_buttonHeight = m_editButton->sizeHint().height();
0038 
0039     connect(m_editButton, &QAbstractButton::clicked, this, &ButtonDelegate::slotEdit);
0040 }
0041 
0042 ButtonDelegate::~ButtonDelegate()
0043 {
0044 }
0045 
0046 void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0047 {
0048     QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
0049     if (option.state & QStyle::State_MouseOver) {
0050         m_editButton->show();
0051         m_editButton->move(QPoint(option.rect.right() - margin - m_editButton->sizeHint().width(),
0052                                   option.rect.top() + (option.rect.height() - m_editButton->height()) / 2 + 1));
0053     } else if (!hoveredIndex().isValid()) {
0054         m_editButton->hide();
0055     }
0056 
0057     QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
0058     int iconMargin = (option.rect.height() - iconSize) / 2;
0059     painter->drawPixmap(option.rect.x() + iconMargin, option.rect.y() + iconMargin, icon.pixmap(iconSize, iconSize));
0060 
0061     int rectX = option.rect.x() + iconMargin + iconSize + margin;
0062     QRect textRect(rectX, option.rect.y(), option.rect.width() - rectX - m_rightMargin, option.rect.height());
0063 
0064     QFontMetrics fm(option.font);
0065     QString elidedText = fm.elidedText(index.data().toString(), Qt::ElideRight, textRect.width());
0066     painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText);
0067 }
0068 
0069 QSize ButtonDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
0070 {
0071     Q_UNUSED(index);
0072     Q_UNUSED(option);
0073 
0074     return QSize(qMax(iconSize, m_buttonHeight), 32);
0075 }
0076 
0077 void ButtonDelegate::slotEdit()
0078 {
0079     const QModelIndex index = hoveredIndex();
0080     QUrl url = index.data(Qt::UserRole).toUrl();
0081     m_dashboard->slotOpenUrl(url);
0082 }
0083 
0084 void ButtonDelegate::slotPractice()
0085 {
0086     const QModelIndex index = hoveredIndex();
0087     QUrl url = index.data(Qt::UserRole).toUrl();
0088     m_dashboard->slotPracticeUrl(url);
0089 }
0090 
0091 QModelIndex ButtonDelegate::hoveredIndex() const
0092 {
0093     const QPoint pos = m_itemView->viewport()->mapFromGlobal(QCursor::pos());
0094     return m_itemView->indexAt(pos);
0095 }