| 
									
										
										
										
											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-03-23 02:04:31 +01:00
										 |  |  |     : GAbstractView(parent) | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-10 03:43:46 +02:00
										 |  |  |     set_frame_shape(FrameShape::Container); | 
					
						
							|  |  |  |     set_frame_shadow(FrameShadow::Sunken); | 
					
						
							| 
									
										
										
										
											2019-03-28 20:15:13 +01:00
										 |  |  |     set_frame_thickness(2); | 
					
						
							| 
									
										
										
										
											2019-09-05 21:37:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     set_should_hide_unnecessary_scrollbars(true); | 
					
						
							| 
									
										
										
										
											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-08-09 19:30:24 +02:00
										 |  |  | void GTableView::update_column_sizes() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!m_size_columns_to_fit_content) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto& model = *this->model(); | 
					
						
							|  |  |  |     int column_count = model.column_count(); | 
					
						
							|  |  |  |     int row_count = model.row_count(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int column = 0; column < column_count; ++column) { | 
					
						
							|  |  |  |         if (is_column_hidden(column)) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         int header_width = header_font().width(model.column_name(column)); | 
					
						
							|  |  |  |         int column_width = header_width; | 
					
						
							|  |  |  |         for (int row = 0; row < row_count; ++row) { | 
					
						
							|  |  |  |             auto cell_data = model.data(model.index(row, column)); | 
					
						
							|  |  |  |             int cell_width = 0; | 
					
						
							|  |  |  |             if (cell_data.is_bitmap()) { | 
					
						
							|  |  |  |                 cell_width = cell_data.as_bitmap().width(); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 cell_width = font().width(cell_data.to_string()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             column_width = max(column_width, cell_width); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         auto& column_data = this->column_data(column); | 
					
						
							|  |  |  |         column_data.width = max(column_data.width, column_width); | 
					
						
							|  |  |  |         column_data.has_initialized_width = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-16 16:03:31 +01:00
										 |  |  | void GTableView::update_content_size() | 
					
						
							| 
									
										
										
										
											2019-02-28 17:58:53 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-23 02:04:31 +01:00
										 |  |  |     if (!model()) | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         return set_content_size({}); | 
					
						
							| 
									
										
										
										
											2019-03-16 16:03:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     int content_width = 0; | 
					
						
							| 
									
										
										
										
											2019-03-23 02:04:31 +01:00
										 |  |  |     int column_count = model()->column_count(); | 
					
						
							| 
									
										
										
										
											2019-05-11 00:16:34 +02:00
										 |  |  |     for (int i = 0; i < column_count; ++i) { | 
					
						
							|  |  |  |         if (!is_column_hidden(i)) | 
					
						
							|  |  |  |             content_width += column_width(i) + horizontal_padding() * 2; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-16 16:03:31 +01:00
										 |  |  |     int content_height = item_count() * item_height(); | 
					
						
							| 
									
										
										
										
											2019-02-28 13:25:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-16 16:03:31 +01:00
										 |  |  |     set_content_size({ content_width, content_height }); | 
					
						
							|  |  |  |     set_size_occupied_by_fixed_elements({ 0, header_height() }); | 
					
						
							| 
									
										
										
										
											2019-02-28 21:30:17 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 13:25:52 +01:00
										 |  |  | void GTableView::did_update_model() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-23 03:53:51 +01:00
										 |  |  |     GAbstractView::did_update_model(); | 
					
						
							| 
									
										
										
										
											2019-08-09 19:30:24 +02:00
										 |  |  |     update_column_sizes(); | 
					
						
							| 
									
										
										
										
											2019-03-16 16:03:31 +01:00
										 |  |  |     update_content_size(); | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-19 00:07:33 +02:00
										 |  |  | Rect GTableView::content_rect(int row, int column) const | 
					
						
							| 
									
										
										
										
											2019-04-18 22:27:14 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto row_rect = this->row_rect(row); | 
					
						
							|  |  |  |     int x = 0; | 
					
						
							|  |  |  |     for (int i = 0; i < column; ++i) | 
					
						
							|  |  |  |         x += column_width(i) + horizontal_padding() * 2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-17 14:56:53 +02:00
										 |  |  |     return { row_rect.x() + x, row_rect.y(), column_width(column) + horizontal_padding() * 2, item_height() }; | 
					
						
							| 
									
										
										
										
											2019-04-18 22:27:14 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-19 00:07:33 +02:00
										 |  |  | Rect GTableView::content_rect(const GModelIndex& index) const | 
					
						
							| 
									
										
										
										
											2019-04-18 22:27:14 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-19 00:07:33 +02:00
										 |  |  |     return content_rect(index.row(), index.column()); | 
					
						
							| 
									
										
										
										
											2019-04-18 22:27:14 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  | Rect GTableView::row_rect(int item_index) const | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-16 16:03:31 +01:00
										 |  |  |     return { 0, header_height() + (item_index * item_height()), max(content_size().width(), width()), item_height() }; | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-09 13:59:01 +01:00
										 |  |  | int GTableView::column_width(int column_index) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-09 04:56:52 +02:00
										 |  |  |     if (!model()) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |     auto& column_data = this->column_data(column_index); | 
					
						
							|  |  |  |     if (!column_data.has_initialized_width) { | 
					
						
							| 
									
										
										
										
											2019-08-09 19:30:24 +02:00
										 |  |  |         ASSERT(!m_size_columns_to_fit_content); | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |         column_data.has_initialized_width = true; | 
					
						
							|  |  |  |         column_data.width = model()->column_metadata(column_index).preferred_width; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return column_data.width; | 
					
						
							| 
									
										
										
										
											2019-03-09 13:59:01 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Rect GTableView::header_rect(int column_index) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-09 04:56:52 +02:00
										 |  |  |     if (!model()) | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2019-03-20 13:35:11 +01:00
										 |  |  |     if (is_column_hidden(column_index)) | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2019-03-09 13:59:01 +01:00
										 |  |  |     int x_offset = 0; | 
					
						
							| 
									
										
										
										
											2019-03-20 13:35:11 +01:00
										 |  |  |     for (int i = 0; i < column_index; ++i) { | 
					
						
							|  |  |  |         if (is_column_hidden(i)) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2019-03-09 13:59:01 +01:00
										 |  |  |         x_offset += column_width(i) + horizontal_padding() * 2; | 
					
						
							| 
									
										
										
										
											2019-03-20 13:35:11 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |     return { x_offset, 0, column_width(column_index) + horizontal_padding() * 2, header_height() }; | 
					
						
							| 
									
										
										
										
											2019-03-09 13:59:01 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-30 08:13:41 +02:00
										 |  |  | Point GTableView::adjusted_position(const Point& position) const | 
					
						
							| 
									
										
										
										
											2019-05-04 21:16:41 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-06 23:38:35 +02:00
										 |  |  |     return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness()); | 
					
						
							| 
									
										
										
										
											2019-05-04 21:16:41 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  | Rect GTableView::column_resize_grabbable_rect(int column) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-09 04:56:52 +02:00
										 |  |  |     if (!model()) | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |     auto header_rect = this->header_rect(column); | 
					
						
							|  |  |  |     return { header_rect.right() - 1, header_rect.top(), 4, header_rect.height() }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  | void GTableView::mousedown_event(GMouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-11 17:26:30 +02:00
										 |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-10 20:54:58 +02:00
										 |  |  |     if (event.button() != GMouseButton::Left) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 10:18:05 +01:00
										 |  |  |     if (event.y() < header_height()) { | 
					
						
							| 
									
										
										
										
											2019-03-23 02:04:31 +01:00
										 |  |  |         for (int i = 0; i < model()->column_count(); ++i) { | 
					
						
							| 
									
										
										
										
											2019-05-10 20:54:58 +02:00
										 |  |  |             if (column_resize_grabbable_rect(i).contains(event.position())) { | 
					
						
							| 
									
										
										
										
											2019-05-10 20:26:55 +02:00
										 |  |  |                 m_resizing_column = i; | 
					
						
							|  |  |  |                 m_in_column_resize = true; | 
					
						
							|  |  |  |                 m_column_resize_original_width = column_width(i); | 
					
						
							|  |  |  |                 m_column_resize_origin = event.position(); | 
					
						
							|  |  |  |                 return; | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-03-09 13:59:01 +01:00
										 |  |  |             auto header_rect = this->header_rect(i); | 
					
						
							| 
									
										
										
										
											2019-08-14 20:34:46 +02:00
										 |  |  |             auto column_metadata = model()->column_metadata(i); | 
					
						
							| 
									
										
										
										
											2019-08-12 11:46:01 +03:00
										 |  |  |             if (header_rect.contains(event.position()) && column_metadata.sortable == GModel::ColumnMetadata::Sortable::True) { | 
					
						
							| 
									
										
										
										
											2019-03-09 14:24:34 +01:00
										 |  |  |                 auto new_sort_order = GSortOrder::Ascending; | 
					
						
							| 
									
										
										
										
											2019-03-23 02:04:31 +01:00
										 |  |  |                 if (model()->key_column() == i) | 
					
						
							|  |  |  |                     new_sort_order = model()->sort_order() == GSortOrder::Ascending | 
					
						
							| 
									
										
										
										
											2019-03-09 14:39:24 +01:00
										 |  |  |                         ? GSortOrder::Descending | 
					
						
							|  |  |  |                         : GSortOrder::Ascending; | 
					
						
							| 
									
										
										
										
											2019-03-23 02:04:31 +01:00
										 |  |  |                 model()->set_key_column_and_sort_order(i, new_sort_order); | 
					
						
							| 
									
										
										
										
											2019-03-09 13:59:01 +01:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-04 10:18:05 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |     auto index = index_at_event_position(event.position()); | 
					
						
							|  |  |  |     if (!index.is_valid()) { | 
					
						
							|  |  |  |         selection().clear(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (event.modifiers() & Mod_Ctrl) | 
					
						
							|  |  |  |         selection().toggle(index); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         selection().set(index); | 
					
						
							| 
									
										
										
										
											2019-06-30 08:13:41 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GModelIndex GTableView::index_at_event_position(const Point& position) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto adjusted_position = this->adjusted_position(position); | 
					
						
							| 
									
										
										
										
											2019-05-10 20:54:58 +02:00
										 |  |  |     for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) { | 
					
						
							|  |  |  |         if (!row_rect(row).contains(adjusted_position)) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         for (int column = 0, column_count = model()->column_count(); column < column_count; ++column) { | 
					
						
							|  |  |  |             if (!content_rect(row, column).contains(adjusted_position)) | 
					
						
							| 
									
										
										
										
											2019-04-18 22:27:14 +02:00
										 |  |  |                 continue; | 
					
						
							| 
									
										
										
										
											2019-06-30 08:13:41 +02:00
										 |  |  |             return model()->index(row, column); | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-17 14:56:53 +02:00
										 |  |  |         return model()->index(row, 0); | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-30 08:13:41 +02:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  | void GTableView::mousemove_event(GMouseEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-08 03:33:13 +02:00
										 |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |     if (m_in_column_resize) { | 
					
						
							|  |  |  |         auto delta = event.position() - m_column_resize_origin; | 
					
						
							|  |  |  |         int new_width = m_column_resize_original_width + delta.x(); | 
					
						
							| 
									
										
										
										
											2019-05-05 00:58:42 +02:00
										 |  |  |         ASSERT(m_resizing_column >= 0 && m_resizing_column < model()->column_count()); | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |         auto& column_data = this->column_data(m_resizing_column); | 
					
						
							|  |  |  |         if (column_data.width != new_width) { | 
					
						
							|  |  |  |             column_data.width = new_width; | 
					
						
							| 
									
										
										
										
											2019-08-08 20:41:24 +02:00
										 |  |  |             dbg() << "New column width: " << new_width; | 
					
						
							| 
									
										
										
										
											2019-05-06 23:38:35 +02:00
										 |  |  |             update_content_size(); | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |             update(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (event.buttons() == 0) { | 
					
						
							|  |  |  |         for (int i = 0; i < model()->column_count(); ++i) { | 
					
						
							| 
									
										
										
										
											2019-08-17 15:01:45 +02:00
										 |  |  |             if (column_resize_grabbable_rect(i).contains(event.position())) { | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |                 window()->set_override_cursor(GStandardCursor::ResizeHorizontal); | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     window()->set_override_cursor(GStandardCursor::None); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GTableView::mouseup_event(GMouseEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto adjusted_position = this->adjusted_position(event.position()); | 
					
						
							|  |  |  |     if (event.button() == GMouseButton::Left) { | 
					
						
							| 
									
										
										
										
											2019-05-05 00:58:42 +02:00
										 |  |  |         if (m_in_column_resize) { | 
					
						
							|  |  |  |             if (!column_resize_grabbable_rect(m_resizing_column).contains(adjusted_position)) | 
					
						
							|  |  |  |                 window()->set_override_cursor(GStandardCursor::None); | 
					
						
							|  |  |  |             m_in_column_resize = false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02: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-04-11 17:26:30 +02:00
										 |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-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-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-09-18 14:10:09 +02:00
										 |  |  |     int first_visible_row = index_at_event_position(frame_inner_rect().top_left()).row(); | 
					
						
							|  |  |  |     int last_visible_row = index_at_event_position(frame_inner_rect().bottom_right()).row(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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)) { | 
					
						
							|  |  |  |                 background_color = Color(210, 210, 210); | 
					
						
							| 
									
										
										
										
											2019-04-20 19:41:37 +02:00
										 |  |  |                 key_column_background_color = Color(180, 180, 180); | 
					
						
							| 
									
										
										
										
											2019-03-15 21:41:27 +01:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 background_color = Color::White; | 
					
						
							| 
									
										
										
										
											2019-04-20 19:41:37 +02:00
										 |  |  |                 key_column_background_color = Color(210, 210, 210); | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |                         text_color = model()->data(cell_index, GModel::Role::ForegroundColor).to_color(Color::Black); | 
					
						
							|  |  |  |                     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-02-28 01:43:50 +01:00
										 |  |  |     painter.fill_rect(unpainted_rect, Color::White); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GTableView::paint_headers(Painter& painter) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-16 16:03:31 +01:00
										 |  |  |     int exposed_width = max(content_size().width(), width()); | 
					
						
							| 
									
										
										
										
											2019-06-30 09:23:16 +02:00
										 |  |  |     painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::WarmGray); | 
					
						
							| 
									
										
										
										
											2019-03-04 10:54:34 +01:00
										 |  |  |     painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, Color::White); | 
					
						
							| 
									
										
										
										
											2019-03-27 20:48:23 +01:00
										 |  |  |     painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::MidGray); | 
					
						
							| 
									
										
										
										
											2019-02-28 10:24:04 +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-05-04 23:45:31 +02:00
										 |  |  |         int column_width = this->column_width(column_index); | 
					
						
							| 
									
										
										
										
											2019-03-23 02:04:31 +01:00
										 |  |  |         bool is_key_column = model()->key_column() == column_index; | 
					
						
							| 
									
										
										
										
											2019-03-04 10:26:16 +01:00
										 |  |  |         Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height()); | 
					
						
							| 
									
										
										
										
											2019-05-04 21:16:41 +02:00
										 |  |  |         StylePainter::paint_button(painter, cell_rect, ButtonStyle::Normal, false); | 
					
						
							|  |  |  |         String text; | 
					
						
							| 
									
										
										
										
											2019-03-09 13:59:01 +01:00
										 |  |  |         if (is_key_column) { | 
					
						
							| 
									
										
										
										
											2019-05-04 21:16:41 +02:00
										 |  |  |             StringBuilder builder; | 
					
						
							|  |  |  |             builder.append(model()->column_name(column_index)); | 
					
						
							|  |  |  |             auto sort_order = model()->sort_order(); | 
					
						
							|  |  |  |             if (sort_order == GSortOrder::Ascending) | 
					
						
							| 
									
										
										
										
											2019-09-04 23:50:40 +03:00
										 |  |  |                 builder.append(" \xc3\xb6"); | 
					
						
							| 
									
										
										
										
											2019-05-04 21:16:41 +02:00
										 |  |  |             else if (sort_order == GSortOrder::Descending) | 
					
						
							| 
									
										
										
										
											2019-09-04 23:50:40 +03:00
										 |  |  |                 builder.append(" \xc3\xb7"); | 
					
						
							| 
									
										
										
										
											2019-05-04 21:16:41 +02:00
										 |  |  |             text = builder.to_string(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             text = model()->column_name(column_index); | 
					
						
							| 
									
										
										
										
											2019-03-09 13:59:01 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-04 21:16:41 +02:00
										 |  |  |         auto text_rect = cell_rect.translated(horizontal_padding(), 0); | 
					
						
							| 
									
										
										
										
											2019-08-09 19:30:24 +02:00
										 |  |  |         painter.draw_text(text_rect, text, header_font(), TextAlignment::CenterLeft, Color::Black); | 
					
						
							| 
									
										
										
										
											2019-02-28 17:58:53 +01:00
										 |  |  |         x_offset += column_width + horizontal_padding() * 2; | 
					
						
							| 
									
										
										
										
											2019-02-28 10:24:04 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  | int GTableView::item_count() const | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-11 17:26:30 +02:00
										 |  |  |     if (!model()) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2019-03-23 02:04:31 +01:00
										 |  |  |     return model()->row_count(); | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-01 13:48:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GTableView::keydown_event(GKeyEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     auto& model = *this->model(); | 
					
						
							|  |  |  |     if (event.key() == KeyCode::Key_Return) { | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |         selection().for_each_index([this](auto& index) { | 
					
						
							|  |  |  |             activate(index); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-03-01 13:48:08 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (event.key() == KeyCode::Key_Up) { | 
					
						
							| 
									
										
										
										
											2019-03-04 10:57:26 +01:00
										 |  |  |         GModelIndex new_index; | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |         if (!selection().is_empty()) { | 
					
						
							|  |  |  |             auto old_index = selection().first(); | 
					
						
							|  |  |  |             new_index = model.index(old_index.row() - 1, old_index.column()); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-03-29 04:58:15 +01:00
										 |  |  |             new_index = model.index(0, 0); | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-01 14:29:34 +01:00
										 |  |  |         if (model.is_valid(new_index)) { | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |             selection().set(new_index); | 
					
						
							| 
									
										
										
										
											2019-03-01 14:29:34 +01:00
										 |  |  |             scroll_into_view(new_index, Orientation::Vertical); | 
					
						
							|  |  |  |             update(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-01 13:48:08 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (event.key() == KeyCode::Key_Down) { | 
					
						
							| 
									
										
										
										
											2019-03-04 10:57:26 +01:00
										 |  |  |         GModelIndex new_index; | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |         if (!selection().is_empty()) { | 
					
						
							|  |  |  |             auto old_index = selection().first(); | 
					
						
							|  |  |  |             new_index = model.index(old_index.row() + 1, old_index.column()); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-03-29 04:58:15 +01:00
										 |  |  |             new_index = model.index(0, 0); | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-01 14:29:34 +01:00
										 |  |  |         if (model.is_valid(new_index)) { | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |             selection().set(new_index); | 
					
						
							| 
									
										
										
										
											2019-03-01 14:29:34 +01:00
										 |  |  |             scroll_into_view(new_index, Orientation::Vertical); | 
					
						
							|  |  |  |             update(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-02 23:58:45 +01:00
										 |  |  |     if (event.key() == KeyCode::Key_PageUp) { | 
					
						
							|  |  |  |         int items_per_page = visible_content_rect().height() / item_height(); | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |         auto old_index = selection().first(); | 
					
						
							|  |  |  |         auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column()); | 
					
						
							| 
									
										
										
										
											2019-03-02 23:58:45 +01:00
										 |  |  |         if (model.is_valid(new_index)) { | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |             selection().set(new_index); | 
					
						
							| 
									
										
										
										
											2019-03-02 23:58:45 +01:00
										 |  |  |             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(); | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02: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()); | 
					
						
							| 
									
										
										
										
											2019-03-02 23:58:45 +01:00
										 |  |  |         if (model.is_valid(new_index)) { | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |             selection().set(new_index); | 
					
						
							| 
									
										
										
										
											2019-03-02 23:58:45 +01:00
										 |  |  |             scroll_into_view(new_index, Orientation::Vertical); | 
					
						
							|  |  |  |             update(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-01 14:29:34 +01: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 16:03:31 +01:00
										 |  |  |     GScrollableWidget::scroll_into_view(rect, orientation); | 
					
						
							| 
									
										
										
										
											2019-03-01 13:48:08 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-20 13:35:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  | GTableView::ColumnData& GTableView::column_data(int column) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (column >= m_column_data.size()) | 
					
						
							| 
									
										
										
										
											2019-05-09 04:56:52 +02:00
										 |  |  |         m_column_data.resize(column + 1); | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |     return m_column_data.at(column); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 13:35:11 +01:00
										 |  |  | bool GTableView::is_column_hidden(int column) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |     return !column_data(column).visibility; | 
					
						
							| 
									
										
										
										
											2019-03-20 13:35:11 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GTableView::set_column_hidden(int column, bool hidden) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |     auto& column_data = this->column_data(column); | 
					
						
							|  |  |  |     if (column_data.visibility == !hidden) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     column_data.visibility = !hidden; | 
					
						
							| 
									
										
										
										
											2019-05-11 00:19:34 +02:00
										 |  |  |     update_content_size(); | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  |     update(); | 
					
						
							| 
									
										
										
										
											2019-03-20 13:35:11 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-25 01:42:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GTableView::doubleclick_event(GMouseEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     if (event.button() == GMouseButton::Left) { | 
					
						
							| 
									
										
										
										
											2019-04-26 23:13:05 +02:00
										 |  |  |         if (event.y() < header_height()) | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2019-09-07 19:35:45 +02:00
										 |  |  |         if (!selection().is_empty()) { | 
					
						
							|  |  |  |             if (is_editable()) { | 
					
						
							|  |  |  |                 begin_editing(selection().first()); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 selection().for_each_index([this](auto& index) { | 
					
						
							|  |  |  |                     activate(index); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-04-25 22:55:44 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-25 01:42:15 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-10 20:26:55 +02:00
										 |  |  | GMenu& GTableView::ensure_header_context_menu() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // FIXME: This menu needs to be rebuilt if the model is swapped out,
 | 
					
						
							|  |  |  |     //        or if the column count/names change.
 | 
					
						
							|  |  |  |     if (!m_header_context_menu) { | 
					
						
							|  |  |  |         ASSERT(model()); | 
					
						
							| 
									
										
										
										
											2019-09-13 22:14:07 +02:00
										 |  |  |         m_header_context_menu = make<GMenu>(); | 
					
						
							| 
									
										
										
										
											2019-05-10 20:26:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for (int column = 0; column < model()->column_count(); ++column) { | 
					
						
							|  |  |  |             auto& column_data = this->column_data(column); | 
					
						
							| 
									
										
										
										
											2019-05-11 00:16:34 +02:00
										 |  |  |             auto name = model()->column_name(column); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |             column_data.visibility_action = GAction::create(name, [this, column](GAction& action) { | 
					
						
							| 
									
										
										
										
											2019-05-10 20:26:55 +02:00
										 |  |  |                 action.set_checked(!action.is_checked()); | 
					
						
							|  |  |  |                 set_column_hidden(column, !action.is_checked()); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             column_data.visibility_action->set_checkable(true); | 
					
						
							|  |  |  |             column_data.visibility_action->set_checked(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             m_header_context_menu->add_action(*column_data.visibility_action); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return *m_header_context_menu; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GTableView::context_menu_event(GContextMenuEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     if (event.position().y() < header_height()) { | 
					
						
							|  |  |  |         ensure_header_context_menu().popup(event.screen_position()); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-30 08:13:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     auto index = index_at_event_position(event.position()); | 
					
						
							| 
									
										
										
										
											2019-09-13 21:58:30 +02:00
										 |  |  |     if (index.is_valid()) { | 
					
						
							|  |  |  |         if (!selection().contains(index)) | 
					
						
							|  |  |  |             selection().set(index); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         selection().clear(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-30 08:13:41 +02:00
										 |  |  |     if (on_context_menu_request) | 
					
						
							|  |  |  |         on_context_menu_request(index, event); | 
					
						
							| 
									
										
										
										
											2019-05-10 20:26:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 23:45:31 +02:00
										 |  |  | void GTableView::leave_event(CEvent&) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     window()->set_override_cursor(GStandardCursor::None); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-09 19:30:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | const Font& GTableView::header_font() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Font::default_bold_font(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-14 20:34:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GTableView::set_cell_painting_delegate(int column, OwnPtr<GTableCellPaintingDelegate>&& delegate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     column_data(column).cell_painting_delegate = move(delegate); | 
					
						
							|  |  |  | } |