Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:09:19

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QSQLRELATIONALDELEGATE_H
0005 #define QSQLRELATIONALDELEGATE_H
0006 
0007 #include <QtSql/qtsqlglobal.h>
0008 
0009 QT_REQUIRE_CONFIG(sqlmodel);
0010 
0011 #ifdef QT_WIDGETS_LIB
0012 
0013 #include <QtWidgets/qstyleditemdelegate.h>
0014 #if QT_CONFIG(listview)
0015 #include <QtWidgets/qlistview.h>
0016 #endif
0017 #if QT_CONFIG(combobox)
0018 #include <QtWidgets/qcombobox.h>
0019 #endif
0020 #include <QtSql/qsqldriver.h>
0021 #include <QtSql/qsqlrelationaltablemodel.h>
0022 #include <QtCore/qmetaobject.h>
0023 QT_BEGIN_NAMESPACE
0024 
0025 class QSqlRelationalDelegate : public QStyledItemDelegate
0026 {
0027     static int fieldIndex(const QSqlTableModel *const model,
0028                           const QSqlDriver *const driver,
0029                           const QString &fieldName)
0030     {
0031         const QString stripped = driver->isIdentifierEscaped(fieldName, QSqlDriver::FieldName)
0032                 ? driver->stripDelimiters(fieldName, QSqlDriver::FieldName)
0033                 : fieldName;
0034         return model->fieldIndex(stripped);
0035     }
0036 
0037 public:
0038 
0039     explicit QSqlRelationalDelegate(QObject *aParent = nullptr)
0040         : QStyledItemDelegate(aParent)
0041     {}
0042 
0043     ~QSqlRelationalDelegate()
0044     {}
0045 
0046     QWidget *createEditor(QWidget *aParent,
0047                           const QStyleOptionViewItem &option,
0048                           const QModelIndex &index) const override
0049     {
0050         const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model());
0051         QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr;
0052         if (!childModel)
0053             return QStyledItemDelegate::createEditor(aParent, option, index);
0054         const QSqlDriver *const driver = childModel->database().driver();
0055 
0056         QComboBox *combo = new QComboBox(aParent);
0057         combo->setModel(childModel);
0058         combo->setModelColumn(fieldIndex(childModel, driver,
0059                                          sqlModel->relation(index.column()).displayColumn()));
0060         combo->installEventFilter(const_cast<QSqlRelationalDelegate *>(this));
0061 
0062         return combo;
0063     }
0064 
0065     void setEditorData(QWidget *editor, const QModelIndex &index) const override
0066     {
0067         if (!index.isValid())
0068             return;
0069 
0070         if (qobject_cast<QComboBox *>(editor)) {
0071             // Taken from QItemDelegate::setEditorData() as we need
0072             // to present the DisplayRole and not the EditRole which
0073             // is the id reference to the related model
0074             QVariant v = index.data(Qt::DisplayRole);
0075             const QByteArray n = editor->metaObject()->userProperty().name();
0076             if (!n.isEmpty()) {
0077                 if (!v.isValid())
0078                     v = QVariant(editor->property(n.data()).metaType());
0079                 editor->setProperty(n.data(), v);
0080                 return;
0081             }
0082         }
0083         QStyledItemDelegate::setEditorData(editor, index);
0084     }
0085 
0086 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
0087 {
0088     if (!index.isValid())
0089         return;
0090 
0091     QSqlRelationalTableModel *sqlModel = qobject_cast<QSqlRelationalTableModel *>(model);
0092     QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr;
0093     QComboBox *combo = qobject_cast<QComboBox *>(editor);
0094     if (!sqlModel || !childModel || !combo) {
0095         QStyledItemDelegate::setModelData(editor, model, index);
0096         return;
0097     }
0098     const QSqlDriver *const driver = childModel->database().driver();
0099 
0100     int currentItem = combo->currentIndex();
0101     int childColIndex = fieldIndex(childModel, driver,
0102                                    sqlModel->relation(index.column()).displayColumn());
0103     int childEditIndex = fieldIndex(childModel, driver,
0104                                     sqlModel->relation(index.column()).indexColumn());
0105     sqlModel->setData(index,
0106             childModel->data(childModel->index(currentItem, childColIndex), Qt::DisplayRole),
0107             Qt::DisplayRole);
0108     sqlModel->setData(index,
0109             childModel->data(childModel->index(currentItem, childEditIndex), Qt::EditRole),
0110             Qt::EditRole);
0111 }
0112 
0113 };
0114 
0115 QT_END_NAMESPACE
0116 
0117 #endif // QT_WIDGETS_LIB
0118 
0119 #endif // QSQLRELATIONALDELEGATE_H