File indexing completed on 2023-09-24 04:09:50
0001 /* 0002 SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "modelcommanderwidget.h" 0008 0009 #include <QPushButton> 0010 #include <QTreeWidget> 0011 #include <QVBoxLayout> 0012 0013 #include "dynamictreemodel.h" 0014 #include "modelcommander.h" 0015 #include <QMetaMethod> 0016 0017 ModelCommanderWidget::ModelCommanderWidget(DynamicTreeModel *dynamicTreeModel, QWidget *parent, Qt::WindowFlags f) 0018 : QWidget(parent, f) 0019 , m_dynamicTreeModel(dynamicTreeModel) 0020 , m_modelCommander(new ModelCommander(m_dynamicTreeModel, this)) 0021 , m_treeWidget(new QTreeWidget) 0022 , m_executeButton(new QPushButton(QStringLiteral("Execute"))) 0023 { 0024 QVBoxLayout *layout = new QVBoxLayout(this); 0025 layout->addWidget(m_treeWidget); 0026 layout->addWidget(m_executeButton); 0027 0028 init(); 0029 0030 connect(m_treeWidget, &QTreeWidget::currentItemChanged, this, &ModelCommanderWidget::currentItemChanged); 0031 0032 connect(m_executeButton, &QPushButton::clicked, this, &ModelCommanderWidget::executeCurrentTest); 0033 } 0034 0035 void ModelCommanderWidget::init() 0036 { 0037 const QMetaObject *mo = m_modelCommander->metaObject(); 0038 QMetaMethod mm; 0039 for (int i = 0; i < mo->methodCount(); ++i) { 0040 mm = mo->method(i); 0041 QString signature = mm.methodSignature(); 0042 if (signature.startsWith(QLatin1String("init_")) && signature.endsWith(QLatin1String("(QString)"))) { 0043 QTreeWidgetItem *testFunctionItem = new QTreeWidgetItem(m_treeWidget, QStringList() << signature.mid(5, signature.length() - 14)); 0044 m_treeWidget->addTopLevelItem(testFunctionItem); 0045 0046 QStringList testData; 0047 QMetaObject::invokeMethod(m_modelCommander, 0048 QByteArray("execute_" + testFunctionItem->text(0).toLatin1()).constData(), 0049 Q_RETURN_ARG(QStringList, testData), 0050 Q_ARG(QString, QString())); 0051 0052 for (const QString &testRun : std::as_const(testData)) { 0053 new QTreeWidgetItem(testFunctionItem, QStringList() << testRun); 0054 } 0055 } 0056 } 0057 } 0058 0059 void ModelCommanderWidget::currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) 0060 { 0061 Q_UNUSED(previous); 0062 initTest(current); 0063 } 0064 0065 void ModelCommanderWidget::executeCurrentTest() 0066 { 0067 executeTest(m_treeWidget->currentItem()); 0068 0069 disconnect(m_executeButton, &QPushButton::clicked, this, &ModelCommanderWidget::executeCurrentTest); 0070 m_executeButton->setText(QStringLiteral("Reset")); 0071 connect(m_executeButton, &QPushButton::clicked, this, &ModelCommanderWidget::resetCurrentTest); 0072 } 0073 0074 void ModelCommanderWidget::resetCurrentTest() 0075 { 0076 initTest(m_treeWidget->currentItem()); 0077 0078 disconnect(m_executeButton, &QPushButton::clicked, this, &ModelCommanderWidget::resetCurrentTest); 0079 m_executeButton->setText(QStringLiteral("Execute")); 0080 connect(m_executeButton, &QPushButton::clicked, this, &ModelCommanderWidget::executeCurrentTest); 0081 } 0082 0083 void ModelCommanderWidget::initTest(QTreeWidgetItem *item) 0084 { 0085 if (!item->parent()) { 0086 return; // m_dynamicTreeModel->clear(); 0087 } 0088 m_dynamicTreeModel->clear(); 0089 bool success = 0090 QMetaObject::invokeMethod(m_modelCommander, QByteArray("init_" + item->parent()->text(0).toLatin1()).constData(), Q_ARG(QString, item->text(0))); 0091 Q_ASSERT(success); 0092 } 0093 0094 void ModelCommanderWidget::executeTest(QTreeWidgetItem *item) 0095 { 0096 if (!item->parent()) { 0097 return; 0098 } 0099 0100 bool success = 0101 QMetaObject::invokeMethod(m_modelCommander, QByteArray("execute_" + item->parent()->text(0).toLatin1()).constData(), Q_ARG(QString, item->text(0))); 0102 Q_ASSERT(success); 0103 } 0104 0105 #include "moc_modelcommanderwidget.cpp"