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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/* This file is part of KDevelop
Copyright 2018 Daniel Mensinger <daniel@mensinger-ka.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "mesonconfigpage.h"
#include "mesonbuilder.h"
#include "mesonjob.h"
#include "mesonmanager.h"
#include "mesonnewbuilddir.h"
#include "mintro/mesonintrospectjob.h"
#include "ui_mesonconfigpage.h"
#include <debug.h>
#include <executecompositejob.h>
#include <interfaces/iplugin.h>
#include <interfaces/iproject.h>
#include <KColorScheme>
#include <QIcon>
using namespace KDevelop;
MesonConfigPage::MesonConfigPage(IPlugin* plugin, IProject* project, QWidget* parent)
: ConfigPage(plugin, nullptr, parent)
, m_project(project)
{
Q_ASSERT(project); // Catch errors early
auto* mgr = dynamic_cast<MesonManager*>(m_project->buildSystemManager());
Q_ASSERT(mgr); // This dialog only works with the MesonManager
m_ui = new Ui::MesonConfigPage;
m_ui->setupUi(this);
m_ui->advanced->setSupportedBackends(mgr->supportedMesonBackends());
m_config = Meson::getMesonConfig(m_project);
if (m_config.buildDirs.isEmpty()) {
m_config.currentIndex = -1;
reset();
return;
} else if (m_config.currentIndex < 0 || m_config.currentIndex >= m_config.buildDirs.size()) {
m_config.currentIndex = 0;
}
QStringList buildPathList;
for (auto& i : m_config.buildDirs) {
buildPathList << i.buildDir.toLocalFile();
}
m_ui->i_buildDirs->blockSignals(true);
m_ui->i_buildDirs->clear();
m_ui->i_buildDirs->addItems(buildPathList);
m_ui->i_buildDirs->setCurrentIndex(m_config.currentIndex);
m_ui->i_buildDirs->blockSignals(false);
reset();
}
void MesonConfigPage::writeConfig()
{
qCDebug(KDEV_Meson) << "Writing config to file";
if (m_config.currentIndex >= 0) {
m_config.buildDirs[m_config.currentIndex] = m_current;
}
if (m_config.buildDirs.isEmpty()) {
m_config.currentIndex = -1;
} else if (m_config.currentIndex < 0 || m_config.currentIndex >= m_config.buildDirs.size()) {
m_config.currentIndex = 0;
}
Meson::writeMesonConfig(m_project, m_config);
}
void MesonConfigPage::apply()
{
qCDebug(KDEV_Meson) << "Applying meson config for build dir " << m_current.buildDir;
readUI();
writeConfig();
if (m_config.currentIndex >= 0 && m_configChanged) {
QList<KJob*> joblist;
auto options = m_ui->options->options();
if (!options) {
qCWarning(KDEV_Meson) << "Options is nullptr. Can not update meson config";
return;
}
QStringList mesonArgs = options->getMesonArgs();
if (mesonArgs.empty()) {
qCDebug(KDEV_Meson) << "Config has not changed --> nothing has to be updated";
return;
}
// Check if a configuration is required
auto status = MesonBuilder::evaluateBuildDirectory(m_current.buildDir, m_current.mesonBackend);
if (status != MesonBuilder::MESON_CONFIGURED) {
joblist << new MesonJob(m_current, m_project, MesonJob::CONFIGURE, mesonArgs, nullptr);
}
joblist << new MesonJob(m_current, m_project, MesonJob::SET_CONFIG, mesonArgs, nullptr);
joblist << m_ui->options->repopulateFromBuildDir(m_project, m_current);
KJob* job = new ExecuteCompositeJob(nullptr, joblist);
connect(job, &KJob::result, this, [this]() {
setDisabled(false);
updateUI();
});
setDisabled(true);
m_configChanged = false;
job->start();
}
}
void MesonConfigPage::defaults()
{
qCDebug(KDEV_Meson) << "Restoring build dir " << m_current.buildDir << " to it's default values";
auto* mgr = dynamic_cast<MesonManager*>(m_project->buildSystemManager());
Q_ASSERT(mgr);
m_current.mesonArgs.clear();
m_current.mesonBackend = mgr->defaultMesonBackend();
m_current.mesonExecutable = mgr->findMeson();
m_ui->options->resetAll();
updateUI();
}
void MesonConfigPage::reset()
{
if (m_config.buildDirs.isEmpty()) {
m_config.currentIndex = -1;
m_ui->i_buildDirs->clear();
setWidgetsDisabled(true);
m_ui->b_addDir->setDisabled(false); // Allow adding a new build dir when there are none
return;
} else if (m_config.currentIndex < 0 || m_config.currentIndex >= m_config.buildDirs.size()) {
m_config.currentIndex = 0;
m_ui->i_buildDirs->blockSignals(true);
m_ui->i_buildDirs->setCurrentIndex(m_config.currentIndex);
m_ui->i_buildDirs->blockSignals(false);
}
setWidgetsDisabled(false);
qCDebug(KDEV_Meson) << "Resetting changes for build dir " << m_current.buildDir;
m_current = m_config.buildDirs[m_config.currentIndex];
m_ui->options->repopulateFromBuildDir(m_project, m_current)->start();
updateUI();
}
void MesonConfigPage::checkStatus()
{
// Get the config build dir status
auto status = MesonBuilder::evaluateBuildDirectory(m_current.buildDir, m_current.mesonBackend);
auto setStatus = [this](const QString& msg, int color) -> void {
KColorScheme scheme(QPalette::Normal);
KColorScheme::ForegroundRole role;
switch (color) {
case 0:
role = KColorScheme::PositiveText;
break;
case 1:
role = KColorScheme::NeutralText;
break;
case 2:
default:
role = KColorScheme::NegativeText;
break;
}
QPalette pal = m_ui->l_status->palette();
pal.setColor(QPalette::WindowText, scheme.foreground(role).color());
m_ui->l_status->setPalette(pal);
m_ui->l_status->setText(i18n("Status: %1", msg));
};
switch (status) {
case MesonBuilder::DOES_NOT_EXIST:
setStatus(i18n("The current build directory does not exist"), 1);
break;
case MesonBuilder::CLEAN:
setStatus(i18n("The current build directory is empty"), 1);
break;
case MesonBuilder::MESON_CONFIGURED:
setStatus(i18n("Build directory configured"), 0);
break;
case MesonBuilder::MESON_FAILED_CONFIGURATION:
setStatus(i18n("This meson build directory is not fully configured"), 1);
break;
case MesonBuilder::INVALID_BUILD_DIR:
setStatus(i18n("The current build directory is invalid"), 2);
break;
case MesonBuilder::DIR_NOT_EMPTY:
setStatus(i18n("This directory does not seem to be a meson build directory"), 2);
break;
case MesonBuilder::EMPTY_STRING:
setStatus(i18n("Invalid build directory configuration (empty build directory string)"), 2);
break;
case MesonBuilder::___UNDEFINED___:
setStatus(i18n("Something went very wrong. This is a bug"), 2);
break;
}
KColorScheme scheme(QPalette::Normal);
KColorScheme::ForegroundRole role;
int numChanged = 0;
auto options = m_ui->options->options();
if (options) {
numChanged = options->numChanged();
}
if (numChanged == 0) {
role = KColorScheme::NormalText;
m_ui->l_changed->setText(i18n("No changes"));
} else {
role = KColorScheme::NeutralText;
m_ui->l_changed->setText(i18np("%1 option changed", "%1 options changed", numChanged));
}
QPalette pal = m_ui->l_changed->palette();
pal.setColor(QPalette::WindowText, scheme.foreground(role).color());
m_ui->l_changed->setPalette(pal);
}
void MesonConfigPage::updateUI()
{
auto aConf = m_ui->advanced->getConfig();
aConf.args = m_current.mesonArgs;
aConf.backend = m_current.mesonBackend;
aConf.meson = m_current.mesonExecutable;
m_ui->advanced->setConfig(aConf);
checkStatus();
}
void MesonConfigPage::readUI()
{
qCDebug(KDEV_Meson) << "Reading current build configuration from the UI " << m_current.buildDir.toLocalFile();
auto aConf = m_ui->advanced->getConfig();
m_current.mesonArgs = aConf.args;
m_current.mesonBackend = aConf.backend;
m_current.mesonExecutable = aConf.meson;
}
void MesonConfigPage::setWidgetsDisabled(bool disabled)
{
m_ui->advanced->setDisabled(disabled);
m_ui->i_buildDirs->setDisabled(disabled);
m_ui->b_addDir->setDisabled(disabled);
m_ui->b_rmDir->setDisabled(disabled);
m_ui->options->setDisabled(disabled);
}
void MesonConfigPage::addBuildDir()
{
qCDebug(KDEV_Meson) << "Adding build directory";
auto* mgr = dynamic_cast<MesonManager*>(m_project->buildSystemManager());
auto* bld = dynamic_cast<MesonBuilder*>(mgr->builder());
Q_ASSERT(mgr);
Q_ASSERT(bld);
MesonNewBuildDir newBD(m_project);
if (!newBD.exec() || !newBD.isConfigValid()) {
qCDebug(KDEV_Meson) << "Failed to create a new build directory";
return;
}
m_current = newBD.currentConfig();
m_current.canonicalizePaths();
m_config.currentIndex = m_config.addBuildDir(m_current);
m_ui->i_buildDirs->blockSignals(true);
m_ui->i_buildDirs->addItem(m_current.buildDir.toLocalFile());
m_ui->i_buildDirs->setCurrentIndex(m_config.currentIndex);
m_ui->i_buildDirs->blockSignals(false);
setWidgetsDisabled(true);
writeConfig();
KJob* job = bld->configure(m_project, m_current, newBD.mesonArgs());
connect(job, &KJob::result, this, [this]() { reset(); });
job->start();
}
void MesonConfigPage::removeBuildDir()
{
qCDebug(KDEV_Meson) << "Removing current build directory";
m_ui->i_buildDirs->blockSignals(true);
m_ui->i_buildDirs->removeItem(m_config.currentIndex);
m_config.removeBuildDir(m_config.currentIndex);
if (m_config.buildDirs.isEmpty()) {
m_config.currentIndex = -1;
} else if (m_config.currentIndex < 0 || m_config.currentIndex >= m_config.buildDirs.size()) {
m_config.currentIndex = 0;
}
m_ui->i_buildDirs->setCurrentIndex(m_config.currentIndex);
m_ui->i_buildDirs->blockSignals(false);
reset();
writeConfig();
}
void MesonConfigPage::changeBuildDirIndex(int index)
{
if (index == m_config.currentIndex || m_config.buildDirs.isEmpty()) {
return;
}
if (index < 0 || index >= m_config.buildDirs.size()) {
qCWarning(KDEV_Meson) << "Invalid build dir index " << index;
return;
}
qCDebug(KDEV_Meson) << "Changing build directory to index " << index;
m_config.currentIndex = index;
reset();
writeConfig();
}
void MesonConfigPage::emitChanged()
{
m_configChanged = true;
checkStatus();
emit changed();
}
QString MesonConfigPage::name() const
{
return i18nc("@title:tab", "Meson");
}
QString MesonConfigPage::fullName() const
{
return i18nc("@title:tab", "Meson Project Configuration");
}
QIcon MesonConfigPage::icon() const
{
return QIcon::fromTheme(QStringLiteral("run-build"));
}
|