From 48e0706275cba3e77ef3ead0234e88ab196c65c2 Mon Sep 17 00:00:00 2001 From: mmtalaie Date: Sun, 23 Aug 2020 12:20:21 +0430 Subject: [PATCH] first commit --- DatabaseInitializer.pro | 29 ++++ XMLReader.cpp | 188 +++++++++++++++++++++++++ XMLReader.h | 41 ++++++ main.cpp | 11 ++ mainwindow.cpp | 304 ++++++++++++++++++++++++++++++++++++++++ mainwindow.h | 81 +++++++++++ mainwindow.ui | 177 +++++++++++++++++++++++ 7 files changed, 831 insertions(+) create mode 100644 DatabaseInitializer.pro create mode 100644 XMLReader.cpp create mode 100644 XMLReader.h create mode 100644 main.cpp create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 mainwindow.ui diff --git a/DatabaseInitializer.pro b/DatabaseInitializer.pro new file mode 100644 index 0000000..a4876ff --- /dev/null +++ b/DatabaseInitializer.pro @@ -0,0 +1,29 @@ +QT += core gui +QT += xml + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# The following define makes your compiler emit warnings if you use +# any Qt feature that has been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += $$files(*.cpp, true) \ + +HEADERS += $$files(*.h, true) + +FORMS += \ + mainwindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/XMLReader.cpp b/XMLReader.cpp new file mode 100644 index 0000000..46a6a14 --- /dev/null +++ b/XMLReader.cpp @@ -0,0 +1,188 @@ +#include "XMLReader.h" + +/*****************************************************************************/ +/** + * @brief Constructor + * @details + * + * @return Nothing + */ +/*****************************************************************************/ +XMLReader::XMLReader(QString xmlAddress) +{ + QFile xmlFile(xmlAddress); + if(!xmlFile.open(QIODevice::ReadOnly)) + { + //"XML file not find" + } + if (!_xmlDoc.setContent(&xmlFile)) + { + // "Oops!!, The file is not a xml file." + } + xmlFile.close(); + getDatabaseNameList(); +} + + + +/*****************************************************************************/ +/** + * @brief Get a list of databases's Names + * @details + * + * @return QVector DBNames + */ +/*****************************************************************************/ +QVector XMLReader::getDatabaseNameList() +{ + _DBElementVector.clear(); + _root = _xmlDoc.firstChildElement(); + QDomNodeList DBs = _root.elementsByTagName("Database"); + for (int DBIndex = 0; DBIndex < DBs.count(); ++DBIndex ) + { + QDomElement DBElement = DBs.at(DBIndex).toElement(); + _DBNameVector.append(DBElement.attribute("name")); + _DBElementVector.append(DBElement); + } + return _DBNameVector; +} + +/*****************************************************************************/ +/** + * @brief Method to get a list of a database's tables + * @details + * + * @return QVector tblNmaes + */ +/*****************************************************************************/ +QVector XMLReader::getDBTableList(QString DBName) +{ + _tableNamevector.clear(); + bool wasDB = false; + for (int i = 0; i < _DBElementVector.count(); ++i) + { + if(_DBElementVector.at(i).attribute("name") == DBName) + { + wasDB = true; + if(!_DBElementVector.at(i).isElement()) + { + QString logText = "Oops!!, The xml file is incorrect. Error was at : " + + _DBElementVector.at(i).text(); + //Erorr logText + break; + + } + QDomNodeList tblNodes = _DBElementVector.at(i).elementsByTagName("Table"); + for (int tindex = 0; tindex < tblNodes.count(); ++tindex) + { + QDomElement tableElement = tblNodes.at(tindex).toElement(); + _tableElementVector.append(tableElement); + _tableNamevector.append(tableElement.attribute("name")); + } + break; + } + } + if(!wasDB) + { + QString logText = "Oops!!, The xml file does not have Database Tage by name : " + + DBName; + //Erorr logText + return _tableNamevector ; + } + if( _tableNamevector.count() == 0) + { + QString logText = "Oops!!, The " + + DBName + + "exist but does not have any table"; + //Erorr logText + return _tableNamevector ; + } + + return _tableNamevector ; +} + + +/*****************************************************************************/ +/** + * @brief Method to get a list of a database's tables + * @details + * + * @return QVector tblNmaes + */ +/*****************************************************************************/ +QVector XMLReader::getDBTableList(QDomElement DBElement) +{ + _tableNamevector.clear(); + QDomNodeList tblNodes = DBElement.elementsByTagName("Table"); + for (int tblIndex = 0; tblIndex < tblNodes.count(); ++tblIndex) + { + if(!tblNodes.at(tblIndex).isElement()) + { + QString logText = "Oops!!, The xml Database Tage by name : " + + DBElement.attribute("name") + + " is not xml element"; + //Error logText + break; + } + QDomElement tableElement = tblNodes.at(tblIndex).toElement(); + _tableNamevector.append(tableElement.attribute("name")); + } + if( _tableNamevector.count() == 0) + { + QString logText = "Oops!!, The " + + DBElement.attribute("name") + + "exist but does not have any table"; + //Error logText + + } + return _tableNamevector; +} +QVector XMLReader::getLastFieldElements() +{ + return _fieldElementVector; +} + +QVector XMLReader::getFieldElements(QDomElement tableElement) +{ + _fieldElementVector.clear(); + QDomNodeList fieldNodes = tableElement.elementsByTagName("Field"); + if(fieldNodes .count() > 0) + { + for(int i = 0; i < fieldNodes.count(); i++) + { + _fieldElementVector.append(fieldNodes.at(i).toElement()); + } + } + else + { + //Error there is no Field. + } + return _fieldElementVector; +} +QVector XMLReader::getFieldElements(QString tableName) +{ + _fieldElementVector.clear(); + if(_tableElementVector.count() > 0) + { + for(int j = 0; j < _tableElementVector.count(); j++) + { + if(_tableElementVector.at(j).attribute("name") == tableName) + { + QDomNodeList fieldNodes = _tableElementVector.at(j).elementsByTagName("Field"); + if(fieldNodes .count() > 0) + { + for(int i = 0; i < fieldNodes.count(); i++) + { + _fieldElementVector.append(fieldNodes.at(i).toElement()); + } + } + else + { + //Error there is no Field. + } + break; + } + } + } + return _fieldElementVector; +} diff --git a/XMLReader.h b/XMLReader.h new file mode 100644 index 0000000..645743a --- /dev/null +++ b/XMLReader.h @@ -0,0 +1,41 @@ +#ifndef XMLREADER_H +#define XMLREADER_H +#include +#include + +class XMLReader +{ +private: + QDomDocument _xmlDoc; + QString _xmlAddress; + QDomElement _root; + QVector _DBNameVector; + QVector _tableNamevector; + QVector _tableElementVector; + QVector _DBElementVector; + QVector _fieldElementVector; +// XMLReader operator=(const XMLReader& ixml) +// { +// _xmlDoc = ixml._xmlDoc; +// _xmlAddress = ixml._xmlAddress; +// _root = ixml._root; +// _DBNameVector = ixml._DBNameVector; +// _DBElementVector = ixml._DBElementVector; +// _tableNamevector = ixml._tableNamevector; +// _tableElementVector = ixml._tableElementVector; +// _fieldElementVector = ixml._fieldElementVector; +// } + +public: + XMLReader(QString xmlAddress); + QVector getDatabaseNameList(); + QVector getDBTableList(QString DBName); + QVector getDBTableList(QDomElement DBElement); + QVector getFieldElements(QDomElement tableElement); + QVector getFieldElements(QString tableName); + QVector getLastFieldElements(); + + +}; + +#endif // XMLREADER_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..1979f74 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,304 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); + _model = new QStandardItemModel(0,1,this); + ui->treeView->setModel(_model); + hideWidgets(); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::readFile() +{ + _model->clear(); + QStandardItem *root = new QStandardItem("root:Root"); + _model->appendRow(root); + + QDomDocument document; + + //load file + QFile file(_fileName); + if(file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + document.setContent(&file); + file.close(); + } + + //read xml file + QDomElement xmlroot = document.firstChildElement(); + + QDomNodeList Databases = xmlroot.elementsByTagName("Database"); + for (int i = 0; i < Databases.count(); ++i) + { + QDomElement database= Databases.at(i).toElement(); + _DBElementVector.append(database); + _DBNameVector.append(database.attribute("name")); + QStandardItem *databaseItem = new QStandardItem(QString("DB : %1").arg(database.attribute("name"))); + + //read tables + QDomNodeList tables = database.elementsByTagName("Table"); + for (int t = 0; t < tables.count(); ++t) + { + QDomElement table = tables.at(t).toElement(); + QStandardItem *tableItem = new QStandardItem(QString("TBL : %1").arg(table.attribute("name"))); + + //read Field +// QDomNodeList fields = table.elementsByTagName("Field"); +// for (int f = 0; f < fields.count(); ++f) +// { +// QDomElement field = fields.at(f).toElement(); +// QStandardItem *fieldItem = new QStandardItem(QString("FLD : %1").arg(field.attribute("name"))); +//// "Name: "+field.attribute("name") + "; Type: " + field.attribute("type") + "; Allow Null: " + field.attribute("null") + "; Is Forign key:" + field.attribute("isforeignkey")); + +// tableItem->appendRow(fieldItem); +// } + databaseItem->appendRow(tableItem); + } + root->appendRow(databaseItem); + } + _xmlDoc = document; +} + +void MainWindow::on_actionOpen_triggered() +{ + _fileName = QFileDialog::getOpenFileName(this, + tr("Open XML file"), "/home/", + tr("Address XML (*.xml);;All Files (*)")); + this->_xmlReader =new XMLReader(_fileName); + readFile(); +} + + +MainWindow::eElementType MainWindow::getElementType(QModelIndex indInput) +{ + auto dataSptd = indInput.data().toString().split(" : "); + + if(dataSptd.at(0) == "root") + return MainWindow::eElementType::root; + else if(dataSptd.at(0) == "DB") + return MainWindow::eElementType::database; + else if(dataSptd.at(0) == "TBL") + return MainWindow::eElementType::table; + else if(dataSptd.at(0) == "FLD") + return MainWindow::eElementType::field; + else + return MainWindow::eElementType::unknown; +} + +void MainWindow::on_treeView_doubleClicked(const QModelIndex &index) +{ + //hideWidgets(); + auto type = getElementType(index); + switch (type) + { + case MainWindow::eElementType::root: + { + break; + } + case MainWindow::eElementType::database: + { + break; + } + case MainWindow::eElementType::table : + { + auto database = index.parent(); + _lastDBElement.clear(); + for (int i = 0; i < _xmlDoc.firstChild().childNodes().count(); ++i) + { + if(_xmlDoc.firstChild().childNodes().at(i).toElement().attribute("name") == database.data().toString().split(" : ").at(1)) + { + _lastDBElement = _xmlDoc.firstChild().childNodes().at(i).toElement(); + break; + } + } + _xmlReader->getDBTableList(_lastDBElement.attribute("name")); + for (int i = 0; i < _lastDBElement.childNodes().count(); ++i) + { + auto tblItem = _lastDBElement.childNodes().at(i); + auto elem = tblItem.toElement(); + if(elem.attribute("name") == index.data().toString().split(" : ").at(1)) + { + _fieldElemensVector.clear(); + _lastTableElement = elem; + _fieldElemensVector = _xmlReader->getFieldElements(elem.attribute("name")); + qDebug() << _fieldElemensVector.count(); + qDebug() << elem.attribute("name"); + qDebug() << _lastTableElement.attribute("name"); + } + } + ui->pushButton->setText("Create Init"); + ui->pushButton->show(); + break; + } + } + + +} + +void MainWindow::on_pushButton_clicked() +{ + qDebug() << _lastTableElement.attribute("name"); + hideWidgets(); + if(_xmlInitDataDoc.isNull()) + { + //create root element + _rootInitData = _xmlInitDataDoc.createElement("Root"); + _xmlInitDataDoc.appendChild(_rootInitData); + } + if(! hasDatabaseChild(_rootInitData,_lastDBElement.attribute("name"))) + { + //create database element + _lastDBElementInitData = _xmlInitDataDoc.createElement("Database"); + _lastDBElementInitData.setAttribute("name",_lastDBElement.attribute("name")); + _rootInitData.appendChild(_lastDBElementInitData); + } + else + { + auto dbs = _rootInitData.elementsByTagName("Database"); + for (int i = 0; i < dbs.count(); ++i) + { + if(dbs.at(i).toElement().attribute("name") == _lastDBElement.attribute("name")) + { + _lastDBElementInitData = dbs.at(i).toElement(); + break; + } + } + } + qDebug() << _lastTableElement.attribute("name"); + if(!hasTableChild(_lastDBElementInitData,_lastTableElement.attribute("name"))) + { + _lastTableElementInitData = _xmlInitDataDoc.createElement("Table"); + qDebug()<<_lastTableElement.attribute("name"); + _lastTableElementInitData.setAttribute("name", _lastTableElement.attribute("name")); + _lastDBElementInitData.appendChild(_lastTableElementInitData); + } + else + { + auto tbls = _lastDBElementInitData.elementsByTagName("Table"); + for (int i = 0; i < tbls.count(); ++i) + { + if(tbls.at(i).toElement().attribute("name") == _lastTableElement.attribute("name")) + { + _lastTableElementInitData = tbls.at(i).toElement(); + break; + } + } + } + _lastEntityElementInitData = _xmlInitDataDoc.createElement("entity"); + _lastTableElementInitData.appendChild(_lastEntityElementInitData); + _fieldIndex = 0; + initUI(); + + +} + + +void MainWindow::initUI() +{ + hideWidgets(); + ui->label_3->setText("put data into line Edit."); + ui->label_3->show(); + auto fieldElement = _fieldElemensVector.at(_fieldIndex).toElement(); + ui->label_4->setText(fieldElement.attribute("name")); + ui->label_4->show(); +// if(fieldElement.attribute("type").toLower().contains("varchar")) +// { +// ui->label_2->show(); +// ui->comboBox->itemData(0); +// ui->comboBox->show(); +// } + ui->lineEdit->clear(); + ui->lineEdit->show(); + ui->label->show(); + if(_fieldIndex < _fieldElemensVector.count()-1) + { + ui->pushButton_3->setText("Next Field"); + ui->pushButton_3->show(); + } + else if(_fieldIndex == _fieldElemensVector.count() - 1) + { + ui->pushButton_3->setText("Done"); + ui->pushButton_3->show(); + } +} + +bool MainWindow::hasDatabaseChild(QDomElement xmlRoot, QString DBName) +{ + auto dbs = xmlRoot.elementsByTagName("Database"); + for (int i = 0; i < dbs.count(); ++i) + { + if(dbs.at(i).toElement().attribute("name") == DBName) + return true; + } + return false; +} +bool MainWindow::hasTableChild(QDomElement database, QString tableName) +{ + auto tbls = database.elementsByTagName("Table"); + for (int i = 0; i < tbls.count(); ++i) + { + if(tbls.at(i).toElement().attribute("name") == tableName) + return true; + } + return false; +} +void MainWindow::hideWidgets() +{ + ui->label->hide(); + ui->label_2->hide(); +// ui->label_3->hide(); + ui->label_4->hide(); + ui->pushButton->hide(); + ui->pushButton_2->hide(); + ui->pushButton_3->hide(); + ui->lineEdit->hide(); + ui->comboBox->hide(); +} + +void MainWindow::on_pushButton_3_clicked() +{ + auto sourceField = _fieldElemensVector.at(_fieldIndex).toElement(); + _lastFieldElementInitData = _xmlInitDataDoc.createElement("Field"); + _lastFieldElementInitData.setAttribute("name", sourceField.attribute("name")); + _lastFieldElementInitData.setAttribute("type", sourceField.attribute("type")); + _lastFieldElementInitData.setAttribute("value",ui->lineEdit->text()); + _lastEntityElementInitData.appendChild(_lastFieldElementInitData); + _fieldIndex++; + if(_fieldIndex < _fieldElemensVector.count()) + { + + initUI(); + } + else + { + hideWidgets(); + } +} + + +void MainWindow::writeFile() +{ + _fileName = QFileDialog::getSaveFileName(this); + QFile savefile(_fileName); + if(!savefile.open(QIODevice::WriteOnly | QIODevice::Text)) + { + qDebug()<< "Ops! can not create file"; + } + QTextStream stream(&savefile); + stream<<_xmlInitDataDoc.toString(); + savefile.close(); + +} + +void MainWindow::on_actionSave_triggered() +{ + writeFile(); +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..905e1da --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,81 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include +#include +#include +#include "XMLReader.h" + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + enum eElementType:quint8 + { + root, + database, + table, + field, + unknown + }; + + + + +private slots: + void on_actionOpen_triggered(); + + void on_treeView_doubleClicked(const QModelIndex &index); + + void on_pushButton_clicked(); + + void on_pushButton_3_clicked(); + + void on_actionSave_triggered(); + +private: + Ui::MainWindow *ui; + QStandardItemModel *_model; + QString _fileName; + //dbData + QDomDocument _xmlDoc; + QDomElement _rootData; + QDomElement _lastDBElement; + QDomElement _lastTableElement; + QVector _fieldElemensVector; + // + QString _xmlDBAddress; + //initData + QDomDocument _xmlInitDataDoc; + QDomElement _rootInitData; + QDomElement _lastDBElementInitData; + QDomElement _lastTableElementInitData; + QDomElement _lastEntityElementInitData; + QDomElement _lastFieldElementInitData; + XMLReader* _xmlReader; + + QVector _DBNameVector; + QVector _DBElementVector; + eElementType getElementType(QModelIndex indInput); + bool hasDatabaseChild(QDomElement xmlRoot, QString DBName); + bool hasTableChild(QDomElement database, QString tableName); + void initUI(); + int _fieldIndex = 0; + + + + void readFile(); + void writeFile(); + void hideWidgets(); +}; +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..689e318 --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,177 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + 0 + 220 + 791 + 331 + + + + + + + 11 + 55 + 89 + 25 + + + + PushButton + + + + + + 590 + 190 + 89 + 25 + + + + PushButton + + + + + + 690 + 190 + 89 + 25 + + + + PushButton + + + + + + 10 + 170 + 47 + 17 + + + + Input : + + + + + + 120 + 160 + 651 + 25 + + + + + + + 11 + 119 + 40 + 17 + + + + Type : + + + + + + 120 + 120 + 111 + 25 + + + + + + + 11 + 86 + 331 + 17 + + + + TextLabel + + + + + + 11 + 21 + 491 + 17 + + + + TextLabel + + + + + + + 0 + 0 + 800 + 22 + + + + + File + + + + + + + + + + + Open + + + + + Save + + + + + Close + + + + + +