AbstractView.cpp 9.3 KB

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