2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 Alexander Drozdov.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2016-10-10 09:49:46 +10:00
|
|
|
|
|
|
|
|
#include "configmodelitemdelegate.h"
|
2020-04-17 15:30:05 +02:00
|
|
|
|
2016-10-10 09:49:46 +10:00
|
|
|
#include "configmodel.h"
|
2022-09-29 15:26:31 +02:00
|
|
|
#include "cmakeprojectmanagertr.h"
|
2016-10-10 09:49:46 +10:00
|
|
|
|
2017-09-14 15:18:50 +02:00
|
|
|
#include <utils/pathchooser.h>
|
|
|
|
|
|
|
|
|
|
#include <QCheckBox>
|
2016-10-10 09:49:46 +10:00
|
|
|
|
2022-09-29 15:26:31 +02:00
|
|
|
using namespace Utils;
|
2016-10-10 09:49:46 +10:00
|
|
|
|
2022-09-29 15:26:31 +02:00
|
|
|
namespace CMakeProjectManager::Internal {
|
|
|
|
|
|
|
|
|
|
ConfigModelItemDelegate::ConfigModelItemDelegate(const FilePath &base, QObject* parent)
|
2016-10-10 09:49:46 +10:00
|
|
|
: QStyledItemDelegate(parent)
|
2017-09-14 15:18:50 +02:00
|
|
|
, m_base(base)
|
2016-10-10 09:49:46 +10:00
|
|
|
{ }
|
|
|
|
|
|
2017-09-14 15:18:50 +02:00
|
|
|
QWidget *ConfigModelItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
|
|
|
|
const QModelIndex &index) const
|
2016-10-10 09:49:46 +10:00
|
|
|
|
|
|
|
|
{
|
2017-09-14 15:18:50 +02:00
|
|
|
if (index.column() == 1) {
|
|
|
|
|
ConfigModel::DataItem data = ConfigModel::dataItemFromIndex(index);
|
|
|
|
|
if (data.type == ConfigModel::DataItem::FILE || data.type == ConfigModel::DataItem::DIRECTORY) {
|
2022-09-29 15:26:31 +02:00
|
|
|
auto edit = new PathChooser(parent);
|
2022-01-07 16:05:41 +01:00
|
|
|
edit->setAttribute(Qt::WA_MacSmallSize);
|
2017-09-14 15:18:50 +02:00
|
|
|
edit->setFocusPolicy(Qt::StrongFocus);
|
2019-12-17 11:53:58 +01:00
|
|
|
edit->setBaseDirectory(m_base);
|
2017-09-14 15:18:50 +02:00
|
|
|
edit->setAutoFillBackground(true);
|
|
|
|
|
if (data.type == ConfigModel::DataItem::FILE) {
|
2022-09-29 15:26:31 +02:00
|
|
|
edit->setExpectedKind(PathChooser::File);
|
|
|
|
|
edit->setPromptDialogTitle(Tr::tr("Select a file for %1").arg(data.key));
|
2017-09-14 15:18:50 +02:00
|
|
|
} else {
|
2022-09-29 15:26:31 +02:00
|
|
|
edit->setExpectedKind(PathChooser::Directory);
|
|
|
|
|
edit->setPromptDialogTitle(Tr::tr("Select a directory for %1").arg(data.key));
|
2017-09-14 15:18:50 +02:00
|
|
|
}
|
|
|
|
|
return edit;
|
|
|
|
|
} else if (!data.values.isEmpty()) {
|
|
|
|
|
auto edit = new QComboBox(parent);
|
2022-01-07 16:05:41 +01:00
|
|
|
edit->setAttribute(Qt::WA_MacSmallSize);
|
2017-09-14 15:18:50 +02:00
|
|
|
edit->setFocusPolicy(Qt::StrongFocus);
|
2022-07-04 11:20:33 +02:00
|
|
|
edit->setAutoFillBackground(true);
|
2022-10-07 14:46:06 +02:00
|
|
|
for (const QString &s : std::as_const(data.values))
|
2017-09-14 15:18:50 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-10 09:49:46 +10:00
|
|
|
|
2017-09-14 15:18:50 +02:00
|
|
|
return QStyledItemDelegate::createEditor(parent, option, index);
|
|
|
|
|
}
|
2016-10-10 09:49:46 +10:00
|
|
|
|
2017-09-14 15:18:50 +02:00
|
|
|
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) {
|
2022-09-29 15:26:31 +02:00
|
|
|
auto edit = static_cast<PathChooser *>(editor);
|
|
|
|
|
edit->setFilePath(FilePath::fromUserInput(data.value));
|
2017-09-14 15:18:50 +02:00
|
|
|
return;
|
|
|
|
|
} else if (!data.values.isEmpty()) {
|
|
|
|
|
auto edit = static_cast<QComboBox *>(editor);
|
|
|
|
|
edit->setCurrentText(data.value);
|
|
|
|
|
return;
|
|
|
|
|
} else if (data.type == ConfigModel::DataItem::BOOLEAN) {
|
|
|
|
|
auto edit = static_cast<QCheckBox *>(editor);
|
|
|
|
|
edit->setChecked(index.data(Qt::CheckStateRole).toBool());
|
|
|
|
|
edit->setText(data.value);
|
|
|
|
|
return;
|
|
|
|
|
} else if (data.type == ConfigModel::DataItem::STRING) {
|
|
|
|
|
auto edit = static_cast<QLineEdit *>(editor);
|
|
|
|
|
edit->setText(data.value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
QStyledItemDelegate::setEditorData(editor, index);
|
2016-10-10 09:49:46 +10:00
|
|
|
}
|
|
|
|
|
|
2017-09-14 15:18:50 +02:00
|
|
|
void ConfigModelItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
|
|
|
|
const QModelIndex &index) const
|
2016-10-10 09:49:46 +10:00
|
|
|
{
|
2017-09-14 15:18:50 +02:00
|
|
|
if (index.column() == 1) {
|
|
|
|
|
ConfigModel::DataItem data = ConfigModel::dataItemFromIndex(index);
|
|
|
|
|
if (data.type == ConfigModel::DataItem::FILE || data.type == ConfigModel::DataItem::DIRECTORY) {
|
2022-09-29 15:26:31 +02:00
|
|
|
auto edit = static_cast<PathChooser *>(editor);
|
2024-06-13 11:24:01 +02:00
|
|
|
if (edit->unexpandedFilePath().toString() != data.value)
|
|
|
|
|
model->setData(index, edit->unexpandedFilePath().toString(), Qt::EditRole);
|
2017-09-14 15:18:50 +02:00
|
|
|
return;
|
|
|
|
|
} else if (!data.values.isEmpty()) {
|
|
|
|
|
auto edit = static_cast<QComboBox *>(editor);
|
|
|
|
|
model->setData(index, edit->currentText(), Qt::EditRole);
|
|
|
|
|
return;
|
|
|
|
|
} else if (data.type == ConfigModel::DataItem::BOOLEAN) {
|
|
|
|
|
auto edit = static_cast<QCheckBox *>(editor);
|
|
|
|
|
model->setData(index, edit->text(), Qt::EditRole);
|
|
|
|
|
} else if (data.type == ConfigModel::DataItem::STRING) {
|
|
|
|
|
auto edit = static_cast<QLineEdit *>(editor);
|
|
|
|
|
model->setData(index, edit->text(), Qt::EditRole);
|
|
|
|
|
}
|
2016-10-10 09:49:46 +10:00
|
|
|
}
|
2017-09-14 15:18:50 +02:00
|
|
|
QStyledItemDelegate::setModelData(editor, model, index);
|
2016-10-10 09:49:46 +10:00
|
|
|
}
|
|
|
|
|
|
2022-02-07 17:01:09 +01:00
|
|
|
QSize ConfigModelItemDelegate::sizeHint(const QStyleOptionViewItem &option,
|
|
|
|
|
const QModelIndex &index) const
|
2016-10-10 09:49:46 +10:00
|
|
|
{
|
2022-01-07 16:05:41 +01:00
|
|
|
static int height = -1;
|
|
|
|
|
if (height < 0) {
|
|
|
|
|
const auto setMaxSize = [](const QWidget &w) {
|
|
|
|
|
if (w.sizeHint().height() > height)
|
|
|
|
|
height = w.sizeHint().height();
|
|
|
|
|
};
|
|
|
|
|
QComboBox box;
|
|
|
|
|
box.setAttribute(Qt::WA_MacSmallSize);
|
|
|
|
|
QCheckBox check;
|
|
|
|
|
setMaxSize(box);
|
|
|
|
|
setMaxSize(check);
|
|
|
|
|
// Do not take the path chooser into consideration, because that would make the height
|
|
|
|
|
// larger on Windows, leading to less items displayed, and the size of PathChooser looks
|
|
|
|
|
// "fine enough" as is.
|
|
|
|
|
}
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(option)
|
|
|
|
|
Q_UNUSED(index)
|
2022-01-07 16:05:41 +01:00
|
|
|
return QSize(100, height);
|
2016-10-10 09:49:46 +10:00
|
|
|
}
|
|
|
|
|
|
2022-09-29 15:26:31 +02:00
|
|
|
} // CMakeProjectManager::Internal
|