GListView.cpp 7.6 KB

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