ItemView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 = position.translated(0, vertical_scrollbar().value());
  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. void ItemView::mousedown_event(MouseEvent& event)
  132. {
  133. if (!model())
  134. return AbstractView::mousedown_event(event);
  135. if (event.button() != MouseButton::Left)
  136. return AbstractView::mousedown_event(event);
  137. auto index = index_at_event_position(event.position());
  138. if (index.is_valid()) {
  139. // We might start dragging this item, but not rubber-banding.
  140. return AbstractView::mousedown_event(event);
  141. }
  142. ASSERT(m_rubber_band_remembered_selection.is_empty());
  143. if (event.modifiers() & Mod_Ctrl) {
  144. selection().for_each_index([&](auto& index) {
  145. m_rubber_band_remembered_selection.append(index);
  146. });
  147. } else {
  148. selection().clear();
  149. }
  150. m_might_drag = false;
  151. m_rubber_banding = true;
  152. m_rubber_band_origin = event.position();
  153. m_rubber_band_current = event.position();
  154. }
  155. void ItemView::mouseup_event(MouseEvent& event)
  156. {
  157. if (m_rubber_banding && event.button() == MouseButton::Left) {
  158. m_rubber_banding = false;
  159. m_rubber_band_remembered_selection.clear();
  160. update();
  161. }
  162. AbstractView::mouseup_event(event);
  163. }
  164. void ItemView::drag_move_event(DragEvent& event)
  165. {
  166. auto index = index_at_event_position(event.position());
  167. ModelIndex new_drop_candidate_index;
  168. if (index.is_valid()) {
  169. bool acceptable = model()->accepts_drag(index, event.data_type());
  170. #ifdef DRAGDROP_DEBUG
  171. dbg() << "Drag of type '" << event.data_type() << "' moving over " << index << ", acceptable: " << acceptable;
  172. #endif
  173. if (acceptable)
  174. new_drop_candidate_index = index;
  175. }
  176. if (m_drop_candidate_index != new_drop_candidate_index) {
  177. m_drop_candidate_index = new_drop_candidate_index;
  178. update();
  179. }
  180. event.accept();
  181. }
  182. void ItemView::mousemove_event(MouseEvent& event)
  183. {
  184. if (!model())
  185. return AbstractView::mousemove_event(event);
  186. if (m_rubber_banding) {
  187. if (m_rubber_band_current != event.position()) {
  188. m_rubber_band_current = event.position();
  189. auto rubber_band_rect = Gfx::Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  190. selection().clear();
  191. for (auto item_index : items_intersecting_rect(rubber_band_rect)) {
  192. selection().add(model()->index(item_index, model_column()));
  193. }
  194. if (event.modifiers() & Mod_Ctrl) {
  195. for (auto stored_item : m_rubber_band_remembered_selection) {
  196. selection().add(stored_item);
  197. }
  198. }
  199. update();
  200. return;
  201. }
  202. }
  203. AbstractView::mousemove_event(event);
  204. }
  205. 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
  206. {
  207. item_rect = this->item_rect(item_index);
  208. icon_rect = { 0, 0, 32, 32 };
  209. icon_rect.center_within(item_rect);
  210. icon_rect.move_by(0, -font.glyph_height() - 6);
  211. text_rect = { 0, icon_rect.bottom() + 6 + 1, font.width(item_text.to_string()), font.glyph_height() };
  212. text_rect.center_horizontally_within(item_rect);
  213. text_rect.inflate(6, 4);
  214. text_rect.intersect(item_rect);
  215. }
  216. void ItemView::second_paint_event(PaintEvent& event)
  217. {
  218. if (!m_rubber_banding)
  219. return;
  220. Painter painter(*this);
  221. painter.add_clip_rect(event.rect());
  222. auto rubber_band_rect = Gfx::Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  223. painter.fill_rect(rubber_band_rect, parent_widget()->palette().rubber_band_fill());
  224. painter.draw_rect(rubber_band_rect, parent_widget()->palette().rubber_band_border());
  225. }
  226. void ItemView::paint_event(PaintEvent& event)
  227. {
  228. Color widget_background_color = palette().color(background_role());
  229. Frame::paint_event(event);
  230. Painter painter(*this);
  231. painter.add_clip_rect(widget_inner_rect());
  232. painter.add_clip_rect(event.rect());
  233. painter.fill_rect(event.rect(), widget_background_color);
  234. painter.translate(frame_thickness(), frame_thickness());
  235. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  236. auto column_metadata = model()->column_metadata(m_model_column);
  237. const Gfx::Font& font = column_metadata.font ? *column_metadata.font : this->font();
  238. for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
  239. auto model_index = model()->index(item_index, m_model_column);
  240. bool is_selected_item = selection().contains(model_index);
  241. Color background_color;
  242. if (is_selected_item) {
  243. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  244. } else {
  245. background_color = widget_background_color;
  246. }
  247. auto icon = model()->data(model_index, Model::Role::Icon);
  248. auto item_text = model()->data(model_index, Model::Role::Display);
  249. Gfx::Rect item_rect;
  250. Gfx::Rect icon_rect;
  251. Gfx::Rect text_rect;
  252. get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
  253. if (icon.is_icon()) {
  254. if (auto bitmap = icon.as_icon().bitmap_for_size(icon_rect.width()))
  255. painter.draw_scaled_bitmap(icon_rect, *bitmap, bitmap->rect());
  256. }
  257. Color text_color;
  258. if (is_selected_item)
  259. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  260. else
  261. text_color = model()->data(model_index, Model::Role::ForegroundColor).to_color(palette().color(foreground_role()));
  262. painter.fill_rect(text_rect, background_color);
  263. painter.draw_text(text_rect, item_text.to_string(), font, Gfx::TextAlignment::Center, text_color, Gfx::TextElision::Right);
  264. if (model_index == m_drop_candidate_index) {
  265. // FIXME: This visualization is not great, as it's also possible to drop things on the text label..
  266. painter.draw_rect(icon_rect.inflated(8, 8), palette().selection(), true);
  267. }
  268. };
  269. }
  270. int ItemView::item_count() const
  271. {
  272. if (!model())
  273. return 0;
  274. return model()->row_count();
  275. }
  276. void ItemView::keydown_event(KeyEvent& event)
  277. {
  278. if (!model())
  279. return;
  280. if (!m_visual_row_count || !m_visual_column_count)
  281. return;
  282. auto& model = *this->model();
  283. if (event.key() == KeyCode::Key_Return) {
  284. activate_selected();
  285. return;
  286. }
  287. if (event.key() == KeyCode::Key_Home) {
  288. auto new_index = model.index(0, 0);
  289. if (model.is_valid(new_index)) {
  290. selection().set(new_index);
  291. scroll_into_view(new_index, Orientation::Vertical);
  292. update();
  293. }
  294. return;
  295. }
  296. if (event.key() == KeyCode::Key_End) {
  297. auto new_index = model.index(model.row_count() - 1, 0);
  298. if (model.is_valid(new_index)) {
  299. selection().set(new_index);
  300. scroll_into_view(new_index, Orientation::Vertical);
  301. update();
  302. }
  303. return;
  304. }
  305. if (event.key() == KeyCode::Key_Up) {
  306. ModelIndex new_index;
  307. if (!selection().is_empty()) {
  308. auto old_index = selection().first();
  309. new_index = model.index(old_index.row() - m_visual_column_count, old_index.column());
  310. } else {
  311. new_index = model.index(0, 0);
  312. }
  313. if (model.is_valid(new_index)) {
  314. selection().set(new_index);
  315. scroll_into_view(new_index, Orientation::Vertical);
  316. update();
  317. }
  318. return;
  319. }
  320. if (event.key() == KeyCode::Key_Down) {
  321. ModelIndex new_index;
  322. if (!selection().is_empty()) {
  323. auto old_index = selection().first();
  324. new_index = model.index(old_index.row() + m_visual_column_count, old_index.column());
  325. } else {
  326. new_index = model.index(0, 0);
  327. }
  328. if (model.is_valid(new_index)) {
  329. selection().set(new_index);
  330. scroll_into_view(new_index, Orientation::Vertical);
  331. update();
  332. }
  333. return;
  334. }
  335. if (event.key() == KeyCode::Key_Left) {
  336. ModelIndex new_index;
  337. if (!selection().is_empty()) {
  338. auto old_index = selection().first();
  339. new_index = model.index(old_index.row() - 1, old_index.column());
  340. } else {
  341. new_index = model.index(0, 0);
  342. }
  343. if (model.is_valid(new_index)) {
  344. selection().set(new_index);
  345. scroll_into_view(new_index, Orientation::Vertical);
  346. update();
  347. }
  348. return;
  349. }
  350. if (event.key() == KeyCode::Key_Right) {
  351. ModelIndex new_index;
  352. if (!selection().is_empty()) {
  353. auto old_index = selection().first();
  354. new_index = model.index(old_index.row() + 1, old_index.column());
  355. } else {
  356. new_index = model.index(0, 0);
  357. }
  358. if (model.is_valid(new_index)) {
  359. selection().set(new_index);
  360. scroll_into_view(new_index, Orientation::Vertical);
  361. update();
  362. }
  363. return;
  364. }
  365. if (event.key() == KeyCode::Key_PageUp) {
  366. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  367. auto old_index = selection().first();
  368. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  369. if (model.is_valid(new_index)) {
  370. selection().set(new_index);
  371. scroll_into_view(new_index, Orientation::Vertical);
  372. update();
  373. }
  374. return;
  375. }
  376. if (event.key() == KeyCode::Key_PageDown) {
  377. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  378. auto old_index = selection().first();
  379. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  380. if (model.is_valid(new_index)) {
  381. selection().set(new_index);
  382. scroll_into_view(new_index, Orientation::Vertical);
  383. update();
  384. }
  385. return;
  386. }
  387. return Widget::keydown_event(event);
  388. }
  389. }