GListView.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. auto index = model()->index(row, m_model_column);
  62. if (event.modifiers() & Mod_Ctrl)
  63. selection().toggle(index);
  64. else
  65. selection().set(index);
  66. return;
  67. }
  68. selection().clear();
  69. }
  70. void GListView::paint_event(GPaintEvent& event)
  71. {
  72. GFrame::paint_event(event);
  73. if (!model())
  74. return;
  75. GPainter painter(*this);
  76. painter.add_clip_rect(frame_inner_rect());
  77. painter.add_clip_rect(event.rect());
  78. painter.translate(frame_thickness(), frame_thickness());
  79. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  80. int exposed_width = max(content_size().width(), width());
  81. int painted_item_index = 0;
  82. for (int row_index = 0; row_index < model()->row_count(); ++row_index) {
  83. bool is_selected_row = selection().contains_row(row_index);
  84. int y = painted_item_index * item_height();
  85. Color background_color;
  86. if (is_selected_row) {
  87. background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  88. } else {
  89. if (alternating_row_colors() && (painted_item_index % 2))
  90. background_color = Color(210, 210, 210);
  91. else
  92. background_color = SystemColor::Base;
  93. }
  94. auto column_metadata = model()->column_metadata(m_model_column);
  95. Rect row_rect(0, y, content_width(), item_height());
  96. painter.fill_rect(row_rect, background_color);
  97. auto index = model()->index(row_index, m_model_column);
  98. auto data = model()->data(index);
  99. auto font = font_for_index(index);
  100. if (data.is_bitmap()) {
  101. painter.blit(row_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  102. } else if (data.is_icon()) {
  103. if (auto bitmap = data.as_icon().bitmap_for_size(16))
  104. painter.blit(row_rect.location(), *bitmap, bitmap->rect());
  105. } else {
  106. Color text_color;
  107. if (is_selected_row)
  108. text_color = Color::White;
  109. else
  110. text_color = model()->data(index, GModel::Role::ForegroundColor).to_color(SystemColor::Text);
  111. auto text_rect = row_rect;
  112. text_rect.move_by(horizontal_padding(), 0);
  113. text_rect.set_width(text_rect.width() - horizontal_padding() * 2);
  114. painter.draw_text(text_rect, data.to_string(), font, column_metadata.text_alignment, text_color);
  115. }
  116. ++painted_item_index;
  117. };
  118. Rect unpainted_rect(0, painted_item_index * item_height(), exposed_width, height());
  119. painter.fill_rect(unpainted_rect, SystemColor::Base);
  120. }
  121. int GListView::item_count() const
  122. {
  123. if (!model())
  124. return 0;
  125. return model()->row_count();
  126. }
  127. void GListView::keydown_event(GKeyEvent& event)
  128. {
  129. if (!model())
  130. return;
  131. auto& model = *this->model();
  132. if (event.key() == KeyCode::Key_Return) {
  133. selection().for_each_index([this](auto& index) {
  134. activate(index);
  135. });
  136. return;
  137. }
  138. if (event.key() == KeyCode::Key_Up) {
  139. GModelIndex new_index;
  140. if (!selection().is_empty()) {
  141. auto old_index = selection().first();
  142. new_index = model.index(old_index.row() - 1, old_index.column());
  143. } else {
  144. new_index = model.index(0, 0);
  145. }
  146. if (model.is_valid(new_index)) {
  147. selection().set(new_index);
  148. scroll_into_view(new_index, Orientation::Vertical);
  149. update();
  150. }
  151. return;
  152. }
  153. if (event.key() == KeyCode::Key_Down) {
  154. GModelIndex new_index;
  155. if (!selection().is_empty()) {
  156. auto old_index = selection().first();
  157. new_index = model.index(old_index.row() + 1, old_index.column());
  158. } else {
  159. new_index = model.index(0, 0);
  160. }
  161. if (model.is_valid(new_index)) {
  162. selection().set(new_index);
  163. scroll_into_view(new_index, Orientation::Vertical);
  164. update();
  165. }
  166. return;
  167. }
  168. if (event.key() == KeyCode::Key_PageUp) {
  169. int items_per_page = visible_content_rect().height() / item_height();
  170. auto old_index = selection().first();
  171. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  172. if (model.is_valid(new_index)) {
  173. selection().set(new_index);
  174. scroll_into_view(new_index, Orientation::Vertical);
  175. update();
  176. }
  177. return;
  178. }
  179. if (event.key() == KeyCode::Key_PageDown) {
  180. int items_per_page = visible_content_rect().height() / item_height();
  181. auto old_index = selection().first();
  182. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  183. if (model.is_valid(new_index)) {
  184. selection().set(new_index);
  185. scroll_into_view(new_index, Orientation::Vertical);
  186. update();
  187. }
  188. return;
  189. }
  190. return GWidget::keydown_event(event);
  191. }
  192. void GListView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  193. {
  194. auto rect = content_rect(index.row());
  195. GScrollableWidget::scroll_into_view(rect, orientation);
  196. }
  197. void GListView::doubleclick_event(GMouseEvent& event)
  198. {
  199. if (!model())
  200. return;
  201. if (event.button() == GMouseButton::Left) {
  202. if (!selection().is_empty()) {
  203. if (is_editable()) {
  204. begin_editing(selection().first());
  205. } else {
  206. selection().for_each_index([this](auto& index) {
  207. activate(index);
  208. });
  209. }
  210. }
  211. }
  212. }