ItemView.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 <LibGUI/DragOperation.h>
  29. #include <LibGUI/ItemView.h>
  30. #include <LibGUI/Model.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGUI/ScrollBar.h>
  33. #include <LibGfx/Palette.h>
  34. //#define DRAGDROP_DEBUG
  35. namespace GUI {
  36. ItemView::ItemView()
  37. {
  38. set_fill_with_background_color(true);
  39. set_background_role(ColorRole::Base);
  40. set_foreground_role(ColorRole::BaseText);
  41. horizontal_scrollbar().set_visible(false);
  42. }
  43. ItemView::~ItemView()
  44. {
  45. }
  46. void ItemView::select_all()
  47. {
  48. selection().clear();
  49. for (int item_index = 0; item_index < item_count(); ++item_index) {
  50. auto index = model()->index(item_index, model_column());
  51. selection().add(index);
  52. }
  53. }
  54. void ItemView::scroll_into_view(const ModelIndex& index, Orientation orientation)
  55. {
  56. ScrollableWidget::scroll_into_view(item_rect(index.row()), orientation);
  57. }
  58. void ItemView::resize_event(ResizeEvent& event)
  59. {
  60. AbstractView::resize_event(event);
  61. update_content_size();
  62. }
  63. void ItemView::did_update_model(unsigned flags)
  64. {
  65. AbstractView::did_update_model(flags);
  66. update_content_size();
  67. update();
  68. }
  69. void ItemView::update_content_size()
  70. {
  71. if (!model())
  72. return set_content_size({});
  73. m_visual_column_count = available_size().width() / effective_item_size().width();
  74. if (m_visual_column_count)
  75. m_visual_row_count = ceil_div(model()->row_count(), m_visual_column_count);
  76. else
  77. m_visual_row_count = 0;
  78. int content_width = available_size().width();
  79. int content_height = m_visual_row_count * effective_item_size().height();
  80. set_content_size({ content_width, content_height });
  81. }
  82. Gfx::Rect ItemView::item_rect(int item_index) const
  83. {
  84. if (!m_visual_row_count || !m_visual_column_count)
  85. return {};
  86. int visual_row_index = item_index / m_visual_column_count;
  87. int visual_column_index = item_index % m_visual_column_count;
  88. return {
  89. visual_column_index * effective_item_size().width(),
  90. visual_row_index * effective_item_size().height(),
  91. effective_item_size().width(),
  92. effective_item_size().height()
  93. };
  94. }
  95. Vector<int> ItemView::items_intersecting_rect(const Gfx::Rect& rect) const
  96. {
  97. ASSERT(model());
  98. const auto& column_metadata = model()->column_metadata(model_column());
  99. const auto& font = column_metadata.font ? *column_metadata.font : this->font();
  100. Vector<int> item_indexes;
  101. for (int item_index = 0; item_index < item_count(); ++item_index) {
  102. Gfx::Rect item_rect;
  103. Gfx::Rect icon_rect;
  104. Gfx::Rect text_rect;
  105. auto item_text = model()->data(model()->index(item_index, model_column()));
  106. get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
  107. if (icon_rect.intersects(rect) || text_rect.intersects(rect))
  108. item_indexes.append(item_index);
  109. }
  110. return item_indexes;
  111. }
  112. ModelIndex ItemView::index_at_event_position(const Gfx::Point& position) const
  113. {
  114. ASSERT(model());
  115. // FIXME: Since all items are the same size, just compute the clicked item index
  116. // instead of iterating over everything.
  117. auto adjusted_position = this->adjusted_position(position);
  118. const auto& column_metadata = model()->column_metadata(model_column());
  119. const auto& font = column_metadata.font ? *column_metadata.font : this->font();
  120. for (int item_index = 0; item_index < item_count(); ++item_index) {
  121. Gfx::Rect item_rect;
  122. Gfx::Rect icon_rect;
  123. Gfx::Rect text_rect;
  124. auto index = model()->index(item_index, model_column());
  125. auto item_text = model()->data(index);
  126. get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
  127. if (icon_rect.contains(adjusted_position) || text_rect.contains(adjusted_position))
  128. return index;
  129. }
  130. return {};
  131. }
  132. Gfx::Point ItemView::adjusted_position(const Gfx::Point& position) const
  133. {
  134. return position.translated(0, vertical_scrollbar().value());
  135. }
  136. void ItemView::mousedown_event(MouseEvent& event)
  137. {
  138. if (!model())
  139. return AbstractView::mousedown_event(event);
  140. if (event.button() != MouseButton::Left)
  141. return AbstractView::mousedown_event(event);
  142. auto index = index_at_event_position(event.position());
  143. if (index.is_valid()) {
  144. // We might start dragging this item, but not rubber-banding.
  145. return AbstractView::mousedown_event(event);
  146. }
  147. ASSERT(m_rubber_band_remembered_selection.is_empty());
  148. if (event.modifiers() & Mod_Ctrl) {
  149. selection().for_each_index([&](auto& index) {
  150. m_rubber_band_remembered_selection.append(index);
  151. });
  152. } else {
  153. selection().clear();
  154. }
  155. auto adjusted_position = this->adjusted_position(event.position());
  156. m_might_drag = false;
  157. m_rubber_banding = true;
  158. m_rubber_band_origin = adjusted_position;
  159. m_rubber_band_current = adjusted_position;
  160. }
  161. void ItemView::mouseup_event(MouseEvent& event)
  162. {
  163. if (m_rubber_banding && event.button() == MouseButton::Left) {
  164. m_rubber_banding = false;
  165. m_rubber_band_remembered_selection.clear();
  166. update();
  167. }
  168. AbstractView::mouseup_event(event);
  169. }
  170. void ItemView::drag_move_event(DragEvent& event)
  171. {
  172. auto index = index_at_event_position(event.position());
  173. ModelIndex new_drop_candidate_index;
  174. if (index.is_valid()) {
  175. bool acceptable = model()->accepts_drag(index, event.data_type());
  176. #ifdef DRAGDROP_DEBUG
  177. dbg() << "Drag of type '" << event.data_type() << "' moving over " << index << ", acceptable: " << acceptable;
  178. #endif
  179. if (acceptable)
  180. new_drop_candidate_index = index;
  181. }
  182. if (m_drop_candidate_index != new_drop_candidate_index) {
  183. m_drop_candidate_index = new_drop_candidate_index;
  184. update();
  185. }
  186. event.accept();
  187. }
  188. void ItemView::mousemove_event(MouseEvent& event)
  189. {
  190. if (!model())
  191. return AbstractView::mousemove_event(event);
  192. if (m_rubber_banding) {
  193. auto adjusted_position = this->adjusted_position(event.position());
  194. if (m_rubber_band_current != adjusted_position) {
  195. m_rubber_band_current = adjusted_position;
  196. auto rubber_band_rect = Gfx::Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  197. selection().clear();
  198. for (auto item_index : items_intersecting_rect(rubber_band_rect)) {
  199. selection().add(model()->index(item_index, model_column()));
  200. }
  201. if (event.modifiers() & Mod_Ctrl) {
  202. for (auto stored_item : m_rubber_band_remembered_selection) {
  203. selection().add(stored_item);
  204. }
  205. }
  206. update();
  207. return;
  208. }
  209. }
  210. AbstractView::mousemove_event(event);
  211. }
  212. void ItemView::get_item_rects(int item_index, const Gfx::Font& font, const Variant& item_text, Gfx::Rect& item_rect, Gfx::Rect& icon_rect, Gfx::Rect& text_rect) const
  213. {
  214. item_rect = this->item_rect(item_index);
  215. icon_rect = { 0, 0, 32, 32 };
  216. icon_rect.center_within(item_rect);
  217. icon_rect.move_by(0, -font.glyph_height() - 6);
  218. text_rect = { 0, icon_rect.bottom() + 6 + 1, font.width(item_text.to_string()), font.glyph_height() };
  219. text_rect.center_horizontally_within(item_rect);
  220. text_rect.inflate(6, 4);
  221. text_rect.intersect(item_rect);
  222. }
  223. void ItemView::second_paint_event(PaintEvent& event)
  224. {
  225. if (!m_rubber_banding)
  226. return;
  227. Painter painter(*this);
  228. painter.add_clip_rect(event.rect());
  229. painter.translate(frame_thickness(), frame_thickness());
  230. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  231. auto rubber_band_rect = Gfx::Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  232. painter.fill_rect(rubber_band_rect, palette().rubber_band_fill());
  233. painter.draw_rect(rubber_band_rect, palette().rubber_band_border());
  234. }
  235. void ItemView::paint_event(PaintEvent& event)
  236. {
  237. Color widget_background_color = palette().color(background_role());
  238. Frame::paint_event(event);
  239. Painter painter(*this);
  240. painter.add_clip_rect(widget_inner_rect());
  241. painter.add_clip_rect(event.rect());
  242. if (fill_with_background_color())
  243. painter.fill_rect(event.rect(), widget_background_color);
  244. painter.translate(frame_thickness(), frame_thickness());
  245. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  246. auto column_metadata = model()->column_metadata(m_model_column);
  247. const Gfx::Font& font = column_metadata.font ? *column_metadata.font : this->font();
  248. for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
  249. auto model_index = model()->index(item_index, m_model_column);
  250. bool is_selected_item = selection().contains(model_index);
  251. Color background_color;
  252. if (is_selected_item) {
  253. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  254. } else {
  255. background_color = widget_background_color;
  256. }
  257. auto icon = model()->data(model_index, Model::Role::Icon);
  258. auto item_text = model()->data(model_index, Model::Role::Display);
  259. Gfx::Rect item_rect;
  260. Gfx::Rect icon_rect;
  261. Gfx::Rect text_rect;
  262. get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
  263. if (icon.is_icon()) {
  264. if (auto bitmap = icon.as_icon().bitmap_for_size(icon_rect.width())) {
  265. Gfx::Rect destination = bitmap->rect();
  266. destination.center_within(icon_rect);
  267. if (m_hovered_index.is_valid() && m_hovered_index == model_index) {
  268. painter.blit_brightened(destination.location(), *bitmap, bitmap->rect());
  269. } else {
  270. painter.blit(destination.location(), *bitmap, bitmap->rect());
  271. }
  272. }
  273. }
  274. Color text_color;
  275. if (is_selected_item)
  276. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  277. else
  278. text_color = model()->data(model_index, Model::Role::ForegroundColor).to_color(palette().color(foreground_role()));
  279. painter.fill_rect(text_rect, background_color);
  280. painter.draw_text(text_rect, item_text.to_string(), font, Gfx::TextAlignment::Center, text_color, Gfx::TextElision::Right);
  281. if (model_index == m_drop_candidate_index) {
  282. // FIXME: This visualization is not great, as it's also possible to drop things on the text label..
  283. painter.draw_rect(icon_rect.inflated(8, 8), palette().selection(), true);
  284. }
  285. };
  286. }
  287. int ItemView::item_count() const
  288. {
  289. if (!model())
  290. return 0;
  291. return model()->row_count();
  292. }
  293. void ItemView::keydown_event(KeyEvent& event)
  294. {
  295. if (!model())
  296. return;
  297. if (!m_visual_row_count || !m_visual_column_count)
  298. return;
  299. auto& model = *this->model();
  300. if (event.key() == KeyCode::Key_Return) {
  301. activate_selected();
  302. return;
  303. }
  304. if (event.key() == KeyCode::Key_Home) {
  305. auto new_index = model.index(0, 0);
  306. if (model.is_valid(new_index)) {
  307. selection().set(new_index);
  308. scroll_into_view(new_index, Orientation::Vertical);
  309. update();
  310. }
  311. return;
  312. }
  313. if (event.key() == KeyCode::Key_End) {
  314. auto new_index = model.index(model.row_count() - 1, 0);
  315. if (model.is_valid(new_index)) {
  316. selection().set(new_index);
  317. scroll_into_view(new_index, Orientation::Vertical);
  318. update();
  319. }
  320. return;
  321. }
  322. if (event.key() == KeyCode::Key_Up) {
  323. ModelIndex new_index;
  324. if (!selection().is_empty()) {
  325. auto old_index = selection().first();
  326. new_index = model.index(old_index.row() - m_visual_column_count, old_index.column());
  327. } else {
  328. new_index = model.index(0, 0);
  329. }
  330. if (model.is_valid(new_index)) {
  331. selection().set(new_index);
  332. scroll_into_view(new_index, Orientation::Vertical);
  333. update();
  334. }
  335. return;
  336. }
  337. if (event.key() == KeyCode::Key_Down) {
  338. ModelIndex new_index;
  339. if (!selection().is_empty()) {
  340. auto old_index = selection().first();
  341. new_index = model.index(old_index.row() + m_visual_column_count, old_index.column());
  342. } else {
  343. new_index = model.index(0, 0);
  344. }
  345. if (model.is_valid(new_index)) {
  346. selection().set(new_index);
  347. scroll_into_view(new_index, Orientation::Vertical);
  348. update();
  349. }
  350. return;
  351. }
  352. if (event.key() == KeyCode::Key_Left) {
  353. ModelIndex new_index;
  354. if (!selection().is_empty()) {
  355. auto old_index = selection().first();
  356. new_index = model.index(old_index.row() - 1, old_index.column());
  357. } else {
  358. new_index = model.index(0, 0);
  359. }
  360. if (model.is_valid(new_index)) {
  361. selection().set(new_index);
  362. scroll_into_view(new_index, Orientation::Vertical);
  363. update();
  364. }
  365. return;
  366. }
  367. if (event.key() == KeyCode::Key_Right) {
  368. ModelIndex new_index;
  369. if (!selection().is_empty()) {
  370. auto old_index = selection().first();
  371. new_index = model.index(old_index.row() + 1, old_index.column());
  372. } else {
  373. new_index = model.index(0, 0);
  374. }
  375. if (model.is_valid(new_index)) {
  376. selection().set(new_index);
  377. scroll_into_view(new_index, Orientation::Vertical);
  378. update();
  379. }
  380. return;
  381. }
  382. if (event.key() == KeyCode::Key_PageUp) {
  383. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  384. auto old_index = selection().first();
  385. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  386. if (model.is_valid(new_index)) {
  387. selection().set(new_index);
  388. scroll_into_view(new_index, Orientation::Vertical);
  389. update();
  390. }
  391. return;
  392. }
  393. if (event.key() == KeyCode::Key_PageDown) {
  394. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  395. auto old_index = selection().first();
  396. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  397. if (model.is_valid(new_index)) {
  398. selection().set(new_index);
  399. scroll_into_view(new_index, Orientation::Vertical);
  400. update();
  401. }
  402. return;
  403. }
  404. return Widget::keydown_event(event);
  405. }
  406. }