File indexing completed on 2024-05-12 04:39:28
0001 /* 0002 SPDX-FileCopyrightText: 2008 Aleix Pol <aleixpol@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "cmakecachedelegate.h" 0008 #include <debug.h> 0009 0010 #include <QLineEdit> 0011 #include <QCheckBox> 0012 #include <QComboBox> 0013 #include <KUrlRequester> 0014 #include <QUrl> 0015 0016 CMakeCacheDelegate::CMakeCacheDelegate(QObject * parent) 0017 : QItemDelegate(parent) 0018 { 0019 m_sample=new KUrlRequester(); 0020 } 0021 0022 CMakeCacheDelegate::~CMakeCacheDelegate() 0023 { 0024 delete m_sample; 0025 } 0026 0027 QWidget * CMakeCacheDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const 0028 { 0029 QWidget *ret=nullptr; 0030 if(index.column()==2) 0031 { 0032 QModelIndex typeIdx=index.sibling(index.row(), 1); 0033 QString type=typeIdx.model()->data(typeIdx, Qt::DisplayRole).toString(); 0034 if(type==QLatin1String("BOOL")) 0035 { 0036 auto* box=new QCheckBox(parent); 0037 connect(box, &QCheckBox::toggled, this, &CMakeCacheDelegate::checkboxToggled); 0038 ret = box; 0039 } 0040 else if(type==QLatin1String("STRING")) 0041 { 0042 QModelIndex stringsIdx=index.sibling(index.row(), 5); 0043 QString strings=typeIdx.model()->data(stringsIdx, Qt::DisplayRole).toString(); 0044 if (!strings.isEmpty()) { 0045 auto* comboBox = new QComboBox(parent); 0046 comboBox->setEditable(true); 0047 comboBox->addItems(strings.split(QLatin1Char(';'))); 0048 ret = comboBox; 0049 } else { 0050 ret=QItemDelegate::createEditor(parent, option, index); 0051 } 0052 } 0053 else if(type==QLatin1String("PATH") || type==QLatin1String("FILEPATH")) 0054 { 0055 auto *r=new KUrlRequester(parent); 0056 if(type==QLatin1String("FILEPATH")) 0057 r->setMode(KFile::File); 0058 else 0059 r->setMode(KFile::Directory | KFile::ExistingOnly); 0060 emit const_cast<CMakeCacheDelegate*>(this)->sizeHintChanged ( index ); 0061 qCDebug(CMAKE) << "EMITINT!" << index; 0062 ret=r; 0063 } 0064 else 0065 { 0066 ret=QItemDelegate::createEditor(parent, option, index); 0067 } 0068 0069 if(!ret) qCDebug(CMAKE) << "Did not recognize type " << type; 0070 } 0071 return ret; 0072 } 0073 0074 void CMakeCacheDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const 0075 { 0076 if(index.column()==2) 0077 { 0078 QModelIndex typeIdx=index.sibling(index.row(), 1); 0079 QString type=index.model()->data(typeIdx, Qt::DisplayRole).toString(); 0080 QString value=index.model()->data(index, Qt::DisplayRole).toString(); 0081 if(type==QLatin1String("BOOL")) 0082 { 0083 auto *boolean=qobject_cast<QCheckBox*>(editor); 0084 boolean->setCheckState(value==QLatin1String("ON") ? Qt::Checked : Qt::Unchecked); 0085 } 0086 else if(type==QLatin1String("PATH") || type==QLatin1String("FILEPATH")) 0087 { 0088 auto *url=qobject_cast<KUrlRequester*>(editor); 0089 url->setUrl(QUrl(value)); 0090 } 0091 else 0092 { 0093 QItemDelegate::setEditorData(editor, index); 0094 } 0095 } 0096 else 0097 qCDebug(CMAKE) << "Error. trying to edit a read-only field"; 0098 } 0099 0100 void CMakeCacheDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const 0101 { 0102 if(index.column()==2) 0103 { 0104 QModelIndex typeIdx=index.sibling(index.row(), 1); 0105 QString type=model->data(typeIdx, Qt::DisplayRole).toString(); 0106 QString value; 0107 if(type==QLatin1String("BOOL")) 0108 { 0109 auto *boolean=qobject_cast<QCheckBox*>(editor); 0110 value = boolean->isChecked() ? QStringLiteral("ON") : QStringLiteral("OFF"); 0111 } 0112 else if(type==QLatin1String("PATH") || type==QLatin1String("FILEPATH")) 0113 { 0114 auto *urlreq=qobject_cast<KUrlRequester*>(editor); 0115 value = urlreq->url().toDisplayString(QUrl::StripTrailingSlash | QUrl::PreferLocalFile); //CMake usually don't put it 0116 } 0117 else 0118 { 0119 QItemDelegate::setModelData(editor, model, index); 0120 return; 0121 } 0122 model->setData(index, value, Qt::DisplayRole); 0123 } 0124 else 0125 qCDebug(CMAKE) << "Error. trying to edit a read-only field"; 0126 } 0127 0128 void CMakeCacheDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const 0129 { 0130 if(index.column()==2) 0131 { 0132 QModelIndex typeIdx=index.sibling(index.row(), 1); 0133 QString type=index.model()->data(typeIdx, Qt::DisplayRole).toString(); 0134 if(type==QLatin1String("BOOL")) 0135 return; 0136 } 0137 QItemDelegate::paint(painter, option, index); 0138 } 0139 0140 QSize CMakeCacheDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index ) const 0141 { 0142 // qCDebug(CMAKE) << "calculant" << index << bool(option.state & QStyle::State_Editing); 0143 QSize ret=QItemDelegate::sizeHint(option, index); 0144 if(index.column()==2 && option.state & QStyle::State_Editing) 0145 { 0146 QModelIndex typeIdx=index.sibling(index.row(), 1); 0147 QString type=index.model()->data(typeIdx, Qt::DisplayRole).toString(); 0148 if(type==QLatin1String("PATH")) 0149 { 0150 ret.setHeight(m_sample->sizeHint().height()); 0151 } 0152 } 0153 return ret; 0154 } 0155 0156 void CMakeCacheDelegate::checkboxToggled() 0157 { 0158 // whenever the check box gets toggled, we directly want to set the 0159 // model data which is done by closing the editor which in turn 0160 // calls setModelData. otherwise, the behavior is quite confusing, see e.g. 0161 // https://bugs.kde.org/show_bug.cgi?id=304352 0162 auto* editor = qobject_cast<QCheckBox*>(sender()); 0163 Q_ASSERT(editor); 0164 emit closeEditor(editor); 0165 } 0166 0167 void CMakeCacheDelegate::closingEditor(QWidget * editor, QAbstractItemDelegate::EndEditHint hint) 0168 { 0169 Q_UNUSED(editor); 0170 Q_UNUSED(hint); 0171 qCDebug(CMAKE) << "closing..."; 0172 } 0173 0174 // void CMakeCacheDelegate::updateEditorGeometry ( QWidget * editor, const QStyleOptionViewItem & option, 0175 // const QModelIndex & index ) const 0176 // { 0177 // if(index.column()==2) 0178 // { 0179 // QModelIndex typeIdx=index.sibling(index.row(), 1); 0180 // QString type=index.model()->data(typeIdx, Qt::DisplayRole).toString(); 0181 // if(type=="PATH") 0182 // { 0183 // KUrlRequester* urlreq=qobject_cast<KUrlRequester*>(editor); 0184 // urlreq->setGeometry(QRect(option.rect.topLeft(), urlreq->sizeHint())); 0185 // return; 0186 // } 0187 // } 0188 // QItemDelegate::updateEditorGeometry( editor, option, index ); 0189 // } 0190 0191 #include "moc_cmakecachedelegate.cpp"