VisualBuilder: Make it possible to insert widgets from the toolbox.

This commit is contained in:
Andreas Kling 2019-04-11 04:13:11 +02:00
parent c71ece77fa
commit 75c76f6692
Notes: sideshowbarker 2024-07-19 14:45:38 +09:00
3 changed files with 41 additions and 6 deletions

View File

@ -2,10 +2,17 @@
#include "VBWidget.h"
#include <LibGUI/GPainter.h>
static VBForm* s_current;
VBForm* VBForm::current()
{
return s_current;
}
VBForm::VBForm(const String& name, GWidget* parent)
: GWidget(parent)
, m_name(name)
{
s_current = this;
set_fill_with_background_color(true);
set_background_color(Color::LightGray);
set_greedy_for_hits(true);
@ -23,6 +30,14 @@ VBForm::VBForm(const String& name, GWidget* parent)
m_widgets.append(move(button1));
}
void VBForm::insert_widget(WidgetType type)
{
auto widget = VBWidget::create(type, *this);
widget->set_rect({ m_next_insertion_position, { m_grid_size * 10 + 1, m_grid_size * 5 + 1 } });
m_next_insertion_position.move_by(m_grid_size, m_grid_size);
m_widgets.append(move(widget));
}
VBForm::~VBForm()
{
}

View File

@ -9,6 +9,8 @@ public:
explicit VBForm(const String& name, GWidget* parent = nullptr);
virtual ~VBForm() override;
static VBForm* current();
String name() const { return m_name; }
void set_name(const String& name) { m_name = name; }
@ -18,6 +20,8 @@ public:
void set_should_snap_to_grip(bool snap) { m_should_snap_to_grid = snap; }
bool should_snap_to_grid() const { return m_should_snap_to_grid; }
void insert_widget(WidgetType);
protected:
virtual void paint_event(GPaintEvent&) override;
virtual void second_paint_event(GPaintEvent&) override;
@ -35,5 +39,6 @@ private:
WeakPtr<VBWidget> m_selected_widget;
Point m_transform_event_origin;
Rect m_transform_widget_origin_rect;
Point m_next_insertion_position;
Direction m_resize_direction { Direction::None };
};

View File

@ -2,14 +2,9 @@
#include <LibGUI/GWidget.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GStatusBar.h>
#include <LibGUI/GToolBar.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GTextEditor.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GFontDatabase.h>
#include <LibCore/CFile.h>
#include <AK/StringBuilder.h>
#include <LibGUI/GButton.h>
#include "VBForm.h"
#include "VBWidget.h"
#include <unistd.h>
@ -68,6 +63,26 @@ GWindow* make_toolbox_window()
auto* widget = new GWidget;
widget->set_fill_with_background_color(true);
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
window->set_main_widget(widget);
auto* button_button = new GButton(widget);
button_button->set_caption("Button");
button_button->on_click = [] (GButton&) {
if (auto* form = VBForm::current())
form->insert_widget(WidgetType::GButton);
};
auto* spinbox_button = new GButton(widget);
spinbox_button->set_caption("SpinBox");
spinbox_button->on_click = [] (GButton&) {
if (auto* form = VBForm::current())
form->insert_widget(WidgetType::GSpinBox);
};
auto* editor_button = new GButton(widget);
editor_button->set_caption("TextEditor");
editor_button->on_click = [] (GButton&) {
if (auto* form = VBForm::current())
form->insert_widget(WidgetType::GTextEditor);
};
return window;
}