ItemView.cpp 16 KB

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