2019-06-07 11:46:02 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <Kernel/KeyCode.h>
|
|
|
|
#include <LibGUI/GAction.h>
|
|
|
|
#include <LibGUI/GMenu.h>
|
2019-03-23 01:42:49 +01:00
|
|
|
#include <LibGUI/GModel.h>
|
2019-03-28 17:19:56 +01:00
|
|
|
#include <LibGUI/GPainter.h>
|
2019-06-07 11:46:02 +02:00
|
|
|
#include <LibGUI/GScrollBar.h>
|
|
|
|
#include <LibGUI/GTableView.h>
|
2019-04-18 22:27:14 +02:00
|
|
|
#include <LibGUI/GTextBox.h>
|
2019-05-04 23:45:31 +02:00
|
|
|
#include <LibGUI/GWindow.h>
|
2019-02-28 01:43:50 +01:00
|
|
|
|
2019-02-28 10:57:09 +01:00
|
|
|
GTableView::GTableView(GWidget* parent)
|
2019-12-13 20:54:40 +01:00
|
|
|
: GAbstractColumnView(parent)
|
2019-02-28 01:43:50 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-28 10:57:09 +01:00
|
|
|
GTableView::~GTableView()
|
2019-02-28 01:43:50 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-28 18:57:36 +01:00
|
|
|
void GTableView::paint_event(GPaintEvent& event)
|
2019-02-28 01:43:50 +01:00
|
|
|
{
|
2019-03-28 16:58:29 +01:00
|
|
|
GFrame::paint_event(event);
|
|
|
|
|
2019-03-28 17:19:56 +01:00
|
|
|
GPainter painter(*this);
|
2019-03-29 15:01:54 +01:00
|
|
|
painter.add_clip_rect(frame_inner_rect());
|
|
|
|
painter.add_clip_rect(event.rect());
|
2019-12-23 20:24:26 +01:00
|
|
|
painter.fill_rect(event.rect(), SystemColor::Base);
|
2019-05-04 21:16:41 +02:00
|
|
|
painter.translate(frame_thickness(), frame_thickness());
|
2019-03-16 16:03:31 +01:00
|
|
|
painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
|
2019-02-28 01:43:50 +01:00
|
|
|
|
2019-11-08 21:45:42 +01:00
|
|
|
if (!model())
|
|
|
|
return;
|
|
|
|
|
2019-03-16 16:03:31 +01:00
|
|
|
int exposed_width = max(content_size().width(), width());
|
2019-02-28 01:43:50 +01:00
|
|
|
int y_offset = header_height();
|
|
|
|
|
2019-12-13 23:36:36 +01:00
|
|
|
bool dummy;
|
|
|
|
int first_visible_row = index_at_event_position(frame_inner_rect().top_left(), dummy).row();
|
|
|
|
int last_visible_row = index_at_event_position(frame_inner_rect().bottom_right(), dummy).row();
|
2019-09-18 14:10:09 +02: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 19:35:45 +02:00
|
|
|
bool is_selected_row = selection().contains_row(row_index);
|
2019-02-28 01:43:50 +01:00
|
|
|
int y = y_offset + painted_item_index * item_height();
|
|
|
|
|
|
|
|
Color background_color;
|
2019-03-09 13:59:01 +01:00
|
|
|
Color key_column_background_color;
|
2019-03-18 04:54:07 +01:00
|
|
|
if (is_selected_row) {
|
2019-03-04 11:17:25 +01:00
|
|
|
background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
|
2019-03-09 13:59:01 +01:00
|
|
|
key_column_background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
|
2019-02-28 01:43:50 +01:00
|
|
|
} else {
|
2019-03-15 21:41:27 +01:00
|
|
|
if (alternating_row_colors() && (painted_item_index % 2)) {
|
2019-12-24 02:25:11 +01:00
|
|
|
background_color = Color(SystemColor::Base).darkened(0.8f);
|
|
|
|
key_column_background_color = Color(SystemColor::Base).darkened(0.7f);
|
2019-03-15 21:41:27 +01:00
|
|
|
} else {
|
2019-12-23 20:24:26 +01:00
|
|
|
background_color = SystemColor::Base;
|
2019-12-24 02:25:11 +01:00
|
|
|
key_column_background_color = Color(SystemColor::Base).darkened(0.9f);
|
2019-03-15 21:41:27 +01:00
|
|
|
}
|
2019-02-28 01:43:50 +01:00
|
|
|
}
|
|
|
|
painter.fill_rect(row_rect(painted_item_index), background_color);
|
2019-03-18 04:54:07 +01:00
|
|
|
|
2019-02-28 01:43:50 +01:00
|
|
|
int x_offset = 0;
|
2019-03-23 02:04:31 +01:00
|
|
|
for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
|
2019-03-20 13:35:11 +01:00
|
|
|
if (is_column_hidden(column_index))
|
|
|
|
continue;
|
2019-03-23 02:04:31 +01:00
|
|
|
auto column_metadata = model()->column_metadata(column_index);
|
2019-05-04 23:45:31 +02:00
|
|
|
int column_width = this->column_width(column_index);
|
2019-03-15 17:37:13 +01:00
|
|
|
const Font& font = column_metadata.font ? *column_metadata.font : this->font();
|
2019-03-23 02:04:31 +01:00
|
|
|
bool is_key_column = model()->key_column() == column_index;
|
2019-02-28 17:58:53 +01:00
|
|
|
Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
|
2019-03-09 13:59:01 +01:00
|
|
|
if (is_key_column) {
|
|
|
|
auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
|
|
|
|
painter.fill_rect(cell_rect_for_fill, key_column_background_color);
|
|
|
|
}
|
2019-03-29 04:58:15 +01:00
|
|
|
auto cell_index = model()->index(row_index, column_index);
|
2019-08-14 20:34:46 +02:00
|
|
|
|
|
|
|
if (auto* delegate = column_data(column_index).cell_painting_delegate.ptr()) {
|
|
|
|
delegate->paint(painter, cell_rect, *model(), cell_index);
|
2019-03-18 04:54:07 +01:00
|
|
|
} else {
|
2019-08-14 20:34:46 +02:00
|
|
|
auto data = model()->data(cell_index);
|
|
|
|
if (data.is_bitmap()) {
|
|
|
|
painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
|
|
|
|
} else if (data.is_icon()) {
|
|
|
|
if (auto bitmap = data.as_icon().bitmap_for_size(16))
|
|
|
|
painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
|
|
|
|
} else {
|
|
|
|
Color text_color;
|
|
|
|
if (is_selected_row)
|
|
|
|
text_color = Color::White;
|
|
|
|
else
|
2019-12-24 02:10:01 +01:00
|
|
|
text_color = model()->data(cell_index, GModel::Role::ForegroundColor).to_color(SystemColor::WindowText);
|
2019-08-14 20:34:46 +02:00
|
|
|
painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color, TextElision::Right);
|
|
|
|
}
|
2019-03-18 04:54:07 +01:00
|
|
|
}
|
2019-02-28 17:58:53 +01:00
|
|
|
x_offset += column_width + horizontal_padding() * 2;
|
2019-02-28 01:43:50 +01:00
|
|
|
}
|
|
|
|
++painted_item_index;
|
|
|
|
};
|
|
|
|
|
2019-02-28 17:58:53 +01:00
|
|
|
Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
|
2019-12-23 20:24:26 +01:00
|
|
|
painter.fill_rect(unpainted_rect, SystemColor::Base);
|
2019-02-28 10:24:04 +01:00
|
|
|
|
2019-03-15 14:50:36 +01:00
|
|
|
// Untranslate the painter vertically and do the column headers.
|
2019-03-16 16:03:31 +01:00
|
|
|
painter.translate(0, vertical_scrollbar().value());
|
2019-03-15 14:50:36 +01:00
|
|
|
if (headers_visible())
|
|
|
|
paint_headers(painter);
|
|
|
|
}
|