ItemView.cpp 15 KB

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