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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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 172 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 211 212
|
/*
SPDX-FileCopyrightText: 2007 Aleix Pol <aleixpol@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "cmakecachemodel.h"
#include <QFile>
#include <KLocalizedString>
#include "cmakecachereader.h"
#include <debug.h>
//4 columns: name, type, value, comment
//name:type=value - comment
CMakeCacheModel::CMakeCacheModel(QObject *parent, const KDevelop::Path &path)
: QStandardItemModel(parent), m_filePath(path)
{
read();
}
void CMakeCacheModel::reset()
{
beginResetModel();
clear();
m_internal.clear();
m_modifiedRows.clear();
read();
endResetModel();
}
void CMakeCacheModel::read()
{
// Set headers
const QStringList labels{
i18nc("@title:column", "Name"),
i18nc("@title:column", "Type"),
i18nc("@title:column", "Value"),
i18nc("@title:column", "Comment"),
i18nc("@title:column", "Advanced"),
i18nc("@title:column", "Strings"),
};
setHorizontalHeaderLabels(labels);
QFile file(m_filePath.toLocalFile());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qCDebug(CMAKE) << "error. Could not find the file";
return;
}
int currentIdx=0;
QStringList currentComment;
QTextStream in(&file);
QHash<QString, int> variablePos;
while (!in.atEnd())
{
QString line = in.readLine().trimmed();
if(line.startsWith(QLatin1String("//")))
{
line.remove(0, 2);
currentComment += line;
} else if(!line.isEmpty() && !line.startsWith(QLatin1Char('#'))) //it is a variable
{
CacheLine c;
c.readLine(line);
if(c.isCorrect())
{
QString name=c.name(), flag=c.flag();
QString type=c.type();
QString value=c.value();
const QList<QStandardItem*> lineItems{
new QStandardItem(name),
new QStandardItem(type),
new QStandardItem(value),
new QStandardItem(currentComment.join(QLatin1Char('\n'))),
};
if(flag==QLatin1String("INTERNAL"))
{
m_internal.insert(name);
} else if(flag==QLatin1String("ADVANCED") || flag==QLatin1String("STRINGS"))
{
const auto posIt = variablePos.constFind(name);
if (posIt != variablePos.constEnd()) {
const int pos = *posIt;
// if the flag is not ADVANCED, it's STRINGS.
// The latter is stored in column 5
int column = flag==QLatin1String("ADVANCED") ? 4 : 5;
QStandardItem *p = item(pos, column);
if(!p)
{
p=new QStandardItem(value);
setItem(pos, column, p);
}
else
{
p->setText(value);
}
}
else
{
qCDebug(CMAKE) << "Flag for an unknown variable";
}
}
if(!flag.isEmpty())
{
lineItems[0]->setText(lineItems[0]->text() + QLatin1Char('-') + flag);
}
insertRow(currentIdx, lineItems);
if (!variablePos.contains(name)) {
variablePos[name]=currentIdx;
}
currentIdx++;
currentComment.clear();
}
}
else if(line.startsWith(QLatin1Char('#')) && line.contains(QLatin1String("INTERNAL")))
{
m_internalBegin=currentIdx;
// qCDebug(CMAKE) << "Comment: " << line << " -.- " << currentIdx;
}
}
}
bool CMakeCacheModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
bool ret = QStandardItemModel::setData(index, value, role);
if (ret) {
const auto i = index.row();
m_modifiedRows.insert(i);
emit valueChanged(item(i, 0)->text(), item(i, 2)->text());
}
return ret;
}
QVariantMap CMakeCacheModel::changedValues() const
{
QVariantMap ret;
for(int i=0; i<rowCount(); i++)
{
if (!m_modifiedRows.contains(i))
continue;
QStandardItem* name = item(i, 0);
QStandardItem* type = item(i, 1);
QStandardItem* valu = item(i, 2);
// QStandardItem* comment = item(i, 3);
ret.insert(name->text()+QLatin1Char(':')+type->text(), valu->text());
}
return ret;
}
QString CMakeCacheModel::value(const QString & varName) const
{
for(int i=0; i<rowCount(); i++)
{
QStandardItem* name = item(i, 0);
if(name->text()==varName) {
QStandardItem* valu = item(i, 2);
return valu->text();
}
}
return QString();
}
bool CMakeCacheModel::isAdvanced(int i) const
{
QStandardItem *p=item(i, 4);
bool isAdv= (p!=nullptr) || i>m_internalBegin;
if(!isAdv)
{
p=item(i, 1);
isAdv = p->text()==QLatin1String("INTERNAL") || p->text()==QLatin1String("STATIC");
}
return isAdv || m_internal.contains(item(i,0)->text());
}
bool CMakeCacheModel::isInternal(int i) const
{
bool isInt= i>m_internalBegin;
return isInt;
}
QList< QModelIndex > CMakeCacheModel::persistentIndices() const
{
QList< QModelIndex > ret;
for(int i=0; i<rowCount(); i++)
{
QStandardItem* type = item(i, 1);
if(type->text()==QLatin1String("BOOL"))
{
QStandardItem* valu = item(i, 2);
ret.append(valu->index());
}
}
return ret;
}
KDevelop::Path CMakeCacheModel::filePath() const
{
return m_filePath;
}
#include "moc_cmakecachemodel.cpp"
|