Skip to content

Commit

Permalink
ui/video-presets: Fix locked preset not activating reliably
Browse files Browse the repository at this point in the history
Fixes a few bugs associated with locked presets. Restoring the selected
preset index is no longer dependent on the order of the presets in the
GUI, the output window's title bar is now updated with the title of the
locked preset when VCS starts up, and locked presets now apply correctly
when VCS is started while there's no capture signal.
  • Loading branch information
leikareipa committed Nov 1, 2023
1 parent 86dd54c commit 1898f8a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
20 changes: 16 additions & 4 deletions src/capture/video_presets.cpp
Expand Up @@ -95,8 +95,10 @@ void kvideopreset_lock(const bool isLocked)

void kvideopreset_activate_preset(const video_preset_s *const preset)
{
if (LOCKED || !kc_has_signal() || !preset)
{
if (
!preset ||
(LOCKED && (preset != MOST_RECENT_ACTIVE_PRESET))
){
return;
}

Expand Down Expand Up @@ -150,12 +152,12 @@ void kvideopreset_remove_all_presets(void)

void kvideopreset_apply_current_active_preset(void)
{
if (LOCKED || !kc_has_signal())
if (!kc_has_signal())
{
return;
}

const video_preset_s *activePreset = strongest_activating_preset();
const video_preset_s *activePreset = (LOCKED? MOST_RECENT_ACTIVE_PRESET : strongest_activating_preset());

if (activePreset)
{
Expand Down Expand Up @@ -266,6 +268,16 @@ void video_properties_s::to_capture_device_properties(
const std::string &nameSpace
)
{
#if defined(CAPTURE_BACKEND_VISION_V4L) || defined(CAPTURE_BACKEND_GENERIC_V4L)
// If the namespace is empty, the values would modify the current capture
// parameters. But if there's no signal, the parameters won't be available
// for modification via V4L and we'd be wasting time trying to change them.
if (!kc_has_signal() && nameSpace.empty())
{
return;
}
#endif

for (const auto &[propName, value]: properties)
{
kc_set_device_property((propName + nameSpace), value);
Expand Down
2 changes: 1 addition & 1 deletion src/capture/vision_v4l/ic_v4l_video_parameters.cpp
Expand Up @@ -29,7 +29,7 @@ bool ic_v4l_controls_c::set_value(const int newValue, const ic_v4l_controls_c::t
{
if (this->v4l_id(control) < 0)
{
DEBUG(("Asked to modify an unrecognized V4L control. Ignoring it."));
DEBUG(("Asked to modify a V4L control that isn't available. Ignoring this."));
return false;
}

Expand Down
15 changes: 15 additions & 0 deletions src/display/qt/widgets/VideoPresetList.cpp
Expand Up @@ -129,6 +129,21 @@ int VideoPresetList::find_preset_idx_in_list(const int presetId)
return -1;
}

int VideoPresetList::find_preset_idx_in_list(const QString &targetLabel)
{
for (int i = 0; i < this->count(); i++)
{
const auto *preset = kvideopreset_get_preset_ptr(this->itemData(i).toUInt());
const auto label = this->make_preset_item_label(preset);
if (label == targetLabel)
{
return i;
}
}

return -1;
}

void VideoPresetList::update_preset_item_label(const unsigned presetId)
{
const auto *const preset = kvideopreset_get_preset_ptr(presetId);
Expand Down
22 changes: 11 additions & 11 deletions src/display/qt/widgets/VideoPresetList.h
Expand Up @@ -22,8 +22,7 @@ class VideoPresetList : public QComboBox

~VideoPresetList(void);

void add_preset(const unsigned presetId,
const bool blockSignal = false);
void add_preset(const unsigned presetId, const bool blockSignal = false);

// Removes the given present from the list, and sets the list's index
// to the one given. If the given preset doesn't exist in the list, no
Expand All @@ -40,6 +39,15 @@ class VideoPresetList : public QComboBox
// no preset is selected.
video_preset_s* current_preset(void) const;

// Creates and returns a string to be displayed as the list item label for
// the given present should the preset be added.
QString make_preset_item_label(const video_preset_s *const preset);

// Returns the list index of the given preset; or -1 if the list doesn't
// contain the preset.
int find_preset_idx_in_list(const int presetId);
int find_preset_idx_in_list(const QString &targetLabel);

signals:
void preset_added(const unsigned presetId);

Expand All @@ -51,17 +59,9 @@ class VideoPresetList : public QComboBox

void preset_selected(const unsigned presetId);

void preset_label_changed(const unsigned presetId,
const QString &newLabel);
void preset_label_changed(const unsigned presetId, const QString &newLabel);

private:
// Returns the list index of the given preset; or -1 if the list doesn't
// contain the preset.
int find_preset_idx_in_list(const int presetId);

// Creates and returns a string to be displayed as the list item label for
// the given present should the preset be added.
QString make_preset_item_label(const video_preset_s *const preset);
};

#endif
9 changes: 5 additions & 4 deletions src/display/qt/windows/ControlPanel/VideoPresets.cpp
Expand Up @@ -167,7 +167,7 @@ control_panel::VideoPresets::VideoPresets(QWidget *parent) :

connect(ui->comboBox_presetList, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](const int index)
{
kpers_set_value(INI_GROUP_VIDEO_PRESETS, "SelectedPresetIndex", std::max(index, 0));
kpers_set_value(INI_GROUP_VIDEO_PRESETS, "SelectedPreset", ui->comboBox_presetList->make_preset_item_label(ui->comboBox_presetList->current_preset()));
});

connect(ui->comboBox_presetList, &VideoPresetList::preset_selected, this, [this]
Expand Down Expand Up @@ -532,7 +532,7 @@ void control_panel::VideoPresets::update_active_preset_indicator(void)

void control_panel::VideoPresets::assign_presets(const std::vector<video_preset_s*> &presets)
{
const unsigned idx = kpers_value_of(INI_GROUP_VIDEO_PRESETS, "SelectedPresetIndex", 0).toUInt();
const QString selectedLabel = kpers_value_of(INI_GROUP_VIDEO_PRESETS, "SelectedPreset", "").toString();

ui->comboBox_presetList->clear();

Expand All @@ -544,8 +544,9 @@ void control_panel::VideoPresets::assign_presets(const std::vector<video_preset_
/// TODO: It would be better to sort the items by (numeric) resolution.
ui->comboBox_presetList->sort_alphabetically();

ui->comboBox_presetList->setCurrentIndex(-1);
ui->comboBox_presetList->setCurrentIndex(std::min(int(idx), (ui->comboBox_presetList->count() - 1)));
const int idx = ui->comboBox_presetList->find_preset_idx_in_list(selectedLabel);
ui->comboBox_presetList->setCurrentIndex(std::max(idx, 0));
emit ui->comboBox_presetList->preset_selected(ui->comboBox_presetList->current_preset()->id);

return;
}
Expand Down

0 comments on commit 1898f8a

Please sign in to comment.