mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:38:11 +00:00
LibGUI: Make GTableModel a retainable object.
It became clear that this class needs to support multiple owners.
This commit is contained in:
parent
41c744b3c8
commit
f47945759b
22 changed files with 44 additions and 34 deletions
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
class DirectoryTableModel final : public GTableModel {
|
class DirectoryTableModel final : public GTableModel {
|
||||||
public:
|
public:
|
||||||
DirectoryTableModel();
|
static Retained<DirectoryTableModel> create() { return adopt(*new DirectoryTableModel); }
|
||||||
virtual ~DirectoryTableModel() override;
|
virtual ~DirectoryTableModel() override;
|
||||||
|
|
||||||
enum Column {
|
enum Column {
|
||||||
|
@ -33,6 +33,8 @@ public:
|
||||||
size_t bytes_in_files() const { return m_bytes_in_files; }
|
size_t bytes_in_files() const { return m_bytes_in_files; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
DirectoryTableModel();
|
||||||
|
|
||||||
String name_for_uid(uid_t) const;
|
String name_for_uid(uid_t) const;
|
||||||
String name_for_gid(gid_t) const;
|
String name_for_gid(gid_t) const;
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,9 @@
|
||||||
|
|
||||||
DirectoryTableView::DirectoryTableView(GWidget* parent)
|
DirectoryTableView::DirectoryTableView(GWidget* parent)
|
||||||
: GTableView(parent)
|
: GTableView(parent)
|
||||||
|
, m_model(DirectoryTableModel::create())
|
||||||
{
|
{
|
||||||
auto directory_model = make<DirectoryTableModel>();
|
set_model(GSortingProxyTableModel::create(m_model.copy_ref()));
|
||||||
m_model = directory_model.ptr();
|
|
||||||
set_model(make<GSortingProxyTableModel>(move(directory_model)));
|
|
||||||
GTableView::model()->set_key_column_and_sort_order(DirectoryTableModel::Column::Name, GSortOrder::Ascending);
|
GTableView::model()->set_key_column_and_sort_order(DirectoryTableModel::Column::Name, GSortOrder::Ascending);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,5 +24,5 @@ private:
|
||||||
|
|
||||||
void set_status_message(const String&);
|
void set_status_message(const String&);
|
||||||
|
|
||||||
DirectoryTableModel* m_model { nullptr };
|
Retained<DirectoryTableModel> m_model;
|
||||||
};
|
};
|
||||||
|
|
|
@ -125,7 +125,7 @@ void IRCAppWindow::setup_widgets()
|
||||||
m_window_list = new GTableView(horizontal_container);
|
m_window_list = new GTableView(horizontal_container);
|
||||||
m_window_list->set_headers_visible(false);
|
m_window_list->set_headers_visible(false);
|
||||||
m_window_list->set_alternating_row_colors(false);
|
m_window_list->set_alternating_row_colors(false);
|
||||||
m_window_list->set_model(OwnPtr<IRCWindowListModel>(m_client.client_window_list_model()));
|
m_window_list->set_model(m_client.client_window_list_model());
|
||||||
m_window_list->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
|
m_window_list->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
|
||||||
m_window_list->set_preferred_size({ 100, 0 });
|
m_window_list->set_preferred_size({ 100, 0 });
|
||||||
m_client.client_window_list_model()->on_activation = [this] (IRCWindow& window) {
|
m_client.client_window_list_model()->on_activation = [this] (IRCWindow& window) {
|
||||||
|
|
|
@ -8,8 +8,8 @@ IRCChannel::IRCChannel(IRCClient& client, const String& name)
|
||||||
: m_client(client)
|
: m_client(client)
|
||||||
, m_name(name)
|
, m_name(name)
|
||||||
, m_log(IRCLogBuffer::create())
|
, m_log(IRCLogBuffer::create())
|
||||||
|
, m_member_model(IRCChannelMemberListModel::create(*this))
|
||||||
{
|
{
|
||||||
m_member_model = new IRCChannelMemberListModel(*this);
|
|
||||||
m_window = m_client.aid_create_window(this, IRCWindow::Channel, m_name);
|
m_window = m_client.aid_create_window(this, IRCWindow::Channel, m_name);
|
||||||
m_window->set_log_buffer(*m_log);
|
m_window->set_log_buffer(*m_log);
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,8 +34,8 @@ public:
|
||||||
const IRCLogBuffer& log() const { return *m_log; }
|
const IRCLogBuffer& log() const { return *m_log; }
|
||||||
IRCLogBuffer& log() { return *m_log; }
|
IRCLogBuffer& log() { return *m_log; }
|
||||||
|
|
||||||
IRCChannelMemberListModel* member_model() { return m_member_model; }
|
IRCChannelMemberListModel* member_model() { return m_member_model.ptr(); }
|
||||||
const IRCChannelMemberListModel* member_model() const { return m_member_model; }
|
const IRCChannelMemberListModel* member_model() const { return m_member_model.ptr(); }
|
||||||
|
|
||||||
int member_count() const { return m_members.size(); }
|
int member_count() const { return m_members.size(); }
|
||||||
String member_at(int i) { return m_members[i].name; }
|
String member_at(int i) { return m_members[i].name; }
|
||||||
|
@ -63,6 +63,6 @@ private:
|
||||||
bool m_open { false };
|
bool m_open { false };
|
||||||
|
|
||||||
Retained<IRCLogBuffer> m_log;
|
Retained<IRCLogBuffer> m_log;
|
||||||
IRCChannelMemberListModel* m_member_model { nullptr };
|
Retained<IRCChannelMemberListModel> m_member_model;
|
||||||
IRCWindow* m_window { nullptr };
|
IRCWindow* m_window { nullptr };
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,7 +8,7 @@ class IRCChannel;
|
||||||
class IRCChannelMemberListModel final : public GTableModel {
|
class IRCChannelMemberListModel final : public GTableModel {
|
||||||
public:
|
public:
|
||||||
enum Column { Name };
|
enum Column { Name };
|
||||||
explicit IRCChannelMemberListModel(IRCChannel&);
|
static Retained<IRCChannelMemberListModel> create(IRCChannel& channel) { return adopt(*new IRCChannelMemberListModel(channel)); }
|
||||||
virtual ~IRCChannelMemberListModel() override;
|
virtual ~IRCChannelMemberListModel() override;
|
||||||
|
|
||||||
virtual int row_count() const override;
|
virtual int row_count() const override;
|
||||||
|
@ -22,5 +22,7 @@ public:
|
||||||
Function<void(const String&)> on_activation;
|
Function<void(const String&)> on_activation;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
explicit IRCChannelMemberListModel(IRCChannel&);
|
||||||
|
|
||||||
IRCChannel& m_channel;
|
IRCChannel& m_channel;
|
||||||
};
|
};
|
||||||
|
|
|
@ -31,10 +31,10 @@ IRCClient::IRCClient(const String& address, int port)
|
||||||
: m_hostname(address)
|
: m_hostname(address)
|
||||||
, m_port(port)
|
, m_port(port)
|
||||||
, m_nickname("anon")
|
, m_nickname("anon")
|
||||||
|
, m_client_window_list_model(IRCWindowListModel::create(*this))
|
||||||
, m_log(IRCLogBuffer::create())
|
, m_log(IRCLogBuffer::create())
|
||||||
{
|
{
|
||||||
m_socket = new GTCPSocket(this);
|
m_socket = new GTCPSocket(this);
|
||||||
m_client_window_list_model = new IRCWindowListModel(*this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IRCClient::~IRCClient()
|
IRCClient::~IRCClient()
|
||||||
|
|
|
@ -46,8 +46,8 @@ public:
|
||||||
void register_subwindow(IRCWindow&);
|
void register_subwindow(IRCWindow&);
|
||||||
void unregister_subwindow(IRCWindow&);
|
void unregister_subwindow(IRCWindow&);
|
||||||
|
|
||||||
IRCWindowListModel* client_window_list_model() { return m_client_window_list_model; }
|
IRCWindowListModel* client_window_list_model() { return m_client_window_list_model.ptr(); }
|
||||||
const IRCWindowListModel* client_window_list_model() const { return m_client_window_list_model; }
|
const IRCWindowListModel* client_window_list_model() const { return m_client_window_list_model.ptr(); }
|
||||||
|
|
||||||
int window_count() const { return m_windows.size(); }
|
int window_count() const { return m_windows.size(); }
|
||||||
const IRCWindow& window_at(int index) const { return *m_windows.at(index); }
|
const IRCWindow& window_at(int index) const { return *m_windows.at(index); }
|
||||||
|
@ -118,7 +118,6 @@ private:
|
||||||
|
|
||||||
IRCWindow* m_server_subwindow { nullptr };
|
IRCWindow* m_server_subwindow { nullptr };
|
||||||
|
|
||||||
IRCWindowListModel* m_client_window_list_model { nullptr };
|
Retained<IRCWindowListModel> m_client_window_list_model;
|
||||||
|
|
||||||
Retained<IRCLogBuffer> m_log;
|
Retained<IRCLogBuffer> m_log;
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,8 +9,8 @@ Retained<IRCLogBuffer> IRCLogBuffer::create()
|
||||||
}
|
}
|
||||||
|
|
||||||
IRCLogBuffer::IRCLogBuffer()
|
IRCLogBuffer::IRCLogBuffer()
|
||||||
|
: m_model(IRCLogBufferModel::create(*this))
|
||||||
{
|
{
|
||||||
m_model = new IRCLogBufferModel(*this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IRCLogBuffer::~IRCLogBuffer()
|
IRCLogBuffer::~IRCLogBuffer()
|
||||||
|
|
|
@ -27,11 +27,11 @@ public:
|
||||||
void add_message(const String& text, Color = Color::Black);
|
void add_message(const String& text, Color = Color::Black);
|
||||||
void dump() const;
|
void dump() const;
|
||||||
|
|
||||||
const IRCLogBufferModel* model() const { return m_model; }
|
const IRCLogBufferModel* model() const { return m_model.ptr(); }
|
||||||
IRCLogBufferModel* model() { return m_model; }
|
IRCLogBufferModel* model() { return m_model.ptr(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
IRCLogBuffer();
|
IRCLogBuffer();
|
||||||
IRCLogBufferModel* m_model { nullptr };
|
Retained<IRCLogBufferModel> m_model;
|
||||||
CircularQueue<Message, 1000> m_messages;
|
CircularQueue<Message, 1000> m_messages;
|
||||||
};
|
};
|
||||||
|
|
|
@ -13,7 +13,7 @@ public:
|
||||||
__Count,
|
__Count,
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit IRCLogBufferModel(Retained<IRCLogBuffer>&&);
|
static Retained<IRCLogBufferModel> create(Retained<IRCLogBuffer>&& log_buffer) { return adopt(*new IRCLogBufferModel(move(log_buffer))); }
|
||||||
virtual ~IRCLogBufferModel() override;
|
virtual ~IRCLogBufferModel() override;
|
||||||
|
|
||||||
virtual int row_count() const override;
|
virtual int row_count() const override;
|
||||||
|
@ -25,5 +25,7 @@ public:
|
||||||
virtual void activate(const GModelIndex&) override;
|
virtual void activate(const GModelIndex&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
explicit IRCLogBufferModel(Retained<IRCLogBuffer>&&);
|
||||||
|
|
||||||
Retained<IRCLogBuffer> m_log_buffer;
|
Retained<IRCLogBuffer> m_log_buffer;
|
||||||
};
|
};
|
||||||
|
|
|
@ -32,7 +32,7 @@ IRCWindow::IRCWindow(IRCClient& client, void* owner, Type type, const String& na
|
||||||
member_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
|
member_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
|
||||||
member_view->set_preferred_size({ 100, 0 });
|
member_view->set_preferred_size({ 100, 0 });
|
||||||
member_view->set_alternating_row_colors(false);
|
member_view->set_alternating_row_colors(false);
|
||||||
member_view->set_model(OwnPtr<IRCChannelMemberListModel>(channel().member_model()));
|
member_view->set_model(channel().member_model());
|
||||||
}
|
}
|
||||||
|
|
||||||
m_text_editor = new GTextEditor(GTextEditor::SingleLine, this);
|
m_text_editor = new GTextEditor(GTextEditor::SingleLine, this);
|
||||||
|
@ -59,7 +59,7 @@ IRCWindow::~IRCWindow()
|
||||||
void IRCWindow::set_log_buffer(const IRCLogBuffer& log_buffer)
|
void IRCWindow::set_log_buffer(const IRCLogBuffer& log_buffer)
|
||||||
{
|
{
|
||||||
m_log_buffer = &log_buffer;
|
m_log_buffer = &log_buffer;
|
||||||
m_table_view->set_model(OwnPtr<IRCLogBufferModel>((IRCLogBufferModel*)log_buffer.model()));
|
m_table_view->set_model(log_buffer.model());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IRCWindow::is_active() const
|
bool IRCWindow::is_active() const
|
||||||
|
|
|
@ -12,7 +12,7 @@ public:
|
||||||
Name,
|
Name,
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit IRCWindowListModel(IRCClient&);
|
static Retained<IRCWindowListModel> create(IRCClient& client) { return adopt(*new IRCWindowListModel(client)); }
|
||||||
virtual ~IRCWindowListModel() override;
|
virtual ~IRCWindowListModel() override;
|
||||||
|
|
||||||
virtual int row_count() const override;
|
virtual int row_count() const override;
|
||||||
|
@ -26,5 +26,7 @@ public:
|
||||||
Function<void(IRCWindow&)> on_activation;
|
Function<void(IRCWindow&)> on_activation;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
explicit IRCWindowListModel(IRCClient&);
|
||||||
|
|
||||||
IRCClient& m_client;
|
IRCClient& m_client;
|
||||||
};
|
};
|
||||||
|
|
|
@ -21,7 +21,7 @@ public:
|
||||||
__Count
|
__Count
|
||||||
};
|
};
|
||||||
|
|
||||||
ProcessTableModel();
|
static Retained<ProcessTableModel> create() { return adopt(*new ProcessTableModel); }
|
||||||
virtual ~ProcessTableModel() override;
|
virtual ~ProcessTableModel() override;
|
||||||
|
|
||||||
virtual int row_count() const override;
|
virtual int row_count() const override;
|
||||||
|
@ -32,6 +32,8 @@ public:
|
||||||
virtual void update() override;
|
virtual void update() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
ProcessTableModel();
|
||||||
|
|
||||||
struct ProcessState {
|
struct ProcessState {
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
unsigned nsched;
|
unsigned nsched;
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
ProcessTableView::ProcessTableView(GWidget* parent)
|
ProcessTableView::ProcessTableView(GWidget* parent)
|
||||||
: GTableView(parent)
|
: GTableView(parent)
|
||||||
{
|
{
|
||||||
set_model(make<GSortingProxyTableModel>(make<ProcessTableModel>()));
|
set_model(GSortingProxyTableModel::create(ProcessTableModel::create()));
|
||||||
model()->set_key_column_and_sort_order(ProcessTableModel::Column::CPU, GSortOrder::Descending);
|
model()->set_key_column_and_sort_order(ProcessTableModel::Column::CPU, GSortOrder::Descending);
|
||||||
start_timer(1000);
|
start_timer(1000);
|
||||||
model()->update();
|
model()->update();
|
||||||
|
|
|
@ -92,7 +92,7 @@ int main(int argc, char** argv)
|
||||||
window->set_should_exit_event_loop_on_close(true);
|
window->set_should_exit_event_loop_on_close(true);
|
||||||
|
|
||||||
Terminal terminal(ptm_fd);
|
Terminal terminal(ptm_fd);
|
||||||
window->set_has_alpha_channel(true);
|
window->set_has_alpha_channel(false);
|
||||||
window->set_main_widget(&terminal);
|
window->set_main_widget(&terminal);
|
||||||
window->move_to(300, 300);
|
window->move_to(300, 300);
|
||||||
terminal.apply_size_increments_to_window(*window);
|
terminal.apply_size_increments_to_window(*window);
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
GSortingProxyTableModel::GSortingProxyTableModel(OwnPtr<GTableModel>&& target)
|
GSortingProxyTableModel::GSortingProxyTableModel(Retained<GTableModel>&& target)
|
||||||
: m_target(move(target))
|
: m_target(move(target))
|
||||||
, m_key_column(-1)
|
, m_key_column(-1)
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
class GSortingProxyTableModel final : public GTableModel {
|
class GSortingProxyTableModel final : public GTableModel {
|
||||||
public:
|
public:
|
||||||
explicit GSortingProxyTableModel(OwnPtr<GTableModel>&&);
|
static Retained<GSortingProxyTableModel> create(Retained<GTableModel>&& model) { return adopt(*new GSortingProxyTableModel(move(model))); }
|
||||||
virtual ~GSortingProxyTableModel() override;
|
virtual ~GSortingProxyTableModel() override;
|
||||||
|
|
||||||
virtual int row_count() const override;
|
virtual int row_count() const override;
|
||||||
|
@ -23,12 +23,14 @@ public:
|
||||||
GModelIndex map_to_target(const GModelIndex&) const;
|
GModelIndex map_to_target(const GModelIndex&) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
explicit GSortingProxyTableModel(Retained<GTableModel>&&);
|
||||||
|
|
||||||
GTableModel& target() { return *m_target; }
|
GTableModel& target() { return *m_target; }
|
||||||
const GTableModel& target() const { return *m_target; }
|
const GTableModel& target() const { return *m_target; }
|
||||||
|
|
||||||
void resort();
|
void resort();
|
||||||
|
|
||||||
OwnPtr<GTableModel> m_target;
|
Retained<GTableModel> m_target;
|
||||||
Vector<int> m_row_mappings;
|
Vector<int> m_row_mappings;
|
||||||
int m_key_column { -1 };
|
int m_key_column { -1 };
|
||||||
GSortOrder m_sort_order { GSortOrder::Ascending };
|
GSortOrder m_sort_order { GSortOrder::Ascending };
|
||||||
|
|
|
@ -34,7 +34,7 @@ private:
|
||||||
GModelIndex m_index;
|
GModelIndex m_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
class GTableModel {
|
class GTableModel : public Retainable<GTableModel> {
|
||||||
public:
|
public:
|
||||||
struct ColumnMetadata {
|
struct ColumnMetadata {
|
||||||
int preferred_width { 0 };
|
int preferred_width { 0 };
|
||||||
|
|
|
@ -13,7 +13,7 @@ GTableView::~GTableView()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void GTableView::set_model(OwnPtr<GTableModel>&& model)
|
void GTableView::set_model(RetainPtr<GTableModel>&& model)
|
||||||
{
|
{
|
||||||
if (model.ptr() == m_model.ptr())
|
if (model.ptr() == m_model.ptr())
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -16,7 +16,7 @@ public:
|
||||||
int header_height() const { return m_headers_visible ? 16 : 0; }
|
int header_height() const { return m_headers_visible ? 16 : 0; }
|
||||||
int item_height() const { return 16; }
|
int item_height() const { return 16; }
|
||||||
|
|
||||||
void set_model(OwnPtr<GTableModel>&&);
|
void set_model(RetainPtr<GTableModel>&&);
|
||||||
GTableModel* model() { return m_model.ptr(); }
|
GTableModel* model() { return m_model.ptr(); }
|
||||||
const GTableModel* model() const { return m_model.ptr(); }
|
const GTableModel* model() const { return m_model.ptr(); }
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ private:
|
||||||
int column_width(int) const;
|
int column_width(int) const;
|
||||||
void update_content_size();
|
void update_content_size();
|
||||||
|
|
||||||
OwnPtr<GTableModel> m_model;
|
RetainPtr<GTableModel> m_model;
|
||||||
int m_horizontal_padding { 5 };
|
int m_horizontal_padding { 5 };
|
||||||
bool m_headers_visible { true };
|
bool m_headers_visible { true };
|
||||||
bool m_alternating_row_colors { true };
|
bool m_alternating_row_colors { true };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue