From 2415717ce014f85bd973364579e694715f7671b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Thu, 10 Apr 2025 23:09:59 +0200 Subject: [PATCH] Add STFSlider ability to be vertical --- stfslider.cpp | 90 +++++++++++++++++++++++++++++++++++++++------- stfslider.h | 3 ++ stretchtoolbar.cpp | 28 +++++++-------- 3 files changed, 93 insertions(+), 28 deletions(-) diff --git a/stfslider.cpp b/stfslider.cpp index b2756db..d9bdbb6 100644 --- a/stfslider.cpp +++ b/stfslider.cpp @@ -12,7 +12,7 @@ static float clamp(float x) STFSlider::STFSlider(const QColor &color, QWidget *parent) : QWidget(parent) { - setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); setMinimumWidth(100); setMouseTracking(true); @@ -64,12 +64,51 @@ void STFSlider::setMTFParams(float low, float mid, float high) update(); } +void STFSlider::orientationChanged(Qt::Orientations orientation) +{ + m_orientation = orientation; + if(m_orientation == Qt::Horizontal) + { + if(m_color == Qt::white) + { + setMaximumSize(QWIDGETSIZE_MAX, 16); + setMinimumSize(16, 16); + } + else + { + setMaximumSize(QWIDGETSIZE_MAX, 10); + setMinimumSize(10, 10); + } + } + else + { + if(m_color == Qt::white) + { + setMaximumSize(16, QWIDGETSIZE_MAX); + setMinimumSize(16, 16); + } + else + { + setMaximumSize(10, QWIDGETSIZE_MAX); + setMinimumSize(10, 10); + } + } +} + void STFSlider::paintEvent(QPaintEvent *event) { QPainter painter(this); QRect rect = event->rect(); qreal w = rect.width() - 1; qreal h = rect.height(); + if(m_orientation == Qt::Vertical) + { + rect = rect.transposed(); + painter.rotate(90); + w = rect.width() - 1; + h = rect.height(); + painter.translate(0, -h); + } QLinearGradient gradient(rect.topLeft(), rect.topRight()); gradient.setColorAt(0, Qt::black); for(int i=1; i<=32; i++) @@ -93,6 +132,11 @@ void STFSlider::paintEvent(QPaintEvent *event) { painter.setPen(p < m_threshold ? Qt::white : Qt::black); painter.resetTransform(); + if(m_orientation == Qt::Vertical) + { + painter.rotate(90); + painter.translate(0, -h); + } painter.translate(w*p, 0); painter.drawPath(tick); }; @@ -105,15 +149,26 @@ void STFSlider::paintEvent(QPaintEvent *event) void STFSlider::mouseMoveEvent(QMouseEvent *event) { - const qreal x = event->position().x(); - if(std::abs(m_blackPoint*width() - x) < 5 || - std::abs((m_blackPoint + (m_whitePoint - m_blackPoint) * m_midPoint)*width() - x) < 5 || - std::abs(m_whitePoint*width() - x) < 5) - setCursor(Qt::SplitHCursor); + qreal x,w; + if(m_orientation == Qt::Horizontal) + { + x = event->position().x(); + w = width(); + } + else + { + x = event->position().y(); + w = height(); + } + + if(std::abs(m_blackPoint*w - x) < 5 || + std::abs((m_blackPoint + (m_whitePoint - m_blackPoint) * m_midPoint)*w - x) < 5 || + std::abs(m_whitePoint*w - x) < 5) + setCursor(m_orientation == Qt::Horizontal ? Qt::SplitHCursor : Qt::SplitVCursor); else unsetCursor(); - qreal xw = x/width(); + qreal xw = x/w; if(event->modifiers() & Qt::ShiftModifier && !m_fineTune) { m_fineTune = true; @@ -154,18 +209,29 @@ void STFSlider::mouseMoveEvent(QMouseEvent *event) void STFSlider::mousePressEvent(QMouseEvent *event) { - const qreal x = event->position().x(); + qreal x,w; + if(m_orientation == Qt::Horizontal) + { + x = event->position().x(); + w = width(); + } + else + { + x = event->position().y(); + w = height(); + } + if(event->modifiers() & Qt::ShiftModifier) { m_fineTune = true; - m_fineTuneX = x/width(); + m_fineTuneX = x/w; } - if(std::abs((m_blackPoint + (m_whitePoint - m_blackPoint) * m_midPoint)*width() - x) < 5) + if(std::abs((m_blackPoint + (m_whitePoint - m_blackPoint) * m_midPoint)*w - x) < 5) m_grabbed = 1; - else if(std::abs(m_blackPoint*width() - x) < 5) + else if(std::abs(m_blackPoint*w - x) < 5) m_grabbed = 0; - else if(std::abs(m_whitePoint*width() - x) < 5) + else if(std::abs(m_whitePoint*w - x) < 5) m_grabbed = 2; else m_grabbed = -1; diff --git a/stfslider.h b/stfslider.h index dd3eee9..b861d76 100644 --- a/stfslider.h +++ b/stfslider.h @@ -15,12 +15,15 @@ class STFSlider : public QWidget float m_fineTuneX; QColor m_color; float m_threshold; + Qt::Orientations m_orientation = Qt::Horizontal; public: explicit STFSlider(const QColor &color = Qt::white, QWidget *parent = nullptr); float blackPoint() const; float midPoint() const; float whitePoint() const; void setMTFParams(float low, float mid, float high); +public slots: + void orientationChanged(Qt::Orientations orientation); signals: void paramChanged(float blackPoint, float midPoint, float whitePoint); protected: diff --git a/stretchtoolbar.cpp b/stretchtoolbar.cpp index 3968549..0042be4 100644 --- a/stretchtoolbar.cpp +++ b/stretchtoolbar.cpp @@ -6,17 +6,6 @@ #include #include "imageringlist.h" -const float BLACK_POINT_SIGMA = -2.8f; -const float MAD_TO_SIGMA = 1.4826f; -const float TARGET_BACKGROUND = 0.25f; - -float MTF(float x, float m) -{ - if(x < 0)return 0; - if(x > 1)return 1; - return ((m - 1) * x) / ((2 * m - 1) * x - m); -} - StretchToolbar::StretchToolbar(QWidget *parent) : QToolBar(tr("Stretch toolbar"), parent) { setObjectName("stretchtoolbar"); @@ -24,16 +13,23 @@ StretchToolbar::StretchToolbar(QWidget *parent) : QToolBar(tr("Stretch toolbar") QVBoxLayout *vbox1 = new QVBoxLayout(lum); m_stfSlider = new STFSlider(Qt::white, this); vbox1->addWidget(m_stfSlider); + connect(this, &StretchToolbar::orientationChanged, m_stfSlider, &STFSlider::orientationChanged); m_stfSliderR = new STFSlider(Qt::red, this); m_stfSliderG = new STFSlider(Qt::green, this); m_stfSliderB = new STFSlider(Qt::blue, this); QWidget *rgb = new QWidget(this); - QVBoxLayout *vbox2 = new QVBoxLayout(rgb); - vbox2->setSpacing(0); - vbox2->addWidget(m_stfSliderR); - vbox2->addWidget(m_stfSliderG); - vbox2->addWidget(m_stfSliderB); + QBoxLayout *box2 = new QBoxLayout(orientation() == Qt::Horizontal ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight, rgb); + box2->setSpacing(0); + box2->addWidget(m_stfSliderR); + box2->addWidget(m_stfSliderG); + box2->addWidget(m_stfSliderB); + connect(this, &StretchToolbar::orientationChanged, m_stfSliderR, &STFSlider::orientationChanged); + connect(this, &StretchToolbar::orientationChanged, m_stfSliderG, &STFSlider::orientationChanged); + connect(this, &StretchToolbar::orientationChanged, m_stfSliderB, &STFSlider::orientationChanged); + connect(this, &StretchToolbar::orientationChanged, [box2](Qt::Orientations orientation){ + box2->setDirection(orientation == Qt::Horizontal ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight); + }); m_stack = new QStackedWidget(this); m_stack->addWidget(lum);