File indexing completed on 2024-05-12 05:43:33
0001 /* 0002 Copyright (C) 2015 Volker Krause <vkrause@kde.org> 0003 0004 This program is free software; you can redistribute it and/or modify it 0005 under the terms of the GNU Library General Public License as published by 0006 the Free Software Foundation; either version 2 of the License, or (at your 0007 option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, but WITHOUT 0010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 0012 License for more details. 0013 0014 You should have received a copy of the GNU General Public License 0015 along with this program. If not, see <https://www.gnu.org/licenses/>. 0016 */ 0017 0018 #include "gnuplotter.h" 0019 0020 #include <QDebug> 0021 #include <QGuiApplication> 0022 #include <QPalette> 0023 #include <QProcess> 0024 #include <QStandardPaths> 0025 #include <QTemporaryDir> 0026 0027 Gnuplotter::Gnuplotter() : 0028 m_tempDir(new QTemporaryDir) 0029 { 0030 } 0031 0032 Gnuplotter::~Gnuplotter() = default; 0033 0034 Gnuplotter::Gnuplotter(Gnuplotter&&) = default; 0035 Gnuplotter& Gnuplotter::operator=(Gnuplotter&&) = default; 0036 0037 bool Gnuplotter::isValid() const 0038 { 0039 return !m_templateFileName.isEmpty() && !m_outputSize.isNull(); 0040 } 0041 0042 bool Gnuplotter::hasGnuplot() 0043 { 0044 return !QStandardPaths::findExecutable(QStringLiteral("gnuplot")).isEmpty(); 0045 } 0046 0047 void Gnuplotter::setTemplate(const QString& templateFileName) 0048 { 0049 m_templateFileName = templateFileName; 0050 } 0051 0052 void Gnuplotter::setSize(const QSize& size) 0053 { 0054 m_outputSize = size; 0055 } 0056 0057 QString Gnuplotter::workingDir() const 0058 { 0059 return m_tempDir->path(); 0060 } 0061 0062 QString Gnuplotter::imageFileName() const 0063 { 0064 return m_tempDir->path() + "/plot.png"; 0065 } 0066 0067 void Gnuplotter::plot() const 0068 { 0069 // TODO error handling 0070 processTemplate(); 0071 QProcess proc; 0072 proc.setProcessChannelMode(QProcess::ForwardedChannels); 0073 proc.setWorkingDirectory(m_tempDir->path()); 0074 proc.start(QStandardPaths::findExecutable(QStringLiteral("gnuplot")), { QStringLiteral("plot.gnuplot") }); 0075 proc.waitForFinished(); 0076 } 0077 0078 void Gnuplotter::processTemplate() const 0079 { 0080 // TODO error handling 0081 0082 QFile out(m_tempDir->path() + "/plot.gnuplot"); 0083 if (!out.open(QFile::WriteOnly | QFile::Truncate)) { 0084 qWarning() << "Failed to write to" << m_tempDir->path() << ":" << out.errorString(); 0085 return; 0086 } 0087 0088 out.write("set terminal png transparent size "); 0089 out.write(QByteArray::number(m_outputSize.width())); 0090 out.write(","); 0091 out.write(QByteArray::number(m_outputSize.height())); 0092 out.write("\nset output \"plot.png\"\n"); 0093 0094 QFile in(m_templateFileName); 0095 if (!in.open(QFile::ReadOnly)) { 0096 qWarning() << "Failed to read" << m_templateFileName << ":" << in.errorString(); 0097 return; 0098 } 0099 0100 auto t = in.readAll(); 0101 t.replace("@TEXTCOLOR@", QGuiApplication::palette().color(QPalette::Text).name().toUtf8()); 0102 0103 out.write(t); 0104 }