Editor: Add feature for set/unset UTF-8 BOM

Change-Id: Iec7e36b1d7a526d7fa8a8096110b91f9c4cc44f9
Reviewed-by: Leandro Melo <leandro.melo@nokia.com>
Reviewed-by: Eike Ziller <eike.ziller@nokia.com>
This commit is contained in:
Yuchen Deng
2012-03-02 08:16:23 +08:00
committed by Eike Ziller
parent a2d3d7f3bf
commit 012ff2d22a
7 changed files with 35 additions and 0 deletions

View File

@@ -140,6 +140,13 @@ void TextDocument::setCodec(const QTextCodec *codec)
d->m_format.codec = codec; d->m_format.codec = codec;
} }
void TextDocument::switchUtf8Bom()
{
if (debug)
qDebug() << Q_FUNC_INFO << this << "UTF-8 BOM: " << !d->m_format.hasUtf8Bom;
d->m_format.hasUtf8Bom = !d->m_format.hasUtf8Bom;
}
/*! /*!
\brief Returns the format obtained from the last call to read(). \brief Returns the format obtained from the last call to read().
*/ */

View File

@@ -55,6 +55,7 @@ public:
Utils::TextFileFormat format() const; Utils::TextFileFormat format() const;
const QTextCodec *codec() const; const QTextCodec *codec() const;
void setCodec(const QTextCodec *); void setCodec(const QTextCodec *);
void switchUtf8Bom();
ReadResult read(const QString &fileName, QStringList *plainTextList, QString *errorString); ReadResult read(const QString &fileName, QStringList *plainTextList, QString *errorString);
ReadResult read(const QString &fileName, QString *plainText, QString *errorString); ReadResult read(const QString &fileName, QString *plainText, QString *errorString);

View File

@@ -5838,6 +5838,11 @@ void BaseTextEditorWidget::circularPaste()
QPlainTextEdit::copy(); QPlainTextEdit::copy();
} }
void BaseTextEditorWidget::switchUtf8bom()
{
baseTextDocument()->switchUtf8Bom();
}
QMimeData *BaseTextEditorWidget::createMimeDataFromSelection() const QMimeData *BaseTextEditorWidget::createMimeDataFromSelection() const
{ {
if (d->m_inBlockSelectionMode) { if (d->m_inBlockSelectionMode) {
@@ -6087,6 +6092,17 @@ void BaseTextEditorWidget::appendStandardContextMenuActions(QMenu *menu)
a = am->command(Constants::CIRCULAR_PASTE)->action(); a = am->command(Constants::CIRCULAR_PASTE)->action();
if (a && a->isEnabled()) if (a && a->isEnabled())
menu->addAction(a); menu->addAction(a);
BaseTextDocument *doc = baseTextDocument();
if (doc->codec()->name() == QString(QLatin1String("UTF-8"))) {
a = am->command(Constants::SWITCH_UTF8BOM)->action();
if (a && a->isEnabled()) {
a->setText(doc->format().hasUtf8Bom ? tr("Delete UTF-8 BOM on Save")
: tr("Add UTF-8 BOM on Save"));
menu->addSeparator();
menu->addAction(a);
}
}
} }

View File

@@ -260,6 +260,7 @@ public slots:
virtual void selectAll(); virtual void selectAll();
void circularPaste(); void circularPaste();
void switchUtf8bom();
void zoomIn(int range = 1); void zoomIn(int range = 1);
void zoomOut(int range = 1); void zoomOut(int range = 1);

View File

@@ -65,6 +65,7 @@ TextEditorActionHandler::TextEditorActionHandler(const char *context,
m_cutAction(0), m_cutAction(0),
m_pasteAction(0), m_pasteAction(0),
m_circularPasteAction(0), m_circularPasteAction(0),
m_switchUtf8bomAction(0),
m_selectAllAction(0), m_selectAllAction(0),
m_gotoAction(0), m_gotoAction(0),
m_printAction(0), m_printAction(0),
@@ -379,6 +380,11 @@ void TextEditorActionHandler::createActions()
connect(m_circularPasteAction, SIGNAL(triggered()), this, SLOT(circularPasteAction())); connect(m_circularPasteAction, SIGNAL(triggered()), this, SLOT(circularPasteAction()));
medit->addAction(command, Core::Constants::G_EDIT_COPYPASTE); medit->addAction(command, Core::Constants::G_EDIT_COPYPASTE);
m_switchUtf8bomAction = new QAction(this);
m_modifyingActions << m_switchUtf8bomAction;
command = am->registerAction(m_switchUtf8bomAction, Constants::SWITCH_UTF8BOM, m_contextId, true);
connect(m_switchUtf8bomAction, SIGNAL(triggered()), this, SLOT(switchUtf8bomAction()));
m_indentAction = new QAction(tr("Indent"), this); m_indentAction = new QAction(tr("Indent"), this);
m_modifyingActions << m_indentAction; m_modifyingActions << m_indentAction;
command = am->registerAction(m_indentAction, Constants::INDENT, m_contextId, true); command = am->registerAction(m_indentAction, Constants::INDENT, m_contextId, true);
@@ -589,6 +595,7 @@ FUNCTION2(copyAction, copy)
FUNCTION2(cutAction, cut) FUNCTION2(cutAction, cut)
FUNCTION2(pasteAction, paste) FUNCTION2(pasteAction, paste)
FUNCTION2(circularPasteAction, circularPaste) FUNCTION2(circularPasteAction, circularPaste)
FUNCTION2(switchUtf8bomAction, switchUtf8bom)
FUNCTION2(formatAction, format) FUNCTION2(formatAction, format)
FUNCTION2(rewrapParagraphAction, rewrapParagraph) FUNCTION2(rewrapParagraphAction, rewrapParagraph)
FUNCTION2(selectAllAction, selectAll) FUNCTION2(selectAllAction, selectAll)

View File

@@ -99,6 +99,7 @@ private slots:
void cutAction(); void cutAction();
void pasteAction(); void pasteAction();
void circularPasteAction(); void circularPasteAction();
void switchUtf8bomAction();
void selectAllAction(); void selectAllAction();
void gotoAction(); void gotoAction();
void printAction(); void printAction();
@@ -170,6 +171,7 @@ private:
QAction *m_cutAction; QAction *m_cutAction;
QAction *m_pasteAction; QAction *m_pasteAction;
QAction *m_circularPasteAction; QAction *m_circularPasteAction;
QAction *m_switchUtf8bomAction;
QAction *m_selectAllAction; QAction *m_selectAllAction;
QAction *m_gotoAction; QAction *m_gotoAction;
QAction *m_printAction; QAction *m_printAction;

View File

@@ -102,6 +102,7 @@ const char TASK_DOWNLOAD_DEFINITIONS[] = "TextEditor.Task.Download";
const char TASK_REGISTER_DEFINITIONS[] = "TextEditor.Task.Register"; const char TASK_REGISTER_DEFINITIONS[] = "TextEditor.Task.Register";
const char TASK_OPEN_FILE[] = "TextEditor.Task.OpenFile"; const char TASK_OPEN_FILE[] = "TextEditor.Task.OpenFile";
const char CIRCULAR_PASTE[] = "TextEditor.CircularPaste"; const char CIRCULAR_PASTE[] = "TextEditor.CircularPaste";
const char SWITCH_UTF8BOM[] = "TextEditor.SwitchUtf8bom";
const char INDENT[] = "TextEditor.Indent"; const char INDENT[] = "TextEditor.Indent";
const char UNINDENT[] = "TextEditor.Unindent"; const char UNINDENT[] = "TextEditor.Unindent";