File indexing completed on 2024-12-22 03:46:49

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.1-or-later
0007 
0008 */
0009 
0010 #include "designwidget.h"
0011 
0012 #include "codegentableview.h"
0013 #include "comboboxdelegate.h"
0014 #include "methodmodel.h"
0015 
0016 #include <QDebug>
0017 
0018 DesignWidget::DesignWidget(QWidget *parent)
0019     : QWidget(parent)
0020 {
0021     ui.setupUi(this);
0022     setInitialContent();
0023 }
0024 
0025 void DesignWidget::setInitialContent()
0026 {
0027     ui.author_edit->setText("Winston Smith");
0028     ui.email_edit->setText("winston@smithfamily.com");
0029     ui.className_edit->setText("MyClass");
0030 
0031     int propRows = 5;
0032     ui.properties_table->setRowCount(propRows);
0033 
0034     insertProperty(0, "QString", "name", false);
0035     insertProperty(1, "int", "age", true);
0036     insertProperty(2, "QColor", "furColor", false);
0037     insertProperty(3, "bool", "pedigree", true);
0038     insertProperty(4, "QString", "owner", false);
0039 
0040     QList<QStringList> args;
0041 
0042     m_methodModel = new MethodModel(this);
0043     insertMethod("public", false, "QString", "getResult", true);
0044     args << (QStringList() << "QString"
0045                            << "silly");
0046     args << (QStringList() << "int"
0047                            << "number"
0048                            << "9");
0049     insertMethod("public slots", false, "void", "somethingStupid", false, args);
0050     insertMethod("protected", false, "void", "reset", false);
0051     args.clear();
0052     args << (QStringList() << "int"
0053                            << "var");
0054     args << (QStringList() << "QString"
0055                            << "input"
0056                            << "QString()");
0057     insertMethod("protected", false, "QModelIndex", "findIndex", true, args);
0058     insertMethod("protected", true, "void", "clear", false);
0059     insertMethod("private", false, "void", "internalMethod", true);
0060 
0061     ui.methods_table->setModel(m_methodModel);
0062 
0063     connect(ui.methods_table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex, QModelIndex)), SLOT(setArgsRootIndex(QModelIndex)));
0064     m_argsModel = new ArgsModel(this);
0065     m_argsModel->setSourceModel(m_methodModel);
0066 
0067     setArgsRootIndex(QModelIndex());
0068 
0069     connect(ui.pushButton, SIGNAL(clicked(bool)), SIGNAL(generateClicked(bool)));
0070 }
0071 
0072 void DesignWidget::insertMethod(const QString &access, bool _virtual, const QString &type, const QString &name, bool _const, QList<QStringList> args)
0073 {
0074     QList<QStandardItem *> method;
0075     method << new QStandardItem(access) << new QStandardItem() << new QStandardItem(type) << new QStandardItem(name) << new QStandardItem();
0076 
0077     method.at(1)->setEditable(false);
0078     method.at(4)->setEditable(false);
0079     method.at(1)->setCheckable(true);
0080     method.at(4)->setCheckable(true);
0081     method.at(1)->setCheckState(_virtual ? Qt::Checked : Qt::Unchecked);
0082     method.at(4)->setCheckState(_const ? Qt::Checked : Qt::Unchecked);
0083 
0084     if (!args.isEmpty()) {
0085         for (const QStringList &arg : args) {
0086             QList<QStandardItem *> argItem;
0087             argItem << new QStandardItem(arg.at(0)) << new QStandardItem(arg.at(1)) << new QStandardItem(arg.value(2));
0088             method.first()->appendRow(argItem);
0089         }
0090     }
0091 
0092     m_methodModel->appendRow(method);
0093 }
0094 
0095 void DesignWidget::insertProperty(int row, const QString &type, const QString &name, bool readonly)
0096 {
0097     ui.properties_table->setItem(row, 0, new QTableWidgetItem(type));
0098     ui.properties_table->setItem(row, 1, new QTableWidgetItem(name));
0099 
0100     QTableWidgetItem *checkableItem = new QTableWidgetItem;
0101     checkableItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
0102     checkableItem->setCheckState(readonly ? Qt::Unchecked : Qt::Checked);
0103     ui.properties_table->setItem(row, 2, checkableItem);
0104 }
0105 
0106 void DesignWidget::setArgsRootIndex(const QModelIndex &index)
0107 {
0108     ui.args_table->setModel(index.isValid() ? m_argsModel : 0);
0109     ui.args_table->setRootIndex(m_argsModel->mapFromSource(index.sibling(index.row(), 0)));
0110 }
0111 
0112 KTextTemplate::Context DesignWidget::getContext()
0113 {
0114     KTextTemplate::Context c;
0115     c.insert("pimpl", ui.pimpl_check->isChecked());
0116     c.insert("className", ui.className_edit->text());
0117     c.insert("author", ui.author_edit->text());
0118     c.insert("email", ui.email_edit->text());
0119     c.insert("qobject", true);
0120     c.insert("licence", ui.licence_combo->currentText());
0121 
0122     if (ui.baseClassAccess->currentText() != "None") {
0123         QVariantHash baseClass;
0124         baseClass.insert("module", ui.baseModule->currentText());
0125         baseClass.insert("access", ui.baseClassAccess->currentText());
0126         baseClass.insert("type", ui.baseClassType->currentText());
0127         c.insert("baseClass", baseClass);
0128     }
0129 
0130     QVariantList properties;
0131     for (int row = 0; row < ui.properties_table->rowCount(); ++row) {
0132         QVariantHash property;
0133 
0134         property.insert("type", ui.properties_table->item(row, 0)->text());
0135         property.insert("name", ui.properties_table->item(row, 1)->text());
0136         property.insert("readonly", ui.properties_table->item(row, 2)->checkState() == Qt::Checked);
0137 
0138         properties << property;
0139     }
0140     c.insert("properties", QVariant(properties));
0141 
0142     QVariantList methods;
0143     for (int row = 0; row < m_methodModel->rowCount(); ++row) {
0144         QVariantHash method;
0145         QStandardItem *firstColumn = m_methodModel->item(row, 0);
0146 
0147         method.insert("accessType", firstColumn->text());
0148         method.insert("virtual", m_methodModel->item(row, 1)->checkState() == Qt::Checked);
0149         method.insert("type", m_methodModel->item(row, 2)->text());
0150         method.insert("name", m_methodModel->item(row, 3)->text());
0151         method.insert("const", m_methodModel->item(row, 4)->checkState() == Qt::Checked);
0152 
0153         if (firstColumn->hasChildren()) {
0154             QVariantList args;
0155 
0156             for (int argRow = 0; argRow < firstColumn->rowCount(); ++argRow) {
0157                 QVariantHash arg;
0158 
0159                 arg.insert("type", firstColumn->child(argRow, 0)->text());
0160                 arg.insert("name", firstColumn->child(argRow, 1)->text());
0161                 arg.insert("default", firstColumn->child(argRow, 2)->text());
0162 
0163                 args << arg;
0164             }
0165             method.insert("args", args);
0166         }
0167         methods << method;
0168     }
0169 
0170     c.insert("methods", QVariant(methods));
0171     return c;
0172 }
0173 
0174 QString DesignWidget::outputType() const
0175 {
0176     return ui.language_combo->currentText();
0177 }
0178 
0179 #include "moc_designwidget.cpp"