kcm-openrc/openrc_tree_item.cpp
Juraj Oravec c95cef0bbd
Port to TreeView with custom model
Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
2025-05-30 22:58:09 +02:00

69 lines
2.1 KiB
C++

/* ============================================================
* KCMOpenRC - OpenRC Service Manager
* Copyright (C) 2025 Juraj Oravec <jurajoravec@mailo.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "openrc_tree_item.h"
OpenRCTreeItem::OpenRCTreeItem(QVariantList data, OpenRCTreeItem *parent)
: m_itemData(std::move(data))
, m_parentItem(parent)
{}
void OpenRCTreeItem::appendChild(std::unique_ptr<OpenRCTreeItem> &&child)
{
m_childItems.push_back(std::move(child));
}
OpenRCTreeItem *OpenRCTreeItem::child(int row)
{
return row >= 0 && row < childCount() ? m_childItems.at(row).get() : nullptr;
}
int OpenRCTreeItem::childCount() const
{
return int(m_childItems.size());
}
int OpenRCTreeItem::columnCount() const
{
return int(m_itemData.count());
}
QVariant OpenRCTreeItem::data(int column) const
{
return m_itemData.value(column);
}
OpenRCTreeItem *OpenRCTreeItem::parentItem()
{
return m_parentItem;
}
int OpenRCTreeItem::row() const
{
if (m_parentItem == nullptr)
return 0;
const auto it = std::find_if(m_parentItem->m_childItems.cbegin(), m_parentItem->m_childItems.cend(),
[this](const std::unique_ptr<OpenRCTreeItem> &treeItem) {
return treeItem.get() == this;
});
if (it != m_parentItem->m_childItems.cend())
return std::distance(m_parentItem->m_childItems.cbegin(), it);
Q_ASSERT(false); // should not happen
return -1;
}