Fix no newlines warnings. Patch by Peter Oberndorfer
[kdevelopdvcssupport.git] / plugins / sourceformatter / sourceformattersettings.cpp
blobc37182ff120de21c6f726b934c6a9f738fac3607
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>
23 #include <QList>
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)
49 setupUi(this);
51 init();
54 SourceFormatterSettings::~SourceFormatterSettings()
58 void SourceFormatterSettings::init()
60 // add texteditor preview
61 KTextEditor::Editor *editor = KTextEditor::EditorChooser::editor();
62 if (!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);
72 m_view->show();
74 KTextEditor::ConfigInterface *iface =
75 qobject_cast<KTextEditor::ConfigInterface*>(m_view);
76 if (iface) {
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('/', '-');
87 KIcon icon(mime);
88 cbLanguagesStyle->addItem(icon, s);
89 cbLanguagesFormatters->addItem(icon, s);
92 // set buttons icons
93 btnNewStyle->setIcon(KIcon("list-add"));
94 btnDelStyle->setIcon(KIcon("list-remove"));
95 btnEditStyle->setIcon(KIcon("configure"));
97 //connect: first tab
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);
118 if (editable)
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());
142 updatePreviewText();
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
170 else
171 cbLanguagesFormatters->setCurrentIndex(cbLanguagesStyle->currentIndex());
174 void SourceFormatterSettings::languagesStylesChanged(int idx)
176 if (idx < 0) // no selection
177 return;
178 //update source formatter
179 setActiveLanguage(cbLanguagesStyle->currentText(), QString());
180 populateStyleList();
182 // reload current style
183 // QString currentStyle = SourceFormatterManager::self()->currentStyle();
184 ISourceFormatter *f = SourceFormatterManager::self()->activeFormatter();
185 QString currentStyle;
186 if(f)
187 currentStyle = m_currentStyles[m_currentLang][f->name()];
188 if(currentStyle.isEmpty())
189 currentStyle = SourceFormatterManager::self()->currentStyle();
190 kDebug() << "Trying to select " << currentStyle << endl;
192 int selectedRow = 0;
193 for (int i = 0; i < listStyles->count(); ++i) {
194 QListWidgetItem *item = listStyles->item(i);
195 if (item->data(STYLE_ROLE).toString() == currentStyle)
196 selectedRow = i;
198 kDebug() << "Selected index is " << selectedRow << endl;
199 listStyles->setCurrentRow(selectedRow);
201 updatePreviewText();
204 void SourceFormatterSettings::populateStyleList()
206 listStyles->clear();
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();
214 //load custom styles
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 *)
229 if (!current)
230 return;
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);
243 } else {
244 btnDelStyle->setEnabled(true);
245 btnEditStyle->setEnabled(true);
247 changed();
248 updatePreviewText();
251 void SourceFormatterSettings::styleRenamed(QListWidgetItem *item)
253 changed();
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)
262 return;
264 //remove list item
265 int idx = listStyles->currentRow();
266 QListWidgetItem *item = listStyles->takeItem(idx);
267 if (!item)
268 return;
270 QString styleName = item->data(STYLE_ROLE).toString();
271 SourceFormatterManager::self()->deleteStyle(styleName);
272 listStyles->setCurrentRow(idx - 1);
275 void SourceFormatterSettings::addStyle()
277 //ask for caption
278 bool ok;
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
283 return;
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);
296 changed();
299 void SourceFormatterSettings::editStyle()
301 QListWidgetItem *item = listStyles->currentItem();
302 if (!item)
303 return;
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()
315 changed();
318 void SourceFormatterSettings::languagesFormattersChanged(int idx)
320 if (idx < 0) // no selection
321 return;
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());
337 int rowToSelect = 0;
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);
356 populateStyleList();
358 //update description label
359 lblDescription->setText(m_currentFormatter->description());
360 updatePreviewText();
361 changed();
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;