2025-04-17 16:51:20 +08:00
|
|
|
#include <QLineEdit>
|
|
|
|
|
#include <QComboBox>
|
|
|
|
|
#include <QEvent>
|
|
|
|
|
|
|
|
|
|
#include "projectTableDelegate.h"
|
|
|
|
|
#include "selectorDialog.h"
|
|
|
|
|
#include "global.h"
|
|
|
|
|
|
|
|
|
|
ProjectTableDelegate::ProjectTableDelegate(QObject *parent)
|
|
|
|
|
: QStyledItemDelegate(parent)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ProjectTableDelegate::~ProjectTableDelegate()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget* ProjectTableDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
|
|
|
|
|
const QModelIndex& index) const
|
|
|
|
|
{
|
|
|
|
|
if(index.column() == TD_ProjectModel) //editline
|
|
|
|
|
{
|
|
|
|
|
QLineEdit *editor = new QLineEdit(parent);
|
|
|
|
|
|
|
|
|
|
// 连接编辑完成信号
|
|
|
|
|
connect(editor, &QLineEdit::editingFinished, this, [this, editor, index]() {
|
|
|
|
|
emit editingFinished(index, editor->text());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return editor;
|
|
|
|
|
}
|
2025-07-22 12:03:43 +08:00
|
|
|
return nullptr;
|
2025-04-17 16:51:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*bool ProjectTableDelegate::eventFilter(QObject* obj, QEvent* event)
|
|
|
|
|
{
|
|
|
|
|
if (event->type() == QEvent::KeyPress) {
|
|
|
|
|
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
|
|
|
|
if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
|
|
|
|
|
QLineEdit* editor = qobject_cast<QLineEdit*>(obj);
|
|
|
|
|
if (editor) {
|
|
|
|
|
emit yourCustomSignal(editor->text());
|
|
|
|
|
commitData(editor); // 显式提交数据
|
|
|
|
|
closeEditor(editor); // 关闭编辑器
|
|
|
|
|
return true; // 阻止事件继续传播
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QStyledItemDelegate::eventFilter(obj, event);
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
/*void ProjectTableDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
|
|
|
|
|
{
|
|
|
|
|
if(index.column() == TD_MetaModel)
|
|
|
|
|
{
|
|
|
|
|
QComboBox* comboBox = static_cast<QComboBox*>(editor);
|
|
|
|
|
comboBox->setCurrentText(index.data(Qt::EditRole).toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProjectTableDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
|
|
|
|
|
const QModelIndex& index) const
|
|
|
|
|
{
|
|
|
|
|
if(index.column() == TD_MetaModel)
|
|
|
|
|
{
|
|
|
|
|
QComboBox* comboBox = static_cast<QComboBox*>(editor);
|
|
|
|
|
model->setData(index, comboBox->currentText(), Qt::EditRole);
|
|
|
|
|
|
|
|
|
|
emit editingFinished(index, comboBox->currentText()); //发送自定义信号
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
bool ProjectTableDelegate::editorEvent(QEvent* event, QAbstractItemModel* model,
|
|
|
|
|
const QStyleOptionViewItem& option, const QModelIndex& index)
|
|
|
|
|
{
|
|
|
|
|
if (event->type() == QEvent::MouseButtonDblClick) {
|
|
|
|
|
if(index.column() == TD_ProjectModel)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if(index.column() == TD_MetaModel)
|
|
|
|
|
{
|
|
|
|
|
SelectorDialog dialog(option.widget->parentWidget()->parentWidget()->parentWidget()->parentWidget());
|
|
|
|
|
dialog.initial(ST_MetaModel);
|
|
|
|
|
if(dialog.exec() == QDialog::Accepted) {
|
2025-05-23 10:30:52 +08:00
|
|
|
modelType meta = dialog.selectedMeta();
|
|
|
|
|
if(!meta.modelType.isEmpty()) {
|
|
|
|
|
model->setData(index, meta.modelName, Qt::EditRole);
|
|
|
|
|
model->setData(index, meta.modelType, Qt::EditRole+1);
|
|
|
|
|
emit editingFinished(index,meta.modelType);
|
2025-04-17 16:51:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if(index.column() == TD_ComponentType)
|
|
|
|
|
{
|
|
|
|
|
SelectorDialog dialog(option.widget->parentWidget()->parentWidget()->parentWidget()->parentWidget());
|
|
|
|
|
dialog.initial(ST_ComponentType);
|
|
|
|
|
if(dialog.exec() == QDialog::Accepted) {
|
|
|
|
|
QString component = dialog.selectedComponent();
|
|
|
|
|
if(!component.isEmpty()) {
|
|
|
|
|
model->setData(index, component, Qt::EditRole);
|
|
|
|
|
emit editingFinished(index,component);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QStyledItemDelegate::editorEvent(event, model, option, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProjectTableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
|
{
|
|
|
|
|
//根据行号设置交替色
|
|
|
|
|
QStyleOptionViewItem opt = option;
|
|
|
|
|
initStyleOption(&opt, index);
|
|
|
|
|
|
|
|
|
|
QModelIndex firstColIndex = index.sibling(index.row(), 0);
|
|
|
|
|
TableItemState state = TableItemState(firstColIndex.data(Qt::UserRole).toInt());
|
|
|
|
|
|
|
|
|
|
/*if(index.column() == 0 && state == TS_create)
|
|
|
|
|
{
|
|
|
|
|
opt.palette.setColor(QPalette::Text, Qt::red);
|
|
|
|
|
}
|
|
|
|
|
else if(index.column() == 0 && state == TS_select)
|
|
|
|
|
{
|
|
|
|
|
opt.palette.setColor(QPalette::Text, Qt::green);
|
|
|
|
|
}
|
|
|
|
|
else if(index.column() == 0 && state == TS_edit)
|
|
|
|
|
{
|
|
|
|
|
opt.palette.setColor(QPalette::Text, Qt::yellow);
|
|
|
|
|
}
|
|
|
|
|
QStyledItemDelegate::paint(painter, opt, index);*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(state == TS_create)
|
|
|
|
|
{
|
|
|
|
|
opt.palette.setColor(QPalette::Text, Qt::red);
|
|
|
|
|
}
|
|
|
|
|
else if(state == TS_select)
|
|
|
|
|
{
|
|
|
|
|
opt.palette.setColor(QPalette::Text, Qt::green);
|
|
|
|
|
}
|
|
|
|
|
else if(state == TS_edit)
|
|
|
|
|
{
|
|
|
|
|
opt.palette.setColor(QPalette::Text, Qt::yellow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//先执行默认绘制(包括背景、文本等基础元素)
|
|
|
|
|
QStyledItemDelegate::paint(painter, opt, index);
|
|
|
|
|
|
|
|
|
|
}
|