File indexing completed on 2024-04-28 16:42:55

0001 // SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "savedcommandsmodel.h"
0005 #include "terminalsettings.h"
0006 
0007 SavedCommandsModel::SavedCommandsModel(QObject *parent)
0008     : QAbstractListModel(parent)
0009     , m_actions(TerminalSettings::self()->actions())
0010 {
0011 }
0012 
0013 SavedCommandsModel::~SavedCommandsModel()
0014 {}
0015 
0016 QVariant SavedCommandsModel::data(const QModelIndex &index, int role) const
0017 {
0018     Q_UNUSED(role)
0019 
0020     if (!index.isValid() || index.row() < 0 || index.row() >= m_actions.count()) {
0021         return false;
0022     }
0023 
0024     return m_actions.at(index.row());
0025 }
0026 
0027 void SavedCommandsModel::save()
0028 {
0029     TerminalSettings::self()->setActions(m_actions);
0030 }
0031 
0032 void SavedCommandsModel::addAction(QString action)
0033 {
0034     beginInsertRows(QModelIndex(), m_actions.size(), m_actions.size());
0035     m_actions.push_back(action);
0036     endInsertRows();
0037     
0038     TerminalSettings::self()->save();
0039 }
0040 
0041 bool SavedCommandsModel::removeRow(int row)
0042 {
0043     if (row < 0 || row >= m_actions.size()) {
0044         return false;
0045     }
0046 
0047     beginRemoveRows(QModelIndex(), row, row);
0048     m_actions.erase(m_actions.begin() + row);
0049     endRemoveRows();
0050 
0051     TerminalSettings::self()->save();
0052     return true;
0053 }