DataSourceItem.cpp
86 lines
| 2.2 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r35 | #include <DataSource/DataSourceItem.h> | ||
Alexandre Leroux
|
r135 | #include <DataSource/DataSourceItemAction.h> | ||
Alexandre Leroux
|
r35 | |||
#include <QVector> | ||||
Alexandre Leroux
|
r137 | namespace { | ||
/// Index of the 'name' value in the item | ||||
const auto NAME_INDEX = 0; | ||||
} // namespace | ||||
Alexandre Leroux
|
r35 | struct DataSourceItem::DataSourceItemPrivate { | ||
Alexandre Leroux
|
r78 | explicit DataSourceItemPrivate(DataSourceItemType type, QVector<QVariant> data) | ||
Alexandre Leroux
|
r135 | : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} | ||
Alexandre Leroux
|
r35 | { | ||
} | ||||
DataSourceItem *m_Parent; | ||||
std::vector<std::unique_ptr<DataSourceItem> > m_Children; | ||||
Alexandre Leroux
|
r78 | DataSourceItemType m_Type; | ||
Alexandre Leroux
|
r35 | QVector<QVariant> m_Data; | ||
Alexandre Leroux
|
r135 | std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions; | ||
Alexandre Leroux
|
r35 | }; | ||
Alexandre Leroux
|
r78 | DataSourceItem::DataSourceItem(DataSourceItemType type, QVector<QVariant> data) | ||
: impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))} | ||||
Alexandre Leroux
|
r35 | { | ||
} | ||||
Alexandre Leroux
|
r135 | QVector<DataSourceItemAction *> DataSourceItem::actions() const noexcept | ||
{ | ||||
Alexandre Leroux
|
r159 | auto result = QVector<DataSourceItemAction *>{}; | ||
Alexandre Leroux
|
r135 | |||
std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions), | ||||
std::back_inserter(result), [](const auto &action) { return action.get(); }); | ||||
return result; | ||||
} | ||||
void DataSourceItem::addAction(std::unique_ptr<DataSourceItemAction> action) noexcept | ||||
{ | ||||
action->setDataSourceItem(this); | ||||
impl->m_Actions.push_back(std::move(action)); | ||||
} | ||||
Alexandre Leroux
|
r35 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept | ||
{ | ||||
child->impl->m_Parent = this; | ||||
impl->m_Children.push_back(std::move(child)); | ||||
} | ||||
DataSourceItem *DataSourceItem::child(int childIndex) const noexcept | ||||
{ | ||||
if (childIndex < 0 || childIndex >= childCount()) { | ||||
return nullptr; | ||||
} | ||||
else { | ||||
return impl->m_Children.at(childIndex).get(); | ||||
} | ||||
} | ||||
int DataSourceItem::childCount() const noexcept | ||||
{ | ||||
return impl->m_Children.size(); | ||||
} | ||||
QVariant DataSourceItem::data(int dataIndex) const noexcept | ||||
{ | ||||
return impl->m_Data.value(dataIndex); | ||||
} | ||||
Alexandre Leroux
|
r137 | QString DataSourceItem::name() const noexcept | ||
{ | ||||
return data(NAME_INDEX).toString(); | ||||
} | ||||
Alexandre Leroux
|
r35 | DataSourceItem *DataSourceItem::parentItem() const noexcept | ||
{ | ||||
return impl->m_Parent; | ||||
} | ||||
Alexandre Leroux
|
r78 | |||
DataSourceItemType DataSourceItem::type() const noexcept | ||||
{ | ||||
return impl->m_Type; | ||||
} | ||||