ItemView.cpp 15 KB

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