GItemView.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <AK/StringBuilder.h>
  27. #include <Kernel/KeyCode.h>
  28. #include <LibDraw/Palette.h>
  29. #include <LibGUI/GDragOperation.h>
  30. #include <LibGUI/GItemView.h>
  31. #include <LibGUI/GModel.h>
  32. #include <LibGUI/GPainter.h>
  33. #include <LibGUI/GScrollBar.h>
  34. GItemView::GItemView(GWidget* parent)
  35. : GAbstractView(parent)
  36. {
  37. set_background_role(ColorRole::Base);
  38. set_foreground_role(ColorRole::BaseText);
  39. set_frame_shape(FrameShape::Container);
  40. set_frame_shadow(FrameShadow::Sunken);
  41. set_frame_thickness(2);
  42. horizontal_scrollbar().set_visible(false);
  43. }
  44. GItemView::~GItemView()
  45. {
  46. }
  47. void GItemView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  48. {
  49. GScrollableWidget::scroll_into_view(item_rect(index.row()), orientation);
  50. }
  51. void GItemView::resize_event(GResizeEvent& event)
  52. {
  53. GAbstractView::resize_event(event);
  54. update_content_size();
  55. }
  56. void GItemView::did_update_model()
  57. {
  58. GAbstractView::did_update_model();
  59. update_content_size();
  60. update();
  61. }
  62. void GItemView::update_content_size()
  63. {
  64. if (!model())
  65. return set_content_size({});
  66. m_visual_column_count = available_size().width() / effective_item_size().width();
  67. if (m_visual_column_count)
  68. m_visual_row_count = ceil_div(model()->row_count(), m_visual_column_count);
  69. else
  70. m_visual_row_count = 0;
  71. int content_width = available_size().width();
  72. int content_height = m_visual_row_count * effective_item_size().height();
  73. set_content_size({ content_width, content_height });
  74. }
  75. Rect GItemView::item_rect(int item_index) const
  76. {
  77. if (!m_visual_row_count || !m_visual_column_count)
  78. return {};
  79. int visual_row_index = item_index / m_visual_column_count;
  80. int visual_column_index = item_index % m_visual_column_count;
  81. return {
  82. visual_column_index * effective_item_size().width(),
  83. visual_row_index * effective_item_size().height(),
  84. effective_item_size().width(),
  85. effective_item_size().height()
  86. };
  87. }
  88. Vector<int> GItemView::items_intersecting_rect(const Rect& rect) const
  89. {
  90. ASSERT(model());
  91. const auto& column_metadata = model()->column_metadata(model_column());
  92. const auto& font = column_metadata.font ? *column_metadata.font : this->font();
  93. Vector<int> item_indexes;
  94. for (int item_index = 0; item_index < item_count(); ++item_index) {
  95. Rect item_rect;
  96. Rect icon_rect;
  97. Rect text_rect;
  98. auto item_text = model()->data(model()->index(item_index, model_column()));
  99. get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
  100. if (icon_rect.intersects(rect) || text_rect.intersects(rect))
  101. item_indexes.append(item_index);
  102. }
  103. return item_indexes;
  104. }
  105. GModelIndex GItemView::index_at_event_position(const Point& position) const
  106. {
  107. ASSERT(model());
  108. // FIXME: Since all items are the same size, just compute the clicked item index
  109. // instead of iterating over everything.
  110. auto adjusted_position = position.translated(0, vertical_scrollbar().value());
  111. const auto& column_metadata = model()->column_metadata(model_column());
  112. const auto& font = column_metadata.font ? *column_metadata.font : this->font();
  113. for (int item_index = 0; item_index < item_count(); ++item_index) {
  114. Rect item_rect;
  115. Rect icon_rect;
  116. Rect text_rect;
  117. auto index = model()->index(item_index, model_column());
  118. auto item_text = model()->data(index);
  119. get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
  120. if (icon_rect.contains(adjusted_position) || text_rect.contains(adjusted_position))
  121. return index;
  122. }
  123. return {};
  124. }
  125. void GItemView::mousedown_event(GMouseEvent& event)
  126. {
  127. if (!model())
  128. return GAbstractView::mousedown_event(event);
  129. if (event.button() != GMouseButton::Left)
  130. return GAbstractView::mousedown_event(event);
  131. auto index = index_at_event_position(event.position());
  132. if (index.is_valid()) {
  133. // We might start dragging this item, but not rubber-banding.
  134. return GAbstractView::mousedown_event(event);
  135. }
  136. ASSERT(m_rubber_band_remembered_selection.is_empty());
  137. if (event.modifiers() & Mod_Ctrl) {
  138. selection().for_each_index([&](auto& index) {
  139. m_rubber_band_remembered_selection.append(index);
  140. });
  141. } else {
  142. selection().clear();
  143. }
  144. m_might_drag = false;
  145. m_rubber_banding = true;
  146. m_rubber_band_origin = event.position();
  147. m_rubber_band_current = event.position();
  148. }
  149. void GItemView::mouseup_event(GMouseEvent& event)
  150. {
  151. if (m_rubber_banding && event.button() == GMouseButton::Left) {
  152. m_rubber_banding = false;
  153. m_rubber_band_remembered_selection.clear();
  154. update();
  155. }
  156. GAbstractView::mouseup_event(event);
  157. }
  158. void GItemView::mousemove_event(GMouseEvent& event)
  159. {
  160. if (!model())
  161. return GAbstractView::mousemove_event(event);
  162. if (m_rubber_banding) {
  163. if (m_rubber_band_current != event.position()) {
  164. m_rubber_band_current = event.position();
  165. auto rubber_band_rect = Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  166. selection().clear();
  167. for (auto item_index : items_intersecting_rect(rubber_band_rect)) {
  168. selection().add(model()->index(item_index, model_column()));
  169. }
  170. if (event.modifiers() & Mod_Ctrl) {
  171. for (auto stored_item : m_rubber_band_remembered_selection) {
  172. selection().add(stored_item);
  173. }
  174. }
  175. update();
  176. return;
  177. }
  178. }
  179. GAbstractView::mousemove_event(event);
  180. }
  181. void GItemView::get_item_rects(int item_index, const Font& font, const GVariant& item_text, Rect& item_rect, Rect& icon_rect, Rect& text_rect) const
  182. {
  183. item_rect = this->item_rect(item_index);
  184. icon_rect = { 0, 0, 32, 32 };
  185. icon_rect.center_within(item_rect);
  186. icon_rect.move_by(0, -font.glyph_height() - 6);
  187. text_rect = { 0, icon_rect.bottom() + 6 + 1, font.width(item_text.to_string()), font.glyph_height() };
  188. text_rect.center_horizontally_within(item_rect);
  189. text_rect.inflate(6, 4);
  190. text_rect.intersect(item_rect);
  191. }
  192. void GItemView::second_paint_event(GPaintEvent& event)
  193. {
  194. if (!m_rubber_banding)
  195. return;
  196. GPainter painter(*this);
  197. painter.add_clip_rect(event.rect());
  198. auto rubber_band_rect = Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  199. painter.fill_rect(rubber_band_rect, parent_widget()->palette().rubber_band_fill());
  200. painter.draw_rect(rubber_band_rect, parent_widget()->palette().rubber_band_border());
  201. }
  202. void GItemView::paint_event(GPaintEvent& event)
  203. {
  204. Color widget_background_color = palette().color(background_role());
  205. GFrame::paint_event(event);
  206. GPainter painter(*this);
  207. painter.add_clip_rect(widget_inner_rect());
  208. painter.add_clip_rect(event.rect());
  209. painter.fill_rect(event.rect(), widget_background_color);
  210. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  211. auto column_metadata = model()->column_metadata(m_model_column);
  212. const Font& font = column_metadata.font ? *column_metadata.font : this->font();
  213. for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
  214. auto model_index = model()->index(item_index, m_model_column);
  215. bool is_selected_item = selection().contains(model_index);
  216. Color background_color;
  217. if (is_selected_item) {
  218. background_color = is_focused() ? palette().selection() : Color::from_rgb(0x606060);
  219. } else {
  220. background_color = widget_background_color;
  221. }
  222. auto icon = model()->data(model_index, GModel::Role::Icon);
  223. auto item_text = model()->data(model_index, GModel::Role::Display);
  224. Rect item_rect;
  225. Rect icon_rect;
  226. Rect text_rect;
  227. get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
  228. if (icon.is_icon()) {
  229. if (auto bitmap = icon.as_icon().bitmap_for_size(icon_rect.width()))
  230. painter.draw_scaled_bitmap(icon_rect, *bitmap, bitmap->rect());
  231. }
  232. Color text_color;
  233. if (is_selected_item)
  234. text_color = palette().selection_text();
  235. else
  236. text_color = model()->data(model_index, GModel::Role::ForegroundColor).to_color(palette().color(foreground_role()));
  237. painter.fill_rect(text_rect, background_color);
  238. painter.draw_text(text_rect, item_text.to_string(), font, TextAlignment::Center, text_color, TextElision::Right);
  239. };
  240. }
  241. int GItemView::item_count() const
  242. {
  243. if (!model())
  244. return 0;
  245. return model()->row_count();
  246. }
  247. void GItemView::keydown_event(GKeyEvent& event)
  248. {
  249. if (!model())
  250. return;
  251. if (!m_visual_row_count || !m_visual_column_count)
  252. return;
  253. auto& model = *this->model();
  254. if (event.key() == KeyCode::Key_Return) {
  255. activate_selected();
  256. return;
  257. }
  258. if (event.key() == KeyCode::Key_Home) {
  259. auto new_index = model.index(0, 0);
  260. if (model.is_valid(new_index)) {
  261. selection().set(new_index);
  262. scroll_into_view(new_index, Orientation::Vertical);
  263. update();
  264. }
  265. return;
  266. }
  267. if (event.key() == KeyCode::Key_End) {
  268. auto new_index = model.index(model.row_count() - 1, 0);
  269. if (model.is_valid(new_index)) {
  270. selection().set(new_index);
  271. scroll_into_view(new_index, Orientation::Vertical);
  272. update();
  273. }
  274. return;
  275. }
  276. if (event.key() == KeyCode::Key_Up) {
  277. GModelIndex new_index;
  278. if (!selection().is_empty()) {
  279. auto old_index = selection().first();
  280. new_index = model.index(old_index.row() - m_visual_column_count, old_index.column());
  281. } else {
  282. new_index = model.index(0, 0);
  283. }
  284. if (model.is_valid(new_index)) {
  285. selection().set(new_index);
  286. scroll_into_view(new_index, Orientation::Vertical);
  287. update();
  288. }
  289. return;
  290. }
  291. if (event.key() == KeyCode::Key_Down) {
  292. GModelIndex new_index;
  293. if (!selection().is_empty()) {
  294. auto old_index = selection().first();
  295. new_index = model.index(old_index.row() + m_visual_column_count, old_index.column());
  296. } else {
  297. new_index = model.index(0, 0);
  298. }
  299. if (model.is_valid(new_index)) {
  300. selection().set(new_index);
  301. scroll_into_view(new_index, Orientation::Vertical);
  302. update();
  303. }
  304. return;
  305. }
  306. if (event.key() == KeyCode::Key_Left) {
  307. GModelIndex new_index;
  308. if (!selection().is_empty()) {
  309. auto old_index = selection().first();
  310. new_index = model.index(old_index.row() - 1, old_index.column());
  311. } else {
  312. new_index = model.index(0, 0);
  313. }
  314. if (model.is_valid(new_index)) {
  315. selection().set(new_index);
  316. scroll_into_view(new_index, Orientation::Vertical);
  317. update();
  318. }
  319. return;
  320. }
  321. if (event.key() == KeyCode::Key_Right) {
  322. GModelIndex new_index;
  323. if (!selection().is_empty()) {
  324. auto old_index = selection().first();
  325. new_index = model.index(old_index.row() + 1, old_index.column());
  326. } else {
  327. new_index = model.index(0, 0);
  328. }
  329. if (model.is_valid(new_index)) {
  330. selection().set(new_index);
  331. scroll_into_view(new_index, Orientation::Vertical);
  332. update();
  333. }
  334. return;
  335. }
  336. if (event.key() == KeyCode::Key_PageUp) {
  337. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  338. auto old_index = selection().first();
  339. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  340. if (model.is_valid(new_index)) {
  341. selection().set(new_index);
  342. scroll_into_view(new_index, Orientation::Vertical);
  343. update();
  344. }
  345. return;
  346. }
  347. if (event.key() == KeyCode::Key_PageDown) {
  348. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  349. auto old_index = selection().first();
  350. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  351. if (model.is_valid(new_index)) {
  352. selection().set(new_index);
  353. scroll_into_view(new_index, Orientation::Vertical);
  354. update();
  355. }
  356. return;
  357. }
  358. return GWidget::keydown_event(event);
  359. }