AbstractView.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. if (m_model)
  44. m_model->unregister_view({}, *this);
  45. }
  46. void AbstractView::set_model(RefPtr<Model> model)
  47. {
  48. if (model == m_model)
  49. return;
  50. if (m_model)
  51. m_model->unregister_view({}, *this);
  52. m_model = move(model);
  53. if (m_model)
  54. m_model->register_view({}, *this);
  55. did_update_model(GUI::Model::InvalidateAllIndexes);
  56. scroll_to_top();
  57. }
  58. void AbstractView::did_update_model(unsigned flags)
  59. {
  60. // FIXME: It's unfortunate that we lose so much view state when the model updates in any way.
  61. stop_editing();
  62. m_edit_index = {};
  63. m_hovered_index = {};
  64. if (!model() || (flags & GUI::Model::InvalidateAllIndexes)) {
  65. clear_selection();
  66. } else {
  67. selection().remove_matching([this](auto& index) { return !model()->is_valid(index); });
  68. }
  69. }
  70. void AbstractView::clear_selection()
  71. {
  72. m_selection.clear();
  73. }
  74. void AbstractView::set_selection(const ModelIndex& new_index)
  75. {
  76. m_selection.set(new_index);
  77. }
  78. void AbstractView::add_selection(const ModelIndex& new_index)
  79. {
  80. m_selection.add(new_index);
  81. }
  82. void AbstractView::remove_selection(const ModelIndex& new_index)
  83. {
  84. m_selection.remove(new_index);
  85. }
  86. void AbstractView::toggle_selection(const ModelIndex& new_index)
  87. {
  88. m_selection.toggle(new_index);
  89. }
  90. void AbstractView::did_update_selection()
  91. {
  92. if (!model() || selection().first() != m_edit_index)
  93. stop_editing();
  94. if (model() && on_selection && selection().first().is_valid())
  95. on_selection(selection().first());
  96. }
  97. void AbstractView::did_scroll()
  98. {
  99. update_edit_widget_position();
  100. }
  101. void AbstractView::update_edit_widget_position()
  102. {
  103. if (!m_edit_widget)
  104. return;
  105. m_edit_widget->set_relative_rect(m_edit_widget_content_rect.translated(-horizontal_scrollbar().value(), -vertical_scrollbar().value()));
  106. }
  107. void AbstractView::begin_editing(const ModelIndex& index)
  108. {
  109. ASSERT(is_editable());
  110. ASSERT(model());
  111. if (m_edit_index == index)
  112. return;
  113. if (!model()->is_editable(index))
  114. return;
  115. if (m_edit_widget) {
  116. remove_child(*m_edit_widget);
  117. m_edit_widget = nullptr;
  118. }
  119. m_edit_index = index;
  120. ASSERT(aid_create_editing_delegate);
  121. m_editing_delegate = aid_create_editing_delegate(index);
  122. m_editing_delegate->bind(*model(), index);
  123. m_editing_delegate->set_value(index.data());
  124. m_edit_widget = m_editing_delegate->widget();
  125. add_child(*m_edit_widget);
  126. m_edit_widget->move_to_back();
  127. m_edit_widget_content_rect = content_rect(index).translated(frame_thickness(), frame_thickness());
  128. update_edit_widget_position();
  129. m_edit_widget->set_focus(true);
  130. m_editing_delegate->will_begin_editing();
  131. m_editing_delegate->on_commit = [this] {
  132. ASSERT(model());
  133. model()->set_data(m_edit_index, m_editing_delegate->value());
  134. stop_editing();
  135. };
  136. m_editing_delegate->on_rollback = [this] {
  137. ASSERT(model());
  138. stop_editing();
  139. };
  140. }
  141. void AbstractView::stop_editing()
  142. {
  143. bool take_back_focus = false;
  144. m_edit_index = {};
  145. if (m_edit_widget) {
  146. take_back_focus = m_edit_widget->is_focused();
  147. remove_child(*m_edit_widget);
  148. m_edit_widget = nullptr;
  149. }
  150. if (take_back_focus)
  151. set_focus(true);
  152. }
  153. void AbstractView::activate(const ModelIndex& index)
  154. {
  155. if (on_activation)
  156. on_activation(index);
  157. }
  158. void AbstractView::activate_selected()
  159. {
  160. if (!on_activation)
  161. return;
  162. selection().for_each_index([this](auto& index) {
  163. on_activation(index);
  164. });
  165. }
  166. void AbstractView::notify_selection_changed(Badge<ModelSelection>)
  167. {
  168. did_update_selection();
  169. if (on_selection_change)
  170. on_selection_change();
  171. update();
  172. }
  173. NonnullRefPtr<Gfx::Font> AbstractView::font_for_index(const ModelIndex& index) const
  174. {
  175. if (!model())
  176. return font();
  177. auto font_data = index.data(ModelRole::Font);
  178. if (font_data.is_font())
  179. return font_data.as_font();
  180. return font();
  181. }
  182. void AbstractView::mousedown_event(MouseEvent& event)
  183. {
  184. ScrollableWidget::mousedown_event(event);
  185. if (!model())
  186. return;
  187. if (event.button() == MouseButton::Left)
  188. m_left_mousedown_position = event.position();
  189. auto index = index_at_event_position(event.position());
  190. m_might_drag = false;
  191. if (!index.is_valid()) {
  192. clear_selection();
  193. } else if (event.modifiers() & Mod_Ctrl) {
  194. set_cursor(index, SelectionUpdate::Ctrl);
  195. } else if (event.button() == MouseButton::Left && m_selection.contains(index) && !m_model->drag_data_type().is_null()) {
  196. // We might be starting a drag, so don't throw away other selected items yet.
  197. m_might_drag = true;
  198. } else {
  199. set_cursor(index, SelectionUpdate::Set);
  200. }
  201. update();
  202. }
  203. void AbstractView::set_hovered_index(const ModelIndex& index)
  204. {
  205. if (m_hovered_index == index)
  206. return;
  207. m_hovered_index = index;
  208. update();
  209. }
  210. void AbstractView::leave_event(Core::Event& event)
  211. {
  212. ScrollableWidget::leave_event(event);
  213. set_hovered_index({});
  214. }
  215. void AbstractView::mousemove_event(MouseEvent& event)
  216. {
  217. if (!model())
  218. return ScrollableWidget::mousemove_event(event);
  219. auto hovered_index = index_at_event_position(event.position());
  220. set_hovered_index(hovered_index);
  221. if (!m_might_drag)
  222. return ScrollableWidget::mousemove_event(event);
  223. if (!(event.buttons() & MouseButton::Left) || m_selection.is_empty()) {
  224. m_might_drag = false;
  225. return ScrollableWidget::mousemove_event(event);
  226. }
  227. auto diff = event.position() - m_left_mousedown_position;
  228. auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
  229. constexpr int drag_distance_threshold = 5;
  230. if (distance_travelled_squared <= drag_distance_threshold)
  231. return ScrollableWidget::mousemove_event(event);
  232. auto data_type = m_model->drag_data_type();
  233. ASSERT(!data_type.is_null());
  234. dbg() << "Initiate drag!";
  235. auto drag_operation = DragOperation::construct();
  236. RefPtr<Gfx::Bitmap> bitmap;
  237. StringBuilder text_builder;
  238. StringBuilder data_builder;
  239. bool first = true;
  240. m_selection.for_each_index([&](auto& index) {
  241. auto text_data = index.data();
  242. if (!first)
  243. text_builder.append(", ");
  244. text_builder.append(text_data.to_string());
  245. auto drag_data = index.data(ModelRole::DragData);
  246. data_builder.append(drag_data.to_string());
  247. data_builder.append('\n');
  248. first = false;
  249. if (!bitmap) {
  250. Variant icon_data = index.data(ModelRole::Icon);
  251. if (icon_data.is_icon())
  252. bitmap = icon_data.as_icon().bitmap_for_size(32);
  253. }
  254. });
  255. drag_operation->set_text(text_builder.to_string());
  256. drag_operation->set_bitmap(bitmap);
  257. drag_operation->set_data(data_type, data_builder.to_string());
  258. auto outcome = drag_operation->exec();
  259. switch (outcome) {
  260. case DragOperation::Outcome::Accepted:
  261. dbg() << "Drag was accepted!";
  262. break;
  263. case DragOperation::Outcome::Cancelled:
  264. dbg() << "Drag was cancelled!";
  265. break;
  266. default:
  267. ASSERT_NOT_REACHED();
  268. break;
  269. }
  270. }
  271. void AbstractView::mouseup_event(MouseEvent& event)
  272. {
  273. ScrollableWidget::mouseup_event(event);
  274. if (!model())
  275. return;
  276. if (m_might_drag) {
  277. // We were unsure about unselecting items other than the current one
  278. // in mousedown_event(), because we could be seeing a start of a drag.
  279. // Since we're here, it was not that; so fix up the selection now.
  280. auto index = index_at_event_position(event.position());
  281. if (index.is_valid())
  282. set_selection(index);
  283. else
  284. clear_selection();
  285. m_might_drag = false;
  286. update();
  287. }
  288. if (activates_on_selection())
  289. activate_selected();
  290. }
  291. void AbstractView::doubleclick_event(MouseEvent& event)
  292. {
  293. if (!model())
  294. return;
  295. if (event.button() != MouseButton::Left)
  296. return;
  297. m_might_drag = false;
  298. auto index = index_at_event_position(event.position());
  299. if (!index.is_valid())
  300. clear_selection();
  301. else if (!m_selection.contains(index))
  302. set_selection(index);
  303. if (is_editable() && edit_triggers() & EditTrigger::DoubleClicked)
  304. begin_editing(cursor_index());
  305. else
  306. activate(cursor_index());
  307. }
  308. void AbstractView::context_menu_event(ContextMenuEvent& event)
  309. {
  310. if (!model())
  311. return;
  312. auto index = index_at_event_position(event.position());
  313. if (index.is_valid())
  314. add_selection(index);
  315. else
  316. clear_selection();
  317. if (on_context_menu_request)
  318. on_context_menu_request(index, event);
  319. }
  320. void AbstractView::drop_event(DropEvent& event)
  321. {
  322. event.accept();
  323. if (!model())
  324. return;
  325. auto index = index_at_event_position(event.position());
  326. if (on_drop)
  327. on_drop(index, event);
  328. }
  329. void AbstractView::set_multi_select(bool multi_select)
  330. {
  331. if (m_multi_select == multi_select)
  332. return;
  333. m_multi_select = multi_select;
  334. if (!multi_select && m_selection.size() > 1) {
  335. auto first_selected = m_selection.first();
  336. m_selection.clear();
  337. m_selection.set(first_selected);
  338. }
  339. }
  340. void AbstractView::set_key_column_and_sort_order(int column, SortOrder sort_order)
  341. {
  342. m_key_column = column;
  343. m_sort_order = sort_order;
  344. if (model())
  345. model()->sort(column, sort_order);
  346. update();
  347. }
  348. void AbstractView::set_cursor(ModelIndex index, SelectionUpdate selection_update, bool scroll_cursor_into_view)
  349. {
  350. if (!model() || !index.is_valid()) {
  351. m_cursor_index = {};
  352. return;
  353. }
  354. if (model()->is_valid(index)) {
  355. if (selection_update == SelectionUpdate::Set)
  356. set_selection(index);
  357. else if (selection_update == SelectionUpdate::Ctrl)
  358. toggle_selection(index);
  359. // FIXME: Support the other SelectionUpdate types
  360. m_cursor_index = index;
  361. if (scroll_cursor_into_view) {
  362. // FIXME: We should scroll into view both vertically *and* horizontally.
  363. scroll_into_view(index, false, true);
  364. }
  365. update();
  366. }
  367. }
  368. void AbstractView::set_edit_triggers(unsigned triggers)
  369. {
  370. m_edit_triggers = triggers;
  371. }
  372. void AbstractView::keydown_event(KeyEvent& event)
  373. {
  374. if (event.key() == KeyCode::Key_F2) {
  375. if (is_editable() && edit_triggers() & EditTrigger::EditKeyPressed) {
  376. begin_editing(cursor_index());
  377. event.accept();
  378. return;
  379. }
  380. }
  381. SelectionUpdate selection_update = SelectionUpdate::Set;
  382. if (event.modifiers() == KeyModifier::Mod_Shift) {
  383. selection_update = SelectionUpdate::Shift;
  384. }
  385. if (event.key() == KeyCode::Key_Left) {
  386. move_cursor(CursorMovement::Left, selection_update);
  387. event.accept();
  388. return;
  389. }
  390. if (event.key() == KeyCode::Key_Right) {
  391. move_cursor(CursorMovement::Right, selection_update);
  392. event.accept();
  393. return;
  394. }
  395. if (event.key() == KeyCode::Key_Up) {
  396. move_cursor(CursorMovement::Up, selection_update);
  397. event.accept();
  398. return;
  399. }
  400. if (event.key() == KeyCode::Key_Down) {
  401. move_cursor(CursorMovement::Down, selection_update);
  402. event.accept();
  403. return;
  404. }
  405. if (event.key() == KeyCode::Key_Home) {
  406. move_cursor(CursorMovement::Home, selection_update);
  407. event.accept();
  408. return;
  409. }
  410. if (event.key() == KeyCode::Key_End) {
  411. move_cursor(CursorMovement::End, selection_update);
  412. event.accept();
  413. return;
  414. }
  415. if (event.key() == KeyCode::Key_PageUp) {
  416. move_cursor(CursorMovement::PageUp, selection_update);
  417. event.accept();
  418. return;
  419. }
  420. if (event.key() == KeyCode::Key_PageDown) {
  421. move_cursor(CursorMovement::PageDown, selection_update);
  422. event.accept();
  423. return;
  424. }
  425. }
  426. }