Use unique pointers for space child items
This commit is contained in:
@@ -131,18 +131,18 @@ void SpaceChildrenModel::insertChildren(std::vector<Quotient::GetSpaceHierarchyJ
|
|||||||
insertChildren(job->rooms(), index(insertRow, 0, parent));
|
insertChildren(job->rooms(), index(insertRow, 0, parent));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
parentItem->insertChild(new SpaceTreeItem(dynamic_cast<NeoChatConnection *>(m_space->connection()),
|
parentItem->insertChild(std::make_unique<SpaceTreeItem>(dynamic_cast<NeoChatConnection *>(m_space->connection()),
|
||||||
parentItem,
|
parentItem,
|
||||||
children[i].roomId,
|
children[i].roomId,
|
||||||
children[i].name,
|
children[i].name,
|
||||||
children[i].canonicalAlias,
|
children[i].canonicalAlias,
|
||||||
children[i].topic,
|
children[i].topic,
|
||||||
children[i].numJoinedMembers,
|
children[i].numJoinedMembers,
|
||||||
children[i].avatarUrl,
|
children[i].avatarUrl,
|
||||||
children[i].guestCanJoin,
|
children[i].guestCanJoin,
|
||||||
children[i].worldReadable,
|
children[i].worldReadable,
|
||||||
children[i].roomType == QLatin1String("m.space"),
|
children[i].roomType == QLatin1String("m.space"),
|
||||||
std::move(children[i].childrenState)));
|
std::move(children[i].childrenState)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
|
|||||||
@@ -32,30 +32,22 @@ SpaceTreeItem::SpaceTreeItem(NeoChatConnection *connection,
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SpaceTreeItem::~SpaceTreeItem()
|
|
||||||
{
|
|
||||||
qDeleteAll(m_children);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SpaceTreeItem::operator==(const SpaceTreeItem &other) const
|
bool SpaceTreeItem::operator==(const SpaceTreeItem &other) const
|
||||||
{
|
{
|
||||||
return m_id == other.id();
|
return m_id == other.id();
|
||||||
}
|
}
|
||||||
|
|
||||||
SpaceTreeItem *SpaceTreeItem::child(int number)
|
SpaceTreeItem *SpaceTreeItem::child(int row)
|
||||||
{
|
{
|
||||||
if (number < 0 || number >= m_children.size()) {
|
return row >= 0 && row < childCount() ? m_children.at(row).get() : nullptr;
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return m_children[number];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int SpaceTreeItem::childCount() const
|
int SpaceTreeItem::childCount() const
|
||||||
{
|
{
|
||||||
return m_children.count();
|
return int(m_children.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SpaceTreeItem::insertChild(SpaceTreeItem *newChild)
|
bool SpaceTreeItem::insertChild(std::unique_ptr<SpaceTreeItem> newChild)
|
||||||
{
|
{
|
||||||
if (newChild == nullptr) {
|
if (newChild == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
@@ -63,30 +55,39 @@ bool SpaceTreeItem::insertChild(SpaceTreeItem *newChild)
|
|||||||
|
|
||||||
for (auto it = m_children.begin(), end = m_children.end(); it != end; ++it) {
|
for (auto it = m_children.begin(), end = m_children.end(); it != end; ++it) {
|
||||||
if (*it == newChild) {
|
if (*it == newChild) {
|
||||||
*it = newChild;
|
*it = std::move(newChild);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_children.append(newChild);
|
m_children.push_back(std::move(newChild));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SpaceTreeItem::removeChild(int row)
|
bool SpaceTreeItem::removeChild(int row)
|
||||||
{
|
{
|
||||||
if (row < 0 || row >= m_children.size()) {
|
if (row < 0 || row >= childCount()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
delete m_children.takeAt(row);
|
m_children.erase(m_children.begin() + row);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SpaceTreeItem::row() const
|
int SpaceTreeItem::row() const
|
||||||
{
|
{
|
||||||
if (m_parentItem) {
|
if (m_parentItem == nullptr) {
|
||||||
return m_parentItem->m_children.indexOf(const_cast<SpaceTreeItem *>(this));
|
return 0;
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
|
const auto it = std::find_if(m_parentItem->m_children.cbegin(), m_parentItem->m_children.cend(), [this](const std::unique_ptr<SpaceTreeItem> &treeItem) {
|
||||||
|
return treeItem.get() == this;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (it != m_parentItem->m_children.cend()) {
|
||||||
|
return std::distance(m_parentItem->m_children.cbegin(), it);
|
||||||
|
}
|
||||||
|
Q_ASSERT(false); // should not happen
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
SpaceTreeItem *SpaceTreeItem::parentItem() const
|
SpaceTreeItem *SpaceTreeItem::parentItem() const
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ public:
|
|||||||
bool worldReadable = {},
|
bool worldReadable = {},
|
||||||
bool isSpace = {},
|
bool isSpace = {},
|
||||||
Quotient::StateEvents childStates = {});
|
Quotient::StateEvents childStates = {});
|
||||||
~SpaceTreeItem();
|
|
||||||
|
|
||||||
bool operator==(const SpaceTreeItem &other) const;
|
bool operator==(const SpaceTreeItem &other) const;
|
||||||
|
|
||||||
@@ -42,7 +41,7 @@ public:
|
|||||||
*
|
*
|
||||||
* Nullptr is returned if there is no child at the given row number.
|
* Nullptr is returned if there is no child at the given row number.
|
||||||
*/
|
*/
|
||||||
SpaceTreeItem *child(int number);
|
SpaceTreeItem *child(int row);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The number of children this item has.
|
* @brief The number of children this item has.
|
||||||
@@ -52,7 +51,7 @@ public:
|
|||||||
/**
|
/**
|
||||||
* @brief Insert the given child.
|
* @brief Insert the given child.
|
||||||
*/
|
*/
|
||||||
bool insertChild(SpaceTreeItem *newChild);
|
bool insertChild(std::unique_ptr<SpaceTreeItem> newChild);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Remove the child at the given row number.
|
* @brief Remove the child at the given row number.
|
||||||
@@ -151,7 +150,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
NeoChatConnection *m_connection;
|
NeoChatConnection *m_connection;
|
||||||
QList<SpaceTreeItem *> m_children;
|
std::vector<std::unique_ptr<SpaceTreeItem>> m_children;
|
||||||
SpaceTreeItem *m_parentItem;
|
SpaceTreeItem *m_parentItem;
|
||||||
|
|
||||||
QString m_id;
|
QString m_id;
|
||||||
|
|||||||
Reference in New Issue
Block a user