GItemView.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 <LibDraw/Palette.h>
  29. #include <LibGUI/GDragOperation.h>
  30. #include <LibGUI/GItemView.h>
  31. #include <LibGUI/GModel.h>
  32. #include <LibGUI/GPainter.h>
  33. #include <LibGUI/GScrollBar.h>
  34. GItemView::GItemView(GWidget* parent)
  35. : GAbstractView(parent)
  36. {
  37. set_background_role(ColorRole::Base);
  38. set_foreground_role(ColorRole::BaseText);
  39. set_frame_shape(FrameShape::Container);
  40. set_frame_shadow(FrameShadow::Sunken);
  41. set_frame_thickness(2);
  42. horizontal_scrollbar().set_visible(false);
  43. }
  44. GItemView::~GItemView()
  45. {
  46. }
  47. void GItemView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  48. {
  49. GScrollableWidget::scroll_into_view(item_rect(index.row()), orientation);
  50. }
  51. void GItemView::resize_event(GResizeEvent& event)
  52. {
  53. GAbstractView::resize_event(event);
  54. update_content_size();
  55. }
  56. void GItemView::did_update_model()
  57. {
  58. GAbstractView::did_update_model();
  59. update_content_size();
  60. update();
  61. }
  62. void GItemView::update_content_size()
  63. {
  64. if (!model())
  65. return set_content_size({});
  66. m_visual_column_count = available_size().width() / effective_item_size().width();
  67. if (m_visual_column_count)
  68. m_visual_row_count = ceil_div(model()->row_count(), m_visual_column_count);
  69. else
  70. m_visual_row_count = 0;
  71. int content_width = available_size().width();
  72. int content_height = m_visual_row_count * effective_item_size().height();
  73. set_content_size({ content_width, content_height });
  74. }
  75. Rect GItemView::item_rect(int item_index) const
  76. {
  77. if (!m_visual_row_count || !m_visual_column_count)
  78. return {};
  79. int visual_row_index = item_index / m_visual_column_count;
  80. int visual_column_index = item_index % m_visual_column_count;
  81. return {
  82. visual_column_index * effective_item_size().width(),
  83. visual_row_index * effective_item_size().height(),
  84. effective_item_size().width(),
  85. effective_item_size().height()
  86. };
  87. }
  88. Vector<int> GItemView::items_intersecting_rect(const Rect& rect) const
  89. {
  90. ASSERT(model());
  91. const auto& column_metadata = model()->column_metadata(model_column());
  92. const auto& font = column_metadata.font ? *column_metadata.font : this->font();
  93. Vector<int> item_indexes;
  94. for (int item_index = 0; item_index < item_count(); ++item_index) {
  95. Rect item_rect;
  96. Rect icon_rect;
  97. Rect text_rect;
  98. auto item_text = model()->data(model()->index(item_index, model_column()));
  99. get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
  100. if (icon_rect.intersects(rect) || text_rect.intersects(rect))
  101. item_indexes.append(item_index);
  102. }
  103. return item_indexes;
  104. }
  105. GModelIndex GItemView::index_at_event_position(const Point& position) const
  106. {
  107. ASSERT(model());
  108. // FIXME: Since all items are the same size, just compute the clicked item index
  109. // instead of iterating over everything.
  110. auto adjusted_position = position.translated(0, vertical_scrollbar().value());
  111. const auto& column_metadata = model()->column_metadata(model_column());
  112. const auto& font = column_metadata.font ? *column_metadata.font : this->font();
  113. for (int item_index = 0; item_index < item_count(); ++item_index) {
  114. Rect item_rect;
  115. Rect icon_rect;
  116. Rect text_rect;
  117. auto index = model()->index(item_index, model_column());
  118. auto item_text = model()->data(index);
  119. get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
  120. if (icon_rect.contains(adjusted_position) || text_rect.contains(adjusted_position))
  121. return index;
  122. }
  123. return {};
  124. }
  125. void GItemView::mousedown_event(GMouseEvent& event)
  126. {
  127. auto index = index_at_event_position(event.position());
  128. if (event.button() == GMouseButton::Left) {
  129. m_left_mousedown_position = event.position();
  130. if (!index.is_valid()) {
  131. if (event.modifiers() & Mod_Ctrl) {
  132. selection().for_each_index([&](auto& index) {
  133. m_rubber_band_remembered_selection.append(index);
  134. });
  135. } else {
  136. selection().clear();
  137. }
  138. m_rubber_banding = true;
  139. m_rubber_band_origin = event.position();
  140. m_rubber_band_current = event.position();
  141. } else {
  142. if (event.modifiers() & Mod_Ctrl)
  143. selection().toggle(index);
  144. else if (selection().size() > 1)
  145. m_might_drag = true;
  146. else
  147. selection().set(index);
  148. }
  149. }
  150. GAbstractView::mousedown_event(event);
  151. }
  152. void GItemView::mouseup_event(GMouseEvent& event)
  153. {
  154. if (m_rubber_banding && event.button() == GMouseButton::Left) {
  155. m_rubber_banding = false;
  156. m_rubber_band_remembered_selection.clear();
  157. update();
  158. return;
  159. }
  160. auto index = index_at_event_position(event.position());
  161. if (index.is_valid()) {
  162. if ((selection().size() > 1) & m_might_drag) {
  163. selection().set(index);
  164. m_might_drag = false;
  165. }
  166. }
  167. GAbstractView::mouseup_event(event);
  168. }
  169. void GItemView::mousemove_event(GMouseEvent& event)
  170. {
  171. if (!model())
  172. return GAbstractView::mousemove_event(event);
  173. if (m_rubber_banding) {
  174. if (m_rubber_band_current != event.position()) {
  175. m_rubber_band_current = event.position();
  176. auto rubber_band_rect = Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  177. selection().clear();
  178. for (auto item_index : items_intersecting_rect(rubber_band_rect)) {
  179. selection().add(model()->index(item_index, model_column()));
  180. }
  181. if (event.modifiers() & Mod_Ctrl) {
  182. for (auto stored_item : m_rubber_band_remembered_selection) {
  183. selection().add(stored_item);
  184. }
  185. }
  186. update();
  187. return;
  188. }
  189. }
  190. if (event.buttons() & GMouseButton::Left && !selection().is_empty()) {
  191. auto diff = event.position() - m_left_mousedown_position;
  192. auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
  193. constexpr int drag_distance_threshold = 5;
  194. if (distance_travelled_squared > (drag_distance_threshold)) {
  195. dbg() << "Initiate drag!";
  196. auto drag_operation = GDragOperation::construct();
  197. RefPtr<GraphicsBitmap> bitmap;
  198. StringBuilder text_builder;
  199. StringBuilder data_builder;
  200. int index_iterations = 0;
  201. selection().for_each_index([&](auto& index) {
  202. index_iterations++;
  203. auto text_data = model()->data(index);
  204. if (index_iterations == 0)
  205. text_builder.append(" ");
  206. text_builder.append(text_data.to_string());
  207. if (!(index_iterations == selection().size()))
  208. text_builder.append(", ");
  209. auto drag_data = model()->data(index, GModel::Role::DragData);
  210. data_builder.append(drag_data.to_string());
  211. data_builder.append('\n');
  212. if (!bitmap) {
  213. GVariant icon_data = model()->data(index, GModel::Role::Icon);
  214. if (icon_data.is_icon())
  215. bitmap = icon_data.as_icon().bitmap_for_size(32);
  216. }
  217. });
  218. drag_operation->set_text(text_builder.to_string());
  219. drag_operation->set_bitmap(bitmap);
  220. drag_operation->set_data("url-list", data_builder.to_string());
  221. auto outcome = drag_operation->exec();
  222. switch (outcome) {
  223. case GDragOperation::Outcome::Accepted:
  224. dbg() << "Drag was accepted!";
  225. break;
  226. case GDragOperation::Outcome::Cancelled:
  227. dbg() << "Drag was cancelled!";
  228. break;
  229. default:
  230. ASSERT_NOT_REACHED();
  231. break;
  232. }
  233. }
  234. }
  235. GAbstractView::mousemove_event(event);
  236. }
  237. void GItemView::context_menu_event(GContextMenuEvent& event)
  238. {
  239. if (!model())
  240. return;
  241. auto index = index_at_event_position(event.position());
  242. if (index.is_valid()) {
  243. if (!selection().contains(index))
  244. selection().set(index);
  245. } else {
  246. selection().clear();
  247. }
  248. if (on_context_menu_request)
  249. on_context_menu_request(index, event);
  250. GAbstractView::context_menu_event(event);
  251. }
  252. void GItemView::doubleclick_event(GMouseEvent& event)
  253. {
  254. if (!model())
  255. return;
  256. if (event.button() == GMouseButton::Left) {
  257. mousedown_event(event);
  258. activate_selected();
  259. }
  260. }
  261. void GItemView::get_item_rects(int item_index, const Font& font, const GVariant& item_text, Rect& item_rect, Rect& icon_rect, Rect& text_rect) const
  262. {
  263. item_rect = this->item_rect(item_index);
  264. icon_rect = { 0, 0, 32, 32 };
  265. icon_rect.center_within(item_rect);
  266. icon_rect.move_by(0, -font.glyph_height() - 6);
  267. text_rect = { 0, icon_rect.bottom() + 6 + 1, font.width(item_text.to_string()), font.glyph_height() };
  268. text_rect.center_horizontally_within(item_rect);
  269. text_rect.inflate(6, 4);
  270. text_rect.intersect(item_rect);
  271. }
  272. void GItemView::second_paint_event(GPaintEvent& event)
  273. {
  274. if (!m_rubber_banding)
  275. return;
  276. GPainter painter(*this);
  277. painter.add_clip_rect(event.rect());
  278. auto rubber_band_rect = Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  279. painter.fill_rect(rubber_band_rect, parent_widget()->palette().rubber_band_fill());
  280. painter.draw_rect(rubber_band_rect, parent_widget()->palette().rubber_band_border());
  281. }
  282. void GItemView::paint_event(GPaintEvent& event)
  283. {
  284. Color widget_background_color = palette().color(background_role());
  285. GFrame::paint_event(event);
  286. GPainter painter(*this);
  287. painter.add_clip_rect(widget_inner_rect());
  288. painter.add_clip_rect(event.rect());
  289. painter.fill_rect(event.rect(), widget_background_color);
  290. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  291. auto column_metadata = model()->column_metadata(m_model_column);
  292. const Font& font = column_metadata.font ? *column_metadata.font : this->font();
  293. for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
  294. auto model_index = model()->index(item_index, m_model_column);
  295. bool is_selected_item = selection().contains(model_index);
  296. Color background_color;
  297. if (is_selected_item) {
  298. background_color = is_focused() ? palette().selection() : Color::from_rgb(0x606060);
  299. } else {
  300. background_color = widget_background_color;
  301. }
  302. auto icon = model()->data(model_index, GModel::Role::Icon);
  303. auto item_text = model()->data(model_index, GModel::Role::Display);
  304. Rect item_rect;
  305. Rect icon_rect;
  306. Rect text_rect;
  307. get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
  308. if (icon.is_icon()) {
  309. if (auto bitmap = icon.as_icon().bitmap_for_size(icon_rect.width()))
  310. painter.draw_scaled_bitmap(icon_rect, *bitmap, bitmap->rect());
  311. }
  312. Color text_color;
  313. if (is_selected_item)
  314. text_color = palette().selection_text();
  315. else
  316. text_color = model()->data(model_index, GModel::Role::ForegroundColor).to_color(palette().color(foreground_role()));
  317. painter.fill_rect(text_rect, background_color);
  318. painter.draw_text(text_rect, item_text.to_string(), font, TextAlignment::Center, text_color, TextElision::Right);
  319. };
  320. }
  321. int GItemView::item_count() const
  322. {
  323. if (!model())
  324. return 0;
  325. return model()->row_count();
  326. }
  327. void GItemView::keydown_event(GKeyEvent& event)
  328. {
  329. if (!model())
  330. return;
  331. if (!m_visual_row_count || !m_visual_column_count)
  332. return;
  333. auto& model = *this->model();
  334. if (event.key() == KeyCode::Key_Return) {
  335. activate_selected();
  336. return;
  337. }
  338. if (event.key() == KeyCode::Key_Home) {
  339. auto new_index = model.index(0, 0);
  340. if (model.is_valid(new_index)) {
  341. selection().set(new_index);
  342. scroll_into_view(new_index, Orientation::Vertical);
  343. update();
  344. }
  345. return;
  346. }
  347. if (event.key() == KeyCode::Key_End) {
  348. auto new_index = model.index(model.row_count() - 1, 0);
  349. if (model.is_valid(new_index)) {
  350. selection().set(new_index);
  351. scroll_into_view(new_index, Orientation::Vertical);
  352. update();
  353. }
  354. return;
  355. }
  356. if (event.key() == KeyCode::Key_Up) {
  357. GModelIndex new_index;
  358. if (!selection().is_empty()) {
  359. auto old_index = selection().first();
  360. new_index = model.index(old_index.row() - m_visual_column_count, old_index.column());
  361. } else {
  362. new_index = model.index(0, 0);
  363. }
  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_Down) {
  372. GModelIndex new_index;
  373. if (!selection().is_empty()) {
  374. auto old_index = selection().first();
  375. new_index = model.index(old_index.row() + m_visual_column_count, old_index.column());
  376. } else {
  377. new_index = model.index(0, 0);
  378. }
  379. if (model.is_valid(new_index)) {
  380. selection().set(new_index);
  381. scroll_into_view(new_index, Orientation::Vertical);
  382. update();
  383. }
  384. return;
  385. }
  386. if (event.key() == KeyCode::Key_Left) {
  387. GModelIndex new_index;
  388. if (!selection().is_empty()) {
  389. auto old_index = selection().first();
  390. new_index = model.index(old_index.row() - 1, old_index.column());
  391. } else {
  392. new_index = model.index(0, 0);
  393. }
  394. if (model.is_valid(new_index)) {
  395. selection().set(new_index);
  396. scroll_into_view(new_index, Orientation::Vertical);
  397. update();
  398. }
  399. return;
  400. }
  401. if (event.key() == KeyCode::Key_Right) {
  402. GModelIndex new_index;
  403. if (!selection().is_empty()) {
  404. auto old_index = selection().first();
  405. new_index = model.index(old_index.row() + 1, old_index.column());
  406. } else {
  407. new_index = model.index(0, 0);
  408. }
  409. if (model.is_valid(new_index)) {
  410. selection().set(new_index);
  411. scroll_into_view(new_index, Orientation::Vertical);
  412. update();
  413. }
  414. return;
  415. }
  416. if (event.key() == KeyCode::Key_PageUp) {
  417. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  418. auto old_index = selection().first();
  419. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  420. if (model.is_valid(new_index)) {
  421. selection().set(new_index);
  422. scroll_into_view(new_index, Orientation::Vertical);
  423. update();
  424. }
  425. return;
  426. }
  427. if (event.key() == KeyCode::Key_PageDown) {
  428. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  429. auto old_index = selection().first();
  430. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  431. if (model.is_valid(new_index)) {
  432. selection().set(new_index);
  433. scroll_into_view(new_index, Orientation::Vertical);
  434. update();
  435. }
  436. return;
  437. }
  438. return GWidget::keydown_event(event);
  439. }