mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:58:11 +00:00
LibGUI: Move editing logic from GTableView up to GAbstractView.
GAbstractView should be able to manage the high-level editing logic, as long as subclasses implement content_rect(GModelIndex) so we know where to put the editing widgets. :^)
This commit is contained in:
parent
9cab7a0707
commit
18785ba5c3
5 changed files with 46 additions and 39 deletions
|
@ -35,11 +35,15 @@ void GAbstractView::model_notification(const GModelNotification& notification)
|
|||
|
||||
void GAbstractView::did_update_model()
|
||||
{
|
||||
if (!model() || model()->selected_index() != m_edit_index)
|
||||
stop_editing();
|
||||
model_notification(GModelNotification(GModelNotification::ModelUpdated));
|
||||
}
|
||||
|
||||
void GAbstractView::did_update_selection()
|
||||
{
|
||||
if (!model() || model()->selected_index() != m_edit_index)
|
||||
stop_editing();
|
||||
}
|
||||
|
||||
void GAbstractView::did_scroll()
|
||||
|
@ -53,3 +57,34 @@ void GAbstractView::update_edit_widget_position()
|
|||
return;
|
||||
m_edit_widget->set_relative_rect(m_edit_widget_content_rect.translated(-horizontal_scrollbar().value(), -vertical_scrollbar().value()));
|
||||
}
|
||||
|
||||
void GAbstractView::begin_editing(const GModelIndex& index)
|
||||
{
|
||||
ASSERT(is_editable());
|
||||
ASSERT(model());
|
||||
if (m_edit_index == index)
|
||||
return;
|
||||
if (!model()->is_editable(index))
|
||||
return;
|
||||
if (m_edit_widget)
|
||||
delete m_edit_widget;
|
||||
m_edit_index = index;
|
||||
m_edit_widget = new GTextBox(this);
|
||||
m_edit_widget->move_to_back();
|
||||
m_edit_widget->set_text(model()->data(index, GModel::Role::Display).to_string());
|
||||
m_edit_widget_content_rect = content_rect(index);
|
||||
update_edit_widget_position();
|
||||
m_edit_widget->set_focus(true);
|
||||
m_edit_widget->on_return_pressed = [this] {
|
||||
ASSERT(model());
|
||||
model()->set_data(m_edit_index, m_edit_widget->text());
|
||||
stop_editing();
|
||||
};
|
||||
}
|
||||
|
||||
void GAbstractView::stop_editing()
|
||||
{
|
||||
m_edit_index = { };
|
||||
delete m_edit_widget;
|
||||
m_edit_widget = nullptr;
|
||||
}
|
||||
|
|
|
@ -23,6 +23,10 @@ public:
|
|||
virtual void did_update_model();
|
||||
virtual void did_update_selection();
|
||||
|
||||
virtual Rect content_rect(const GModelIndex&) const { return { }; }
|
||||
void begin_editing(const GModelIndex&);
|
||||
void stop_editing();
|
||||
|
||||
Function<void(const GModelNotification&)> on_model_notification;
|
||||
|
||||
virtual const char* class_name() const override { return "GAbstractView"; }
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <AK/Badge.h>
|
||||
#include <AK/Function.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/Retainable.h>
|
||||
#include <LibGUI/GModelIndex.h>
|
||||
#include <LibGUI/GVariant.h>
|
||||
#include <SharedGraphics/TextAlignment.h>
|
||||
|
|
|
@ -39,7 +39,7 @@ void GTableView::did_update_model()
|
|||
update();
|
||||
}
|
||||
|
||||
Rect GTableView::cell_content_rect(int row, int column) const
|
||||
Rect GTableView::content_rect(int row, int column) const
|
||||
{
|
||||
auto row_rect = this->row_rect(row);
|
||||
int x = 0;
|
||||
|
@ -49,9 +49,9 @@ Rect GTableView::cell_content_rect(int row, int column) const
|
|||
return { horizontal_padding() + row_rect.x() + x, row_rect.y(), column_width(column), item_height() };
|
||||
}
|
||||
|
||||
Rect GTableView::cell_content_rect(const GModelIndex& index) const
|
||||
Rect GTableView::content_rect(const GModelIndex& index) const
|
||||
{
|
||||
return cell_content_rect(index.row(), index.column());
|
||||
return content_rect(index.row(), index.column());
|
||||
}
|
||||
|
||||
Rect GTableView::row_rect(int item_index) const
|
||||
|
@ -107,7 +107,7 @@ void GTableView::mousedown_event(GMouseEvent& event)
|
|||
if (!row_rect(row).contains(adjusted_position))
|
||||
continue;
|
||||
for (int column = 0, column_count = model()->column_count(); column < column_count; ++column) {
|
||||
if (!cell_content_rect(row, column).contains(adjusted_position))
|
||||
if (!content_rect(row, column).contains(adjusted_position))
|
||||
continue;
|
||||
model()->set_selected_index(model()->index(row, column));
|
||||
update();
|
||||
|
@ -326,34 +326,3 @@ void GTableView::doubleclick_event(GMouseEvent& event)
|
|||
model()->activate(model()->selected_index());
|
||||
}
|
||||
}
|
||||
|
||||
void GTableView::begin_editing(const GModelIndex& index)
|
||||
{
|
||||
ASSERT(is_editable());
|
||||
ASSERT(model());
|
||||
if (m_edit_index == index)
|
||||
return;
|
||||
if (!model()->is_editable(index))
|
||||
return;
|
||||
if (m_edit_widget)
|
||||
delete m_edit_widget;
|
||||
m_edit_index = index;
|
||||
m_edit_widget = new GTextBox(this);
|
||||
m_edit_widget->move_to_back();
|
||||
m_edit_widget->set_text(model()->data(index, GModel::Role::Display).to_string());
|
||||
m_edit_widget_content_rect = cell_content_rect(index);
|
||||
update_edit_widget_position();
|
||||
m_edit_widget->set_focus(true);
|
||||
m_edit_widget->on_return_pressed = [this] {
|
||||
ASSERT(model());
|
||||
model()->set_data(m_edit_index, m_edit_widget->text());
|
||||
stop_editing();
|
||||
};
|
||||
}
|
||||
|
||||
void GTableView::stop_editing()
|
||||
{
|
||||
m_edit_index = { };
|
||||
m_edit_widget->delete_later();
|
||||
m_edit_widget = nullptr;
|
||||
}
|
||||
|
|
|
@ -30,8 +30,7 @@ public:
|
|||
bool is_column_hidden(int) const;
|
||||
void set_column_hidden(int, bool);
|
||||
|
||||
void begin_editing(const GModelIndex&);
|
||||
void stop_editing();
|
||||
virtual Rect content_rect(const GModelIndex&) const override;
|
||||
|
||||
virtual const char* class_name() const override { return "GTableView"; }
|
||||
|
||||
|
@ -42,14 +41,13 @@ private:
|
|||
virtual void doubleclick_event(GMouseEvent&) override;
|
||||
virtual void keydown_event(GKeyEvent&) override;
|
||||
|
||||
Rect content_rect(int row, int column) const;
|
||||
void paint_headers(Painter&);
|
||||
int item_count() const;
|
||||
Rect row_rect(int item_index) const;
|
||||
Rect header_rect(int) const;
|
||||
int column_width(int) const;
|
||||
void update_content_size();
|
||||
Rect cell_content_rect(const GModelIndex&) const;
|
||||
Rect cell_content_rect(int row, int column) const;
|
||||
|
||||
Vector<bool> m_column_visibility;
|
||||
int m_horizontal_padding { 5 };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue