Lua: Extend addFloatingWidget with x position arg

Change-Id: I50ee58885c06dfc13e8a7dff1392b2dfbfb5fd3a
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Artur Twardy
2024-10-24 09:45:51 +02:00
parent f789087c4a
commit b6b7f31ec9
2 changed files with 16 additions and 14 deletions

View File

@@ -53,10 +53,11 @@ void fillRemainingViewportWidth(QWidget *widget, const QSize &viewportSize, cons
}
QPoint getPositionOnViewport(const BaseTextEditor * const editor, const QWidget * const widget,
int basePostion, const QSize &viewportSize, const QMargins &margins)
int basePos, int xPos, const QSize &viewportSize,
const QMargins &margins)
{
QTextCursor cursor = QTextCursor(editor->textDocument()->document());
cursor.setPosition(basePostion);
cursor.setPosition(basePos);
const QRect cursorRect = editor->editorWidget()->cursorRect(cursor);
QPoint widgetPosDefault = cursorRect.bottomLeft();
@@ -83,15 +84,15 @@ QPoint getPositionOnViewport(const BaseTextEditor * const editor, const QWidget
maxY = 0;
}
int x = widgetPosDefault.x() > maxX ? maxX : widgetPosDefault.x();
int x = xPos != -1 ? xPos : std::min(widgetPosDefault.x(), maxX);
int y = widgetPosDefault.y() + fontSize;
y = y > maxY ? maxY : y;
y = std::min(y, maxY);
return {x, y};
}
void addFloatingWidget(BaseTextEditor *editor, QWidget *widget, int pos, const QRect &margins,
bool fillWidth = false)
void addFloatingWidget(BaseTextEditor *editor, QWidget *widget, int yPos, int xPos,
const QRect &margins, bool fillWidth = false)
{
QMargins widgetMargins{margins.left(), margins.top(), margins.width(), margins.height()};
@@ -99,7 +100,7 @@ void addFloatingWidget(BaseTextEditor *editor, QWidget *widget, int pos, const Q
TextEditorWidget *editorWidget = editor->editorWidget();
const QSize viewportSize = editorWidget->viewport()->size();
widget->move(getPositionOnViewport(editor, widget, pos, viewportSize, widgetMargins));
widget->move(getPositionOnViewport(editor, widget, yPos, xPos, viewportSize, widgetMargins));
if (fillWidth)
fillRemainingViewportWidth(widget, viewportSize, widgetMargins);
@@ -286,20 +287,20 @@ void setupTextEditorModule()
},
"addFloatingWidget",
sol::overload(
[](const TextEditorPtr &textEditor, QWidget *widget, int position,
[](const TextEditorPtr &textEditor, QWidget *widget, int yPos, int xPos,
const QRect &margins, bool fillWidth) {
QTC_ASSERT(textEditor, throw sol::error("TextEditor is not valid"));
addFloatingWidget(textEditor, widget, position, margins, fillWidth);
addFloatingWidget(textEditor, widget, yPos, xPos, margins, fillWidth);
},
[](const TextEditorPtr &textEditor, Layouting::Widget *widget, int position,
[](const TextEditorPtr &textEditor, Layouting::Widget *widget, int yPos, int xPos,
const QRect &margins, bool fillWidth) {
QTC_ASSERT(textEditor, throw sol::error("TextEditor is not valid"));
addFloatingWidget(textEditor, widget->emerge(), position, margins, fillWidth);
addFloatingWidget(textEditor, widget->emerge(), yPos, xPos, margins, fillWidth);
},
[](const TextEditorPtr &textEditor, Layouting::Layout *layout, int position,
[](const TextEditorPtr &textEditor, Layouting::Layout *layout, int yPos, int xPos,
const QRect &margins, bool fillWidth = false) {
QTC_ASSERT(textEditor, throw sol::error("TextEditor is not valid"));
addFloatingWidget(textEditor, layout->emerge(), position, margins, fillWidth);
addFloatingWidget(textEditor, layout->emerge(), yPos, xPos, margins, fillWidth);
}),
"cursor",
[](const TextEditorPtr &textEditor) {

View File

@@ -109,8 +109,9 @@ function TextEditor:cursor() end
---@param widget Widget|Layout The widget to be added as a floating widget.
---@param position integer The position in the document where the widget should appear.
---@param margins integer[] Four integers, representing left, top, right, bottom margins
---@param xPos integer Sets widget to fixed x position if x != -1, otherwise automatic x position calculation is done
---@param fillWidth boolean If true, the widget will fill remaining space from its x position to size of the TextEditor viewport
function TextEditor:addFloatingWidget(widget, position, margins, fillWidth) end
function TextEditor:addFloatingWidget(widget, position, xPos, margins, fillWidth) end
---Checks if the current suggestion is locked. The suggestion is locked when the user can use it.
---@return boolean True if the suggestion is locked, false otherwise.