1 /* GNUPLOT - QtGnuplotInstance.h */
2
3 /*[
4 * Copyright 2009 Jérôme Lodewyck
5 *
6 * Permission to use, copy, and distribute this software and its
7 * documentation for any purpose with or without fee is hereby granted,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation.
11 *
12 * Permission to modify the software is granted, but not the right to
13 * distribute the complete modified source code. Modifications are to
14 * be distributed as patches to the released version. Permission to
15 * distribute binaries produced by compiling modified sources is granted,
16 * provided you
17 * 1. distribute the corresponding source modifications from the
18 * released version in the form of a patch file along with the binaries,
19 * 2. add special version identification to distinguish your version
20 * in addition to the base release version number,
21 * 3. provide your name and address as the primary contact for the
22 * support of your modified version, and
23 * 4. retain our contact information in regard to use of the base
24 * software.
25 * Permission to distribute the released version of the source code along
26 * with corresponding source modifications in the form of a patch file is
27 * granted with same provisions 2 through 4 for binary distributions.
28 *
29 * This software is provided "as is" without express or implied warranty
30 * to the extent permitted by applicable law.
31 *
32 *
33 * Alternatively, the contents of this file may be used under the terms of the
34 * GNU General Public License Version 2 or later (the "GPL"), in which case the
35 * provisions of GPL are applicable instead of those above. If you wish to allow
36 * use of your version of this file only under the terms of the GPL and not
37 * to allow others to use your version of this file under the above gnuplot
38 * license, indicate your decision by deleting the provisions above and replace
39 * them with the notice and other provisions required by the GPL. If you do not
40 * delete the provisions above, a recipient may use your version of this file
41 * under either the GPL or the gnuplot license.
42 ]*/
43
44 #include "QtGnuplotInstance.h"
45 #include "QtGnuplotWidget.h"
46
47 #include <QtCore>
48 #include <iostream>
49
QtGnuplotInstance(QtGnuplotWidget * widget,QString gnuplotPath)50 QtGnuplotInstance::QtGnuplotInstance(QtGnuplotWidget* widget, QString gnuplotPath)
51 {
52 m_gnuplot.setProcessChannelMode(QProcess::MergedChannels);
53 m_gnuplot.start(gnuplotPath);
54 m_gnuplot.waitForStarted();
55 connect(&m_gnuplot, SIGNAL(readyReadStandardOutput()), this, SLOT(gnuplotDataReady()));
56
57 if (m_gnuplot.state() == QProcess::NotRunning)
58 qDebug() << "Error starting gnuplot" << m_gnuplot.error();
59
60 setWidget(widget);
61 }
62
~QtGnuplotInstance()63 QtGnuplotInstance::~QtGnuplotInstance()
64 {
65 m_gnuplot.close();
66 }
67
setWidget(QtGnuplotWidget * widget)68 void QtGnuplotInstance::setWidget(QtGnuplotWidget* widget)
69 {
70 m_widget = widget;
71
72 if (m_widget)
73 {
74 QByteArray command;
75 command.append("set term qt widget \"" + m_widget->serverName() + "\" size " +
76 QString::number(m_widget->plotAreaSize().width()) + "," +
77 QString::number(m_widget->plotAreaSize().height()) + "\n");
78 exec(command);
79 }
80 }
81
widget()82 QtGnuplotWidget* QtGnuplotInstance::widget()
83 {
84 return m_widget;
85 }
86
gnuplotDataReady()87 void QtGnuplotInstance::gnuplotDataReady()
88 {
89 QByteArray result = m_gnuplot.readAllStandardOutput();
90 emit(gnuplotOutput(result.constData()));
91 }
92
exec(const QByteArray & command)93 void QtGnuplotInstance::exec(const QByteArray& command)
94 {
95 if (m_gnuplot.state() == QProcess::Running)
96 m_gnuplot.write(command);
97 else
98 qDebug() << "Not running";
99 }
100
execAndRead(const QByteArray & command,int msecs)101 QByteArray QtGnuplotInstance::execAndRead(const QByteArray& command, int msecs)
102 {
103 m_gnuplot.waitForReadyRead(0);
104 QByteArray trailing;
105 do
106 {
107 trailing = m_gnuplot.readAllStandardOutput();
108 emit(gnuplotOutput(trailing.constData()));
109 } while (!trailing.isEmpty());
110
111 m_gnuplot.disconnect(SIGNAL(readyReadStandardOutput()));
112 exec(command);
113 m_gnuplot.waitForReadyRead(msecs);
114 QByteArray answer = m_gnuplot.readAllStandardOutput();
115 connect(&m_gnuplot, SIGNAL(readyReadStandardOutput()), this, SLOT(gnuplotDataReady()));
116 return answer;
117 }
118
operator <<(QtGnuplotInstance & instance,const QString & command)119 QtGnuplotInstance& operator<<(QtGnuplotInstance& instance, const QString& command)
120 {
121 QByteArray array;
122 array.append(command);
123 instance.exec(array);
124
125 return instance;
126 }
127
operator <<(QtGnuplotInstance & instance,const QVector<QPointF> & points)128 QtGnuplotInstance& operator<<(QtGnuplotInstance& instance, const QVector<QPointF>& points)
129 {
130 QByteArray array;
131
132 foreach (QPointF point, points)
133 array += QByteArray::number(point.x()) + " " + QByteArray::number(point.y()) + "\n";
134
135 array += "e\n";
136 instance.exec(array);
137
138 return instance;
139 }
140