Browse Source

first commit

master
mmtalaie 4 years ago
commit
48e0706275
  1. 29
      DatabaseInitializer.pro
  2. 188
      XMLReader.cpp
  3. 41
      XMLReader.h
  4. 11
      main.cpp
  5. 304
      mainwindow.cpp
  6. 81
      mainwindow.h
  7. 177
      mainwindow.ui

29
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

188
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<QString> DBNames
*/
/*****************************************************************************/
QVector<QString> 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<QString> tblNmaes
*/
/*****************************************************************************/
QVector<QString> 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<QString> tblNmaes
*/
/*****************************************************************************/
QVector<QString> 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<QDomElement> XMLReader::getLastFieldElements()
{
return _fieldElementVector;
}
QVector<QDomElement> 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<QDomElement> 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;
}

41
XMLReader.h

@ -0,0 +1,41 @@
#ifndef XMLREADER_H
#define XMLREADER_H
#include <QObject>
#include <QtXml>
class XMLReader
{
private:
QDomDocument _xmlDoc;
QString _xmlAddress;
QDomElement _root;
QVector<QString> _DBNameVector;
QVector<QString> _tableNamevector;
QVector<QDomElement> _tableElementVector;
QVector<QDomElement> _DBElementVector;
QVector<QDomElement> _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<QString> getDatabaseNameList();
QVector<QString> getDBTableList(QString DBName);
QVector<QString> getDBTableList(QDomElement DBElement);
QVector<QDomElement> getFieldElements(QDomElement tableElement);
QVector<QDomElement> getFieldElements(QString tableName);
QVector<QDomElement> getLastFieldElements();
};
#endif // XMLREADER_H

11
main.cpp

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

304
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();
}

81
mainwindow.h

@ -0,0 +1,81 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
#include <QtCore>
#include <QtGui>
#include <QtXml>
#include <QDebug>
#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<QDomElement> _fieldElemensVector;
//
QString _xmlDBAddress;
//initData
QDomDocument _xmlInitDataDoc;
QDomElement _rootInitData;
QDomElement _lastDBElementInitData;
QDomElement _lastTableElementInitData;
QDomElement _lastEntityElementInitData;
QDomElement _lastFieldElementInitData;
XMLReader* _xmlReader;
QVector<QString> _DBNameVector;
QVector<QDomElement> _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

177
mainwindow.ui

@ -0,0 +1,177 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTreeView" name="treeView">
<property name="geometry">
<rect>
<x>0</x>
<y>220</y>
<width>791</width>
<height>331</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>11</x>
<y>55</y>
<width>89</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>590</x>
<y>190</y>
<width>89</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_3">
<property name="geometry">
<rect>
<x>690</x>
<y>190</y>
<width>89</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>170</y>
<width>47</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>Input : </string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>120</x>
<y>160</y>
<width>651</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>11</x>
<y>119</y>
<width>40</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>Type :</string>
</property>
</widget>
<widget class="QComboBox" name="comboBox">
<property name="geometry">
<rect>
<x>120</x>
<y>120</y>
<width>111</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>11</x>
<y>86</y>
<width>331</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>11</x>
<y>21</y>
<width>491</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionOpen"/>
<addaction name="actionSave"/>
<addaction name="actionClose"/>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionOpen">
<property name="text">
<string>Open</string>
</property>
</action>
<action name="actionSave">
<property name="text">
<string>Save</string>
</property>
</action>
<action name="actionClose">
<property name="text">
<string>Close</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
Loading…
Cancel
Save