GAbstractView.cpp 9.2 KB

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