1 /* This file is part of KDevelop
2 * Copyright (C) 2008 Cédric Pasteur <cedric.pasteur@free.fr>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
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 GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "sourceformattersettings.h"
22 #include <QVBoxLayout>
24 #include <QListWidgetItem>
25 #include <QInputDialog>
26 #include <KMessageBox>
27 #include <KIconLoader>
28 #include <KGenericFactory>
29 #include <ktexteditor/document.h>
30 #include <ktexteditor/view.h>
31 #include <ktexteditor/editor.h>
32 #include <ktexteditor/editorchooser.h>
33 #include <ktexteditor/configinterface.h>
35 #include <interfaces/iplugin.h>
36 #include <util/interfaces/isourceformatter.h>
37 #include <util/sourceformattermanager.h>
39 #include "editstyledialog.h"
41 #define STYLE_ROLE (Qt::UserRole+1)
43 K_PLUGIN_FACTORY(SourceFormatterSettingsFactory
, registerPlugin
<SourceFormatterSettings
>();)
44 K_EXPORT_PLUGIN(SourceFormatterSettingsFactory("kcm_kdevsourceformattersettings"))
46 SourceFormatterSettings::SourceFormatterSettings(QWidget
*parent
, const QVariantList
&args
)
47 : KCModule(SourceFormatterSettingsFactory::componentData(), parent
, args
)
54 SourceFormatterSettings::~SourceFormatterSettings()
58 void SourceFormatterSettings::init()
60 // add texteditor preview
61 KTextEditor::Editor
*editor
= KTextEditor::EditorChooser::editor();
63 KMessageBox::error(this, i18n("A KDE text-editor component could not be found.\n"
64 "Please check your KDE installation."));
66 m_document
= editor
->createDocument(this);
68 m_view
= qobject_cast
<KTextEditor::View
*>(m_document
->createView(textEditor
));
69 QVBoxLayout
*layout
= new QVBoxLayout(textEditor
);
70 layout
->addWidget(m_view
);
71 textEditor
->setLayout(layout
);
74 KTextEditor::ConfigInterface
*iface
=
75 qobject_cast
<KTextEditor::ConfigInterface
*>(m_view
);
77 iface
->setConfigValue("dynamic-word-wrap", false);
78 iface
->setConfigValue("icon-bar", false);
81 //init language combo box
82 SourceFormatterManager
*manager
= SourceFormatterManager::self();
83 QStringList l
= manager
->languages();
84 foreach(QString s
, l
) {
85 //! todo add real icons to support
86 QString mime
= manager
->mimeTypeForLanguage(s
).replace('/', '-');
88 cbLanguagesStyle
->addItem(icon
, s
);
89 cbLanguagesFormatters
->addItem(icon
, s
);
93 btnNewStyle
->setIcon(KIcon("list-add"));
94 btnDelStyle
->setIcon(KIcon("list-remove"));
95 btnEditStyle
->setIcon(KIcon("configure"));
98 connect(tabWidget
, SIGNAL(currentChanged(QWidget
*)), this, SLOT(currentTabChanged()));
99 connect(cbLanguagesStyle
, SIGNAL(currentIndexChanged(int)), this, SLOT(languagesStylesChanged(int)));
100 connect(listStyles
, SIGNAL(currentItemChanged(QListWidgetItem
*, QListWidgetItem
*)),
101 this, SLOT(currentStyleChanged(QListWidgetItem
*, QListWidgetItem
*)));
102 connect(listStyles
, SIGNAL(itemChanged(QListWidgetItem
*)),
103 this, SLOT(styleRenamed(QListWidgetItem
*)));
104 connect(btnDelStyle
, SIGNAL(clicked()), this, SLOT(deleteStyle()));
105 connect(btnNewStyle
, SIGNAL(clicked()), this, SLOT(addStyle()));
106 connect(btnEditStyle
, SIGNAL(clicked()), this, SLOT(editStyle()));
107 connect(chkKateModelines
, SIGNAL(stateChanged(int)), this, SLOT(modelineChanged()));
109 // connect: second tab
110 connect(cbLanguagesFormatters
, SIGNAL(currentIndexChanged(int)), this, SLOT(languagesFormattersChanged(int)));
111 connect(cbFormatters
, SIGNAL(currentIndexChanged(int)), this, SLOT(formattersChanged(int)));
114 void SourceFormatterSettings::addItemInStyleList(const QString
&caption
, const QString
&name
, bool editable
)
116 QListWidgetItem
*item
= new QListWidgetItem(caption
);
117 item
->setData(STYLE_ROLE
, name
);
119 item
->setFlags(Qt::ItemIsEditable
| Qt::ItemIsEnabled
| Qt::ItemIsSelectable
);
120 listStyles
->addItem(item
);
123 void SourceFormatterSettings::updatePreviewText()
125 if (m_currentFormatter
) {
126 m_document
->setReadWrite(true);
127 m_document
->setText(m_currentFormatter
->formatSource(m_previewText
, m_currentMimeType
));
128 m_document
->setReadWrite(false);
132 void SourceFormatterSettings::load()
134 SourceFormatterManager::self()->loadConfig(); //reload
135 cbLanguagesStyle
->setCurrentIndex(0); // select the first item to fill the rest
136 cbLanguagesFormatters
->setCurrentIndex(0);
137 languagesStylesChanged(0);
138 languagesFormattersChanged(0);
139 //update kate modeline
140 chkKateModelines
->setChecked(SourceFormatterManager::self()->modelinesEnabled());
145 void SourceFormatterSettings::save()
147 // save current styles
148 KConfigGroup group
= SourceFormatterManager::self()->configGroup().parent().parent();
149 StyleHash::const_iterator it
= m_currentStyles
.constBegin();
150 for (; it
!= m_currentStyles
.constEnd(); ++it
) {
151 QHash
<QString
, QString
> hash
= it
.value();
152 QString lang
= it
.key();
153 kDebug() << "Saving lang " << lang
<< " hash " << hash
<< endl
;
154 QHash
<QString
, QString
>::const_iterator it2
= hash
.constBegin();
155 for (; it2
!= hash
.constEnd(); ++it2
) {
156 kDebug() << "Saving lang " << lang
<< " plugin " << it2
.key() << " style = " << it2
.value() << endl
;
157 group
.group(lang
).group(it2
.key()).writeEntry("Style", it2
.value());
161 SourceFormatterManager::self()->setModelinesEnabled(chkKateModelines
->isChecked());
162 SourceFormatterManager::self()->saveConfig();
165 void SourceFormatterSettings::currentTabChanged()
167 int idx
= tabWidget
->currentIndex();
168 if (idx
== 0) //style tab shown
169 cbLanguagesStyle
->setCurrentIndex(cbLanguagesFormatters
->currentIndex()); // trigger reload of style list
171 cbLanguagesFormatters
->setCurrentIndex(cbLanguagesStyle
->currentIndex());
174 void SourceFormatterSettings::languagesStylesChanged(int idx
)
176 if (idx
< 0) // no selection
178 //update source formatter
179 setActiveLanguage(cbLanguagesStyle
->currentText(), QString());
182 // reload current style
183 // QString currentStyle = SourceFormatterManager::self()->currentStyle();
184 ISourceFormatter
*f
= SourceFormatterManager::self()->activeFormatter();
185 QString currentStyle
;
187 currentStyle
= m_currentStyles
[m_currentLang
][f
->name()];
188 if(currentStyle
.isEmpty())
189 currentStyle
= SourceFormatterManager::self()->currentStyle();
190 kDebug() << "Trying to select " << currentStyle
<< endl
;
193 for (int i
= 0; i
< listStyles
->count(); ++i
) {
194 QListWidgetItem
*item
= listStyles
->item(i
);
195 if (item
->data(STYLE_ROLE
).toString() == currentStyle
)
198 kDebug() << "Selected index is " << selectedRow
<< endl
;
199 listStyles
->setCurrentRow(selectedRow
);
204 void SourceFormatterSettings::populateStyleList()
207 //add predefined styles
208 QMap
<QString
, QString
> map
= m_currentFormatter
->predefinedStyles(m_currentMimeType
);
209 QMap
<QString
, QString
>::const_iterator it
= map
.constBegin();
210 for (; it
!= map
.constEnd(); ++it
)
211 addItemInStyleList(it
.value(), it
.key());
212 m_numberOfPredefinedStyles
= map
.count();
215 KConfigGroup pluginGroup
= SourceFormatterManager::self()->configGroup();
216 QStringList keyList
= pluginGroup
.keyList();
217 foreach(QString key
, keyList
) {
218 if (key
.startsWith("User")) { // style definition
219 QString caption
= pluginGroup
.readEntry("Caption" + key
.mid(4));
220 addItemInStyleList(caption
, key
);
221 kDebug() << "Adding item in list: user" << key
.mid(4)
222 << " " << caption
<< endl
;
227 void SourceFormatterSettings::currentStyleChanged(QListWidgetItem
*current
, QListWidgetItem
*)
231 SourceFormatterManager
*manager
= SourceFormatterManager::self();
233 QString styleName
= current
->data(STYLE_ROLE
).toString();
234 manager
->setCurrentStyle(styleName
);
235 // save current style
236 QHash
<QString
, QString
> &hash
= m_currentStyles
[manager
->activeLanguage()];
237 if(manager
->activeFormatter())
238 hash
[manager
->activeFormatter()->name()] = styleName
;
240 if (listStyles
->currentRow() < m_numberOfPredefinedStyles
) {
241 btnDelStyle
->setEnabled(false);
242 btnEditStyle
->setEnabled(false);
244 btnDelStyle
->setEnabled(true);
245 btnEditStyle
->setEnabled(true);
251 void SourceFormatterSettings::styleRenamed(QListWidgetItem
*item
)
256 void SourceFormatterSettings::deleteStyle()
259 int res
= KMessageBox::questionYesNo(this, i18n("Are you sure you"
260 " want to delete this style?", i18n("Delete style")));
261 if (res
== KMessageBox::No
)
265 int idx
= listStyles
->currentRow();
266 QListWidgetItem
*item
= listStyles
->takeItem(idx
);
270 QString styleName
= item
->data(STYLE_ROLE
).toString();
271 SourceFormatterManager::self()->deleteStyle(styleName
);
272 listStyles
->setCurrentRow(idx
- 1);
275 void SourceFormatterSettings::addStyle()
279 QString caption
= QInputDialog::getText(this,
280 i18n("New style"), i18n("Please enter a name for the new style"),
281 QLineEdit::Normal
, i18n("Custom Style"), &ok
);
282 if (!ok
) // dialog aborted
285 EditStyleDialog
dialog(m_currentFormatter
, m_currentMimeType
);
286 if (dialog
.exec() == QDialog::Accepted
) {
287 SourceFormatterManager
*manager
= SourceFormatterManager::self();
288 QString name
= manager
->nameForNewStyle();
289 manager
->saveStyle(name
, dialog
.content());
290 manager
->renameStyle(name
, caption
);
291 // add item in list and select it
292 addItemInStyleList(caption
, name
, true);
293 listStyles
->setCurrentRow(listStyles
->count() - 1);
299 void SourceFormatterSettings::editStyle()
301 QListWidgetItem
*item
= listStyles
->currentItem();
304 QString styleName
= item
->data(STYLE_ROLE
).toString();
306 SourceFormatterManager
*manager
= SourceFormatterManager::self();
307 QString content
= manager
->configGroup().readEntry(styleName
);
308 EditStyleDialog
dialog(m_currentFormatter
, m_currentMimeType
, content
);
309 if (dialog
.exec() == QDialog::Accepted
)
310 SourceFormatterManager::self()->saveStyle(styleName
, dialog
.content());
313 void SourceFormatterSettings::modelineChanged()
318 void SourceFormatterSettings::languagesFormattersChanged(int idx
)
320 if (idx
< 0) // no selection
322 //update source formatter
323 setActiveLanguage(cbLanguagesFormatters
->currentText(), QString());
325 poulateFormattersList(); // will call setActiveLanguage
326 // updatePreviewText();
329 void SourceFormatterSettings::poulateFormattersList()
331 cbFormatters
->blockSignals(true);
332 cbFormatters
->clear();
334 SourceFormatterManager
*manager
= SourceFormatterManager::self();
335 QList
<KDevelop::IPlugin
*> list
= manager
->pluginListForLanguage(cbLanguagesFormatters
->currentText());
338 foreach(KDevelop::IPlugin
*plugin
, list
) {
339 ISourceFormatter
*formatter
= plugin
->extension
<ISourceFormatter
>();
340 cbFormatters
->addItem(formatter
->caption(), formatter
->name());
341 if (m_currentFormatter
&& (formatter
->name() == m_currentFormatter
->name())) {
342 rowToSelect
= cbFormatters
->count() - 1;
343 kDebug() << "Selecting " << rowToSelect
<< formatter
->name() << endl
;
346 cbFormatters
->blockSignals(false);
347 cbFormatters
->setCurrentIndex(rowToSelect
);
350 void SourceFormatterSettings::formattersChanged(int idx
)
352 // update formatter for this language
353 QString lang
= cbLanguagesFormatters
->currentText();
354 QString name
= cbFormatters
->itemData(idx
).toString();
355 setActiveLanguage(lang
, name
);
358 //update description label
359 lblDescription
->setText(m_currentFormatter
->description());
364 void SourceFormatterSettings::setActiveLanguage(const QString
&lang
, const QString
&plugin
)
366 kDebug() << "lang = " << lang
<< " plugin = " << plugin
<< endl
;
367 SourceFormatterManager
*manager
= SourceFormatterManager::self();
368 manager
->setActiveLanguage(lang
, plugin
);
370 m_currentFormatter
= manager
->activeFormatter();
371 m_currentMimeType
= KMimeType::mimeType(manager
->mimeTypeForLanguage(lang
));
372 m_previewText
= m_currentFormatter
->previewText(m_currentMimeType
);
373 QString mode
= m_currentFormatter
->highlightModeForMime(m_currentMimeType
);
374 m_document
->setHighlightingMode(mode
);
375 m_currentLang
= lang
;
378 #include "sourceformattersettings.moc"
379 // kate: indent-mode cstyle; space-indent off; tab-width 4;