1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/*
SPDX-FileCopyrightText: 2008 Aleix Pol <aleixpol@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "cmakecachedelegate.h"
#include <debug.h>
#include <QLineEdit>
#include <QCheckBox>
#include <QComboBox>
#include <KUrlRequester>
#include <QUrl>
CMakeCacheDelegate::CMakeCacheDelegate(QObject * parent)
: QItemDelegate(parent)
{
m_sample=new KUrlRequester();
}
CMakeCacheDelegate::~CMakeCacheDelegate()
{
delete m_sample;
}
QWidget * CMakeCacheDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
QWidget *ret=nullptr;
if(index.column()==2)
{
QModelIndex typeIdx=index.sibling(index.row(), 1);
QString type=typeIdx.model()->data(typeIdx, Qt::DisplayRole).toString();
if(type==QLatin1String("BOOL"))
{
auto* box=new QCheckBox(parent);
connect(box, &QCheckBox::toggled, this, &CMakeCacheDelegate::checkboxToggled);
ret = box;
}
else if(type==QLatin1String("STRING"))
{
QModelIndex stringsIdx=index.sibling(index.row(), 5);
QString strings=typeIdx.model()->data(stringsIdx, Qt::DisplayRole).toString();
if (!strings.isEmpty()) {
auto* comboBox = new QComboBox(parent);
comboBox->setEditable(true);
comboBox->addItems(strings.split(QLatin1Char(';')));
ret = comboBox;
} else {
ret=QItemDelegate::createEditor(parent, option, index);
}
}
else if(type==QLatin1String("PATH") || type==QLatin1String("FILEPATH"))
{
auto *r=new KUrlRequester(parent);
if(type==QLatin1String("FILEPATH"))
r->setMode(KFile::File);
else
r->setMode(KFile::Directory | KFile::ExistingOnly);
emit const_cast<CMakeCacheDelegate*>(this)->sizeHintChanged ( index );
qCDebug(CMAKE) << "EMITINT!" << index;
ret=r;
}
else
{
ret=QItemDelegate::createEditor(parent, option, index);
}
if(!ret) qCDebug(CMAKE) << "Did not recognize type " << type;
}
return ret;
}
void CMakeCacheDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
{
if(index.column()==2)
{
QModelIndex typeIdx=index.sibling(index.row(), 1);
QString type=index.model()->data(typeIdx, Qt::DisplayRole).toString();
QString value=index.model()->data(index, Qt::DisplayRole).toString();
if(type==QLatin1String("BOOL"))
{
auto *boolean=qobject_cast<QCheckBox*>(editor);
boolean->setCheckState(value==QLatin1String("ON") ? Qt::Checked : Qt::Unchecked);
}
else if(type==QLatin1String("PATH") || type==QLatin1String("FILEPATH"))
{
auto *url=qobject_cast<KUrlRequester*>(editor);
url->setUrl(QUrl(value));
}
else
{
QItemDelegate::setEditorData(editor, index);
}
}
else
qCDebug(CMAKE) << "Error. trying to edit a read-only field";
}
void CMakeCacheDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{
if(index.column()==2)
{
QModelIndex typeIdx=index.sibling(index.row(), 1);
QString type=model->data(typeIdx, Qt::DisplayRole).toString();
QString value;
if(type==QLatin1String("BOOL"))
{
auto *boolean=qobject_cast<QCheckBox*>(editor);
value = boolean->isChecked() ? QStringLiteral("ON") : QStringLiteral("OFF");
}
else if(type==QLatin1String("PATH") || type==QLatin1String("FILEPATH"))
{
auto *urlreq=qobject_cast<KUrlRequester*>(editor);
value = urlreq->url().toDisplayString(QUrl::StripTrailingSlash | QUrl::PreferLocalFile); //CMake usually don't put it
}
else
{
QItemDelegate::setModelData(editor, model, index);
return;
}
model->setData(index, value, Qt::DisplayRole);
}
else
qCDebug(CMAKE) << "Error. trying to edit a read-only field";
}
void CMakeCacheDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
if(index.column()==2)
{
QModelIndex typeIdx=index.sibling(index.row(), 1);
QString type=index.model()->data(typeIdx, Qt::DisplayRole).toString();
if(type==QLatin1String("BOOL"))
return;
}
QItemDelegate::paint(painter, option, index);
}
QSize CMakeCacheDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
// qCDebug(CMAKE) << "calculant" << index << bool(option.state & QStyle::State_Editing);
QSize ret=QItemDelegate::sizeHint(option, index);
if(index.column()==2 && option.state & QStyle::State_Editing)
{
QModelIndex typeIdx=index.sibling(index.row(), 1);
QString type=index.model()->data(typeIdx, Qt::DisplayRole).toString();
if(type==QLatin1String("PATH"))
{
ret.setHeight(m_sample->sizeHint().height());
}
}
return ret;
}
void CMakeCacheDelegate::checkboxToggled()
{
// whenever the check box gets toggled, we directly want to set the
// model data which is done by closing the editor which in turn
// calls setModelData. otherwise, the behavior is quite confusing, see e.g.
// https://bugs.kde.org/show_bug.cgi?id=304352
auto* editor = qobject_cast<QCheckBox*>(sender());
Q_ASSERT(editor);
emit closeEditor(editor);
}
void CMakeCacheDelegate::closingEditor(QWidget * editor, QAbstractItemDelegate::EndEditHint hint)
{
Q_UNUSED(editor);
Q_UNUSED(hint);
qCDebug(CMAKE) << "closing...";
}
// void CMakeCacheDelegate::updateEditorGeometry ( QWidget * editor, const QStyleOptionViewItem & option,
// const QModelIndex & index ) const
// {
// if(index.column()==2)
// {
// QModelIndex typeIdx=index.sibling(index.row(), 1);
// QString type=index.model()->data(typeIdx, Qt::DisplayRole).toString();
// if(type=="PATH")
// {
// KUrlRequester* urlreq=qobject_cast<KUrlRequester*>(editor);
// urlreq->setGeometry(QRect(option.rect.topLeft(), urlreq->sizeHint()));
// return;
// }
// }
// QItemDelegate::updateEditorGeometry( editor, option, index );
// }
#include "moc_cmakecachedelegate.cpp"
|