configmodel.cpp 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "configmodel.h"

#include <utils/algorithm.h>
#include <utils/qtcassert.h>

31
#include <QCoreApplication>
32 33 34 35 36 37 38
#include <QFont>

namespace CMakeProjectManager {

static bool isTrue(const QString &value)
{
    const QString lower = value.toLower();
39 40
    return lower == QStringLiteral("true") || lower == QStringLiteral("on")
            || lower == QStringLiteral("1") || lower == QStringLiteral("yes");
41 42 43 44 45 46 47
}

ConfigModel::ConfigModel(QObject *parent) : QAbstractTableModel(parent)
{ }

int ConfigModel::rowCount(const QModelIndex &parent) const
{
48
    return parent.isValid() ? 0 : m_configuration.count();
49 50 51 52
}

int ConfigModel::columnCount(const QModelIndex &parent) const
{
53
    return parent.isValid() ? 0 : 3;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

Qt::ItemFlags ConfigModel::flags(const QModelIndex &index) const
{
    QTC_ASSERT(index.model() == this, return Qt::NoItemFlags);
    QTC_ASSERT(index.isValid(), return Qt::NoItemFlags);
    QTC_ASSERT(index.column() >= 0 && index.column() < columnCount(QModelIndex()), return Qt::NoItemFlags);
    QTC_ASSERT(index.row() >= 0 && index.row() < rowCount(QModelIndex()), return Qt::NoItemFlags);

    const InternalDataItem &item = itemAtRow(index.row());

    if (index.column() == 1) {
        if (item.type == DataItem::BOOLEAN)
            return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
        else
            return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable;
    } else {
71 72 73 74
        Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
        if (item.isUserNew)
            return flags |= Qt::ItemIsEditable;
        return flags;
75 76 77 78 79 80 81 82 83 84 85 86
    }
}

QVariant ConfigModel::data(const QModelIndex &index, int role) const
{
    QTC_ASSERT(index.model() == this, return QVariant());
    QTC_ASSERT(index.isValid(), return QVariant());
    QTC_ASSERT(index.row() >= 0 && index.row() < rowCount(QModelIndex()), return QVariant());
    QTC_ASSERT(index.column() >= 0 && index.column() < columnCount(QModelIndex()), return QVariant());

    const InternalDataItem &item = m_configuration[index.row()];

87 88 89 90 91 92 93 94 95
    if (index.column() < 2) {
        switch (role) {
        case ItemTypeRole:
            return item.type;
        case ItemValuesRole:
            return item.values;
        }
    }

96 97 98 99 100 101 102 103
    switch (index.column()) {
    case 0:
        switch (role) {
        case Qt::DisplayRole:
            return item.key.isEmpty() ? tr("<UNSET>") : item.key;
        case Qt::EditRole:
            return item.key;
        case Qt::ToolTipRole:
104
            return item.toolTip();
105 106 107 108
        case Qt::FontRole: {
            QFont font;
            font.setItalic(item.isCMakeChanged);
            font.setBold(item.isUserNew);
109
            font.setStrikeOut(!item.inCMakeCache && !item.isUserNew);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
            return font;
        }
        default:
            return QVariant();
        }
    case 1: {
        const QString value = item.isUserChanged ? item.newValue : item.value;
        switch (role) {
        case Qt::CheckStateRole:
            return (item.type == DataItem::BOOLEAN)
                    ? QVariant(isTrue(value) ? Qt::Checked : Qt::Unchecked) : QVariant();
        case Qt::DisplayRole:
            return value;
        case Qt::EditRole:
            return (item.type == DataItem::BOOLEAN) ? QVariant(isTrue(value)) : QVariant(value);
        case Qt::FontRole: {
            QFont font;
            font.setBold(item.isUserChanged || item.isUserNew);
            font.setItalic(item.isCMakeChanged);
            return font;
        }
        case Qt::ToolTipRole:
132
            return item.toolTip();
133 134 135 136 137 138 139
        default:
            return QVariant();
        }
    }
    case 2:
        switch (role) {
        case Qt::EditRole:
140
            return "0";
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        case Qt::DisplayRole:
            return QString::fromLatin1(item.isAdvanced ? "1" : "0");
        case Qt::CheckStateRole:
            return item.isAdvanced ? Qt::Checked : Qt::Unchecked;
        default:
            return QVariant();
        }
    default:
        return QVariant();
    }
}

bool ConfigModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    QTC_ASSERT(index.model() == this, return false);
    QTC_ASSERT(index.isValid(), return false);
    QTC_ASSERT(index.row() >= 0 && index.row() < rowCount(QModelIndex()), return false);
    QTC_ASSERT(index.column() >= 0 && index.column() < 2, return false);

    QString newValue = value.toString();
    if (role == Qt::CheckStateRole) {
        if (index.column() != 1)
            return false;
        newValue = QString::fromLatin1(value.toInt() == 0 ? "OFF" : "ON");
    } else if (role != Qt::EditRole) {
        return false;
    }

    InternalDataItem &item = itemAtRow(index.row());
    switch (index.column()) {
    case 0:
172
        if (!item.key.isEmpty() && !item.isUserNew)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
            return false;
        item.key = newValue;
        item.isUserNew = true;
        item.isUserChanged = false;
        emit dataChanged(index, index);
        return true;
    case 1:
        if (item.value == newValue) {
            item.newValue.clear();
            item.isUserChanged = false;
        } else {
            item.newValue = newValue;
            item.isUserChanged = true;
        }
        emit dataChanged(index, index);
        return true;
    case 2:
    default:
        return false;
    }
}

QVariant ConfigModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Vertical || role != Qt::DisplayRole)
        return QVariant();
    switch (section) {
    case 0:
        return tr("Setting");
    case 1:
        return tr("Value");
    case 2:
        return tr("Advanced");
    default:
        return QVariant();
    }
}

211 212 213
void ConfigModel::appendConfiguration(const QString &key,
                                      const QString &value,
                                      const ConfigModel::DataItem::Type type,
214 215
                                      const QString &description,
                                      const QStringList &values)
216 217 218 219 220 221
{
    DataItem item;
    item.key = key;
    item.type = type;
    item.value = value;
    item.description = description;
222
    item.values = values;
223 224 225 226 227 228 229 230 231

    InternalDataItem internalItem(item);
    internalItem.isUserNew = true;

    beginResetModel();
    m_configuration.append(internalItem);
    endResetModel();
}

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
void ConfigModel::setConfiguration(const QList<ConfigModel::DataItem> &config)
{
    QList<DataItem> tmp = config;
    Utils::sort(tmp,
                [](const ConfigModel::DataItem &i, const ConfigModel::DataItem &j) {
                    return i.key < j.key;
                });
    auto newIt = tmp.constBegin();
    auto newEndIt = tmp.constEnd();
    auto oldIt = m_configuration.constBegin();
    auto oldEndIt = m_configuration.constEnd();

    QList<InternalDataItem> result;
    while (newIt != newEndIt && oldIt != oldEndIt) {
        if (newIt->key < oldIt->key) {
            // Add new entry:
            result << InternalDataItem(*newIt);
            ++newIt;
        } else if (newIt->key > oldIt->key) {
251 252 253
            // Keep old user settings, but skip other entries:
            if (oldIt->isUserChanged || oldIt->isUserNew)
                result << InternalDataItem(*oldIt);
254 255 256 257
            ++oldIt;
        } else {
            // merge old/new entry:
            InternalDataItem item(*newIt);
258
            item.newValue = (newIt->value != oldIt->newValue) ? oldIt->newValue : QString();
259
            item.isCMakeChanged = (oldIt->value != newIt->value);
260
            item.isUserChanged = !item.newValue.isEmpty() && (item.newValue != item.value);
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
            result << item;
            ++newIt;
            ++oldIt;
        }
    }

    // Add remaining new entries:
    for (; newIt != newEndIt; ++newIt)
        result << InternalDataItem(*newIt);

    beginResetModel();
    m_configuration = result;
    endResetModel();
}

void ConfigModel::flush()
{
    beginResetModel();
    m_configuration.clear();
    endResetModel();
}

void ConfigModel::resetAllChanges()
{
    const QList<InternalDataItem> tmp
            = Utils::filtered(m_configuration,
                              [](const InternalDataItem &i) { return !i.isUserNew; });

    beginResetModel();
    m_configuration = Utils::transform(tmp, [](const InternalDataItem &i) -> InternalDataItem {
        InternalDataItem ni(i);
        ni.newValue.clear();
        ni.isUserChanged = false;
        return ni;
    });
    endResetModel();
}

bool ConfigModel::hasChanges() const
{
    return Utils::contains(m_configuration, [](const InternalDataItem &i) { return i.isUserChanged || i.isUserNew; });
}

bool ConfigModel::hasCMakeChanges() const
{
    return Utils::contains(m_configuration, [](const InternalDataItem &i) { return i.isCMakeChanged; });
}

QList<ConfigModel::DataItem> ConfigModel::configurationChanges() const
{
    const QList<InternalDataItem> tmp
312 313 314 315
            = Utils::filtered(m_configuration, [](const InternalDataItem &i) {
        return i.isUserChanged || i.isUserNew || !i.inCMakeCache;
    });
    return Utils::transform(tmp, [](const InternalDataItem &item) {
316 317 318
        DataItem newItem(item);
        if (item.isUserChanged)
            newItem.value = item.newValue;
319 320
        return newItem;
    });
321 322 323 324 325 326 327 328 329 330 331 332 333 334
}

ConfigModel::InternalDataItem &ConfigModel::itemAtRow(int row)
{
    QTC_CHECK(row >= 0);
    return m_configuration[row];
}

const ConfigModel::InternalDataItem &ConfigModel::itemAtRow(int row) const
{
    QTC_CHECK(row >= 0);
    return m_configuration[row];
}

335
ConfigModel::InternalDataItem::InternalDataItem(const ConfigModel::DataItem &item) : DataItem(item)
336 337
{ }

338 339 340 341 342 343 344 345
QString ConfigModel::InternalDataItem::toolTip() const
{
    QStringList tooltip(description);
    if (!value.isEmpty() && !newValue.isEmpty() && value != newValue)
        tooltip << QCoreApplication::translate("CMakeProjectManager", "Current CMake: %1").arg(value);
    return tooltip.join("<br>");
}

346 347
} // namespace CMakeProjectManager