File indexing completed on 2024-05-05 05:40:56
0001 /************************************************************************* 0002 * Copyright (C) 2021 by Renaud Guezennec * 0003 * * 0004 * https://rolisteam.org/ * 0005 * * 0006 * rolisteam is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published * 0008 * by the Free Software Foundation; either version 2 of the License, * 0009 * or (at your option) any later version. * 0010 * * 0011 * This program is distributed in the hope that it will be useful, * 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0014 * GNU General Public License for more details. * 0015 * * 0016 * You should have received a copy of the GNU General Public License * 0017 * along with this program; if not, write to the * 0018 * Free Software Foundation, Inc., * 0019 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0020 *************************************************************************/ 0021 #include "checkboxdelegate.h" 0022 0023 #include <QPainter> 0024 0025 #include "customs/centeredcheckbox.h" 0026 0027 namespace rwidgets 0028 { 0029 CheckBoxDelegate::CheckBoxDelegate(bool aRedCheckBox, QObject* parent) : m_editor(new CenteredCheckBox) 0030 { 0031 Q_UNUSED(aRedCheckBox) 0032 Q_UNUSED(parent) 0033 // m_editor= new CenteredCheckBox(); 0034 // m_editor->setParent(parent); 0035 connect(m_editor.get(), &CenteredCheckBox::commitEditor, this, &CheckBoxDelegate::commitEditor); 0036 } 0037 CheckBoxDelegate::~CheckBoxDelegate()= default; 0038 QWidget* CheckBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, 0039 const QModelIndex& index) const 0040 { 0041 Q_UNUSED(option) 0042 Q_UNUSED(index) 0043 CenteredCheckBox* cb= new CenteredCheckBox(parent); 0044 return cb; 0045 } 0046 void CheckBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const 0047 { 0048 CenteredCheckBox* cb= qobject_cast<CenteredCheckBox*>(editor); 0049 bool checked= index.data().toBool(); 0050 cb->setCheckedDelegate(!checked); 0051 } 0052 void CheckBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const 0053 { 0054 CenteredCheckBox* cb= qobject_cast<CenteredCheckBox*>(editor); 0055 model->setData(index, cb->isCheckedDelegate()); 0056 QStyledItemDelegate::setEditorData(editor, index); 0057 } 0058 QSize CheckBoxDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const 0059 { 0060 QSize a= QStyledItemDelegate::sizeHint(option, index); 0061 a.setHeight(30); 0062 a.setWidth(150); 0063 return a; 0064 } 0065 0066 void CheckBoxDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const 0067 { 0068 painter->save(); 0069 0070 if(!index.isValid()) 0071 { 0072 return; 0073 } 0074 0075 // QStyleOptionViewItemV4 opt = option; 0076 // QStyledItemDelegate::initStyleOption(&option, index); 0077 0078 QVariant var= index.data(); 0079 0080 bool checked= var.toBool(); 0081 // QVariant color= index.data(Qt::BackgroundRole); 0082 0083 if(option.state & QStyle::State_Selected) 0084 { 0085 painter->fillRect(option.rect, option.palette.highlight()); 0086 } 0087 0088 m_editor->setCheckedDelegate(checked); 0089 0090 m_editor->resize(option.rect.size()); 0091 m_editor->setAutoFillBackground(false); 0092 painter->translate(option.rect.topLeft()); 0093 m_editor->render(painter, QPoint(), QRegion(), QWidget::DrawChildren); 0094 0095 painter->restore(); 0096 } 0097 void CheckBoxDelegate::commitEditor() 0098 { 0099 CenteredCheckBox* editor= qobject_cast<CenteredCheckBox*>(sender()); 0100 // std::cout<<"commitEditor "<<(editor==m_editor)<<" "<<editor->isCheckedDelegate()<<std::endl; 0101 emit commitData(editor); 0102 } 0103 } // namespace rwidgets