Lua: Add possibility to insert tool button to toolbar

Change-Id: I3c7c8f7f88cbb84173bff876ac1dd2e3885bea6a
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Krzysztof Chrusciel
2025-03-18 21:50:45 +01:00
parent ecbbe36c38
commit 774c0214b1
6 changed files with 73 additions and 0 deletions

View File

@@ -30,6 +30,7 @@
#include <QTabWidget> #include <QTabWidget>
#include <QTextEdit> #include <QTextEdit>
#include <QToolBar> #include <QToolBar>
#include <QToolButton>
namespace Layouting { namespace Layouting {
@@ -1088,6 +1089,19 @@ ToolBar::ToolBar(std::initializer_list<I> ps)
access(this)->setOrientation(Qt::Horizontal); access(this)->setOrientation(Qt::Horizontal);
} }
// ToolButton
ToolButton::ToolButton(std::initializer_list<I> ps)
{
ptr = new Implementation;
apply(this, ps);
}
void ToolButton::setDefaultAction(QAction *action)
{
access(this)->setDefaultAction(action);
}
// TabWidget // TabWidget
TabWidget::TabWidget(std::initializer_list<I> ps) TabWidget::TabWidget(std::initializer_list<I> ps)

View File

@@ -5,6 +5,7 @@
#include "builderutils.h" #include "builderutils.h"
#include <QAction>
#include <QString> #include <QString>
#include <initializer_list> #include <initializer_list>
@@ -38,6 +39,7 @@ class QStackedWidget;
class QTabWidget; class QTabWidget;
class QTextEdit; class QTextEdit;
class QToolBar; class QToolBar;
class QToolButton;
class QVBoxLayout; class QVBoxLayout;
class QWidget; class QWidget;
QT_END_NAMESPACE QT_END_NAMESPACE
@@ -435,6 +437,17 @@ public:
ToolBar(std::initializer_list<I> items); ToolBar(std::initializer_list<I> items);
}; };
class QTCREATOR_UTILS_EXPORT ToolButton : public Widget
{
public:
using Implementation = QToolButton;
using I = Building::BuilderItem<ToolButton>;
ToolButton(std::initializer_list<I> items);
void setDefaultAction(QAction *action);
};
class QTCREATOR_UTILS_EXPORT Spinner : public Widget class QTCREATOR_UTILS_EXPORT Spinner : public Widget
{ {
public: public:

View File

@@ -119,6 +119,7 @@ CREATE_HAS_FUNC(setViewportMargins, int(), int(), int(), int());
CREATE_HAS_FUNC(setCursor, Qt::CursorShape()) CREATE_HAS_FUNC(setCursor, Qt::CursorShape())
CREATE_HAS_FUNC(setMinimumWidth, int()); CREATE_HAS_FUNC(setMinimumWidth, int());
CREATE_HAS_FUNC(setEnableCodeCopyButton, bool()); CREATE_HAS_FUNC(setEnableCodeCopyButton, bool());
CREATE_HAS_FUNC(setDefaultAction, nullptr);
template<class T> template<class T>
void setProperties(std::unique_ptr<T> &item, const sol::table &children, QObject *guard) void setProperties(std::unique_ptr<T> &item, const sol::table &children, QObject *guard)
@@ -153,6 +154,12 @@ void setProperties(std::unique_ptr<T> &item, const sol::table &children, QObject
item->setEnableCodeCopyButton(*enableCodeCopyButton); item->setEnableCodeCopyButton(*enableCodeCopyButton);
} }
if constexpr (has_setDefaultAction<T>) {
const auto defaultAction = children.get<sol::optional<QAction *>>("defaultAction"sv);
if (defaultAction)
item->setDefaultAction(*defaultAction);
}
if constexpr (has_setVisible<T>) { if constexpr (has_setVisible<T>) {
const auto visible = children.get<sol::optional<bool>>("visible"sv); const auto visible = children.get<sol::optional<bool>>("visible"sv);
if (visible) if (visible)
@@ -675,6 +682,14 @@ void setupGuiModule()
}), }),
sol::base_classes, sol::base_classes,
sol::bases<Widget, Object, Thing>()); sol::bases<Widget, Object, Thing>());
gui.new_usertype<ToolButton>(
"ToolButton",
sol::call_constructor,
sol::factories([guard](const sol::table &children) {
return constructWidgetType<ToolButton>(children, guard);
}),
sol::base_classes,
sol::bases<Widget, Object, Thing>());
gui.new_usertype<TabWidget>( gui.new_usertype<TabWidget>(
"TabWidget", "TabWidget",
sol::call_constructor, sol::call_constructor,

View File

@@ -412,6 +412,13 @@ void setupTextEditorModule()
QTC_ASSERT(textEditor, throw sol::error("TextEditor is not valid")); QTC_ASSERT(textEditor, throw sol::error("TextEditor is not valid"));
return addEmbeddedWidget(textEditor, toWidget(widget), position); return addEmbeddedWidget(textEditor, toWidget(widget), position);
}, },
"insertExtraToolBarWidget",
[](const TextEditorPtr &textEditor,
TextEditorWidget::Side side,
LayoutOrWidget widget) {
QTC_ASSERT(textEditor, throw sol::error("TextEditor is not valid"));
textEditor->editorWidget()->insertExtraToolBarWidget(side, toWidget(widget));
},
"setRefactorMarker", "setRefactorMarker",
[pluginSpec, activeMarkers]( [pluginSpec, activeMarkers](
const TextEditorPtr &textEditor, const TextEditorPtr &textEditor,
@@ -461,6 +468,11 @@ void setupTextEditorModule()
return textEditor->editorWidget()->hasFocus(); return textEditor->editorWidget()->hasFocus();
}); });
result["Side"] = lua.create_table_with(
"Left", TextEditorWidget::Left,
"Right", TextEditorWidget::Right
);
result.new_usertype<TextSuggestion::Data>( result.new_usertype<TextSuggestion::Data>(
"Suggestion", "Suggestion",
"create", "create",

View File

@@ -214,6 +214,13 @@ local toolBar = {}
---@return ToolBar ---@return ToolBar
function gui.ToolBar(options) end function gui.ToolBar(options) end
---@class ToolButton : Widget
local toolButton = {}
---@param options WidgetOptions
---@return ToolButton
function gui.ToolButton(options) end
---@class TabWidget : Widget ---@class TabWidget : Widget
local tabWidget = {} local tabWidget = {}

View File

@@ -208,6 +208,11 @@ function EmbeddedWidget:onShouldClose(fn) end
---@return EmbeddedWidget interface An interface to control the floating widget. ---@return EmbeddedWidget interface An interface to control the floating widget.
function TextEditor:addEmbeddedWidget(widget, position) end function TextEditor:addEmbeddedWidget(widget, position) end
---Inserts a widget into toolbar.
---@param side TextEditor.Side The side where the widget should be added.
---@param widget Widget|Layout The widget to be added to the toolbar
function TextEditor:insertExtraToolBarWidget(side, widget) end
---Adds an refactor marker in the text editor at given cursor position. ---Adds an refactor marker in the text editor at given cursor position.
---@param icon Utils.Icon|FilePath|string Icon to be used. If specified icon is invalid the default QtCreator for markers is used. ---@param icon Utils.Icon|FilePath|string Icon to be used. If specified icon is invalid the default QtCreator for markers is used.
---@param position integer The position in the document where the marker should appear. ---@param position integer The position in the document where the marker should appear.
@@ -236,4 +241,11 @@ function TextEditor:hasFocus() end
---@return TextEditor|nil editor The currently active editor or nil if there is none. ---@return TextEditor|nil editor The currently active editor or nil if there is none.
function textEditor.currentEditor() end function textEditor.currentEditor() end
---@enum TextEditor.Side
---Side of the toolbar.
textEditor.Side = {
Left = 0,
Right = 0
}
return textEditor return textEditor