GListView.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <Kernel/KeyCode.h>
  27. #include <LibDraw/Palette.h>
  28. #include <LibGUI/GListView.h>
  29. #include <LibGUI/GPainter.h>
  30. #include <LibGUI/GScrollBar.h>
  31. GListView::GListView(GWidget* parent)
  32. : GAbstractView(parent)
  33. {
  34. set_background_role(ColorRole::Base);
  35. set_foreground_role(ColorRole::BaseText);
  36. set_frame_shape(FrameShape::Container);
  37. set_frame_shadow(FrameShadow::Sunken);
  38. set_frame_thickness(2);
  39. }
  40. GListView::~GListView()
  41. {
  42. }
  43. void GListView::update_content_size()
  44. {
  45. if (!model())
  46. return set_content_size({});
  47. int content_width = 0;
  48. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  49. auto text = model()->data(model()->index(row, m_model_column), GModel::Role::Display);
  50. content_width = max(content_width, font().width(text.to_string()));
  51. }
  52. content_width = max(content_width, widget_inner_rect().width());
  53. int content_height = item_count() * item_height();
  54. set_content_size({ content_width, content_height });
  55. }
  56. void GListView::resize_event(GResizeEvent& event)
  57. {
  58. update_content_size();
  59. GAbstractView::resize_event(event);
  60. }
  61. void GListView::did_update_model()
  62. {
  63. GAbstractView::did_update_model();
  64. update_content_size();
  65. update();
  66. }
  67. Rect GListView::content_rect(int row) const
  68. {
  69. return { 0, row * item_height(), content_width(), item_height() };
  70. }
  71. Rect GListView::content_rect(const GModelIndex& index) const
  72. {
  73. return content_rect(index.row());
  74. }
  75. GModelIndex GListView::index_at_event_position(const Point& point) const
  76. {
  77. ASSERT(model());
  78. auto adjusted_position = this->adjusted_position(point);
  79. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  80. if (!content_rect(row).contains(adjusted_position))
  81. continue;
  82. return model()->index(row, m_model_column);
  83. }
  84. return {};
  85. }
  86. Point GListView::adjusted_position(const Point& position) const
  87. {
  88. return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  89. }
  90. void GListView::paint_event(GPaintEvent& event)
  91. {
  92. GFrame::paint_event(event);
  93. if (!model())
  94. return;
  95. GPainter painter(*this);
  96. painter.add_clip_rect(frame_inner_rect());
  97. painter.add_clip_rect(event.rect());
  98. painter.translate(frame_thickness(), frame_thickness());
  99. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  100. int exposed_width = max(content_size().width(), width());
  101. int painted_item_index = 0;
  102. for (int row_index = 0; row_index < model()->row_count(); ++row_index) {
  103. bool is_selected_row = selection().contains_row(row_index);
  104. int y = painted_item_index * item_height();
  105. Color background_color;
  106. if (is_selected_row) {
  107. background_color = is_focused() ? palette().selection() : Color::from_rgb(0x606060);
  108. } else {
  109. Color row_fill_color = palette().color(background_role());
  110. if (alternating_row_colors() && (painted_item_index % 2)) {
  111. background_color = row_fill_color.darkened(0.8f);
  112. } else {
  113. background_color = row_fill_color;
  114. }
  115. }
  116. auto column_metadata = model()->column_metadata(m_model_column);
  117. Rect row_rect(0, y, content_width(), item_height());
  118. painter.fill_rect(row_rect, background_color);
  119. auto index = model()->index(row_index, m_model_column);
  120. auto data = model()->data(index);
  121. auto font = font_for_index(index);
  122. if (data.is_bitmap()) {
  123. painter.blit(row_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  124. } else if (data.is_icon()) {
  125. if (auto bitmap = data.as_icon().bitmap_for_size(16))
  126. painter.blit(row_rect.location(), *bitmap, bitmap->rect());
  127. } else {
  128. Color text_color;
  129. if (is_selected_row)
  130. text_color = palette().selection_text();
  131. else
  132. text_color = model()->data(index, GModel::Role::ForegroundColor).to_color(palette().color(foreground_role()));
  133. auto text_rect = row_rect;
  134. text_rect.move_by(horizontal_padding(), 0);
  135. text_rect.set_width(text_rect.width() - horizontal_padding() * 2);
  136. painter.draw_text(text_rect, data.to_string(), font, column_metadata.text_alignment, text_color);
  137. }
  138. ++painted_item_index;
  139. };
  140. Rect unpainted_rect(0, painted_item_index * item_height(), exposed_width, height());
  141. painter.fill_rect(unpainted_rect, palette().color(background_role()));
  142. }
  143. int GListView::item_count() const
  144. {
  145. if (!model())
  146. return 0;
  147. return model()->row_count();
  148. }
  149. void GListView::keydown_event(GKeyEvent& event)
  150. {
  151. if (!model())
  152. return;
  153. auto& model = *this->model();
  154. if (event.key() == KeyCode::Key_Return) {
  155. activate_selected();
  156. return;
  157. }
  158. if (event.key() == KeyCode::Key_Up) {
  159. GModelIndex new_index;
  160. if (!selection().is_empty()) {
  161. auto old_index = selection().first();
  162. new_index = model.index(old_index.row() - 1, old_index.column());
  163. } else {
  164. new_index = model.index(0, 0);
  165. }
  166. if (model.is_valid(new_index)) {
  167. selection().set(new_index);
  168. scroll_into_view(new_index, Orientation::Vertical);
  169. update();
  170. }
  171. return;
  172. }
  173. if (event.key() == KeyCode::Key_Down) {
  174. GModelIndex new_index;
  175. if (!selection().is_empty()) {
  176. auto old_index = selection().first();
  177. new_index = model.index(old_index.row() + 1, old_index.column());
  178. } else {
  179. new_index = model.index(0, 0);
  180. }
  181. if (model.is_valid(new_index)) {
  182. selection().set(new_index);
  183. scroll_into_view(new_index, Orientation::Vertical);
  184. update();
  185. }
  186. return;
  187. }
  188. if (event.key() == KeyCode::Key_PageUp) {
  189. int items_per_page = visible_content_rect().height() / item_height();
  190. auto old_index = selection().first();
  191. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  192. if (model.is_valid(new_index)) {
  193. selection().set(new_index);
  194. scroll_into_view(new_index, Orientation::Vertical);
  195. update();
  196. }
  197. return;
  198. }
  199. if (event.key() == KeyCode::Key_PageDown) {
  200. int items_per_page = visible_content_rect().height() / item_height();
  201. auto old_index = selection().first();
  202. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  203. if (model.is_valid(new_index)) {
  204. selection().set(new_index);
  205. scroll_into_view(new_index, Orientation::Vertical);
  206. update();
  207. }
  208. return;
  209. }
  210. return GWidget::keydown_event(event);
  211. }
  212. void GListView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  213. {
  214. auto rect = content_rect(index.row());
  215. GScrollableWidget::scroll_into_view(rect, orientation);
  216. }
  217. void GListView::doubleclick_event(GMouseEvent& event)
  218. {
  219. if (!model())
  220. return;
  221. if (event.button() == GMouseButton::Left) {
  222. if (!selection().is_empty()) {
  223. if (is_editable())
  224. begin_editing(selection().first());
  225. else
  226. activate_selected();
  227. }
  228. }
  229. }