File indexing completed on 2024-05-12 04:38:19
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 "environmentprofilemodel.h" 0009 0010 #include "environmentprofilelistmodel.h" 0011 0012 #include <KLocalizedString> 0013 0014 #include <QVariant> 0015 #include <QModelIndex> 0016 0017 using namespace KDevelop; 0018 0019 EnvironmentProfileModel::EnvironmentProfileModel(EnvironmentProfileListModel* profileListModel, 0020 QObject* parent) 0021 : QAbstractTableModel(parent) 0022 , m_profileListModel(profileListModel) 0023 { 0024 connect(m_profileListModel, &EnvironmentProfileListModel::profileAboutToBeRemoved, 0025 this, &EnvironmentProfileModel::onProfileAboutToBeRemoved); 0026 } 0027 0028 int EnvironmentProfileModel::rowCount(const QModelIndex& parent) const 0029 { 0030 if (parent.isValid()) { 0031 return 0; 0032 } 0033 0034 return m_varsByIndex.count(); 0035 } 0036 0037 int EnvironmentProfileModel::columnCount(const QModelIndex& parent) const 0038 { 0039 if (parent.isValid()) { 0040 return 0; 0041 } 0042 0043 return 2; 0044 } 0045 0046 Qt::ItemFlags EnvironmentProfileModel::flags(const QModelIndex& index) const 0047 { 0048 if (!index.isValid()) { 0049 return Qt::NoItemFlags; 0050 } 0051 0052 return (Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled); 0053 } 0054 0055 QVariant EnvironmentProfileModel::data(const QModelIndex& index, int role) const 0056 { 0057 if (!index.isValid() || 0058 index.row() < 0 || index.row() >= rowCount() || 0059 index.column() < 0 || index.column() >= columnCount(QModelIndex()) || 0060 m_currentProfileName.isEmpty()) { 0061 return {}; 0062 } 0063 0064 const auto variable = m_varsByIndex[index.row()]; 0065 if (role == VariableRole) { 0066 return variable; 0067 } 0068 if (role == ValueRole) { 0069 const auto& variables = m_profileListModel->variables(m_currentProfileName); 0070 return variables.value(variable); 0071 } 0072 if (role == Qt::DisplayRole || role == Qt::EditRole) { 0073 if (index.column() == VariableColumn) { 0074 return variable; 0075 } 0076 const auto& variables = m_profileListModel->variables(m_currentProfileName); 0077 return variables.value(variable); 0078 } 0079 return {}; 0080 } 0081 0082 QVariant EnvironmentProfileModel::headerData(int section, Qt::Orientation orientation, int role) const 0083 { 0084 if (section < 0 || section >= columnCount(QModelIndex()) || 0085 orientation != Qt::Horizontal || 0086 role != Qt::DisplayRole) { 0087 return {}; 0088 } 0089 0090 if (section == VariableColumn) { 0091 return i18nc("@title:column", "Variable"); 0092 } 0093 return i18nc("@title:column", "Value"); 0094 } 0095 0096 bool EnvironmentProfileModel::setData(const QModelIndex& index, const QVariant& data, int role) 0097 { 0098 if (!index.isValid() || 0099 index.row() < 0 || index.row() >= rowCount() || 0100 index.column() < 0 || index.column() >= columnCount(QModelIndex()) || 0101 m_currentProfileName.isEmpty()) { 0102 return false; 0103 } 0104 0105 if (role == Qt::EditRole) { 0106 auto& variables = m_profileListModel->variables(m_currentProfileName); 0107 if (index.column() == VariableColumn) { 0108 const QString oldVariable = m_varsByIndex[index.row()]; 0109 const QString value = variables.take(oldVariable); 0110 const QString newVariable = data.toString(); 0111 variables.insert(newVariable, value); 0112 m_varsByIndex[index.row()] = newVariable; 0113 } else { 0114 variables.insert(m_varsByIndex[index.row()], data.toString()); 0115 } 0116 emit dataChanged(index, index); 0117 } 0118 return true; 0119 } 0120 0121 void EnvironmentProfileModel::addVariable(const QString& variableName, const QString& value) 0122 { 0123 if (m_currentProfileName.isEmpty()) { 0124 return; 0125 } 0126 0127 const int pos = m_varsByIndex.indexOf(variableName); 0128 if (pos != -1) { 0129 return; // No duplicates, first value 0130 } 0131 0132 auto& variables = m_profileListModel->variables(m_currentProfileName); 0133 0134 const int insertPos = rowCount(); 0135 beginInsertRows(QModelIndex(), insertPos, insertPos); 0136 m_varsByIndex << variableName; 0137 variables.insert(variableName, value); 0138 endInsertRows(); 0139 } 0140 0141 void EnvironmentProfileModel::removeVariables(const QStringList& variableNames) 0142 { 0143 for (const auto& variableName : variableNames) { 0144 removeVariable(variableName); 0145 } 0146 } 0147 0148 void EnvironmentProfileModel::removeVariable(const QString& variableName) 0149 { 0150 if (m_currentProfileName.isEmpty()) { 0151 return; 0152 } 0153 0154 const int pos = m_varsByIndex.indexOf(variableName); 0155 if (pos == -1) { 0156 return; 0157 } 0158 0159 auto& variables = m_profileListModel->variables(m_currentProfileName); 0160 0161 beginRemoveRows(QModelIndex(), pos, pos); 0162 m_varsByIndex.removeAt(pos); 0163 variables.remove(variableName); 0164 endRemoveRows(); 0165 } 0166 0167 void EnvironmentProfileModel::setCurrentProfile(const QString& profileName) 0168 { 0169 if (profileName == m_currentProfileName) { 0170 return; 0171 } 0172 0173 beginResetModel(); 0174 m_currentProfileName = profileName; 0175 m_varsByIndex.clear(); 0176 0177 if (!m_currentProfileName.isEmpty()) { 0178 const auto& variables = m_profileListModel->variables(m_currentProfileName); 0179 0180 m_varsByIndex.reserve(variables.size()); 0181 const auto endIt = variables.constEnd(); 0182 for (auto it = variables.constBegin(); it != endIt; ++it) { 0183 m_varsByIndex << it.key(); 0184 } 0185 } 0186 endResetModel(); 0187 } 0188 0189 void EnvironmentProfileModel::setVariablesFromString(const QString& plainText) 0190 { 0191 if (m_currentProfileName.isEmpty()) { 0192 return; 0193 } 0194 0195 beginResetModel(); 0196 0197 auto& variables = m_profileListModel->variables(m_currentProfileName); 0198 variables.clear(); 0199 m_varsByIndex.clear(); 0200 0201 const auto lines = plainText.splitRef(QLatin1Char('\n'), Qt::SkipEmptyParts); 0202 for (const auto& line : lines) { 0203 const int pos = line.indexOf(QLatin1Char('=')); 0204 // has a = and at least 1 char 0205 if (pos < 0) { 0206 continue; 0207 } 0208 const QString variableName = line.left(pos).trimmed().toString(); 0209 if (variableName.isEmpty()) { 0210 continue; 0211 } 0212 const QString value = line.mid(pos + 1).trimmed().toString(); 0213 m_varsByIndex << variableName; 0214 variables.insert(variableName, value); 0215 } 0216 0217 endResetModel(); 0218 } 0219 0220 void EnvironmentProfileModel::onProfileAboutToBeRemoved(const QString& profileName) 0221 { 0222 if (m_currentProfileName == profileName) { 0223 setCurrentProfile(QString()); 0224 } 0225 } 0226 0227 #include "moc_environmentprofilemodel.cpp"