Serenity Operating System
at master 441 lines 15 kB view raw
1/* 2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2022, the SerenityOS developers. 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <LibGUI/AbstractTableView.h> 9#include <LibGUI/Action.h> 10#include <LibGUI/HeaderView.h> 11#include <LibGUI/Menu.h> 12#include <LibGUI/Model.h> 13#include <LibGUI/Painter.h> 14#include <LibGUI/Window.h> 15#include <LibGfx/Font/FontDatabase.h> 16#include <LibGfx/Palette.h> 17#include <LibGfx/StylePainter.h> 18 19namespace GUI { 20 21HeaderView::HeaderView(AbstractTableView& table_view, Gfx::Orientation orientation) 22 : m_table_view(table_view) 23 , m_orientation(orientation) 24{ 25 set_font(Gfx::FontDatabase::default_font().bold_variant()); 26 27 if (m_orientation == Gfx::Orientation::Horizontal) { 28 set_fixed_height(16); 29 } else { 30 set_fixed_width(40); 31 } 32} 33 34void HeaderView::set_section_size(int section, int size) 35{ 36 auto& data = section_data(section); 37 if (data.size == size) 38 return; 39 data.size = size; 40 data.has_initialized_size = true; 41 data.size = size; 42 m_table_view.header_did_change_section_size({}, m_orientation, section, size); 43} 44 45int HeaderView::section_size(int section) const 46{ 47 return section_data(section).size; 48} 49 50HeaderView::SectionData& HeaderView::section_data(int section) const 51{ 52 VERIFY(model()); 53 if (static_cast<size_t>(section) >= m_section_data.size()) { 54 m_section_data.resize(section_count()); 55 } 56 return m_section_data.at(section); 57} 58 59Gfx::IntRect HeaderView::section_rect(int section) const 60{ 61 if (!model()) 62 return {}; 63 auto& data = section_data(section); 64 if (!data.visibility) 65 return {}; 66 int offset = 0; 67 for (int i = 0; i < section; ++i) { 68 if (!is_section_visible(i)) 69 continue; 70 offset += section_data(i).size; 71 if (orientation() == Gfx::Orientation::Horizontal) 72 offset += m_table_view.horizontal_padding() * 2; 73 } 74 if (orientation() == Gfx::Orientation::Horizontal) 75 return { offset, 0, section_size(section) + m_table_view.horizontal_padding() * 2, height() }; 76 return { 0, offset, width(), section_size(section) }; 77} 78 79HeaderView::VisibleSectionRange HeaderView::visible_section_range() const 80{ 81 auto section_count = this->section_count(); 82 auto is_horizontal = m_orientation == Orientation::Horizontal; 83 auto rect = m_table_view.visible_content_rect(); 84 auto start = is_horizontal ? rect.top_left().x() : rect.top_left().y(); 85 auto end = is_horizontal ? (rect.top_left().x() + m_table_view.content_width()) : rect.bottom_left().y(); 86 auto offset = 0; 87 VisibleSectionRange range; 88 for (; range.end < section_count; ++range.end) { 89 auto& section = section_data(range.end); 90 int section_size = section.size; 91 if (orientation() == Gfx::Orientation::Horizontal) 92 section_size += m_table_view.horizontal_padding() * 2; 93 if (offset + section_size < start) { 94 if (section.visibility) 95 offset += section_size; 96 ++range.start; 97 range.start_offset = offset; 98 continue; 99 } 100 if (offset >= end) 101 break; 102 if (section.visibility) 103 offset += section_size; 104 } 105 return range; 106} 107 108Gfx::IntRect HeaderView::section_resize_grabbable_rect(int section) const 109{ 110 if (!model()) 111 return {}; 112 // FIXME: Support resizable rows. 113 if (m_orientation == Gfx::Orientation::Vertical) 114 return {}; 115 auto rect = section_rect(section); 116 return { rect.right() - 1, rect.top(), 4, rect.height() }; 117} 118 119int HeaderView::section_count() const 120{ 121 if (!model()) 122 return 0; 123 return m_orientation == Gfx::Orientation::Horizontal ? model()->column_count() : model()->row_count(); 124} 125 126void HeaderView::doubleclick_event(MouseEvent& event) 127{ 128 if (!model()) 129 return; 130 131 auto range = visible_section_range(); 132 for (int i = range.start; i < range.end; ++i) { 133 if (section_resize_grabbable_rect(i).contains(event.position())) { 134 if (on_resize_doubleclick) 135 on_resize_doubleclick(i); 136 } 137 } 138} 139 140void HeaderView::mousedown_event(MouseEvent& event) 141{ 142 if (event.button() != GUI::MouseButton::Primary) 143 return; 144 145 if (!model()) 146 return; 147 148 auto& model = *this->model(); 149 auto range = visible_section_range(); 150 151 for (int i = range.start; i < range.end; ++i) { 152 if (section_resize_grabbable_rect(i).contains(event.position())) { 153 m_resizing_section = i; 154 m_in_section_resize = true; 155 m_section_resize_original_width = section_size(i); 156 m_section_resize_origin = event.position(); 157 return; 158 } 159 auto rect = this->section_rect(i); 160 if (rect.contains(event.position()) && model.is_column_sortable(i)) { 161 m_pressed_section = i; 162 m_pressed_section_is_pressed = true; 163 update(); 164 return; 165 } 166 } 167} 168 169void HeaderView::mousemove_event(MouseEvent& event) 170{ 171 if (!model()) 172 return; 173 174 if (m_in_section_resize) { 175 auto delta = event.position() - m_section_resize_origin; 176 int new_size = m_section_resize_original_width + delta.primary_offset_for_orientation(m_orientation); 177 178 auto minimum_size = orientation() == Orientation::Horizontal 179 ? m_table_view.minimum_column_width(m_resizing_section) 180 : m_table_view.minimum_row_height(m_resizing_section); 181 182 if (new_size <= minimum_size) 183 new_size = minimum_size; 184 VERIFY(m_resizing_section >= 0 && m_resizing_section < model()->column_count()); 185 set_section_size(m_resizing_section, new_size); 186 return; 187 } 188 189 if (m_pressed_section != -1) { 190 auto header_rect = this->section_rect(m_pressed_section); 191 if (header_rect.contains(event.position())) { 192 set_hovered_section(m_pressed_section); 193 if (!m_pressed_section_is_pressed) 194 update(); 195 m_pressed_section_is_pressed = true; 196 } else { 197 set_hovered_section(-1); 198 if (m_pressed_section_is_pressed) 199 update(); 200 m_pressed_section_is_pressed = false; 201 } 202 return; 203 } 204 205 if (event.buttons() == 0) { 206 bool found_hovered_header = false; 207 auto range = visible_section_range(); 208 for (int i = range.start; i < range.end; ++i) { 209 if (section_resize_grabbable_rect(i).contains(event.position())) { 210 set_override_cursor(Gfx::StandardCursor::ResizeColumn); 211 set_hovered_section(-1); 212 return; 213 } 214 if (section_rect(i).contains(event.position())) { 215 set_hovered_section(i); 216 found_hovered_header = true; 217 } 218 } 219 if (!found_hovered_header) 220 set_hovered_section(-1); 221 } 222 set_override_cursor(Gfx::StandardCursor::None); 223} 224 225void HeaderView::mouseup_event(MouseEvent& event) 226{ 227 if (event.button() == MouseButton::Primary) { 228 if (m_in_section_resize) { 229 if (!section_resize_grabbable_rect(m_resizing_section).contains(event.position())) 230 set_override_cursor(Gfx::StandardCursor::None); 231 m_in_section_resize = false; 232 return; 233 } 234 if (m_pressed_section != -1) { 235 if (m_orientation == Gfx::Orientation::Horizontal && section_rect(m_pressed_section).contains(event.position())) { 236 auto new_sort_order = m_table_view.sort_order(); 237 if (m_table_view.key_column() == m_pressed_section) 238 new_sort_order = m_table_view.sort_order() == SortOrder::Ascending 239 ? SortOrder::Descending 240 : SortOrder::Ascending; 241 m_table_view.set_key_column_and_sort_order(m_pressed_section, new_sort_order); 242 } 243 m_pressed_section = -1; 244 m_pressed_section_is_pressed = false; 245 update(); 246 return; 247 } 248 } 249} 250 251void HeaderView::paint_horizontal(Painter& painter) 252{ 253 painter.draw_line({ 0, 0 }, { rect().right(), 0 }, palette().threed_highlight()); 254 painter.draw_line({ 0, rect().bottom() }, { rect().right(), rect().bottom() }, palette().threed_shadow1()); 255 auto range = visible_section_range(); 256 int x_offset = range.start_offset; 257 for (int section = range.start; section < range.end; ++section) { 258 auto& section_data = this->section_data(section); 259 if (!section_data.visibility) 260 continue; 261 int section_width = section_data.size; 262 bool is_key_column = m_table_view.key_column() == section; 263 Gfx::IntRect cell_rect(x_offset, 0, section_width + m_table_view.horizontal_padding() * 2, height()); 264 bool pressed = section == m_pressed_section && m_pressed_section_is_pressed; 265 bool hovered = section == m_hovered_section && model()->is_column_sortable(section); 266 Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered); 267 268 auto text = model()->column_name(section); 269 auto text_rect = cell_rect.shrunken(m_table_view.horizontal_padding() * 2, 0); 270 if (pressed) 271 text_rect.translate_by(1, 1); 272 painter.draw_text(text_rect, text, font(), section_data.alignment, palette().button_text()); 273 274 if (is_key_column && (m_table_view.sort_order() != SortOrder::None)) { 275 Gfx::IntPoint offset { text_rect.x() + static_cast<int>(ceilf(font().width(text))) + sorting_arrow_offset, sorting_arrow_offset }; 276 auto coordinates = m_table_view.sort_order() == SortOrder::Ascending 277 ? ascending_arrow_coordinates.span() 278 : descending_arrow_coordinates.span(); 279 280 painter.draw_triangle(offset, coordinates, palette().button_text()); 281 } 282 283 x_offset += section_width + m_table_view.horizontal_padding() * 2; 284 } 285 286 if (x_offset < rect().right()) { 287 Gfx::IntRect cell_rect(x_offset, 0, width() - x_offset, height()); 288 Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, false, false); 289 } 290} 291 292void HeaderView::paint_vertical(Painter& painter) 293{ 294 painter.draw_line(rect().top_left(), rect().bottom_left(), palette().threed_highlight()); 295 painter.draw_line(rect().top_right(), rect().bottom_right(), palette().threed_shadow1()); 296 auto range = visible_section_range(); 297 int y_offset = range.start_offset; 298 for (int section = range.start; section < range.end; ++section) { 299 auto& section_data = this->section_data(section); 300 if (!section_data.visibility) 301 continue; 302 int section_size = section_data.size; 303 Gfx::IntRect cell_rect(0, y_offset, width(), section_size); 304 bool pressed = section == m_pressed_section && m_pressed_section_is_pressed; 305 bool hovered = false; 306 Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered); 307 DeprecatedString text = DeprecatedString::number(section); 308 auto text_rect = cell_rect.shrunken(m_table_view.horizontal_padding() * 2, 0); 309 if (pressed) 310 text_rect.translate_by(1, 1); 311 painter.draw_text(text_rect, text, font(), section_data.alignment, palette().button_text()); 312 y_offset += section_size; 313 } 314 315 if (y_offset < rect().bottom()) { 316 Gfx::IntRect cell_rect(0, y_offset, width(), height() - y_offset); 317 Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, false, false); 318 } 319} 320 321void HeaderView::paint_event(PaintEvent& event) 322{ 323 Painter painter(*this); 324 painter.add_clip_rect(event.rect()); 325 painter.fill_rect(rect(), palette().button()); 326 if (orientation() == Gfx::Orientation::Horizontal) 327 paint_horizontal(painter); 328 else 329 paint_vertical(painter); 330} 331 332void HeaderView::set_section_visible(int section, bool visible) 333{ 334 auto& data = section_data(section); 335 if (data.visibility == visible) 336 return; 337 data.visibility = visible; 338 if (data.visibility_action) { 339 data.visibility_action->set_checked(visible); 340 } 341 m_table_view.header_did_change_section_visibility({}, m_orientation, section, visible); 342 update(); 343} 344 345Menu& HeaderView::ensure_context_menu() 346{ 347 // FIXME: This menu needs to be rebuilt if the model is swapped out, 348 // or if the column count/names change. 349 if (!m_context_menu) { 350 VERIFY(model()); 351 m_context_menu = Menu::construct(); 352 353 if (m_orientation == Gfx::Orientation::Vertical) { 354 dbgln("FIXME: Support context menus for vertical GUI::HeaderView"); 355 return *m_context_menu; 356 } 357 358 int section_count = this->section_count(); 359 for (int section = 0; section < section_count; ++section) { 360 auto& column_data = this->section_data(section); 361 auto name = model()->column_name(section); 362 column_data.visibility_action = Action::create_checkable(name, [this, section](auto& action) { 363 set_section_visible(section, action.is_checked()); 364 }); 365 column_data.visibility_action->set_checked(column_data.visibility); 366 367 m_context_menu->add_action(*column_data.visibility_action); 368 } 369 } 370 return *m_context_menu; 371} 372 373void HeaderView::context_menu_event(ContextMenuEvent& event) 374{ 375 ensure_context_menu().popup(event.screen_position()); 376} 377 378void HeaderView::leave_event(Core::Event& event) 379{ 380 Widget::leave_event(event); 381 set_hovered_section(-1); 382} 383 384Gfx::TextAlignment HeaderView::section_alignment(int section) const 385{ 386 return section_data(section).alignment; 387} 388 389void HeaderView::set_section_alignment(int section, Gfx::TextAlignment alignment) 390{ 391 section_data(section).alignment = alignment; 392} 393 394void HeaderView::set_default_section_size(int section, int size) 395{ 396 auto minimum_column_width = m_table_view.minimum_column_width(section); 397 398 if (orientation() == Gfx::Orientation::Horizontal && size < minimum_column_width) 399 size = minimum_column_width; 400 401 auto& data = section_data(section); 402 if (data.default_size == size) 403 return; 404 data.default_size = size; 405 data.has_initialized_default_size = true; 406} 407 408int HeaderView::default_section_size(int section) const 409{ 410 return section_data(section).default_size; 411} 412 413bool HeaderView::is_default_section_size_initialized(int section) const 414{ 415 return section_data(section).has_initialized_default_size; 416} 417 418bool HeaderView::is_section_visible(int section) const 419{ 420 return section_data(section).visibility; 421} 422 423void HeaderView::set_hovered_section(int section) 424{ 425 if (m_hovered_section == section) 426 return; 427 m_hovered_section = section; 428 update(); 429} 430 431Model* HeaderView::model() 432{ 433 return m_table_view.model(); 434} 435 436Model const* HeaderView::model() const 437{ 438 return m_table_view.model(); 439} 440 441}