gui/graphview.h \
signalcontroller.h \
metatypes.h \
- mathhelpers.h \
integratedpeak.h \
sequence.h \
sequenceselectormodel.h \
imagedrawer.h \
signaldrawer.h \
enumflags.h \
- jsonserializable.h
+ jsonserializable.h \
+ helpers.h
FORMS += \
gui/mainwindow.ui \
QString CSVDataWriterBackend::filename(const QString& name, const QString& extra) const
{
- QString newname = name;
+
+ QString newname;
+ int dotIdx;
+
+ dotIdx = name.lastIndexOf('.');
+ if (dotIdx > 0) {
+ QString suffix = name.mid(dotIdx+1).toLower();
+ if (suffix.compare(FILE_EXTENSION) == 0)
+ newname = name.mid(0, dotIdx-1); /* Cut the suffix */
+ else
+ newname = name;
+ } else
+ newname = name;
+
if (extra.compare("") != 0)
return newname + "_" + extra + "." + FILE_EXTENSION;
else
#include "datafileexporter.h"
#include "csvdatawriterbackend.h"
+#include "helpers.h"
#include <QtCore/QDir>
#include <QtCore/QLocale>
#include <QtWidgets/QMessageBox>
-const QStringList DataFileExporter::SUPPORTED_FORMATS = QStringList() << "Comma separated values (CSV)";
+#include <QDebug>
+
+const std::vector<OutputFormat> DataFileExporter::SUPPORTED_FORMATS = { OutputFormat("Comma separated values", "csv") };
+
+/* Static methods */
+QStringList DataFileExporter::supportedFormats()
+{
+ QStringList list;
+
+ for (const OutputFormat& of : SUPPORTED_FORMATS) {
+ list << of.first;
+ }
+
+ return list;
+}
DataFileExporter::DataFileExporter(QObject* parent) :
QObject(parent)
DataFileExporter::ReturnCode ret;
DataWriterBackend* backend;
- if (formatIdx < 0 || formatIdx >= SUPPORTED_FORMATS.length()) {
+ if (formatIdx < 0 || formatIdx >= SUPPORTED_FORMATS.size()) {
return ReturnCode::E_INVAL_FORMAT;
}
case OutputFormats::CSV:
backend = new CSVDataWriterBackend(this);
break;
+ default:
+ return ReturnCode::E_INVAL_FORMAT;
}
if (list.size() > 1)
return ReturnCode::SUCCESS;
}
+
+/** Public slots **/
+
+void DataFileExporter::guessFormatFromSuffix(const QString& name)
+{
+ QString suffix = getFileNameSuffix(name);
+
+ if (suffix.length() == 0)
+ return;
+
+ for (size_t i = 0; i < SUPPORTED_FORMATS.size(); i++) {
+ const OutputFormat& of = SUPPORTED_FORMATS.at(i);
+ if (suffix.compare(of.second) == 0) {
+ qDebug() << "Changing format to idx" << i;
+ emit formatChanged(i);
+ return;
+ }
+ }
+
+ qDebug() << "Cannot guess type from suffix";
+}
#include <QtCore/QFile>
#include <QtCore/QObject>
+typedef std::pair<const QString, const QString> OutputFormat;
+
class DataFileExporter : public QObject
{
Q_OBJECT
explicit DataFileExporter(QObject* parent = nullptr);
ReturnCode exportData(const WriteList& header, const QString& path, const int formatIdx);
- static const QStringList SUPPORTED_FORMATS;
+ static QStringList supportedFormats();
private:
enum class OutputFormats {
CSV
QString toCSVLine(double time, double value);
ReturnCode writeToFile(QFile& file, const DataWriterBackend* writer, const std::pair<QVariantList, std::vector<QVariantList>>& data);
+ static const std::vector<OutputFormat> SUPPORTED_FORMATS;
+
signals:
+ void formatChanged(const int idx);
public slots:
+ void guessFormatFromSuffix(const QString& name);
};
void DataManager::processDataExport(GeneratorFunc genfunc)
{
DataFileExporter exporter(this);
- ExportRawDataDialog dlg(DataFileExporter::SUPPORTED_FORMATS);
+ ExportRawDataDialog dlg(DataFileExporter::supportedFormats());
DataFileExporter::WriteList writeList;
std::shared_ptr<SingleRunData> sr = m_activeSequence->selectedRun();
int ret = 1;
+ connect(&dlg, SIGNAL(fileNameChanged(QString)), &exporter, SLOT(guessFormatFromSuffix(QString)));
+ connect(&exporter, SIGNAL(formatChanged(int)), &dlg, SLOT(onFormatChanged(int)));
for (const std::string& s : sr->allKeys())
dlg.addAvailableSignal(QString::fromStdString(s));
if (sr == nullptr)
return;
+ connect(&dlg, &ExportGraphToImageDialog::fileNameChanged, [&dlg, this](const QString& name) {
+ int idx = ImageDrawer::guessFormatFromSuffix(name);
+ if (idx > 0)
+ dlg.onFormatChanged(idx);
+ });
+
for (const std::pair<std::string, std::shared_ptr<SignalController>>& p : sr->allControllers()) {
GUIViewport vp = p.second->guiViewport();
dlg.addSignal(vp.absFromTime, vp.absFromValue, vp.absToTime, vp.absToValue, p.first);
connect(ui->qpb_cancel, SIGNAL(clicked()), this, SLOT(reject()));
connect(ui->qpb_ok, SIGNAL(clicked()), this, SLOT(checkAndAccept()));
connect(ui->qcbox_signal, SIGNAL(currentIndexChanged(QString)), this, SLOT(changeBounds(QString)));
+ connect(ui->qle_destPath, SIGNAL(textChanged(QString)), this, SLOT(onFileNameChanged(QString)));
}
void ExportGraphToImageDialog::addSignal(const double fromX, const double fromY, const double toX, const double toY, const std::string& key)
return w;
}
-/* Private slots */
+/** Public slots **/
+void ExportGraphToImageDialog::onFormatChanged(const int idx)
+{
+ ui->qcbox_outputFormat->setCurrentIndex(idx);
+}
+
+/** Private slots **/
void ExportGraphToImageDialog::changeBounds(const QString& key)
{
ui->qle_destPath->setText(fdlg.selectedFiles()[0]);
}
+void ExportGraphToImageDialog::onFileNameChanged(const QString& name)
+{
+ emit fileNameChanged(name);
+}
+
ExportGraphToImageDialog::~ExportGraphToImageDialog()
{
delete ui;
Ui::ExportGraphToImageDialog *ui;
+public slots:
+ void onFormatChanged(const int idx);
+
private slots:
void checkAndAccept();
void changeBounds(const QString& key);
void onBrowseClicked();
+ void onFileNameChanged(const QString& name);
+
+signals:
+ void fileNameChanged(const QString& name);
};
connect(ui->qpb_browse, SIGNAL(clicked()), this, SLOT(onBrowseClicked()));
connect(ui->qpb_ok, SIGNAL(clicked()), this, SLOT(accept()));
connect(ui->qpb_cancel, SIGNAL(clicked()), this, SLOT(reject()));
+ connect(ui->qle_destPath, SIGNAL(textChanged(QString)), this, SLOT(onFileNameChanged(QString)));
}
/* Public functions */
return kl;
}
+/* Public slots */
+void ExportRawDataDialog::onFormatChanged(const int idx)
+{
+ ui->qcbox_formats->setCurrentIndex(idx);
+}
+
/* Private slots */
void ExportRawDataDialog::onBrowseClicked()
ui->qle_destPath->setText(fdlg.selectedFiles()[0]);
}
+void ExportRawDataDialog::onFileNameChanged(const QString& name)
+{
+ emit fileNameChanged(name);
+}
+
ExportRawDataDialog::~ExportRawDataDialog()
{
delete ui;
QStringList m_supportedFormats;
Ui::ExportRawDataDialog* ui;
+public slots:
+ void onFormatChanged(const int idx);
+
private slots:
void onBrowseClicked();
+ void onFileNameChanged(const QString& name);
+signals:
+ void fileNameChanged(const QString& name);
};
#endif // EXPORTRAWDATADIALOG_H
#include "gui/graphviewcontextmenu.h"
#include "signaldrawer.h"
-#include "mathhelpers.h"
+#include "helpers.h"
#include "metatypes.h"
#include <QtGui/QMouseEvent>
#include <QtWidgets/QWidget>
#include "gui/signalview.h"
#include "ui_signalview.h"
-#include "mathhelpers.h"
+#include "helpers.h"
#include <cmath>
#include <QtGui/QFontMetrics>
#include <QtGui/QPainter>
*/
-#ifndef MATHHELPERS_H
-#define MATHHELPERS_H
+#ifndef HELPERS_H
+#define HELPERS_H
-template <typename T> T average(T* vals, size_t len) {
+#include <QtCore/QString>
+
+template <typename T> static T average(T* vals, size_t len) {
T sum = 0;
for (size_t i = 0; i < len; i++)
sum += vals[i];
return sum / len;
}
-template <typename T> T clamp(T in, T min, T max) {
+template <typename T> static T clamp(T in, T min, T max) {
if (in < min)
return min;
if (in > max)
return max;
}
+static QString getFileNameSuffix(const QString& name)
+{
+ int dotIdx = name.lastIndexOf('.');
+
+ if (dotIdx < 0)
+ return "";
+
+ return name.mid(dotIdx+1).toLower();
+}
+
+
#endif // MATHHELPERS_H
*/
+#include "helpers.h"
#include "imagedrawer.h"
#include <QtCore/QFile>
#include <QtWidgets/QMessageBox>
+
#include <QDebug>
-ImageDrawer::ImageDrawer(std::shared_ptr<SignalController> controller) :
+ImageDrawer::ImageDrawer(std::shared_ptr<SignalController> controller, QObject* parent) :
+ QObject(parent),
SignalDrawer(controller),
m_controller(controller)
{
QImage image;
QFile file(filename);
+ if (m_controller == nullptr)
+ return ReturnCode::E_NO_CONTROLLER;
+
if (!setDimensions(width, height))
return ReturnCode::E_INVAL_DIMENSIONS;
if (!draw(fromX, fromY, toX, toY, layers))
{
return QImageWriter::supportedImageFormats();
}
+
+int ImageDrawer::guessFormatFromSuffix(const QString& name)
+{
+ QList<QByteArray> formats = ImageDrawer::supportedImageFormats();
+ QString suffix = getFileNameSuffix(name);
+
+ if (suffix.length() == 0)
+ return -1;
+
+ for (int i = 0; i < formats.size(); i++) {
+ const QString f = QString(formats.at(i));
+ if (suffix.compare(f) == 0) {
+ qDebug() << "Changing format to idx" << i << ", type" << f;
+ return i;
+ }
+ }
+
+ qDebug() << "Cannot guess type from suffix";
+ return -1;
+}
#ifndef IMAGEDRAWER_H
#define IMAGEDRAWER_H
+#include "gui/exportgraphtoimagedialog.h"
#include "signaldrawer.h"
#include <QtGui/QImageWriter>
-class ImageDrawer : protected SignalDrawer
+class ImageDrawer : public QObject, protected SignalDrawer
{
+ Q_OBJECT
public:
enum class ReturnCode {
SUCCESS,
- E_INVAL_DIMENSIONS,
E_CANNOT_DRAW,
E_CANNOT_CREATE_IMAGE,
E_CANNOT_WRITE_IMAGE,
+ E_INVAL_DIMENSIONS,
+ E_NO_CONTROLLER,
E_WOULD_OVERWRITE
};
- explicit ImageDrawer(std::shared_ptr<SignalController> controller);
+ explicit ImageDrawer(std::shared_ptr<SignalController> controller, QObject* parent = nullptr);
ReturnCode render(const QString& filename, const QByteArray format, const int width, const int height,
const double fromX, const double fromY, const double toX, const double toY, const GraphLayers layers);
+ static int guessFormatFromSuffix(const QString& name);
static QList<QByteArray> supportedImageFormats();
private:
std::shared_ptr<SignalController> m_controller;
QImageWriter m_imageWriter;
+public slots:
+
+signals:
+ void formatChanged(const int idx);
};