File indexing completed on 2024-12-15 03:45:02
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "schemaeditwidget.h" 0008 #include "ui_schemaeditwidget.h" 0009 #include "schemaentryitemeditorfactory.h" 0010 0011 #include <model/schemamodel.h> 0012 #include <rest/restapi.h> 0013 #include <core/product.h> 0014 #include <core/schemaentrytemplates.h> 0015 0016 #include <QDebug> 0017 #include <QInputDialog> 0018 #include <QMenu> 0019 #include <QMessageBox> 0020 #include <QNetworkReply> 0021 #include <QStyledItemDelegate> 0022 0023 using namespace KUserFeedback::Console; 0024 0025 SchemaEditWidget::SchemaEditWidget(QWidget *parent) : 0026 QWidget(parent), 0027 ui(new Ui::SchemaEditWidget), 0028 m_schemaModel(new SchemaModel(this)) 0029 { 0030 ui->setupUi(this); 0031 0032 ui->schemaView->setModel(m_schemaModel); 0033 qobject_cast<QStyledItemDelegate*>(ui->schemaView->itemDelegate())->setItemEditorFactory(new SchemaEntryItemEditorFactory); 0034 ui->schemaView->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents); 0035 0036 connect(ui->actionAddSource, &QAction::triggered, this, &SchemaEditWidget::addSource); 0037 connect(ui->actionAddSourceElement, &QAction::triggered, this, &SchemaEditWidget::addSourceEntry); 0038 connect(ui->actionDelete, &QAction::triggered, this, &SchemaEditWidget::deleteEntry); 0039 0040 connect(ui->schemaView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &SchemaEditWidget::updateState); 0041 connect(ui->schemaView, &QWidget::customContextMenuRequested, this, &SchemaEditWidget::contextMenu); 0042 0043 connect(m_schemaModel, &QAbstractItemModel::dataChanged, this, [this]() { 0044 Q_EMIT productChanged(); 0045 }); 0046 0047 addActions({ ui->actionAddSource, ui->actionAddSourceElement, ui->actionDelete }); 0048 updateState(); 0049 } 0050 0051 SchemaEditWidget::~SchemaEditWidget() = default; 0052 0053 void SchemaEditWidget::setRESTClient(RESTClient* client) 0054 { 0055 m_restClient = client; 0056 } 0057 0058 Product SchemaEditWidget::product() const 0059 { 0060 return m_schemaModel->product(); 0061 } 0062 0063 void SchemaEditWidget::setProduct(const Product& product) 0064 { 0065 m_schemaModel->setProduct(product); 0066 ui->schemaView->expandAll(); 0067 updateState(); 0068 } 0069 0070 void SchemaEditWidget::addSource() 0071 { 0072 const auto name = QInputDialog::getText(this, tr("Add Source"), tr("Name:")); 0073 if (name.isEmpty()) 0074 return; 0075 m_schemaModel->addEntry(name); 0076 Q_EMIT productChanged(); 0077 } 0078 0079 void SchemaEditWidget::addSourceEntry() 0080 { 0081 const auto name = QInputDialog::getText(this, tr("Add Source Element"), tr("Name:")); 0082 if (name.isEmpty()) 0083 return; 0084 m_schemaModel->addElement(currentSource(), name); 0085 ui->schemaView->expand(currentSource()); 0086 Q_EMIT productChanged(); 0087 } 0088 0089 void SchemaEditWidget::deleteEntry() 0090 { 0091 const auto selection = ui->schemaView->selectionModel()->selection(); 0092 if (selection.isEmpty()) 0093 return; 0094 0095 if (currentSource().isValid()) { 0096 const auto r = QMessageBox::critical(this, tr("Delete Source"), 0097 tr("Do you really want to delete the source '%1', and all recorded data for it?").arg(currentSource().data().toString()), 0098 QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel); 0099 if (r != QMessageBox::Discard) 0100 return; 0101 } else { 0102 const auto r = QMessageBox::critical(this, tr("Delete Schema Entry"), 0103 tr("Do you really want to delete this entry, and all recorded data for it?"), 0104 QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel); 0105 if (r != QMessageBox::Discard) 0106 return; 0107 } 0108 0109 const auto idx = selection.first().topLeft(); 0110 m_schemaModel->deleteRow(idx); 0111 Q_EMIT productChanged(); 0112 } 0113 0114 void SchemaEditWidget::updateState() 0115 { 0116 const auto selection = ui->schemaView->selectionModel()->selection(); 0117 ui->actionDelete->setEnabled(!selection.isEmpty()); 0118 ui->actionAddSource->setEnabled(product().isValid()); 0119 ui->actionAddSourceElement->setEnabled(currentSource().isValid()); 0120 } 0121 0122 void SchemaEditWidget::contextMenu(QPoint pos) 0123 { 0124 QMenu menu; 0125 menu.addActions({ ui->actionAddSource, ui->actionAddSourceElement, ui->actionDelete }); 0126 menu.exec(ui->schemaView->viewport()->mapToGlobal(pos)); 0127 } 0128 0129 QModelIndex SchemaEditWidget::currentSource() const 0130 { 0131 const auto selection = ui->schemaView->selectionModel()->selectedRows(); 0132 if (selection.isEmpty()) 0133 return {}; 0134 const auto idx = selection.at(0); 0135 if (!idx.parent().isValid()) 0136 return idx; 0137 return {}; 0138 } 0139 0140 #include "moc_schemaeditwidget.cpp"