GListView.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include <Kernel/KeyCode.h>
  2. #include <LibGUI/GListView.h>
  3. #include <LibGUI/GPainter.h>
  4. #include <LibGUI/GScrollBar.h>
  5. GListView::GListView(GWidget* parent)
  6. : GAbstractView(parent)
  7. {
  8. set_frame_shape(FrameShape::Container);
  9. set_frame_shadow(FrameShadow::Sunken);
  10. set_frame_thickness(2);
  11. }
  12. GListView::~GListView()
  13. {
  14. }
  15. void GListView::update_content_size()
  16. {
  17. if (!model())
  18. return set_content_size({});
  19. int content_width = 0;
  20. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  21. auto text = model()->data(model()->index(row, m_model_column), GModel::Role::Display);
  22. content_width = max(content_width, font().width(text.to_string()));
  23. }
  24. content_width = max(content_width, widget_inner_rect().width());
  25. int content_height = item_count() * item_height();
  26. set_content_size({ content_width, content_height });
  27. }
  28. void GListView::resize_event(GResizeEvent& event)
  29. {
  30. update_content_size();
  31. GAbstractView::resize_event(event);
  32. }
  33. void GListView::did_update_model()
  34. {
  35. GAbstractView::did_update_model();
  36. update_content_size();
  37. update();
  38. }
  39. Rect GListView::content_rect(int row) const
  40. {
  41. return { 0, row * item_height(), content_width(), item_height() };
  42. }
  43. Rect GListView::content_rect(const GModelIndex& index) const
  44. {
  45. return content_rect(index.row());
  46. }
  47. Point GListView::adjusted_position(const Point& position)
  48. {
  49. return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  50. }
  51. void GListView::mousedown_event(GMouseEvent& event)
  52. {
  53. if (!model())
  54. return;
  55. if (event.button() != GMouseButton::Left)
  56. return;
  57. auto adjusted_position = this->adjusted_position(event.position());
  58. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  59. if (!content_rect(row).contains(adjusted_position))
  60. continue;
  61. model()->set_selected_index(model()->index(row, m_model_column));
  62. update();
  63. return;
  64. }
  65. model()->set_selected_index({});
  66. update();
  67. }
  68. void GListView::paint_event(GPaintEvent& event)
  69. {
  70. GFrame::paint_event(event);
  71. if (!model())
  72. return;
  73. GPainter painter(*this);
  74. painter.add_clip_rect(frame_inner_rect());
  75. painter.add_clip_rect(event.rect());
  76. painter.translate(frame_thickness(), frame_thickness());
  77. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  78. int exposed_width = max(content_size().width(), width());
  79. int painted_item_index = 0;
  80. for (int row_index = 0; row_index < model()->row_count(); ++row_index) {
  81. bool is_selected_row = row_index == model()->selected_index().row();
  82. int y = painted_item_index * item_height();
  83. Color background_color;
  84. if (is_selected_row) {
  85. background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  86. } else {
  87. if (alternating_row_colors() && (painted_item_index % 2))
  88. background_color = Color(210, 210, 210);
  89. else
  90. background_color = Color::White;
  91. }
  92. auto column_metadata = model()->column_metadata(m_model_column);
  93. const Font& font = column_metadata.font ? *column_metadata.font : this->font();
  94. Rect row_rect(0, y, content_width(), item_height());
  95. painter.fill_rect(row_rect, background_color);
  96. auto index = model()->index(row_index, m_model_column);
  97. auto data = model()->data(index);
  98. if (data.is_bitmap()) {
  99. painter.blit(row_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  100. } else if (data.is_icon()) {
  101. if (auto bitmap = data.as_icon().bitmap_for_size(16))
  102. painter.blit(row_rect.location(), *bitmap, bitmap->rect());
  103. } else {
  104. Color text_color;
  105. if (is_selected_row)
  106. text_color = Color::White;
  107. else
  108. text_color = model()->data(index, GModel::Role::ForegroundColor).to_color(Color::Black);
  109. auto text_rect = row_rect;
  110. text_rect.move_by(horizontal_padding(), 0);
  111. text_rect.set_width(text_rect.width() - horizontal_padding() * 2);
  112. painter.draw_text(text_rect, data.to_string(), font, column_metadata.text_alignment, text_color);
  113. }
  114. ++painted_item_index;
  115. };
  116. Rect unpainted_rect(0, painted_item_index * item_height(), exposed_width, height());
  117. painter.fill_rect(unpainted_rect, Color::White);
  118. }
  119. int GListView::item_count() const
  120. {
  121. if (!model())
  122. return 0;
  123. return model()->row_count();
  124. }
  125. void GListView::keydown_event(GKeyEvent& event)
  126. {
  127. if (!model())
  128. return;
  129. auto& model = *this->model();
  130. if (event.key() == KeyCode::Key_Return) {
  131. activate(model.selected_index());
  132. return;
  133. }
  134. if (event.key() == KeyCode::Key_Up) {
  135. GModelIndex new_index;
  136. if (model.selected_index().is_valid())
  137. new_index = model.index(model.selected_index().row() - 1, model.selected_index().column());
  138. else
  139. new_index = model.index(0, 0);
  140. if (model.is_valid(new_index)) {
  141. model.set_selected_index(new_index);
  142. scroll_into_view(new_index, Orientation::Vertical);
  143. update();
  144. }
  145. return;
  146. }
  147. if (event.key() == KeyCode::Key_Down) {
  148. GModelIndex new_index;
  149. if (model.selected_index().is_valid())
  150. new_index = model.index(model.selected_index().row() + 1, model.selected_index().column());
  151. else
  152. new_index = model.index(0, 0);
  153. if (model.is_valid(new_index)) {
  154. model.set_selected_index(new_index);
  155. scroll_into_view(new_index, Orientation::Vertical);
  156. update();
  157. }
  158. return;
  159. }
  160. if (event.key() == KeyCode::Key_PageUp) {
  161. int items_per_page = visible_content_rect().height() / item_height();
  162. auto new_index = model.index(max(0, model.selected_index().row() - items_per_page), model.selected_index().column());
  163. if (model.is_valid(new_index)) {
  164. model.set_selected_index(new_index);
  165. scroll_into_view(new_index, Orientation::Vertical);
  166. update();
  167. }
  168. return;
  169. }
  170. if (event.key() == KeyCode::Key_PageDown) {
  171. int items_per_page = visible_content_rect().height() / item_height();
  172. auto new_index = model.index(min(model.row_count() - 1, model.selected_index().row() + items_per_page), model.selected_index().column());
  173. if (model.is_valid(new_index)) {
  174. model.set_selected_index(new_index);
  175. scroll_into_view(new_index, Orientation::Vertical);
  176. update();
  177. }
  178. return;
  179. }
  180. return GWidget::keydown_event(event);
  181. }
  182. void GListView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  183. {
  184. auto rect = content_rect(index.row());
  185. GScrollableWidget::scroll_into_view(rect, orientation);
  186. }
  187. void GListView::doubleclick_event(GMouseEvent& event)
  188. {
  189. if (!model())
  190. return;
  191. auto& model = *this->model();
  192. if (event.button() == GMouseButton::Left) {
  193. if (model.selected_index().is_valid()) {
  194. if (is_editable())
  195. begin_editing(model.selected_index());
  196. else
  197. activate(model.selected_index());
  198. }
  199. }
  200. }