Serenity Operating System
at hosted 577 lines 20 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/StringBuilder.h> 28#include <AK/Vector.h> 29#include <LibGUI/AbstractTableView.h> 30#include <LibGUI/Action.h> 31#include <LibGUI/Menu.h> 32#include <LibGUI/Model.h> 33#include <LibGUI/Painter.h> 34#include <LibGUI/ScrollBar.h> 35#include <LibGUI/Window.h> 36#include <LibGfx/Palette.h> 37 38namespace GUI { 39 40static const int minimum_column_width = 2; 41 42AbstractTableView::AbstractTableView() 43{ 44 set_should_hide_unnecessary_scrollbars(true); 45} 46 47AbstractTableView::~AbstractTableView() 48{ 49} 50 51void AbstractTableView::select_all() 52{ 53 selection().clear(); 54 for (int item_index = 0; item_index < item_count(); ++item_index) { 55 auto index = model()->index(item_index); 56 selection().add(index); 57 } 58} 59 60void AbstractTableView::update_column_sizes() 61{ 62 if (!m_size_columns_to_fit_content) 63 return; 64 65 if (!model()) 66 return; 67 68 auto& model = *this->model(); 69 int column_count = model.column_count(); 70 int row_count = model.row_count(); 71 int key_column = model.key_column(); 72 73 for (int column = 0; column < column_count; ++column) { 74 if (is_column_hidden(column)) 75 continue; 76 int header_width = header_font().width(model.column_name(column)); 77 if (column == key_column) 78 header_width += font().width(" \xc3\xb6"); 79 int column_width = header_width; 80 for (int row = 0; row < row_count; ++row) { 81 auto cell_data = model.data(model.index(row, column)); 82 int cell_width = 0; 83 if (cell_data.is_bitmap()) { 84 cell_width = cell_data.as_bitmap().width(); 85 } else { 86 cell_width = font().width(cell_data.to_string()); 87 } 88 column_width = max(column_width, cell_width); 89 } 90 auto& column_data = this->column_data(column); 91 column_data.width = max(column_data.width, column_width); 92 column_data.has_initialized_width = true; 93 } 94} 95 96void AbstractTableView::update_content_size() 97{ 98 if (!model()) 99 return set_content_size({}); 100 101 int content_width = 0; 102 int column_count = model()->column_count(); 103 for (int i = 0; i < column_count; ++i) { 104 if (!is_column_hidden(i)) 105 content_width += column_width(i) + horizontal_padding() * 2; 106 } 107 int content_height = item_count() * item_height(); 108 109 set_content_size({ content_width, content_height }); 110 set_size_occupied_by_fixed_elements({ 0, header_height() }); 111} 112 113Gfx::Rect AbstractTableView::header_rect(int column_index) const 114{ 115 if (!model()) 116 return {}; 117 if (is_column_hidden(column_index)) 118 return {}; 119 int x_offset = 0; 120 for (int i = 0; i < column_index; ++i) { 121 if (is_column_hidden(i)) 122 continue; 123 x_offset += column_width(i) + horizontal_padding() * 2; 124 } 125 return { x_offset, 0, column_width(column_index) + horizontal_padding() * 2, header_height() }; 126} 127 128void AbstractTableView::set_hovered_header_index(int index) 129{ 130 if (m_hovered_column_header_index == index) 131 return; 132 m_hovered_column_header_index = index; 133 update_headers(); 134} 135 136void AbstractTableView::paint_headers(Painter& painter) 137{ 138 if (!headers_visible()) 139 return; 140 int exposed_width = max(content_size().width(), width()); 141 painter.fill_rect({ 0, 0, exposed_width, header_height() }, palette().button()); 142 painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, palette().threed_highlight()); 143 painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, palette().threed_shadow1()); 144 int x_offset = 0; 145 int column_count = model()->column_count(); 146 for (int column_index = 0; column_index < column_count; ++column_index) { 147 if (is_column_hidden(column_index)) 148 continue; 149 int column_width = this->column_width(column_index); 150 bool is_key_column = model()->key_column() == column_index; 151 Gfx::Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height()); 152 bool pressed = column_index == m_pressed_column_header_index && m_pressed_column_header_is_pressed; 153 bool hovered = column_index == m_hovered_column_header_index && model()->column_metadata(column_index).sortable == Model::ColumnMetadata::Sortable::True; 154 Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered); 155 String text; 156 if (is_key_column) { 157 StringBuilder builder; 158 builder.append(model()->column_name(column_index)); 159 auto sort_order = model()->sort_order(); 160 if (sort_order == SortOrder::Ascending) 161 builder.append(" \xc3\xb6"); 162 else if (sort_order == SortOrder::Descending) 163 builder.append(" \xc3\xb7"); 164 text = builder.to_string(); 165 } else { 166 text = model()->column_name(column_index); 167 } 168 auto text_rect = cell_rect.translated(horizontal_padding(), 0); 169 if (pressed) 170 text_rect.move_by(1, 1); 171 painter.draw_text(text_rect, text, header_font(), Gfx::TextAlignment::CenterLeft, palette().button_text()); 172 x_offset += column_width + horizontal_padding() * 2; 173 } 174} 175 176bool AbstractTableView::is_column_hidden(int column) const 177{ 178 return !column_data(column).visibility; 179} 180 181void AbstractTableView::set_column_hidden(int column, bool hidden) 182{ 183 auto& column_data = this->column_data(column); 184 if (column_data.visibility == !hidden) 185 return; 186 column_data.visibility = !hidden; 187 update_content_size(); 188 update(); 189} 190 191Menu& AbstractTableView::ensure_header_context_menu() 192{ 193 // FIXME: This menu needs to be rebuilt if the model is swapped out, 194 // or if the column count/names change. 195 if (!m_header_context_menu) { 196 ASSERT(model()); 197 m_header_context_menu = Menu::construct(); 198 199 for (int column = 0; column < model()->column_count(); ++column) { 200 auto& column_data = this->column_data(column); 201 auto name = model()->column_name(column); 202 column_data.visibility_action = Action::create(name, [this, column](Action& action) { 203 action.set_checked(!action.is_checked()); 204 set_column_hidden(column, !action.is_checked()); 205 }); 206 column_data.visibility_action->set_checkable(true); 207 column_data.visibility_action->set_checked(true); 208 209 m_header_context_menu->add_action(*column_data.visibility_action); 210 } 211 } 212 return *m_header_context_menu; 213} 214 215const Gfx::Font& AbstractTableView::header_font() 216{ 217 return Gfx::Font::default_bold_font(); 218} 219 220void AbstractTableView::set_cell_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate>&& delegate) 221{ 222 column_data(column).cell_painting_delegate = move(delegate); 223} 224 225void AbstractTableView::update_headers() 226{ 227 Gfx::Rect rect { 0, 0, frame_inner_rect().width(), header_height() }; 228 rect.move_by(frame_thickness(), frame_thickness()); 229 update(rect); 230} 231 232AbstractTableView::ColumnData& AbstractTableView::column_data(int column) const 233{ 234 if (static_cast<size_t>(column) >= m_column_data.size()) 235 m_column_data.resize(column + 1); 236 return m_column_data.at(column); 237} 238 239Gfx::Rect AbstractTableView::column_resize_grabbable_rect(int column) const 240{ 241 if (!model()) 242 return {}; 243 auto header_rect = this->header_rect(column); 244 return { header_rect.right() - 1, header_rect.top(), 4, header_rect.height() }; 245} 246 247int AbstractTableView::column_width(int column_index) const 248{ 249 if (!model()) 250 return 0; 251 auto& column_data = this->column_data(column_index); 252 if (!column_data.has_initialized_width) { 253 ASSERT(!m_size_columns_to_fit_content); 254 column_data.has_initialized_width = true; 255 column_data.width = model()->column_metadata(column_index).preferred_width; 256 } 257 return column_data.width; 258} 259 260void AbstractTableView::mousemove_event(MouseEvent& event) 261{ 262 if (!model()) 263 return AbstractView::mousemove_event(event); 264 265 auto adjusted_position = this->adjusted_position(event.position()); 266 Gfx::Point horizontally_adjusted_position(adjusted_position.x(), event.position().y()); 267 268 if (m_in_column_resize) { 269 auto delta = adjusted_position - m_column_resize_origin; 270 int new_width = m_column_resize_original_width + delta.x(); 271 if (new_width <= minimum_column_width) 272 new_width = minimum_column_width; 273 ASSERT(m_resizing_column >= 0 && m_resizing_column < model()->column_count()); 274 auto& column_data = this->column_data(m_resizing_column); 275 if (column_data.width != new_width) { 276 column_data.width = new_width; 277 update_content_size(); 278 update(); 279 } 280 return; 281 } 282 283 if (m_pressed_column_header_index != -1) { 284 auto header_rect = this->header_rect(m_pressed_column_header_index); 285 if (header_rect.contains(horizontally_adjusted_position)) { 286 set_hovered_header_index(m_pressed_column_header_index); 287 if (!m_pressed_column_header_is_pressed) 288 update_headers(); 289 m_pressed_column_header_is_pressed = true; 290 } else { 291 set_hovered_header_index(-1); 292 if (m_pressed_column_header_is_pressed) 293 update_headers(); 294 m_pressed_column_header_is_pressed = false; 295 } 296 return; 297 } 298 299 if (event.buttons() == 0) { 300 int column_count = model()->column_count(); 301 bool found_hovered_header = false; 302 for (int i = 0; i < column_count; ++i) { 303 if (column_resize_grabbable_rect(i).contains(horizontally_adjusted_position)) { 304 window()->set_override_cursor(StandardCursor::ResizeHorizontal); 305 set_hovered_header_index(-1); 306 return; 307 } 308 if (header_rect(i).contains(horizontally_adjusted_position)) { 309 set_hovered_header_index(i); 310 found_hovered_header = true; 311 } 312 } 313 if (!found_hovered_header) 314 set_hovered_header_index(-1); 315 } 316 window()->set_override_cursor(StandardCursor::None); 317 318 AbstractView::mousemove_event(event); 319} 320 321void AbstractTableView::mouseup_event(MouseEvent& event) 322{ 323 auto adjusted_position = this->adjusted_position(event.position()); 324 Gfx::Point horizontally_adjusted_position(adjusted_position.x(), event.position().y()); 325 if (event.button() == MouseButton::Left) { 326 if (m_in_column_resize) { 327 if (!column_resize_grabbable_rect(m_resizing_column).contains(horizontally_adjusted_position)) 328 window()->set_override_cursor(StandardCursor::None); 329 m_in_column_resize = false; 330 return; 331 } 332 if (m_pressed_column_header_index != -1) { 333 auto header_rect = this->header_rect(m_pressed_column_header_index); 334 if (header_rect.contains(horizontally_adjusted_position)) { 335 auto new_sort_order = SortOrder::Ascending; 336 if (model()->key_column() == m_pressed_column_header_index) 337 new_sort_order = model()->sort_order() == SortOrder::Ascending 338 ? SortOrder::Descending 339 : SortOrder::Ascending; 340 model()->set_key_column_and_sort_order(m_pressed_column_header_index, new_sort_order); 341 } 342 m_pressed_column_header_index = -1; 343 m_pressed_column_header_is_pressed = false; 344 update_headers(); 345 return; 346 } 347 } 348 349 AbstractView::mouseup_event(event); 350} 351 352void AbstractTableView::mousedown_event(MouseEvent& event) 353{ 354 if (!model()) 355 return AbstractView::mousedown_event(event); 356 357 if (event.button() != MouseButton::Left) 358 return AbstractView::mousedown_event(event); 359 360 auto adjusted_position = this->adjusted_position(event.position()); 361 Gfx::Point horizontally_adjusted_position(adjusted_position.x(), event.position().y()); 362 363 if (event.y() < header_height()) { 364 int column_count = model()->column_count(); 365 for (int i = 0; i < column_count; ++i) { 366 if (column_resize_grabbable_rect(i).contains(horizontally_adjusted_position)) { 367 m_resizing_column = i; 368 m_in_column_resize = true; 369 m_column_resize_original_width = column_width(i); 370 m_column_resize_origin = adjusted_position; 371 return; 372 } 373 auto header_rect = this->header_rect(i); 374 auto column_metadata = model()->column_metadata(i); 375 if (header_rect.contains(horizontally_adjusted_position) && column_metadata.sortable == Model::ColumnMetadata::Sortable::True) { 376 m_pressed_column_header_index = i; 377 m_pressed_column_header_is_pressed = true; 378 update_headers(); 379 return; 380 } 381 } 382 return; 383 } 384 385 bool is_toggle; 386 auto index = index_at_event_position(event.position(), is_toggle); 387 388 if (index.is_valid() && is_toggle && model()->row_count(index)) { 389 toggle_index(index); 390 return; 391 } 392 393 AbstractView::mousedown_event(event); 394} 395 396ModelIndex AbstractTableView::index_at_event_position(const Gfx::Point& position, bool& is_toggle) const 397{ 398 is_toggle = false; 399 if (!model()) 400 return {}; 401 402 auto adjusted_position = this->adjusted_position(position); 403 for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) { 404 if (!row_rect(row).contains(adjusted_position)) 405 continue; 406 for (int column = 0, column_count = model()->column_count(); column < column_count; ++column) { 407 if (!content_rect(row, column).contains(adjusted_position)) 408 continue; 409 return model()->index(row, column); 410 } 411 return model()->index(row, 0); 412 } 413 return {}; 414} 415 416ModelIndex AbstractTableView::index_at_event_position(const Gfx::Point& position) const 417{ 418 bool is_toggle; 419 auto index = index_at_event_position(position, is_toggle); 420 return is_toggle ? ModelIndex() : index; 421} 422 423int AbstractTableView::item_count() const 424{ 425 if (!model()) 426 return 0; 427 return model()->row_count(); 428} 429 430void AbstractTableView::keydown_event(KeyEvent& event) 431{ 432 if (!model()) 433 return; 434 auto& model = *this->model(); 435 if (event.key() == KeyCode::Key_Return) { 436 activate_selected(); 437 return; 438 } 439 if (event.key() == KeyCode::Key_Up) { 440 ModelIndex new_index; 441 if (!selection().is_empty()) { 442 auto old_index = selection().first(); 443 new_index = model.index(old_index.row() - 1, old_index.column()); 444 } else { 445 new_index = model.index(0, 0); 446 } 447 if (model.is_valid(new_index)) { 448 selection().set(new_index); 449 scroll_into_view(new_index, Orientation::Vertical); 450 update(); 451 } 452 return; 453 } 454 if (event.key() == KeyCode::Key_Down) { 455 ModelIndex new_index; 456 if (!selection().is_empty()) { 457 auto old_index = selection().first(); 458 new_index = model.index(old_index.row() + 1, old_index.column()); 459 } else { 460 new_index = model.index(0, 0); 461 } 462 if (model.is_valid(new_index)) { 463 selection().set(new_index); 464 scroll_into_view(new_index, Orientation::Vertical); 465 update(); 466 } 467 return; 468 } 469 if (event.key() == KeyCode::Key_PageUp) { 470 int items_per_page = visible_content_rect().height() / item_height(); 471 auto old_index = selection().first(); 472 auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column()); 473 if (model.is_valid(new_index)) { 474 selection().set(new_index); 475 scroll_into_view(new_index, Orientation::Vertical); 476 update(); 477 } 478 return; 479 } 480 if (event.key() == KeyCode::Key_PageDown) { 481 int items_per_page = visible_content_rect().height() / item_height(); 482 auto old_index = selection().first(); 483 auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column()); 484 if (model.is_valid(new_index)) { 485 selection().set(new_index); 486 scroll_into_view(new_index, Orientation::Vertical); 487 update(); 488 } 489 return; 490 } 491 return Widget::keydown_event(event); 492} 493 494void AbstractTableView::scroll_into_view(const ModelIndex& index, Orientation orientation) 495{ 496 auto rect = row_rect(index.row()).translated(0, -header_height()); 497 ScrollableWidget::scroll_into_view(rect, orientation); 498} 499 500void AbstractTableView::doubleclick_event(MouseEvent& event) 501{ 502 if (!model()) 503 return; 504 if (event.button() == MouseButton::Left) { 505 if (event.y() < header_height()) 506 return; 507 if (!selection().is_empty()) { 508 if (is_editable()) 509 begin_editing(selection().first()); 510 else 511 activate_selected(); 512 } 513 } 514} 515 516void AbstractTableView::context_menu_event(ContextMenuEvent& event) 517{ 518 if (!model()) 519 return; 520 if (event.position().y() < header_height()) { 521 ensure_header_context_menu().popup(event.screen_position()); 522 return; 523 } 524 525 bool is_toggle; 526 auto index = index_at_event_position(event.position(), is_toggle); 527 if (index.is_valid()) { 528 if (!selection().contains(index)) 529 selection().set(index); 530 } else { 531 selection().clear(); 532 } 533 if (on_context_menu_request) 534 on_context_menu_request(index, event); 535} 536 537void AbstractTableView::leave_event(Core::Event& event) 538{ 539 AbstractView::leave_event(event); 540 window()->set_override_cursor(StandardCursor::None); 541 set_hovered_header_index(-1); 542} 543 544Gfx::Rect AbstractTableView::content_rect(int row, int column) const 545{ 546 auto row_rect = this->row_rect(row); 547 int x = 0; 548 for (int i = 0; i < column; ++i) 549 x += column_width(i) + horizontal_padding() * 2; 550 551 return { row_rect.x() + x, row_rect.y(), column_width(column) + horizontal_padding() * 2, item_height() }; 552} 553 554Gfx::Rect AbstractTableView::content_rect(const ModelIndex& index) const 555{ 556 return content_rect(index.row(), index.column()); 557} 558 559Gfx::Rect AbstractTableView::row_rect(int item_index) const 560{ 561 return { 0, header_height() + (item_index * item_height()), max(content_size().width(), width()), item_height() }; 562} 563 564Gfx::Point AbstractTableView::adjusted_position(const Gfx::Point& position) const 565{ 566 return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness()); 567} 568 569void AbstractTableView::did_update_model() 570{ 571 AbstractView::did_update_model(); 572 update_column_sizes(); 573 update_content_size(); 574 update(); 575} 576 577}