Update project database class

Add method :
addDiagram and removeDiagram
This commit is contained in:
Claveau Joshua
2020-07-06 21:52:14 +02:00
parent ff19b3d4d7
commit dae4329699
5 changed files with 118 additions and 87 deletions

View File

@@ -56,10 +56,7 @@ projectDataBase::~projectDataBase() {
/** /**
* @brief projectDataBase::updateDB * @brief projectDataBase::updateDB
* Up to date the content of the data base. * Up to date the content of the data base.
* Except at the creation of this class, * Emit the signal dataBaseUpdated
* call this method each time you want to query the data base
* to be sure that the content reflect the current state of the project.
* Emit the singal dataBaseUpdated
*/ */
void projectDataBase::updateDB() void projectDataBase::updateDB()
{ {
@@ -149,6 +146,45 @@ void projectDataBase::elementInfoChanged(Element *element)
} }
} }
void projectDataBase::addDiagram(Diagram *diagram)
{
m_insert_diagram_query.bindValue(":uuid", diagram->uuid().toString());
m_insert_diagram_query.bindValue(":pos", m_project->folioIndex(diagram));
if(!m_insert_diagram_query.exec()) {
qDebug() << "projectDataBase::addDiagram insert error : " << m_insert_diagram_query.lastError();
}
m_insert_diagram_info_query.bindValue(":uuid", diagram->uuid());
auto infos = diagram->border_and_titleblock.titleblockInformation();
for (auto key : QETApp::diagramInfoKeys())
{
if (key == "date") {
m_insert_diagram_info_query.bindValue(":date", QDate::fromString(infos.value("date").toString(), Qt::SystemLocaleShortDate));
} else {
auto value = infos.value(key);
auto bind = key.prepend(":");
m_insert_diagram_info_query.bindValue(bind, value);
}
}
if (!m_insert_diagram_info_query.exec()) {
qDebug() << "projectDataBase::addDiagram insert info error : " << m_insert_diagram_info_query.lastError();
} else {
emit dataBaseUpdated();
}
}
void projectDataBase::removeDiagram(Diagram *diagram)
{
m_remove_diagram_query.bindValue(":uuid", diagram->uuid().toString());
if (!m_remove_diagram_query.exec()) {
qDebug() << "projectDataBase::removeDiagram delete error : " << m_remove_diagram_query.lastError();
} else {
emit dataBaseUpdated();
}
}
/** /**
* @brief projectDataBase::createDataBase * @brief projectDataBase::createDataBase
* Create the data base * Create the data base
@@ -313,14 +349,12 @@ void projectDataBase::populateDiagramTable()
QSqlQuery query_(m_data_base); QSqlQuery query_(m_data_base);
query_.exec("DELETE FROM diagram"); query_.exec("DELETE FROM diagram");
QString insert_("INSERT INTO diagram (uuid, pos) VALUES (:uuid, :pos)");
query_.prepare(insert_);
for (auto diagram : m_project->diagrams()) for (auto diagram : m_project->diagrams())
{ {
query_.bindValue(":uuid", diagram->uuid().toString()); m_insert_diagram_query.bindValue(":uuid", diagram->uuid().toString());
query_.bindValue(":pos", m_project->folioIndex(diagram)); m_insert_diagram_query.bindValue(":pos", m_project->folioIndex(diagram));
if(!query_.exec()) { if(!m_insert_diagram_query.exec()) {
qDebug() << "projectDataBase::populateDiagramTable insert error : " << query_.lastError(); qDebug() << "projectDataBase::populateDiagramTable insert error : " << m_insert_diagram_query.lastError();
} }
} }
} }
@@ -391,43 +425,51 @@ void projectDataBase::populateDiagramInfoTable()
QSqlQuery query(m_data_base); QSqlQuery query(m_data_base);
query.exec("DELETE FROM diagram_info"); query.exec("DELETE FROM diagram_info");
//Prepare the query used for insert new record
QStringList bind_values;
for (auto key : QETApp::diagramInfoKeys()) {
bind_values << key.prepend(":");
}
QString insert("INSERT INTO diagram_info (diagram_uuid, " +
QETApp::diagramInfoKeys().join(", ") +
") VALUES (:uuid, " +
bind_values.join(", ") +
")");
query.prepare(insert);
for (auto *diagram : m_project->diagrams()) for (auto *diagram : m_project->diagrams())
{ {
query.bindValue(":uuid", diagram->uuid()); m_insert_diagram_info_query.bindValue(":uuid", diagram->uuid());
auto infos = diagram->border_and_titleblock.titleblockInformation(); auto infos = diagram->border_and_titleblock.titleblockInformation();
for (auto key : QETApp::diagramInfoKeys()) for (auto key : QETApp::diagramInfoKeys())
{ {
if (key == "date") { if (key == "date") {
query.bindValue(":date", QDate::fromString(infos.value("date").toString(), Qt::SystemLocaleShortDate)); m_insert_diagram_info_query.bindValue(":date", QDate::fromString(infos.value("date").toString(), Qt::SystemLocaleShortDate));
} else { } else {
auto value = infos.value(key); auto value = infos.value(key);
auto bind = key.prepend(":"); auto bind = key.prepend(":");
query.bindValue(bind, value); m_insert_diagram_info_query.bindValue(bind, value);
} }
} }
if (!query.exec()) { if (!m_insert_diagram_info_query.exec()) {
qDebug() << "projectDataBase::populateDiagramInfoTable insert error : " << query.lastError(); qDebug() << "projectDataBase::populateDiagramInfoTable insert error : " << m_insert_diagram_info_query.lastError();
} }
} }
} }
void projectDataBase::prepareQuery() void projectDataBase::prepareQuery()
{ {
//INSERT DIAGRAM
m_insert_diagram_query = QSqlQuery(m_data_base);
m_insert_diagram_query.prepare("INSERT INTO diagram (uuid, pos) VALUES (:uuid, :pos)");
//REMOVE DIAGRAM
m_remove_diagram_query = QSqlQuery(m_data_base);
m_remove_diagram_query.prepare("DELETE FROM diagram WHERE uuid=:uuid");
//INSERT DIAGRAM INFO
m_insert_diagram_info_query = QSqlQuery(m_data_base);
QStringList bind_diag_info_values;
for (auto key : QETApp::diagramInfoKeys()) {
bind_diag_info_values << key.prepend(":");
}
QString insert_diag_info("INSERT INTO diagram_info (diagram_uuid, " +
QETApp::diagramInfoKeys().join(", ") +
") VALUES (:uuid, " +
bind_diag_info_values.join(", ") +
")");
m_insert_diagram_info_query.prepare(insert_diag_info);
//INSERT ELEMENT //INSERT ELEMENT
QString insert_element_query("INSERT INTO element (uuid, diagram_uuid, pos, type, sub_type) VALUES (:uuid, :diagram_uuid, :pos, :type, :sub_type)"); QString insert_element_query("INSERT INTO element (uuid, diagram_uuid, pos, type, sub_type) VALUES (:uuid, :diagram_uuid, :pos, :type, :sub_type)");
m_insert_elements_query = QSqlQuery(m_data_base); m_insert_elements_query = QSqlQuery(m_data_base);

View File

@@ -26,6 +26,7 @@
class Element; class Element;
class QETProject; class QETProject;
class Diagram;
/** /**
* @brief The projectDataBase class * @brief The projectDataBase class
@@ -51,6 +52,8 @@ class projectDataBase : public QObject
void addElement(Element *element); void addElement(Element *element);
void removeElement(Element *element); void removeElement(Element *element);
void elementInfoChanged(Element *element); void elementInfoChanged(Element *element);
void addDiagram(Diagram *diagram);
void removeDiagram(Diagram *diagram);
signals: signals:
void dataBaseUpdated(); void dataBaseUpdated();
@@ -72,7 +75,10 @@ class projectDataBase : public QObject
QSqlQuery m_insert_elements_query, QSqlQuery m_insert_elements_query,
m_insert_element_info_query, m_insert_element_info_query,
m_remove_element_query, m_remove_element_query,
m_update_element_query; m_update_element_query,
m_insert_diagram_query,
m_remove_diagram_query,
m_insert_diagram_info_query;
public: public:
static void exportDb(projectDataBase *db, QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString()); static void exportDb(projectDataBase *db, QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString());

View File

@@ -60,7 +60,7 @@ QColor Diagram::background_color = Qt::white;
*/ */
Diagram::Diagram(QETProject *project) : Diagram::Diagram(QETProject *project) :
QGraphicsScene (project), QGraphicsScene (project),
m_project (nullptr), m_project (project),
diagram_qet_version_ (-1), diagram_qet_version_ (-1),
draw_grid_ (true), draw_grid_ (true),
use_border_ (true), use_border_ (true),
@@ -77,7 +77,6 @@ Diagram::Diagram(QETProject *project) :
//http://www.qtcentre.org/archive/index.php/t-33730.html //http://www.qtcentre.org/archive/index.php/t-33730.html
//http://tech-artists.org/t/qt-properly-removing-qgraphicitems/3063 //http://tech-artists.org/t/qt-properly-removing-qgraphicitems/3063
setProject(project);
qgi_manager_ = new QGIManager(this); qgi_manager_ = new QGIManager(this);
setBackgroundBrush(Qt::white); setBackgroundBrush(Qt::white);
conductor_setter_ = new QGraphicsLineItem(nullptr); conductor_setter_ = new QGraphicsLineItem(nullptr);
@@ -1836,20 +1835,6 @@ QETProject *Diagram::project() const {
return(m_project); return(m_project);
} }
/**
* @brief Diagram::setProject
* Set parent project of this diagram, project also become the parent QObject of this diagram
* @param project new project
*/
void Diagram::setProject(QETProject *project)
{
if (m_project == project)
return;
m_project = project;
setParent (project);
}
/** /**
@return the folio number of this diagram within its parent project, or -1 @return the folio number of this diagram within its parent project, or -1
if it is has no parent project if it is has no parent project

View File

@@ -48,15 +48,21 @@ class DiagramEventInterface;
This class represents an electric diagram. It manages its various child This class represents an electric diagram. It manages its various child
elements, conductors and texts and handles their graphic rendering. elements, conductors and texts and handles their graphic rendering.
*/ */
class DiagramFolioList;
class QETProject;
class Diagram : public QGraphicsScene class Diagram : public QGraphicsScene
{ {
friend DiagramFolioList;
friend QETProject;
Q_OBJECT Q_OBJECT
// constructors, destructor // constructors, destructor
public: private:
Diagram(QETProject *project); Diagram(QETProject *project);
~Diagram() override; ~Diagram() override;
private:
Diagram(const Diagram &diagram); Diagram(const Diagram &diagram);
// ATTRIBUTES // ATTRIBUTES
@@ -148,7 +154,6 @@ class Diagram : public QGraphicsScene
// methods related to parent project // methods related to parent project
QETProject *project() const; QETProject *project() const;
void setProject(QETProject *);
int folioIndex() const; int folioIndex() const;
qreal declaredQElectroTechVersion(bool = true) const; qreal declaredQElectroTechVersion(bool = true) const;
void showMe() {emit showDiagram(this);} void showMe() {emit showDiagram(this);}

View File

@@ -112,8 +112,10 @@ QETProject::QETProject(KAutoSaveFile *backup, QObject *parent) :
* @brief QETProject::~QETProject * @brief QETProject::~QETProject
* Destructor * Destructor
*/ */
QETProject::~QETProject() { QETProject::~QETProject()
qDeleteAll(m_diagrams_list); {
for (auto diagram : m_diagrams_list)
diagram->deleteLater();
} }
/** /**
@@ -1209,18 +1211,24 @@ QList <Diagram *> QETProject::addNewDiagramFolioList()
return(diagram_list); return(diagram_list);
} }
/**
Enleve un schema du projet et emet le signal diagramRemoved
@param diagram le schema a enlever
*/
void QETProject::removeDiagram(Diagram *diagram) {
// ne fait rien si le projet est en lecture seule
if (isReadOnly()) return;
if (!diagram || !m_diagrams_list.contains(diagram)) return;
if (m_diagrams_list.removeAll(diagram)) { /**
* @brief QETProject::removeDiagram
* Remove @diagram from project
* @param diagram
*/
void QETProject::removeDiagram(Diagram *diagram)
{
if (isReadOnly() ||
!diagram || !m_diagrams_list.contains(diagram)) {
return;
}
if (m_diagrams_list.removeAll(diagram))
{
m_data_base.removeDiagram(diagram);
emit(diagramRemoved(this, diagram)); emit(diagramRemoved(this, diagram));
delete diagram; diagram->deleteLater();
} }
updateDiagramsFolioData(); updateDiagramsFolioData();
@@ -1333,8 +1341,6 @@ void QETProject::readProjectXml(QDomDocument &xml_project)
*/ */
void QETProject::readDiagramsXml(QDomDocument &xml_project) void QETProject::readDiagramsXml(QDomDocument &xml_project)
{ {
QMultiMap<int, Diagram *> loaded_diagrams;
//@TODO try to solve a weird bug (dialog is black) since port to Qt5 with the DialogWaiting //@TODO try to solve a weird bug (dialog is black) since port to Qt5 with the DialogWaiting
//show DialogWaiting //show DialogWaiting
DialogWaiting *dlgWaiting = nullptr; DialogWaiting *dlgWaiting = nullptr;
@@ -1364,27 +1370,17 @@ void QETProject::readDiagramsXml(QDomDocument &xml_project)
{ {
QDomElement diagram_xml_element = diagram_nodes.at(i).toElement(); QDomElement diagram_xml_element = diagram_nodes.at(i).toElement();
Diagram *diagram = new Diagram(this); Diagram *diagram = new Diagram(this);
bool diagram_loading = diagram -> initFromXml(diagram_xml_element);
if (diagram_loading)
{
if(dlgWaiting)
dlgWaiting->setDetail( diagram->title() );
//Get the attribute "order" of the diagram
int diagram_order = -1; int diagram_order = -1;
if (!QET::attributeIsAnInteger(diagram_xml_element, "order", &diagram_order)) diagram_order = 500000; if (!QET::attributeIsAnInteger(diagram_xml_element, "order", &diagram_order)) diagram_order = 500000;
loaded_diagrams.insert(diagram_order, diagram);
}
else
{
delete diagram;
}
}
}
//Add the diagrams according to there "order" attribute addDiagram(diagram, diagram_order-1);
foreach(Diagram *diagram, loaded_diagrams.values())
addDiagram(diagram); diagram->initFromXml(diagram_xml_element);
if(dlgWaiting)
dlgWaiting->setDetail(diagram->title());
}
}
//Initialise links between elements in this project //Initialise links between elements in this project
//and refresh the text of conductor //and refresh the text of conductor
@@ -1639,9 +1635,6 @@ void QETProject::addDiagram(Diagram *diagram, int pos)
return; return;
} }
// Ensure diagram know is parent project
diagram->setProject(this);
connect(&diagram->border_and_titleblock, &BorderTitleBlock::needFolioData, this, &QETProject::updateDiagramsFolioData); connect(&diagram->border_and_titleblock, &BorderTitleBlock::needFolioData, this, &QETProject::updateDiagramsFolioData);
connect(diagram, &Diagram::usedTitleBlockTemplateChanged, this, &QETProject::usedTitleBlockTemplateChanged); connect(diagram, &Diagram::usedTitleBlockTemplateChanged, this, &QETProject::usedTitleBlockTemplateChanged);
@@ -1650,7 +1643,7 @@ void QETProject::addDiagram(Diagram *diagram, int pos)
} else { } else {
m_diagrams_list.insert(pos, diagram); m_diagrams_list.insert(pos, diagram);
} }
m_data_base.addDiagram(diagram);
updateDiagramsFolioData(); updateDiagramsFolioData();
} }