File indexing completed on 2024-05-12 04:38:18

0001 /*
0002     SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
0003     SPDX-FileCopyrightText: 2017 Friedrich W. H. Kossebau <kossebau@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "environmentprofilelistmodel.h"
0009 
0010 #include <KLocalizedString>
0011 
0012 using namespace KDevelop;
0013 
0014 EnvironmentProfileListModel::EnvironmentProfileListModel(QObject* parent)
0015     : QAbstractItemModel(parent)
0016 {
0017 }
0018 
0019 int EnvironmentProfileListModel::rowCount(const QModelIndex& parent) const
0020 {
0021     if (parent.isValid()) {
0022         return 0;
0023     }
0024 
0025     return profileNames().count();
0026 }
0027 
0028 int EnvironmentProfileListModel::columnCount(const QModelIndex& parent) const
0029 {
0030     if (parent.isValid()) {
0031         return 0;
0032     }
0033 
0034     return 1;
0035 }
0036 
0037 QVariant EnvironmentProfileListModel::data(const QModelIndex& index, int role) const
0038 {
0039     if (!index.isValid() ||
0040         index.row() < 0 || index.row() >= rowCount() ||
0041         index.column() != 0) {
0042         return {};
0043     }
0044 
0045     if (role == Qt::DisplayRole) {
0046         auto profileName = profileNames().at(index.row());
0047         if (profileName == defaultProfileName()) {
0048             profileName = i18n("%1 (default profile)", profileName);
0049         }
0050         return profileName;
0051     }
0052 
0053     return {};
0054 }
0055 
0056 QModelIndex EnvironmentProfileListModel::index(int row, int column, const QModelIndex& parent) const
0057 {
0058     if (parent.isValid()) {
0059         return {};
0060     }
0061     return createIndex(row, column);
0062 }
0063 
0064 QModelIndex EnvironmentProfileListModel::parent(const QModelIndex& index) const
0065 {
0066     Q_UNUSED(index);
0067     return {};
0068 }
0069 
0070 int EnvironmentProfileListModel::defaultProfileIndex() const
0071 {
0072     return profileNames().indexOf(defaultProfileName());
0073 }
0074 
0075 QString EnvironmentProfileListModel::profileName(int profileIndex) const
0076 {
0077     return profileNames().value(profileIndex);
0078 }
0079 
0080 int EnvironmentProfileListModel::profileIndex(const QString& profileName) const
0081 {
0082     return profileNames().indexOf(profileName);
0083 }
0084 
0085 bool EnvironmentProfileListModel::hasProfile(const QString& profileName) const
0086 {
0087     return profileNames().contains(profileName);
0088 }
0089 
0090 QMap<QString, QString>& EnvironmentProfileListModel::variables(const QString& profileName)
0091 {
0092     return EnvironmentProfileList::variables(profileName);
0093 }
0094 
0095 int EnvironmentProfileListModel::addProfile(const QString& newProfileName)
0096 {
0097     const auto profileNames = this->profileNames();
0098     if (newProfileName.isEmpty() ||
0099         profileNames.contains(newProfileName)) {
0100         return -1;
0101     }
0102 
0103     // estimate insert position by comparison as used by QMap for the keys
0104     int insertPos = 0;
0105     while (insertPos < profileNames.size() &&
0106            profileNames.at(insertPos) < newProfileName) {
0107         ++insertPos;
0108     }
0109     beginInsertRows(QModelIndex(), insertPos, insertPos);
0110 
0111     // trigger creation of new profile
0112     variables(newProfileName);
0113 
0114     endInsertRows();
0115     return insertPos;
0116 }
0117 
0118 void EnvironmentProfileListModel::removeProfile(int profileIndex)
0119 {
0120     const auto profileNames = this->profileNames();
0121     if (profileIndex < 0 || profileIndex >= profileNames.size()) {
0122         return;
0123     }
0124     const auto profileName = profileNames.at(profileIndex);
0125     if (defaultProfileName() == profileName) {
0126         return;
0127     }
0128 
0129     emit profileAboutToBeRemoved(profileName);
0130 
0131     beginRemoveRows(QModelIndex(), profileIndex, profileIndex);
0132 
0133     EnvironmentProfileList::removeProfile(profileName);
0134 
0135     endRemoveRows();
0136 }
0137 
0138 int EnvironmentProfileListModel::cloneProfile(const QString& newProfileName, const QString& sourceProfileName)
0139 {
0140     const auto profileNames = this->profileNames();
0141     if (newProfileName.isEmpty() ||
0142         profileNames.contains(newProfileName) ||
0143         !profileNames.contains(sourceProfileName)) {
0144         return -1;
0145     }
0146 
0147     // estimate insert position by comparison as used by QMap for the keys
0148     int insertPos = 0;
0149     while (insertPos < profileNames.size() &&
0150            profileNames.at(insertPos) < newProfileName) {
0151         ++insertPos;
0152     }
0153 
0154     beginInsertRows(QModelIndex(), insertPos, insertPos);
0155 
0156     variables(newProfileName) = variables(sourceProfileName);
0157 
0158     endInsertRows();
0159 
0160     return insertPos;
0161 }
0162 
0163 void EnvironmentProfileListModel::setDefaultProfile(int profileIndex)
0164 {
0165     const auto profileNames = this->profileNames();
0166     const auto oldDefaultProfileName = defaultProfileName();
0167     const int oldDefaultProfileIndex = profileNames.indexOf(oldDefaultProfileName);
0168 
0169     if (profileIndex < 0 || profileIndex >= profileNames.size() ||
0170         oldDefaultProfileIndex == profileIndex) {
0171         return;
0172     }
0173 
0174     const auto oldIndex = index(oldDefaultProfileIndex, 0);
0175 
0176     const auto profileName = profileNames.at(profileIndex);
0177     EnvironmentProfileList::setDefaultProfile(profileName);
0178 
0179     const int newDefaultProfileIndex = profileNames.indexOf(profileName);
0180     const auto newIndex = index(newDefaultProfileIndex, 0);
0181 
0182     emit dataChanged(oldIndex, oldIndex);
0183     emit dataChanged(newIndex, newIndex);
0184     emit defaultProfileChanged(newDefaultProfileIndex);
0185 }
0186 
0187 void EnvironmentProfileListModel::loadFromConfig(KConfig* config)
0188 {
0189     beginResetModel();
0190 
0191     loadSettings(config);
0192 
0193     endResetModel();
0194 }
0195 
0196 
0197 void EnvironmentProfileListModel::saveToConfig(KConfig* config)
0198 {
0199     saveSettings(config);
0200 }
0201 
0202 #include "moc_environmentprofilelistmodel.cpp"