diff --git a/src/plugins/texteditor/CMakeLists.txt b/src/plugins/texteditor/CMakeLists.txt index 14a20e0d847..655ce8f4354 100644 --- a/src/plugins/texteditor/CMakeLists.txt +++ b/src/plugins/texteditor/CMakeLists.txt @@ -40,6 +40,7 @@ add_qtc_plugin(TextEditor codeassist/runner.cpp codeassist/runner.h codeassist/textdocumentmanipulator.cpp codeassist/textdocumentmanipulator.h codeassist/textdocumentmanipulatorinterface.h + codecchooser.cpp codecchooser.h codestyleeditor.cpp codestyleeditor.h codestylepool.cpp codestylepool.h codestyleselectorwidget.cpp codestyleselectorwidget.h diff --git a/src/plugins/texteditor/behaviorsettingswidget.cpp b/src/plugins/texteditor/behaviorsettingswidget.cpp index 4c9c051d60f..207dc58ec0d 100644 --- a/src/plugins/texteditor/behaviorsettingswidget.cpp +++ b/src/plugins/texteditor/behaviorsettingswidget.cpp @@ -25,17 +25,17 @@ #include "behaviorsettingswidget.h" +#include "behaviorsettings.h" +#include "codecchooser.h" +#include "extraencodingsettings.h" +#include "simplecodestylepreferenceswidget.h" +#include "storagesettings.h" #include "tabsettingswidget.h" +#include "typingsettings.h" #include #include -#include -#include -#include -#include -#include - #include #include #include @@ -54,7 +54,6 @@ namespace TextEditor { struct BehaviorSettingsWidgetPrivate { - QList m_codecs; SimpleCodeStylePreferencesWidget *tabPreferencesWidget; QComboBox *tabKeyBehavior; @@ -70,7 +69,7 @@ struct BehaviorSettingsWidgetPrivate QCheckBox *cleanIndentation; QCheckBox *inEntireDocument; QGroupBox *groupBoxEncodings; - QComboBox *encodingBox; + CodecChooser *encodingBox; QComboBox *utf8BomBox; QLabel *defaultLineEndingsLabel; QComboBox *defaultLineEndings; @@ -145,7 +144,7 @@ BehaviorSettingsWidget::BehaviorSettingsWidget(QWidget *parent) d->inEntireDocument->setEnabled(false); d->inEntireDocument->setToolTip(tr("Cleans whitespace in entire document instead of only for changed parts.")); - d->encodingBox = new QComboBox; + d->encodingBox = new CodecChooser; d->encodingBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon); d->encodingBox->setMinimumContentsLength(20); @@ -188,32 +187,6 @@ BehaviorSettingsWidget::BehaviorSettingsWidget(QWidget *parent) d->groupBoxMouse = new QGroupBox(tr("Mouse and Keyboard")); - QList mibs = QTextCodec::availableMibs(); - Utils::sort(mibs); - QList::iterator firstNonNegative = - std::find_if(mibs.begin(), mibs.end(), [](int n) { return n >=0; }); - if (firstNonNegative != mibs.end()) - std::rotate(mibs.begin(), firstNonNegative, mibs.end()); - for (int mib : qAsConst(mibs)) { - if (QTextCodec *codec = QTextCodec::codecForMib(mib)) { - QString compoundName = QLatin1String(codec->name()); - const QList aliases = codec->aliases(); - for (const QByteArray &alias : aliases) { - compoundName += QLatin1String(" / "); - compoundName += QString::fromLatin1(alias); - } - d->encodingBox->addItem(compoundName); - d->m_codecs.append(codec); - } - } - - // Qt5 doesn't list the system locale (QTBUG-34283), so add it manually - const QString system(QLatin1String("System")); - if (d->encodingBox->findText(system) == -1) { - d->encodingBox->insertItem(0, system); - d->m_codecs.prepend(QTextCodec::codecForLocale()); - } - using namespace Utils::Layouting; const auto indent = [](QWidget *inner) { return Row { Space(30), inner }; }; @@ -293,8 +266,8 @@ BehaviorSettingsWidget::BehaviorSettingsWidget(QWidget *parent) this, &BehaviorSettingsWidget::slotBehaviorSettingsChanged); connect(d->utf8BomBox, &QComboBox::currentIndexChanged, this, &BehaviorSettingsWidget::slotExtraEncodingChanged); - connect(d->encodingBox, &QComboBox::currentIndexChanged, - this, &BehaviorSettingsWidget::slotEncodingBoxChanged); + connect(d->encodingBox, &CodecChooser::codecChanged, + this, &BehaviorSettingsWidget::textCodecChanged); connect(d->constrainTooltipsBox, &QComboBox::currentIndexChanged, this, &BehaviorSettingsWidget::slotBehaviorSettingsChanged); connect(d->keyboardTooltips, &QAbstractButton::clicked, @@ -323,28 +296,12 @@ void BehaviorSettingsWidget::setAssignedCodec(QTextCodec *codec) { const QString codecName = Core::ICore::settings()->value( Core::Constants::SETTINGS_DEFAULTTEXTENCODING).toString(); - - int rememberedSystemPosition = -1; - for (int i = 0; i < d->m_codecs.size(); ++i) { - if (codec == d->m_codecs.at(i)) { - if (d->encodingBox->itemText(i) == codecName) { - d->encodingBox->setCurrentIndex(i); - return; - } else { // we've got System matching encoding - but have explicitly set the codec - rememberedSystemPosition = i; - } - } - } - if (rememberedSystemPosition != -1) - d->encodingBox->setCurrentIndex(rememberedSystemPosition); + d->encodingBox->setAssignedCodec(codec, codecName); } QByteArray BehaviorSettingsWidget::assignedCodecName() const { - return d->encodingBox->currentIndex() == 0 - ? QByteArray("System") // we prepend System to the available codecs - : d->m_codecs.at(d->encodingBox->currentIndex())->name(); - + return d->encodingBox->assignedCodecName(); } void BehaviorSettingsWidget::setCodeStyle(ICodeStylePreferences *preferences) @@ -488,9 +445,4 @@ void BehaviorSettingsWidget::slotExtraEncodingChanged() emit extraEncodingSettingsChanged(settings); } -void BehaviorSettingsWidget::slotEncodingBoxChanged(int index) -{ - emit textCodecChanged(d->m_codecs.at(index)); -} - } // TextEditor diff --git a/src/plugins/texteditor/behaviorsettingswidget.h b/src/plugins/texteditor/behaviorsettingswidget.h index 83ffaf2680d..99a88369796 100644 --- a/src/plugins/texteditor/behaviorsettingswidget.h +++ b/src/plugins/texteditor/behaviorsettingswidget.h @@ -88,7 +88,6 @@ private: void slotStorageSettingsChanged(); void slotBehaviorSettingsChanged(); void slotExtraEncodingChanged(); - void slotEncodingBoxChanged(int index); void updateConstrainTooltipsBoxTooltip() const; BehaviorSettingsWidgetPrivate *d; diff --git a/src/plugins/texteditor/codecchooser.cpp b/src/plugins/texteditor/codecchooser.cpp new file mode 100644 index 00000000000..24fd357a383 --- /dev/null +++ b/src/plugins/texteditor/codecchooser.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "codecchooser.h" + +#include + +#include + +namespace TextEditor { + +CodecChooser::CodecChooser() +{ + QList mibs = QTextCodec::availableMibs(); + Utils::sort(mibs); + QList::iterator firstNonNegative = + std::find_if(mibs.begin(), mibs.end(), [](int n) { return n >=0; }); + if (firstNonNegative != mibs.end()) + std::rotate(mibs.begin(), firstNonNegative, mibs.end()); + for (int mib : qAsConst(mibs)) { + if (QTextCodec *codec = QTextCodec::codecForMib(mib)) { + QString compoundName = QLatin1String(codec->name()); + const QList aliases = codec->aliases(); + for (const QByteArray &alias : aliases) { + compoundName += QLatin1String(" / "); + compoundName += QString::fromLatin1(alias); + } + addItem(compoundName); + m_codecs.append(codec); + } + } + connect(this, &QComboBox::currentIndexChanged, + this, [this](int index) { emit codecChanged(m_codecs.at(index)); }); +} + +QTextCodec *CodecChooser::currentCodec() const +{ + return codecAt(currentIndex()); +} + +QTextCodec *CodecChooser::codecAt(int index) const +{ + if (index < 0) + index = 0; + return m_codecs[index]; +} + +void CodecChooser::setAssignedCodec(QTextCodec *codec, const QString &name) +{ + int rememberedSystemPosition = -1; + for (int i = 0, total = m_codecs.size(); i < total; ++i) { + if (codec != m_codecs.at(i)) + continue; + if (itemText(i) == name) { + setCurrentIndex(i); + return; + } + // we've got System matching encoding - but have explicitly set the codec + rememberedSystemPosition = i; + } + if (rememberedSystemPosition != -1) + setCurrentIndex(rememberedSystemPosition); +} + +QByteArray CodecChooser::assignedCodecName() const +{ + const int index = currentIndex(); + return index == 0 + ? QByteArray("System") // we prepend System to the available codecs + : m_codecs.at(index)->name(); +} + +} // TextEditor diff --git a/src/plugins/texteditor/codecchooser.h b/src/plugins/texteditor/codecchooser.h new file mode 100644 index 00000000000..690850bf01b --- /dev/null +++ b/src/plugins/texteditor/codecchooser.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include "texteditor_global.h" + +#include + +QT_BEGIN_NAMESPACE +class QTextCodec; +QT_END_NAMESPACE + +namespace TextEditor { + +class TEXTEDITOR_EXPORT CodecChooser : public QComboBox +{ + Q_OBJECT + +public: + CodecChooser(); + QTextCodec *currentCodec() const; + QTextCodec *codecAt(int index) const; + void setAssignedCodec(QTextCodec *codec, const QString &name); + QByteArray assignedCodecName() const; + +signals: + void codecChanged(QTextCodec *codec); + +private: + QList m_codecs; +}; + +} // namespace TextEditor diff --git a/src/plugins/texteditor/texteditor.qbs b/src/plugins/texteditor/texteditor.qbs index 7fd4fc925be..7d4e62991c4 100644 --- a/src/plugins/texteditor/texteditor.qbs +++ b/src/plugins/texteditor/texteditor.qbs @@ -37,6 +37,8 @@ Project { "circularclipboard.h", "circularclipboardassist.cpp", "circularclipboardassist.h", + "codecchooser.cpp", + "codecchooser.h", "codestyleeditor.cpp", "codestyleeditor.h", "codestylepool.cpp",