From f4ff420036656e453c26d1502bf1b8f96f4cea59 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Thu, 14 Sep 2017 15:18:50 +0200 Subject: CMake: Improve delegates for CMake configuration Change-Id: Ib1d2bfca1b2faafd36c53f24c6649e73ee0af190 Reviewed-by: Tim Jenssen --- .../configmodelitemdelegate.cpp | 128 +++++++++++++++------ 1 file changed, 93 insertions(+), 35 deletions(-) (limited to 'src/plugins/cmakeprojectmanager/configmodelitemdelegate.cpp') diff --git a/src/plugins/cmakeprojectmanager/configmodelitemdelegate.cpp b/src/plugins/cmakeprojectmanager/configmodelitemdelegate.cpp index b2b19b837e..f5d24b5174 100644 --- a/src/plugins/cmakeprojectmanager/configmodelitemdelegate.cpp +++ b/src/plugins/cmakeprojectmanager/configmodelitemdelegate.cpp @@ -19,59 +19,117 @@ #include "configmodelitemdelegate.h" #include "configmodel.h" +#include +#include + #include +#include +#include +#include namespace CMakeProjectManager { -ConfigModelItemDelegate::ConfigModelItemDelegate(QObject* parent) +ConfigModelItemDelegate::ConfigModelItemDelegate(const Utils::FileName &base, QObject* parent) : QStyledItemDelegate(parent) + , m_base(base) { } -ConfigModelItemDelegate::~ConfigModelItemDelegate() -{ } +QWidget *ConfigModelItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, + const QModelIndex &index) const -QWidget* ConfigModelItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { - // ComboBox ony in column 2 - if (index.column() != 1) - return QStyledItemDelegate::createEditor(parent, option, index); - - auto model = index.model(); - auto values = model->data(index, ConfigModel::ItemValuesRole).toStringList(); - if (values.isEmpty()) - return QStyledItemDelegate::createEditor(parent, option, index); + if (index.column() == 1) { + ConfigModel::DataItem data = ConfigModel::dataItemFromIndex(index); + if (data.type == ConfigModel::DataItem::FILE || data.type == ConfigModel::DataItem::DIRECTORY) { + auto edit = new Utils::PathChooser(parent); + edit->setFocusPolicy(Qt::StrongFocus); + edit->setBaseFileName(m_base); + edit->setAutoFillBackground(true); + if (data.type == ConfigModel::DataItem::FILE) { + edit->setExpectedKind(Utils::PathChooser::File); + edit->setPromptDialogTitle(tr("Select a file for %1").arg(data.key)); + } else { + edit->setExpectedKind(Utils::PathChooser::Directory); + edit->setPromptDialogTitle(tr("Select a directory for %1").arg(data.key)); + } + return edit; + } else if (!data.values.isEmpty()) { + auto edit = new QComboBox(parent); + edit->setFocusPolicy(Qt::StrongFocus); + for (const QString &s : Utils::asConst(data.values)) + edit->addItem(s); + return edit; + } else if (data.type == ConfigModel::DataItem::BOOLEAN) { + auto edit = new QCheckBox(parent); + edit->setFocusPolicy(Qt::StrongFocus); + return edit; + } else if (data.type == ConfigModel::DataItem::STRING) { + auto edit = new QLineEdit(parent); + edit->setFocusPolicy(Qt::StrongFocus); + return edit; + } + } - // Create the combobox and populate it - auto cb = new QComboBox(parent); - cb->addItems(values); - cb->setEditable(true); + return QStyledItemDelegate::createEditor(parent, option, index); +} - return cb; +void ConfigModelItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const +{ + if (index.column() == 1) { + ConfigModel::DataItem data = ConfigModel::dataItemFromIndex(index); + if (data.type == ConfigModel::DataItem::FILE || data.type == ConfigModel::DataItem::DIRECTORY) { + auto edit = static_cast(editor); + edit->setFileName(Utils::FileName::fromUserInput(data.value)); + return; + } else if (!data.values.isEmpty()) { + auto edit = static_cast(editor); + edit->setCurrentText(data.value); + return; + } else if (data.type == ConfigModel::DataItem::BOOLEAN) { + auto edit = static_cast(editor); + edit->setChecked(index.data(Qt::CheckStateRole).toBool()); + edit->setText(data.value); + return; + } else if (data.type == ConfigModel::DataItem::STRING) { + auto edit = static_cast(editor); + edit->setText(data.value); + return; + } + } + QStyledItemDelegate::setEditorData(editor, index); } -void ConfigModelItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const +void ConfigModelItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const { - if (QComboBox* cb = qobject_cast(editor)) { - // get the index of the text in the combobox that matches the current value of the itenm - QString currentText = index.data(Qt::EditRole).toString(); - int cbIndex = cb->findText(currentText); - // if it is valid, adjust the combobox - if (cbIndex >= 0) - cb->setCurrentIndex(cbIndex); - else - cb->setEditText(currentText); - } else { - QStyledItemDelegate::setEditorData(editor, index); + if (index.column() == 1) { + ConfigModel::DataItem data = ConfigModel::dataItemFromIndex(index); + if (data.type == ConfigModel::DataItem::FILE || data.type == ConfigModel::DataItem::DIRECTORY) { + auto edit = static_cast(editor); + if (edit->rawPath() != data.value) + model->setData(index, edit->fileName().toUserOutput(), Qt::EditRole); + return; + } else if (!data.values.isEmpty()) { + auto edit = static_cast(editor); + model->setData(index, edit->currentText(), Qt::EditRole); + return; + } else if (data.type == ConfigModel::DataItem::BOOLEAN) { + auto edit = static_cast(editor); + model->setData(index, edit->text(), Qt::EditRole); + } else if (data.type == ConfigModel::DataItem::STRING) { + auto edit = static_cast(editor); + model->setData(index, edit->text(), Qt::EditRole); + } } + QStyledItemDelegate::setModelData(editor, model, index); } -void ConfigModelItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const +QSize CMakeProjectManager::ConfigModelItemDelegate::sizeHint(const QStyleOptionViewItem &option, + const QModelIndex &index) const { - if (QComboBox* cb = qobject_cast(editor)) - // save the current text of the combo box as the current value of the item - model->setData(index, cb->currentText(), Qt::EditRole); - else - QStyledItemDelegate::setModelData(editor, model, index); + Q_UNUSED(option); + Q_UNUSED(index); + return QSize(100, m_measurement.sizeHint().height()); } } // namespace CMakeProjectManager -- cgit v1.2.1