From a0970b37b9e1276c80e554a737a9ef71cef729b9 Mon Sep 17 00:00:00 2001 From: joshua Date: Thu, 19 Jan 2023 20:18:21 +0100 Subject: [PATCH] Improve and simplify the use of versions --- sources/bordertitleblock.cpp | 4 +- sources/editor/elementscene.cpp | 3 +- sources/factory/elementpicturefactory.cpp | 25 +++---- sources/machine_info.cpp | 6 +- sources/print/projectprintwindow.cpp | 4 +- sources/qet.h | 4 -- sources/qetapp.cpp | 3 +- sources/qetgraphicsitem/element.cpp | 26 ++++--- sources/qetproject.cpp | 24 +++---- sources/qetversion.cpp | 85 +++++++++++++++++++++++ sources/qetversion.h | 43 ++++++++++++ sources/ui/aboutqetdialog.cpp | 3 +- 12 files changed, 175 insertions(+), 55 deletions(-) create mode 100644 sources/qetversion.cpp create mode 100644 sources/qetversion.h diff --git a/sources/bordertitleblock.cpp b/sources/bordertitleblock.cpp index a89519fd4..d281fd88c 100644 --- a/sources/bordertitleblock.cpp +++ b/sources/bordertitleblock.cpp @@ -22,9 +22,11 @@ #include "diagramposition.h" #include "math.h" #include "qetapp.h" +#include "qetversion.h" #include "titleblocktemplate.h" #include "titleblocktemplaterenderer.h" + #include #include #include @@ -317,7 +319,7 @@ void BorderTitleBlock::importTitleBlock(const TitleBlockProperties &ip) { setPlant(ip.plant); setLocMach(ip.locmach); setIndicerev(ip.indexrev); - setVersion(QET::displayedVersion); + setVersion(QetVersion::displayedVersion()); setFolio(ip.folio); setAutoPageNum(ip.auto_page_num); if (m_edge != ip.display_at) diff --git a/sources/editor/elementscene.cpp b/sources/editor/elementscene.cpp index 38d3137bc..e493bfaec 100644 --- a/sources/editor/elementscene.cpp +++ b/sources/editor/elementscene.cpp @@ -35,6 +35,7 @@ #include "graphicspart/parttext.h" #include "ui/qetelementeditor.h" #include "ui/elementpropertieseditorwidget.h" +#include "../qetversion.h" #include #include @@ -448,7 +449,7 @@ const QDomDocument ElementScene::toXml(bool all_parts) root.setAttribute("hotspot_y", QString("%1").arg( -(qRound(size.y() - (ymargin/2))))); - root.setAttribute("version", QET::version); + QetVersion::toXmlAttribute(root); root.setAttribute("link_type", m_element_data.typeToString(m_element_data.m_type)); //Uuid used to compare two elements diff --git a/sources/factory/elementpicturefactory.cpp b/sources/factory/elementpicturefactory.cpp index 4754d92d3..b38a08bdc 100644 --- a/sources/factory/elementpicturefactory.cpp +++ b/sources/factory/elementpicturefactory.cpp @@ -19,8 +19,8 @@ #include "../ElementsCollection/elementslocation.h" #include "../editor/graphicspart/partline.h" -#include "../qet.h" #include "../qetapp.h" +#include "../qetversion.h" #include #include @@ -154,19 +154,16 @@ bool ElementPictureFactory::build(const ElementsLocation &location, QDomElement dom = location.xml(); //Check if the current version can read the xml description - if (dom.hasAttribute("version")) - { - QVersionNumber qet_version = QVersionNumber::fromString(QET::version); - QVersionNumber element_version = QVersionNumber::fromString(dom.attribute("version")); - if (qet_version < element_version) - { - std::cerr << qPrintable( - QObject::tr("Avertissement : l'élément " - " a été enregistré avec une version" - " ultérieure de QElectroTech.") - ) << std::endl; - } - } + const auto elmt_version = QetVersion::fromXmlAttribute(dom); + if (!elmt_version.isNull() + && QetVersion::currentVersion() < elmt_version) + { + std::cerr << qPrintable( + QObject::tr("Avertissement : l'élément " + " a été enregistré avec une version" + " ultérieure de QElectroTech.") + ) << std::endl; + } //This attributes must be present and valid int w, h, hot_x, hot_y; diff --git a/sources/machine_info.cpp b/sources/machine_info.cpp index 946b2f6c5..c9eb1d9c1 100644 --- a/sources/machine_info.cpp +++ b/sources/machine_info.cpp @@ -16,6 +16,9 @@ along with QElectroTech. If not, see . */ #include "machine_info.h" + +#include "qetversion.h" + #include #include #include @@ -28,7 +31,6 @@ #ifdef Q_OS_WIN #include #endif -#include "qet.h" MachineInfo *MachineInfo::m_instance = nullptr; /** @@ -121,7 +123,7 @@ void MachineInfo::send_info_to_debug() #endif #endif qInfo() << "GitRevision " + QString(GIT_COMMIT_SHA); - qInfo()<< "QElectroTech V " + QET::displayedVersion; + qInfo()<< "QElectroTech V " + QetVersion::displayedVersion(); qInfo()<< QObject::tr("Compilation : ") + pc.built.version; qInfo()<< "Built with Qt " + pc.built.QT + " - " + pc.built.arch diff --git a/sources/print/projectprintwindow.cpp b/sources/print/projectprintwindow.cpp index bd9fe039d..e6f518723 100644 --- a/sources/print/projectprintwindow.cpp +++ b/sources/print/projectprintwindow.cpp @@ -20,6 +20,8 @@ #include "../diagram.h" #include "../qeticons.h" #include "../qetproject.h" +#include "../qetversion.h" + #include "ui_projectprintwindow.h" #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // ### Qt 6: remove @@ -84,7 +86,7 @@ void ProjectPrintWindow::launchDialog(QETProject *project, QPrinter::OutputForma if (!file_name.endsWith(".pdf")) { file_name.append(".pdf"); } - printer_->setCreator(QString("QElectroTech %1").arg(QET::displayedVersion)); + printer_->setCreator(QString("QElectroTech %1").arg(QetVersion::displayedVersion())); printer_->setOutputFileName(file_name); printer_->setOutputFormat(QPrinter::PdfFormat); } diff --git a/sources/qet.h b/sources/qet.h index 2dc4afe7e..5a5a2d9e6 100644 --- a/sources/qet.h +++ b/sources/qet.h @@ -28,10 +28,6 @@ class QActionGroup; anywhere else within the QElectroTech application. */ namespace QET { - /// QElectroTech version (as string, used to mark projects and elements XML documents) - const QString version = "0.100"; - /// QElectroTech displayed version - const QString displayedVersion = "0.100-DEV"; QString license(); //Describe the current state of a graphic item diff --git a/sources/qetapp.cpp b/sources/qetapp.cpp index c0ce3abf8..bb64c1ca5 100644 --- a/sources/qetapp.cpp +++ b/sources/qetapp.cpp @@ -37,6 +37,7 @@ #include "ui/configpage/generalconfigurationpage.h" #include "machine_info.h" #include "TerminalStrip/ui/terminalstripeditorwindow.h" +#include "qetversion.h" #include #include @@ -2314,7 +2315,7 @@ void QETApp::printHelp() */ void QETApp::printVersion() { - std::cout << qPrintable(QET::displayedVersion) << std::endl; + std::cout << qPrintable(QetVersion::displayedVersion()) << std::endl; } /** diff --git a/sources/qetgraphicsitem/element.cpp b/sources/qetgraphicsitem/element.cpp index 5800a3072..e4dab18a6 100644 --- a/sources/qetgraphicsitem/element.cpp +++ b/sources/qetgraphicsitem/element.cpp @@ -34,6 +34,7 @@ #include "elementtextitemgroup.h" #include "iostream" #include "../qetxml.h" +#include "../qetversion.h" #include #include @@ -426,20 +427,17 @@ bool Element::buildFromXml(const QDomElement &xml_def_elmt, int *state) return(false); } - //Check if the current version can read the xml description - if (xml_def_elmt.hasAttribute(QStringLiteral("version"))) - { - QVersionNumber qet_version = QVersionNumber::fromString(QET::version); - QVersionNumber element_version = QVersionNumber::fromString(xml_def_elmt.attribute(QStringLiteral("version"))); - if (qet_version < element_version) - { - std::cerr << qPrintable( - QObject::tr("Avertissement : l'élément " - " a été enregistré avec une version" - " ultérieure de QElectroTech.") - ) << std::endl; - } - } + //Check if the current version can read the xml description + const auto elmt_version = QetVersion::fromXmlAttribute(xml_def_elmt); + if (!elmt_version.isNull() + && QetVersion::currentVersion() < elmt_version) + { + std::cerr << qPrintable( + QObject::tr("Avertissement : l'élément " + " a été enregistré avec une version" + " ultérieure de QElectroTech.") + ) << std::endl; + } //This attribute must be present and valid int w = 0, h = 0, hot_x = 0, hot_y = 0; diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index 52d09ce0e..61413a241 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -33,6 +33,7 @@ #include "ui/importelementdialog.h" #include "TerminalStrip/terminalstrip.h" #include "qetxml.h" +#include "qetversion.h" #include #include @@ -907,7 +908,7 @@ QDomDocument QETProject::toXml() // racine du projet QDomDocument xml_doc; QDomElement project_root = xml_doc.createElement("project"); - project_root.setAttribute("version", QET::version); + QetVersion::toXmlAttribute(project_root); if (project_title_.isEmpty()) { // if project_title_is Empty add title from m_file_path @@ -1337,18 +1338,11 @@ void QETProject::readProjectXml(QDomDocument &xml_project) if (root_elmt.tagName() == QLatin1String("project")) { //Normal opening mode - if (root_elmt.hasAttribute(QStringLiteral("version"))) + m_project_qet_version = QetVersion::fromXmlAttribute(root_elmt); + if (!m_project_qet_version.isNull()) { - bool conv_ok; - QVersionNumber qet_version = QVersionNumber::fromString(QET::version); - m_project_qet_version = QVersionNumber::fromString(root_elmt.attribute(QStringLiteral("version"))); - -#if TODO_LIST -#pragma message("@TODO use of version convert") -#endif - if (qet_version < m_project_qet_version) + if (QetVersion::currentVersion() < m_project_qet_version) { - int ret = QET::QetMessageBox::warning( nullptr, tr("Avertissement", @@ -1357,7 +1351,7 @@ void QETProject::readProjectXml(QDomDocument &xml_project) "\n qui est ultérieure à votre version !" " \n" "Vous utilisez actuellement QElectroTech en version %2") - .arg(root_elmt.attribute(QStringLiteral("version")), QET::version + + .arg(root_elmt.attribute(QStringLiteral("version")), QetVersion::currentVersion().toString() + tr(".\n Il est alors possible que l'ouverture de tout ou partie de ce " "document échoue.\n" "Que désirez vous faire ?"), @@ -1374,16 +1368,14 @@ void QETProject::readProjectXml(QDomDocument &xml_project) //Since QElectrotech 0.9 the compatibility with project made with //Qet 0.6 or lower is break; - //keep float here for very old version - qreal r_project_qet_version = root_elmt.attribute(QStringLiteral("version")).toDouble(&conv_ok); - if (conv_ok && m_project_qet_version < QVersionNumber(0, 100) && r_project_qet_version <= 0.6) + if (m_project_qet_version <= QetVersion::versionZeroDotSix()) { auto ret = QET::QetMessageBox::warning( nullptr, tr("Avertissement ", "message box title"), tr("Le projet que vous tentez d'ouvrir est partiellement " "compatible avec votre version %1 de QElectroTech.\n") - .arg(QET::version) + + .arg(QetVersion::currentVersion().toString()) + tr("Afin de le rendre totalement compatible veuillez ouvrir ce même projet " "avec la version 0.8, ou 0.80 de QElectroTech et sauvegarder le projet " "et l'ouvrir à nouveau avec cette version.\n" diff --git a/sources/qetversion.cpp b/sources/qetversion.cpp new file mode 100644 index 000000000..3e42d993b --- /dev/null +++ b/sources/qetversion.cpp @@ -0,0 +1,85 @@ +/* + Copyright 2006-2023 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#include "qetversion.h" + +namespace QetVersion +{ + QetVersion::VersionType VERSION_TYPE = QetVersion::dev; + + QVersionNumber currentVersion() + { + return QVersionNumber{ 0, 100, 0 }; + } + + QString displayedVersion() + { + auto str = currentVersion().toString(); + + switch (VERSION_TYPE) { + case dev: + str.append(QStringLiteral("-dev")); + break; + case alpha1: + str.append(QStringLiteral("-alpha 1 ")); + break; + case alpha2: + str.append(QStringLiteral("-alpha 2 ")); + break; + case alpha3: + str.append(QStringLiteral("-alpha 3 ")); + break; + default: + break; + } + + return str; + } + + /** + * @brief toXmlAttribute + * Write @a version as an attribute of @a xml + * @param xml + * @param version + */ + void toXmlAttribute(QDomElement &xml) + { + xml.setAttribute(QStringLiteral("version"), currentVersion().toString()); + } + + /** + * @brief fromXmlAttribute + * Read and return a QVersionNumber + * stored as attribute of @a xml + * @param xml + * @return the version stored in @a xml. + * Note that the returned version can be null + */ + QVersionNumber fromXmlAttribute(const QDomElement &xml) + { + if (xml.hasAttribute(QStringLiteral("version"))) { + return QVersionNumber::fromString(xml.attribute(QStringLiteral("version"))); + } else { + return QVersionNumber{}; + } + } + + QVersionNumber versionZeroDotSix() + { + return QVersionNumber{ 0, 60 }; + } +} diff --git a/sources/qetversion.h b/sources/qetversion.h new file mode 100644 index 000000000..e80d1af52 --- /dev/null +++ b/sources/qetversion.h @@ -0,0 +1,43 @@ +/* + Copyright 2006-2023 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#ifndef QETVERSION_H +#define QETVERSION_H + +#include +#include + +namespace QetVersion +{ + enum VersionType { + dev = 0, + alpha1 = 1, + alpha2 = 2, + alpha3 = 3, + stable = 4 + }; + + QVersionNumber currentVersion(); + QString displayedVersion(); + + QVersionNumber versionZeroDotSix(); + + void toXmlAttribute(QDomElement &xml); + QVersionNumber fromXmlAttribute(const QDomElement &xml); +} + +#endif // QETVERSION_H diff --git a/sources/ui/aboutqetdialog.cpp b/sources/ui/aboutqetdialog.cpp index e2460fe21..ec28b656c 100644 --- a/sources/ui/aboutqetdialog.cpp +++ b/sources/ui/aboutqetdialog.cpp @@ -21,6 +21,7 @@ #include "../qet.h" #include "../qetapp.h" #include "ui_aboutqetdialog.h" +#include "../qetversion.h" #include @@ -165,7 +166,7 @@ void AboutQETDialog::setContributors() void AboutQETDialog::setVersion() { QString str = "QElectroTech V " - + QET::displayedVersion + + QetVersion::displayedVersion() + ""; ui->m_version_label->setText(str + MachineInfo::instance()->compilation_info()); }