AbstractView.cpp 9.9 KB

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