File indexing completed on 2024-05-05 04:38:44

0001 /*
0002     SPDX-FileCopyrightText: 2013 Ivan Shapovalov <intelfx100@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "environmentselectionmodel.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 namespace {
0012 
0013 QStringList entriesFromEnv(const KDevelop::EnvironmentProfileList& env)
0014 {
0015     // We add an empty (i. e. default profile) entry to the front of the model's list.
0016     return QStringList(QString()) + env.profileNames();
0017 }
0018 
0019 }
0020 
0021 namespace KDevelop {
0022 
0023 EnvironmentSelectionModel::EnvironmentSelectionModel(QObject* parent) :
0024     QStringListModel(parent)
0025     , m_env(KSharedConfig::openConfig())
0026 {
0027     const QStringList entries = entriesFromEnv(m_env);
0028     setStringList(entries);
0029     m_profilesLookupTable = QSet<QString>(entries.begin(), entries.end());
0030 }
0031 
0032 QVariant EnvironmentSelectionModel::headerData(int section, Qt::Orientation orientation, int role) const
0033 {
0034     if (section != 0 ||
0035         orientation != Qt::Horizontal ||
0036         role != Qt::DisplayRole) {
0037         return QVariant();
0038     }
0039 
0040     return i18nc("@title:column", "Profile");
0041 }
0042 
0043 QVariant EnvironmentSelectionModel::data(const QModelIndex& index, int role) const
0044 {
0045     QVariant nativeData = QStringListModel::data(index, Qt::DisplayRole);
0046     QString profileName = nativeData.toString();
0047 
0048     switch (role) {
0049     case Qt::DisplayRole:
0050         if (profileName.isEmpty()) {
0051             return i18nc("@item:inlistbox", "Use default profile (currently: %1)", m_env.defaultProfileName());
0052         }
0053 
0054         if (!m_profilesLookupTable.contains(profileName)) {
0055             return i18nc("@item:inlistbox", "%1 (does not exist)", profileName);
0056         }
0057 
0058         return nativeData;
0059 
0060     case EffectiveNameRole:
0061         if (profileName.isEmpty()) {
0062             return m_env.defaultProfileName();
0063         }
0064 
0065         return nativeData;
0066 
0067     default:
0068         return QStringListModel::data(index, role);
0069     }
0070 }
0071 
0072 bool EnvironmentSelectionModel::setData(const QModelIndex& /*index*/, const QVariant& /*value*/, int /*role*/)
0073 {
0074     return false;
0075 }
0076 
0077 EnvironmentProfileList EnvironmentSelectionModel::environmentProfiles() const
0078 {
0079     return m_env;
0080 }
0081 
0082 void EnvironmentSelectionModel::reload()
0083 {
0084     m_env = EnvironmentProfileList(KSharedConfig::openConfig());
0085 
0086     const QStringList entries = entriesFromEnv(m_env);
0087     setStringList(entries);
0088     m_profilesLookupTable = QSet<QString>(entries.begin(), entries.end());
0089 }
0090 
0091 QString EnvironmentSelectionModel::reloadSelectedItem(const QString& currentProfile)
0092 {
0093     if (m_profilesLookupTable.contains(currentProfile)) {
0094         return currentProfile;
0095     } else {
0096         return QString();
0097     }
0098 }
0099 
0100 } // namespace KDevelop
0101 
0102 #include "moc_environmentselectionmodel.cpp"