Commit 7f0ab1a9 authored by mae's avatar mae
Browse files

Fix project-specific default file encoding

The project specific default file encoding would always be
locked to the default encoding when the project is loaded
the first time. This is unexpected and confuses users who
like to change the default encoding in the options dialog but
have never discovered (nor need) the project specific encoding.

The commit solves this by introducing a new value "Default" to
the project specific encoding combo box, which happens to
be the default.

Task-number: QTCREATORBUG-1996

Matthias
parent b3aff7b3
......@@ -38,12 +38,8 @@ const char * const CODEC("EditorConfiguration.Codec");
}
EditorConfiguration::EditorConfiguration()
: m_defaultTextCodec(QTextCodec::codecForLocale())
: m_defaultTextCodec(0)
{
QSettings* settings = Core::ICore::instance()->settings();
if (QTextCodec *candidate = QTextCodec::codecForName(
settings->value(QLatin1String("General/DefaultFileEncoding")).toByteArray()))
m_defaultTextCodec = candidate;
}
QTextCodec *EditorConfiguration::defaultTextCodec() const
......@@ -53,23 +49,22 @@ QTextCodec *EditorConfiguration::defaultTextCodec() const
void EditorConfiguration::setDefaultTextCodec(QTextCodec *codec)
{
if (!codec)
return;
m_defaultTextCodec = codec;
}
QVariantMap EditorConfiguration::toMap() const
{
QVariantMap map;
map.insert(QLatin1String(CODEC), m_defaultTextCodec->name());
QByteArray name = "Default";
if (m_defaultTextCodec)
name = m_defaultTextCodec->name();
map.insert(QLatin1String(CODEC), name);
return map;
}
bool EditorConfiguration::fromMap(const QVariantMap &map)
void EditorConfiguration::fromMap(const QVariantMap &map)
{
QTextCodec *codec = QTextCodec::codecForName(map.value(QLatin1String(CODEC)).toString().toLocal8Bit());
if (!codec)
return false;
QByteArray name = map.value(QLatin1String(CODEC)).toString().toLocal8Bit();
QTextCodec *codec = QTextCodec::codecForName(name);
m_defaultTextCodec = codec;
return true;
}
......@@ -45,11 +45,12 @@ class PROJECTEXPLORER_EXPORT EditorConfiguration
public:
EditorConfiguration();
// defaultTextCodec can be 0, in that case the editor settings default encoding shall be used
QTextCodec *defaultTextCodec() const;
void setDefaultTextCodec(QTextCodec *codec);
QVariantMap toMap() const;
bool fromMap(const QVariantMap &map);
void fromMap(const QVariantMap &map);
private:
QTextCodec *m_defaultTextCodec;
......
......@@ -90,7 +90,14 @@ EditorSettingsWidget::EditorSettingsWidget(Project *project)
m_project(project)
{
m_ui.setupUi(this);
QTextCodec *defaultTextCodec = m_project->editorConfiguration()->defaultTextCodec();
QTextCodec *defaultTextCodec = 0;
m_codecs += defaultTextCodec;
m_ui.encodingComboBox->addItem(tr("Default"));
defaultTextCodec = m_project->editorConfiguration()->defaultTextCodec();
qDebug() << "create editor settings widget for project with encoding" << defaultTextCodec;
QList<int> mibs = QTextCodec::availableMibs();
qSort(mibs);
QList<int> sortedMibs;
......@@ -100,7 +107,7 @@ EditorSettingsWidget::EditorSettingsWidget(Project *project)
foreach (int mib, mibs)
if (mib < 0)
sortedMibs += mib;
int i = 0;
int i = 1; // 0 is the default
foreach (int mib, sortedMibs) {
QTextCodec *codec = QTextCodec::codecForMib(mib);
m_codecs += codec;
......
......@@ -244,8 +244,7 @@ bool Project::fromMap(const QVariantMap &map)
{
if (map.contains(QLatin1String(EDITOR_SETTINGS_KEY))) {
QVariantMap values(map.value(QLatin1String(EDITOR_SETTINGS_KEY)).toMap());
if (!m_editorConfiguration->fromMap(values))
return false;
m_editorConfiguration->fromMap(values);
}
bool ok;
......
......@@ -59,6 +59,7 @@
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QMessageBox>
#include <QtCore/QTextCodec>
namespace {
bool debug = false;
......@@ -826,8 +827,10 @@ bool SessionManager::projectContainsFile(Project *p, const QString &fileName) co
void SessionManager::setEditorCodec(Core::IEditor *editor, const QString &fileName)
{
if (TextEditor::ITextEditor *textEditor = qobject_cast<TextEditor::ITextEditor*>(editor))
if (Project *project = projectForFile(fileName))
textEditor->setTextCodec(project->editorConfiguration()->defaultTextCodec());
if (Project *project = projectForFile(fileName)) {
if (QTextCodec *codec = project->editorConfiguration()->defaultTextCodec())
textEditor->setTextCodec(codec);
}
}
QString SessionManager::currentSession() const
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment