~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to src/profileengine/editor/profileeditor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2004 by Alexander Dymo <adymo@kdevelop.org>             *
3
 
 *                                                                         *
4
 
 *   This program is free software; you can redistribute it and/or modify  *
5
 
 *   it under the terms of the GNU Library General Public License as       *
6
 
 *   published by the Free Software Foundation; either version 2 of the    *
7
 
 *   License, or (at your option) any later version.                       *
8
 
 *                                                                         *
9
 
 *   This program is distributed in the hope that it will be useful,       *
10
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12
 
 *   GNU General Public License for more details.                          *
13
 
 *                                                                         *
14
 
 *   You should have received a copy of the GNU Library General Public     *
15
 
 *   License along with this program; if not, write to the                 *
16
 
 *   Free Software Foundation, Inc.,                                       *
17
 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18
 
 ***************************************************************************/
19
 
#include "profileeditor.h"
20
 
 
21
 
#include <qlayout.h>
22
 
#include <klineedit.h>
23
 
#include <qtextedit.h>
24
 
#include <qpalette.h>
25
 
 
26
 
#include <kdebug.h>
27
 
#include <kpushbutton.h>
28
 
#include <klistbox.h>
29
 
#include <klistview.h>
30
 
#include <kcombobox.h>
31
 
#include <klocale.h>
32
 
#include <kmessagebox.h>
33
 
#include <kdialogbase.h>
34
 
#include <kglobalsettings.h>
35
 
#include <kdeversion.h>
36
 
 
37
 
#include <profile.h>
38
 
 
39
 
#include "addprofilewidget.h"
40
 
 
41
 
class ProfileItem: public KListViewItem {
42
 
public:
43
 
    ProfileItem(KListView *parent, Profile *profile)
44
 
        :KListViewItem(parent), m_profile(profile)
45
 
    {
46
 
        setText(0, profile->genericName());
47
 
        setText(1, profile->description());
48
 
    }
49
 
    
50
 
    ProfileItem(KListViewItem *parent, Profile *profile)
51
 
        : KListViewItem(parent), m_profile(profile)
52
 
    {
53
 
        setText(0, profile->genericName());
54
 
        setText(1, profile->description());
55
 
    }
56
 
    
57
 
    Profile *profile() const { return m_profile; }
58
 
    
59
 
private:
60
 
    Profile *m_profile;
61
 
};
62
 
 
63
 
class EDListItem: public KListViewItem{
64
 
public:
65
 
    EDListItem(KListView *parent, const QString &text, bool derived)
66
 
        : KListViewItem(parent, text), m_derived(derived)
67
 
    {
68
 
    }
69
 
    
70
 
    bool isDerived() const { return m_derived; }
71
 
 
72
 
    virtual void paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int alignment)
73
 
    {
74
 
        QColorGroup cgNew = cg;
75
 
        if (m_derived)
76
 
            cgNew.setColor(QColorGroup::Text, KGlobalSettings::inactiveTextColor());
77
 
        KListViewItem::paintCell(p, cgNew, column, width, alignment);
78
 
    }
79
 
    
80
 
private:
81
 
    bool m_derived;
82
 
};
83
 
 
84
 
 
85
 
class ProfileListBuilding {
86
 
public:
87
 
    ProfileItem * operator() (ProfileItem *parent, Profile *profile)
88
 
    {
89
 
        parent->setOpen(true);
90
 
        return new ProfileItem(parent, profile);
91
 
    }
92
 
};
93
 
 
94
 
 
95
 
ProfileEditor::ProfileEditor(QWidget *parent, const char *name)
96
 
    :ProfileEditorBase(parent, name)
97
 
{
98
 
    refresh();
99
 
}
100
 
 
101
 
void ProfileEditor::refresh()
102
 
{
103
 
    profilesList->clear();
104
 
    
105
 
    ProfileItem *item = new ProfileItem(profilesList, engine.rootProfile());
106
 
    ProfileListBuilding op;
107
 
    engine.walkProfiles<ProfileListBuilding, ProfileItem>(op, item, engine.rootProfile());
108
 
    
109
 
    profilesList->setSelected(item, true);
110
 
    profilesList->setCurrentItem(item);
111
 
    
112
 
    refreshAvailableList();
113
 
    refreshPropertyCombo();
114
 
}
115
 
 
116
 
void ProfileEditor::refreshPropertyCombo()
117
 
{
118
 
    KTrader::OfferList list = KTrader::self()->query(QString::fromLatin1("KDevelop/Plugin"));
119
 
    QStringList props;
120
 
    for (KTrader::OfferList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it)
121
 
    {
122
 
        QStringList currProps = (*it)->property("X-KDevelop-Properties").toStringList();
123
 
        for (QStringList::const_iterator p = currProps.constBegin(); 
124
 
                p != currProps.constEnd(); ++p)
125
 
            if (!props.contains(*p))
126
 
                props.append(*p);
127
 
    }
128
 
    props.sort();
129
 
    propertyCombo->insertStringList(props);
130
 
    propertyCombo->setCurrentText("");
131
 
}
132
 
 
133
 
void ProfileEditor::refreshAvailableList()
134
 
{
135
 
    //filling a list of available plugins
136
 
    allList->clear();
137
 
    allCore = new KListViewItem(allList, i18n("Core"));
138
 
    allCore->setOpen(true);
139
 
    allGlobal = new KListViewItem(allList, i18n("Global"));
140
 
    allGlobal->setOpen(true);
141
 
    allProject = new KListViewItem(allList, i18n("Project"));
142
 
    allProject->setOpen(true);
143
 
    
144
 
    KTrader::OfferList olist = engine.allOffers(ProfileEngine::Core);
145
 
    for (KTrader::OfferList::iterator it = olist.begin(); it != olist.end(); ++it)
146
 
        new KListViewItem(allCore, (*it)->desktopEntryName(), (*it)->genericName());
147
 
    olist = engine.allOffers(ProfileEngine::Global);
148
 
    for (KTrader::OfferList::iterator it = olist.begin(); it != olist.end(); ++it)
149
 
        new KListViewItem(allGlobal, (*it)->desktopEntryName(), (*it)->genericName());
150
 
    olist = engine.allOffers(ProfileEngine::Project);
151
 
    for (KTrader::OfferList::iterator it = olist.begin(); it != olist.end(); ++it)
152
 
        new KListViewItem(allProject, (*it)->desktopEntryName(), (*it)->genericName());
153
 
}
154
 
 
155
 
void ProfileEditor::profileExecuted(QListViewItem *item)
156
 
{    
157
 
    if (!item || item->text(0) == "KDevelop")
158
 
        removeProfileButton->setEnabled(false);
159
 
    else
160
 
        removeProfileButton->setEnabled(true);
161
 
 
162
 
    fillPropertyList(currentProfile());
163
 
    fillEDLists(currentProfile());
164
 
    fillPluginsList(currentProfile());
165
 
}
166
 
 
167
 
void ProfileEditor::fillPropertyList(Profile *profile)
168
 
{
169
 
    derivedPropertiesBox->clear();
170
 
    ownPropertiesBox->clear();
171
 
    
172
 
    Profile::EntryList list = profile->list(Profile::Properties);
173
 
    for (Profile::EntryList::const_iterator it = list.begin(); it != list.end(); ++it)
174
 
    {
175
 
        if ((*it).derived)
176
 
            derivedPropertiesBox->insertItem((*it).name);
177
 
        else
178
 
            ownPropertiesBox->insertItem((*it).name);
179
 
    }
180
 
}
181
 
 
182
 
void ProfileEditor::fillEDLists(Profile *profile)
183
 
{    
184
 
    //filling a list of enabled plugins
185
 
    enabledList->clear();
186
 
    Profile::EntryList list = profile->list(Profile::ExplicitEnable);
187
 
    for (Profile::EntryList::const_iterator it = list.begin(); it != list.end(); ++it)
188
 
        new EDListItem(enabledList, (*it).name, (*it).derived);
189
 
 
190
 
    //filling a list of disabled plugins
191
 
    disabledList->clear();
192
 
    list = profile->list(Profile::ExplicitDisable);
193
 
    for (Profile::EntryList::const_iterator it = list.begin(); it != list.end(); ++it)
194
 
        new EDListItem(disabledList, (*it).name, (*it).derived);
195
 
}
196
 
 
197
 
void ProfileEditor::fillPluginsList(Profile *profile)
198
 
{
199
 
    pluginsView->clear();
200
 
    
201
 
    KListViewItem *core = new KListViewItem(pluginsView, i18n("Core Plugins"));
202
 
    core->setOpen(true);
203
 
    KListViewItem *global = new KListViewItem(pluginsView, i18n("Global Plugins"));
204
 
    global->setOpen(true);
205
 
    KListViewItem *project = new KListViewItem(pluginsView, i18n("Project Plugins"));
206
 
    project->setOpen(true);
207
 
 
208
 
    KTrader::OfferList coreOffers = engine.offers(profile->name(), ProfileEngine::Core);
209
 
    for (KTrader::OfferList::const_iterator it = coreOffers.constBegin();
210
 
            it != coreOffers.constEnd(); ++it)
211
 
        new KListViewItem(core, (*it)->desktopEntryName(), (*it)->genericName(),
212
 
            (*it)->property("X-KDevelop-Properties").toStringList().join(", "));
213
 
        
214
 
    KTrader::OfferList globalOffers = engine.offers(profile->name(), ProfileEngine::Global);
215
 
    for (KTrader::OfferList::const_iterator it = globalOffers.constBegin();
216
 
            it != globalOffers.constEnd(); ++it)
217
 
        new KListViewItem(global, (*it)->desktopEntryName(), (*it)->genericName(),
218
 
            (*it)->property("X-KDevelop-Properties").toStringList().join(", "));
219
 
    
220
 
    KTrader::OfferList projectOffers = engine.offers(profile->name(), ProfileEngine::Project);
221
 
    for (KTrader::OfferList::const_iterator it = projectOffers.constBegin();
222
 
            it != projectOffers.constEnd(); ++it)
223
 
        new KListViewItem(project, (*it)->desktopEntryName(), (*it)->genericName(),
224
 
            (*it)->property("X-KDevelop-Properties").toStringList().join(", "));
225
 
}
226
 
 
227
 
void ProfileEditor::propertyExecuted(QListBoxItem *item)
228
 
{
229
 
    removePropertyButton->setEnabled(item != 0);
230
 
}
231
 
 
232
 
void ProfileEditor::addProfile()
233
 
{
234
 
    if (!profilesList->currentItem())
235
 
        return;
236
 
    
237
 
    KDialogBase dlg(KDialogBase::Plain, i18n("Add Profile"), KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok);
238
 
    dlg.plainPage()->setMargin(0);
239
 
    (new QVBoxLayout(dlg.plainPage(), 0, 0))->setAutoAdd(true);
240
 
    AddProfileWidget *prof = new AddProfileWidget(dlg.plainPage());
241
 
    prof->nameEdit->setFocus();
242
 
    if (dlg.exec() == QDialog::Accepted)
243
 
    {
244
 
        Profile *profile = new Profile(currentProfile(), prof->nameEdit->text(), 
245
 
            prof->genericNameEdit->text(), 
246
 
            prof->descriptionEdit->text());
247
 
        profilesList->currentItem()->setOpen(true);
248
 
        new ProfileItem(static_cast<KListViewItem*>(profilesList->currentItem()), profile);
249
 
    }
250
 
}
251
 
 
252
 
void ProfileEditor::removeProfile()
253
 
{
254
 
    if (KMessageBox::warningContinueCancel(this, i18n("Remove selected profile and all its subprofiles?"),
255
 
        i18n("Remove Profile"),KStdGuiItem::del()) == KMessageBox::Continue)
256
 
    {
257
 
        Profile *profile = currentProfile();
258
 
        if (profile->remove())
259
 
        {
260
 
            QListViewItem *item = profilesList->currentItem();
261
 
            profilesList->setCurrentItem(item->parent());
262
 
            profile->detachFromParent();
263
 
            delete profile;
264
 
            delete item;
265
 
        }
266
 
        else
267
 
            KMessageBox::error(this, i18n("Cannot remove this profile because it is not a local (user-created) profile."), i18n("Remove Profile"));
268
 
    }
269
 
}
270
 
 
271
 
void ProfileEditor::addProperty()
272
 
{
273
 
    if ( (!propertyCombo->currentText().isEmpty()) &&
274
 
        (ownPropertiesBox->findItem(propertyCombo->currentText()) == 0) &&
275
 
        (derivedPropertiesBox->findItem(propertyCombo->currentText()) == 0) )
276
 
    {
277
 
        ownPropertiesBox->insertItem(propertyCombo->currentText());
278
 
        
279
 
        currentProfile()->addEntry(Profile::Properties, propertyCombo->currentText());
280
 
        currentProfile()->save();
281
 
    }
282
 
    
283
 
    fillPluginsList(currentProfile());
284
 
}
285
 
 
286
 
void ProfileEditor::removeProperty()
287
 
{
288
 
    currentProfile()->removeEntry(Profile::Properties, ownPropertiesBox->currentText());
289
 
    currentProfile()->save();
290
 
    
291
 
    ownPropertiesBox->removeItem(ownPropertiesBox->currentItem());
292
 
 
293
 
    fillPluginsList(currentProfile());
294
 
}
295
 
 
296
 
Profile *ProfileEditor::currentProfile()
297
 
{
298
 
    ProfileItem *item = dynamic_cast<ProfileItem*>(profilesList->currentItem());
299
 
    if (!item)
300
 
        return 0;
301
 
    return item->profile();
302
 
}
303
 
 
304
 
void ProfileEditor::accept()
305
 
{
306
 
}
307
 
 
308
 
void ProfileEditor::addEnabled()
309
 
{
310
 
    if (!allList->currentItem() && allEdit->text().isEmpty())
311
 
        return;
312
 
    QString text;
313
 
    if (!allEdit->text().isEmpty())
314
 
    {
315
 
        text = allEdit->text();
316
 
        allEdit->clear();
317
 
    }
318
 
    else
319
 
    {
320
 
        if ((allList->currentItem() == allGlobal) || (allList->currentItem() == allProject))
321
 
            return;
322
 
        text = allList->currentItem()->text(0);
323
 
    }
324
 
    
325
 
    if (enabledList->findItem(text, 0) != 0)
326
 
        return;
327
 
    if (disabledList->findItem(text, 0) != 0)
328
 
    {
329
 
        KMessageBox::error(this, i18n("This plugin is already contained in the list of disabled plugins."),
330
 
            i18n("Enable Plugin"));
331
 
        return;
332
 
    }
333
 
    currentProfile()->addEntry(Profile::ExplicitEnable, text);
334
 
    currentProfile()->save();
335
 
    fillPluginsList(currentProfile());
336
 
    new EDListItem(enabledList, text, false);
337
 
}
338
 
 
339
 
void ProfileEditor::delEnabled()
340
 
{
341
 
    if (!enabledList->currentItem())
342
 
        return;
343
 
    
344
 
    EDListItem *item = dynamic_cast<EDListItem*>(enabledList->currentItem());
345
 
    if (item && !item->isDerived())
346
 
    {
347
 
        currentProfile()->removeEntry(Profile::ExplicitEnable, enabledList->currentItem()->text(0));
348
 
        currentProfile()->save();
349
 
        fillPluginsList(currentProfile());
350
 
        delete enabledList->currentItem();
351
 
    }
352
 
}
353
 
 
354
 
void ProfileEditor::addDisabled()
355
 
{
356
 
    if (!allList->currentItem() && allEdit->text().isEmpty())
357
 
        return;
358
 
    QString text;
359
 
    if (!allEdit->text().isEmpty())
360
 
    {
361
 
        text = allEdit->text();
362
 
        allEdit->clear();
363
 
    }
364
 
    else
365
 
    {
366
 
        if ((allList->currentItem() == allGlobal) || (allList->currentItem() == allProject))
367
 
            return;
368
 
        text = allList->currentItem()->text(0);
369
 
    }
370
 
    
371
 
    if (disabledList->findItem(text, 0) != 0)
372
 
        return;
373
 
    if (enabledList->findItem(text, 0) != 0)
374
 
    {
375
 
        KMessageBox::error(this, i18n("This plugin is already contained in the list of enabled plugins."),
376
 
            i18n("Disable Plugin"));
377
 
        return;
378
 
    }
379
 
    currentProfile()->addEntry(Profile::ExplicitDisable, text);
380
 
    currentProfile()->save();
381
 
    fillPluginsList(currentProfile());
382
 
    new EDListItem(disabledList, text, false);
383
 
}
384
 
 
385
 
void ProfileEditor::delDisabled()
386
 
{
387
 
    if (!disabledList->currentItem())
388
 
        return;
389
 
    
390
 
    EDListItem *item = dynamic_cast<EDListItem*>(disabledList->currentItem());
391
 
    if (item && !item->isDerived())
392
 
    {
393
 
        currentProfile()->removeEntry(Profile::ExplicitDisable, disabledList->currentItem()->text(0));
394
 
        delete disabledList->currentItem();
395
 
        currentProfile()->save();
396
 
        fillPluginsList(currentProfile());
397
 
    }
398
 
}
399
 
 
400
 
#include "profileeditor.moc"