GTableView: Add ability to hide individual columns at view-level.

Use this in IRCClient to hide the "sender" column in the server message view
since everything in that view comes from the "Server" anyway.
This commit is contained in:
Andreas Kling 2019-03-20 13:35:11 +01:00
parent d17a91f185
commit 951377e93e
5 changed files with 41 additions and 3 deletions

View file

@ -246,7 +246,7 @@ void IRCClient::handle(const Message& msg, const String&)
void IRCClient::add_server_message(const String& text)
{
m_log->add_message(0, "Server", text);
m_log->add_message(0, "", text);
m_server_subwindow->did_add_message();
}
@ -354,7 +354,7 @@ void IRCClient::handle_ping(const Message& msg)
{
if (msg.arguments.size() < 0)
return;
m_log->add_message(0, "Server", "Ping? Pong!");
m_log->add_message(0, "", "Ping? Pong!");
send_pong(msg.arguments[0]);
}

View file

@ -60,6 +60,8 @@ GVariant IRCLogBufferModel::data(const GModelIndex& index, Role role) const
}
}
if (role == Role::ForegroundColor) {
if (index.column() == Column::Timestamp)
return Color(Color::MidGray);
if (index.column() == Column::Text)
return m_log_buffer->at(index.row()).color;
}

View file

@ -26,6 +26,10 @@ IRCWindow::IRCWindow(IRCClient& client, void* owner, Type type, const String& na
m_table_view->set_font(Font::default_fixed_width_font());
m_table_view->set_alternating_row_colors(false);
if (m_type == Server) {
m_table_view->set_column_hidden(IRCLogBufferModel::Column::Name, true);
}
if (m_type == Channel) {
auto* member_view = new GTableView(container);
member_view->set_headers_visible(false);

View file

@ -63,9 +63,14 @@ int GTableView::column_width(int column_index) const
Rect GTableView::header_rect(int column_index) const
{
if (is_column_hidden(column_index))
return { };
int x_offset = 0;
for (int i = 0; i < column_index; ++i)
for (int i = 0; i < column_index; ++i) {
if (is_column_hidden(i))
continue;
x_offset += column_width(i) + horizontal_padding() * 2;
}
auto column_metadata = m_model->column_metadata(column_index);
int column_width = column_metadata.preferred_width;
return { x_offset, 0, column_width + horizontal_padding() * 2, header_height() };
@ -137,6 +142,8 @@ void GTableView::paint_event(GPaintEvent& event)
int x_offset = 0;
for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
if (is_column_hidden(column_index))
continue;
auto column_metadata = m_model->column_metadata(column_index);
int column_width = column_metadata.preferred_width;
const Font& font = column_metadata.font ? *column_metadata.font : this->font();
@ -192,6 +199,8 @@ void GTableView::paint_headers(Painter& painter)
painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::DarkGray);
int x_offset = 0;
for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
if (is_column_hidden(column_index))
continue;
auto column_metadata = m_model->column_metadata(column_index);
int column_width = column_metadata.preferred_width;
bool is_key_column = m_model->key_column() == column_index;
@ -277,3 +286,22 @@ void GTableView::scroll_into_view(const GModelIndex& index, Orientation orientat
auto rect = row_rect(index.row()).translated(0, -header_height());
GScrollableWidget::scroll_into_view(rect, orientation);
}
bool GTableView::is_column_hidden(int column) const
{
if (column >= 0 && column < m_column_visibility.size())
return !m_column_visibility[column];
return false;
}
void GTableView::set_column_hidden(int column, bool hidden)
{
ASSERT(column >= 0);
if (m_column_visibility.size() <= column) {
int previous_column_count = m_column_visibility.size();
m_column_visibility.resize(column + 1);
for (int i = previous_column_count; i < m_column_visibility.size(); ++i)
m_column_visibility[i] = true;
}
m_column_visibility[column] = !hidden;
}

View file

@ -35,6 +35,9 @@ public:
void scroll_into_view(const GModelIndex&, Orientation);
bool is_column_hidden(int) const;
void set_column_hidden(int, bool);
private:
virtual void model_notification(const GModelNotification&);
@ -49,6 +52,7 @@ private:
int column_width(int) const;
void update_content_size();
Vector<bool> m_column_visibility;
RetainPtr<GTableModel> m_model;
int m_horizontal_padding { 5 };
bool m_headers_visible { true };