File indexing completed on 2024-05-05 17:18:51

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007  * This is a delegate for query creator
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgquerydelegate.h"
0012 
0013 #include <qpainter.h>
0014 #include <qtablewidget.h>
0015 
0016 #include "skgmainpanel.h"
0017 #include "skgpredicatcreator.h"
0018 #include "skgtraces.h"
0019 
0020 SKGQueryDelegate::SKGQueryDelegate(QObject* iParent, SKGDocument* iDoc, bool iModeUpdate, QStringList  iListAtt):
0021     QItemDelegate(iParent), m_document(iDoc), m_updateMode(iModeUpdate), m_listAtt(std::move(iListAtt))
0022 {
0023 }
0024 
0025 SKGQueryDelegate::~SKGQueryDelegate()
0026 {
0027     m_document = nullptr;
0028 }
0029 
0030 QWidget* SKGQueryDelegate::createEditor(QWidget* iParent,
0031                                         const QStyleOptionViewItem& option,
0032                                         const QModelIndex& index) const
0033 {
0034     SKGTRACEINFUNC(1)
0035     Q_UNUSED(option)
0036 
0037     QTableWidgetItem* it_h = (qobject_cast<QTableWidget*>(this->parent()))->horizontalHeaderItem(index.column());
0038     QString attname = it_h->data(Qt::UserRole).toString();
0039 
0040     auto editor = new SKGPredicatCreator(iParent, m_document, attname, m_updateMode, m_listAtt);
0041     connect(editor, &SKGPredicatCreator::editingFinished, this, &SKGQueryDelegate::commitAndCloseEditor);
0042     return editor;
0043 }
0044 
0045 void SKGQueryDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
0046 {
0047     SKGTRACEINFUNC(1)
0048     auto* pred = qobject_cast<SKGPredicatCreator*>(editor);
0049     if (pred != nullptr) {
0050         pred->setXmlDescription(index.model()->data(index, Qt::UserRole).toString());
0051     } else {
0052         QItemDelegate::setEditorData(editor, index);
0053     }
0054 }
0055 
0056 void SKGQueryDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
0057                                     const QModelIndex& index) const
0058 {
0059     SKGTRACEINFUNC(1)
0060     auto* pred = qobject_cast<SKGPredicatCreator*>(editor);
0061     if ((pred != nullptr) && (model != nullptr)) {
0062         QString xml = pred->xmlDescription();
0063         model->setData(index, pred->text(), Qt::DisplayRole);
0064         model->setData(index, xml, Qt::UserRole);
0065     } else {
0066         QItemDelegate::setModelData(editor, model, index);
0067     }
0068 }
0069 
0070 void SKGQueryDelegate::commitAndCloseEditor()
0071 {
0072     auto* editor = qobject_cast<SKGPredicatCreator*>(sender());
0073     Q_EMIT commitData(editor);
0074     Q_EMIT closeEditor(editor);
0075 }
0076 
0077 
0078