GListView.cpp 7.7 KB

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