Commit a9b70458 authored by hjk's avatar hjk
Browse files

TextEditor: Move toolbar widgets from *Editor to *Widget



Lifetime is coupled to the widgets, not the editor.

Change-Id: I01560bbdd6c399376d3a4184f0eb83e4153f830e
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent ac00146a
......@@ -277,8 +277,8 @@ void CppEditorWidget::createToolBar(CPPEditor *editor)
connect(cmd, SIGNAL(keySequenceChanged()), this, SLOT(updatePreprocessorButtonTooltip()));
updatePreprocessorButtonTooltip();
connect(d->m_preprocessorButton, SIGNAL(clicked()), this, SLOT(showPreProcessorWidget()));
editor->insertExtraToolBarWidget(TextEditor::BaseTextEditor::Left, d->m_preprocessorButton);
editor->insertExtraToolBarWidget(TextEditor::BaseTextEditor::Left, d->m_cppEditorOutline->widget());
insertExtraToolBarWidget(TextEditor::BaseTextEditorWidget::Left, d->m_preprocessorButton);
insertExtraToolBarWidget(TextEditor::BaseTextEditorWidget::Left, d->m_cppEditorOutline->widget());
}
void CppEditorWidget::paste()
......
......@@ -248,7 +248,7 @@ void GlslEditorWidget::createToolBar(GlslEditor *editor)
policy.setHorizontalPolicy(QSizePolicy::Expanding);
m_outlineCombo->setSizePolicy(policy);
editor->insertExtraToolBarWidget(TextEditor::BaseTextEditor::Left, m_outlineCombo);
insertExtraToolBarWidget(TextEditor::BaseTextEditorWidget::Left, m_outlineCombo);
}
void GlslEditorWidget::updateDocument()
......
......@@ -578,7 +578,7 @@ void QmlJSTextEditorWidget::createToolBar(QmlJSEditor *editor)
connect(this, SIGNAL(cursorPositionChanged()), m_updateOutlineIndexTimer, SLOT(start()));
editor->insertExtraToolBarWidget(TextEditor::BaseTextEditor::Left, m_outlineCombo);
editor->editorWidget()->insertExtraToolBarWidget(TextEditor::BaseTextEditorWidget::Left, m_outlineCombo);
}
TextEditor::BaseTextEditorWidget::Link QmlJSTextEditorWidget::findLinkAt(const QTextCursor &cursor,
......
......@@ -195,12 +195,6 @@ public:
// Note: This is always a copy of IContext::m_widget.
BaseTextEditorWidget *m_editorWidget;
QToolBar *m_toolBar;
QWidget *m_stretchWidget;
QAction *m_cursorPositionLabelAction;
Utils::LineColumnLabel *m_cursorPositionLabel;
QAction *m_fileEncodingLabelAction;
Utils::LineColumnLabel *m_fileEncodingLabel;
CommentDefinition m_commentDefinition;
std::function<CompletionAssistProvider *()> m_completionAssistProvider;
QScopedPointer<AutoCompleter> m_autoCompleter;
......@@ -210,6 +204,7 @@ class BaseTextEditorWidgetPrivate : public QObject
{
public:
BaseTextEditorWidgetPrivate(BaseTextEditorWidget *parent);
~BaseTextEditorWidgetPrivate() { delete m_toolBar; }
// FIXME: Remove after relevant members have been moved to BaseTextEditorPrivate
BaseTextEditorPrivate *dd() { return q->editor()->d; }
......@@ -274,6 +269,8 @@ public:
void highlightSearchResultsSlot(const QString &txt, Core::FindFlags findFlags);
void setFindScope(const QTextCursor &start, const QTextCursor &end, int, int);
void updateCursorPosition();
// parentheses matcher
void _q_matchParentheses();
void _q_highlightBlocks();
......@@ -283,6 +280,13 @@ public:
public:
BaseTextEditorWidget *q;
QToolBar *m_toolBar;
QWidget *m_stretchWidget;
Utils::LineColumnLabel *m_cursorPositionLabel;
Utils::LineColumnLabel *m_fileEncodingLabel;
QAction *m_cursorPositionLabelAction;
QAction *m_fileEncodingLabelAction;
bool m_contentsChanged;
bool m_lastCursorChangeWasInteresting;
......@@ -400,6 +404,12 @@ public:
BaseTextEditorWidgetPrivate::BaseTextEditorWidgetPrivate(BaseTextEditorWidget *parent)
: q(parent),
m_toolBar(0),
m_stretchWidget(0),
m_cursorPositionLabel(0),
m_fileEncodingLabel(0),
m_cursorPositionLabelAction(0),
m_fileEncodingLabelAction(0),
m_contentsChanged(false),
m_lastCursorChangeWasInteresting(false),
m_parenthesesMatchingEnabled(false),
......@@ -452,7 +462,32 @@ BaseTextEditorWidgetPrivate::BaseTextEditorWidgetPrivate(BaseTextEditorWidget *p
this, &BaseTextEditorWidgetPrivate::setFindScope);
aggregate->add(baseTextFind);
aggregate->add(q);
m_stretchWidget = new QWidget;
m_stretchWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
m_toolBar = new QToolBar;
m_toolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
m_toolBar->addWidget(m_stretchWidget);
m_cursorPositionLabel = new Utils::LineColumnLabel;
const int spacing = q->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) / 2;
m_cursorPositionLabel->setContentsMargins(spacing, 0, spacing, 0);
m_fileEncodingLabel = new Utils::LineColumnLabel;
m_fileEncodingLabel->setContentsMargins(spacing, 0, spacing, 0);
m_cursorPositionLabelAction = m_toolBar->addWidget(m_cursorPositionLabel);
m_fileEncodingLabelAction = m_toolBar->addWidget(m_fileEncodingLabel);
connect(m_cursorPositionLabel, &LineColumnLabel::clicked, [this] {
Core::EditorManager::activateEditor(q->editor(), Core::EditorManager::IgnoreNavigationHistory);
if (Core::Command *cmd = Core::ActionManager::command(Core::Constants::GOTO)) {
if (QAction *act = cmd->action())
act->trigger();
}
});
}
class TextEditExtraArea : public QWidget
{
public:
......@@ -582,6 +617,9 @@ void BaseTextEditorWidgetPrivate::ctor(const QSharedPointer<BaseTextDocument> &d
QObject::connect(q, &QPlainTextEdit::cursorPositionChanged,
q, &BaseTextEditorWidget::slotCursorPositionChanged);
QObject::connect(q, &QPlainTextEdit::cursorPositionChanged,
this, &BaseTextEditorWidgetPrivate::updateCursorPosition);
QObject::connect(q, &QPlainTextEdit::updateRequest,
this, &BaseTextEditorWidgetPrivate::slotUpdateRequest);
......@@ -871,7 +909,8 @@ void BaseTextEditorWidget::selectEncoding()
void BaseTextEditorWidget::updateTextCodecLabel()
{
editor()->setFileEncodingLabelText(QString::fromLatin1(d->m_document->codec()->name()));
QString text = QString::fromLatin1(d->m_document->codec()->name());
d->m_fileEncodingLabel->setText(text, text);
}
QString BaseTextEditorWidget::msgTextTooLarge(quint64 size)
......@@ -919,8 +958,8 @@ bool BaseTextEditorWidget::open(QString *errorString, const QString &fileName, c
if (d->m_document->open(errorString, fileName, realFileName)) {
moveCursor(QTextCursor::Start);
d->updateCannotDecodeInfo();
if (editor()->fileEncodingLabel()) {
connect(editor()->fileEncodingLabel(), SIGNAL(clicked()), this,
if (d->m_fileEncodingLabel) {
connect(d->m_fileEncodingLabel, SIGNAL(clicked()), this,
SLOT(selectEncoding()), Qt::UniqueConnection);
connect(d->m_document->document(), SIGNAL(modificationChanged(bool)), this,
SLOT(updateTextCodecLabel()), Qt::UniqueConnection);
......@@ -6000,7 +6039,7 @@ void BaseTextEditorWidget::setDisplaySettings(const DisplaySettings &ds)
setHighlightCurrentLine(ds.m_highlightCurrentLine);
setRevisionsVisible(ds.m_markTextChanges);
setCenterOnScroll(ds.m_centerCursorOnScroll);
editor()->setFileEncodingLabelVisible(ds.m_displayFileEncoding);
d->m_fileEncodingLabelAction->setVisible(ds.m_displayFileEncoding);
if (d->m_displaySettings.m_visualizeWhitespace != ds.m_visualizeWhitespace) {
if (SyntaxHighlighter *highlighter = baseTextDocument()->syntaxHighlighter())
......@@ -6473,36 +6512,11 @@ BaseTextEditor::BaseTextEditor(BaseTextEditorWidget *widget)
setWidget(widget);
d->m_editorWidget = widget;
d->m_cursorPositionLabel = new Utils::LineColumnLabel;
const int spacing = widget->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) / 2;
d->m_cursorPositionLabel->setContentsMargins(spacing, 0, spacing, 0);
d->m_fileEncodingLabel = new Utils::LineColumnLabel;
d->m_fileEncodingLabel->setContentsMargins(spacing, 0, spacing, 0);
d->m_stretchWidget = new QWidget;
d->m_stretchWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
d->m_toolBar = new QToolBar;
d->m_toolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
d->m_toolBar->addWidget(d->m_stretchWidget);
d->m_cursorPositionLabelAction = d->m_toolBar->addWidget(d->m_cursorPositionLabel);
d->m_fileEncodingLabelAction = d->m_toolBar->addWidget(d->m_fileEncodingLabel);
d->m_completionAssistProvider = [] () -> CompletionAssistProvider * { return 0; };
setFileEncodingLabelVisible(widget->displaySettings().m_displayFileEncoding);
connect(widget, &QPlainTextEdit::cursorPositionChanged,
this, &BaseTextEditor::updateCursorPosition);
connect(d->m_cursorPositionLabel, &LineColumnLabel::clicked,
this, &BaseTextEditor::openGotoLocator);
}
BaseTextEditor::~BaseTextEditor()
{
delete d->m_toolBar;
delete d->m_editorWidget;
delete d;
}
......@@ -6519,10 +6533,10 @@ IDocument *BaseTextEditor::document()
QWidget *BaseTextEditor::toolBar()
{
return d->m_toolBar;
return d->m_editorWidget->d->m_toolBar;
}
void BaseTextEditor::insertExtraToolBarWidget(BaseTextEditor::Side side,
void BaseTextEditorWidget::insertExtraToolBarWidget(BaseTextEditorWidget::Side side,
QWidget *widget)
{
if (widget->sizePolicy().horizontalPolicy() & QSizePolicy::ExpandFlag) {
......@@ -6648,45 +6662,21 @@ void BaseTextEditor::setCompletionAssistProvider(const std::function<CompletionA
d->m_completionAssistProvider = provider;
}
QObject *BaseTextEditor::fileEncodingLabel() const
{
return d->m_fileEncodingLabel;
}
void BaseTextEditor::updateCursorPosition()
void BaseTextEditorWidgetPrivate::updateCursorPosition()
{
const QTextCursor cursor = d->m_editorWidget->textCursor();
const QTextCursor cursor = q->textCursor();
const QTextBlock block = cursor.block();
const int line = block.blockNumber() + 1;
const int column = cursor.position() - block.position();
d->m_cursorPositionLabel->setText(tr("Line: %1, Col: %2").arg(line)
.arg(baseTextDocument()->tabSettings().columnAt(block.text(),
m_cursorPositionLabel->show();
m_cursorPositionLabel->setText(tr("Line: %1, Col: %2").arg(line)
.arg(q->baseTextDocument()->tabSettings().columnAt(block.text(),
column)+1),
tr("Line: 9999, Col: 999"));
m_contextHelpId.clear();
q->editor()->m_contextHelpId.clear();
if (!block.isVisible())
d->m_editorWidget->ensureCursorVisible();
}
void BaseTextEditor::openGotoLocator()
{
Core::EditorManager::activateEditor(this, Core::EditorManager::IgnoreNavigationHistory);
if (Core::Command *cmd = Core::ActionManager::command(Core::Constants::GOTO)) {
if (QAction *act = cmd->action())
act->trigger();
}
}
void BaseTextEditor::setFileEncodingLabelVisible(bool visible)
{
d->m_fileEncodingLabelAction->setVisible(visible);
}
void BaseTextEditor::setFileEncodingLabelText(const QString &text)
{
d->m_fileEncodingLabel->setText(text, text);
q->ensureCursorVisible();
}
QString BaseTextEditor::contextHelpId() const
......
......@@ -143,9 +143,6 @@ public:
bool restoreState(const QByteArray &state);
QWidget *toolBar();
enum Side { Left, Right };
void insertExtraToolBarWidget(Side side, QWidget *widget);
QString contextHelpId() const; // from IContext
int currentLine() const;
......@@ -192,8 +189,6 @@ public:
// There should be something simpler.
void setCompletionAssistProvider(const std::function<CompletionAssistProvider *()> &provider);
QObject *fileEncodingLabel() const; // FIXME: Remove
void setAutoCompleter(AutoCompleter *autoCompleter);
AutoCompleter *autoCompleter() const;
......@@ -205,12 +200,6 @@ signals:
void markTooltipRequested(TextEditor::BaseTextEditor *editor, const QPoint &globalPos, int line);
void contextHelpIdRequested(TextEditor::BaseTextEditor *editor, int position);
private slots:
void updateCursorPosition();
void openGotoLocator();
void setFileEncodingLabelVisible(bool visible);
void setFileEncodingLabelText(const QString &text);
private:
friend class Internal::BaseTextEditorWidgetPrivate;
friend class BaseTextEditorWidget;
......@@ -372,6 +361,9 @@ public:
void setIfdefedOutBlocks(const QList<BlockRange> &blocks);
bool isMissingSyntaxDefinition() const;
enum Side { Left, Right };
void insertExtraToolBarWidget(Side side, QWidget *widget);
public slots:
virtual void copy();
virtual void paste();
......
......@@ -614,7 +614,8 @@ QComboBox *VcsBaseEditorWidgetPrivate::entriesComboBox()
policy.setHorizontalPolicy(QSizePolicy::Expanding);
m_entriesComboBox->setSizePolicy(policy);
m_editor->insertExtraToolBarWidget(TextEditor::BaseTextEditor::Left, m_entriesComboBox);
m_editor->editorWidget()->insertExtraToolBarWidget
(TextEditor::BaseTextEditorWidget::Left, m_entriesComboBox);
return m_entriesComboBox;
}
......@@ -1337,7 +1338,7 @@ bool VcsBaseEditorWidget::setConfigurationWidget(VcsBaseEditorParameterWidget *w
return false;
d->m_configurationWidget = w;
d->m_editor->insertExtraToolBarWidget(TextEditor::BaseTextEditor::Right, w);
d->m_editor->editorWidget()->insertExtraToolBarWidget(TextEditor::BaseTextEditorWidget::Right, w);
return true;
}
......
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