| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtSql module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QSQLRELATIONALDELEGATE_H |
| 41 | #define QSQLRELATIONALDELEGATE_H |
| 42 | |
| 43 | #include <QtSql/qtsqlglobal.h> |
| 44 | |
| 45 | QT_REQUIRE_CONFIG(sqlmodel); |
| 46 | |
| 47 | #ifdef QT_WIDGETS_LIB |
| 48 | |
| 49 | #include <QtWidgets/qstyleditemdelegate.h> |
| 50 | #if QT_CONFIG(listview) |
| 51 | #include <QtWidgets/qlistview.h> |
| 52 | #endif |
| 53 | #if QT_CONFIG(combobox) |
| 54 | #include <QtWidgets/qcombobox.h> |
| 55 | #endif |
| 56 | #include <QtSql/qsqldriver.h> |
| 57 | #include <QtSql/qsqlrelationaltablemodel.h> |
| 58 | #include <QtCore/qmetaobject.h> |
| 59 | QT_BEGIN_NAMESPACE |
| 60 | |
| 61 | class QSqlRelationalDelegate : public QStyledItemDelegate |
| 62 | { |
| 63 | static int fieldIndex(const QSqlTableModel *const model, |
| 64 | const QSqlDriver *const driver, |
| 65 | const QString &fieldName) |
| 66 | { |
| 67 | const QString stripped = driver->isIdentifierEscaped(fieldName, QSqlDriver::FieldName) |
| 68 | ? driver->stripDelimiters(fieldName, QSqlDriver::FieldName) |
| 69 | : fieldName; |
| 70 | return model->fieldIndex(stripped); |
| 71 | } |
| 72 | |
| 73 | public: |
| 74 | |
| 75 | explicit QSqlRelationalDelegate(QObject *aParent = nullptr) |
| 76 | : QStyledItemDelegate(aParent) |
| 77 | {} |
| 78 | |
| 79 | ~QSqlRelationalDelegate() |
| 80 | {} |
| 81 | |
| 82 | QWidget *createEditor(QWidget *aParent, |
| 83 | const QStyleOptionViewItem &option, |
| 84 | const QModelIndex &index) const override |
| 85 | { |
| 86 | const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model()); |
| 87 | QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr; |
| 88 | if (!childModel) |
| 89 | return QStyledItemDelegate::createEditor(aParent, option, index); |
| 90 | const QSqlDriver *const driver = childModel->database().driver(); |
| 91 | |
| 92 | QComboBox *combo = new QComboBox(aParent); |
| 93 | combo->setModel(childModel); |
| 94 | combo->setModelColumn(fieldIndex(childModel, driver, |
| 95 | sqlModel->relation(index.column()).displayColumn())); |
| 96 | combo->installEventFilter(const_cast<QSqlRelationalDelegate *>(this)); |
| 97 | |
| 98 | return combo; |
| 99 | } |
| 100 | |
| 101 | void setEditorData(QWidget *editor, const QModelIndex &index) const override |
| 102 | { |
| 103 | if (!index.isValid()) |
| 104 | return; |
| 105 | |
| 106 | if (qobject_cast<QComboBox *>(editor)) { |
| 107 | // Taken from QItemDelegate::setEditorData() as we need |
| 108 | // to present the DisplayRole and not the EditRole which |
| 109 | // is the id reference to the related model |
| 110 | QVariant v = index.data(Qt::DisplayRole); |
| 111 | const QByteArray n = editor->metaObject()->userProperty().name(); |
| 112 | if (!n.isEmpty()) { |
| 113 | if (!v.isValid()) |
| 114 | v = QVariant(editor->property(n.data()).metaType()); |
| 115 | editor->setProperty(n.data(), v); |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | QStyledItemDelegate::setEditorData(editor, index); |
| 120 | } |
| 121 | |
| 122 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override |
| 123 | { |
| 124 | if (!index.isValid()) |
| 125 | return; |
| 126 | |
| 127 | QSqlRelationalTableModel *sqlModel = qobject_cast<QSqlRelationalTableModel *>(model); |
| 128 | QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr; |
| 129 | QComboBox *combo = qobject_cast<QComboBox *>(editor); |
| 130 | if (!sqlModel || !childModel || !combo) { |
| 131 | QStyledItemDelegate::setModelData(editor, model, index); |
| 132 | return; |
| 133 | } |
| 134 | const QSqlDriver *const driver = childModel->database().driver(); |
| 135 | |
| 136 | int currentItem = combo->currentIndex(); |
| 137 | int childColIndex = fieldIndex(childModel, driver, |
| 138 | sqlModel->relation(index.column()).displayColumn()); |
| 139 | int childEditIndex = fieldIndex(childModel, driver, |
| 140 | sqlModel->relation(index.column()).indexColumn()); |
| 141 | sqlModel->setData(index, |
| 142 | childModel->data(childModel->index(currentItem, childColIndex), Qt::DisplayRole), |
| 143 | Qt::DisplayRole); |
| 144 | sqlModel->setData(index, |
| 145 | childModel->data(childModel->index(currentItem, childEditIndex), Qt::EditRole), |
| 146 | Qt::EditRole); |
| 147 | } |
| 148 | |
| 149 | }; |
| 150 | |
| 151 | QT_END_NAMESPACE |
| 152 | |
| 153 | #endif // QT_WIDGETS_LIB |
| 154 | |
| 155 | #endif // QSQLRELATIONALDELEGATE_H |
| 156 | |