-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5bd88de
commit 60254fe
Showing
17 changed files
with
1,118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.o | ||
Makefile | ||
*.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
# XmlParser | ||
GUI program parse xml files, mapping in QTableView and save in SQLite. Qt5 | ||
|
||
#For build | ||
qmake | ||
make | ||
|
||
#For run | ||
./XmlParser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2018-11-21T12:15:31 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT += core gui xml sql | ||
|
||
CONFIG += c++11 | ||
|
||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
|
||
TARGET = XmlParser | ||
TEMPLATE = app | ||
|
||
|
||
SOURCES += main.cpp\ | ||
mainwindow.cpp \ | ||
database.cpp \ | ||
parserxmlworker.cpp \ | ||
dialogeditrecord.cpp | ||
|
||
HEADERS += mainwindow.h \ | ||
database.h \ | ||
parserxmlworker.h \ | ||
dialogeditrecord.h | ||
|
||
FORMS += mainwindow.ui \ | ||
dialogeditrecord.ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
#include "database.h" | ||
|
||
DataBase::DataBase(QObject *parent) : QObject(parent) | ||
{ | ||
} | ||
|
||
|
||
void DataBase::connectToDataBase() | ||
{ | ||
if(!QFile(DATABASE_NAME).exists()) | ||
{ | ||
this->restoreDataBase(); | ||
} | ||
else | ||
{ | ||
this->openDataBase(); | ||
} | ||
} | ||
|
||
|
||
/* Методы восстановления базы данных | ||
* */ | ||
bool DataBase::restoreDataBase() | ||
{ | ||
if(this->openDataBase()) | ||
{ | ||
if(!this->createTable()) | ||
{ | ||
return false; | ||
} | ||
else | ||
{ | ||
return true; | ||
} | ||
} | ||
else | ||
{ | ||
qDebug() << "Не удалось восстановить базу данных"; | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
/* Метод для открытия базы данных */ | ||
bool DataBase::openDataBase() | ||
{ | ||
/* База данных открывается по заданному пути | ||
* и имени базы данных, если она существует | ||
* */ | ||
db = QSqlDatabase::addDatabase("QSQLITE"); | ||
db.setHostName(HOST_NAME); | ||
db.setDatabaseName(DATABASE_NAME); | ||
|
||
if(db.open()) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
bool DataBase::createTable() | ||
{ | ||
QSqlQuery query; | ||
QString strCreate = "CREATE TABLE "; | ||
strCreate +=TABLE; | ||
strCreate += " (name INTEGER, value VARCHAR(255));"; | ||
|
||
if(!query.exec(strCreate)) | ||
{ | ||
qDebug() << "Error crate table " << query.lastError(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void DataBase::printTables() | ||
{ | ||
foreach(QString str, db.tables()) | ||
{ | ||
qDebug() << "Tables:" << str; | ||
} | ||
} | ||
|
||
void DataBase::doInsert(QMultiMap<QString, QString> *arrayXml) | ||
{ | ||
int count = 0; | ||
|
||
//doCleanData(); | ||
createTable(); | ||
|
||
QSqlQuery query; | ||
//Insert data in database | ||
//QString strInsert = "INSERT INTO "; | ||
//strInsert += TABLE; | ||
//strInsert += " (name, value) VALUES('%1', '%2');"; | ||
|
||
QString strInsert = "INSERT INTO "; | ||
strInsert += TABLE; | ||
strInsert += " (name, value) VALUES(:name, :value)"; | ||
|
||
query.prepare(strInsert); | ||
|
||
for(QMultiMap<QString, QString>::iterator iii = arrayXml->begin(); iii != arrayXml->end(); iii++) | ||
{ | ||
//QString strInsert2 = strInsert.arg(iii.key()) | ||
// .arg(iii.value()); | ||
|
||
query.bindValue(":name", iii.key()); | ||
query.bindValue(":value", iii.value()); | ||
|
||
if(!query.exec()) | ||
{ | ||
qDebug() << "Error insert data"; | ||
break; | ||
} | ||
|
||
if((count % 10) == 0) | ||
{ | ||
|
||
emit changeProgressInsert(count); | ||
} | ||
count++; | ||
|
||
} | ||
|
||
|
||
|
||
emit changeProgressInsert(arrayXml->size()); | ||
} | ||
|
||
void DataBase::doCleanData() | ||
{ | ||
QSqlQuery query; | ||
|
||
QString strDrop = "DROP TABLE "; | ||
strDrop += TABLE; | ||
strDrop += " ;"; | ||
|
||
if(!query.exec(strDrop)) | ||
{ | ||
qDebug() << "Error drop table " << query.lastError(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef DATABASE_H | ||
#define DATABASE_H | ||
|
||
#include <QObject> | ||
#include <QtSql> | ||
|
||
#define DATABASE_NAME "my_sqllite" | ||
#define USER_NAME "user" | ||
#define HOST_NAME "127.0.0.1" | ||
#define PASSWORD "1" | ||
|
||
#define TABLE "xmldata" | ||
|
||
|
||
/* Вспомогтаельный класс для работы с БД */ | ||
class DataBase : public QObject | ||
{ | ||
Q_OBJECT | ||
bool restoreDataBase(); | ||
bool openDataBase(); | ||
|
||
QSqlDatabase db; | ||
|
||
public: | ||
explicit DataBase(QObject *parent = 0); | ||
void connectToDataBase(); | ||
void printTables(); | ||
|
||
bool createTable(); | ||
|
||
signals: | ||
void changeProgressInsert(int value); | ||
|
||
public slots: | ||
void doInsert(QMultiMap<QString, QString> *arrayXml); | ||
void doCleanData(); | ||
|
||
}; | ||
|
||
#endif // DATABASE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "dialogeditrecord.h" | ||
#include "ui_dialogeditrecord.h" | ||
|
||
DialogEditRecord::DialogEditRecord(int row, QWidget *parent) : | ||
QDialog(parent), | ||
ui(new Ui::DialogEditRecord) | ||
{ | ||
ui->setupUi(this); | ||
|
||
setupModel(); | ||
|
||
if(row != -1) | ||
{ | ||
mapper->setCurrentModelIndex(model->index(row,0)); | ||
} | ||
|
||
} | ||
|
||
DialogEditRecord::~DialogEditRecord() | ||
{ | ||
delete ui; | ||
} | ||
|
||
void DialogEditRecord::setupModel() | ||
{ | ||
// Инициализируем модель и делаем выборку | ||
model = new QSqlTableModel(this); | ||
model->setTable(TABLE); | ||
model->setEditStrategy(QSqlTableModel::OnManualSubmit); | ||
model->select(); | ||
|
||
// Инициализируем mapper и привязываем поля данных к объектам LineEdit | ||
mapper = new QDataWidgetMapper(); | ||
mapper->setModel(model); | ||
|
||
mapper->addMapping(ui->lineEditName, 0); | ||
mapper->addMapping(ui->lineEditValue, 1); | ||
|
||
//mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit); | ||
//connect(ui->previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious())); | ||
//connect(ui->nextButton, SIGNAL(clicked()), mapper, SLOT(toNext())); | ||
|
||
connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtons(int))); | ||
} | ||
|
||
void DialogEditRecord::on_pushButton_clicked() | ||
{ | ||
mapper->submit(); | ||
model->submitAll(); | ||
emit readyToUpdate(); | ||
this->close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef DIALOGEDITRECORD_H | ||
#define DIALOGEDITRECORD_H | ||
|
||
#include "database.h" | ||
|
||
#include <QDialog> | ||
#include <QSqlTableModel> | ||
#include <QDataWidgetMapper> | ||
#include <QSqlTableModel> | ||
#include <QDataWidgetMapper> | ||
|
||
|
||
|
||
namespace Ui { | ||
class DialogEditRecord; | ||
} | ||
|
||
class DialogEditRecord : public QDialog | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit DialogEditRecord(int row, QWidget *parent = 0); | ||
~DialogEditRecord(); | ||
|
||
signals: | ||
void readyToUpdate(); | ||
|
||
private slots: | ||
void on_pushButton_clicked(); | ||
|
||
private: | ||
void setupModel(); | ||
|
||
Ui::DialogEditRecord *ui; | ||
QSqlTableModel *model; | ||
QDataWidgetMapper *mapper; | ||
}; | ||
|
||
#endif // DIALOGEDITRECORD_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>DialogEditRecord</class> | ||
<widget class="QDialog" name="DialogEditRecord"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>247</width> | ||
<height>157</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>Dialog</string> | ||
</property> | ||
<layout class="QFormLayout" name="formLayout_2"> | ||
<item row="0" column="0"> | ||
<layout class="QFormLayout" name="formLayout"> | ||
<item row="0" column="1"> | ||
<widget class="QLineEdit" name="lineEditName"/> | ||
</item> | ||
<item row="2" column="1"> | ||
<widget class="QLineEdit" name="lineEditValue"/> | ||
</item> | ||
<item row="0" column="0"> | ||
<widget class="QLabel" name="label"> | ||
<property name="text"> | ||
<string>Имя</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item row="2" column="0"> | ||
<widget class="QLabel" name="label_2"> | ||
<property name="text"> | ||
<string>Значение</string> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</item> | ||
<item row="1" column="0" colspan="2"> | ||
<widget class="QPushButton" name="pushButton"> | ||
<property name="text"> | ||
<string>Запись</string> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</widget> | ||
<resources/> | ||
<connections/> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
Oops, something went wrong.