configmodel.cpp 12 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
/****************************************************************************
**
** 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>
30
#include <utils/theme/theme.h>
31

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

namespace CMakeProjectManager {

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

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

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

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

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 {
72 73 74 75
        Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
        if (item.isUserNew)
            return flags |= Qt::ItemIsEditable;
        return flags;
76 77 78 79 80 81 82 83 84 85 86 87
    }
}

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()];

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

97 98 99 100 101 102 103 104
    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:
105
            return item.toolTip();
106 107 108 109
        case Qt::FontRole: {
            QFont font;
            font.setItalic(item.isCMakeChanged);
            font.setBold(item.isUserNew);
110
            font.setStrikeOut(!item.inCMakeCache && !item.isUserNew);
111 112 113 114 115 116
            return font;
        }
        default:
            return QVariant();
        }
    case 1: {
117 118 119
        const QString value = item.currentValue();
        const QString kitValue = m_kitConfiguartion.value(item.key);

120 121 122 123 124 125 126 127 128 129 130 131 132 133
        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;
        }
134 135 136 137 138 139 140 141 142 143 144 145 146
        case Qt::ForegroundRole:
            return Utils::creatorTheme()->color((!kitValue.isNull() && kitValue != value)
                    ? Utils::Theme::TextColorHighlight : Utils::Theme::TextColorNormal);
        case Qt::ToolTipRole: {
            QString tooltip = item.toolTip();
            const QString kitValue = m_kitConfiguartion.value(item.key);
            if (!kitValue.isNull()) {
                if (!tooltip.isEmpty())
                    tooltip.append("<br>");
                tooltip.append(tr("Kit value: %1").arg(kitValue));
            }
            return tooltip;
        }
147 148 149 150 151 152 153
        default:
            return QVariant();
        }
    }
    case 2:
        switch (role) {
        case Qt::EditRole:
154
            return "0";
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        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:
186
        if (!item.key.isEmpty() && !item.isUserNew)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
            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();
    }
}

225 226 227
void ConfigModel::appendConfiguration(const QString &key,
                                      const QString &value,
                                      const ConfigModel::DataItem::Type type,
228 229
                                      const QString &description,
                                      const QStringList &values)
230 231 232 233 234 235
{
    DataItem item;
    item.key = key;
    item.type = type;
    item.value = value;
    item.description = description;
236
    item.values = values;
237 238 239 240 241 242 243 244 245

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

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

246 247 248 249 250 251 252 253 254 255 256 257 258 259
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) {
260 261 262
        if (newIt->isHidden) {
            ++newIt;
        } else if (newIt->key < oldIt->key) {
263 264 265 266
            // Add new entry:
            result << InternalDataItem(*newIt);
            ++newIt;
        } else if (newIt->key > oldIt->key) {
267 268 269
            // Keep old user settings, but skip other entries:
            if (oldIt->isUserChanged || oldIt->isUserNew)
                result << InternalDataItem(*oldIt);
270 271 272 273
            ++oldIt;
        } else {
            // merge old/new entry:
            InternalDataItem item(*newIt);
274
            item.newValue = (newIt->value != oldIt->newValue) ? oldIt->newValue : QString();
275
            item.isCMakeChanged = (oldIt->value != newIt->value);
276
            item.isUserChanged = !item.newValue.isEmpty() && (item.newValue != item.value);
277 278 279 280 281 282 283
            result << item;
            ++newIt;
            ++oldIt;
        }
    }

    // Add remaining new entries:
284 285 286
    for (; newIt != newEndIt; ++newIt) {
        if (newIt->isHidden)
            continue;
287
        result << InternalDataItem(*newIt);
288
    }
289 290 291 292 293 294

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

295 296 297 298 299
void ConfigModel::setKitConfiguration(const QHash<QString, QString> &kitConfig)
{
    m_kitConfiguartion = kitConfig;
}

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
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
336 337 338 339
            = Utils::filtered(m_configuration, [](const InternalDataItem &i) {
        return i.isUserChanged || i.isUserNew || !i.inCMakeCache;
    });
    return Utils::transform(tmp, [](const InternalDataItem &item) {
340 341 342
        DataItem newItem(item);
        if (item.isUserChanged)
            newItem.value = item.newValue;
343 344
        return newItem;
    });
345 346 347 348 349 350 351 352 353 354 355 356 357 358
}

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];
}

359
ConfigModel::InternalDataItem::InternalDataItem(const ConfigModel::DataItem &item) : DataItem(item)
360 361
{ }

362 363 364
QString ConfigModel::InternalDataItem::toolTip() const
{
    QStringList tooltip(description);
365 366 367 368 369 370
    if (inCMakeCache) {
        if (value != newValue)
            tooltip << QCoreApplication::translate("CMakeProjectManager", "Current CMake: %1").arg(value);
    }  else {
        tooltip << QCoreApplication::translate("CMakeProjectManager", "Not in CMakeCache.txt").arg(value);
    }
371 372 373
    return tooltip.join("<br>");
}

374 375 376 377
QString ConfigModel::InternalDataItem::currentValue() const
{
    return isUserChanged ? newValue : value;
}
378

379
} // namespace CMakeProjectManager