File indexing completed on 2024-05-05 05:40:56
0001 /*************************************************************************** 0002 * Copyright (C) 2021 by Renaud Guezennec * 0003 * http://www.rolisteam.org/contact * 0004 * * 0005 * This software is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "actiondelegate.h" 0021 0022 #include <QDebug> 0023 #include <QHBoxLayout> 0024 #include <QLabel> 0025 #include <QPainter> 0026 #include <QToolButton> 0027 0028 #include "model/actiononlistmodel.h" 0029 0030 LabelWithOptions::LabelWithOptions(QWidget* parent) : QWidget(parent) 0031 { 0032 m_layout= new QHBoxLayout(); 0033 setLayout(m_layout); 0034 0035 m_label.reset(new QLabel()); 0036 m_layout->addWidget(m_label.get()); 0037 } 0038 LabelWithOptions::~LabelWithOptions()= default; 0039 void LabelWithOptions::setData(const QStringList icon, const QStringList name) 0040 { 0041 if(icon.size() != name.size()) 0042 return; 0043 0044 while(m_layout->count() > 1) 0045 { 0046 m_layout->removeItem(m_layout->itemAt(1)); 0047 } 0048 0049 int i= 0; 0050 for(const auto& iconPath : icon) 0051 { 0052 auto btn= new QToolButton(); 0053 btn->setIcon(QIcon::fromTheme(iconPath)); 0054 btn->setToolTip(name[i]); 0055 m_layout->addWidget(btn); 0056 ++i; 0057 } 0058 } 0059 0060 QString LabelWithOptions::text() const 0061 { 0062 return m_label->text(); 0063 } 0064 0065 void LabelWithOptions::setText(const QString& text) 0066 { 0067 if(this->text() == text) 0068 return; 0069 m_label->setText(text); 0070 emit textChanged(); 0071 } 0072 0073 // end of label 0074 0075 ActionDelegate::ActionDelegate(QObject* object) : m_label(new LabelWithOptions) 0076 { 0077 // m_label->setVisible(true); 0078 } 0079 0080 QWidget* ActionDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, 0081 const QModelIndex& index) const 0082 { 0083 0084 auto listIcon= index.data(ActionOnListModel::PossibleIcon).toStringList(); 0085 auto listname= index.data(ActionOnListModel::PossibleAction).toStringList(); 0086 0087 auto wid= new LabelWithOptions(parent); 0088 wid->setData(listIcon, listname); 0089 0090 return wid; 0091 } 0092 0093 QSize ActionDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const 0094 { 0095 QSize a= QStyledItemDelegate::sizeHint(option, index); 0096 a.setHeight(30); 0097 a.setWidth(150); 0098 return a; 0099 } 0100 0101 void ActionDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const 0102 { 0103 QStyleOptionViewItem opt(option); 0104 initStyleOption(&opt, index); 0105 0106 painter->save(); 0107 qDebug() << "Action dElegate: Paint"; 0108 auto listIcon= index.data(ActionOnListModel::PossibleIcon).toStringList(); 0109 auto listname= index.data(ActionOnListModel::PossibleAction).toStringList(); 0110 0111 if(option.state & QStyle::State_Selected) 0112 { 0113 painter->fillRect(option.rect, option.palette.highlight()); 0114 } 0115 0116 /*m_label->setData(listIcon, listname); 0117 m_label->setText(index.data().toString()); 0118 m_label->resize(option.rect.size()); 0119 qDebug() << option.rect.size() << index.data().toString(); 0120 0121 m_label->render(painter, QPoint(), QRegion(), QWidget::DrawChildren);*/ 0122 0123 auto textRect= option.rect; 0124 textRect.setRight(textRect.width() - (listIcon.size() * 32)); 0125 0126 painter->drawText(textRect, index.data().toString()); 0127 int i= 1; 0128 for(const auto& icon : listIcon) 0129 { 0130 auto pic= QIcon::fromTheme(icon); 0131 0132 painter->drawPixmap(option.rect.width() - (32 * i), option.rect.top(), pic.pixmap(QSize(25, 25))); 0133 ++i; 0134 } 0135 painter->restore(); 0136 }