mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 09:42:45 +00:00 
			
		
		
		
	LibGUI: Move AbstractTableView::keydown_event() down to TableView
We can't really share this stuff with TreeView anyway, since tables and trees have very different spatial relationships between indexes.
This commit is contained in:
		
							parent
							
								
									9d9a31384e
								
							
						
					
					
						commit
						80f43ffc27
					
				
					 4 changed files with 44 additions and 43 deletions
				
			
		|  | @ -447,48 +447,6 @@ void AbstractTableView::move_selection(int steps) | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void AbstractTableView::keydown_event(KeyEvent& event) |  | ||||||
| { |  | ||||||
|     if (!model()) |  | ||||||
|         return; |  | ||||||
|     auto& model = *this->model(); |  | ||||||
|     if (event.key() == KeyCode::Key_Return) { |  | ||||||
|         activate_selected(); |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
|     if (event.key() == KeyCode::Key_Up) { |  | ||||||
|         move_selection(-1); |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
|     if (event.key() == KeyCode::Key_Down) { |  | ||||||
|         move_selection(1); |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
|     if (event.key() == KeyCode::Key_PageUp) { |  | ||||||
|         int items_per_page = visible_content_rect().height() / item_height(); |  | ||||||
|         auto old_index = selection().first(); |  | ||||||
|         auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column()); |  | ||||||
|         if (model.is_valid(new_index)) { |  | ||||||
|             selection().set(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(); |  | ||||||
|         auto old_index = selection().first(); |  | ||||||
|         auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column()); |  | ||||||
|         if (model.is_valid(new_index)) { |  | ||||||
|             selection().set(new_index); |  | ||||||
|             scroll_into_view(new_index, Orientation::Vertical); |  | ||||||
|             update(); |  | ||||||
|         } |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
|     return Widget::keydown_event(event); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void AbstractTableView::scroll_into_view(const ModelIndex& index, Orientation orientation) | void AbstractTableView::scroll_into_view(const ModelIndex& index, Orientation orientation) | ||||||
| { | { | ||||||
|     auto rect = row_rect(index.row()).translated(0, -header_height()); |     auto rect = row_rect(index.row()).translated(0, -header_height()); | ||||||
|  |  | ||||||
|  | @ -83,7 +83,6 @@ protected: | ||||||
|     virtual void mousedown_event(MouseEvent&) override; |     virtual void mousedown_event(MouseEvent&) override; | ||||||
|     virtual void mousemove_event(MouseEvent&) override; |     virtual void mousemove_event(MouseEvent&) override; | ||||||
|     virtual void doubleclick_event(MouseEvent&) override; |     virtual void doubleclick_event(MouseEvent&) override; | ||||||
|     virtual void keydown_event(KeyEvent&) override; |  | ||||||
|     virtual void leave_event(Core::Event&) override; |     virtual void leave_event(Core::Event&) override; | ||||||
|     virtual void context_menu_event(ContextMenuEvent&) override; |     virtual void context_menu_event(ContextMenuEvent&) override; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -155,4 +155,47 @@ void TableView::paint_event(PaintEvent& event) | ||||||
|         paint_headers(painter); |         paint_headers(painter); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | void TableView::keydown_event(KeyEvent& event) | ||||||
|  | { | ||||||
|  |     if (!model()) | ||||||
|  |         return; | ||||||
|  |     auto& model = *this->model(); | ||||||
|  |     if (event.key() == KeyCode::Key_Return) { | ||||||
|  |         activate_selected(); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  |     if (event.key() == KeyCode::Key_Up) { | ||||||
|  |         move_selection(-1); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  |     if (event.key() == KeyCode::Key_Down) { | ||||||
|  |         move_selection(1); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  |     if (event.key() == KeyCode::Key_PageUp) { | ||||||
|  |         int items_per_page = visible_content_rect().height() / item_height(); | ||||||
|  |         auto old_index = selection().first(); | ||||||
|  |         auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column()); | ||||||
|  |         if (model.is_valid(new_index)) { | ||||||
|  |             selection().set(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(); | ||||||
|  |         auto old_index = selection().first(); | ||||||
|  |         auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column()); | ||||||
|  |         if (model.is_valid(new_index)) { | ||||||
|  |             selection().set(new_index); | ||||||
|  |             scroll_into_view(new_index, Orientation::Vertical); | ||||||
|  |             update(); | ||||||
|  |         } | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  |     return Widget::keydown_event(event); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -38,6 +38,7 @@ public: | ||||||
| protected: | protected: | ||||||
|     TableView(); |     TableView(); | ||||||
| 
 | 
 | ||||||
|  |     virtual void keydown_event(KeyEvent&) override; | ||||||
|     virtual void paint_event(PaintEvent&) override; |     virtual void paint_event(PaintEvent&) override; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling