diff --git a/apps/opencs/view/widget/scenetoolrun.cpp b/apps/opencs/view/widget/scenetoolrun.cpp index f378725ae9..92f3193feb 100644 --- a/apps/opencs/view/widget/scenetoolrun.cpp +++ b/apps/opencs/view/widget/scenetoolrun.cpp @@ -1,6 +1,8 @@ #include "scenetoolrun.hpp" +#include + #include #include #include @@ -11,11 +13,11 @@ void CSVWidget::SceneToolRun::adjustToolTips() { QString toolTip = mToolTip; - if (mCurrentIndex==-1) + if (mSelected==mProfiles.end()) toolTip += "

No debug profile selected (function disabled)"; else { - toolTip += "

Debug profile: " + QString::fromUtf8 (mProfiles[mCurrentIndex].c_str()); + toolTip += "

Debug profile: " + QString::fromUtf8 (mSelected->c_str()); toolTip += "

(right click to switch to a different profile)"; } @@ -24,16 +26,19 @@ void CSVWidget::SceneToolRun::adjustToolTips() void CSVWidget::SceneToolRun::updateIcon() { - setIcon (QIcon (mCurrentIndex==-1 ? mIconDisabled : mIcon)); + setIcon (QIcon (mSelected==mProfiles.end() ? mIconDisabled : mIcon)); } void CSVWidget::SceneToolRun::updatePanel() { mTable->setRowCount (mProfiles.size()); - for (int i=0; i (mProfiles.size()); ++i) + int i = 0; + + for (std::set::const_iterator iter (mProfiles.begin()); iter!=mProfiles.end(); + ++iter, ++i) { - mTable->setItem (i, 0, new QTableWidgetItem (QString::fromUtf8 (mProfiles[i].c_str()))); + mTable->setItem (i, 0, new QTableWidgetItem (QString::fromUtf8 (iter->c_str()))); mTable->setItem (i, 1, new QTableWidgetItem ( QApplication::style()->standardIcon (QStyle::SP_TitleBarCloseButton), "")); @@ -42,8 +47,8 @@ void CSVWidget::SceneToolRun::updatePanel() CSVWidget::SceneToolRun::SceneToolRun (SceneToolbar *parent, const QString& toolTip, const QString& icon, const QString& iconDisabled, const std::vector& profiles) -: SceneTool (parent, Type_TopAction), mProfiles (profiles), - mCurrentIndex (profiles.empty() ? -1 : 0), mToolTip (toolTip), mIcon (icon), +: SceneTool (parent, Type_TopAction), mProfiles (profiles.begin(), profiles.end()), + mSelected (mProfiles.begin()), mToolTip (toolTip), mIcon (icon), mIconDisabled (iconDisabled) { updateIcon(); @@ -80,42 +85,67 @@ void CSVWidget::SceneToolRun::showPanel (const QPoint& position) void CSVWidget::SceneToolRun::activate() { - if (mCurrentIndex!=-1) - emit runRequest (mProfiles[mCurrentIndex]); + if (mSelected!=mProfiles.end()) + emit runRequest (*mSelected); } void CSVWidget::SceneToolRun::removeProfile (const std::string& profile) { - std::pair::iterator, std::vector::iterator> - result = std::equal_range (mProfiles.begin(), mProfiles.end(), profile); + std::set::iterator iter = mProfiles.find (profile); - if (result.first!=result.second) + if (iter!=mProfiles.end()) { - mProfiles.erase (result.first); + if (iter==mSelected) + { + if (iter!=mProfiles.begin()) + --mSelected; + else + ++mSelected; + } - if (mCurrentIndex>=static_cast (mProfiles.size())) - --mCurrentIndex; + mProfiles.erase (iter); - if (mCurrentIndex==-1) + if (mSelected==mProfiles.end()) updateIcon(); adjustToolTips(); } } +void CSVWidget::SceneToolRun::addProfile (const std::string& profile) +{ + std::set::iterator iter = mProfiles.find (profile); + + if (iter==mProfiles.end()) + { + mProfiles.insert (profile); + + if (mSelected==mProfiles.end()) + { + mSelected = mProfiles.begin(); + updateIcon(); + } + + adjustToolTips(); + } +} + void CSVWidget::SceneToolRun::clicked (const QModelIndex& index) { if (index.column()==0) { // select profile - mCurrentIndex = index.row(); + mSelected = mProfiles.begin(); + std::advance (mSelected, index.row()); mPanel->hide(); adjustToolTips(); } else if (index.column()==1) { // remove profile from list - removeProfile (mProfiles.at (index.row())); + std::set::iterator iter = mProfiles.begin(); + std::advance (iter, index.row()); + removeProfile (*iter); updatePanel(); } } \ No newline at end of file diff --git a/apps/opencs/view/widget/scenetoolrun.hpp b/apps/opencs/view/widget/scenetoolrun.hpp index 609ddcd34e..4396c22881 100644 --- a/apps/opencs/view/widget/scenetoolrun.hpp +++ b/apps/opencs/view/widget/scenetoolrun.hpp @@ -1,7 +1,7 @@ #ifndef CSV_WIDGET_SCENETOOLRUN_H #define CSV_WIDGET_SCENETOOLRUN_H -#include +#include #include #include "scenetool.hpp" @@ -16,8 +16,8 @@ namespace CSVWidget { Q_OBJECT - std::vector mProfiles; - int mCurrentIndex; + std::set mProfiles; + std::set::iterator mSelected; QString mToolTip; QString mIcon; QString mIconDisabled; @@ -45,6 +45,12 @@ namespace CSVWidget /// panel. void removeProfile (const std::string& profile); + /// \attention This function doe not add the profile to the profile selection + /// panel. This only happens when the panel is re-opened. + /// + /// \note Adding profiles that are already listed is a no-op. + void addProfile (const std::string& profile); + private slots: void clicked (const QModelIndex& index);