mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 12:40:35 +01:00
Nomenclature model : Improve function to setup the name of header
This commit is contained in:
@@ -84,57 +84,6 @@ QVector<QStringList> projectDataBase::elementsInfoFromQuery(const QString &query
|
||||
return result_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief projectDataBase::headersFromElementsInfoQuery
|
||||
* @param query
|
||||
* @return the header according to @query.
|
||||
* Header can be false, notably when user create is own query.
|
||||
*/
|
||||
QStringList projectDataBase::headersFromElementNomenclatureViewQuery(const QString &query)
|
||||
{
|
||||
QStringList header_string;
|
||||
if (!query.startsWith("SELECT ") && !query.contains("FROM")) {
|
||||
return header_string;
|
||||
}
|
||||
|
||||
auto header = query;
|
||||
header.remove(0, 7); //Remove SELECT from the string;
|
||||
header.truncate(header.indexOf("FROM")); //Now we only have the string between SELECT and FROM
|
||||
header.replace(" ", ""); //remove white space
|
||||
QStringList list = header.split(","); //split each column
|
||||
|
||||
if (list.isEmpty()) {
|
||||
return header_string;
|
||||
}
|
||||
|
||||
for (int i=0 ; i<list.size() ; ++i)
|
||||
{
|
||||
auto str = list.at(i);
|
||||
|
||||
if (str == "position") {
|
||||
header_string.append(tr("Position"));
|
||||
} else if (str == "diagram_position") {
|
||||
header_string.append(tr("Position du folio"));
|
||||
} else {
|
||||
auto elmt_str = QETApp::elementTranslatedInfoKey(str);
|
||||
if (!elmt_str.isEmpty()) {
|
||||
header_string.append(elmt_str);
|
||||
continue;
|
||||
}
|
||||
auto diagram_str = QETApp::diagramTranslatedInfoKey(str);
|
||||
if (!diagram_str.isEmpty()) {
|
||||
header_string.append(diagram_str);
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
header_string.append(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return header_string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief projectDataBase::updateDB
|
||||
* Up to date the content of the data base.
|
||||
@@ -160,6 +109,14 @@ QETProject *projectDataBase::project() const {
|
||||
return m_project;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief projectDataBase::newQuery
|
||||
* @return a QSqlquery with @query as query and the internal database of this class as database to use.
|
||||
*/
|
||||
QSqlQuery projectDataBase::newQuery(const QString &query) {
|
||||
return QSqlQuery(query, m_data_base);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief projectDataBase::createDataBase
|
||||
* Create the data base
|
||||
|
||||
@@ -48,8 +48,7 @@ class projectDataBase : public QObject
|
||||
QVector<QStringList> elementsInfoFromQuery(const QString &query);
|
||||
void updateDB();
|
||||
QETProject *project() const;
|
||||
|
||||
static QStringList headersFromElementNomenclatureViewQuery(const QString &query);
|
||||
QSqlQuery newQuery(const QString &query = QString());
|
||||
|
||||
signals:
|
||||
void dataBaseUpdated();
|
||||
|
||||
@@ -97,7 +97,6 @@ QetGraphicsTableItem *QetGraphicsTableFactory::newTable(Diagram *diagram, AddTab
|
||||
{
|
||||
auto model = new NomenclatureModel(diagram->project(), diagram->project());
|
||||
model->query(dialog->queryStr());
|
||||
model->autoHeaders();
|
||||
model->setData(model->index(0,0), int(dialog->tableAlignment()), Qt::TextAlignmentRole);
|
||||
model->setData(model->index(0,0), dialog->tableFont(), Qt::FontRole);
|
||||
model->setData(model->index(0,0), QETUtils::marginsToString(dialog->headerMargins()), Qt::UserRole+1);
|
||||
|
||||
@@ -22,6 +22,9 @@
|
||||
|
||||
#include <QModelIndex>
|
||||
#include <QFont>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlRecord>
|
||||
|
||||
|
||||
/**
|
||||
* @brief NomenclatureModel::NomenclatureModel
|
||||
@@ -179,12 +182,15 @@ void NomenclatureModel::query(const QString &query)
|
||||
}
|
||||
m_project->dataBase()->updateDB();
|
||||
if (rm_) {
|
||||
setHeaderString();
|
||||
m_record = m_project->dataBase()->elementsInfoFromQuery(m_query);
|
||||
connect(m_project->dataBase(), &projectDataBase::dataBaseUpdated, this, &NomenclatureModel::dataBaseUpdated);
|
||||
}
|
||||
}
|
||||
|
||||
if (rm_) { emit endResetModel();}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,18 +205,6 @@ QETProject *NomenclatureModel::project() const {
|
||||
return m_project.data();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief NomenclatureModel::autoHeaders
|
||||
* Try to determine the name of each columns header
|
||||
*/
|
||||
void NomenclatureModel::autoHeaders()
|
||||
{
|
||||
auto headers = projectDataBase::headersFromElementNomenclatureViewQuery(m_query);
|
||||
for (auto i=0 ; i<headers.size() ; ++i) {
|
||||
this->setHeaderData(i, Qt::Horizontal, headers.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief NomenclatureModel::toXml
|
||||
* Save the model to xml,since model can have unlimited data we only save few data (only these used by qelectrotech).
|
||||
@@ -263,7 +257,6 @@ void NomenclatureModel::fromXml(const QDomElement &element)
|
||||
return;
|
||||
|
||||
query(element.firstChildElement("query").text());
|
||||
autoHeaders();
|
||||
|
||||
//Index 0,0
|
||||
auto index_00 = element.firstChildElement("index00");
|
||||
@@ -302,3 +295,30 @@ void NomenclatureModel::dataBaseUpdated()
|
||||
emit dataChanged(this->index(0,0), this->index(row-1, col-1), QVector<int>(Qt::DisplayRole));
|
||||
}
|
||||
}
|
||||
|
||||
void NomenclatureModel::setHeaderString()
|
||||
{
|
||||
auto q = m_project->dataBase()->newQuery(m_query);
|
||||
auto record = q.record();
|
||||
|
||||
for (auto i=0 ; i<record.count() ; ++i)
|
||||
{
|
||||
auto field_name = record.fieldName(i);
|
||||
QString header_name;
|
||||
|
||||
if (field_name == "position") {
|
||||
header_name = tr("Position");
|
||||
} else if (field_name == "diagram_position") {
|
||||
header_name = tr("Position du folio");
|
||||
} else {
|
||||
header_name = QETApp::elementTranslatedInfoKey(field_name);
|
||||
if (header_name.isEmpty()) {
|
||||
header_name = QETApp::diagramTranslatedInfoKey(field_name);
|
||||
}
|
||||
if (header_name.isEmpty()) {
|
||||
header_name = field_name;
|
||||
}
|
||||
}
|
||||
this->setHeaderData(i, Qt::Horizontal, header_name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,6 @@ class NomenclatureModel : public QAbstractTableModel
|
||||
void query(const QString &query);
|
||||
QString queryString() const;
|
||||
QETProject *project() const;
|
||||
void autoHeaders();
|
||||
|
||||
QDomElement toXml(QDomDocument &document) const;
|
||||
void fromXml(const QDomElement &element);
|
||||
@@ -56,6 +55,7 @@ class NomenclatureModel : public QAbstractTableModel
|
||||
|
||||
private:
|
||||
void dataBaseUpdated();
|
||||
void setHeaderString();
|
||||
|
||||
private:
|
||||
QPointer<QETProject> m_project;
|
||||
|
||||
Reference in New Issue
Block a user