TextEditor: Factor out codec chooser

Change-Id: I664f7ba484f379b8d2b8254d4f880c0d37d70ff8
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Orgad Shaneh
2022-07-26 23:38:00 +03:00
committed by Orgad Shaneh
parent 4edb238073
commit eb33d26090
6 changed files with 166 additions and 61 deletions

View File

@@ -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

View File

@@ -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 <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <texteditor/typingsettings.h>
#include <texteditor/storagesettings.h>
#include <texteditor/behaviorsettings.h>
#include <texteditor/extraencodingsettings.h>
#include <texteditor/simplecodestylepreferenceswidget.h>
#include <utils/algorithm.h>
#include <utils/hostosinfo.h>
#include <utils/layoutbuilder.h>
@@ -54,7 +54,6 @@ namespace TextEditor {
struct BehaviorSettingsWidgetPrivate
{
QList<QTextCodec *> 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<int> mibs = QTextCodec::availableMibs();
Utils::sort(mibs);
QList<int>::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<QByteArray> 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

View File

@@ -88,7 +88,6 @@ private:
void slotStorageSettingsChanged();
void slotBehaviorSettingsChanged();
void slotExtraEncodingChanged();
void slotEncodingBoxChanged(int index);
void updateConstrainTooltipsBoxTooltip() const;
BehaviorSettingsWidgetPrivate *d;

View File

@@ -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 <utils/algorithm.h>
#include <QTextCodec>
namespace TextEditor {
CodecChooser::CodecChooser()
{
QList<int> mibs = QTextCodec::availableMibs();
Utils::sort(mibs);
QList<int>::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<QByteArray> 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

View File

@@ -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 <QComboBox>
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<QTextCodec *> m_codecs;
};
} // namespace TextEditor

View File

@@ -37,6 +37,8 @@ Project {
"circularclipboard.h",
"circularclipboardassist.cpp",
"circularclipboardassist.h",
"codecchooser.cpp",
"codecchooser.h",
"codestyleeditor.cpp",
"codestyleeditor.h",
"codestylepool.cpp",