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

build-plugin: Update run button after model edit

- Move the Targets UI state updating to TargetsUI
- emit dataChanged when the edit is done
- update the build&run button states on dataChanged
- rename index parameter to itemIndex to not clash with function
parent 7bbf0434
Pipeline #243595 passed with stage
in 15 minutes and 13 seconds
......@@ -393,53 +393,59 @@ QVariant TargetModel::headerData(int section, Qt::Orientation orientation, int r
return QVariant();
}
bool TargetModel::setData(const QModelIndex &index, const QVariant &value, int role)
bool TargetModel::setData(const QModelIndex &itemIndex, const QVariant &value, int role)
{
// FIXME
if (role != Qt::EditRole && role != Qt::CheckStateRole) {
return false;
}
if (!index.isValid() || !hasIndex(index.row(), index.column(), index.parent())) {
if (!itemIndex.isValid() || !hasIndex(itemIndex.row(), itemIndex.column(), itemIndex.parent())) {
return false;
}
int row = index.row();
if (index.internalId() == InvalidIndex) {
int row = itemIndex.row();
if (itemIndex.internalId() == InvalidIndex) {
if (row < 0 || row >= m_targets.size()) {
return false;
}
switch (index.column()) {
switch (itemIndex.column()) {
case 0:
m_targets[row].name = value.toString();
Q_EMIT dataChanged(createIndex(row, 0), createIndex(row, 0));
return true;
case 1:
Q_EMIT dataChanged(createIndex(row, 1), createIndex(row, 1));
m_targets[row].workDir = value.toString();
return true;
}
} else {
int rootIndex = index.internalId();
if (rootIndex < 0 || rootIndex >= m_targets.size()) {
int rootRow = itemIndex.internalId();
if (rootRow < 0 || rootRow >= m_targets.size()) {
return false;
}
if (row < 0 || row >= m_targets[rootIndex].commands.size()) {
if (row < 0 || row >= m_targets[rootRow].commands.size()) {
return false;
}
if (role == Qt::CheckStateRole) {
if (index.column() == 0) {
m_targets[rootIndex].defaultCmd = m_targets[rootIndex].commands[row].buildCmd;
Q_EMIT dataChanged(createIndex(0, 0, rootIndex), createIndex(m_targets[rootIndex].commands.size() - 1, 0, rootIndex));
if (itemIndex.column() == 0) {
m_targets[rootRow].defaultCmd = m_targets[rootRow].commands[row].buildCmd;
Q_EMIT dataChanged(createIndex(0, 0, rootRow), createIndex(m_targets[rootRow].commands.size() - 1, 0, rootRow));
}
} else {
switch (index.column()) {
QModelIndex rootIndex = createIndex(rootRow, 0);
switch (itemIndex.column()) {
case 0:
m_targets[rootIndex].commands[row].name = value.toString();
m_targets[rootRow].commands[row].name = value.toString();
Q_EMIT dataChanged(index(row, 0, rootIndex), index(row, 0, rootIndex));
return true;
case 1:
m_targets[rootIndex].commands[row].buildCmd = value.toString();
m_targets[rootRow].commands[row].buildCmd = value.toString();
Q_EMIT dataChanged(index(row, 1, rootIndex), index(row, 1, rootIndex));
return true;
case 2:
m_targets[rootIndex].commands[row].runCmd = value.toString();
m_targets[rootRow].commands[row].runCmd = value.toString();
Q_EMIT dataChanged(index(row, 2, rootIndex), index(row, 2, rootIndex));
return true;
}
}
......
......@@ -233,7 +233,6 @@ KateBuildView::KateBuildView(KTextEditor::Plugin *plugin, KTextEditor::MainWindo
slotBuildActiveTarget();
});
connect(m_targetsUi, &TargetsUi::enterPressed, this, &KateBuildView::slotBuildActiveTarget);
connect(m_targetsUi->targetsView->selectionModel(), &QItemSelectionModel::currentChanged, this, &KateBuildView::onSelectionChanged);
m_proc.setOutputChannelMode(KProcess::SeparateChannels);
connect(&m_proc, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &KateBuildView::slotProcExited);
......@@ -1419,20 +1418,6 @@ void KateBuildView::slotAddProjectTarget()
}
}
void KateBuildView::onSelectionChanged(const QModelIndex &current, const QModelIndex &)
{
if (!current.isValid() || !current.parent().isValid()) {
m_targetsUi->buildButton->setEnabled(false);
m_targetsUi->runButton->setEnabled(false);
return;
}
const bool hasBuildCmd = !current.siblingAtColumn(1).data().toString().isEmpty();
const bool hasRunCmd = !current.siblingAtColumn(2).data().toString().isEmpty();
m_targetsUi->buildButton->setEnabled(hasBuildCmd);
// Run button can be enabled even if there is no build command
m_targetsUi->runButton->setEnabled(hasRunCmd);
}
/******************************************************************/
bool KateBuildView::eventFilter(QObject *obj, QEvent *event)
{
......
......@@ -112,8 +112,6 @@ private Q_SLOTS:
void slotProjectMapChanged();
void slotAddProjectTarget();
void onSelectionChanged(const QModelIndex &, const QModelIndex &current);
protected:
bool eventFilter(QObject *obj, QEvent *ev) override;
......
......@@ -87,6 +87,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(targetFilterEdit, &QLineEdit::textChanged, this, [this](const QString &text) {
proxyModel.setFilter(text);
targetsView->expandAll();
......@@ -120,6 +123,21 @@ void TargetsUi::targetActivated(const QModelIndex &index)
targetCombo->setCurrentIndex(rootItem.row());
}
void TargetsUi::updateBuildRunButtonStates()
{
const QModelIndex &currentIndex = targetsView->currentIndex();
if (!currentIndex.isValid() || !currentIndex.parent().isValid()) {
buildButton->setEnabled(false);
runButton->setEnabled(false);
return;
}
const bool hasBuildCmd = !currentIndex.siblingAtColumn(1).data().toString().isEmpty();
const bool hasRunCmd = !currentIndex.siblingAtColumn(2).data().toString().isEmpty();
buildButton->setEnabled(hasBuildCmd);
// Run button can be enabled even if there is no build command
runButton->setEnabled(hasRunCmd);
}
bool TargetsUi::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
......
......@@ -52,6 +52,8 @@ protected:
bool eventFilter(QObject *obj, QEvent *event) override;
private:
void updateBuildRunButtonStates();
TargetHtmlDelegate *m_delegate = nullptr;
};
......
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