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

Build plugin: Target selection tweaks

- Fix a couple of model index bugs
- Fix "build selected" invoked wrong slot
- Enable/disable move up/down buttons
- Insert target/set copy after original
- New command empty
parent 4143a8ef
Pipeline #247284 passed with stage
in 4 minutes and 31 seconds
......@@ -43,7 +43,7 @@ QModelIndex TargetModel::addTargetSet(const QString &setName, const QString &wor
QString newName = setName;
for (int i = 0; i < m_targets.count(); i++) {
if (m_targets[i].name == newName) {
newName += QStringLiteral(" 2");
newName += QStringLiteral("+");
i = -1;
}
}
......@@ -67,7 +67,7 @@ QModelIndex TargetModel::addCommand(const QModelIndex &parentIndex, const QStrin
QString newName = cmdName;
for (int i = 0; i < m_targets[rootRow].commands.count(); i++) {
if (m_targets[rootRow].commands[i].name == newName) {
newName += QStringLiteral(" 2");
newName += QStringLiteral("+");
i = -1;
}
}
......@@ -92,48 +92,45 @@ QModelIndex TargetModel::copyTargetOrSet(const QModelIndex &index)
return QModelIndex();
}
beginInsertRows(QModelIndex(), m_targets.count(), m_targets.count());
QString newName = m_targets[rootRow].name + QStringLiteral(" 2");
beginInsertRows(QModelIndex(), rootRow + 1, rootRow + 1);
QString newName = m_targets[rootRow].name + QStringLiteral("+");
for (int i = 0; i < m_targets.count(); i++) {
if (m_targets[i].name == newName) {
newName += QStringLiteral(" 2");
newName += QStringLiteral("+");
i = -1;
}
}
m_targets << m_targets[rootRow];
m_targets.last().name = newName;
m_targets.insert(rootRow + 1, m_targets[rootRow]);
m_targets[rootRow + 1].name = newName;
endInsertRows();
return createIndex(m_targets.count() - 1, 0, InvalidIndex);
;
return createIndex(rootRow + 1, 0, InvalidIndex);
}
if (m_targets.count() <= static_cast<int>(rootRow)) {
return QModelIndex();
}
if (index.row() < 0) {
int cmdRow = index.row();
if (cmdRow < 0) {
return QModelIndex();
}
if (index.row() >= m_targets[rootRow].commands.count()) {
if (cmdRow >= m_targets[rootRow].commands.count()) {
return QModelIndex();
}
QModelIndex rootIndex = createIndex(rootRow, 0, InvalidIndex);
beginInsertRows(rootIndex, m_targets[rootRow].commands.count(), m_targets[rootRow].commands.count());
const auto cmd = m_targets[rootRow].commands[index.row()];
QString newName = cmd.name + QStringLiteral(" 2");
beginInsertRows(rootIndex, cmdRow + 1, cmdRow + 1);
const auto cmd = m_targets[rootRow].commands[cmdRow];
QString newName = cmd.name + QStringLiteral("+");
for (int i = 0; i < m_targets[rootRow].commands.count(); i++) {
if (m_targets[rootRow].commands[i].name == newName) {
newName += QStringLiteral(" 2");
newName += QStringLiteral("+");
i = -1;
}
}
m_targets[rootRow].commands << Command{newName, cmd.buildCmd, cmd.runCmd};
m_targets[rootRow].commands.insert(cmdRow + 1, Command{newName, cmd.buildCmd, cmd.runCmd});
endInsertRows();
return createIndex(m_targets[rootRow].commands.count() - 1, 0, rootRow);
return createIndex(cmdRow + 1, 0, rootRow);
}
void TargetModel::deleteItem(const QModelIndex &index)
......@@ -212,7 +209,7 @@ void TargetModel::moveRowDown(const QModelIndex &itemIndex)
QModelIndex parent = itemIndex.parent();
int row = itemIndex.row();
if (row > m_targets.size() - 2) {
if (row >= m_targets.size() - 1) {
return;
}
beginMoveRows(parent, row, row, parent, row + 2);
......@@ -224,6 +221,9 @@ void TargetModel::moveRowDown(const QModelIndex &itemIndex)
qWarning() << "Bad root row index" << rootRow << m_targets.size();
return;
}
if (row >= m_targets[rootRow].commands.size() - 1) {
return;
}
m_targets[rootRow].commands.move(row, row + 1);
}
endMoveRows();
......
......@@ -61,7 +61,7 @@ K_PLUGIN_FACTORY_WITH_JSON(KateBuildPluginFactory, "katebuildplugin.json", regis
static const QString DefConfigCmd = QStringLiteral("cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ../");
static const QString DefConfClean;
static const QString DefTargetName = QStringLiteral("all");
static const QString DefTargetName = QStringLiteral("build");
static const QString DefBuildCmd = QStringLiteral("make");
static const QString DefCleanCmd = QStringLiteral("make clean");
static const QString NinjaPrefix = QStringLiteral("[ninja-detection]");
......@@ -142,7 +142,7 @@ KateBuildView::KateBuildView(KTextEditor::Plugin *plugin, KTextEditor::MainWindo
a = actionCollection()->addAction(QStringLiteral("build_selected_target"));
a->setText(i18n("Build Selected Target"));
connect(a, &QAction::triggered, this, &KateBuildView::slotBuildPreviousTarget);
connect(a, &QAction::triggered, this, &KateBuildView::slotBuildSelectedTarget);
a = actionCollection()->addAction(QStringLiteral("build_and_run_selected_target"));
a->setText(i18n("Build and Run Selected Target"));
......@@ -296,7 +296,7 @@ void KateBuildView::readSessionConfig(const KConfigGroup &cg)
m_targetsUi->targetsView->expandAll();
m_targetsUi->targetsView->resizeColumnToContents(0);
m_targetsUi->targetsView->resizeColumnToContents(1);
m_targetsUi->updateBuildRunButtonStates();
m_targetsUi->updateTargetsButtonStates();
}
/******************************************************************/
......@@ -1206,23 +1206,14 @@ void KateBuildView::slotAddTargetClicked()
{
QModelIndex current = m_targetsUi->targetsView->currentIndex();
QString currName = DefTargetName;
QString currCmd = DefBuildCmd;
QString currCmd;
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();
if (current.parent().isValid()) {
// we need the root item
current = current.parent();
}
current = m_targetsUi->proxyModel.mapToSource(current);
QModelIndex index = m_targetsUi->targetsModel.addCommand(current, currName, currCmd, currRun);
index = m_targetsUi->proxyModel.mapFromSource(index);
m_targetsUi->targetsView->setCurrentIndex(index);
......
......@@ -94,8 +94,9 @@ 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->selectionModel(), &QItemSelectionModel::currentChanged, this, &TargetsUi::updateBuildRunButtonStates);
connect(&targetsModel, &QAbstractItemModel::dataChanged, this, &TargetsUi::updateBuildRunButtonStates);
connect(targetsView->selectionModel(), &QItemSelectionModel::currentChanged, this, &TargetsUi::updateTargetsButtonStates);
connect(&targetsModel, &QAbstractItemModel::dataChanged, this, &TargetsUi::updateTargetsButtonStates);
connect(&targetsModel, &QAbstractItemModel::rowsMoved, this, &TargetsUi::updateTargetsButtonStates);
connect(targetFilterEdit, &QLineEdit::textChanged, this, [this](const QString &text) {
proxyModel.setFilter(text);
......@@ -143,23 +144,35 @@ void TargetsUi::targetActivated(const QModelIndex &index)
targetCombo->setCurrentIndex(rootItem.row());
}
void TargetsUi::updateBuildRunButtonStates()
void TargetsUi::updateTargetsButtonStates()
{
QModelIndex currentIndex = targetsView->currentIndex();
if (!currentIndex.isValid()) {
buildButton->setEnabled(false);
runButton->setEnabled(false);
moveTargetUp->setEnabled(false);
moveTargetDown->setEnabled(false);
return;
}
// If this is a root item, try it's first child
moveTargetUp->setEnabled(currentIndex.row() > 0);
// If this is a root item
if (!currentIndex.parent().isValid()) {
// move down button
int rows = targetsView->model()->rowCount();
moveTargetDown->setEnabled(currentIndex.row() < rows - 1);
// try it's first child to see if we can build/run
currentIndex = targetsView->model()->index(0, 0, currentIndex.siblingAtColumn(0));
if (!currentIndex.isValid()) {
buildButton->setEnabled(false);
runButton->setEnabled(false);
return;
}
} else {
int rows = targetsView->model()->rowCount(currentIndex.parent());
moveTargetDown->setEnabled(currentIndex.row() < rows - 1);
}
const bool hasBuildCmd = !currentIndex.siblingAtColumn(1).data().toString().isEmpty();
......
......@@ -43,7 +43,7 @@ public:
QToolButton *buildButton = nullptr;
QToolButton *runButton = nullptr;
void updateBuildRunButtonStates();
void updateTargetsButtonStates();
public Q_SLOTS:
void targetSetSelected(int index);
......
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