TableView.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <LibGUI/Action.h>
  28. #include <LibGUI/HeaderView.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_fill_with_background_color(true);
  41. set_background_role(ColorRole::Base);
  42. set_foreground_role(ColorRole::BaseText);
  43. }
  44. TableView::~TableView()
  45. {
  46. }
  47. void TableView::paint_event(PaintEvent& event)
  48. {
  49. Color widget_background_color = palette().color(background_role());
  50. Frame::paint_event(event);
  51. Painter painter(*this);
  52. painter.add_clip_rect(frame_inner_rect());
  53. painter.add_clip_rect(event.rect());
  54. if (fill_with_background_color())
  55. painter.fill_rect(event.rect(), widget_background_color);
  56. painter.translate(frame_thickness(), frame_thickness());
  57. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  58. if (!model())
  59. return;
  60. int exposed_width = max(content_size().width(), width());
  61. int y_offset = column_header().height();
  62. bool dummy;
  63. int first_visible_row = index_at_event_position(frame_inner_rect().top_left(), dummy).row();
  64. int last_visible_row = index_at_event_position(frame_inner_rect().bottom_right(), dummy).row();
  65. if (first_visible_row == -1)
  66. first_visible_row = 0;
  67. if (last_visible_row == -1)
  68. last_visible_row = model()->row_count() - 1;
  69. int painted_item_index = first_visible_row;
  70. for (int row_index = first_visible_row; row_index <= last_visible_row; ++row_index) {
  71. bool is_selected_row = selection().contains_row(row_index);
  72. int y = y_offset + painted_item_index * item_height();
  73. Color background_color;
  74. Color key_column_background_color;
  75. if (is_selected_row && highlight_selected_rows()) {
  76. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  77. key_column_background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  78. } else {
  79. if (alternating_row_colors() && (painted_item_index % 2)) {
  80. background_color = widget_background_color.darkened(0.8f);
  81. key_column_background_color = widget_background_color.darkened(0.7f);
  82. } else {
  83. background_color = widget_background_color;
  84. key_column_background_color = widget_background_color.darkened(0.9f);
  85. }
  86. }
  87. painter.fill_rect(row_rect(painted_item_index), background_color);
  88. int x_offset = 0;
  89. for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
  90. if (!column_header().is_section_visible(column_index))
  91. continue;
  92. int column_width = this->column_width(column_index);
  93. bool is_key_column = m_key_column == column_index;
  94. Gfx::IntRect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
  95. auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
  96. if (is_key_column)
  97. painter.fill_rect(cell_rect_for_fill, key_column_background_color);
  98. auto cell_index = model()->index(row_index, column_index);
  99. if (auto* delegate = column_painting_delegate(column_index)) {
  100. delegate->paint(painter, cell_rect, palette(), cell_index);
  101. } else {
  102. auto data = cell_index.data();
  103. if (data.is_bitmap()) {
  104. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  105. } else if (data.is_icon()) {
  106. if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
  107. if (m_hovered_index.is_valid() && cell_index.row() == m_hovered_index.row())
  108. painter.blit_brightened(cell_rect.location(), *bitmap, bitmap->rect());
  109. else
  110. painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
  111. }
  112. } else {
  113. Color text_color;
  114. if (is_selected_row)
  115. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  116. else
  117. text_color = cell_index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
  118. if (!is_selected_row) {
  119. auto cell_background_color = cell_index.data(ModelRole::BackgroundColor);
  120. if (cell_background_color.is_valid())
  121. painter.fill_rect(cell_rect_for_fill, cell_background_color.to_color(background_color));
  122. }
  123. auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
  124. painter.draw_text(cell_rect, data.to_string(), font_for_index(cell_index), text_alignment, text_color, Gfx::TextElision::Right);
  125. }
  126. }
  127. x_offset += column_width + horizontal_padding() * 2;
  128. }
  129. ++painted_item_index;
  130. };
  131. Gfx::IntRect unpainted_rect(0, column_header().height() + painted_item_index * item_height(), exposed_width, height());
  132. if (fill_with_background_color())
  133. painter.fill_rect(unpainted_rect, widget_background_color);
  134. }
  135. void TableView::keydown_event(KeyEvent& event)
  136. {
  137. if (!model())
  138. return;
  139. auto& model = *this->model();
  140. if (event.key() == KeyCode::Key_Return) {
  141. activate_or_edit_selected();
  142. return;
  143. }
  144. if (event.key() == KeyCode::Key_Left) {
  145. move_selection(0, -1);
  146. return;
  147. }
  148. if (event.key() == KeyCode::Key_Right) {
  149. move_selection(0, 1);
  150. return;
  151. }
  152. if (event.key() == KeyCode::Key_Up) {
  153. move_selection(-1, 0);
  154. return;
  155. }
  156. if (event.key() == KeyCode::Key_Down) {
  157. move_selection(1, 0);
  158. return;
  159. }
  160. if (event.key() == KeyCode::Key_PageUp) {
  161. int items_per_page = visible_content_rect().height() / item_height();
  162. auto old_index = selection().first();
  163. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  164. if (model.is_valid(new_index)) {
  165. selection().set(new_index);
  166. scroll_into_view(new_index, Orientation::Vertical);
  167. update();
  168. }
  169. return;
  170. }
  171. if (event.key() == KeyCode::Key_PageDown) {
  172. int items_per_page = visible_content_rect().height() / item_height();
  173. auto old_index = selection().first();
  174. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  175. if (model.is_valid(new_index)) {
  176. selection().set(new_index);
  177. scroll_into_view(new_index, Orientation::Vertical);
  178. update();
  179. }
  180. return;
  181. }
  182. return Widget::keydown_event(event);
  183. }
  184. }