19 #include <QContextMenuEvent>
21 #include <ui_QmitkGnuplotWidget.h>
26 m_ContextMenu(nullptr),
27 m_CopyPlotAction(nullptr),
28 m_CopyScriptAction(nullptr),
29 m_Process(new QProcess(this))
34 m_Process, SIGNAL(stateChanged(QProcess::ProcessState)),
this, SLOT(OnProcessStateChanged(QProcess::ProcessState)));
35 connect(m_Process, SIGNAL(error(QProcess::ProcessError)),
this, SLOT(OnProcessError(QProcess::ProcessError)));
37 m_Process, SIGNAL(finished(
int, QProcess::ExitStatus)),
this, SLOT(OnProcessFinished(
int, QProcess::ExitStatus)));
39 this->CreateContextMenu();
46 void QmitkGnuplotWidget::CreateContextMenu()
48 m_CopyPlotAction =
new QAction(
"Copy &Plot",
this);
49 connect(m_CopyPlotAction, SIGNAL(triggered()),
this, SLOT(OnCopyPlot()));
51 m_CopyScriptAction =
new QAction(
"Copy &Script",
this);
52 connect(m_CopyScriptAction, SIGNAL(triggered()),
this, SLOT(OnCopyScript()));
54 m_ContextMenu =
new QMenu(
this);
56 m_ContextMenu->addActions(QList<QAction *>() << m_CopyPlotAction << m_CopyScriptAction);
61 const QPixmap *plot = m_Ui->label->pixmap();
63 m_CopyPlotAction->setEnabled(plot !=
nullptr && !plot->isNull());
64 m_CopyScriptAction->setEnabled(!m_Commands.empty());
66 m_ContextMenu->popup(event->globalPos());
71 void QmitkGnuplotWidget::OnCopyPlot()
73 const QPixmap *plot = m_Ui->label->pixmap();
75 if (plot !=
nullptr && !plot->isNull())
76 QApplication::clipboard()->setPixmap(*plot);
79 void QmitkGnuplotWidget::OnCopyScript()
81 if (m_Commands.empty())
84 QString script = this->CreateSetTermCommand();
86 Q_FOREACH (
const QString &command, m_Commands)
88 script += command +
"\n";
91 QApplication::clipboard()->setText(script);
96 m_ModifiedTime.Modified();
98 if (m_Process->isOpen() || m_Commands.isEmpty() || m_GnuplotPath.isEmpty())
106 return m_GnuplotPath;
111 m_GnuplotPath = path;
112 m_ModifiedTime.Modified();
122 m_Commands = commands;
123 m_ModifiedTime.Modified();
128 if (m_UpdateTime < m_ModifiedTime)
129 m_Process->start(m_GnuplotPath, QStringList() <<
"-");
134 return QSize(400, 300);
137 QString QmitkGnuplotWidget::CreateSetTermCommand()
const
139 return QString(
"set term pngcairo size %1,%2 enhanced font '%3,%4'\n")
142 .arg(this->font().family())
143 .arg(this->font().pointSize());
146 void QmitkGnuplotWidget::OnProcessStateChanged(QProcess::ProcessState state)
148 if (state == QProcess::Running)
150 m_UpdateTime = m_ModifiedTime;
152 m_Process->write(this->CreateSetTermCommand().toLatin1());
154 Q_FOREACH (
const QString &command, m_Commands)
156 m_Process->write(QString(
"%1\n").arg(command).toLatin1());
159 m_Process->write(
"exit\n");
160 m_Process->closeWriteChannel();
164 void QmitkGnuplotWidget::OnProcessError(QProcess::ProcessError error)
168 case QProcess::FailedToStart:
169 m_Ui->label->setText(
"Gnuplot failed to start!");
172 case QProcess::Crashed:
173 m_Ui->label->setText(
"Gnuplot crashed!");
176 case QProcess::Timedout:
177 m_Ui->label->setText(
"Gnuplot timed out!");
180 case QProcess::WriteError:
181 m_Ui->label->setText(
"Could not write to gnuplot!");
184 case QProcess::ReadError:
185 m_Ui->label->setText(
"Could not read from gnuplot!");
189 m_Ui->label->setText(
"An unknown error occurred!");
194 void QmitkGnuplotWidget::OnProcessFinished(
int exitCode, QProcess::ExitStatus exitStatus)
196 bool needUpdate =
false;
198 if (exitStatus != QProcess::CrashExit)
202 if (m_UpdateTime < m_ModifiedTime)
208 m_Ui->label->setPixmap(QPixmap::fromImage(QImage::fromData(m_Process->readAllStandardOutput(),
"PNG")));
213 m_Ui->label->setText(QString(
"Gnuplot exit code: %1!").arg(exitCode));