From 702d8f2a1e0ff1c61c483ea44a0e7ebc951b43d6 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sun, 13 Sep 2026 20:20:42 +0200 Subject: [PATCH] viewer: the background is a control, and shortcuts work from every panel The display mapping always had a black point; it was just pinned to zero with no way to move it. It gets the second slider in the display toolbar (0 = off, as before; both sliders are width-capped so the strip stays tight), a logarithmic scale that starts at a true zero, and the wheel with B held, mirroring F for the foreground. The white point now floors at the black point plus one. F, B, A and Home/End/PageUp/PageDown used to reach their targets only with the image focused, because they lived in keyPressEvent. They move into an application-wide event filter that steps aside when the focused widget genuinely uses the key: text entry gets everything, the file tree keeps its navigation keys, and open menus are untouched. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_015vRwzF8PLaaa2novAKYorq --- viewer/JFJochViewerWindow.cpp | 83 ++++++++++++++----- viewer/JFJochViewerWindow.h | 9 +- viewer/image_viewer/JFJochImage.cpp | 17 +++- viewer/image_viewer/JFJochImage.h | 2 + viewer/toolbar/JFJochViewerToolbarDisplay.cpp | 64 ++++++++++++-- viewer/toolbar/JFJochViewerToolbarDisplay.h | 17 +++- viewer/widgets/SliderPlusBox.cpp | 22 +++-- 7 files changed, 170 insertions(+), 44 deletions(-) diff --git a/viewer/JFJochViewerWindow.cpp b/viewer/JFJochViewerWindow.cpp index 753e4e4c8..857765049 100644 --- a/viewer/JFJochViewerWindow.cpp +++ b/viewer/JFJochViewerWindow.cpp @@ -7,6 +7,13 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -236,6 +243,12 @@ JFJochViewerWindow::JFJochViewerWindow(QWidget *parent, bool dbus, const QString connect(toolBarDisplay, &JFJochViewerToolbarDisplay::setForeground, viewer, &JFJochDiffractionImage::changeForeground); + connect(toolBarDisplay, &JFJochViewerToolbarDisplay::setBackground, viewer, + &JFJochDiffractionImage::changeBackground); + + connect(viewer, &JFJochDiffractionImage::backgroundChanged, + toolBarDisplay, &JFJochViewerToolbarDisplay::updateBackground); + connect(viewer, &JFJochDiffractionImage::autoForegroundChanged, toolBarDisplay, &JFJochViewerToolbarDisplay::updateAutoForeground); @@ -439,12 +452,21 @@ JFJochViewerWindow::JFJochViewerWindow(QWidget *parent, bool dbus, const QString connect(this, &JFJochViewerWindow::adjustForegroundButton, viewer, &JFJochDiffractionImage::adjustForeground); + connect(this, &JFJochViewerWindow::adjustBackgroundButton, + viewer, &JFJochDiffractionImage::adjustBackground); + connect(this, &JFJochViewerWindow::setAutoForeground, viewer, &JFJochDiffractionImage::setAutoForeground); connect(this, &JFJochViewerWindow::oneShotAutoForeground, viewer, &JFJochDiffractionImage::oneShotAutoForeground); + connect(this, &JFJochViewerWindow::stepImage, + toolBarImage, &JFJochViewerToolbarImage::stepImage); + + // See eventFilter: window-wide keyboard shortcuts, whatever widget has the focus. + qApp->installEventFilter(this); + // Processing jobs: run rugnux locally / copy a cluster command line, and surface // finished runs as switchable dataset snapshots. connect(processingJobsWindow, &JFJochProcessingJobsWindow::registerSnapshot, @@ -777,27 +799,48 @@ void JFJochViewerWindow::NewDatasetInfo() { statusbar, &JFJochViewerStatusBar::display); } -void JFJochViewerWindow::keyPressEvent(QKeyEvent *event) { - if (event->key() == Qt::Key_F) { - emit adjustForegroundButton(true); - event->accept(); - return; +bool JFJochViewerWindow::eventFilter(QObject *obj, QEvent *event) { + // The filter sees every key of this window before the focused widget does, so F/B/A and the + // frame-navigation keys work with any panel focused, not only the image. It steps aside when + // the focused widget genuinely uses the key: any text-entry widget for all of them, item + // views (the file tree) for the navigation keys, and everything while a popup is open. + const auto type = event->type(); + if ((type == QEvent::KeyPress || type == QEvent::KeyRelease) + && QApplication::activeWindow() == this && !QApplication::activePopupWidget()) { + const auto *ke = static_cast(event); + const int key = ke->key(); + QWidget *focus = QApplication::focusWidget(); + const bool editing = qobject_cast(focus) || qobject_cast(focus) + || qobject_cast(focus) || qobject_cast(focus) + || qobject_cast(focus); + const bool plain = (ke->modifiers() & ~Qt::KeypadModifier) == Qt::NoModifier; + if (!editing && plain) { + if (type == QEvent::KeyPress) { + switch (key) { + case Qt::Key_F: emit adjustForegroundButton(true); return true; + case Qt::Key_B: emit adjustBackgroundButton(true); return true; + case Qt::Key_A: + if (!ke->isAutoRepeat()) + emit oneShotAutoForeground(); + return true; + default: break; + } + if (!qobject_cast(focus)) { + switch (key) { + case Qt::Key_Home: emit stepImage(std::numeric_limits::min()); return true; + case Qt::Key_End: emit stepImage(std::numeric_limits::max()); return true; + case Qt::Key_PageUp: emit stepImage(1); return true; + case Qt::Key_PageDown: emit stepImage(-1); return true; + default: break; + } + } + } else { + if (key == Qt::Key_F) { emit adjustForegroundButton(false); return true; } + if (key == Qt::Key_B) { emit adjustBackgroundButton(false); return true; } + } } - if (event->key() == Qt::Key_A && ! event->isAutoRepeat()) { - emit oneShotAutoForeground(); - event->accept(); - return; - } - QMainWindow::keyPressEvent(event); -} - -void JFJochViewerWindow::keyReleaseEvent(QKeyEvent *event) { - if (event->key() == Qt::Key_F) { - emit adjustForegroundButton(false); - event->accept(); - return; - } - QMainWindow::keyReleaseEvent(event); + } + return QMainWindow::eventFilter(obj, event); } void JFJochViewerWindow::OnFileLoadError(QString title, QString message) { diff --git a/viewer/JFJochViewerWindow.h b/viewer/JFJochViewerWindow.h index 188279301..86cabdb34 100644 --- a/viewer/JFJochViewerWindow.h +++ b/viewer/JFJochViewerWindow.h @@ -72,8 +72,10 @@ private: enum class Perspective { Image, Processing }; void ApplyPerspective(Perspective p); - void keyPressEvent(QKeyEvent *event) override; - void keyReleaseEvent(QKeyEvent *event) override; + // Keyboard shortcuts (F/B/A, Home/End/PageUp/PageDown) are handled in an application-wide + // event filter rather than keyPressEvent, so they work wherever the focus happens to be, + // stepping aside when the focused widget genuinely uses the key (text entry, item views). + bool eventFilter(QObject *obj, QEvent *event) override; void closeEvent(QCloseEvent *event) override; void changeEvent(QEvent *event) override; @@ -108,7 +110,10 @@ signals: void LoadFileRequest(const QString &filename, qint64 image_number, qint64 summation, bool retry); void LoadImageRequest(int64_t image_number, int64_t summation); void adjustForegroundButton(bool input); + void adjustBackgroundButton(bool input); void setAutoForeground(bool val); void oneShotAutoForeground(); + // Re-emitted by the shortcut filter; the navigation toolbar applies the step, clamped. + void stepImage(int steps); }; diff --git a/viewer/image_viewer/JFJochImage.cpp b/viewer/image_viewer/JFJochImage.cpp index cd2141ab7..d58b6887d 100644 --- a/viewer/image_viewer/JFJochImage.cpp +++ b/viewer/image_viewer/JFJochImage.cpp @@ -172,7 +172,13 @@ void JFJochImage::wheelEvent(QWheelEvent *event) { const bool exp_fg_adjust = (event->modifiers() & Qt::ControlModifier); const bool lin_fg_adjust = (event->modifiers() & Qt::ShiftModifier) || m_adjustForegroundWithWheel; - if (exp_fg_adjust || lin_fg_adjust) { + if (m_adjustBackgroundWithWheel) { + // Held B: the wheel moves the black point, clamped so a positive display range remains. + float new_background = background + event->angleDelta().y() / 120.0f; + new_background = std::clamp(new_background, 0.0f, std::max(0.0f, foreground - 1.0f)); + changeBackground(new_background); + emit backgroundChanged(background); + } else if (exp_fg_adjust || lin_fg_adjust) { float new_foreground = foreground; if (exp_fg_adjust) { @@ -182,8 +188,9 @@ void JFJochImage::wheelEvent(QWheelEvent *event) { new_foreground = foreground + event->angleDelta().y() / 120.0f; } - if (new_foreground < 1.0f) - new_foreground = 1.0f; + // Keep the white point above the black point (1.0 when the background sits at zero). + if (new_foreground < background + 1.0f) + new_foreground = background + 1.0f; changeForeground(new_foreground); emit foregroundChanged(foreground); @@ -1046,6 +1053,10 @@ void JFJochImage::adjustForeground(bool input) { m_adjustForegroundWithWheel = input; } +void JFJochImage::adjustBackground(bool input) { + m_adjustBackgroundWithWheel = input; +} + double JFJochImage::GetScaleFactor() const { return scale_factor; } diff --git a/viewer/image_viewer/JFJochImage.h b/viewer/image_viewer/JFJochImage.h index 6c1b6541d..73349a979 100644 --- a/viewer/image_viewer/JFJochImage.h +++ b/viewer/image_viewer/JFJochImage.h @@ -73,6 +73,7 @@ class JFJochImage : public QGraphicsView { Q_OBJECT bool m_adjustForegroundWithWheel = false; + bool m_adjustBackgroundWithWheel = false; // Viewport-lock support: guard prevents the emit<->apply ping-pong between // two linked views, and the helper broadcasts the current transform+center. @@ -276,6 +277,7 @@ public slots: void fitToView(); void adjustForeground(bool input); + void adjustBackground(bool input); void setZoom(double input); public: explicit JFJochImage(QWidget *parent = nullptr); diff --git a/viewer/toolbar/JFJochViewerToolbarDisplay.cpp b/viewer/toolbar/JFJochViewerToolbarDisplay.cpp index 19882b7f2..d9fac3972 100644 --- a/viewer/toolbar/JFJochViewerToolbarDisplay.cpp +++ b/viewer/toolbar/JFJochViewerToolbarDisplay.cpp @@ -18,19 +18,41 @@ JFJochViewerToolbarDisplay::JFJochViewerToolbarDisplay(QWidget *parent) connect(foreground_tail_timer_, &QTimer::timeout, this, &JFJochViewerToolbarDisplay::ApplyPendingForeground); + background_tail_timer_ = new QTimer(this); + background_tail_timer_->setSingleShot(true); + connect(background_tail_timer_, &QTimer::timeout, + this, &JFJochViewerToolbarDisplay::ApplyPendingBackground); + + const QString sliderStyle = + "QSlider::groove:horizontal { height:6px; background:#F3D9D4; border-radius:3px; }" + "QSlider::sub-page:horizontal { background:#FA7268; border-radius:3px; }" + "QSlider::handle:horizontal { background:#1F3A5F; width:14px; margin:-5px 0; border-radius:7px; }" + "QSlider::handle:horizontal:hover { background:#16314F; }"; + // Two sliders share the strip, so each is capped rather than expanding across the window. + const int slider_cap = fontMetrics().averageCharWidth() * 36; + addWidget(new QLabel(" Foreground ", this)); foreground_slider = new SliderPlusBox(1, MAX_SLIDER_NO_IMAGE, 1.0, 1, this, SliderPlusBox::ScaleType::Logarithmic); foreground_slider->setValue(10); - foreground_slider->setToolTip("White point of the color map (image contrast)"); - foreground_slider->setStyleSheet( - "QSlider::groove:horizontal { height:6px; background:#F3D9D4; border-radius:3px; }" - "QSlider::sub-page:horizontal { background:#FA7268; border-radius:3px; }" - "QSlider::handle:horizontal { background:#1F3A5F; width:14px; margin:-5px 0; border-radius:7px; }" - "QSlider::handle:horizontal:hover { background:#16314F; }"); + foreground_slider->setToolTip("White point of the color map (image contrast); hold F and use" + " the mouse wheel, Ctrl+wheel for coarse steps"); + foreground_slider->setStyleSheet(sliderStyle); + foreground_slider->setMaximumWidth(slider_cap); addWidget(foreground_slider); + addWidget(new QLabel(" Background ", this)); + + background_slider = new SliderPlusBox(0, MAX_SLIDER_NO_IMAGE, 1.0, 1, this, + SliderPlusBox::ScaleType::Logarithmic); + background_slider->setValue(0); + background_slider->setToolTip("Black point of the color map (0 = off); hold B and use" + " the mouse wheel"); + background_slider->setStyleSheet(sliderStyle); + background_slider->setMaximumWidth(slider_cap); + addWidget(background_slider); + auto makeToggle = [this](const QString &text, const QString &tip) { auto *b = new QToolButton(this); b->setText(text); @@ -63,6 +85,7 @@ JFJochViewerToolbarDisplay::JFJochViewerToolbarDisplay(QWidget *parent) addWidget(color_map_select); connect(foreground_slider, &SliderPlusBox::valueChanged, this, &JFJochViewerToolbarDisplay::foregroundSet); + connect(background_slider, &SliderPlusBox::valueChanged, this, &JFJochViewerToolbarDisplay::backgroundSet); connect(color_map_select, QOverload::of(&QComboBox::currentIndexChanged), this, &JFJochViewerToolbarDisplay::colorComboBoxSet); connect(auto_foreground_button, &QToolButton::clicked, this, &JFJochViewerToolbarDisplay::autoForegroundButtonPressed); @@ -76,6 +99,27 @@ void JFJochViewerToolbarDisplay::foregroundSet(double val) { emit setForeground(static_cast(val)); } +void JFJochViewerToolbarDisplay::backgroundSet(double val) { + emit setBackground(static_cast(val)); +} + +void JFJochViewerToolbarDisplay::updateBackground(float val) { + pending_background_ = val; + if (RemoteDisplayMode() && background_rate_.isValid() + && background_rate_.elapsed() < kRemoteRepaintIntervalMs) { + background_tail_timer_->start(kRemoteRepaintIntervalMs); + return; + } + background_tail_timer_->stop(); + ApplyPendingBackground(); +} + +void JFJochViewerToolbarDisplay::ApplyPendingBackground() { + background_rate_.restart(); + QSignalBlocker blocker(background_slider); + background_slider->setValue(pending_background_); +} + void JFJochViewerToolbarDisplay::updateForeground(float val) { pending_foreground_ = val; if (RemoteDisplayMode() && foreground_rate_.isValid() @@ -123,11 +167,15 @@ void JFJochViewerToolbarDisplay::HDRModeButtonPressed() { void JFJochViewerToolbarDisplay::imageLoaded(std::shared_ptr image) { if (image) { auto valid = image->ValidMinMax(); - if (valid.has_value() && valid->second > 1) + if (valid.has_value() && valid->second > 1) { foreground_slider->setMax(valid->second); - else + background_slider->setMax(valid->second); + } else { foreground_slider->setMax(MAX_SLIDER_NO_IMAGE); + background_slider->setMax(MAX_SLIDER_NO_IMAGE); + } } else { foreground_slider->setMax(MAX_SLIDER_NO_IMAGE); + background_slider->setMax(MAX_SLIDER_NO_IMAGE); } } diff --git a/viewer/toolbar/JFJochViewerToolbarDisplay.h b/viewer/toolbar/JFJochViewerToolbarDisplay.h index 8e486533f..0f41efe33 100644 --- a/viewer/toolbar/JFJochViewerToolbarDisplay.h +++ b/viewer/toolbar/JFJochViewerToolbarDisplay.h @@ -11,26 +11,32 @@ #include #include "../../reader/JFJochReaderImage.h" -// Display toolbar: how the image is rendered — foreground (white-point) slider, auto-contrast, -// HDR (full dynamic range), and the colour map. The two "Reanalyze" hero buttons are appended -// to this bar by the main window. +// Display toolbar: how the image is rendered — foreground (white-point) and background +// (black-point) sliders, auto-contrast, HDR (full dynamic range), and the colour map. The two +// "Reanalyze" hero buttons are appended to this bar by the main window. class JFJochViewerToolbarDisplay : public QToolBar { Q_OBJECT bool auto_foreground = false; SliderPlusBox *foreground_slider; + SliderPlusBox *background_slider; QToolButton *auto_foreground_button; QToolButton *hdr_mode_button; QComboBox *color_map_select; // A ctrl-wheel or auto-foreground burst reports a new foreground per event, and on a remote // display each slider update is a repaint of its own. Hold the readback to the remote - // cadence; the tail run shows the final value. + // cadence; the tail run shows the final value. The background readback (held B + wheel) + // gets the same treatment. void ApplyPendingForeground(); float pending_foreground_ = 0.0f; QElapsedTimer foreground_rate_; QTimer *foreground_tail_timer_ = nullptr; + void ApplyPendingBackground(); + float pending_background_ = 0.0f; + QElapsedTimer background_rate_; + QTimer *background_tail_timer_ = nullptr; public: JFJochViewerToolbarDisplay(QWidget *parent = nullptr); @@ -41,18 +47,21 @@ public: signals: void setForeground(float val); + void setBackground(float val); void colorMapChanged(int val); void setAutoForeground(bool val); void setHDRMode(bool val); public slots: void updateForeground(float val); + void updateBackground(float val); void updateAutoForeground(bool val); void updateColorMap(int val); void updateHDRMode(bool val); void imageLoaded(std::shared_ptr image); private slots: void foregroundSet(double val); + void backgroundSet(double val); void colorComboBoxSet(int val); void autoForegroundButtonPressed(); void HDRModeButtonPressed(); diff --git a/viewer/widgets/SliderPlusBox.cpp b/viewer/widgets/SliderPlusBox.cpp index ce117eb0e..ab4571b3f 100644 --- a/viewer/widgets/SliderPlusBox.cpp +++ b/viewer/widgets/SliderPlusBox.cpp @@ -10,9 +10,11 @@ SliderPlusBox::SliderPlusBox(double min, double max, double step, int decimals, throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Scale factor for SliderPlusBox must be positive"); - if (m_scaleType == Logarithmic && m_min <= 0) + // A logarithmic scale accepts a minimum of exactly zero: slider position 0 means 0 ("off"), + // and the rest of the track spans [1, max] logarithmically. + if (m_scaleType == Logarithmic && m_min < 0) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, - "Minimum value must be greater than zero for logarithmic scale"); + "Minimum value must not be negative for logarithmic scale"); if (m_max <= m_min) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, @@ -89,8 +91,11 @@ double SliderPlusBox::sliderToValue(int sliderValue) const { if (m_scaleType == Linear) { return sliderValue * m_step; } else { // Logarithmic + const double lo = (m_min > 0.0) ? m_min : 1.0; + if (m_min <= 0.0 && sliderValue <= 0) + return 0.0; // Map from slider position (0-1000) to logarithmic value range - double logMin = std::log10(m_min); + double logMin = std::log10(lo); double logMax = std::log10(m_max); double normalizedPos = static_cast(sliderValue) / 1000.0; double logValue = logMin + normalizedPos * (logMax - logMin); @@ -102,10 +107,13 @@ int SliderPlusBox::valueToSlider(double value) const { if (m_scaleType == Linear) { return static_cast(value / m_step); } else { // Logarithmic + const double lo = (m_min > 0.0) ? m_min : 1.0; + if (m_min <= 0.0 && value < lo) + return 0; // Map from value to slider position (0-1000) - double logMin = std::log10(m_min); + double logMin = std::log10(lo); double logMax = std::log10(m_max); - double logValue = std::log10(std::max(value, m_min)); // Ensure we don't go below min + double logValue = std::log10(std::max(value, lo)); // Ensure we don't go below min double normalizedPos = (logValue - logMin) / (logMax - logMin); return static_cast(normalizedPos * 1000.0); } @@ -137,8 +145,8 @@ void SliderPlusBox::setScaleType(ScaleType type) { if (m_scaleType == type) return; - if (type == Logarithmic && m_min <= 0) { - qWarning() << "Cannot switch to logarithmic scale with min <= 0"; + if (type == Logarithmic && m_min < 0) { + qWarning() << "Cannot switch to logarithmic scale with min < 0"; return; }