Commit 5aca93b8 authored by Kåre Särs's avatar Kåre Särs
Browse files

Add more build-plugin target model index checking

- Also make the add command use the current command in stead of just "make"
parent 3b8dd5e5
Pipeline #242338 passed with stage
in 10 minutes and 26 seconds
......@@ -15,27 +15,29 @@ TargetFilterProxyModel::TargetFilterProxyModel(QObject *parent)
bool TargetFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex srcIndex = sourceModel()->index(sourceRow, 0, sourceParent);
if (!srcIndex.isValid()) {
qDebug() << "srcIndex is invalid";
return false;
}
if (m_filter.isEmpty()) {
return true;
}
QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
QString name = index0.data().toString();
if (index0.internalId() == 0xffffffff) {
int i = 0;
auto childIndex = index0.model()->index(i, 0, index0);
while (childIndex.data().isValid()) {
name = childIndex.data().toString();
if (name.contains(m_filter, Qt::CaseInsensitive)) {
return true;
}
i++;
childIndex = index0.model()->index(i, 0, index0);
QString name = srcIndex.data().toString();
if (name.contains(m_filter, Qt::CaseInsensitive)) {
return true;
}
for (int row = 0; row < sourceModel()->rowCount(srcIndex); ++row) {
const QModelIndex childIndex = srcIndex.model()->index(row, 0, srcIndex);
name = childIndex.data().toString();
if (name.contains(m_filter, Qt::CaseInsensitive)) {
return true;
}
return false;
}
return name.contains(m_filter, Qt::CaseInsensitive);
return false;
}
void TargetFilterProxyModel::setFilter(const QString &filter)
......
......@@ -74,7 +74,7 @@ QModelIndex TargetModel::addCommand(const QModelIndex &parentIndex, const QStrin
{
int rootRow = parentIndex.row();
if (rootRow < 0 || rootRow >= m_targets.size()) {
qDebug() << "rootRow not valid";
qDebug() << "rootRow" << rootRow << "not valid" << m_targets.size();
return QModelIndex();
}
......@@ -187,12 +187,26 @@ void TargetModel::deleteItem(const QModelIndex &index)
}
if (index.internalId() == InvalidIndex) {
if (index.row() < 0 || index.row() >= m_targets.size()) {
qWarning() << "Bad target-set row:" << index.row() << m_targets.size();
return;
}
beginRemoveRows(index.parent(), index.row(), index.row());
m_targets.removeAt(index.row());
endRemoveRows();
} else if (index.internalId() < static_cast<quint64>(m_targets.size()) && m_targets[static_cast<int>(index.internalId())].commands.count() > index.row()) {
} else {
int setRow = static_cast<int>(index.internalId());
if (setRow >= m_targets.size()) {
qWarning() << "Bad target-set row:" << index.internalId() << m_targets.size();
return;
}
TargetSet &set = m_targets[setRow];
if (index.row() < 0 || index.row() >= set.commands.size()) {
qWarning() << "Bad command row:" << index.row() << set.commands.size();
return;
}
beginRemoveRows(index.parent(), index.row(), index.row());
m_targets[static_cast<int>(index.internalId())].commands.removeAt(index.row());
set.commands.removeAt(index.row());
endRemoveRows();
}
}
......@@ -461,6 +475,10 @@ int TargetModel::rowCount(const QModelIndex &parent) const
return 0;
}
if (parent.column() != 0) {
return 0;
}
int row = parent.row();
if (row < 0 || row >= m_targets.size()) {
return 0;
......@@ -476,19 +494,44 @@ int TargetModel::columnCount(const QModelIndex &) const
QModelIndex TargetModel::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0) {
return QModelIndex();
}
quint32 rootIndex = InvalidIndex;
if (parent.isValid()) {
if (parent.internalId() == InvalidIndex) {
rootIndex = parent.row();
if (parent.isValid() && parent.internalId() == InvalidIndex) {
// This is a command (child of a root element)
if (parent.column() != 0) {
// Only root item column 0 can have children
return QModelIndex();
}
rootIndex = parent.row();
if (parent.row() >= m_targets.size() || row >= m_targets.at(parent.row()).commands.size()) {
return QModelIndex();
}
return createIndex(row, column, rootIndex);
}
// This is a root item
if (row >= m_targets.size()) {
return QModelIndex();
}
return createIndex(row, column, rootIndex);
}
QModelIndex TargetModel::parent(const QModelIndex &child) const
{
if (!child.isValid()) {
return QModelIndex();
}
if (child.internalId() == InvalidIndex) {
return QModelIndex();
}
return createIndex(child.internalId(), 0, InvalidIndex);
int pRow = (int)child.internalId();
if (pRow < 0 || pRow >= m_targets.size()) {
return QModelIndex();
}
return createIndex(pRow, 0, InvalidIndex);
}
/***************************************************************************
* This file is part of Kate build plugin *
* SPDX-FileCopyrightText: 2014 Kåre Särs <kare.sars@iki.fi> *
* SPDX-FileCopyrightText: 2014 Kåre Särs <kare.sars@iki.fi> *
* *
* SPDX-License-Identifier: LGPL-2.0-or-later
***************************************************************************/
......
......@@ -1210,12 +1210,25 @@ void KateBuildView::processLine(QStringView line)
void KateBuildView::slotAddTargetClicked()
{
QModelIndex current = m_targetsUi->targetsView->currentIndex();
QString currName = DefTargetName;
QString currCmd = DefBuildCmd;
QString currRun;
if (current.parent().isValid()) {
// Copy the active command
const QModelIndex nameIndex = current.siblingAtColumn(0);
currName = nameIndex.data().toString();
const QModelIndex cmdIndex = current.siblingAtColumn(1);
currCmd = cmdIndex.data().toString();
const QModelIndex runIndex = current.siblingAtColumn(2);
currRun = runIndex.data().toString();
// we need the root item
current = current.parent();
}
current = m_targetsUi->proxyModel.mapToSource(current);
QModelIndex index = m_targetsUi->targetsModel.addCommand(current, DefTargetName, DefBuildCmd, QString());
QModelIndex index = m_targetsUi->targetsModel.addCommand(current, currName, currCmd, currRun);
index = m_targetsUi->proxyModel.mapFromSource(index);
m_targetsUi->targetsView->setCurrentIndex(index);
}
......
......@@ -84,7 +84,6 @@ TargetsUi::TargetsUi(QObject *view, QWidget *parent)
connect(targetCombo, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &TargetsUi::targetSetSelected);
connect(targetsView->selectionModel(), &QItemSelectionModel::currentChanged, this, &TargetsUi::targetActivated);
// connect(targetsView, SIGNAL(clicked(QModelIndex)), this, SLOT(targetActivated(QModelIndex)));
connect(targetFilterEdit, &QLineEdit::textChanged, this, [this](const QString &text) {
proxyModel.setFilter(text);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment