TableView.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/ModelEditingDelegate.h>
  32. #include <LibGUI/Painter.h>
  33. #include <LibGUI/ScrollBar.h>
  34. #include <LibGUI/TableView.h>
  35. #include <LibGUI/TextBox.h>
  36. #include <LibGUI/Window.h>
  37. #include <LibGfx/Palette.h>
  38. namespace GUI {
  39. TableView::TableView()
  40. {
  41. set_fill_with_background_color(true);
  42. set_background_role(ColorRole::Base);
  43. set_foreground_role(ColorRole::BaseText);
  44. }
  45. TableView::~TableView()
  46. {
  47. }
  48. void TableView::paint_event(PaintEvent& event)
  49. {
  50. Color widget_background_color = palette().color(background_role());
  51. Frame::paint_event(event);
  52. Painter painter(*this);
  53. painter.add_clip_rect(frame_inner_rect());
  54. painter.add_clip_rect(event.rect());
  55. if (fill_with_background_color())
  56. painter.fill_rect(event.rect(), widget_background_color);
  57. painter.translate(frame_thickness(), frame_thickness());
  58. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  59. if (!model())
  60. return;
  61. int exposed_width = max(content_size().width(), width());
  62. int x_offset = row_header().is_visible() ? row_header().width() : 0;
  63. int y_offset = column_header().is_visible() ? column_header().height() : 0;
  64. bool dummy;
  65. int first_visible_row = index_at_event_position(frame_inner_rect().top_left(), dummy).row();
  66. int last_visible_row = index_at_event_position(frame_inner_rect().bottom_right(), dummy).row();
  67. if (first_visible_row == -1)
  68. first_visible_row = 0;
  69. if (last_visible_row == -1)
  70. last_visible_row = model()->row_count() - 1;
  71. int painted_item_index = first_visible_row;
  72. for (int row_index = first_visible_row; row_index <= last_visible_row; ++row_index) {
  73. bool is_selected_row = selection().contains_row(row_index);
  74. int y = y_offset + painted_item_index * row_height();
  75. Color background_color;
  76. Color key_column_background_color;
  77. if (is_selected_row && highlight_selected_rows()) {
  78. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  79. key_column_background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  80. } else {
  81. if (alternating_row_colors() && (painted_item_index % 2)) {
  82. background_color = widget_background_color.darkened(0.8f);
  83. key_column_background_color = widget_background_color.darkened(0.7f);
  84. } else {
  85. background_color = widget_background_color;
  86. key_column_background_color = widget_background_color.darkened(0.9f);
  87. }
  88. }
  89. painter.fill_rect(row_rect(painted_item_index), background_color);
  90. int x = x_offset;
  91. for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
  92. if (!column_header().is_section_visible(column_index))
  93. continue;
  94. int column_width = this->column_width(column_index);
  95. bool is_key_column = m_key_column == column_index;
  96. Gfx::IntRect cell_rect(horizontal_padding() + x, y, column_width, row_height());
  97. auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
  98. if (is_key_column)
  99. painter.fill_rect(cell_rect_for_fill, key_column_background_color);
  100. auto cell_index = model()->index(row_index, column_index);
  101. if (auto* delegate = column_painting_delegate(column_index)) {
  102. delegate->paint(painter, cell_rect, palette(), cell_index);
  103. } else {
  104. auto data = cell_index.data();
  105. if (data.is_bitmap()) {
  106. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  107. } else if (data.is_icon()) {
  108. if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
  109. if (m_hovered_index.is_valid() && cell_index.row() == m_hovered_index.row())
  110. painter.blit_brightened(cell_rect.location(), *bitmap, bitmap->rect());
  111. else
  112. painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
  113. }
  114. } else {
  115. Color text_color;
  116. if (is_selected_row)
  117. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  118. else
  119. text_color = cell_index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
  120. if (!is_selected_row) {
  121. auto cell_background_color = cell_index.data(ModelRole::BackgroundColor);
  122. if (cell_background_color.is_valid())
  123. painter.fill_rect(cell_rect_for_fill, cell_background_color.to_color(background_color));
  124. }
  125. auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
  126. painter.draw_text(cell_rect, data.to_string(), font_for_index(cell_index), text_alignment, text_color, Gfx::TextElision::Right);
  127. }
  128. }
  129. if (m_grid_style == GridStyle::Horizontal || m_grid_style == GridStyle::Both)
  130. painter.draw_line(cell_rect_for_fill.bottom_left(), cell_rect_for_fill.bottom_right(), palette().ruler());
  131. if (m_grid_style == GridStyle::Vertical || m_grid_style == GridStyle::Both)
  132. painter.draw_line(cell_rect_for_fill.top_right(), cell_rect_for_fill.bottom_right(), palette().ruler());
  133. if (m_cursor_style == CursorStyle::Item && cell_index == cursor_index())
  134. painter.draw_rect(cell_rect_for_fill, palette().text_cursor());
  135. x += column_width + horizontal_padding() * 2;
  136. }
  137. ++painted_item_index;
  138. };
  139. Gfx::IntRect unpainted_rect(0, column_header().height() + painted_item_index * row_height(), exposed_width, height());
  140. if (fill_with_background_color())
  141. painter.fill_rect(unpainted_rect, widget_background_color);
  142. }
  143. void TableView::keydown_event(KeyEvent& event)
  144. {
  145. if (!model())
  146. return;
  147. if (event.key() == KeyCode::Key_Return) {
  148. activate(cursor_index());
  149. return;
  150. }
  151. AbstractTableView::keydown_event(event);
  152. if (event.is_accepted())
  153. return;
  154. if (is_editable() && edit_triggers() & EditTrigger::AnyKeyPressed && !event.text().is_empty()) {
  155. begin_editing(cursor_index());
  156. if (m_editing_delegate) {
  157. if (event.key() == KeyCode::Key_Delete || event.key() == KeyCode::Key_Backspace)
  158. m_editing_delegate->set_value(String::empty());
  159. else
  160. m_editing_delegate->set_value(event.text());
  161. }
  162. }
  163. }
  164. void TableView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  165. {
  166. if (!model())
  167. return;
  168. auto& model = *this->model();
  169. switch (movement) {
  170. case CursorMovement::Left:
  171. move_cursor_relative(0, -1, selection_update);
  172. break;
  173. case CursorMovement::Right:
  174. move_cursor_relative(0, 1, selection_update);
  175. break;
  176. case CursorMovement::Up:
  177. move_cursor_relative(-1, 0, selection_update);
  178. break;
  179. case CursorMovement::Down:
  180. move_cursor_relative(1, 0, selection_update);
  181. break;
  182. case CursorMovement::Home: {
  183. auto index = model.index(0, 0);
  184. set_cursor(index, selection_update);
  185. scroll_into_view(index, false, true);
  186. break;
  187. }
  188. case CursorMovement::End: {
  189. auto index = model.index(model.row_count() - 1, 0);
  190. set_cursor(index, selection_update);
  191. scroll_into_view(index, false, true);
  192. break;
  193. }
  194. case CursorMovement::PageUp: {
  195. int items_per_page = visible_content_rect().height() / row_height();
  196. auto old_index = selection().first();
  197. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  198. if (model.is_valid(new_index))
  199. set_cursor(new_index, selection_update);
  200. break;
  201. }
  202. case CursorMovement::PageDown: {
  203. int items_per_page = visible_content_rect().height() / row_height();
  204. auto old_index = selection().first();
  205. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  206. if (model.is_valid(new_index))
  207. set_cursor(new_index, selection_update);
  208. break;
  209. }
  210. }
  211. }
  212. void TableView::set_grid_style(GridStyle style)
  213. {
  214. if (m_grid_style == style)
  215. return;
  216. m_grid_style = style;
  217. update();
  218. }
  219. void TableView::set_cursor_style(CursorStyle style)
  220. {
  221. if (m_cursor_style == style)
  222. return;
  223. m_cursor_style = style;
  224. update();
  225. }
  226. }