Skip to content

Commit

Permalink
Simplify GroupItem
Browse files Browse the repository at this point in the history
  • Loading branch information
gpospelov committed Feb 17, 2021
1 parent 18b80bb commit fc63eff
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
14 changes: 3 additions & 11 deletions source/libmvvm_model/mvvm/model/groupitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using namespace ModelView;
namespace {

//! Returns vector of model types for given vector of items.
std::vector<std::string> modelTypes(const std::vector<SessionItem*> items)
std::vector<std::string> modelTypes(const std::vector<SessionItem*>& items)
{
std::vector<std::string> result;
std::transform(items.begin(), items.end(), std::back_inserter(result),
Expand All @@ -29,9 +29,7 @@ std::vector<std::string> modelTypes(const std::vector<SessionItem*> items)

GroupItem::~GroupItem() = default;

GroupItem::GroupItem(model_type modelType)
: SessionItem(std::move(modelType))
, m_index_to_select(0)
GroupItem::GroupItem(model_type modelType) : SessionItem(std::move(modelType)), m_index_to_select(0)
{
registerTag(TagInfo::universalTag(T_GROUP_ITEMS), /*set_as_default*/ true);
setData(ComboProperty());
Expand All @@ -46,7 +44,7 @@ int GroupItem::currentIndex() const

const SessionItem* GroupItem::currentItem() const
{
return isValidIndex() ? getItem("", currentIndex()) : nullptr;
return currentIndex() != -1 ? getItem("", currentIndex()) : nullptr;
}

SessionItem* GroupItem::currentItem()
Expand All @@ -67,7 +65,6 @@ void GroupItem::setCurrentType(const std::string& model_type)
if (index == -1)
throw std::runtime_error("GroupItem::setCurrentType() -> Model type '" + model_type
+ "' doesn't belong to the group");

setCurrentIndex(index);
}

Expand All @@ -78,11 +75,6 @@ void GroupItem::setCurrentIndex(int index)
setData(combo, ItemDataRole::DATA);
}

bool GroupItem::isValidIndex() const
{
return currentIndex() != -1;
}

//! Updates internal data representing selection of items, and current selection.
//! To be called during GroupItem's construction.

Expand Down
8 changes: 2 additions & 6 deletions source/libmvvm_model/mvvm/model/groupitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class MVVM_MODEL_EXPORT GroupItem : public SessionItem {
protected:
GroupItem(model_type modelType);
void setCurrentIndex(int index);
bool isValidIndex() const;
template <typename T> void addToGroup(const std::string& text = {}, bool make_selected = false);
void updateCombo();

Expand All @@ -48,13 +47,10 @@ class MVVM_MODEL_EXPORT GroupItem : public SessionItem {
//! @param make_selected defines whether the item should be selected by default.
template <typename T> void GroupItem::addToGroup(const std::string& text, bool make_selected)
{
auto item = std::make_unique<T>();
std::string item_text = text.empty() ? item->modelType() : text;
m_item_text.push_back(item_text);
insertItem(std::move(item), TagRow::append(T_GROUP_ITEMS));
m_item_text.push_back(text.empty() ? T().modelType() : text);
insertItem<T>(TagRow::append(T_GROUP_ITEMS));
if (make_selected)
m_index_to_select = m_item_text.size() - 1;

updateCombo();
}

Expand Down

0 comments on commit fc63eff

Please sign in to comment.