2019-02-28 09:57:09 +00:00
|
|
|
#include <LibGUI/GTableView.h>
|
2019-03-23 00:42:49 +00:00
|
|
|
#include <LibGUI/GModel.h>
|
2019-02-28 00:43:50 +00:00
|
|
|
#include <LibGUI/GScrollBar.h>
|
2019-02-28 09:57:09 +00:00
|
|
|
#include <SharedGraphics/Painter.h>
|
2019-03-01 12:48:08 +00:00
|
|
|
#include <Kernel/KeyCode.h>
|
2019-02-28 00:43:50 +00:00
|
|
|
|
2019-02-28 09:57:09 +00:00
|
|
|
GTableView::GTableView(GWidget* parent)
|
2019-03-23 01:04:31 +00:00
|
|
|
: GAbstractView(parent)
|
2019-02-28 00:43:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:57:09 +00:00
|
|
|
GTableView::~GTableView()
|
2019-02-28 00:43:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-16 15:03:31 +00:00
|
|
|
void GTableView::update_content_size()
|
2019-02-28 16:58:53 +00:00
|
|
|
{
|
2019-03-23 01:04:31 +00:00
|
|
|
if (!model())
|
2019-03-16 15:03:31 +00:00
|
|
|
return set_content_size({ });
|
|
|
|
|
|
|
|
int content_width = 0;
|
2019-03-23 01:04:31 +00:00
|
|
|
int column_count = model()->column_count();
|
2019-02-28 16:58:53 +00:00
|
|
|
for (int i = 0; i < column_count; ++i)
|
2019-03-23 01:04:31 +00:00
|
|
|
content_width += model()->column_metadata(i).preferred_width + horizontal_padding() * 2;
|
2019-03-16 15:03:31 +00:00
|
|
|
int content_height = item_count() * item_height();
|
2019-02-28 12:25:52 +00:00
|
|
|
|
2019-03-16 15:03:31 +00:00
|
|
|
set_content_size({ content_width, content_height });
|
|
|
|
set_size_occupied_by_fixed_elements({ 0, header_height() });
|
2019-02-28 20:30:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 12:25:52 +00:00
|
|
|
void GTableView::did_update_model()
|
|
|
|
{
|
2019-03-23 02:53:51 +00:00
|
|
|
GAbstractView::did_update_model();
|
2019-03-16 15:03:31 +00:00
|
|
|
update_content_size();
|
2019-02-28 00:43:50 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:57:09 +00:00
|
|
|
Rect GTableView::row_rect(int item_index) const
|
2019-02-28 00:43:50 +00:00
|
|
|
{
|
2019-03-16 15:03:31 +00:00
|
|
|
return { 0, header_height() + (item_index * item_height()), max(content_size().width(), width()), item_height() };
|
2019-02-28 00:43:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 12:59:01 +00:00
|
|
|
int GTableView::column_width(int column_index) const
|
|
|
|
{
|
2019-03-23 01:04:31 +00:00
|
|
|
return model()->column_metadata(column_index).preferred_width;
|
2019-03-09 12:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Rect GTableView::header_rect(int column_index) const
|
|
|
|
{
|
2019-03-20 12:35:11 +00:00
|
|
|
if (is_column_hidden(column_index))
|
|
|
|
return { };
|
2019-03-09 12:59:01 +00:00
|
|
|
int x_offset = 0;
|
2019-03-20 12:35:11 +00:00
|
|
|
for (int i = 0; i < column_index; ++i) {
|
|
|
|
if (is_column_hidden(i))
|
|
|
|
continue;
|
2019-03-09 12:59:01 +00:00
|
|
|
x_offset += column_width(i) + horizontal_padding() * 2;
|
2019-03-20 12:35:11 +00:00
|
|
|
}
|
2019-03-23 01:04:31 +00:00
|
|
|
auto column_metadata = model()->column_metadata(column_index);
|
2019-03-09 12:59:01 +00:00
|
|
|
int column_width = column_metadata.preferred_width;
|
|
|
|
return { x_offset, 0, column_width + horizontal_padding() * 2, header_height() };
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:57:09 +00:00
|
|
|
void GTableView::mousedown_event(GMouseEvent& event)
|
2019-02-28 00:43:50 +00:00
|
|
|
{
|
2019-03-04 09:18:05 +00:00
|
|
|
if (event.y() < header_height()) {
|
2019-03-16 15:03:31 +00:00
|
|
|
auto adjusted_position = event.position().translated(horizontal_scrollbar().value(), 0);
|
2019-03-23 01:04:31 +00:00
|
|
|
for (int i = 0; i < model()->column_count(); ++i) {
|
2019-03-09 12:59:01 +00:00
|
|
|
auto header_rect = this->header_rect(i);
|
|
|
|
if (header_rect.contains(adjusted_position)) {
|
2019-03-09 13:24:34 +00:00
|
|
|
auto new_sort_order = GSortOrder::Ascending;
|
2019-03-23 01:04:31 +00:00
|
|
|
if (model()->key_column() == i)
|
|
|
|
new_sort_order = model()->sort_order() == GSortOrder::Ascending
|
2019-03-09 13:39:24 +00:00
|
|
|
? GSortOrder::Descending
|
|
|
|
: GSortOrder::Ascending;
|
2019-03-23 01:04:31 +00:00
|
|
|
model()->set_key_column_and_sort_order(i, new_sort_order);
|
2019-03-09 12:59:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 09:18:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-28 00:43:50 +00:00
|
|
|
if (event.button() == GMouseButton::Left) {
|
2019-03-16 15:03:31 +00:00
|
|
|
auto adjusted_position = event.position().translated(0, vertical_scrollbar().value());
|
2019-02-28 00:43:50 +00:00
|
|
|
for (int i = 0; i < item_count(); ++i) {
|
2019-02-28 13:05:02 +00:00
|
|
|
if (row_rect(i).contains(adjusted_position)) {
|
2019-03-23 01:04:31 +00:00
|
|
|
model()->set_selected_index({ i, 0 });
|
2019-02-28 13:05:02 +00:00
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
2019-02-28 00:43:50 +00:00
|
|
|
}
|
2019-03-23 01:04:31 +00:00
|
|
|
model()->set_selected_index({ });
|
2019-03-06 18:44:18 +00:00
|
|
|
update();
|
2019-02-28 00:43:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 17:57:36 +00:00
|
|
|
void GTableView::paint_event(GPaintEvent& event)
|
2019-02-28 00:43:50 +00:00
|
|
|
{
|
|
|
|
Painter painter(*this);
|
2019-02-28 17:57:36 +00:00
|
|
|
painter.set_clip_rect(event.rect());
|
2019-03-15 13:50:36 +00:00
|
|
|
painter.save();
|
2019-03-16 15:03:31 +00:00
|
|
|
painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
|
2019-02-28 00:43:50 +00:00
|
|
|
|
2019-03-16 15:03:31 +00:00
|
|
|
int exposed_width = max(content_size().width(), width());
|
2019-02-28 00:43:50 +00:00
|
|
|
int painted_item_index = 0;
|
|
|
|
int y_offset = header_height();
|
|
|
|
|
2019-03-23 01:04:31 +00:00
|
|
|
for (int row_index = 0; row_index < model()->row_count(); ++row_index) {
|
|
|
|
bool is_selected_row = row_index == model()->selected_index().row();
|
2019-02-28 00:43:50 +00:00
|
|
|
int y = y_offset + painted_item_index * item_height();
|
|
|
|
|
|
|
|
Color background_color;
|
2019-03-09 12:59:01 +00:00
|
|
|
Color key_column_background_color;
|
2019-03-18 03:54:07 +00:00
|
|
|
if (is_selected_row) {
|
2019-03-04 10:17:25 +00:00
|
|
|
background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
|
2019-03-09 12:59:01 +00:00
|
|
|
key_column_background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
|
2019-02-28 00:43:50 +00:00
|
|
|
} else {
|
2019-03-15 20:41:27 +00:00
|
|
|
if (alternating_row_colors() && (painted_item_index % 2)) {
|
|
|
|
background_color = Color(210, 210, 210);
|
|
|
|
key_column_background_color = Color(190, 190, 190);
|
|
|
|
} else {
|
|
|
|
background_color = Color::White;
|
|
|
|
key_column_background_color = Color(235, 235, 235);
|
|
|
|
}
|
2019-02-28 00:43:50 +00:00
|
|
|
}
|
|
|
|
painter.fill_rect(row_rect(painted_item_index), background_color);
|
2019-03-18 03:54:07 +00:00
|
|
|
|
2019-02-28 00:43:50 +00:00
|
|
|
int x_offset = 0;
|
2019-03-23 01:04:31 +00:00
|
|
|
for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
|
2019-03-20 12:35:11 +00:00
|
|
|
if (is_column_hidden(column_index))
|
|
|
|
continue;
|
2019-03-23 01:04:31 +00:00
|
|
|
auto column_metadata = model()->column_metadata(column_index);
|
2019-02-28 10:27:04 +00:00
|
|
|
int column_width = column_metadata.preferred_width;
|
2019-03-15 16:37:13 +00:00
|
|
|
const Font& font = column_metadata.font ? *column_metadata.font : this->font();
|
2019-03-23 01:04:31 +00:00
|
|
|
bool is_key_column = model()->key_column() == column_index;
|
2019-02-28 16:58:53 +00:00
|
|
|
Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
|
2019-03-09 12:59:01 +00: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-18 03:54:07 +00:00
|
|
|
GModelIndex cell_index(row_index, column_index);
|
2019-03-23 01:04:31 +00:00
|
|
|
auto data = model()->data(cell_index);
|
2019-03-18 03:54:07 +00:00
|
|
|
if (data.is_bitmap()) {
|
2019-02-28 19:16:10 +00:00
|
|
|
painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
|
2019-03-18 03:54:07 +00:00
|
|
|
} else {
|
|
|
|
Color text_color;
|
|
|
|
if (is_selected_row)
|
|
|
|
text_color = Color::White;
|
|
|
|
else
|
2019-03-23 01:04:31 +00:00
|
|
|
text_color = model()->data(cell_index, GModel::Role::ForegroundColor).to_color(Color::Black);
|
2019-03-15 16:37:13 +00:00
|
|
|
painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color);
|
2019-03-18 03:54:07 +00:00
|
|
|
}
|
2019-02-28 16:58:53 +00:00
|
|
|
x_offset += column_width + horizontal_padding() * 2;
|
2019-02-28 00:43:50 +00:00
|
|
|
}
|
|
|
|
++painted_item_index;
|
|
|
|
};
|
|
|
|
|
2019-02-28 16:58:53 +00:00
|
|
|
Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
|
2019-02-28 00:43:50 +00:00
|
|
|
painter.fill_rect(unpainted_rect, Color::White);
|
2019-02-28 09:24:04 +00:00
|
|
|
|
2019-03-15 13:50:36 +00:00
|
|
|
// Untranslate the painter vertically and do the column headers.
|
2019-03-16 15:03:31 +00:00
|
|
|
painter.translate(0, vertical_scrollbar().value());
|
2019-03-15 13:50:36 +00:00
|
|
|
if (headers_visible())
|
|
|
|
paint_headers(painter);
|
|
|
|
|
|
|
|
painter.restore();
|
|
|
|
|
|
|
|
if (is_focused()) {
|
|
|
|
Rect item_area_rect {
|
|
|
|
0,
|
|
|
|
header_height(),
|
2019-03-16 15:03:31 +00:00
|
|
|
width() - vertical_scrollbar().width(),
|
|
|
|
height() - header_height() - horizontal_scrollbar().height()
|
2019-03-15 13:50:36 +00:00
|
|
|
};
|
|
|
|
painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTableView::paint_headers(Painter& painter)
|
|
|
|
{
|
2019-03-16 15:03:31 +00:00
|
|
|
int exposed_width = max(content_size().width(), width());
|
2019-02-28 16:58:53 +00:00
|
|
|
painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::LightGray);
|
2019-03-04 09:54:34 +00:00
|
|
|
painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, Color::White);
|
|
|
|
painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::DarkGray);
|
2019-02-28 09:24:04 +00:00
|
|
|
int x_offset = 0;
|
2019-03-23 01:04:31 +00:00
|
|
|
for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
|
2019-03-20 12:35:11 +00:00
|
|
|
if (is_column_hidden(column_index))
|
|
|
|
continue;
|
2019-03-23 01:04:31 +00:00
|
|
|
auto column_metadata = model()->column_metadata(column_index);
|
2019-02-28 10:27:04 +00:00
|
|
|
int column_width = column_metadata.preferred_width;
|
2019-03-23 01:04:31 +00:00
|
|
|
bool is_key_column = model()->key_column() == column_index;
|
2019-03-04 09:26:16 +00:00
|
|
|
Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height());
|
2019-03-09 12:59:01 +00:00
|
|
|
if (is_key_column) {
|
|
|
|
painter.fill_rect(cell_rect.shrunken(2, 2), Color::from_rgb(0xdddddd));
|
|
|
|
}
|
2019-03-23 01:04:31 +00:00
|
|
|
painter.draw_text(cell_rect.translated(horizontal_padding(), 0), model()->column_name(column_index), Font::default_bold_font(), TextAlignment::CenterLeft, Color::Black);
|
2019-02-28 16:58:53 +00:00
|
|
|
x_offset += column_width + horizontal_padding() * 2;
|
2019-03-04 09:26:16 +00:00
|
|
|
// Draw column separator.
|
2019-03-04 09:54:34 +00:00
|
|
|
painter.draw_line(cell_rect.top_left().translated(0, 1), cell_rect.bottom_left().translated(0, -1), Color::White);
|
|
|
|
painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right().translated(0, -1), Color::DarkGray);
|
2019-02-28 09:24:04 +00:00
|
|
|
}
|
2019-03-04 09:26:16 +00:00
|
|
|
// Draw the "start" of a new column to make the last separator look right.
|
2019-03-04 09:54:34 +00:00
|
|
|
painter.draw_line({ x_offset, 1 }, { x_offset, header_height() - 2 }, Color::White);
|
2019-02-28 00:43:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 09:57:09 +00:00
|
|
|
int GTableView::item_count() const
|
2019-02-28 00:43:50 +00:00
|
|
|
{
|
2019-03-23 01:04:31 +00:00
|
|
|
return model()->row_count();
|
2019-02-28 00:43:50 +00:00
|
|
|
}
|
2019-03-01 12:48:08 +00:00
|
|
|
|
|
|
|
void GTableView::keydown_event(GKeyEvent& event)
|
|
|
|
{
|
|
|
|
if (!model())
|
|
|
|
return;
|
|
|
|
auto& model = *this->model();
|
|
|
|
if (event.key() == KeyCode::Key_Return) {
|
|
|
|
model.activate(model.selected_index());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.key() == KeyCode::Key_Up) {
|
2019-03-04 09:57:26 +00:00
|
|
|
GModelIndex new_index;
|
|
|
|
if (model.selected_index().is_valid())
|
|
|
|
new_index = { model.selected_index().row() - 1, model.selected_index().column() };
|
|
|
|
else
|
|
|
|
new_index = { 0, 0 };
|
2019-03-01 13:29:34 +00:00
|
|
|
if (model.is_valid(new_index)) {
|
|
|
|
model.set_selected_index(new_index);
|
|
|
|
scroll_into_view(new_index, Orientation::Vertical);
|
|
|
|
update();
|
|
|
|
}
|
2019-03-01 12:48:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.key() == KeyCode::Key_Down) {
|
2019-03-04 09:57:26 +00:00
|
|
|
GModelIndex new_index;
|
|
|
|
if (model.selected_index().is_valid())
|
|
|
|
new_index = { model.selected_index().row() + 1, model.selected_index().column() };
|
|
|
|
else
|
|
|
|
new_index = { 0, 0 };
|
2019-03-01 13:29:34 +00:00
|
|
|
if (model.is_valid(new_index)) {
|
|
|
|
model.set_selected_index(new_index);
|
|
|
|
scroll_into_view(new_index, Orientation::Vertical);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-03-02 22:58:45 +00:00
|
|
|
if (event.key() == KeyCode::Key_PageUp) {
|
|
|
|
int items_per_page = visible_content_rect().height() / item_height();
|
|
|
|
GModelIndex new_index(max(0, model.selected_index().row() - items_per_page), model.selected_index().column());
|
|
|
|
if (model.is_valid(new_index)) {
|
|
|
|
model.set_selected_index(new_index);
|
|
|
|
scroll_into_view(new_index, Orientation::Vertical);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.key() == KeyCode::Key_PageDown) {
|
|
|
|
int items_per_page = visible_content_rect().height() / item_height();
|
|
|
|
GModelIndex new_index(min(model.row_count() - 1, model.selected_index().row() + items_per_page), model.selected_index().column());
|
|
|
|
if (model.is_valid(new_index)) {
|
|
|
|
model.set_selected_index(new_index);
|
|
|
|
scroll_into_view(new_index, Orientation::Vertical);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-03-01 13:29:34 +00:00
|
|
|
return GWidget::keydown_event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GTableView::scroll_into_view(const GModelIndex& index, Orientation orientation)
|
|
|
|
{
|
|
|
|
auto rect = row_rect(index.row()).translated(0, -header_height());
|
2019-03-16 15:03:31 +00:00
|
|
|
GScrollableWidget::scroll_into_view(rect, orientation);
|
2019-03-01 12:48:08 +00:00
|
|
|
}
|
2019-03-20 12:35:11 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|