You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

304 lines
9.3 KiB

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