forked from qt-creator/qt-creator
Editor: modernize line column label
This change contains various adjustments to the line column label: - Display the number of selected characters - Show the number of cursors instead of the position when multiple text cursors are present in the current editor - show detailed information for each cursor in the line column tooltip Fixes: QTCREATORBUG-29381 Change-Id: Ib4f36b290797f1d84f268deeb9556e78ae921c37 Reviewed-by: Christian Stenger <christian.stenger@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: André Hartmann <aha_1980@gmx.de>
This commit is contained in:
@@ -157,7 +157,6 @@ public:
|
|||||||
: FixedSizeClickLabel(parent)
|
: FixedSizeClickLabel(parent)
|
||||||
, m_editor(parent)
|
, m_editor(parent)
|
||||||
{
|
{
|
||||||
setMaxText(Tr::tr("Line: 9999, Col: 999"));
|
|
||||||
connect(m_editor, &QPlainTextEdit::cursorPositionChanged, this, &LineColumnLabel::update);
|
connect(m_editor, &QPlainTextEdit::cursorPositionChanged, this, &LineColumnLabel::update);
|
||||||
connect(this, &FixedSizeClickLabel::clicked, ActionManager::instance(), [this] {
|
connect(this, &FixedSizeClickLabel::clicked, ActionManager::instance(), [this] {
|
||||||
emit m_editor->activateEditor(EditorManager::IgnoreNavigationHistory);
|
emit m_editor->activateEditor(EditorManager::IgnoreNavigationHistory);
|
||||||
@@ -173,18 +172,74 @@ public:
|
|||||||
private:
|
private:
|
||||||
void update()
|
void update()
|
||||||
{
|
{
|
||||||
const QTextCursor cursor = m_editor->textCursor();
|
const Utils::MultiTextCursor &cursors = m_editor->multiTextCursor();
|
||||||
|
QString text;
|
||||||
|
if (cursors.hasMultipleCursors()) {
|
||||||
|
text = Tr::tr("Cursors: %2").arg(cursors.cursorCount());
|
||||||
|
} else {
|
||||||
|
const QTextCursor cursor = cursors.mainCursor();
|
||||||
const QTextBlock block = cursor.block();
|
const QTextBlock block = cursor.block();
|
||||||
const int line = block.blockNumber() + 1;
|
const int line = block.blockNumber() + 1;
|
||||||
const TabSettings &tabSettings = m_editor->textDocument()->tabSettings();
|
const TabSettings &tabSettings = m_editor->textDocument()->tabSettings();
|
||||||
const int column = tabSettings.columnAt(block.text(), cursor.positionInBlock()) + 1;
|
const int column = tabSettings.columnAt(block.text(), cursor.positionInBlock()) + 1;
|
||||||
const QString text = Tr::tr("Line: %1, Col: %2");
|
text = Tr::tr("Line: %1, Col: %2").arg(line).arg(column);
|
||||||
setText(text.arg(line).arg(column));
|
|
||||||
const QString toolTipText = Tr::tr("Cursor position: %1");
|
const QString toolTipText = Tr::tr("Cursor position: %1");
|
||||||
setToolTip(toolTipText.arg(QString::number(cursor.position())));
|
setToolTip(toolTipText.arg(cursor.position()));
|
||||||
QFont f = font();
|
}
|
||||||
f.setItalic(m_editor->multiTextCursor().hasMultipleCursors());
|
int selection = 0;
|
||||||
setFont(f);
|
for (const QTextCursor &cursor : cursors)
|
||||||
|
selection += cursor.selectionEnd() - cursor.selectionStart();
|
||||||
|
if (selection > 0)
|
||||||
|
text += " " + Tr::tr("(Sel: %1)").arg(selection);
|
||||||
|
setText(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool event(QEvent *event) override
|
||||||
|
{
|
||||||
|
if (event->type() != QEvent::ToolTip)
|
||||||
|
return FixedSizeClickLabel::event(event);
|
||||||
|
|
||||||
|
QString tooltipText = "<table cellpadding='2'>\n";
|
||||||
|
|
||||||
|
const MultiTextCursor multiCursor = m_editor->multiTextCursor();
|
||||||
|
const QList<QTextCursor> cursors = multiCursor.cursors().mid(0, 15);
|
||||||
|
|
||||||
|
tooltipText += "<tr>";
|
||||||
|
tooltipText += QString("<th align='left'>%1</th>").arg(Tr::tr("Cursors:"));
|
||||||
|
tooltipText += QString("<td>%1</td>").arg(multiCursor.cursorCount());
|
||||||
|
tooltipText += "</tr>\n";
|
||||||
|
|
||||||
|
auto addRow = [&](const QString header, auto cellText) {
|
||||||
|
tooltipText += "<tr>";
|
||||||
|
tooltipText += QString("<th align='left'>%1</th>").arg(header);
|
||||||
|
for (const QTextCursor &c : cursors)
|
||||||
|
tooltipText += QString("<td>%1</td>").arg(cellText(c));
|
||||||
|
if (multiCursor.cursorCount() > cursors.count())
|
||||||
|
tooltipText += QString("<td>...</td>");
|
||||||
|
tooltipText += "</tr>\n";
|
||||||
|
};
|
||||||
|
|
||||||
|
addRow(Tr::tr("Line:"), [](const QTextCursor &c) { return c.blockNumber() + 1; });
|
||||||
|
|
||||||
|
const TabSettings &tabSettings = m_editor->textDocument()->tabSettings();
|
||||||
|
addRow(Tr::tr("Column:"), [&](const QTextCursor &c) {
|
||||||
|
return tabSettings.columnAt(c.block().text(), c.positionInBlock()) + 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
addRow(Tr::tr("Selection length:"),
|
||||||
|
[](const QTextCursor &c) { return c.selectionEnd() - c.selectionStart(); });
|
||||||
|
|
||||||
|
addRow(Tr::tr("Position in document:"), [](const QTextCursor &c) { return c.position(); });
|
||||||
|
|
||||||
|
addRow(Tr::tr("Anchor:"), [](const QTextCursor &c) { return c.anchor(); });
|
||||||
|
|
||||||
|
tooltipText += "</table>\n";
|
||||||
|
|
||||||
|
ToolTip::show(static_cast<const QHelpEvent *>(event)->globalPos(),
|
||||||
|
tooltipText,
|
||||||
|
Qt::RichText);
|
||||||
|
event->accept();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
TextEditorWidget *m_editor;
|
TextEditorWidget *m_editor;
|
||||||
|
|||||||
Reference in New Issue
Block a user