File indexing completed on 2024-05-12 05:43:34

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 "gnuplotwidget.h"
0019 
0020 #include <QDebug>
0021 #include <QPixmap>
0022 #include <QResizeEvent>
0023 
0024 GnuplotWidget::GnuplotWidget(QWidget* parent):
0025     QLabel(parent)
0026 {
0027     setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
0028     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0029 
0030     if (!Gnuplotter::hasGnuplot())
0031         setText(tr("'gnuplot' not found, plotting not available."));
0032 }
0033 
0034 GnuplotWidget::~GnuplotWidget() = default;
0035 
0036 void GnuplotWidget::setPlotter(Gnuplotter&& plotter)
0037 {
0038     m_plotter = std::move(plotter);
0039     replot();
0040 }
0041 
0042 QSize GnuplotWidget::minimumSizeHint() const
0043 {
0044     // we can be made smaller than the content image, by re-rendering
0045     return QWidget::minimumSizeHint();
0046 }
0047 
0048 void GnuplotWidget::resizeEvent(QResizeEvent* event)
0049 {
0050     replot();
0051     QWidget::resizeEvent(event);
0052 }
0053 
0054 void GnuplotWidget::replot()
0055 {
0056     m_plotter.setSize(size());
0057     qDebug() << size() << m_plotter.isValid();
0058     if (!m_plotter.isValid())
0059         return;
0060 
0061     m_plotter.plot();
0062     QPixmap p;
0063     p.load(m_plotter.imageFileName());
0064     setPixmap(p);
0065 }