File indexing completed on 2024-05-12 16:25:47

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "commandsmodel.h"
0008 
0009 CommandsModel::CommandsModel(QObject *parent)
0010     : QAbstractListModel(parent)
0011 {
0012 }
0013 
0014 CommandsModel::~CommandsModel() = default;
0015 
0016 int CommandsModel::rowCount(const QModelIndex &parent) const
0017 {
0018     if (parent.isValid()) {
0019         return 0; // flat model
0020     }
0021     return mCommands.count();
0022 }
0023 
0024 QVariant CommandsModel::data(const QModelIndex &index, int role) const
0025 {
0026     if (index.row() < 0 || index.row() >= mCommands.count()) {
0027         return {};
0028     }
0029     const Command &command = mCommands.at(index.row());
0030     switch (role) {
0031     case Description:
0032         return command.description();
0033     case Parameters:
0034         return command.params();
0035     case CompleterName:
0036         return command.commandName().mid(1);
0037     case TranslatedDescription:
0038         return command.translatedDescription();
0039     case TranslatedParams:
0040         return command.translatedParams();
0041     case CommandName:
0042     case Qt::DisplayRole: // for the completion popup (until we have a delegate)
0043         return command.commandName();
0044     case Permissions:
0045         return command.permissions();
0046     }
0047 
0048     return {};
0049 }
0050 
0051 Commands CommandsModel::commands() const
0052 {
0053     return mCommands;
0054 }
0055 
0056 void CommandsModel::clear()
0057 {
0058     if (!mCommands.isEmpty()) {
0059         beginResetModel();
0060         mCommands.clear();
0061         endResetModel();
0062     }
0063 }
0064 
0065 void CommandsModel::setCommands(const Commands &commands)
0066 {
0067     clear();
0068     if (!commands.isEmpty()) {
0069         beginInsertRows(QModelIndex(), 0, commands.count() - 1);
0070         mCommands = commands;
0071         endInsertRows();
0072     }
0073 }
0074 
0075 #include "moc_commandsmodel.cpp"