TableView.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include <AK/StringBuilder.h>
  27. #include <Kernel/KeyCode.h>
  28. #include <LibGUI/Action.h>
  29. #include <LibGUI/Menu.h>
  30. #include <LibGUI/Model.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGUI/ScrollBar.h>
  33. #include <LibGUI/TableView.h>
  34. #include <LibGUI/TextBox.h>
  35. #include <LibGUI/Window.h>
  36. #include <LibGfx/Palette.h>
  37. namespace GUI {
  38. TableView::TableView()
  39. {
  40. set_background_role(ColorRole::Base);
  41. set_foreground_role(ColorRole::BaseText);
  42. }
  43. TableView::~TableView()
  44. {
  45. }
  46. void TableView::paint_event(PaintEvent& event)
  47. {
  48. Color widget_background_color = palette().color(background_role());
  49. Frame::paint_event(event);
  50. Painter painter(*this);
  51. painter.add_clip_rect(frame_inner_rect());
  52. painter.add_clip_rect(event.rect());
  53. painter.fill_rect(event.rect(), widget_background_color);
  54. painter.translate(frame_thickness(), frame_thickness());
  55. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  56. if (!model())
  57. return;
  58. int exposed_width = max(content_size().width(), width());
  59. int y_offset = header_height();
  60. bool dummy;
  61. int first_visible_row = index_at_event_position(frame_inner_rect().top_left(), dummy).row();
  62. int last_visible_row = index_at_event_position(frame_inner_rect().bottom_right(), dummy).row();
  63. if (first_visible_row == -1)
  64. first_visible_row = 0;
  65. if (last_visible_row == -1)
  66. last_visible_row = model()->row_count() - 1;
  67. int painted_item_index = first_visible_row;
  68. for (int row_index = first_visible_row; row_index <= last_visible_row; ++row_index) {
  69. bool is_selected_row = selection().contains_row(row_index);
  70. int y = y_offset + painted_item_index * item_height();
  71. Color background_color;
  72. Color key_column_background_color;
  73. if (is_selected_row) {
  74. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  75. key_column_background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  76. } else {
  77. if (alternating_row_colors() && (painted_item_index % 2)) {
  78. background_color = widget_background_color.darkened(0.8f);
  79. key_column_background_color = widget_background_color.darkened(0.7f);
  80. } else {
  81. background_color = widget_background_color;
  82. key_column_background_color = widget_background_color.darkened(0.9f);
  83. }
  84. }
  85. painter.fill_rect(row_rect(painted_item_index), background_color);
  86. int x_offset = 0;
  87. for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
  88. if (is_column_hidden(column_index))
  89. continue;
  90. auto column_metadata = model()->column_metadata(column_index);
  91. int column_width = this->column_width(column_index);
  92. const Gfx::Font& font = column_metadata.font ? *column_metadata.font : this->font();
  93. bool is_key_column = model()->key_column() == column_index;
  94. Gfx::Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
  95. if (is_key_column) {
  96. auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
  97. painter.fill_rect(cell_rect_for_fill, key_column_background_color);
  98. }
  99. auto cell_index = model()->index(row_index, column_index);
  100. if (auto* delegate = column_data(column_index).cell_painting_delegate.ptr()) {
  101. delegate->paint(painter, cell_rect, palette(), *model(), cell_index);
  102. } else {
  103. auto data = model()->data(cell_index);
  104. if (data.is_bitmap()) {
  105. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  106. } else if (data.is_icon()) {
  107. if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
  108. if (m_hovered_index.is_valid() && cell_index.row() == m_hovered_index.row())
  109. painter.blit_brightened(cell_rect.location(), *bitmap, bitmap->rect());
  110. else
  111. painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
  112. }
  113. } else {
  114. Color text_color;
  115. if (is_selected_row)
  116. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  117. else
  118. text_color = model()->data(cell_index, Model::Role::ForegroundColor).to_color(palette().color(foreground_role()));
  119. painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color, Gfx::TextElision::Right);
  120. }
  121. }
  122. x_offset += column_width + horizontal_padding() * 2;
  123. }
  124. ++painted_item_index;
  125. };
  126. Gfx::Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
  127. painter.fill_rect(unpainted_rect, widget_background_color);
  128. // Untranslate the painter vertically and do the column headers.
  129. painter.translate(0, vertical_scrollbar().value());
  130. if (headers_visible())
  131. paint_headers(painter);
  132. }
  133. }