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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
/* This file is part of KDevelop
Copyright 2019 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 "mesonrewriterpage.h"
#include "mesonconfig.h"
#include "mesonmanager.h"
#include "mesonoptionbaseview.h"
#include "mesonrewriterinput.h"
#include "mintro/mesonintrospectjob.h"
#include "mintro/mesonprojectinfo.h"
#include "rewriter/mesondefaultopts.h"
#include "rewriter/mesonkwargsinfo.h"
#include "rewriter/mesonkwargsmodify.h"
#include "rewriter/mesonrewriterjob.h"
#include "ui_mesonrewriterpage.h"
#include <interfaces/iplugin.h>
#include <interfaces/iproject.h>
#include <util/executecompositejob.h>
#include <KColorScheme>
#include <QIcon>
#include <QInputDialog>
#include <algorithm>
#include <debug.h>
using namespace KDevelop;
using namespace std;
class JobDeleter
{
public:
explicit JobDeleter(QList<KJob*> jobs)
: m_jobs(jobs)
{
}
~JobDeleter()
{
for (KJob* i : m_jobs) {
delete i;
}
}
private:
QList<KJob*> m_jobs;
};
MesonRewriterPage::MesonRewriterPage(IPlugin* plugin, IProject* project, QWidget* parent)
: ConfigPage(plugin, nullptr, parent)
, m_project(project)
{
Q_ASSERT(m_project);
m_ui = new Ui::MesonRewriterPage;
m_ui->setupUi(this);
m_projectKwargs = constructPojectInputs();
// Initialize widgets
for (auto* i : m_projectKwargs) {
m_ui->c_project->addWidget(i);
connect(i, &MesonRewriterInputBase::configChanged, this, &MesonRewriterPage::emitChanged);
}
recalculateLengths();
reset();
}
#define STRING_INPUT(name, id) new MesonRewriterInputString(QStringLiteral(name), QStringLiteral(id), this)
QVector<MesonRewriterInputBase*> MesonRewriterPage::constructPojectInputs()
{
return {
STRING_INPUT("Version", "version"),
STRING_INPUT("License", "license"),
STRING_INPUT("Meson version", "meson_version"),
STRING_INPUT("Subprojects directory", "subproject_dir"),
};
}
MesonOptContainerPtr MesonRewriterPage::constructDefaultOpt(const QString& name, const QString& value)
{
if (!m_opts) {
return nullptr;
}
for (auto& i : m_opts->options()) {
if (i->name() != name) {
continue;
}
if (!value.isNull()) {
i->setFromString(value);
}
auto optView = MesonOptionBaseView::fromOption(i, this);
if (!optView) {
continue;
}
auto opt = std::make_shared<MesonRewriterOptionContainer>(optView, this);
if (!opt) {
continue;
}
connect(opt.get(), &MesonRewriterOptionContainer::configChanged, this, &MesonRewriterPage::emitChanged);
return opt;
}
return nullptr;
}
void MesonRewriterPage::setWidgetsDisabled(bool disabled)
{
m_ui->c_tabs->setDisabled(disabled);
}
void MesonRewriterPage::recalculateLengths()
{
// Calculate the maximum name width to align all widgets
vector<int> lengths;
int maxWidth = 50;
lengths.reserve(m_projectKwargs.size() + m_defaultOpts.size());
auto input_op = [](MesonRewriterInputBase* x) -> int { return x->nameWidth(); };
auto optCont_op = [](MesonOptContainerPtr x) -> int { return x->view()->nameWidth(); };
transform(begin(m_projectKwargs), end(m_projectKwargs), back_inserter(lengths), input_op);
transform(begin(m_defaultOpts), end(m_defaultOpts), back_inserter(lengths), optCont_op);
maxWidth = accumulate(begin(lengths), end(lengths), maxWidth, [](int a, int b) -> int { return max(a, b); });
// Set widgets width
for (auto* i : m_projectKwargs) {
i->setMinNameWidth(maxWidth);
}
for (auto& i : m_defaultOpts) {
i->view()->setMinNameWidth(maxWidth);
}
m_ui->l_dispProject->setMinimumWidth(maxWidth);
}
void MesonRewriterPage::checkStatus()
{
// Get the config build dir status
auto setStatus = [this](const QString& msg, int color) -> void {
KColorScheme scheme(QPalette::Normal);
KColorScheme::ForegroundRole role;
switch (color) {
case 0:
role = KColorScheme::PositiveText;
setDisabled(false);
break;
case 1:
role = KColorScheme::NeutralText;
setDisabled(true);
break;
case 2:
default:
role = KColorScheme::NegativeText;
setDisabled(true);
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 (m_state) {
case START:
setStatus(i18n("Initializing GUI"), 1);
break;
case LOADING:
setStatus(i18n("Loading project data..."), 1);
break;
case WRITING:
setStatus(i18n("Writing project data..."), 1);
break;
case READY:
setStatus(i18n("Initializing GUI"), 0);
break;
case ERROR:
setStatus(i18n("Loading meson rewriter data failed"), 2);
break;
}
// Remove old default options
m_defaultOpts.erase(remove_if(begin(m_defaultOpts), end(m_defaultOpts), [](auto x) { return x->shouldDelete(); }),
end(m_defaultOpts));
KColorScheme scheme(QPalette::Normal);
KColorScheme::ForegroundRole role;
int numChanged = 0;
numChanged += count_if(begin(m_projectKwargs), end(m_projectKwargs), [](auto* x) { return x->hasChanged(); });
numChanged += count_if(begin(m_defaultOpts), end(m_defaultOpts), [](auto x) { return x->hasChanged(); });
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 MesonRewriterPage::setStatus(MesonRewriterPage::State s)
{
m_state = s;
checkStatus();
}
void MesonRewriterPage::apply()
{
qCDebug(KDEV_Meson) << "REWRITER GUI: APPLY";
auto projectSet = make_shared<MesonKWARGSProjectModify>(MesonKWARGSProjectModify::SET);
auto projectDel = make_shared<MesonKWARGSProjectModify>(MesonKWARGSProjectModify::DELETE);
auto defOptsSet = make_shared<MesonRewriterDefaultOpts>(MesonRewriterDefaultOpts::SET);
auto defOptsDel = make_shared<MesonRewriterDefaultOpts>(MesonRewriterDefaultOpts::DELETE);
auto writer = [](MesonRewriterInputBase* widget, MesonKWARGSModifyPtr set, MesonKWARGSModifyPtr del) {
if (!widget->hasChanged()) {
return;
}
if (widget->isEnabled()) {
widget->writeToAction(set.get());
} else {
widget->writeToAction(del.get());
}
};
for_each(begin(m_projectKwargs), end(m_projectKwargs), [&](auto* w) { writer(w, projectSet, projectDel); });
QStringList deletedOptions = m_initialDefaultOpts;
for (auto& i : m_defaultOpts) {
auto opt = i->view()->option();
// Detect deleted options by removing all current present options from the initial option list
deletedOptions.removeAll(opt->name());
if (opt->isUpdated() || !m_initialDefaultOpts.contains(opt->name())) {
defOptsSet->set(opt->name(), opt->value());
}
}
for (auto i : deletedOptions) {
defOptsDel->set(i, QString());
}
QVector<MesonRewriterActionPtr> actions = { projectSet, projectDel, defOptsSet, defOptsDel };
KJob* rewriterJob = new MesonRewriterJob(m_project, actions, this);
// Reload the GUI once the data has been written
connect(rewriterJob, &KJob::result, this, &MesonRewriterPage::reset);
setStatus(WRITING);
rewriterJob->start();
}
void MesonRewriterPage::reset()
{
qCDebug(KDEV_Meson) << "REWRITER GUI: RESET";
Meson::BuildDir buildDir = Meson::currentBuildDir(m_project);
if (!buildDir.isValid()) {
setStatus(ERROR);
return;
}
auto projectInfo = std::make_shared<MesonKWARGSProjectInfo>();
QVector<MesonRewriterActionPtr> actions = { projectInfo };
QVector<MesonIntrospectJob::Type> types = { MesonIntrospectJob::PROJECTINFO, MesonIntrospectJob::BUILDOPTIONS };
MesonIntrospectJob::Mode mode = MesonIntrospectJob::MESON_FILE;
auto introspectJob = new MesonIntrospectJob(m_project, buildDir, types, mode, this);
auto rewriterJob = new MesonRewriterJob(m_project, actions, this);
QList<KJob*> jobs = { introspectJob, rewriterJob };
// Don't automatically delete jobs beause they are used in the lambda below
for (KJob* i : jobs) {
i->setAutoDelete(false);
}
KJob* job = new ExecuteCompositeJob(this, jobs);
connect(job, &KJob::result, this, [=]() -> void {
JobDeleter deleter(jobs); // Make sure to free all jobs with RAII
auto prInfo = introspectJob->projectInfo();
m_opts = introspectJob->buildOptions();
if (!prInfo || !m_opts) {
setStatus(ERROR);
return;
}
m_ui->l_project->setText(QStringLiteral("<html><head/><body><h3>") + prInfo->name()
+ QStringLiteral("</h3></body></html>"));
auto setter = [](MesonRewriterInputBase* w, MesonKWARGSInfoPtr i) { w->resetFromAction(i.get()); };
for_each(begin(m_projectKwargs), end(m_projectKwargs), [=](auto* x) { setter(x, projectInfo); });
// Updated the default options
m_defaultOpts.clear();
m_initialDefaultOpts.clear();
if (projectInfo->hasKWARG(QStringLiteral("default_options"))) {
auto rawValues = projectInfo->getArray(QStringLiteral("default_options"));
auto options = m_opts->options();
for (auto i : rawValues) {
int idx = i.indexOf(QLatin1Char('='));
if (idx < 0) {
continue;
}
QString name = i.left(idx);
QString val = i.mid(idx + 1);
auto opt = constructDefaultOpt(name, val);
if (!opt) {
continue;
}
m_defaultOpts += opt;
m_initialDefaultOpts += name;
m_ui->c_defOpts->addWidget(opt.get());
}
}
recalculateLengths();
setStatus(READY);
return;
});
setStatus(LOADING);
job->start();
}
void MesonRewriterPage::newOption()
{
// Sort by section
QStringList core;
QStringList backend;
QStringList base;
QStringList compiler;
QStringList directory;
QStringList user;
QStringList test;
for (auto& i : m_opts->options()) {
switch (i->section()) {
case MesonOptionBase::CORE:
core += i->name();
break;
case MesonOptionBase::BACKEND:
backend += i->name();
break;
case MesonOptionBase::BASE:
base += i->name();
break;
case MesonOptionBase::COMPILER:
compiler += i->name();
break;
case MesonOptionBase::DIRECTORY:
directory += i->name();
break;
case MesonOptionBase::USER:
user += i->name();
break;
case MesonOptionBase::TEST:
test += i->name();
break;
}
}
QStringList total = core + backend + base + compiler + directory + user + test;
// Remove already existing options
for (auto& i : m_defaultOpts) {
total.removeAll(i->view()->option()->name());
}
QInputDialog dialog(this);
dialog.setOption(QInputDialog::UseListViewForComboBoxItems, true);
dialog.setInputMode(QInputDialog::TextInput);
dialog.setWindowTitle(i18nc("@title:window", "Select Additional Meson Option"));
dialog.setLabelText(i18nc("@label:listbox", "Meson option to add:"));
dialog.setComboBoxItems(total);
if (dialog.exec() != QDialog::Accepted) {
return;
}
auto opt = constructDefaultOpt(dialog.textValue(), QString());
if (!opt) {
return;
}
m_defaultOpts += opt;
m_ui->c_defOpts->addWidget(opt.get());
recalculateLengths();
}
void MesonRewriterPage::defaults()
{
reset();
}
void MesonRewriterPage::emitChanged()
{
m_configChanged = true;
checkStatus();
emit changed();
}
QString MesonRewriterPage::name() const
{
return i18nc("@title:tab", "Project");
}
QString MesonRewriterPage::fullName() const
{
return i18nc("@title:tab", "Meson Project Settings");
}
QIcon MesonRewriterPage::icon() const
{
return QIcon::fromTheme(QStringLiteral("run-build"));
}
|