Files
Jungfraujoch/viewer/widgets/LiveComboBox.h
T
leonarski_fandClaude Opus 5.5 9cf987cd4e jfjoch_viewer: live updates no longer rebuild open combo boxes
During a collection every dataset tick ran JFJochViewerDatasetInfo::UpdateLabels(), which
clear()ed the plot-type combo and re-added every item, so an open dropdown lost its hovered
row and scroll position on each tick and picking a plot type was a race against the next
update. The same clear-and-refill ran per tick or per image in the calibration window's
calibration combo, the ROI list combo and the powder calibrant combo; the last two also
threw away the user's choice (the calibrant was reset to the first entry on every image).

New LiveComboBox::SetItems() takes the full item list and leaves the combo untouched when the
list is unchanged. When it does change while the popup is open, the change is held back and
applied (queued) once the popup window hides, so a click that closes the popup lands first.
A replacement keeps the selection by item data, blocks signals, and a held-back one emits
itemsReplaced() so the owner can re-plot.

The calibration window now re-shows the selected calibration on a dataset tick instead of
always reloading the pixel mask over it.

Tested offscreen: a timer fires datasetLoaded() every 25 ms while the dropdown is open; the
popup and hovered row survive, newly available indexing metrics are held back until the popup
closes, and the picked type sticks and is what the chart plots. The rc175 code fails the
hover checks of the same harness.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-12 19:46:48 +02:00

44 lines
1.5 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <QComboBox>
#include <QColor>
#include <QVector>
#include <optional>
// A combo box whose items are refreshed by live updates. SetItems() leaves the items alone when
// they are unchanged, and while the popup is open it holds the change back until the popup has
// closed, so a live update never closes the dropdown or moves the selection under the user.
// Replacing the items keeps the selection by item data (the first item when it is gone) and emits
// no currentIndexChanged; a replacement held back over an open popup emits itemsReplaced() instead.
class LiveComboBox : public QComboBox {
Q_OBJECT
public:
struct Item {
QString text;
QVariant data;
QString tooltip;
QColor swatch; // shown as a small square icon when valid
bool separator = false;
bool operator==(const Item &other) const = default;
};
explicit LiveComboBox(QWidget *parent = nullptr);
// True if the items were replaced now; false if unchanged or held back for an open popup.
bool SetItems(const QVector<Item> &items);
signals:
void itemsReplaced();
private:
QVector<Item> items_;
std::optional<QVector<Item>> pending_;
void Replace(const QVector<Item> &items);
void ApplyPending();
bool eventFilter(QObject *watched, QEvent *event) override;
};