AbstractView.cpp 11 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 <AK/Vector.h>
  28. #include <LibGUI/AbstractView.h>
  29. #include <LibGUI/DragOperation.h>
  30. #include <LibGUI/Model.h>
  31. #include <LibGUI/ModelEditingDelegate.h>
  32. #include <LibGUI/Painter.h>
  33. #include <LibGUI/ScrollBar.h>
  34. #include <LibGUI/TextBox.h>
  35. namespace GUI {
  36. AbstractView::AbstractView()
  37. : m_sort_order(SortOrder::Ascending)
  38. , m_selection(*this)
  39. {
  40. }
  41. AbstractView::~AbstractView()
  42. {
  43. }
  44. void AbstractView::set_model(RefPtr<Model> model)
  45. {
  46. if (model == m_model)
  47. return;
  48. if (m_model)
  49. m_model->unregister_view({}, *this);
  50. m_model = move(model);
  51. if (m_model)
  52. m_model->register_view({}, *this);
  53. did_update_model(GUI::Model::InvalidateAllIndexes);
  54. scroll_to_top();
  55. }
  56. void AbstractView::did_update_model(unsigned flags)
  57. {
  58. // FIXME: It's unfortunate that we lose so much view state when the model updates in any way.
  59. stop_editing();
  60. m_edit_index = {};
  61. m_hovered_index = {};
  62. if (!model() || (flags & GUI::Model::InvalidateAllIndexes)) {
  63. clear_selection();
  64. } else {
  65. selection().remove_matching([this](auto& index) { return !model()->is_valid(index); });
  66. }
  67. }
  68. void AbstractView::clear_selection()
  69. {
  70. m_selection.clear();
  71. }
  72. void AbstractView::set_selection(const ModelIndex& new_index)
  73. {
  74. m_selection.set(new_index);
  75. }
  76. void AbstractView::add_selection(const ModelIndex& new_index)
  77. {
  78. m_selection.add(new_index);
  79. }
  80. void AbstractView::remove_selection(const ModelIndex& new_index)
  81. {
  82. m_selection.remove(new_index);
  83. }
  84. void AbstractView::toggle_selection(const ModelIndex& new_index)
  85. {
  86. m_selection.toggle(new_index);
  87. }
  88. void AbstractView::did_update_selection()
  89. {
  90. if (!model() || selection().first() != m_edit_index)
  91. stop_editing();
  92. if (model() && on_selection && selection().first().is_valid())
  93. on_selection(selection().first());
  94. }
  95. void AbstractView::did_scroll()
  96. {
  97. update_edit_widget_position();
  98. }
  99. void AbstractView::update_edit_widget_position()
  100. {
  101. if (!m_edit_widget)
  102. return;
  103. m_edit_widget->set_relative_rect(m_edit_widget_content_rect.translated(-horizontal_scrollbar().value(), -vertical_scrollbar().value()));
  104. }
  105. void AbstractView::begin_editing(const ModelIndex& index)
  106. {
  107. ASSERT(is_editable());
  108. ASSERT(model());
  109. if (m_edit_index == index)
  110. return;
  111. if (!model()->is_editable(index))
  112. return;
  113. if (m_edit_widget) {
  114. remove_child(*m_edit_widget);
  115. m_edit_widget = nullptr;
  116. }
  117. m_edit_index = index;
  118. ASSERT(aid_create_editing_delegate);
  119. m_editing_delegate = aid_create_editing_delegate(index);
  120. m_editing_delegate->bind(*model(), index);
  121. m_editing_delegate->set_value(index.data());
  122. m_edit_widget = m_editing_delegate->widget();
  123. add_child(*m_edit_widget);
  124. m_edit_widget->move_to_back();
  125. m_edit_widget_content_rect = content_rect(index).translated(frame_thickness(), frame_thickness());
  126. update_edit_widget_position();
  127. m_edit_widget->set_focus(true);
  128. m_editing_delegate->will_begin_editing();
  129. m_editing_delegate->on_commit = [this] {
  130. ASSERT(model());
  131. model()->set_data(m_edit_index, m_editing_delegate->value());
  132. stop_editing();
  133. };
  134. }
  135. void AbstractView::stop_editing()
  136. {
  137. bool take_back_focus = false;
  138. m_edit_index = {};
  139. if (m_edit_widget) {
  140. take_back_focus = m_edit_widget->is_focused();
  141. remove_child(*m_edit_widget);
  142. m_edit_widget = nullptr;
  143. }
  144. if (take_back_focus)
  145. set_focus(true);
  146. }
  147. void AbstractView::activate(const ModelIndex& index)
  148. {
  149. if (on_activation)
  150. on_activation(index);
  151. }
  152. void AbstractView::activate_selected()
  153. {
  154. if (!on_activation)
  155. return;
  156. selection().for_each_index([this](auto& index) {
  157. on_activation(index);
  158. });
  159. }
  160. void AbstractView::notify_selection_changed(Badge<ModelSelection>)
  161. {
  162. did_update_selection();
  163. if (on_selection_change)
  164. on_selection_change();
  165. update();
  166. }
  167. NonnullRefPtr<Gfx::Font> AbstractView::font_for_index(const ModelIndex& index) const
  168. {
  169. if (!model())
  170. return font();
  171. auto font_data = index.data(ModelRole::Font);
  172. if (font_data.is_font())
  173. return font_data.as_font();
  174. return font();
  175. }
  176. void AbstractView::mousedown_event(MouseEvent& event)
  177. {
  178. ScrollableWidget::mousedown_event(event);
  179. if (!model())
  180. return;
  181. if (event.button() == MouseButton::Left)
  182. m_left_mousedown_position = event.position();
  183. auto index = index_at_event_position(event.position());
  184. m_might_drag = false;
  185. if (!index.is_valid()) {
  186. clear_selection();
  187. } else if (event.modifiers() & Mod_Ctrl) {
  188. toggle_selection(index);
  189. } else if (event.button() == MouseButton::Left && m_selection.contains(index) && !m_model->drag_data_type().is_null()) {
  190. // We might be starting a drag, so don't throw away other selected items yet.
  191. m_might_drag = true;
  192. } else {
  193. set_selection(index);
  194. }
  195. update();
  196. }
  197. void AbstractView::set_hovered_index(const ModelIndex& index)
  198. {
  199. if (m_hovered_index == index)
  200. return;
  201. m_hovered_index = index;
  202. if (m_hovered_index.is_valid())
  203. m_last_valid_hovered_index = m_hovered_index;
  204. update();
  205. }
  206. void AbstractView::set_last_valid_hovered_index(const ModelIndex& index)
  207. {
  208. if (m_last_valid_hovered_index == index)
  209. return;
  210. m_last_valid_hovered_index = index;
  211. update();
  212. }
  213. void AbstractView::leave_event(Core::Event& event)
  214. {
  215. ScrollableWidget::leave_event(event);
  216. set_hovered_index({});
  217. }
  218. void AbstractView::mousemove_event(MouseEvent& event)
  219. {
  220. if (!model())
  221. return ScrollableWidget::mousemove_event(event);
  222. auto hovered_index = index_at_event_position(event.position());
  223. set_hovered_index(hovered_index);
  224. if (!m_might_drag)
  225. return ScrollableWidget::mousemove_event(event);
  226. if (!(event.buttons() & MouseButton::Left) || m_selection.is_empty()) {
  227. m_might_drag = false;
  228. return ScrollableWidget::mousemove_event(event);
  229. }
  230. auto diff = event.position() - m_left_mousedown_position;
  231. auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
  232. constexpr int drag_distance_threshold = 5;
  233. if (distance_travelled_squared <= drag_distance_threshold)
  234. return ScrollableWidget::mousemove_event(event);
  235. auto data_type = m_model->drag_data_type();
  236. ASSERT(!data_type.is_null());
  237. dbg() << "Initiate drag!";
  238. auto drag_operation = DragOperation::construct();
  239. RefPtr<Gfx::Bitmap> bitmap;
  240. StringBuilder text_builder;
  241. StringBuilder data_builder;
  242. bool first = true;
  243. m_selection.for_each_index([&](auto& index) {
  244. auto text_data = index.data();
  245. if (!first)
  246. text_builder.append(", ");
  247. text_builder.append(text_data.to_string());
  248. auto drag_data = index.data(ModelRole::DragData);
  249. data_builder.append(drag_data.to_string());
  250. data_builder.append('\n');
  251. first = false;
  252. if (!bitmap) {
  253. Variant icon_data = index.data(ModelRole::Icon);
  254. if (icon_data.is_icon())
  255. bitmap = icon_data.as_icon().bitmap_for_size(32);
  256. }
  257. });
  258. drag_operation->set_text(text_builder.to_string());
  259. drag_operation->set_bitmap(bitmap);
  260. drag_operation->set_data(data_type, data_builder.to_string());
  261. auto outcome = drag_operation->exec();
  262. switch (outcome) {
  263. case DragOperation::Outcome::Accepted:
  264. dbg() << "Drag was accepted!";
  265. break;
  266. case DragOperation::Outcome::Cancelled:
  267. dbg() << "Drag was cancelled!";
  268. break;
  269. default:
  270. ASSERT_NOT_REACHED();
  271. break;
  272. }
  273. }
  274. void AbstractView::mouseup_event(MouseEvent& event)
  275. {
  276. ScrollableWidget::mouseup_event(event);
  277. if (!model())
  278. return;
  279. if (m_might_drag) {
  280. // We were unsure about unselecting items other than the current one
  281. // in mousedown_event(), because we could be seeing a start of a drag.
  282. // Since we're here, it was not that; so fix up the selection now.
  283. auto index = index_at_event_position(event.position());
  284. if (index.is_valid())
  285. set_selection(index);
  286. else
  287. clear_selection();
  288. m_might_drag = false;
  289. update();
  290. }
  291. if (activates_on_selection())
  292. activate_selected();
  293. }
  294. void AbstractView::doubleclick_event(MouseEvent& event)
  295. {
  296. if (!model())
  297. return;
  298. if (event.button() != MouseButton::Left)
  299. return;
  300. m_might_drag = false;
  301. auto index = index_at_event_position(event.position());
  302. if (!index.is_valid())
  303. clear_selection();
  304. else if (!m_selection.contains(index))
  305. set_selection(index);
  306. activate_or_edit_selected();
  307. }
  308. void AbstractView::activate_or_edit_selected()
  309. {
  310. if (is_editable())
  311. begin_editing(selection().first());
  312. else
  313. activate_selected();
  314. }
  315. void AbstractView::context_menu_event(ContextMenuEvent& event)
  316. {
  317. if (!model())
  318. return;
  319. auto index = index_at_event_position(event.position());
  320. if (index.is_valid())
  321. add_selection(index);
  322. else
  323. clear_selection();
  324. if (on_context_menu_request)
  325. on_context_menu_request(index, event);
  326. }
  327. void AbstractView::drop_event(DropEvent& event)
  328. {
  329. event.accept();
  330. if (!model())
  331. return;
  332. auto index = index_at_event_position(event.position());
  333. if (on_drop)
  334. on_drop(index, event);
  335. }
  336. void AbstractView::set_multi_select(bool multi_select)
  337. {
  338. if (m_multi_select == multi_select)
  339. return;
  340. m_multi_select = multi_select;
  341. if (!multi_select && m_selection.size() > 1) {
  342. auto first_selected = m_selection.first();
  343. m_selection.clear();
  344. m_selection.set(first_selected);
  345. }
  346. }
  347. void AbstractView::set_key_column_and_sort_order(int column, SortOrder sort_order)
  348. {
  349. m_key_column = column;
  350. m_sort_order = sort_order;
  351. if (model())
  352. model()->sort(column, sort_order);
  353. update();
  354. }
  355. }