diff --git a/shell/settings/environmentgroupmodel.cpp b/shell/settings/environmentgroupmodel.cpp index 7b10fe439..a76a3eeb1 100644 --- a/shell/settings/environmentgroupmodel.cpp +++ b/shell/settings/environmentgroupmodel.cpp @@ -1,242 +1,241 @@ /* This file is part of KDevelop Copyright 2007 Andreas Pakulat This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "environmentgroupmodel.h" #include #include #include #include namespace KDevelop { -EnvironmentGroupModel::EnvironmentGroupModel() - : QAbstractTableModel() +EnvironmentGroupModel::EnvironmentGroupModel(QObject* parent) + : QAbstractTableModel(parent) { } int EnvironmentGroupModel::rowCount( const QModelIndex& parent ) const { if( parent.isValid() ) return 0; if( !m_currentGroup.isEmpty() ) return m_varsByIndex.count(); return 0; } int EnvironmentGroupModel::columnCount( const QModelIndex& parent ) const { if( parent.isValid() ) return 0; return 2; } Qt::ItemFlags EnvironmentGroupModel::flags( const QModelIndex& idx ) const { if( !idx.isValid() || m_currentGroup.isEmpty() ) { return Qt::NoItemFlags ; } return ( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ); } QVariant EnvironmentGroupModel::data( const QModelIndex& idx, int role ) const { if( !idx.isValid() || m_currentGroup.isEmpty() || idx.row() < 0 || idx.row() >= rowCount() || idx.column() < 0 || idx.column() >= columnCount(QModelIndex()) ) { return QVariant(); } const QString variable = m_varsByIndex[idx.row()]; if (role == VariableRole) { return variable; } else if (role == ValueRole) { return variables( m_currentGroup ).value(variable); } else if (role == Qt::DisplayRole || role == Qt::EditRole) { if(idx.column() == VariableColumn) { return variable; } else { return variables( m_currentGroup ).value(variable); } } return QVariant(); } QVariant EnvironmentGroupModel::headerData( int section, Qt::Orientation orientation, int role ) const { if( section < 0 || section >= columnCount(QModelIndex()) || m_currentGroup.isEmpty() || orientation != Qt::Horizontal || role != Qt::DisplayRole ) { return QVariant(); } if( section == VariableColumn ) { return i18n( "Variable" ); } else { return i18n( "Value" ); } } bool EnvironmentGroupModel::setData( const QModelIndex& idx, const QVariant& data, int role ) { if( !idx.isValid() || m_currentGroup.isEmpty() || idx.row() < 0 || idx.row() >= rowCount() || idx.column() < 0 || idx.column() >= columnCount(QModelIndex()) ) { return false; } if (role == Qt::EditRole) { if (idx.column() == VariableColumn) { const QString oldVariable = m_varsByIndex[idx.row()]; const QString value = variables(m_currentGroup).take(oldVariable); const QString newVariable = data.toString(); variables(m_currentGroup).insert(newVariable, value); m_varsByIndex[idx.row()] = newVariable; } else { variables(m_currentGroup).insert(m_varsByIndex[idx.row()], data.toString()); } emit dataChanged(idx, idx); } return true; } QModelIndex EnvironmentGroupModel::addVariable( const QString& var, const QString& value ) { const int pos = m_varsByIndex.indexOf(var); if (pos != -1) { return index(pos, 0, QModelIndex()); // No duplicates } const int insertPos = rowCount(); beginInsertRows( QModelIndex(), insertPos, insertPos ); m_varsByIndex << var; variables( m_currentGroup ).insert( var, value ); endInsertRows(); return index(insertPos, 0, QModelIndex()); } void EnvironmentGroupModel::removeGroup( const QString& grp ) { if( groups().contains( grp ) ) { EnvironmentGroupList::removeGroup( grp ); setCurrentGroup(defaultGroup()); } } void EnvironmentGroupModel::removeVariables(const QStringList& variables) { foreach (const QString& variable, variables) { removeVariable(variable); } } void EnvironmentGroupModel::removeVariable(const QString& variable) { if (m_currentGroup.isEmpty()) return; const int pos = m_varsByIndex.indexOf(variable); if (pos == -1) return; beginRemoveRows(QModelIndex(), pos, pos); m_varsByIndex.removeAt(pos); variables(m_currentGroup).remove(variable); endRemoveRows(); } void EnvironmentGroupModel::setCurrentGroup( const QString& group ) { if( group.isEmpty() ) return; beginResetModel(); m_currentGroup = group; m_varsByIndex.clear(); foreach( const QString &var, variables( m_currentGroup ).keys() ) { m_varsByIndex << var; } endResetModel(); } bool EnvironmentGroupModel::cloneCurrentGroup( const QString& newGroup ) { if( newGroup.isEmpty() || groups().contains( newGroup ) ) { return false; } beginResetModel(); foreach( const QString &key, m_varsByIndex ) { variables( newGroup ).insert( key, variables( m_currentGroup ).value( key ) ); } m_currentGroup = newGroup; endResetModel(); return true; } void EnvironmentGroupModel::changeDefaultGroup( const QString& grp ) { if( !grp.isEmpty() ) setDefaultGroup( grp ); } void EnvironmentGroupModel::loadEnvironmentFromString(const QString& plainText) { beginResetModel(); m_varsByIndex.clear(); variables(m_currentGroup).clear(); const QStringList lines = plainText.split(QLatin1Char('\n'), QString::SkipEmptyParts); foreach (const auto& line, lines) { const QString var = line.section('=', 0, 0); const QString value = line.section('=', 1, -1).trimmed(); if (!var.isEmpty()) { m_varsByIndex << var; variables(m_currentGroup).insert(var, value); } } endResetModel(); } void EnvironmentGroupModel::loadFromConfig( KConfig* cfg ) { loadSettings( cfg ); setCurrentGroup(defaultGroup()); } void EnvironmentGroupModel::saveToConfig( KConfig* cfg ) { saveSettings( cfg ); } } - diff --git a/shell/settings/environmentgroupmodel.h b/shell/settings/environmentgroupmodel.h index 831ec7aec..83810ad27 100644 --- a/shell/settings/environmentgroupmodel.h +++ b/shell/settings/environmentgroupmodel.h @@ -1,80 +1,81 @@ /* This file is part of KDevelop Copyright 2007 Andreas Pakulat This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KDEVPLATFORM_ENVIRONMENTGROUPMODEL_H #define KDEVPLATFORM_ENVIRONMENTGROUPMODEL_H #include #include #include "util/environmentgrouplist.h" class QVariant; class QModelIndex; namespace KDevelop { class EnvironmentGroupModel : public QAbstractTableModel, public EnvironmentGroupList { Q_OBJECT public: enum Role { VariableRole = Qt::UserRole + 1, ValueRole }; enum Column { VariableColumn, ValueColumn }; - EnvironmentGroupModel(); + explicit EnvironmentGroupModel(QObject* parent = nullptr); + int rowCount( const QModelIndex &parent = QModelIndex() ) const override; int columnCount( const QModelIndex &parent = QModelIndex() ) const override; Qt::ItemFlags flags( const QModelIndex& idx ) const override; QVariant data( const QModelIndex& idx, int role = Qt::DisplayRole) const override; QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; bool setData( const QModelIndex& idx, const QVariant&, int role = Qt::EditRole) override; void setCurrentGroup( const QString& group ); bool cloneCurrentGroup( const QString& newGroup ); /** * Load a set of environment variables from a string. * * @p plainText In the form "FOO=1\nBAR=2" */ void loadEnvironmentFromString(const QString& plainText); void loadFromConfig( KConfig* ); void saveToConfig( KConfig* ); QModelIndex addVariable( const QString& var, const QString& value ); void removeVariable(const QString& variable); void removeVariables(const QStringList& variables); void removeGroup( const QString& grp ); void changeDefaultGroup( const QString& grp ); private: QStringList m_varsByIndex; QString m_currentGroup; }; } #endif diff --git a/shell/settings/environmentpreferences.cpp b/shell/settings/environmentpreferences.cpp index cefae55c0..ba9fa6745 100644 --- a/shell/settings/environmentpreferences.cpp +++ b/shell/settings/environmentpreferences.cpp @@ -1,97 +1,97 @@ /* This file is part of KDevelop Copyright 2006 Adam Treat This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "environmentpreferences.h" #include #include #include #include #include #include "environmentwidget.h" namespace KDevelop { class EnvironmentPreferencesPrivate { public: EnvironmentWidget *preferencesDialog; KConfigSkeleton* skel; QString activeGroup; }; EnvironmentPreferences::EnvironmentPreferences(const QString& activeGroup, QWidget* parent) : ConfigPage(nullptr, nullptr, parent), d(new EnvironmentPreferencesPrivate) { QVBoxLayout * l = new QVBoxLayout( this ); d->preferencesDialog = new EnvironmentWidget( this ); l->addWidget( d->preferencesDialog ); connect(d->preferencesDialog, &EnvironmentWidget::changed, this, &EnvironmentPreferences::changed); - d->skel = new KConfigSkeleton(KSharedConfig::openConfig()); + d->skel = new KConfigSkeleton(KSharedConfig::openConfig(), this); setConfigSkeleton(d->skel); d->activeGroup = activeGroup; } EnvironmentPreferences::~EnvironmentPreferences( ) { delete d; } void EnvironmentPreferences::apply() { d->preferencesDialog->saveSettings(d->skel->config()); ConfigPage::apply(); } void EnvironmentPreferences::reset() { d->preferencesDialog->loadSettings(d->skel->config()); d->preferencesDialog->setActiveGroup(d->activeGroup); ConfigPage::reset(); } void EnvironmentPreferences::defaults() { d->preferencesDialog->defaults(d->skel->config()); ConfigPage::defaults(); } QString EnvironmentPreferences::name() const { return i18n("Environment"); } QString EnvironmentPreferences::fullName() const { return i18n("Configure Environment Variables"); } QIcon EnvironmentPreferences::icon() const { return QIcon::fromTheme(QStringLiteral("utilities-terminal")); } } diff --git a/shell/settings/environmentwidget.cpp b/shell/settings/environmentwidget.cpp index 9fe34db65..9cbb4f6b4 100644 --- a/shell/settings/environmentwidget.cpp +++ b/shell/settings/environmentwidget.cpp @@ -1,244 +1,245 @@ /* This file is part of KDevelop Copyright 2006 Adam Treat Copyright 2007 Dukju Ahn Copyright 2008 Andreas Pakuat This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "environmentwidget.h" #include #include #include #include #include #include #include #include #include "environmentgroupmodel.h" #include "placeholderitemproxymodel.h" #include "../debug.h" namespace KDevelop { EnvironmentWidget::EnvironmentWidget( QWidget *parent ) - : QWidget( parent ), groupModel( new EnvironmentGroupModel() ), proxyModel( new QSortFilterProxyModel() ) + : QWidget( parent ) + , groupModel( new EnvironmentGroupModel(this) ) + , proxyModel( new QSortFilterProxyModel(this) ) { - // setup ui ui.setupUi( this ); ui.variableTable->verticalHeader()->hide(); proxyModel->setSourceModel( groupModel ); PlaceholderItemProxyModel* topProxyModel = new PlaceholderItemProxyModel(this); topProxyModel->setSourceModel(proxyModel); topProxyModel->setColumnHint(0, i18n("Enter variable ...")); connect(topProxyModel, &PlaceholderItemProxyModel::dataInserted, this, &EnvironmentWidget::handleVariableInserted); ui.variableTable->setModel( topProxyModel ); ui.variableTable->horizontalHeader()->setSectionResizeMode( 0, QHeaderView::ResizeToContents ); ui.variableTable->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch ); ui.addgrpBtn->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); ui.clonegrpBtn->setIcon(QIcon::fromTheme(QStringLiteral("edit-clone"))); ui.removegrpBtn->setIcon(QIcon::fromTheme(QStringLiteral("list-remove"))); ui.deleteButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove"))); ui.deleteButton->setShortcut(Qt::Key_Delete); ui.batchModeEditButton->setIcon(QIcon::fromTheme(QStringLiteral("format-list-unordered"))); connect( ui.deleteButton, &QPushButton::clicked, this, &EnvironmentWidget::deleteButtonClicked ); connect( ui.batchModeEditButton, &QPushButton::clicked, this, &EnvironmentWidget::batchModeEditButtonClicked ); connect( ui.clonegrpBtn, &QPushButton::clicked, this, &EnvironmentWidget::cloneGroupClicked ); connect( ui.addgrpBtn, &QPushButton::clicked, this, &EnvironmentWidget::addGroupClicked ); connect( ui.addgrpBtn, &QPushButton::clicked, this, &EnvironmentWidget::changed ); connect( ui.removegrpBtn, &QPushButton::clicked, this, &EnvironmentWidget::removeGroupClicked ); connect( ui.removegrpBtn, &QPushButton::clicked, this, &EnvironmentWidget::changed ); connect( ui.setAsDefaultBtn, &QPushButton::clicked, this, &EnvironmentWidget::setAsDefault ); connect( ui.setAsDefaultBtn, &QPushButton::clicked, this, &EnvironmentWidget::changed ); connect( ui.activeCombo, static_cast(&KComboBox::currentIndexChanged), this, &EnvironmentWidget::activeGroupChanged ); connect( ui.activeCombo, &KComboBox::editTextChanged, this, &EnvironmentWidget::enableButtons); connect( groupModel, &EnvironmentGroupModel::dataChanged, this, &EnvironmentWidget::changed ); connect( groupModel, &EnvironmentGroupModel::rowsRemoved, this, &EnvironmentWidget::changed ); connect( groupModel, &EnvironmentGroupModel::rowsInserted, this, &EnvironmentWidget::changed ); connect( groupModel, &EnvironmentGroupModel::rowsRemoved, this, &EnvironmentWidget::enableDeleteButton ); connect( groupModel, &EnvironmentGroupModel::rowsInserted, this, &EnvironmentWidget::enableDeleteButton ); connect( groupModel, &EnvironmentGroupModel::modelReset, this, &EnvironmentWidget::enableDeleteButton ); } void EnvironmentWidget::setActiveGroup( const QString& group ) { ui.activeCombo->setCurrentItem(group); } void EnvironmentWidget::enableDeleteButton() { ui.deleteButton->setEnabled( groupModel->rowCount() > 0 ); } void EnvironmentWidget::setAsDefault() { groupModel->changeDefaultGroup( ui.activeCombo->currentText() ); enableButtons( ui.activeCombo->currentText() ); emit changed(); } void EnvironmentWidget::loadSettings( KConfig* config ) { qCDebug(SHELL) << "Loading groups from config"; groupModel->loadFromConfig( config ); ui.activeCombo->clear(); QStringList groupList = groupModel->groups(); qCDebug(SHELL) << "Grouplist:" << groupList << "default group:" << groupModel->defaultGroup(); ui.activeCombo->addItems( groupList ); int idx = ui.activeCombo->findText( groupModel->defaultGroup() ); ui.activeCombo->setCurrentIndex( idx ); } void EnvironmentWidget::saveSettings( KConfig* config ) { groupModel->saveToConfig( config ); } void EnvironmentWidget::defaults( KConfig* config ) { loadSettings( config ); } void EnvironmentWidget::deleteButtonClicked() { QModelIndexList selected = ui.variableTable->selectionModel()->selectedRows(); if( selected.isEmpty() ) return; QStringList variables; foreach( const QModelIndex &idx, selected ) { const QString variable = idx.data(EnvironmentGroupModel::VariableRole).toString(); variables << variable; } groupModel->removeVariables(variables); } void EnvironmentWidget::handleVariableInserted(int /*column*/, const QVariant& value) { groupModel->addVariable(value.toString(), QString()); } void EnvironmentWidget::batchModeEditButtonClicked() { QDialog dialog(this); dialog.setWindowTitle( i18n( "Batch Edit Mode" ) ); QVBoxLayout *layout = new QVBoxLayout(&dialog); auto edit = new QPlainTextEdit; edit->setPlaceholderText(QStringLiteral("VARIABLE1=VALUE1\nVARIABLE2=VALUE2")); QString text; for (int i = 0; i < proxyModel->rowCount(); ++i) { const auto variable = proxyModel->index(i, EnvironmentGroupModel::VariableColumn).data().toString(); const auto value = proxyModel->index(i, EnvironmentGroupModel::ValueColumn).data().toString(); text.append(QStringLiteral("%1=%2\n").arg(variable, value)); } edit->setPlainText(text); layout->addWidget( edit ); auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); auto okButton = buttonBox->button(QDialogButtonBox::Ok); okButton->setDefault(true); okButton->setShortcut(Qt::CTRL | Qt::Key_Return); dialog.connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept); dialog.connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject); layout->addWidget(buttonBox); dialog.resize(600, 400); if ( dialog.exec() != QDialog::Accepted ) { return; } groupModel->loadEnvironmentFromString(edit->toPlainText()); } void EnvironmentWidget::addGroupClicked() { QString curText = ui.activeCombo->currentText(); if( groupModel->groups().contains( curText ) ) { return; // same group name cannot be added twice. } ui.activeCombo->addItem( curText ); ui.activeCombo->setCurrentItem( curText ); } void EnvironmentWidget::cloneGroupClicked() { QString newGroup = ui.activeCombo->currentText(); if( !groupModel->cloneCurrentGroup( newGroup ) ) { const KLocalizedString newGroupTemplate = ki18nc("a copy of the existing environment was created", "%1 (Cloned %2)").subs(newGroup); for (int id = 1; ; ++id) { newGroup = newGroupTemplate.subs(id).toString(); if (groupModel->cloneCurrentGroup(newGroup)) { break; } } } ui.activeCombo->addItem( newGroup ); ui.activeCombo->setCurrentItem( newGroup ); } void EnvironmentWidget::removeGroupClicked() { int idx = ui.activeCombo->currentIndex(); if( idx < 0 || ui.activeCombo->count() == 1 ) { return; } QString curText = ui.activeCombo->currentText(); groupModel->removeGroup( curText ); ui.activeCombo->removeItem( idx ); ui.activeCombo->setCurrentItem( groupModel->defaultGroup() ); } void EnvironmentWidget::activeGroupChanged( int /*idx*/ ) { groupModel->setCurrentGroup( ui.activeCombo->currentText() ); enableButtons( ui.activeCombo->currentText() ); } void EnvironmentWidget::enableButtons( const QString& txt ) { ui.addgrpBtn->setEnabled( !groupModel->groups().contains( txt ) ); ui.removegrpBtn->setEnabled( ( groupModel->groups().contains( txt ) && groupModel->defaultGroup() != txt ) ); ui.setAsDefaultBtn->setEnabled( ( groupModel->groups().contains( txt ) && groupModel->defaultGroup() != txt ) ); } } #include "moc_environmentwidget.cpp" diff --git a/shell/settings/sourceformattersettings.cpp b/shell/settings/sourceformattersettings.cpp index e0bb42bb4..020dc2dac 100644 --- a/shell/settings/sourceformattersettings.cpp +++ b/shell/settings/sourceformattersettings.cpp @@ -1,540 +1,540 @@ /* This file is part of KDevelop * Copyright (C) 2008 Cédric Pasteur 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; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "sourceformattersettings.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "editstyledialog.h" #include "../debug.h" #define STYLE_ROLE (Qt::UserRole+1) using KDevelop::Core; using KDevelop::ISourceFormatter; using KDevelop::SourceFormatterStyle; using KDevelop::SourceFormatterController; using KDevelop::SourceFormatter; namespace { namespace Strings { QString userStylePrefix() { return QStringLiteral("User"); } } } LanguageSettings::LanguageSettings() : selectedFormatter(nullptr), selectedStyle(nullptr) { } SourceFormatterSettings::SourceFormatterSettings(QWidget* parent) : KDevelop::ConfigPage(nullptr, nullptr, parent) { setupUi(this); connect( cbLanguages, static_cast(&KComboBox::currentIndexChanged), this, &SourceFormatterSettings::selectLanguage ); connect( cbFormatters, static_cast(&KComboBox::currentIndexChanged), this, &SourceFormatterSettings::selectFormatter ); connect( chkKateModelines, &QCheckBox::toggled, this, &SourceFormatterSettings::somethingChanged ); connect( chkKateOverrideIndentation, &QCheckBox::toggled, this, &SourceFormatterSettings::somethingChanged ); connect( styleList, &QListWidget::currentRowChanged, this, &SourceFormatterSettings::selectStyle ); connect( btnDelStyle, &QPushButton::clicked, this, &SourceFormatterSettings::deleteStyle ); connect( btnNewStyle, &QPushButton::clicked, this, &SourceFormatterSettings::newStyle ); connect( btnEditStyle, &QPushButton::clicked, this, &SourceFormatterSettings::editStyle ); connect( styleList, &QListWidget::itemChanged, this, &SourceFormatterSettings::styleNameChanged ); m_document = KTextEditor::Editor::instance()->createDocument(this); m_document->setReadWrite(false); m_view = m_document->createView(textEditor); m_view->setStatusBarEnabled(false); QVBoxLayout *layout2 = new QVBoxLayout(textEditor); layout2->addWidget(m_view); textEditor->setLayout(layout2); m_view->show(); KTextEditor::ConfigInterface *iface = qobject_cast(m_view); if (iface) { iface->setConfigValue(QStringLiteral("dynamic-word-wrap"), false); iface->setConfigValue(QStringLiteral("icon-bar"), false); } } SourceFormatterSettings::~SourceFormatterSettings() { qDeleteAll(formatters); } void selectAvailableStyle(LanguageSettings& lang) { Q_ASSERT(!lang.selectedFormatter->styles.empty()); lang.selectedStyle = *lang.selectedFormatter->styles.begin(); } void SourceFormatterSettings::reset() { SourceFormatterController* fmtctrl = Core::self()->sourceFormatterControllerInternal(); QList plugins = KDevelop::ICore::self()->pluginController()->allPluginsForExtension( QStringLiteral("org.kdevelop.ISourceFormatter") ); foreach( KDevelop::IPlugin* plugin, plugins ) { KDevelop::ISourceFormatter* ifmt = plugin->extension(); auto info = KDevelop::Core::self()->pluginControllerInternal()->pluginInfo( plugin ); KDevelop::SourceFormatter* formatter; FormatterMap::const_iterator iter = formatters.constFind(ifmt->name()); if (iter == formatters.constEnd()) { formatter = fmtctrl->createFormatterForPlugin(ifmt); formatters[ifmt->name()] = formatter; } else { formatter = iter.value(); } foreach ( const SourceFormatterStyle* style, formatter->styles ) { foreach ( const SourceFormatterStyle::MimeHighlightPair& item, style->mimeTypes() ) { QMimeType mime = QMimeDatabase().mimeTypeForName(item.mimeType); if (!mime.isValid()) { qCWarning(SHELL) << "plugin" << info.name() << "supports unknown mimetype entry" << item.mimeType; continue; } QString languageName = item.highlightMode; LanguageSettings& l = languages[languageName]; l.mimetypes.append(mime); l.formatters.insert( formatter ); } } } // Sort the languages, preferring firstly active, then loaded languages QList sortedLanguages; foreach(const auto language, KDevelop::ICore::self()->languageController()->activeLanguages() + KDevelop::ICore::self()->languageController()->loadedLanguages()) { if( languages.contains( language->name() ) && !sortedLanguages.contains(language->name()) ) { sortedLanguages.push_back( language->name() ); } } foreach( const QString& name, languages.keys() ) if( !sortedLanguages.contains( name ) ) sortedLanguages.push_back( name ); foreach( const QString& name, sortedLanguages ) { // Pick the first appropriate mimetype for this language KConfigGroup grp = fmtctrl->sessionConfig(); LanguageSettings& l = languages[name]; const QList mimetypes = l.mimetypes; foreach (const QMimeType& mimetype, mimetypes) { QStringList formatterAndStyleName = grp.readEntry(mimetype.name(), QString()).split(QStringLiteral("||"), QString::KeepEmptyParts); FormatterMap::const_iterator formatterIter = formatters.constFind(formatterAndStyleName.first()); if (formatterIter == formatters.constEnd()) { qCDebug(SHELL) << "Reference to unknown formatter" << formatterAndStyleName.first(); Q_ASSERT(!l.formatters.empty()); // otherwise there should be no entry for 'name' l.selectedFormatter = *l.formatters.begin(); selectAvailableStyle(l); } else { l.selectedFormatter = formatterIter.value(); SourceFormatter::StyleMap::const_iterator styleIter = l.selectedFormatter->styles.constFind(formatterAndStyleName.at( 1 )); if (styleIter == l.selectedFormatter->styles.constEnd()) { qCDebug(SHELL) << "No style" << formatterAndStyleName.at( 1 ) << "found for formatter" << formatterAndStyleName.first(); selectAvailableStyle(l); } else { l.selectedStyle = styleIter.value(); } } } if (!l.selectedFormatter) { Q_ASSERT(!l.formatters.empty()); l.selectedFormatter = *l.formatters.begin(); } if (!l.selectedStyle) { selectAvailableStyle(l); } } bool b = blockSignals( true ); cbLanguages->blockSignals( !b ); cbFormatters->blockSignals( !b ); styleList->blockSignals( !b ); chkKateModelines->blockSignals( !b ); chkKateOverrideIndentation->blockSignals( !b ); cbLanguages->clear(); cbFormatters->clear(); styleList->clear(); chkKateModelines->setChecked( fmtctrl->sessionConfig().readEntry( SourceFormatterController::kateModeLineConfigKey(), false ) ); chkKateOverrideIndentation->setChecked( fmtctrl->sessionConfig().readEntry( SourceFormatterController::kateOverrideIndentationConfigKey(), false ) ); foreach( const QString& name, sortedLanguages ) { cbLanguages->addItem( name ); } if( cbLanguages->count() == 0 ) { cbLanguages->setEnabled( false ); selectLanguage( -1 ); } else { cbLanguages->setCurrentIndex( 0 ); selectLanguage( 0 ); } updatePreview(); blockSignals( b ); cbLanguages->blockSignals( b ); cbFormatters->blockSignals( b ); styleList->blockSignals( b ); chkKateModelines->blockSignals( b ); chkKateOverrideIndentation->blockSignals( b ); } void SourceFormatterSettings::apply() { KConfigGroup globalConfig = Core::self()->sourceFormatterControllerInternal()->globalConfig(); foreach( SourceFormatter* fmt, formatters ) { KConfigGroup fmtgrp = globalConfig.group( fmt->formatter->name() ); // delete all styles so we don't leave any behind when all user styles are deleted foreach( const QString& subgrp, fmtgrp.groupList() ) { if( subgrp.startsWith( Strings::userStylePrefix() ) ) { fmtgrp.deleteGroup( subgrp ); } } foreach( const SourceFormatterStyle* style, fmt->styles ) { if( style->name().startsWith( Strings::userStylePrefix() ) ) { KConfigGroup stylegrp = fmtgrp.group( style->name() ); stylegrp.writeEntry( SourceFormatterController::styleCaptionKey(), style->caption() ); stylegrp.writeEntry( SourceFormatterController::styleContentKey(), style->content() ); stylegrp.writeEntry( SourceFormatterController::styleMimeTypesKey(), style->mimeTypesVariant() ); stylegrp.writeEntry( SourceFormatterController::styleSampleKey(), style->overrideSample() ); } } } KConfigGroup sessionConfig = Core::self()->sourceFormatterControllerInternal()->sessionConfig(); for ( LanguageMap::const_iterator iter = languages.constBegin(); iter != languages.constEnd(); ++iter ) { foreach(const QMimeType& mime, iter.value().mimetypes) { sessionConfig.writeEntry(mime.name(), QStringLiteral("%1||%2").arg(iter.value().selectedFormatter->formatter->name(), iter.value().selectedStyle->name())); } } sessionConfig.writeEntry( SourceFormatterController::kateModeLineConfigKey(), chkKateModelines->isChecked() ); sessionConfig.writeEntry( SourceFormatterController::kateOverrideIndentationConfigKey(), chkKateOverrideIndentation->isChecked() ); sessionConfig.sync(); globalConfig.sync(); Core::self()->sourceFormatterControllerInternal()->settingsChanged(); } void SourceFormatterSettings::defaults() { // do nothing } void SourceFormatterSettings::enableStyleButtons() { bool userEntry = styleList->currentItem() && styleList->currentItem()->data( STYLE_ROLE ).toString().startsWith( Strings::userStylePrefix() ); QString languageName = cbLanguages->currentText(); QMap< QString, LanguageSettings >::const_iterator it = languages.constFind(languageName); bool hasEditWidget = false; if (it != languages.constEnd()) { const LanguageSettings& l = it.value(); Q_ASSERT(l.selectedFormatter); ISourceFormatter* fmt = l.selectedFormatter->formatter; - hasEditWidget = ( fmt && fmt->editStyleWidget( l.mimetypes.first() ) ); + hasEditWidget = ( fmt && QScopedPointer(fmt->editStyleWidget( l.mimetypes.first() )) ); } btnDelStyle->setEnabled( userEntry ); btnEditStyle->setEnabled( userEntry && hasEditWidget ); btnNewStyle->setEnabled( cbFormatters->currentIndex() >= 0 && hasEditWidget ); } void SourceFormatterSettings::selectLanguage( int idx ) { cbFormatters->clear(); if( idx < 0 ) { cbFormatters->setEnabled( false ); selectFormatter( -1 ); return; } cbFormatters->setEnabled( true ); { QSignalBlocker blocker(cbFormatters); LanguageSettings& l = languages[cbLanguages->itemText( idx )]; foreach( const SourceFormatter* fmt, l.formatters ) { cbFormatters->addItem( fmt->formatter->caption(), fmt->formatter->name() ); } cbFormatters->setCurrentIndex(cbFormatters->findData(l.selectedFormatter->formatter->name())); } selectFormatter( cbFormatters->currentIndex() ); emit changed(); } void SourceFormatterSettings::selectFormatter( int idx ) { styleList->clear(); if( idx < 0 ) { styleList->setEnabled( false ); enableStyleButtons(); return; } styleList->setEnabled( true ); LanguageSettings& l = languages[ cbLanguages->currentText() ]; Q_ASSERT( idx < l.formatters.size() ); FormatterMap::const_iterator formatterIter = formatters.constFind(cbFormatters->itemData( idx ).toString()); Q_ASSERT( formatterIter != formatters.constEnd() ); Q_ASSERT( l.formatters.contains(formatterIter.value()) ); if (l.selectedFormatter != formatterIter.value()) { l.selectedFormatter = formatterIter.value(); l.selectedStyle = nullptr; // will hold 0 until a style is picked } foreach( const SourceFormatterStyle* style, formatterIter.value()->styles ) { if ( ! style->supportsLanguage(cbLanguages->currentText())) { // do not list items which do not support the selected language continue; } QListWidgetItem* item = addStyle( *style ); if (style == l.selectedStyle) { styleList->setCurrentItem(item); } } if (l.selectedStyle == nullptr) { styleList->setCurrentRow(0); } enableStyleButtons(); emit changed(); } void SourceFormatterSettings::selectStyle( int row ) { if( row < 0 ) { enableStyleButtons(); return; } styleList->setCurrentRow( row ); LanguageSettings& l = languages[ cbLanguages->currentText() ]; l.selectedStyle = l.selectedFormatter->styles[styleList->item( row )->data( STYLE_ROLE ).toString()]; enableStyleButtons(); updatePreview(); emit changed(); } void SourceFormatterSettings::deleteStyle() { Q_ASSERT( styleList->currentRow() >= 0 ); QListWidgetItem* item = styleList->currentItem(); LanguageSettings& l = languages[ cbLanguages->currentText() ]; SourceFormatter* fmt = l.selectedFormatter; SourceFormatter::StyleMap::iterator styleIter = fmt->styles.find(item->data( STYLE_ROLE ).toString()); QStringList otherLanguageNames; QList otherlanguages; for ( LanguageMap::iterator languageIter = languages.begin(); languageIter != languages.end(); ++languageIter ) { if ( &languageIter.value() != &l && languageIter.value().selectedStyle == styleIter.value() ) { otherLanguageNames.append(languageIter.key()); otherlanguages.append(&languageIter.value()); } } if (!otherLanguageNames.empty() && KMessageBox::warningContinueCancel(this, i18n("The style %1 is also used for the following languages:\n%2.\nAre you sure you want to delete it?", styleIter.value()->caption(), otherLanguageNames.join(QStringLiteral("\n"))), i18n("Style being deleted")) != KMessageBox::Continue) { return; } styleList->takeItem( styleList->currentRow() ); fmt->styles.erase(styleIter); delete item; selectStyle( styleList->count() > 0 ? 0 : -1 ); foreach (LanguageSettings* lang, otherlanguages) { selectAvailableStyle(*lang); } updatePreview(); emit changed(); } void SourceFormatterSettings::editStyle() { QString language = cbLanguages->currentText(); Q_ASSERT( languages.contains( language ) ); LanguageSettings& l = languages[ language ]; SourceFormatter* fmt = l.selectedFormatter; QMimeType mimetype = l.mimetypes.first(); - if( fmt->formatter->editStyleWidget( mimetype ) != nullptr ) { + if( QScopedPointer(fmt->formatter->editStyleWidget( mimetype )) ) { EditStyleDialog dlg( fmt->formatter, mimetype, *l.selectedStyle, this ); if( dlg.exec() == QDialog::Accepted ) { l.selectedStyle->setContent(dlg.content()); } updatePreview(); emit changed(); } } void SourceFormatterSettings::newStyle() { QListWidgetItem* item = styleList->currentItem(); LanguageSettings& l = languages[ cbLanguages->currentText() ]; SourceFormatter* fmt = l.selectedFormatter; int idx = 0; for( int i = 0; i < styleList->count(); i++ ) { QString name = styleList->item( i )->data( STYLE_ROLE ).toString(); if( name.startsWith( Strings::userStylePrefix() ) && name.midRef( Strings::userStylePrefix().length() ).toInt() >= idx ) { idx = name.midRef( Strings::userStylePrefix().length() ).toInt(); } } // Increase number for next style idx++; SourceFormatterStyle* s = new SourceFormatterStyle( QStringLiteral( "%1%2" ).arg( Strings::userStylePrefix() ).arg( idx ) ); if( item ) { SourceFormatterStyle* existstyle = fmt->styles[ item->data( STYLE_ROLE ).toString() ]; s->setCaption( i18n( "New %1", existstyle->caption() ) ); s->copyDataFrom( existstyle ); } else { s->setCaption( i18n( "New Style" ) ); } fmt->styles[ s->name() ] = s; QListWidgetItem* newitem = addStyle( *s ); selectStyle( styleList->row( newitem ) ); styleList->editItem( newitem ); emit changed(); } void SourceFormatterSettings::styleNameChanged( QListWidgetItem* item ) { if ( !item->isSelected() ) { return; } LanguageSettings& l = languages[ cbLanguages->currentText() ]; l.selectedStyle->setCaption( item->text() ); emit changed(); } QListWidgetItem* SourceFormatterSettings::addStyle( const SourceFormatterStyle& s ) { QListWidgetItem* item = new QListWidgetItem( styleList ); item->setText( s.caption() ); item->setData( STYLE_ROLE, s.name() ); if( s.name().startsWith( Strings::userStylePrefix() ) ) { item->setFlags( item->flags() | Qt::ItemIsEditable ); } styleList->addItem( item ); return item; } void SourceFormatterSettings::updatePreview() { m_document->setReadWrite( true ); QString langName = cbLanguages->itemText( cbLanguages->currentIndex() ); if( !langName.isEmpty() ) { LanguageSettings& l = languages[ langName ]; SourceFormatter* fmt = l.selectedFormatter; SourceFormatterStyle* style = l.selectedStyle; descriptionLabel->setText( style->description() ); if( style->usePreview() ) { ISourceFormatter* ifmt = fmt->formatter; QMimeType mime = l.mimetypes.first(); m_document->setHighlightingMode( style->modeForMimetype( mime ) ); //NOTE: this is ugly, but otherwise kate might remove tabs again :-/ // see also: https://bugs.kde.org/show_bug.cgi?id=291074 KTextEditor::ConfigInterface* iface = qobject_cast(m_document); QVariant oldReplaceTabs; if (iface) { oldReplaceTabs = iface->configValue(QStringLiteral("replace-tabs")); iface->setConfigValue(QStringLiteral("replace-tabs"), false); } m_document->setText( ifmt->formatSourceWithStyle( *style, ifmt->previewText( *style, mime ), QUrl(), mime ) ); if (iface) { iface->setConfigValue(QStringLiteral("replace-tabs"), oldReplaceTabs); } previewLabel->show(); textEditor->show(); }else{ previewLabel->hide(); textEditor->hide(); } } else { m_document->setText( i18n( "No Language selected" ) ); } m_view->setCursorPosition( KTextEditor::Cursor( 0, 0 ) ); m_document->setReadWrite( false ); } void SourceFormatterSettings::somethingChanged() { // Widgets are managed manually, so we have to explicitly tell KCModule // that we have some changes, otherwise it won't call "save" and/or will not activate // "Appy" emit changed(); } QString SourceFormatterSettings::name() const { return i18n("Source Formatter"); } QString SourceFormatterSettings::fullName() const { return i18n("Configure Source Formatter"); } QIcon SourceFormatterSettings::icon() const { return QIcon::fromTheme(QStringLiteral("text-field")); }