qmakebuilderpreferences.cpp 6.35 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

26
27
#include <KIO/DeleteJob>
#include <KLocalizedString>
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

Kevin Funk's avatar
Kevin Funk committed
48
49
50
51
    m_chooserUi = new QMakeBuildDirChooser(m_project);
    auto groupBoxLayout = new QVBoxLayout(m_prefsUi->groupBox);
    groupBoxLayout->addWidget(m_chooserUi);

52
    m_chooserUi->kcfg_buildDir->setEnabled(false); // build directory MUST NOT be changed here
Kevin Funk's avatar
Kevin Funk committed
53
54
    connect(m_chooserUi, &QMakeBuildDirChooser::changed, this, &QMakeBuilderPreferences::changed);
    connect(m_chooserUi, &QMakeBuildDirChooser::changed, this, &QMakeBuilderPreferences::validate);
55

56
    connect(m_prefsUi->buildDirCombo, &QComboBox::currentTextChanged, this, &QMakeBuilderPreferences::loadOtherConfig);
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
57
    connect(m_prefsUi->buildDirCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
58
            &QMakeBuilderPreferences::changed);
59
60
    connect(m_prefsUi->addButton, &QAbstractButton::pressed, this, &QMakeBuilderPreferences::addBuildConfig);
    connect(m_prefsUi->removeButton, &QAbstractButton::pressed, this, &QMakeBuilderPreferences::removeBuildConfig);
61

62
    reset(); // load initial values
63
64
65
66
}

QMakeBuilderPreferences::~QMakeBuilderPreferences()
{
67
68
    // not a QObject !
    delete m_chooserUi;
69
70
}

71
void QMakeBuilderPreferences::reset()
72
{
Laurent Navet's avatar
Laurent Navet committed
73
    qCDebug(KDEV_QMAKEBUILDER) << "loading data";
74
    // refresh combobox
75
    KConfigGroup cg(m_project->projectConfiguration(), QMakeConfig::CONFIG_GROUP);
76
    const QString buildPath = cg.readEntry(QMakeConfig::BUILD_FOLDER, QString());
77
78

    // update build list (this will trigger loadOtherConfig if signals are still connected)
79
    disconnect(m_prefsUi->buildDirCombo, &QComboBox::currentTextChanged, this, &QMakeBuilderPreferences::loadOtherConfig);
80
81
    m_prefsUi->buildDirCombo->clear();
    m_prefsUi->buildDirCombo->insertItems(0, cg.groupList());
82
83
84
85
    if (m_prefsUi->buildDirCombo->contains(buildPath)) {
        m_prefsUi->buildDirCombo->setCurrentItem(buildPath);
        m_chooserUi->loadConfig(buildPath);
    }
Laurent Navet's avatar
Laurent Navet committed
86
    qCDebug(KDEV_QMAKEBUILDER) << "Loaded" << cg.groupList() << buildPath;
87
    m_prefsUi->removeButton->setEnabled(m_prefsUi->buildDirCombo->count() > 1);
88
    connect(m_prefsUi->buildDirCombo, &QComboBox::currentTextChanged, this, &QMakeBuilderPreferences::loadOtherConfig);
89
90

    validate();
91
92
}

93
94
95
96
QString QMakeBuilderPreferences::name() const
{
    return i18n("QMake");
}
97

98
void QMakeBuilderPreferences::apply()
99
{
Laurent Navet's avatar
Laurent Navet committed
100
    qCDebug(KDEV_QMAKEBUILDER) << "Saving data";
101
102
    QString errormsg;

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

void QMakeBuilderPreferences::validate()
{
Kevin Funk's avatar
Kevin Funk committed
119
    m_chooserUi->validate();
120
}
121

122
123
void QMakeBuilderPreferences::loadOtherConfig(const QString& config)
{
124
    qCDebug(KDEV_QMAKEBUILDER) << "Loading config " << config;
125
    m_chooserUi->loadConfig(config);
Yuri Chornoivan's avatar
Yuri Chornoivan committed
126
    apply(); // since current config has changed, it must be saved immediately
127
128
129
130
}

void QMakeBuilderPreferences::addBuildConfig()
{
Laurent Navet's avatar
Laurent Navet committed
131
    qCDebug(KDEV_QMAKEBUILDER) << "Adding a new config.";
Yuri Chornoivan's avatar
Yuri Chornoivan committed
132
    // for more simplicity, just launch regular dialog
133
    auto dlg = new QMakeBuildDirChooserDialog(m_project);
134
    if (dlg->exec() == QDialog::Accepted) {
135
136
        m_prefsUi->buildDirCombo->setCurrentItem(dlg->buildDir(), true);
        m_prefsUi->removeButton->setEnabled(m_prefsUi->buildDirCombo->count() > 1);
137
        // TODO run qmake
138
139
140
141
142
    }
}

void QMakeBuilderPreferences::removeBuildConfig()
{
Laurent Navet's avatar
Laurent Navet committed
143
    qCDebug(KDEV_QMAKEBUILDER) << "Removing config" << m_prefsUi->buildDirCombo->currentText();
144
    QString removed = m_prefsUi->buildDirCombo->currentText();
145
    KConfigGroup cg(m_project->projectConfiguration(), QMakeConfig::CONFIG_GROUP);
146
147

    m_prefsUi->buildDirCombo->removeItem(m_prefsUi->buildDirCombo->currentIndex());
148
    m_prefsUi->removeButton->setEnabled(m_prefsUi->buildDirCombo->count() > 1);
149
150
    cg.group(removed).deleteGroup(KConfigBase::Persistent);

151
152
153
154
155
    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
156
157
            auto deleteJob = KIO::del(QUrl::fromLocalFile(removed));
            KJobWidgets::setWindow(deleteJob, this);
158
            if (!deleteJob->exec())
Pino Toscano's avatar
Pino Toscano committed
159
                KMessageBox::error(this, i18n("Could not remove: %1.", removed));
160
161
162
        }
    }
}