2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-07 12:46:02 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Action.h>
|
2020-08-25 12:25:39 +03:00
|
|
|
#include <LibGUI/HeaderView.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Menu.h>
|
|
|
|
#include <LibGUI/Model.h>
|
2020-08-28 21:47:02 +03:00
|
|
|
#include <LibGUI/ModelEditingDelegate.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Painter.h>
|
|
|
|
#include <LibGUI/TableView.h>
|
|
|
|
#include <LibGUI/Window.h>
|
2020-03-30 20:57:44 +03:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-02-28 03:43:50 +03:00
|
|
|
|
2021-03-11 18:35:40 +03:00
|
|
|
REGISTER_WIDGET(GUI, TableView)
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
2020-02-23 14:07:13 +03:00
|
|
|
TableView::TableView()
|
2019-02-28 03:43:50 +03:00
|
|
|
{
|
2020-04-18 22:55:37 +03:00
|
|
|
set_fill_with_background_color(true);
|
2019-12-24 22:57:54 +03:00
|
|
|
set_background_role(ColorRole::Base);
|
2019-12-25 00:01:32 +03:00
|
|
|
set_foreground_role(ColorRole::BaseText);
|
2019-02-28 03:43:50 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
TableView::~TableView()
|
2019-02-28 03:43:50 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void TableView::paint_event(PaintEvent& event)
|
2019-02-28 03:43:50 +03:00
|
|
|
{
|
2019-12-24 22:57:54 +03:00
|
|
|
Color widget_background_color = palette().color(background_role());
|
2020-02-02 17:07:41 +03:00
|
|
|
Frame::paint_event(event);
|
2019-03-28 18:58:29 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Painter painter(*this);
|
2019-03-29 17:01:54 +03:00
|
|
|
painter.add_clip_rect(frame_inner_rect());
|
|
|
|
painter.add_clip_rect(event.rect());
|
2020-04-18 22:55:37 +03:00
|
|
|
if (fill_with_background_color())
|
|
|
|
painter.fill_rect(event.rect(), widget_background_color);
|
2019-05-04 22:16:41 +03:00
|
|
|
painter.translate(frame_thickness(), frame_thickness());
|
2019-03-16 18:03:31 +03:00
|
|
|
painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
|
2019-02-28 03:43:50 +03:00
|
|
|
|
2019-11-08 23:45:42 +03:00
|
|
|
if (!model())
|
|
|
|
return;
|
|
|
|
|
2020-12-28 03:14:01 +03:00
|
|
|
auto selection_color = is_focused() ? palette().selection() : palette().inactive_selection();
|
|
|
|
|
2019-03-16 18:03:31 +03:00
|
|
|
int exposed_width = max(content_size().width(), width());
|
2020-08-26 16:54:03 +03:00
|
|
|
int x_offset = row_header().is_visible() ? row_header().width() : 0;
|
|
|
|
int y_offset = column_header().is_visible() ? column_header().height() : 0;
|
2019-02-28 03:43:50 +03:00
|
|
|
|
2019-12-14 01:36:36 +03:00
|
|
|
bool dummy;
|
2021-02-23 21:23:34 +03:00
|
|
|
int first_visible_row = index_at_event_position(frame_inner_rect().top_left().translated(x_offset, y_offset), dummy).row();
|
|
|
|
int last_visible_row = index_at_event_position(frame_inner_rect().bottom_right().translated(x_offset, y_offset), dummy).row();
|
2019-09-18 15:10:09 +03:00
|
|
|
|
|
|
|
if (first_visible_row == -1)
|
|
|
|
first_visible_row = 0;
|
|
|
|
if (last_visible_row == -1)
|
|
|
|
last_visible_row = model()->row_count() - 1;
|
|
|
|
|
|
|
|
int painted_item_index = first_visible_row;
|
|
|
|
|
|
|
|
for (int row_index = first_visible_row; row_index <= last_visible_row; ++row_index) {
|
2019-09-07 20:35:45 +03:00
|
|
|
bool is_selected_row = selection().contains_row(row_index);
|
2020-08-26 21:34:07 +03:00
|
|
|
int y = y_offset + painted_item_index * row_height();
|
2019-02-28 03:43:50 +03:00
|
|
|
|
|
|
|
Color background_color;
|
2019-03-09 15:59:01 +03:00
|
|
|
Color key_column_background_color;
|
2020-08-23 17:05:47 +03:00
|
|
|
if (is_selected_row && highlight_selected_rows()) {
|
2020-12-28 03:14:01 +03:00
|
|
|
background_color = selection_color;
|
|
|
|
key_column_background_color = selection_color;
|
2019-02-28 03:43:50 +03:00
|
|
|
} else {
|
2019-03-15 23:41:27 +03:00
|
|
|
if (alternating_row_colors() && (painted_item_index % 2)) {
|
2019-12-24 22:57:54 +03:00
|
|
|
background_color = widget_background_color.darkened(0.8f);
|
|
|
|
key_column_background_color = widget_background_color.darkened(0.7f);
|
2019-03-15 23:41:27 +03:00
|
|
|
} else {
|
2019-12-24 22:57:54 +03:00
|
|
|
background_color = widget_background_color;
|
|
|
|
key_column_background_color = widget_background_color.darkened(0.9f);
|
2019-03-15 23:41:27 +03:00
|
|
|
}
|
2019-02-28 03:43:50 +03:00
|
|
|
}
|
2020-10-27 18:18:35 +03:00
|
|
|
|
|
|
|
auto row_rect = this->row_rect(painted_item_index);
|
|
|
|
painter.fill_rect(row_rect, background_color);
|
2019-03-18 06:54:07 +03:00
|
|
|
|
2020-08-26 16:54:03 +03:00
|
|
|
int x = x_offset;
|
2019-03-23 04:04:31 +03:00
|
|
|
for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
|
2020-08-25 12:25:39 +03:00
|
|
|
if (!column_header().is_section_visible(column_index))
|
2019-03-20 15:35:11 +03:00
|
|
|
continue;
|
2019-05-05 00:45:31 +03:00
|
|
|
int column_width = this->column_width(column_index);
|
2020-08-16 11:44:10 +03:00
|
|
|
bool is_key_column = m_key_column == column_index;
|
2020-08-26 21:34:07 +03:00
|
|
|
Gfx::IntRect cell_rect(horizontal_padding() + x, y, column_width, row_height());
|
2020-04-11 20:20:40 +03:00
|
|
|
auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
|
2021-03-11 18:35:40 +03:00
|
|
|
if (is_key_column && is_key_column_highlighted())
|
2019-03-09 15:59:01 +03:00
|
|
|
painter.fill_rect(cell_rect_for_fill, key_column_background_color);
|
2019-03-29 06:58:15 +03:00
|
|
|
auto cell_index = model()->index(row_index, column_index);
|
2019-08-14 21:34:46 +03:00
|
|
|
|
2020-08-25 12:25:39 +03:00
|
|
|
if (auto* delegate = column_painting_delegate(column_index)) {
|
2020-08-16 17:05:24 +03:00
|
|
|
delegate->paint(painter, cell_rect, palette(), cell_index);
|
2019-03-18 06:54:07 +03:00
|
|
|
} else {
|
2020-08-16 17:14:39 +03:00
|
|
|
auto data = cell_index.data();
|
2019-08-14 21:34:46 +03:00
|
|
|
if (data.is_bitmap()) {
|
2021-03-11 18:35:40 +03:00
|
|
|
auto cell_constrained_bitmap_rect = data.as_bitmap().rect();
|
|
|
|
if (data.as_bitmap().rect().width() > column_width)
|
|
|
|
cell_constrained_bitmap_rect.set_width(column_width);
|
|
|
|
if (data.as_bitmap().rect().height() > row_height())
|
|
|
|
cell_constrained_bitmap_rect.set_height(row_height());
|
|
|
|
cell_rect.set_y(cell_rect.y() + (row_height() - cell_constrained_bitmap_rect.height()) / 2);
|
|
|
|
cell_rect.set_x(cell_rect.x() + (column_width - cell_constrained_bitmap_rect.width()) / 2);
|
|
|
|
painter.blit(cell_rect.location(), data.as_bitmap(), cell_constrained_bitmap_rect);
|
2019-08-14 21:34:46 +03:00
|
|
|
} else if (data.is_icon()) {
|
2020-03-30 20:57:44 +03:00
|
|
|
if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
|
2021-03-04 07:39:56 +03:00
|
|
|
cell_rect.set_y(cell_rect.y() + (row_height() - bitmap->height()) / 2);
|
2020-10-26 23:32:27 +03:00
|
|
|
if (is_selected_row) {
|
2020-12-28 03:14:01 +03:00
|
|
|
auto tint = selection_color.with_alpha(100);
|
2020-10-26 23:32:27 +03:00
|
|
|
painter.blit_filtered(cell_rect.location(), *bitmap, bitmap->rect(), [&](auto src) { return src.blend(tint); });
|
|
|
|
} else if (m_hovered_index.is_valid() && cell_index.row() == m_hovered_index.row()) {
|
2020-03-30 20:57:44 +03:00
|
|
|
painter.blit_brightened(cell_rect.location(), *bitmap, bitmap->rect());
|
2020-10-26 23:32:27 +03:00
|
|
|
} else {
|
2021-07-27 18:23:52 +03:00
|
|
|
auto opacity = cell_index.data(ModelRole::IconOpacity).as_float_or(1.0f);
|
|
|
|
painter.blit(cell_rect.location(), *bitmap, bitmap->rect(), opacity);
|
2020-10-26 23:32:27 +03:00
|
|
|
}
|
2020-03-30 20:57:44 +03:00
|
|
|
}
|
2019-08-14 21:34:46 +03:00
|
|
|
} else {
|
2020-04-12 16:13:39 +03:00
|
|
|
if (!is_selected_row) {
|
2020-08-16 17:14:39 +03:00
|
|
|
auto cell_background_color = cell_index.data(ModelRole::BackgroundColor);
|
2020-04-12 16:13:39 +03:00
|
|
|
if (cell_background_color.is_valid())
|
|
|
|
painter.fill_rect(cell_rect_for_fill, cell_background_color.to_color(background_color));
|
2020-04-11 19:56:15 +03:00
|
|
|
}
|
2020-08-27 19:36:31 +03:00
|
|
|
|
2020-08-16 17:14:39 +03:00
|
|
|
auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
|
2020-10-21 00:13:28 +03:00
|
|
|
draw_item_text(painter, cell_index, is_selected_row, cell_rect, data.to_string(), font_for_index(cell_index), text_alignment, Gfx::TextElision::Right);
|
2019-08-14 21:34:46 +03:00
|
|
|
}
|
2019-03-18 06:54:07 +03:00
|
|
|
}
|
2020-08-28 17:52:06 +03:00
|
|
|
|
|
|
|
if (m_grid_style == GridStyle::Horizontal || m_grid_style == GridStyle::Both)
|
|
|
|
painter.draw_line(cell_rect_for_fill.bottom_left(), cell_rect_for_fill.bottom_right(), palette().ruler());
|
|
|
|
if (m_grid_style == GridStyle::Vertical || m_grid_style == GridStyle::Both)
|
|
|
|
painter.draw_line(cell_rect_for_fill.top_right(), cell_rect_for_fill.bottom_right(), palette().ruler());
|
|
|
|
|
2020-12-17 02:49:42 +03:00
|
|
|
if (selection_behavior() == SelectionBehavior::SelectItems && cell_index == cursor_index())
|
2020-08-28 17:52:06 +03:00
|
|
|
painter.draw_rect(cell_rect_for_fill, palette().text_cursor());
|
|
|
|
|
2020-08-26 16:54:03 +03:00
|
|
|
x += column_width + horizontal_padding() * 2;
|
2019-02-28 03:43:50 +03:00
|
|
|
}
|
2020-10-27 18:18:35 +03:00
|
|
|
|
2020-12-17 02:49:42 +03:00
|
|
|
if (is_focused() && selection_behavior() == SelectionBehavior::SelectRows && row_index == cursor_index().row()) {
|
2020-10-27 18:18:35 +03:00
|
|
|
painter.draw_rect(row_rect, widget_background_color);
|
|
|
|
painter.draw_focus_rect(row_rect, palette().focus_outline());
|
|
|
|
}
|
2021-01-09 13:53:29 +03:00
|
|
|
|
|
|
|
if (has_pending_drop() && selection_behavior() == SelectionBehavior::SelectRows && row_index == drop_candidate_index().row()) {
|
|
|
|
painter.draw_rect(row_rect, palette().selection(), true);
|
|
|
|
}
|
2019-02-28 03:43:50 +03:00
|
|
|
++painted_item_index;
|
|
|
|
};
|
|
|
|
|
2020-08-26 21:34:07 +03:00
|
|
|
Gfx::IntRect unpainted_rect(0, column_header().height() + painted_item_index * row_height(), exposed_width, height());
|
2020-04-18 22:55:37 +03:00
|
|
|
if (fill_with_background_color())
|
|
|
|
painter.fill_rect(unpainted_rect, widget_background_color);
|
2019-03-15 16:50:36 +03:00
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
|
2020-05-21 14:40:52 +03:00
|
|
|
void TableView::keydown_event(KeyEvent& event)
|
|
|
|
{
|
|
|
|
if (!model())
|
2020-10-28 23:21:50 +03:00
|
|
|
return AbstractTableView::keydown_event(event);
|
2020-08-28 21:55:25 +03:00
|
|
|
|
2020-08-28 21:47:02 +03:00
|
|
|
AbstractTableView::keydown_event(event);
|
|
|
|
|
|
|
|
if (event.is_accepted())
|
|
|
|
return;
|
|
|
|
|
2021-07-11 22:46:34 +03:00
|
|
|
auto is_delete = event.key() == Key_Delete;
|
|
|
|
auto is_backspace = event.key() == Key_Backspace;
|
|
|
|
auto is_clear = is_delete || is_backspace;
|
|
|
|
if (is_editable() && edit_triggers() & EditTrigger::AnyKeyPressed && (event.code_point() != 0 || is_clear)) {
|
2020-08-28 21:47:02 +03:00
|
|
|
begin_editing(cursor_index());
|
2020-08-29 02:04:22 +03:00
|
|
|
if (m_editing_delegate) {
|
2020-11-29 10:35:58 +03:00
|
|
|
if (is_delete)
|
2021-07-11 22:46:34 +03:00
|
|
|
m_editing_delegate->set_value(String {});
|
|
|
|
else if (is_backspace)
|
|
|
|
m_editing_delegate->set_value(String::empty());
|
2020-08-29 02:04:22 +03:00
|
|
|
else
|
2021-07-11 22:46:34 +03:00
|
|
|
m_editing_delegate->set_value(event.text(), ModelEditingDelegate::SelectionBehavior::DoNotSelect);
|
2020-08-29 02:04:22 +03:00
|
|
|
}
|
2020-08-28 21:47:02 +03:00
|
|
|
}
|
2020-08-27 18:47:19 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 19:36:31 +03:00
|
|
|
void TableView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
|
2020-08-27 18:47:19 +03:00
|
|
|
{
|
|
|
|
if (!model())
|
2020-08-24 22:02:46 +03:00
|
|
|
return;
|
2020-08-27 18:47:19 +03:00
|
|
|
auto& model = *this->model();
|
|
|
|
switch (movement) {
|
|
|
|
case CursorMovement::Left:
|
2020-08-27 19:36:31 +03:00
|
|
|
move_cursor_relative(0, -1, selection_update);
|
2020-08-27 18:47:19 +03:00
|
|
|
break;
|
|
|
|
case CursorMovement::Right:
|
2020-08-27 19:36:31 +03:00
|
|
|
move_cursor_relative(0, 1, selection_update);
|
2020-08-27 18:47:19 +03:00
|
|
|
break;
|
|
|
|
case CursorMovement::Up:
|
2020-08-27 19:36:31 +03:00
|
|
|
move_cursor_relative(-1, 0, selection_update);
|
2020-08-27 18:47:19 +03:00
|
|
|
break;
|
|
|
|
case CursorMovement::Down:
|
2020-08-27 19:36:31 +03:00
|
|
|
move_cursor_relative(1, 0, selection_update);
|
2020-08-27 18:47:19 +03:00
|
|
|
break;
|
|
|
|
case CursorMovement::Home: {
|
|
|
|
auto index = model.index(0, 0);
|
2020-08-27 19:36:31 +03:00
|
|
|
set_cursor(index, selection_update);
|
2020-08-27 18:47:19 +03:00
|
|
|
break;
|
2020-05-21 14:40:52 +03:00
|
|
|
}
|
2020-08-27 18:47:19 +03:00
|
|
|
case CursorMovement::End: {
|
|
|
|
auto index = model.index(model.row_count() - 1, 0);
|
2020-08-27 19:36:31 +03:00
|
|
|
set_cursor(index, selection_update);
|
2020-08-27 18:47:19 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CursorMovement::PageUp: {
|
2020-08-26 21:34:07 +03:00
|
|
|
int items_per_page = visible_content_rect().height() / row_height();
|
2020-05-21 14:40:52 +03:00
|
|
|
auto old_index = selection().first();
|
|
|
|
auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
|
2021-06-27 15:08:16 +03:00
|
|
|
if (model.is_within_range(new_index))
|
2020-08-27 19:36:31 +03:00
|
|
|
set_cursor(new_index, selection_update);
|
2020-08-27 18:47:19 +03:00
|
|
|
break;
|
2020-05-21 14:40:52 +03:00
|
|
|
}
|
2020-08-27 18:47:19 +03:00
|
|
|
case CursorMovement::PageDown: {
|
2020-08-26 21:34:07 +03:00
|
|
|
int items_per_page = visible_content_rect().height() / row_height();
|
2020-05-21 14:40:52 +03:00
|
|
|
auto old_index = selection().first();
|
|
|
|
auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
|
2021-06-27 15:08:16 +03:00
|
|
|
if (model.is_within_range(new_index))
|
2020-08-27 19:36:31 +03:00
|
|
|
set_cursor(new_index, selection_update);
|
2020-08-27 18:47:19 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-05-21 14:40:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 17:52:06 +03:00
|
|
|
void TableView::set_grid_style(GridStyle style)
|
|
|
|
{
|
|
|
|
if (m_grid_style == style)
|
|
|
|
return;
|
|
|
|
m_grid_style = style;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
}
|