AbstractView.cpp 9.4 KB

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