Warning, file /education/cantor/src/lib/graphicpackage.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2020 Sirgienko Nikita <warquark@gmail.com>
0004 */
0005 
0006 #include "graphicpackage.h"
0007 
0008 #include <QDir>
0009 #include <QDomDocument>
0010 #include <QDebug>
0011 
0012 #include "session.h"
0013 #include "expression.h"
0014 
0015 using namespace Cantor;
0016 
0017 class Cantor::GraphicPackagePrivate
0018 {
0019   public:
0020     QString id;
0021     QString name;
0022     QString testPresenceCommand;
0023     QString enableSupportCommand;
0024     QString disableSupportCommand;
0025     QString saveToFileCommandTemplate;
0026     QStringList plotPrecenseKeywords;
0027 };
0028 
0029 Cantor::GraphicPackage::GraphicPackage(const Cantor::GraphicPackage& other): d(new GraphicPackagePrivate)
0030 {
0031     *d = *other.d;
0032 }
0033 
0034 Cantor::GraphicPackage::GraphicPackage(): d(new GraphicPackagePrivate)
0035 {
0036 }
0037 
0038 Cantor::GraphicPackage::~GraphicPackage()
0039 {
0040     delete d;
0041 }
0042 
0043 QString Cantor::GraphicPackage::id() const
0044 {
0045     return d->id;
0046 }
0047 
0048 QString Cantor::GraphicPackage::name() const
0049 {
0050     return d->name;
0051 }
0052 
0053 Expression* Cantor::GraphicPackage::isAvailable(Session* session) const
0054 {
0055     return session->evaluateExpression(d->testPresenceCommand, Expression::FinishingBehavior::DoNotDelete, true);
0056 }
0057 
0058 QString Cantor::GraphicPackage::enableSupportCommand(QString additionalInfo) const
0059 {
0060     return d->enableSupportCommand.arg(additionalInfo);
0061 }
0062 
0063 QString Cantor::GraphicPackage::disableSupportCommand() const
0064 {
0065     return d->disableSupportCommand;
0066 }
0067 
0068 QString Cantor::GraphicPackage::savePlotCommand(QString filenamePrefix, int plotNumber, QString additionalInfo) const
0069 {
0070     return d->saveToFileCommandTemplate.arg(filenamePrefix, QString::number(plotNumber), additionalInfo);
0071 }
0072 
0073 bool Cantor::GraphicPackage::isHavePlotCommand() const
0074 {
0075     return !d->saveToFileCommandTemplate.isEmpty();
0076 }
0077 
0078 const QStringList & Cantor::GraphicPackage::plotCommandPrecentsKeywords() const
0079 {
0080     return d->plotPrecenseKeywords;
0081 }
0082 
0083 QList<GraphicPackage> Cantor::GraphicPackage::loadFromFile(const QString& filename)
0084 {
0085     QList<GraphicPackage> packages;
0086 
0087     if (!QFile::exists(filename))
0088         return packages;
0089 
0090     QFile fin(filename);
0091     if (fin.open(QFile::ReadOnly))
0092     {
0093         QDomDocument doc;
0094         if (doc.setContent(fin.readAll()) && doc.firstChildElement(QLatin1String("GraphicPackages")).isNull() == false)
0095         {
0096             const auto& elements = doc.elementsByTagName(QLatin1String("GraphicPackage"));
0097             for (int i = 0; i < elements.size(); i++)
0098             {
0099                 const QDomNode& root = elements.item(i);
0100 
0101                 GraphicPackage package;
0102                 package.d->id = root.firstChildElement(QLatin1String("Id")).text().trimmed();
0103                 package.d->name = root.firstChildElement(QLatin1String("Name")).text().trimmed();
0104                 package.d->testPresenceCommand = root.firstChildElement(QLatin1String("TestPresenceCommand")).text().trimmed();
0105                 package.d->enableSupportCommand = root.firstChildElement(QLatin1String("EnableCommand")).text().trimmed();
0106                 package.d->disableSupportCommand = root.firstChildElement(QLatin1String("DisableCommand")).text().trimmed();
0107                 package.d->saveToFileCommandTemplate = root.firstChildElement(QLatin1String("ToFileCommandTemplate")).text().trimmed();
0108 
0109                 QString delimiter = QLatin1String("\n");
0110                 const QDomElement& delimiterElement = root.firstChildElement(QLatin1String("PlotPrecenseKeywordsDelimiter"));
0111                 if (!delimiterElement.isNull())
0112                     delimiter = delimiterElement.text().trimmed();
0113                 package.d->plotPrecenseKeywords = root.firstChildElement(QLatin1String("PlotPrecenseKeywords")).text().trimmed().split(delimiter, QString::SkipEmptyParts);
0114                 for (QString& name : package.d->plotPrecenseKeywords)
0115                     name = name.trimmed();
0116 
0117                 packages.append(package);
0118             }
0119         }
0120         else
0121             qWarning() << "fail parse" << filename << "as xml file";
0122     }
0123 
0124     return packages;
0125 }
0126 
0127 int Cantor::GraphicPackage::findById(const GraphicPackage& package, const QList<GraphicPackage>& list)
0128 {
0129     for (int i = 0; i < list.size(); i++)
0130         if (list[i].id() == package.id())
0131             return i;
0132     return -1;
0133 }
0134 
0135