From 76157ae1bc477533111329c89d64dc6cdb0563b7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Sun, 9 Mar 2014 23:43:18 +0100 Subject: [PATCH] - Make the portion of the signal that is exported to image adjustable - Use the current zoom level as default portion of the signal in the "export to image" setup dialog --- datamanager.cpp | 12 ++++++++++-- gui/graphview.cpp | 11 +++++++---- imagedrawer.cpp | 5 +++-- imagedrawer.h | 3 ++- signalcontroller.cpp | 26 ++++++++++++++++++++++++++ signalcontroller.h | 27 +++++++++++++++++++++++---- 6 files changed, 71 insertions(+), 13 deletions(-) diff --git a/datamanager.cpp b/datamanager.cpp index e0555b4..11b9ae5 100644 --- a/datamanager.cpp +++ b/datamanager.cpp @@ -411,7 +411,8 @@ void DataManager::onExportGraphToImage() return; for (const std::pair>& p : sr->allControllers()) { - dlg.addSignal(p.second->fromXAbs(), p.second->fromYAbs(), p.second->toXAbs(), p.second->toYAbs(), p.first); + GUIViewport vp = p.second->guiViewport(); + dlg.addSignal(vp.absFromTime, vp.absFromValue, vp.absToTime, vp.absToValue, p.first); } do { @@ -420,6 +421,7 @@ void DataManager::onExportGraphToImage() std::shared_ptr ctrl = sr->controllerAt(key); GraphLayers gl = GraphLayers::GRAPH | GraphLayers::SCALE; ImageDrawer::ReturnCode drwRet; + double relFromX, relFromY, relToX, relToY; if (ctrl == nullptr) { Logger::log(Logger::Level::CRITICAL, ME_SENDER_STR, "Invalid key " + QString::fromStdString(key) + " passed to image exporter"); @@ -434,8 +436,14 @@ void DataManager::onExportGraphToImage() if (dlg.includePeaks()) gl |= GraphLayers::INTEGRATION; + relFromX = ctrl->timeToRel(dlg.fromX()); + relToX = ctrl->timeToRel(dlg.toX()); + relFromY = ctrl->valueToRel(dlg.fromY()); + relToY = ctrl->valueToRel(dlg.toY()); + ImageDrawer imageDrawer(ctrl); - drwRet = imageDrawer.render(dlg.path() + "." + dlg.imageFormat(), dlg.imageFormat().toLatin1(), dlg.imageWidth(), dlg.imageHeight(), gl); + drwRet = imageDrawer.render(dlg.path() + "." + dlg.imageFormat(), dlg.imageFormat().toLatin1(), dlg.imageWidth(), dlg.imageHeight(), + relFromX, relFromY, relToX, relToY, gl); switch (drwRet) { case ImageDrawer::ReturnCode::SUCCESS: ret = 0; diff --git a/gui/graphview.cpp b/gui/graphview.cpp index 20c5519..4239404 100644 --- a/gui/graphview.cpp +++ b/gui/graphview.cpp @@ -41,6 +41,7 @@ GraphView::GraphView(std::shared_ptr controller, QWidget* pare setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding)); setMinimumWidth(SignalDrawer::SCALE_MARGIN_VALUE); setMinimumHeight(SignalDrawer::SCALE_MARGIN_TIME); + m_controller->setGUIViewport(m_relXMin, m_relYMin, m_relXMax, m_relXMax); connect(&m_ctxMenu, SIGNAL(deletePeak(QPoint)), this, SLOT(onCtxMenuDeletePeak(QPoint))); connect(&m_ctxMenu, SIGNAL(zoomOut()), this, SLOT(onCtxMenuZoomOut())); @@ -439,9 +440,10 @@ void GraphView::updateValuesUnderCrosshair(const int xPix) void GraphView::unzoom() { setDefaultZoom(); - if (draw(m_relXMin, m_relYMin, m_relXMax, m_relYMax)) + if (draw(m_relXMin, m_relYMin, m_relXMax, m_relYMax)) { update(); - else + m_controller->setGUIViewport(m_relXMin, m_relYMin, m_relXMax, m_relYMax); + } else Logger::log(Logger::Level::CRITICAL, ME_SENDER_STR, __QFUNC__ + " unable to draw pixmap"); } @@ -456,9 +458,10 @@ void GraphView::setDefaultZoom() void GraphView::zoom(const int fromXPix, const int fromYPix, const int toXPix, const int toYPix) { bool ret = draw(xPixToRel(fromXPix), yPixToRel(fromYPix), xPixToRel(toXPix), yPixToRel(toYPix)); - if (ret) + if (ret) { update(); - else + m_controller->setGUIViewport(m_relXMin, m_relYMin, m_relXMax, m_relYMax); + } else Logger::log(Logger::Level::CRITICAL, ME_SENDER_STR, __QFUNC__ + " unable to draw pixmap"); } diff --git a/imagedrawer.cpp b/imagedrawer.cpp index 6771f66..d4c2042 100644 --- a/imagedrawer.cpp +++ b/imagedrawer.cpp @@ -11,14 +11,15 @@ ImageDrawer::ImageDrawer(std::shared_ptr controller) : m_imageWriter.setCompression(0); } -ImageDrawer::ReturnCode ImageDrawer::render(const QString& filename, const QByteArray format, const int width, const int height, const GraphLayers layers) +ImageDrawer::ReturnCode ImageDrawer::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) { QImage image; QFile file(filename); if (!setDimensions(width, height)) return ReturnCode::E_INVAL_DIMENSIONS; - if (!draw(SignalController::RELATIVE_MIN, SignalController::RELATIVE_MIN, SignalController::RELATIVE_MAX, SignalController::RELATIVE_MAX, layers)) + if (!draw(fromX, fromY, toX, toY, layers)) return ReturnCode::E_CANNOT_DRAW; m_imageWriter.setFileName(filename); diff --git a/imagedrawer.h b/imagedrawer.h index 12ba352..e3d0405 100644 --- a/imagedrawer.h +++ b/imagedrawer.h @@ -17,7 +17,8 @@ public: }; explicit ImageDrawer(std::shared_ptr controller); - ReturnCode render(const QString& filename, const QByteArray format, const int width, const int height, const GraphLayers layers); + 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 QList supportedImageFormats(); diff --git a/signalcontroller.cpp b/signalcontroller.cpp index 94880fb..70e0f78 100644 --- a/signalcontroller.cpp +++ b/signalcontroller.cpp @@ -237,6 +237,14 @@ double SignalController::valueToRel(const double value) return ret; } +double SignalController::timeToRel(const double time) +{ + const double fromTime = m_signal->timeAt(0); + const double toTime = m_signal->timeAt(m_signal->count()-1); + const double y = toTime - fromTime; + return (time - fromTime) * (RELATIVE_MAX - RELATIVE_MIN) / y; +} + size_t SignalController::relToIdx(const double rel) { size_t ret = (m_signal->count() - 1) * (rel - RELATIVE_MIN) / (RELATIVE_MAX - RELATIVE_MIN); @@ -251,6 +259,24 @@ double SignalController::relToValue(const double rel) return val; } +double SignalController::relToTime(const double rel) +{ + return m_signal->timeAt(relToIdx(rel)); +} + +void SignalController::setGUIViewport(const double fromX, const double fromY, const double toX, const double toY) +{ + m_guiViewport.absFromTime = relToTime(fromX); + m_guiViewport.absToTime = relToTime(toX); + m_guiViewport.absFromValue = relToValue(fromY); + m_guiViewport.absToValue = relToValue(toY); + + m_guiViewport.relFromX = fromX; + m_guiViewport.relFromY = fromY; + m_guiViewport.relToX = toX; + m_guiViewport.relToY = toY; +} + /** Public slots **/ SignalController::~SignalController() diff --git a/signalcontroller.h b/signalcontroller.h index f5c17f2..8f5574a 100644 --- a/signalcontroller.h +++ b/signalcontroller.h @@ -32,6 +32,18 @@ #include +struct GUIViewport { + double relFromX; + double relFromY; + double relToX; + double relToY; + + double absFromTime; + double absToTime; + double absFromValue; + double absToValue; +}; + struct GraphDrawData { GraphDrawData() : ddata(nullptr), ddataLen(0) {} @@ -83,15 +95,24 @@ public: std::shared_ptr getGraphDrawData(const double fromX, const double toX); std::vector getPeaksDrawData(const double fromX, const double fromY, const double toX, const double toY); RulerDrawData getRulerDrawData(const double from, const double to, SignalController::Axis axis); + GUIViewport guiViewport() const { return m_guiViewport; } double fromXAbs() const { return m_signal->timeAt(0); } double toXAbs() const { return m_signal->timeAt(m_signal->count()-1); } double fromYAbs() const { return m_signal->valueAt(0); } double toYAbs() const { return m_signal->valueAt(m_signal->count()-1); } PeakDrawData integratePeak(const double fromX, const double fromY, const double toX, const double toY); IntegrationTableModel* integrationTableModel() { return m_integTblModel;} + void setGUIViewport(const double fromX, const double fromY, const double toX, const double toY); const std::shared_ptr signal() const { return m_signal; } const std::shared_ptr cIntegrator() const { return m_integrator; } + double idxToRel(const size_t idx); + double valueToRel(const double value); + double timeToRel(const double time); + size_t relToIdx(const double rel); + double relToValue(const double rel); + double relToTime(const double rel); + static const double RELATIVE_MAX; static const double RELATIVE_MIN; @@ -101,10 +122,8 @@ private: std::shared_ptr m_integrator; std::shared_ptr m_signal; - double idxToRel(const size_t idx); - double valueToRel(const double value); - size_t relToIdx(const double rel); - double relToValue(const double rel); + GUIViewport m_guiViewport; + void drawIntegratedPeak(const std::shared_ptr peak); PeakDrawData genPeakDrawData(const std::shared_ptr peak); -- 2.43.5