forked from qt-creator/qt-creator
Make LineColumnButton re-usable
Change-Id: I34e755018eef41e7e7cf254ccba72a1ec5a1c5ff Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -148,114 +148,118 @@ using ListTransformationMethod = void(QStringList &);
|
|||||||
|
|
||||||
static constexpr char dropProperty[] = "dropProp";
|
static constexpr char dropProperty[] = "dropProp";
|
||||||
|
|
||||||
class LineColumnButton : public QToolButton
|
class LineColumnButtonPrivate
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
|
||||||
public:
|
public:
|
||||||
LineColumnButton(TextEditorWidget *parent)
|
QSize m_maxSize;
|
||||||
: QToolButton(parent)
|
TextEditorWidget *m_editor;
|
||||||
, m_editor(parent)
|
};
|
||||||
{
|
|
||||||
connect(m_editor, &QPlainTextEdit::cursorPositionChanged, this, &LineColumnButton::update);
|
} // namespace Internal
|
||||||
connect(this, &QToolButton::pressed, ActionManager::instance(), [this] {
|
|
||||||
emit m_editor->activateEditor(EditorManager::IgnoreNavigationHistory);
|
LineColumnButton::LineColumnButton(TextEditorWidget *parent)
|
||||||
QMetaObject::invokeMethod(ActionManager::instance(), [] {
|
: QToolButton(parent)
|
||||||
|
, m_d(new LineColumnButtonPrivate)
|
||||||
|
{
|
||||||
|
m_d->m_editor = parent;
|
||||||
|
connect(m_d->m_editor, &QPlainTextEdit::cursorPositionChanged, this, &LineColumnButton::update);
|
||||||
|
connect(this, &QToolButton::clicked, ActionManager::instance(), [this] {
|
||||||
|
emit m_d->m_editor->activateEditor(EditorManager::IgnoreNavigationHistory);
|
||||||
|
QMetaObject::invokeMethod(
|
||||||
|
ActionManager::instance(),
|
||||||
|
[] {
|
||||||
if (Command *cmd = ActionManager::command(Core::Constants::GOTO)) {
|
if (Command *cmd = ActionManager::command(Core::Constants::GOTO)) {
|
||||||
if (QAction *act = cmd->action())
|
if (QAction *act = cmd->action())
|
||||||
act->trigger();
|
act->trigger();
|
||||||
}
|
}
|
||||||
}, Qt::QueuedConnection);
|
},
|
||||||
});
|
Qt::QueuedConnection);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
LineColumnButton::~LineColumnButton() = default;
|
||||||
|
|
||||||
|
void LineColumnButton::update()
|
||||||
|
{
|
||||||
|
const Utils::MultiTextCursor &cursors = m_d->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 int line = block.blockNumber() + 1;
|
||||||
|
const TabSettings &tabSettings = m_d->m_editor->textDocument()->tabSettings();
|
||||||
|
const int column = tabSettings.columnAt(block.text(), cursor.positionInBlock()) + 1;
|
||||||
|
text = Tr::tr("Line: %1, Col: %2").arg(line).arg(column);
|
||||||
|
const QString toolTipText = Tr::tr("Cursor position: %1");
|
||||||
|
setToolTip(toolTipText.arg(cursor.position()));
|
||||||
}
|
}
|
||||||
|
int selection = 0;
|
||||||
|
for (const QTextCursor &cursor : cursors)
|
||||||
|
selection += cursor.selectionEnd() - cursor.selectionStart();
|
||||||
|
if (selection > 0)
|
||||||
|
text += " " + Tr::tr("(Sel: %1)").arg(selection);
|
||||||
|
setText(text);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
bool LineColumnButton::event(QEvent *event)
|
||||||
void update()
|
{
|
||||||
{
|
if (event->type() != QEvent::ToolTip)
|
||||||
const Utils::MultiTextCursor &cursors = m_editor->multiTextCursor();
|
return QToolButton::event(event);
|
||||||
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 int line = block.blockNumber() + 1;
|
|
||||||
const TabSettings &tabSettings = m_editor->textDocument()->tabSettings();
|
|
||||||
const int column = tabSettings.columnAt(block.text(), cursor.positionInBlock()) + 1;
|
|
||||||
text = Tr::tr("Line: %1, Col: %2").arg(line).arg(column);
|
|
||||||
const QString toolTipText = Tr::tr("Cursor position: %1");
|
|
||||||
setToolTip(toolTipText.arg(cursor.position()));
|
|
||||||
}
|
|
||||||
int selection = 0;
|
|
||||||
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
|
QString tooltipText = "<table cellpadding='2'>\n";
|
||||||
{
|
|
||||||
if (event->type() != QEvent::ToolTip)
|
|
||||||
return QToolButton::event(event);
|
|
||||||
|
|
||||||
QString tooltipText = "<table cellpadding='2'>\n";
|
const MultiTextCursor multiCursor = m_d->m_editor->multiTextCursor();
|
||||||
|
const QList<QTextCursor> cursors = multiCursor.cursors().mid(0, 15);
|
||||||
|
|
||||||
const MultiTextCursor multiCursor = m_editor->multiTextCursor();
|
tooltipText += "<tr>";
|
||||||
const QList<QTextCursor> cursors = multiCursor.cursors().mid(0, 15);
|
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 += "<tr>";
|
||||||
tooltipText += QString("<th align='left'>%1</th>").arg(Tr::tr("Cursors:"));
|
tooltipText += QString("<th align='left'>%1</th>").arg(header);
|
||||||
tooltipText += QString("<td>%1</td>").arg(multiCursor.cursorCount());
|
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";
|
tooltipText += "</tr>\n";
|
||||||
|
};
|
||||||
|
|
||||||
auto addRow = [&](const QString header, auto cellText) {
|
addRow(Tr::tr("Line:"), [](const QTextCursor &c) { return c.blockNumber() + 1; });
|
||||||
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_d->m_editor->textDocument()->tabSettings();
|
||||||
|
addRow(Tr::tr("Column:"), [&](const QTextCursor &c) {
|
||||||
|
return tabSettings.columnAt(c.block().text(), c.positionInBlock()) + 1;
|
||||||
|
});
|
||||||
|
|
||||||
const TabSettings &tabSettings = m_editor->textDocument()->tabSettings();
|
addRow(Tr::tr("Selection length:"),
|
||||||
addRow(Tr::tr("Column:"), [&](const QTextCursor &c) {
|
[](const QTextCursor &c) { return c.selectionEnd() - c.selectionStart(); });
|
||||||
return tabSettings.columnAt(c.block().text(), c.positionInBlock()) + 1;
|
|
||||||
});
|
|
||||||
|
|
||||||
addRow(Tr::tr("Selection length:"),
|
addRow(Tr::tr("Position in document:"), [](const QTextCursor &c) { return c.position(); });
|
||||||
[](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(); });
|
||||||
|
|
||||||
addRow(Tr::tr("Anchor:"), [](const QTextCursor &c) { return c.anchor(); });
|
tooltipText += "</table>\n";
|
||||||
|
|
||||||
tooltipText += "</table>\n";
|
ToolTip::show(static_cast<const QHelpEvent *>(event)->globalPos(), tooltipText, Qt::RichText);
|
||||||
|
event->accept();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
ToolTip::show(static_cast<const QHelpEvent *>(event)->globalPos(),
|
QSize LineColumnButton::sizeHint() const
|
||||||
tooltipText,
|
{
|
||||||
Qt::RichText);
|
const QSize size = QToolButton::sizeHint();
|
||||||
event->accept();
|
auto wider = [](const QSize &left, const QSize &right) { return left.width() < right.width(); };
|
||||||
return true;
|
if (m_d->m_editor->multiTextCursor().hasSelection())
|
||||||
}
|
return std::max(m_d->m_maxSize, size, wider); // do not save the size if we have a selection
|
||||||
|
m_d->m_maxSize = std::max(m_d->m_maxSize, size, wider);
|
||||||
|
return m_d->m_maxSize;
|
||||||
|
}
|
||||||
|
|
||||||
QSize sizeHint() const override
|
namespace Internal {
|
||||||
{
|
|
||||||
const QSize size = QToolButton::sizeHint();
|
|
||||||
auto wider = [](const QSize &left, const QSize &right) {
|
|
||||||
return left.width() < right.width();
|
|
||||||
};
|
|
||||||
if (m_editor->multiTextCursor().hasSelection())
|
|
||||||
return std::max(m_maxSize, size, wider); // do not save the size if we have a selection
|
|
||||||
m_maxSize = std::max(m_maxSize, size, wider);
|
|
||||||
return m_maxSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
mutable QSize m_maxSize;
|
|
||||||
TextEditorWidget *m_editor;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TextEditorAnimator : public QObject
|
class TextEditorAnimator : public QObject
|
||||||
{
|
{
|
||||||
|
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include <QPlainTextEdit>
|
#include <QPlainTextEdit>
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
|
#include <QToolButton>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -56,6 +57,7 @@ using TextMarks = QList<TextMark *>;
|
|||||||
|
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
class BaseTextEditorPrivate;
|
class BaseTextEditorPrivate;
|
||||||
|
class LineColumnButtonPrivate;
|
||||||
class TextEditorFactoryPrivate;
|
class TextEditorFactoryPrivate;
|
||||||
class TextEditorWidgetPrivate;
|
class TextEditorWidgetPrivate;
|
||||||
class TextEditorOverlay;
|
class TextEditorOverlay;
|
||||||
@@ -699,6 +701,21 @@ private:
|
|||||||
Internal::TextEditorFactoryPrivate *d;
|
Internal::TextEditorFactoryPrivate *d;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class TEXTEDITOR_EXPORT LineColumnButton : public QToolButton
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LineColumnButton(TextEditorWidget *parent);
|
||||||
|
~LineColumnButton();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void update();
|
||||||
|
bool event(QEvent *event) override;
|
||||||
|
QSize sizeHint() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<Internal::LineColumnButtonPrivate> m_d;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace TextEditor
|
} // namespace TextEditor
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
Reference in New Issue
Block a user