| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  | #include <Kernel/KeyCode.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  | #include <LibDraw/Palette.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  | #include <LibGUI/GListView.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GPainter.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  | #include <LibGUI/GScrollBar.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | GListView::GListView(GWidget* parent) | 
					
						
							|  |  |  |     : GAbstractView(parent) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |     set_background_role(ColorRole::Base); | 
					
						
							| 
									
										
										
										
											2019-12-24 22:01:32 +01:00
										 |  |  |     set_foreground_role(ColorRole::BaseText); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |     set_frame_shape(FrameShape::Container); | 
					
						
							|  |  |  |     set_frame_shadow(FrameShadow::Sunken); | 
					
						
							|  |  |  |     set_frame_thickness(2); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GListView::~GListView() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GListView::update_content_size() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         return set_content_size({}); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     int content_width = 0; | 
					
						
							|  |  |  |     for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) { | 
					
						
							|  |  |  |         auto text = model()->data(model()->index(row, m_model_column), GModel::Role::Display); | 
					
						
							|  |  |  |         content_width = max(content_width, font().width(text.to_string())); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     content_width = max(content_width, widget_inner_rect().width()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int content_height = item_count() * item_height(); | 
					
						
							|  |  |  |     set_content_size({ content_width, content_height }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GListView::resize_event(GResizeEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     update_content_size(); | 
					
						
							|  |  |  |     GAbstractView::resize_event(event); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GListView::did_update_model() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     GAbstractView::did_update_model(); | 
					
						
							|  |  |  |     update_content_size(); | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Rect GListView::content_rect(int row) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return { 0, row * item_height(), content_width(), item_height() }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Rect GListView::content_rect(const GModelIndex& index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return content_rect(index.row()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Point GListView::adjusted_position(const Point& position) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GListView::mousedown_event(GMouseEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (event.button() != GMouseButton::Left) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto adjusted_position = this->adjusted_position(event.position()); | 
					
						
							|  |  |  |     for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) { | 
					
						
							|  |  |  |         if (!content_rect(row).contains(adjusted_position)) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |         auto index = model()->index(row, m_model_column); | 
					
						
							|  |  |  |         if (event.modifiers() & Mod_Ctrl) | 
					
						
							|  |  |  |             selection().toggle(index); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             selection().set(index); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |     selection().clear(); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GListView::paint_event(GPaintEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     GFrame::paint_event(event); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     GPainter painter(*this); | 
					
						
							|  |  |  |     painter.add_clip_rect(frame_inner_rect()); | 
					
						
							|  |  |  |     painter.add_clip_rect(event.rect()); | 
					
						
							|  |  |  |     painter.translate(frame_thickness(), frame_thickness()); | 
					
						
							|  |  |  |     painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int exposed_width = max(content_size().width(), width()); | 
					
						
							|  |  |  |     int painted_item_index = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int row_index = 0; row_index < model()->row_count(); ++row_index) { | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |         bool is_selected_row = selection().contains_row(row_index); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |         int y = painted_item_index * item_height(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Color background_color; | 
					
						
							|  |  |  |         if (is_selected_row) { | 
					
						
							| 
									
										
										
										
											2019-12-27 22:51:00 +03:00
										 |  |  |             background_color = is_focused() ? palette().selection() : Color::from_rgb(0x606060); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-12-27 22:51:00 +03:00
										 |  |  |             Color row_fill_color = palette().color(background_role()); | 
					
						
							|  |  |  |             if (alternating_row_colors() && (painted_item_index % 2)) { | 
					
						
							|  |  |  |                 background_color = row_fill_color.darkened(0.8f); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 background_color = row_fill_color; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto column_metadata = model()->column_metadata(m_model_column); | 
					
						
							| 
									
										
										
										
											2019-10-22 21:38:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |         Rect row_rect(0, y, content_width(), item_height()); | 
					
						
							|  |  |  |         painter.fill_rect(row_rect, background_color); | 
					
						
							|  |  |  |         auto index = model()->index(row_index, m_model_column); | 
					
						
							|  |  |  |         auto data = model()->data(index); | 
					
						
							| 
									
										
										
										
											2019-10-22 21:38:04 +02:00
										 |  |  |         auto font = font_for_index(index); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |         if (data.is_bitmap()) { | 
					
						
							|  |  |  |             painter.blit(row_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(row_rect.location(), *bitmap, bitmap->rect()); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             Color text_color; | 
					
						
							|  |  |  |             if (is_selected_row) | 
					
						
							| 
									
										
										
										
											2019-12-27 22:51:00 +03:00
										 |  |  |                 text_color = palette().selection_text(); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |                 text_color = model()->data(index, GModel::Role::ForegroundColor).to_color(palette().color(foreground_role())); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |             auto text_rect = row_rect; | 
					
						
							|  |  |  |             text_rect.move_by(horizontal_padding(), 0); | 
					
						
							|  |  |  |             text_rect.set_width(text_rect.width() - horizontal_padding() * 2); | 
					
						
							|  |  |  |             painter.draw_text(text_rect, data.to_string(), font, column_metadata.text_alignment, text_color); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ++painted_item_index; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Rect unpainted_rect(0, painted_item_index * item_height(), exposed_width, height()); | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |     painter.fill_rect(unpainted_rect, palette().color(background_role())); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int GListView::item_count() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     return model()->row_count(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GListView::keydown_event(GKeyEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     auto& model = *this->model(); | 
					
						
							|  |  |  |     if (event.key() == KeyCode::Key_Return) { | 
					
						
							| 
									
										
										
										
											2020-01-10 18:50:26 +03:00
										 |  |  |         activate_selected(); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (event.key() == KeyCode::Key_Up) { | 
					
						
							|  |  |  |         GModelIndex new_index; | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |         if (!selection().is_empty()) { | 
					
						
							|  |  |  |             auto old_index = selection().first(); | 
					
						
							|  |  |  |             new_index = model.index(old_index.row() - 1, old_index.column()); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |             new_index = model.index(0, 0); | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |         if (model.is_valid(new_index)) { | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |             selection().set(new_index); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |             scroll_into_view(new_index, Orientation::Vertical); | 
					
						
							|  |  |  |             update(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (event.key() == KeyCode::Key_Down) { | 
					
						
							|  |  |  |         GModelIndex new_index; | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |         if (!selection().is_empty()) { | 
					
						
							|  |  |  |             auto old_index = selection().first(); | 
					
						
							|  |  |  |             new_index = model.index(old_index.row() + 1, old_index.column()); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |             new_index = model.index(0, 0); | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |         if (model.is_valid(new_index)) { | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |             selection().set(new_index); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |             scroll_into_view(new_index, Orientation::Vertical); | 
					
						
							|  |  |  |             update(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (event.key() == KeyCode::Key_PageUp) { | 
					
						
							|  |  |  |         int items_per_page = visible_content_rect().height() / item_height(); | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |         auto old_index = selection().first(); | 
					
						
							|  |  |  |         auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column()); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |         if (model.is_valid(new_index)) { | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |             selection().set(new_index); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02: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 20:31:11 +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-05-27 20:19:05 +02:00
										 |  |  |         if (model.is_valid(new_index)) { | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |             selection().set(new_index); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |             scroll_into_view(new_index, Orientation::Vertical); | 
					
						
							|  |  |  |             update(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return GWidget::keydown_event(event); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GListView::scroll_into_view(const GModelIndex& index, Orientation orientation) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto rect = content_rect(index.row()); | 
					
						
							|  |  |  |     GScrollableWidget::scroll_into_view(rect, orientation); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GListView::doubleclick_event(GMouseEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     if (event.button() == GMouseButton::Left) { | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |         if (!selection().is_empty()) { | 
					
						
							| 
									
										
										
										
											2020-01-10 18:50:26 +03:00
										 |  |  |             if (is_editable()) | 
					
						
							| 
									
										
										
										
											2019-09-07 20:31:11 +02:00
										 |  |  |                 begin_editing(selection().first()); | 
					
						
							| 
									
										
										
										
											2020-01-10 18:50:26 +03:00
										 |  |  |             else | 
					
						
							|  |  |  |                 activate_selected(); | 
					
						
							| 
									
										
										
										
											2019-05-27 20:19:05 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |