TextEditor: Re-organize Plain text editor setup

Change-Id: I202e66a9cd295a85e3eea31177280e2c5d409ced
Reviewed-by: Christian Stenger <christian.stenger@digia.com>
This commit is contained in:
hjk
2014-08-22 19:14:48 +02:00
parent 550db7d1d1
commit f9f5fdcf2b
4 changed files with 61 additions and 85 deletions

View File

@@ -533,6 +533,27 @@ BaseTextEditorWidgetPrivate::BaseTextEditorWidgetPrivate(BaseTextEditorWidget *p
using namespace Internal; using namespace Internal;
/*!
* Test if syntax highlighter is available (or unneeded) for \a widget.
* If not found, show a warning with a link to the relevant settings page.
*/
static void updateEditorInfoBar(BaseTextEditorWidget *widget)
{
Id infoSyntaxDefinition(Constants::INFO_SYNTAX_DEFINITION);
InfoBar *infoBar = widget->textDocument()->infoBar();
if (!widget->isMissingSyntaxDefinition()) {
infoBar->removeInfo(infoSyntaxDefinition);
} else if (infoBar->canInfoBeAdded(infoSyntaxDefinition)) {
InfoBarEntry info(infoSyntaxDefinition,
BaseTextEditor::tr("A highlight definition was not found for this file. "
"Would you like to try to find one?"),
InfoBarEntry::GlobalSuppressionEnabled);
info.setCustomButtonInfo(BaseTextEditor::tr("Show Highlighter Options..."),
widget, SLOT(acceptMissingSyntaxDefinitionInfo()));
infoBar->addInfo(info);
}
}
QString BaseTextEditorWidget::plainTextFromSelection(const QTextCursor &cursor) const QString BaseTextEditorWidget::plainTextFromSelection(const QTextCursor &cursor) const
{ {
// Copy the selected text as plain text // Copy the selected text as plain text
@@ -6512,11 +6533,9 @@ QColor BaseTextEditorWidget::replacementPenColor(int blockNumber) const
BaseTextEditor *BaseTextEditorWidget::createEditor() BaseTextEditor *BaseTextEditorWidget::createEditor()
{ {
QTC_CHECK("should not happen anymore" && false);
auto editor = new BaseTextEditor; auto editor = new BaseTextEditor;
editor->setEditorWidget(this); editor->setEditorWidget(this);
editor->setContext(Core::Context(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID,
TextEditor::Constants::C_TEXTEDITOR));
editor->setDuplicateSupported(true);
return editor; return editor;
} }
@@ -6555,6 +6574,7 @@ BaseTextEditor::BaseTextEditor()
{ {
d->m_completionAssistProvider = [] () -> CompletionAssistProvider * { return 0; }; d->m_completionAssistProvider = [] () -> CompletionAssistProvider * { return 0; };
addContext(TextEditor::Constants::C_TEXTEDITOR); addContext(TextEditor::Constants::C_TEXTEDITOR);
setDuplicateSupported(true);
} }
void BaseTextEditor::setEditorWidget(BaseTextEditorWidget *widget) void BaseTextEditor::setEditorWidget(BaseTextEditorWidget *widget)
@@ -7126,7 +7146,7 @@ void BaseTextEditorWidget::configureMimeType(const MimeType &mimeType)
textDocument()->setFontSettings(TextEditorSettings::fontSettings()); textDocument()->setFontSettings(TextEditorSettings::fontSettings());
emit configured(editor()); updateEditorInfoBar(this);
} }
bool BaseTextEditorWidget::isMissingSyntaxDefinition() const bool BaseTextEditorWidget::isMissingSyntaxDefinition() const
@@ -7141,26 +7161,27 @@ void BaseTextEditorWidget::acceptMissingSyntaxDefinitionInfo()
this); this);
} }
void BaseTextEditorWidget::configureMimeType()
{
MimeType mimeType;
if (textDocument())
mimeType = MimeDatabase::findByFile(textDocument()->filePath());
configureMimeType(mimeType);
}
// The remnants of PlainTextEditor. // The remnants of PlainTextEditor.
void BaseTextEditorWidget::setupAsPlainEditor() void BaseTextEditorWidget::setupAsPlainEditor()
{ {
setRevisionsVisible(true); setRevisionsVisible(true);
setMarksVisible(true); setMarksVisible(true);
setLineSeparatorsAllowed(true); setLineSeparatorsAllowed(true);
setLineSeparatorsAllowed(true);
textDocument()->setMimeType(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT)); textDocument()->setMimeType(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT));
connect(textDocument(), SIGNAL(filePathChanged(QString,QString)), auto reconf = [this]() {
this, SLOT(configureMimeType())); MimeType mimeType;
connect(Manager::instance(), SIGNAL(mimeTypesRegistered()), this, SLOT(configureMimeType())); if (textDocument())
mimeType = MimeDatabase::findByFile(textDocument()->filePath());
configureMimeType(mimeType);
};
connect(textDocument(), &IDocument::filePathChanged, reconf);
connect(Manager::instance(), &Manager::mimeTypesRegistered, reconf);
updateEditorInfoBar(this);
} }
IEditor *BaseTextEditor::duplicate() IEditor *BaseTextEditor::duplicate()
@@ -7169,7 +7190,7 @@ IEditor *BaseTextEditor::duplicate()
if (d->m_origin) if (d->m_origin)
return d->m_origin->duplicateTextEditor(this); return d->m_origin->duplicateTextEditor(this);
// Use standard setup if that's available. // Use old setup if that's available.
if (d->m_editorCreator) { if (d->m_editorCreator) {
BaseTextEditor *editor = d->m_editorCreator(); BaseTextEditor *editor = d->m_editorCreator();
BaseTextEditorWidget *widget = editor->ensureWidget(); BaseTextEditorWidget *widget = editor->ensureWidget();
@@ -7178,16 +7199,9 @@ IEditor *BaseTextEditor::duplicate()
return editor; return editor;
} }
// That's a really plain text editor.
auto newWidget = new BaseTextEditorWidget;
newWidget->setTextDocument(editorWidget()->textDocumentPtr());
newWidget->setupAsPlainEditor();
auto editor = newWidget->editor();
editor->addContext(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID);
editor->setDuplicateSupported(true);
return editor;
// If neither is sufficient, you need to implement 'YourEditor::duplicate'. // If neither is sufficient, you need to implement 'YourEditor::duplicate'.
QTC_CHECK(false);
return 0;
} }
QWidget *BaseTextEditor::widget() const QWidget *BaseTextEditor::widget() const
@@ -7228,6 +7242,7 @@ BaseTextDocumentPtr BaseTextEditor::ensureDocument()
BaseTextEditorFactory::BaseTextEditorFactory(QObject *parent) BaseTextEditorFactory::BaseTextEditorFactory(QObject *parent)
: IEditorFactory(parent) : IEditorFactory(parent)
{ {
m_editorCreator = []() { return new BaseTextEditor; };
m_widgetCreator = []() { return new BaseTextEditorWidget; }; m_widgetCreator = []() { return new BaseTextEditorWidget; };
} }

View File

@@ -504,8 +504,6 @@ signals:
void requestZoomReset(); void requestZoomReset();
void requestBlockUpdate(const QTextBlock &); void requestBlockUpdate(const QTextBlock &);
void configured(Core::IEditor *editor);
protected: protected:
bool event(QEvent *e); bool event(QEvent *e);
void inputMethodEvent(QInputMethodEvent *e); void inputMethodEvent(QInputMethodEvent *e);
@@ -612,7 +610,6 @@ protected slots:
virtual void slotCursorPositionChanged(); // Used in VcsBase virtual void slotCursorPositionChanged(); // Used in VcsBase
virtual void slotCodeStyleSettingsChanged(const QVariant &); // Used in CppEditor virtual void slotCodeStyleSettingsChanged(const QVariant &); // Used in CppEditor
void configureMimeType();
void doFoo(); void doFoo();
private: private:

View File

@@ -38,64 +38,36 @@
#include <coreplugin/coreconstants.h> #include <coreplugin/coreconstants.h>
#include <coreplugin/infobar.h> #include <coreplugin/infobar.h>
#include <utils/qtcassert.h>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
using namespace TextEditor; namespace TextEditor {
using namespace TextEditor::Internal; namespace Internal {
PlainTextEditorFactory::PlainTextEditorFactory(QObject *parent) class PlainTextEditorWidget : public BaseTextEditorWidget
: Core::IEditorFactory(parent) {
public:
PlainTextEditorWidget() {}
void finalizeInitialization() { setupAsPlainEditor(); }
};
PlainTextEditorFactory::PlainTextEditorFactory()
{ {
setId(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); setId(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID);
setDisplayName(qApp->translate("OpenWith::Editors", Core::Constants::K_DEFAULT_TEXT_EDITOR_DISPLAY_NAME)); setDisplayName(qApp->translate("OpenWith::Editors", Core::Constants::K_DEFAULT_TEXT_EDITOR_DISPLAY_NAME));
addMimeType(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT)); addMimeType(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT));
new TextEditorActionHandler(this, setDocumentCreator([]() { return new BaseTextDocument(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); });
Core::Constants::K_DEFAULT_TEXT_EDITOR_ID, setEditorWidgetCreator([]() { return new PlainTextEditorWidget; });
setIndenterCreator([]() { return new NormalIndenter; });
setEditorActionHandlers(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID,
TextEditorActionHandler::Format | TextEditorActionHandler::Format |
TextEditorActionHandler::UnCommentSelection | TextEditorActionHandler::UnCommentSelection |
TextEditorActionHandler::UnCollapseAll); TextEditorActionHandler::UnCollapseAll);
} }
Core::IEditor *PlainTextEditorFactory::createEditor() } // namespace Internal
{ } // namespace TextEditor
BaseTextDocumentPtr doc(new BaseTextDocument(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID));
doc->setIndenter(new NormalIndenter);
auto widget = new BaseTextEditorWidget;
widget->setTextDocument(doc);
widget->setupAsPlainEditor();
connect(widget, &BaseTextEditorWidget::configured,
this, &PlainTextEditorFactory::updateEditorInfoBar);
updateEditorInfoBar(widget->editor());
return widget->editor();
}
/*!
* Test if syntax highlighter is available (or unneeded) for \a editor.
* If not found, show a warning with a link to the relevant settings page.
*/
void PlainTextEditorFactory::updateEditorInfoBar(Core::IEditor *editor)
{
BaseTextEditor *textEditor = qobject_cast<BaseTextEditor *>(editor);
if (textEditor) {
Core::IDocument *file = editor->document();
if (!file)
return;
BaseTextEditorWidget *widget = textEditor->editorWidget();
Core::Id infoSyntaxDefinition(Constants::INFO_SYNTAX_DEFINITION);
Core::InfoBar *infoBar = file->infoBar();
if (!widget->isMissingSyntaxDefinition()) {
infoBar->removeInfo(infoSyntaxDefinition);
} else if (infoBar->canInfoBeAdded(infoSyntaxDefinition)) {
Core::InfoBarEntry info(infoSyntaxDefinition,
tr("A highlight definition was not found for this file. "
"Would you like to try to find one?"),
Core::InfoBarEntry::GlobalSuppressionEnabled);
info.setCustomButtonInfo(tr("Show Highlighter Options..."),
widget, SLOT(acceptMissingSyntaxDefinitionInfo()));
infoBar->addInfo(info);
}
}
}

View File

@@ -30,25 +30,17 @@
#ifndef PLAINTEXTEDITORFACTORY_H #ifndef PLAINTEXTEDITORFACTORY_H
#define PLAINTEXTEDITORFACTORY_H #define PLAINTEXTEDITORFACTORY_H
#include <coreplugin/editormanager/ieditorfactory.h> #include <texteditor/basetexteditor.h>
#include <QStringList>
namespace TextEditor { namespace TextEditor {
namespace Internal { namespace Internal {
class PlainTextEditorFactory : public Core::IEditorFactory class PlainTextEditorFactory : public TextEditor::BaseTextEditorFactory
{ {
Q_OBJECT Q_OBJECT
public: public:
PlainTextEditorFactory(QObject *parent = 0); PlainTextEditorFactory();
using Core::IEditorFactory::addMimeType;
Core::IEditor *createEditor();
private slots:
void updateEditorInfoBar(Core::IEditor *editor);
}; };
} // namespace Internal } // namespace Internal