qmakebuilderpreferences.cpp 6.65 KB
Newer Older
1
2
3
/* KDevelop QMake Support
 *
 * Copyright 2007 Andreas Pakulat <apaku@gmx.de>
4
 * Copyright 2014 Kevin Funk <kfunk@kde.org>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "qmakebuilderpreferences.h"

Kevin Funk's avatar
Kevin Funk committed
24
#include <QIcon>
25

Kevin Funk's avatar
Kevin Funk committed
26
#include <kio/deletejob.h>
Kevin Funk's avatar
Kevin Funk committed
27
#include <klocalizedstring.h>
28
#include <KMessageBox>
Kevin Funk's avatar
Kevin Funk committed
29
#include <KJobWidgets>
30
31

#include "ui_qmakeconfig.h"
Kevin Funk's avatar
Kevin Funk committed
32
33
34
#include "qmakebuilddirchooser.h"
#include "qmakebuilddirchooserdialog.h"
#include "qmakeconfig.h"
35
#include "qmakebuilderconfig.h"
36
#include <debug.h>
37

38
39
#include <interfaces/iproject.h>

40
41
42
43
QMakeBuilderPreferences::QMakeBuilderPreferences(KDevelop::IPlugin* plugin,
                                                 const KDevelop::ProjectConfigOptions& options, QWidget* parent)
    : KDevelop::ConfigPage(plugin, nullptr, parent)
    , m_project(options.project)
44
45
{
    m_prefsUi = new Ui::QMakeConfig;
46
    m_prefsUi->setupUi(this);
47
48

    // display icons instead of text
49
    m_prefsUi->addButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
50
    m_prefsUi->addButton->setText(QString());
51
    m_prefsUi->removeButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
52
53
    m_prefsUi->removeButton->setText(QString());

Kevin Funk's avatar
Kevin Funk committed
54
55
56
57
    m_chooserUi = new QMakeBuildDirChooser(m_project);
    auto groupBoxLayout = new QVBoxLayout(m_prefsUi->groupBox);
    groupBoxLayout->addWidget(m_chooserUi);

58
    m_chooserUi->kcfg_buildDir->setEnabled(false); // build directory MUST NOT be changed here
Kevin Funk's avatar
Kevin Funk committed
59
60
    connect(m_chooserUi, &QMakeBuildDirChooser::changed, this, &QMakeBuilderPreferences::changed);
    connect(m_chooserUi, &QMakeBuildDirChooser::changed, this, &QMakeBuilderPreferences::validate);
61
62

    connect(m_prefsUi->buildDirCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(loadOtherConfig(QString)));
63
64
    connect(m_prefsUi->buildDirCombo, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
            &QMakeBuilderPreferences::changed);
65
66
    connect(m_prefsUi->addButton, &QAbstractButton::pressed, this, &QMakeBuilderPreferences::addBuildConfig);
    connect(m_prefsUi->removeButton, &QAbstractButton::pressed, this, &QMakeBuilderPreferences::removeBuildConfig);
67

68
    reset(); // load initial values
69
70
71
72
}

QMakeBuilderPreferences::~QMakeBuilderPreferences()
{
73
74
    // not a QObject !
    delete m_chooserUi;
75
76
}

77
void QMakeBuilderPreferences::reset()
78
{
Laurent Navet's avatar
Laurent Navet committed
79
    qCDebug(KDEV_QMAKEBUILDER) << "loading data";
80
    // refresh combobox
81
    KConfigGroup cg(m_project->projectConfiguration(), QMakeConfig::CONFIG_GROUP);
82
    const QString buildPath = cg.readEntry(QMakeConfig::BUILD_FOLDER, QString());
83
84
85
86
87

    // update build list (this will trigger loadOtherConfig if signals are still connected)
    disconnect(m_prefsUi->buildDirCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(loadOtherConfig(QString)));
    m_prefsUi->buildDirCombo->clear();
    m_prefsUi->buildDirCombo->insertItems(0, cg.groupList());
88
89
90
91
    if (m_prefsUi->buildDirCombo->contains(buildPath)) {
        m_prefsUi->buildDirCombo->setCurrentItem(buildPath);
        m_chooserUi->loadConfig(buildPath);
    }
Laurent Navet's avatar
Laurent Navet committed
92
    qCDebug(KDEV_QMAKEBUILDER) << "Loaded" << cg.groupList() << buildPath;
93
94
    m_prefsUi->removeButton->setEnabled(m_prefsUi->buildDirCombo->count() > 1);
    connect(m_prefsUi->buildDirCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(loadOtherConfig(QString)));
95
96

    validate();
97
98
}

99
100
101
102
QString QMakeBuilderPreferences::name() const
{
    return i18n("QMake");
}
103

104
void QMakeBuilderPreferences::apply()
105
{
Laurent Navet's avatar
Laurent Navet committed
106
    qCDebug(KDEV_QMAKEBUILDER) << "Saving data";
107
108
    QString errormsg;

109
    if (m_chooserUi->validate(&errormsg)) {
110
        // data is valid: save, once in the build dir's data and also as current data
111
        m_chooserUi->saveConfig();
112
        KConfigGroup config(m_project->projectConfiguration(), QMakeConfig::CONFIG_GROUP);
113
114
        m_chooserUi->saveConfig(config);
        config.writeEntry(QMakeConfig::BUILD_FOLDER, m_chooserUi->buildDir());
115
    } else {
116
        // invalid data: message box
117
        KMessageBox::error(nullptr, errormsg, QStringLiteral("Data is invalid!"));
patrick pereira's avatar
patrick pereira committed
118
        // FIXME dialog behaves like if save really happened (dialog closes if user click ok) even if changed signal is
119
        // emitted
120
121
122
123
124
    }
}

void QMakeBuilderPreferences::validate()
{
Kevin Funk's avatar
Kevin Funk committed
125
    m_chooserUi->validate();
126
}
127

128
129
void QMakeBuilderPreferences::loadOtherConfig(const QString& config)
{
130
    qCDebug(KDEV_QMAKEBUILDER) << "Loading config " << config;
131
    m_chooserUi->loadConfig(config);
132
    apply(); // since current config has changed, it must be saved immediateley
133
134
135
136
}

void QMakeBuilderPreferences::addBuildConfig()
{
Laurent Navet's avatar
Laurent Navet committed
137
    qCDebug(KDEV_QMAKEBUILDER) << "Adding a new config.";
138
    // for more simpicity, just launch regular dialog
139
    auto dlg = new QMakeBuildDirChooserDialog(m_project);
140
    if (dlg->exec() == QDialog::Accepted) {
141
142
        m_prefsUi->buildDirCombo->setCurrentItem(dlg->buildDir(), true);
        m_prefsUi->removeButton->setEnabled(m_prefsUi->buildDirCombo->count() > 1);
143
        // TODO run qmake
144
145
146
147
148
    }
}

void QMakeBuilderPreferences::removeBuildConfig()
{
Laurent Navet's avatar
Laurent Navet committed
149
    qCDebug(KDEV_QMAKEBUILDER) << "Removing config" << m_prefsUi->buildDirCombo->currentText();
150
    QString removed = m_prefsUi->buildDirCombo->currentText();
151
    KConfigGroup cg(m_project->projectConfiguration(), QMakeConfig::CONFIG_GROUP);
152
153

    m_prefsUi->buildDirCombo->removeItem(m_prefsUi->buildDirCombo->currentIndex());
154
    m_prefsUi->removeButton->setEnabled(m_prefsUi->buildDirCombo->count() > 1);
155
156
    cg.group(removed).deleteGroup(KConfigBase::Persistent);

157
158
159
160
161
    if (QDir(removed).exists()) {
        int ret = KMessageBox::warningYesNo(this, i18n("The %1 directory is about to be removed in KDevelop's list.\n"
                                                       "Do you want KDevelop to remove it in the file system as well?",
                                                       removed));
        if (ret == KMessageBox::Yes) {
Kevin Funk's avatar
Kevin Funk committed
162
163
            auto deleteJob = KIO::del(QUrl::fromLocalFile(removed));
            KJobWidgets::setWindow(deleteJob, this);
164
            if (!deleteJob->exec())
Pino Toscano's avatar
Pino Toscano committed
165
                KMessageBox::error(this, i18n("Could not remove: %1.", removed));
166
167
168
        }
    }
}