Skip to content

Commit

Permalink
Build-plugin: Make Project targets behave like regular targets.
Browse files Browse the repository at this point in the history
- Make it possible to move the project target-set and store the row
- Store the last active target and restore on session-read
- On first build(&run) invocation, open the target selection "dialog"
  in stead, if it is not already visible.
  • Loading branch information
kasars committed Oct 29, 2022
1 parent 3644280 commit 4abcb6d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 19 deletions.
1 change: 1 addition & 0 deletions .kateproject
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"exclude_patterns": [ "^po" ],
"build": {
"directory": "build",
"default_target": "kate-all",
"targets": [
{
"name": "kdesrcbuild-kate",
Expand Down
15 changes: 12 additions & 3 deletions addons/katebuild-plugin/TargetModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ void TargetModel::clear()

QModelIndex TargetModel::addTargetSet(const QString &setName, const QString &workDir)
{
return insertTargetSet(m_targets.count(), setName, workDir);
}

QModelIndex TargetModel::insertTargetSet(int row, const QString &setName, const QString &workDir)
{
if (row < 0 || row > m_targets.count()) {
qWarning() << "Row index out of bounds:" << row << m_targets.count();
}

// make the name unique
QString newName = setName;
for (int i = 0; i < m_targets.count(); i++) {
Expand All @@ -48,11 +57,11 @@ QModelIndex TargetModel::addTargetSet(const QString &setName, const QString &wor
}
}

beginInsertRows(QModelIndex(), m_targets.count(), m_targets.count());
beginInsertRows(QModelIndex(), row, row);
TargetModel::TargetSet targetSet(newName, workDir);
m_targets << targetSet;
m_targets.insert(row, targetSet);
endInsertRows();
return index(m_targets.count() - 1, 0);
return index(row, 0);
}

QModelIndex TargetModel::addCommand(const QModelIndex &parentIndex, const QString &cmdName, const QString &buildCmd, const QString &runCmd)
Expand Down
4 changes: 4 additions & 0 deletions addons/katebuild-plugin/TargetModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public Q_SLOTS:
* inserted target-set */
QModelIndex addTargetSet(const QString &setName, const QString &workDir);

/** This function insert a target set and returns the model-index of the newly
* inserted target-set */
QModelIndex insertTargetSet(int row, const QString &setName, const QString &workDir);

/** This function adds a new command to a target-set and returns the model index */
QModelIndex addCommand(const QModelIndex &parentIndex, const QString &cmdName, const QString &buildCmd, const QString &runCmd);

Expand Down
69 changes: 55 additions & 14 deletions addons/katebuild-plugin/plugin_katebuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ KateBuildView::~KateBuildView()
void KateBuildView::readSessionConfig(const KConfigGroup &cg)
{
int numTargets = cg.readEntry(QStringLiteral("NumTargets"), 0);
m_projectTargetsetRow = cg.readEntry("ProjectTargetSetRow", 0);
m_targetsUi->targetsModel.clear();

for (int i = 0; i < numTargets; i++) {
Expand Down Expand Up @@ -295,20 +296,52 @@ void KateBuildView::readSessionConfig(const KConfigGroup &cg)
m_showMarks->setChecked(showMarks);

// Add project targets, if any
slotAddProjectTarget();
addProjectTarget();

m_targetsUi->targetsView->expandAll();
m_targetsUi->targetsView->resizeColumnToContents(0);
m_targetsUi->targetsView->resizeColumnToContents(1);

// pre-select the last active target or the first target of the first set
int prevTargetSetRow = cg.readEntry(QStringLiteral("Active Target Index"), 0);
int prevCmdRow = cg.readEntry(QStringLiteral("Active Target Command"), 0);
QModelIndex rootIndex = m_targetsUi->targetsModel.index(prevTargetSetRow);
QModelIndex cmdIndex = m_targetsUi->targetsModel.index(prevCmdRow, 0, rootIndex);
cmdIndex = m_targetsUi->proxyModel.mapFromSource(cmdIndex);
m_targetsUi->targetsView->setCurrentIndex(cmdIndex);

m_targetsUi->updateTargetsButtonStates();
}

/******************************************************************/
void KateBuildView::writeSessionConfig(KConfigGroup &cg)
{
// Don't save project targets, is not our area of accountability
m_targetsUi->targetsModel.deleteTargetSet(i18n("Project Plugin Targets"));
// Save the active target
QModelIndex activeIndex = m_targetsUi->targetsView->currentIndex();
activeIndex = m_targetsUi->proxyModel.mapToSource(activeIndex);
if (activeIndex.isValid()) {
if (activeIndex.parent().isValid()) {
cg.writeEntry(QStringLiteral("Active Target Index"), activeIndex.parent().row());
cg.writeEntry(QStringLiteral("Active Target Command"), activeIndex.row());
} else {
cg.writeEntry(QStringLiteral("Active Target Index"), activeIndex.row());
cg.writeEntry(QStringLiteral("Active Target Command"), 0);
}
}

QList<TargetModel::TargetSet> targets = m_targetsUi->targetsModel.targetSets();

// Don't save project target-set, but save the row index
m_projectTargetsetRow = 0;
for (int i = 0; i < targets.size(); i++) {
if (i18n("Project Plugin Targets") == targets[i].name) {
m_projectTargetsetRow = i;
targets.removeAt(i);
break;
}
}

cg.writeEntry("ProjectTargetSetRow", m_projectTargetsetRow);
cg.writeEntry("NumTargets", targets.size());

for (int i = 0; i < targets.size(); i++) {
Expand All @@ -327,9 +360,6 @@ void KateBuildView::writeSessionConfig(KConfigGroup &cg)
cg.writeEntry(QStringLiteral("%1 Target Names").arg(i), cmdNames);
}
cg.writeEntry(QStringLiteral("Show Marks"), m_showMarks->isChecked());

// Restore project targets, if any
slotAddProjectTarget();
}

/******************************************************************/
Expand Down Expand Up @@ -810,10 +840,11 @@ bool KateBuildView::slotStop()
void KateBuildView::slotBuildSelectedTarget()
{
QModelIndex currentIndex = m_targetsUi->targetsView->currentIndex();
if (!currentIndex.isValid()) {
if (!currentIndex.isValid() || (m_firstBuild && !m_targetsUi->targetsView->isVisible())) {
slotSelectTarget();
return;
}
m_firstBuild = false;

if (!currentIndex.parent().isValid()) {
// This is a root item, try to build the first command
Expand All @@ -832,10 +863,11 @@ void KateBuildView::slotBuildSelectedTarget()
void KateBuildView::slotBuildAndRunSelectedTarget()
{
QModelIndex currentIndex = m_targetsUi->targetsView->currentIndex();
if (!currentIndex.isValid()) {
if (!currentIndex.isValid() || (m_firstBuild && !m_targetsUi->targetsView->isVisible())) {
slotSelectTarget();
return;
}
m_firstBuild = false;

if (!currentIndex.parent().isValid()) {
// This is a root item, try to build the first command
Expand Down Expand Up @@ -1326,7 +1358,7 @@ void KateBuildView::slotPluginViewCreated(const QString &name, QObject *pluginVi
// add view
if (pluginView && name == QLatin1String("kateprojectplugin")) {
m_projectPluginView = pluginView;
slotAddProjectTarget();
addProjectTarget();
connect(pluginView, SIGNAL(projectMapChanged()), this, SLOT(slotProjectMapChanged()), Qt::UniqueConnection);
}
}
Expand All @@ -1349,11 +1381,11 @@ void KateBuildView::slotProjectMapChanged()
return;
}
m_targetsUi->targetsModel.deleteTargetSet(i18n("Project Plugin Targets"));
slotAddProjectTarget();
addProjectTarget();
}

/******************************************************************/
void KateBuildView::slotAddProjectTarget()
void KateBuildView::addProjectTarget()
{
// only do stuff with valid project
if (!m_projectPluginView) {
Expand All @@ -1377,7 +1409,10 @@ void KateBuildView::slotAddProjectTarget()
if (!projectsBaseDir.isEmpty()) {
projectsBuildDir = QDir(projectsBaseDir).absoluteFilePath(projectsBuildDir);
}
const QModelIndex set = m_targetsUi->targetsModel.addTargetSet(i18n("Project Plugin Targets"), projectsBuildDir);

m_projectTargetsetRow = std::min(m_projectTargetsetRow, m_targetsUi->targetsModel.rowCount());
const QModelIndex set = m_targetsUi->targetsModel.insertTargetSet(m_projectTargetsetRow, i18n("Project Plugin Targets"), projectsBuildDir);
const QString defaultTarget = buildMap.value(QStringLiteral("default_target")).toString();

const QVariantList targetsets = buildMap.value(QStringLiteral("targets")).toList();
for (const QVariant &targetVariant : targetsets) {
Expand All @@ -1389,10 +1424,16 @@ void KateBuildView::slotAddProjectTarget()
if (tgtName.isEmpty() || buildCmd.isEmpty()) {
continue;
}
m_targetsUi->targetsModel.addCommand(set, tgtName, buildCmd, runCmd);
QPersistentModelIndex idx = m_targetsUi->targetsModel.addCommand(set, tgtName, buildCmd, runCmd);
if (tgtName == defaultTarget) {
// A bit of backwards compatibility, move the "default" target to the top
while (idx.row() > 0) {
m_targetsUi->targetsModel.moveRowUp(idx);
}
}
}

if (!set.model()->index(0, 0, set).data().isValid()) {
if (!set.model()->index(0, 0, set).isValid()) {
QString buildCmd = buildMap.value(QStringLiteral("build")).toString();
QString cleanCmd = buildMap.value(QStringLiteral("clean")).toString();
QString quickCmd = buildMap.value(QStringLiteral("quick")).toString();
Expand Down
5 changes: 4 additions & 1 deletion addons/katebuild-plugin/plugin_katebuild.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ private Q_SLOTS:
void slotPluginViewCreated(const QString &name, QObject *pluginView);
void slotPluginViewDeleted(const QString &name, QObject *pluginView);
void slotProjectMapChanged();
void slotAddProjectTarget();

protected:
bool eventFilter(QObject *obj, QEvent *ev) override;
Expand All @@ -130,6 +129,8 @@ private Q_SLOTS:
void clearMarks();
void addMarks(KTextEditor::Document *doc, bool mark);

void addProjectTarget();

KTextEditor::MainWindow *m_win;
QWidget *m_toolView;
Ui::build m_buildUi{};
Expand All @@ -155,6 +156,8 @@ private Q_SLOTS:
QPointer<KTextEditor::Message> m_infoMessage;
QPointer<QAction> m_showMarks;
QHash<KTextEditor::Document *, QPointer<KTextEditor::Document>> m_markedDocs;
int m_projectTargetsetRow = 0;
bool m_firstBuild = true;

/**
* current project plugin view, if any
Expand Down
2 changes: 1 addition & 1 deletion addons/katebuild-plugin/targets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TargetsUi::TargetsUi(QObject *view, QWidget *parent)
proxyModel.setSourceModel(&targetsModel);

targetFilterEdit = new QLineEdit(this);
targetFilterEdit->setPlaceholderText(i18n("Filter targets, use arrow keys to select, press Enter to execute"));
targetFilterEdit->setPlaceholderText(i18n("Filter targets, use arrow keys to select, Enter to execute"));
targetFilterEdit->setClearButtonEnabled(true);

newTarget = new QToolButton(this);
Expand Down

0 comments on commit 4abcb6d

Please sign in to comment.