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.
592 lines
18 KiB
592 lines
18 KiB
#include <QFileDialog>
|
|
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
#include "state.h"
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
, ui(new Ui::MainWindow)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
//create model
|
|
hideWidgets();
|
|
_model = new QStandardItemModel(0,1,this);
|
|
ui->treeView->setModel(_model);
|
|
}
|
|
|
|
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::readFile(QDomDocument xmlInput)
|
|
{
|
|
_model->clear();
|
|
QStandardItem *root = new QStandardItem("root:Root");
|
|
_model->appendRow(root);
|
|
|
|
QDomDocument document;
|
|
document = xmlInput;
|
|
|
|
//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();
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
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<<_xmlDoc.toString();
|
|
savefile.close();
|
|
|
|
}
|
|
|
|
|
|
void MainWindow::on_actionCreate_Database_triggered()
|
|
{
|
|
hideWidgets();
|
|
_state = eStateLevel::createDatabase;
|
|
if(_xmlDoc.isNull())
|
|
{
|
|
_root = _xmlDoc.createElement("Root");
|
|
_xmlDoc.appendChild(_root);
|
|
|
|
}
|
|
else
|
|
{
|
|
_root = _xmlDoc.firstChild().toElement();
|
|
}
|
|
|
|
readFile(_xmlDoc);
|
|
|
|
|
|
ui->label_1->setText("Database Name");
|
|
ui->label_1->show();
|
|
ui->lineEdit_1->show();
|
|
ui->Append_btn->show();
|
|
}
|
|
|
|
void MainWindow::on_actionAdd_Table_triggered()
|
|
{
|
|
|
|
if(!_xmlDoc.isNull())
|
|
{
|
|
hideWidgets();
|
|
_state = eStateLevel::createTable;
|
|
ui->label_1->setText("Table Name");
|
|
ui->label_1->show();
|
|
ui->lineEdit_1->show();
|
|
ui->Append_btn->show();
|
|
}
|
|
}
|
|
|
|
void MainWindow::on_actionAdd_Field_triggered()
|
|
{
|
|
|
|
if(!_xmlDoc.isNull() /*&& !_lastDBElement.isNull()*/)
|
|
{
|
|
hideWidgets();
|
|
_state = eStateLevel::createField;
|
|
ui->label_1->setText("Field Name :");
|
|
ui->cmbType->show();
|
|
ui->label_1->show();
|
|
ui->lineEdit_1->show();
|
|
ui->Append_btn->show();
|
|
ui->chbIsNull->show();
|
|
ui->chbIsForignKey->show();
|
|
}
|
|
|
|
}
|
|
|
|
void MainWindow::on_chbIsForignKey_stateChanged(int arg1)
|
|
{
|
|
if(arg1 > 0)
|
|
{
|
|
ui->cmbTablsName->addItem("");
|
|
for (int i = 0; i < _lastDBElement.childNodes().count(); ++i)
|
|
{
|
|
auto tblElem = _lastDBElement.childNodes().at(i);
|
|
ui->cmbTablsName->addItem(tblElem.toElement().attribute("name"));
|
|
}
|
|
ui->cmbTablsName->show();
|
|
}
|
|
else
|
|
{
|
|
ui->cmbTablsName->hide();
|
|
}
|
|
}
|
|
|
|
void MainWindow::hideWidgets()
|
|
{
|
|
ui->label_1->hide();
|
|
ui->label_2->hide();
|
|
ui->label_3->hide();
|
|
ui->label_4->hide();
|
|
|
|
ui->lineEdit_1->hide();
|
|
ui->lineEdit_2->hide();
|
|
ui->lineEdit_3->hide();
|
|
ui->lineEdit_4->hide();
|
|
|
|
ui->Append_btn->setText("Append");
|
|
ui->Append_btn->hide();
|
|
|
|
ui->cmbType->hide();
|
|
ui->chbIsNull->hide();
|
|
ui->chbIsForignKey->hide();
|
|
ui->cmbTablsName->hide();
|
|
|
|
}
|
|
|
|
void MainWindow::on_Append_btn_clicked()
|
|
{
|
|
switch (_state)
|
|
{
|
|
case eStateLevel::createDatabase:
|
|
{
|
|
_lastDBElement =_xmlDoc.createElement("Database");
|
|
auto dbName = ui->lineEdit_1->text();
|
|
_lastDBElement.setAttribute("name",dbName);
|
|
_DBElementVector.append(_lastDBElement);
|
|
_root.appendChild(_lastDBElement);
|
|
_xmlDoc.childNodes().at(0) = _root;
|
|
readFile(_xmlDoc);
|
|
hideWidgets();
|
|
ui->lineEdit_1->setText("");
|
|
break;
|
|
}
|
|
case eStateLevel::createTable:
|
|
{
|
|
_lastTableElement = _xmlDoc.createElement("Table");
|
|
auto tableName = ui->lineEdit_1->text();
|
|
_lastTableElement.setAttribute("name",tableName);
|
|
_lastDBElement.appendChild(_lastTableElement);
|
|
readFile(_xmlDoc);
|
|
ui->lineEdit_1->setText("");
|
|
hideWidgets();
|
|
break;
|
|
}
|
|
case eStateLevel::createField:
|
|
{
|
|
if(_lastTableElement.isNull() || _lastDBElement.isNull())
|
|
{
|
|
//TODO :: show mesagebox Text "Please select Database and Table"
|
|
break;
|
|
}
|
|
if(ui->lineEdit_1->text() == "")
|
|
{
|
|
//TODO :: show mesagebox Text "Please set the field name"
|
|
break;
|
|
}
|
|
auto a = ui->cmbType->currentIndex();
|
|
if(a < 1)
|
|
{
|
|
//TODO :: show mesagebox Text "Please set the Type"
|
|
break;
|
|
}
|
|
auto fieldName = ui->lineEdit_1->text();
|
|
auto field = _xmlDoc.createElement("Field");
|
|
field.setAttribute("name",fieldName);
|
|
if(ui->cmbType->currentText() == "VARCHAR")
|
|
{
|
|
field.setAttribute("type",QString("%1(%2)").arg(ui->cmbType->currentText()).arg(ui->lineEdit_2->text()));
|
|
}
|
|
else
|
|
{
|
|
field.setAttribute("type",ui->cmbType->currentText());
|
|
}
|
|
if(ui->chbIsNull->checkState() == Qt::CheckState::Unchecked)
|
|
field.setAttribute("null","NOT");
|
|
if(ui->chbIsForignKey->checkState() == Qt::CheckState::Checked)
|
|
{
|
|
field.setAttribute("isforeignkey","true");
|
|
field.setAttribute("lookuptable",ui->cmbTablsName->currentText());
|
|
field.setAttribute("lookuptable","ID");
|
|
}
|
|
else
|
|
{
|
|
field.setAttribute("isforeignkey","false");
|
|
}
|
|
|
|
_lastTableElement.appendChild(field);
|
|
|
|
readFile(_xmlDoc);
|
|
ui->lineEdit_1->setText("");
|
|
hideWidgets();
|
|
|
|
break;
|
|
}
|
|
case eStateLevel::editDatabase:
|
|
{
|
|
|
|
auto oldDB = _lastDBElement;
|
|
_lastDBElement.setAttribute("name",ui->lineEdit_1->text());
|
|
auto root = _xmlDoc.firstChildElement().replaceChild(_lastDBElement,oldDB);
|
|
readFile(_xmlDoc);
|
|
ui->lineEdit_1->setText("");
|
|
hideWidgets();
|
|
break;
|
|
}
|
|
case eStateLevel::editTable:
|
|
{
|
|
|
|
auto oldTbl = _lastTableElement;
|
|
_lastTableElement.setAttribute("name",ui->lineEdit_1->text());
|
|
auto root = _lastDBElement.replaceChild(_lastTableElement,oldTbl);
|
|
readFile(_xmlDoc);
|
|
ui->lineEdit_1->setText("");
|
|
hideWidgets();
|
|
break;
|
|
}
|
|
case eStateLevel::editField:
|
|
{
|
|
if(_lastTableElement.isNull() || _lastDBElement.isNull())
|
|
{
|
|
//TODO :: show mesagebox Text "Please select Database and Table"
|
|
break;
|
|
}
|
|
if(ui->lineEdit_1->text() == "")
|
|
{
|
|
//TODO :: show mesagebox Text "Please set the field name"
|
|
break;
|
|
}
|
|
auto a = ui->cmbType->currentIndex();
|
|
if(a < 1)
|
|
{
|
|
//TODO :: show mesagebox Text "Please set the Type"
|
|
break;
|
|
}
|
|
auto fieldName = ui->lineEdit_1->text();
|
|
auto field = _xmlDoc.createElement("Field");
|
|
field.setAttribute("name",fieldName);
|
|
if(ui->cmbType->currentText() == "VARCHAR")
|
|
{
|
|
field.setAttribute("type",QString("%1(%2)").arg(ui->cmbType->currentText()).arg(ui->lineEdit_2->text()));
|
|
}
|
|
else
|
|
{
|
|
field.setAttribute("type",ui->cmbType->currentText());
|
|
}
|
|
if(ui->chbIsNull->checkState() == Qt::CheckState::Unchecked)
|
|
field.setAttribute("null","NOT");
|
|
if(ui->chbIsForignKey->checkState() == Qt::CheckState::Checked)
|
|
{
|
|
field.setAttribute("isforeignkey","true");
|
|
field.setAttribute("lookuptable",ui->cmbTablsName->currentText());
|
|
field.setAttribute("lookuptable","ID");
|
|
}
|
|
else
|
|
{
|
|
field.setAttribute("isforeignkey","false");
|
|
}
|
|
auto oldFld = _lastFieldElement;
|
|
_lastTableElement.replaceChild(field,oldFld);
|
|
|
|
readFile(_xmlDoc);
|
|
ui->lineEdit_1->setText("");
|
|
hideWidgets();
|
|
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::on_cmbTablsName_currentIndexChanged(int index)
|
|
{
|
|
if(ui->chbIsForignKey->checkState() == Qt::CheckState::Checked)
|
|
{
|
|
if(index > 0 )
|
|
{
|
|
ui->lineEdit_1->setText(QString("%1_id").arg(ui->cmbTablsName->currentText()));
|
|
ui->cmbType->setCurrentIndex(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
|
|
{
|
|
hideWidgets();
|
|
auto type = getElementType(index);
|
|
switch (type)
|
|
{
|
|
case MainWindow::eElementType::root :
|
|
{
|
|
break;
|
|
}
|
|
case MainWindow::eElementType::database :
|
|
{
|
|
_state = MainWindow::eStateLevel::editDatabase;
|
|
for (int i = 0; i < _xmlDoc.firstChild().childNodes().count(); ++i)
|
|
{
|
|
if( _xmlDoc.firstChild().childNodes().at(i).toElement().attribute("name") == index.data().toString().split(" : ").at(1))
|
|
{
|
|
|
|
_lastDBElement = _xmlDoc.firstChild().childNodes().at(i).toElement();
|
|
ui->label_1->setText("Database Name: ");
|
|
ui->label_1->show();
|
|
ui->lineEdit_1->setText(_xmlDoc.firstChild().childNodes().at(i).toElement().attribute("name"));
|
|
ui->lineEdit_1->show();
|
|
ui->Append_btn->setText("edit");
|
|
ui->Append_btn->show();
|
|
break;
|
|
}
|
|
}
|
|
// _lastDBElement.clear();
|
|
break;
|
|
}
|
|
case MainWindow::eElementType::table :
|
|
{
|
|
_state = MainWindow::eStateLevel::editTable;
|
|
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;
|
|
}
|
|
}
|
|
for (int i = 0; i < _lastDBElement.childNodes().count(); ++i)
|
|
{
|
|
auto tblItem = _lastDBElement.childNodes().at(i);
|
|
_lastTableElement.clear();
|
|
auto elem = tblItem.toElement();
|
|
if(elem.attribute("name") == index.data().toString().split(" : ").at(1))
|
|
{
|
|
_lastTableElement = elem;
|
|
ui->label_1->setText("Table Name: ");
|
|
ui->label_1->show();
|
|
ui->lineEdit_1->setText(elem.attribute("name"));
|
|
ui->lineEdit_1->show();
|
|
ui->Append_btn->setText("edit");
|
|
ui->Append_btn->show();
|
|
break;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
case MainWindow::eElementType::field :
|
|
{
|
|
_state = MainWindow::eStateLevel::editField;
|
|
auto table = index.parent();
|
|
auto database = table.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;
|
|
}
|
|
}
|
|
for (int i = 0; i < _lastDBElement.childNodes().count(); ++i)
|
|
{
|
|
auto tblelem = _lastDBElement.childNodes().at(i).toElement();
|
|
_lastTableElement.clear();
|
|
if(tblelem.attribute("name") == table.data().toString().split(" : ").at(1))
|
|
{
|
|
_lastTableElement = tblelem;
|
|
break;
|
|
}
|
|
}
|
|
for (int i = 0; i < _lastTableElement.childNodes().count(); ++i)
|
|
{
|
|
_lastFieldElement = _lastTableElement.childNodes().at(i).toElement();
|
|
if(_lastFieldElement.attribute("name") == index.data().toString().split(" : ").at(1))
|
|
{
|
|
ui->label_1->setText("Field Name: ");
|
|
ui->label_1->show();
|
|
ui->lineEdit_1->setText(_lastFieldElement.attribute("name"));
|
|
ui->lineEdit_1->show();
|
|
ui->Append_btn->setText("edit");
|
|
if(_lastFieldElement.attribute("type") == "INTEGER")
|
|
ui->cmbType->setCurrentText("INT");
|
|
else if(_lastFieldElement.attribute("type").toLower().contains("varchar"))
|
|
{
|
|
ui->cmbType->setCurrentText("VARCHAR");
|
|
auto size = _lastFieldElement.attribute("type").toLower().split("(").at(1);
|
|
size.replace(")","");
|
|
ui->lineEdit_2->setText(size);
|
|
ui->lineEdit_2->show();
|
|
}
|
|
else
|
|
ui->cmbType->setCurrentText(_lastFieldElement.attribute("type"));
|
|
|
|
ui->cmbType->show();
|
|
if(_lastFieldElement.attribute("null") == "NOT")
|
|
ui->chbIsNull->setCheckState(Qt::CheckState::Unchecked);
|
|
else
|
|
{
|
|
ui->chbIsNull->setCheckState(Qt::CheckState::Checked);
|
|
}
|
|
ui->chbIsNull->show();
|
|
|
|
if(_lastFieldElement.attribute("isforeignkey") == "true")
|
|
{
|
|
ui->chbIsForignKey->show();
|
|
ui->cmbTablsName->show();
|
|
ui->chbIsForignKey->setCheckState(Qt::CheckState::Checked);
|
|
ui->cmbTablsName->setCurrentText(_lastFieldElement.attribute("lookuptable"));
|
|
}
|
|
else
|
|
{
|
|
ui->chbIsForignKey->setCheckState(Qt::CheckState::Unchecked);
|
|
}
|
|
ui->Append_btn->show();
|
|
}
|
|
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
int a = index.row();
|
|
int b = index.column();
|
|
auto parent = index.parent();
|
|
auto par0 = parent.parent();
|
|
auto ac = index.data();
|
|
qDebug()<<parent.data() << " " << par0.data();
|
|
}
|
|
|
|
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_cmbType_currentTextChanged(const QString &arg1)
|
|
{
|
|
if(arg1 == "VARCHAR")
|
|
{
|
|
ui->lineEdit_2->show();
|
|
ui->label_2->setText("Len :");
|
|
ui->label_2->show();
|
|
ui->lineEdit_2->setText("200");
|
|
}
|
|
else
|
|
{
|
|
ui->lineEdit_2->hide();
|
|
ui->label_2->setText("Len :");
|
|
ui->label_2->hide();
|
|
ui->lineEdit_2->setText("200");
|
|
}
|
|
|
|
}
|
|
|
|
void MainWindow::on_actionsave_triggered()
|
|
{
|
|
writeFile();
|
|
}
|
|
|
|
void MainWindow::on_actionEdit_Database_triggered()
|
|
{
|
|
_fileName = QFileDialog::getOpenFileName(this,
|
|
tr("Open XML file"), "/home/",
|
|
tr("Address XML (*.xml);;All Files (*)"));
|
|
readFile();
|
|
}
|
|
|