From d0881d6edaeb47cefc8e74133a236c89003454da Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Mon, 10 Aug 2020 12:29:08 -0400 Subject: [PATCH 1/2] Make classification scores "editable" Add ability to edit classification scores. Similar to setting the type, doing so will nuke any other classifiers other that might be present. --- sealtk/noaa/gui/CMakeLists.txt | 2 + sealtk/noaa/gui/TrackScoreDelegate.cpp | 63 ++++++++++++++++++++++++++ sealtk/noaa/gui/TrackScoreDelegate.hpp | 43 ++++++++++++++++++ sealtk/noaa/gui/Window.cpp | 25 ++++++++-- 4 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 sealtk/noaa/gui/TrackScoreDelegate.cpp create mode 100644 sealtk/noaa/gui/TrackScoreDelegate.hpp diff --git a/sealtk/noaa/gui/CMakeLists.txt b/sealtk/noaa/gui/CMakeLists.txt index 8e88d30..94c9da2 100644 --- a/sealtk/noaa/gui/CMakeLists.txt +++ b/sealtk/noaa/gui/CMakeLists.txt @@ -10,6 +10,7 @@ sealtk_add_library(sealtk::noaa_gui NotesDelegate.cpp Player.cpp Resources.cpp + TrackScoreDelegate.cpp TrackTypeDelegate.cpp Window.cpp SEALTKBranding.qrc @@ -19,6 +20,7 @@ sealtk_add_library(sealtk::noaa_gui NotesDelegate.hpp Player.hpp Resources.hpp + TrackScoreDelegate.hpp TrackTypeDelegate.hpp Window.hpp diff --git a/sealtk/noaa/gui/TrackScoreDelegate.cpp b/sealtk/noaa/gui/TrackScoreDelegate.cpp new file mode 100644 index 0000000..c49b083 --- /dev/null +++ b/sealtk/noaa/gui/TrackScoreDelegate.cpp @@ -0,0 +1,63 @@ +/* This file is part of SEAL-TK, and is distributed under the OSI-approved BSD + * 3-Clause License. See top-level LICENSE file or + * https://github.com/Kitware/seal-tk/blob/master/LICENSE for details. */ + +#include + +#include + +#include + +namespace sealtk +{ + +namespace noaa +{ + +namespace gui +{ + +// ---------------------------------------------------------------------------- +TrackScoreDelegate::TrackScoreDelegate(QObject* parent) + : qtDoubleSpinBoxDelegate{parent} +{ + this->setRange(0.0, 1.0); + this->setPrecision(5); +} + +// ---------------------------------------------------------------------------- +TrackScoreDelegate::~TrackScoreDelegate() +{ +} + +// ---------------------------------------------------------------------------- +void TrackScoreDelegate::setEditorData( + QWidget* editor, QModelIndex const& index) const +{ + if (auto* const model = index.model()) + { + auto const& data = model->data(index, core::ClassificationScoreRole); + auto* const spin = qobject_cast(editor); + spin->setValue(data.toDouble()); + } +} + +// ---------------------------------------------------------------------------- +void TrackScoreDelegate::setModelData( + QWidget* editor, QAbstractItemModel* model, QModelIndex const& index) const +{ + QDoubleSpinBox* spin = qobject_cast(editor); + spin->interpretText(); + + auto const& typeData = model->data(index, core::ClassificationTypeRole); + auto const& newScore = spin->value(); + + auto newClassification = QVariantHash{{typeData.toString(), newScore}}; + model->setData(index, newClassification, core::ClassificationRole); +} + +} // namespace gui + +} // namespace noaa + +} // namespace sealtk diff --git a/sealtk/noaa/gui/TrackScoreDelegate.hpp b/sealtk/noaa/gui/TrackScoreDelegate.hpp new file mode 100644 index 0000000..f9fcebe --- /dev/null +++ b/sealtk/noaa/gui/TrackScoreDelegate.hpp @@ -0,0 +1,43 @@ +/* This file is part of SEAL-TK, and is distributed under the OSI-approved BSD + * 3-Clause License. See top-level LICENSE file or + * https://github.com/Kitware/seal-tk/blob/master/LICENSE for details. */ + +#ifndef sealtk_noaa_gui_TrackScoreDelegate_hpp +#define sealtk_noaa_gui_TrackScoreDelegate_hpp + +#include + +#include + +namespace sealtk +{ + +namespace noaa +{ + +namespace gui +{ + +class SEALTK_NOAA_GUI_EXPORT TrackScoreDelegate + : public qtDoubleSpinBoxDelegate +{ + Q_OBJECT + +public: + explicit TrackScoreDelegate(QObject* parent = nullptr); + ~TrackScoreDelegate() override; + + void setEditorData( + QWidget* editor, QModelIndex const& index) const override; + void setModelData( + QWidget* editor, QAbstractItemModel* model, + QModelIndex const& index) const override; +}; + +} // namespace gui + +} // namespace noaa + +} // namespace sealtk + +#endif diff --git a/sealtk/noaa/gui/Window.cpp b/sealtk/noaa/gui/Window.cpp index 2e90c77..3212fd8 100644 --- a/sealtk/noaa/gui/Window.cpp +++ b/sealtk/noaa/gui/Window.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -119,12 +120,26 @@ class TrackRepresentation : public sg::AbstractItemRepresentation auto const defaultFlags = this->sg::AbstractItemRepresentation::flags(index); - if (index.column() == 3 || index.column() == 5) + switch (index.column()) { - return defaultFlags | Qt::ItemIsEditable; - } + case 3: // Type + case 5: // Notes + return defaultFlags | Qt::ItemIsEditable; - return defaultFlags; + case 4: // Score + { + auto const& data = + index.model()->data(index, sc::ClassificationRole); + if (!data.toHash().isEmpty()) + { + return defaultFlags | Qt::ItemIsEditable; + } + return defaultFlags; + } + + default: + return defaultFlags; + } } }; @@ -187,6 +202,7 @@ class WindowPrivate sc::ScalarFilterModel trackModelFilter; TrackRepresentation trackRepresentation; TrackTypeDelegate typeDelegate; + TrackScoreDelegate scoreDelegate; NotesDelegate notesDelegate; sg::ClassificationSummaryRepresentation statisticsRepresentation; @@ -258,6 +274,7 @@ Window::Window(QWidget* parent) d->trackRepresentation.setSourceModel(&d->trackModelFilter); d->ui.tracks->setModel(&d->trackRepresentation); d->ui.tracks->setItemDelegateForColumn(3, &d->typeDelegate); + d->ui.tracks->setItemDelegateForColumn(4, &d->scoreDelegate); d->ui.tracks->setItemDelegateForColumn(5, &d->notesDelegate); connect(d->scoreFilter, &sg::FilterWidget::filterMinimumChanged, From 28e3b482f2f27e4e93e30d769606d1f86937b35c Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Fri, 18 Sep 2020 15:02:51 -0400 Subject: [PATCH 2/2] Try to improve score delegate Tweak TrackScoreDelegate to try to improve its appearance and usability, by ensuring it does not have a frame, changing it to be right-aligned to match the non-editing alignment, and ensuring it does not have a frame (which is not desired when being used as a view item edit widget). --- sealtk/noaa/gui/TrackScoreDelegate.cpp | 16 ++++++++++++++++ sealtk/noaa/gui/TrackScoreDelegate.hpp | 3 +++ 2 files changed, 19 insertions(+) diff --git a/sealtk/noaa/gui/TrackScoreDelegate.cpp b/sealtk/noaa/gui/TrackScoreDelegate.cpp index c49b083..92196fe 100644 --- a/sealtk/noaa/gui/TrackScoreDelegate.cpp +++ b/sealtk/noaa/gui/TrackScoreDelegate.cpp @@ -30,6 +30,22 @@ TrackScoreDelegate::~TrackScoreDelegate() { } +//----------------------------------------------------------------------------- +QWidget* TrackScoreDelegate::createEditor( + QWidget* parent, QStyleOptionViewItem const& item, + QModelIndex const& index) const +{ + auto* const editor = + static_cast( + qtDoubleSpinBoxDelegate::createEditor(parent, item, index)); + + editor->setFrame(false); + editor->setSingleStep(0.01); + editor->setAlignment(Qt::AlignRight | Qt::AlignVCenter); + + return editor; +} + // ---------------------------------------------------------------------------- void TrackScoreDelegate::setEditorData( QWidget* editor, QModelIndex const& index) const diff --git a/sealtk/noaa/gui/TrackScoreDelegate.hpp b/sealtk/noaa/gui/TrackScoreDelegate.hpp index f9fcebe..e77e466 100644 --- a/sealtk/noaa/gui/TrackScoreDelegate.hpp +++ b/sealtk/noaa/gui/TrackScoreDelegate.hpp @@ -27,6 +27,9 @@ class SEALTK_NOAA_GUI_EXPORT TrackScoreDelegate explicit TrackScoreDelegate(QObject* parent = nullptr); ~TrackScoreDelegate() override; + QWidget* createEditor( + QWidget* parent, QStyleOptionViewItem const& item, + QModelIndex const& index) const override; void setEditorData( QWidget* editor, QModelIndex const& index) const override; void setModelData(