AbstractView.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <AK/Utf8View.h>
  8. #include <AK/Vector.h>
  9. #include <LibCore/Timer.h>
  10. #include <LibGUI/AbstractView.h>
  11. #include <LibGUI/DragOperation.h>
  12. #include <LibGUI/Model.h>
  13. #include <LibGUI/ModelEditingDelegate.h>
  14. #include <LibGUI/Painter.h>
  15. #include <LibGUI/Scrollbar.h>
  16. #include <LibGUI/TextBox.h>
  17. #include <LibGfx/Palette.h>
  18. namespace GUI {
  19. AbstractView::AbstractView()
  20. : m_sort_order(SortOrder::Ascending)
  21. , m_selection(*this)
  22. {
  23. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  24. }
  25. AbstractView::~AbstractView()
  26. {
  27. if (m_searching_timer)
  28. m_searching_timer->stop();
  29. if (m_model)
  30. m_model->unregister_view({}, *this);
  31. }
  32. void AbstractView::set_model(RefPtr<Model> model)
  33. {
  34. if (model == m_model)
  35. return;
  36. if (m_model)
  37. m_model->unregister_view({}, *this);
  38. m_model = move(model);
  39. if (m_model)
  40. m_model->register_view({}, *this);
  41. model_did_update(GUI::Model::InvalidateAllIndices);
  42. scroll_to_top();
  43. }
  44. void AbstractView::model_did_update(unsigned int flags)
  45. {
  46. if (!model() || (flags & GUI::Model::InvalidateAllIndices)) {
  47. stop_editing();
  48. m_edit_index = {};
  49. m_hovered_index = {};
  50. m_cursor_index = {};
  51. m_drop_candidate_index = {};
  52. clear_selection();
  53. } else {
  54. // FIXME: These may no longer point to whatever they did before,
  55. // but let's be optimistic until we can be sure about it.
  56. if (!model()->is_valid(m_edit_index)) {
  57. stop_editing();
  58. m_edit_index = {};
  59. }
  60. if (!model()->is_valid(m_hovered_index))
  61. m_hovered_index = {};
  62. if (!model()->is_valid(m_cursor_index))
  63. m_cursor_index = {};
  64. if (!model()->is_valid(m_drop_candidate_index))
  65. m_drop_candidate_index = {};
  66. selection().remove_matching([this](auto& index) { return !model()->is_valid(index); });
  67. }
  68. m_selection_start_index = {};
  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::set_selection_start_index(const ModelIndex& new_index)
  79. {
  80. m_selection_start_index = new_index;
  81. }
  82. void AbstractView::add_selection(const ModelIndex& new_index)
  83. {
  84. m_selection.add(new_index);
  85. }
  86. void AbstractView::remove_selection(const ModelIndex& new_index)
  87. {
  88. m_selection.remove(new_index);
  89. }
  90. void AbstractView::toggle_selection(const ModelIndex& new_index)
  91. {
  92. m_selection.toggle(new_index);
  93. }
  94. void AbstractView::did_update_selection()
  95. {
  96. if (!model() || selection().first() != m_edit_index)
  97. stop_editing();
  98. if (model() && on_selection && selection().first().is_valid())
  99. on_selection(selection().first());
  100. }
  101. void AbstractView::did_scroll()
  102. {
  103. update_edit_widget_position();
  104. }
  105. void AbstractView::update_edit_widget_position()
  106. {
  107. if (!m_edit_widget)
  108. return;
  109. m_edit_widget->set_relative_rect(m_edit_widget_content_rect.translated(-horizontal_scrollbar().value(), -vertical_scrollbar().value()));
  110. }
  111. void AbstractView::begin_editing(const ModelIndex& index)
  112. {
  113. VERIFY(is_editable());
  114. VERIFY(model());
  115. if (m_edit_index == index)
  116. return;
  117. if (!model()->is_editable(index))
  118. return;
  119. if (m_edit_widget) {
  120. remove_child(*m_edit_widget);
  121. m_edit_widget = nullptr;
  122. }
  123. m_edit_index = index;
  124. VERIFY(aid_create_editing_delegate);
  125. m_editing_delegate = aid_create_editing_delegate(index);
  126. m_editing_delegate->bind(*model(), index);
  127. m_editing_delegate->set_value(index.data());
  128. m_edit_widget = m_editing_delegate->widget();
  129. add_child(*m_edit_widget);
  130. m_edit_widget->move_to_back();
  131. m_edit_widget_content_rect = content_rect(index).translated(frame_thickness(), frame_thickness());
  132. update_edit_widget_position();
  133. m_edit_widget->set_focus(true);
  134. m_editing_delegate->will_begin_editing();
  135. m_editing_delegate->on_commit = [this] {
  136. VERIFY(model());
  137. model()->set_data(m_edit_index, m_editing_delegate->value());
  138. stop_editing();
  139. };
  140. m_editing_delegate->on_rollback = [this] {
  141. VERIFY(model());
  142. stop_editing();
  143. };
  144. }
  145. void AbstractView::stop_editing()
  146. {
  147. bool take_back_focus = false;
  148. m_edit_index = {};
  149. if (m_edit_widget) {
  150. take_back_focus = m_edit_widget->is_focused();
  151. remove_child(*m_edit_widget);
  152. m_edit_widget = nullptr;
  153. }
  154. if (take_back_focus)
  155. set_focus(true);
  156. }
  157. void AbstractView::activate(const ModelIndex& index)
  158. {
  159. if (on_activation)
  160. on_activation(index);
  161. }
  162. void AbstractView::activate_selected()
  163. {
  164. if (!on_activation)
  165. return;
  166. selection().for_each_index([this](auto& index) {
  167. on_activation(index);
  168. });
  169. }
  170. void AbstractView::notify_selection_changed(Badge<ModelSelection>)
  171. {
  172. did_update_selection();
  173. if (on_selection_change)
  174. on_selection_change();
  175. if (!m_suppress_update_on_selection_change)
  176. update();
  177. }
  178. NonnullRefPtr<Gfx::Font> AbstractView::font_for_index(const ModelIndex& index) const
  179. {
  180. if (!model())
  181. return font();
  182. auto font_data = index.data(ModelRole::Font);
  183. if (font_data.is_font())
  184. return font_data.as_font();
  185. return font();
  186. }
  187. void AbstractView::mousedown_event(MouseEvent& event)
  188. {
  189. AbstractScrollableWidget::mousedown_event(event);
  190. if (!model())
  191. return;
  192. if (event.button() == MouseButton::Left)
  193. m_left_mousedown_position = event.position();
  194. auto index = index_at_event_position(event.position());
  195. m_might_drag = false;
  196. if (!index.is_valid()) {
  197. clear_selection();
  198. } else if (event.modifiers() & Mod_Ctrl) {
  199. set_cursor(index, SelectionUpdate::Ctrl);
  200. } else if (event.modifiers() & Mod_Shift) {
  201. set_cursor(index, SelectionUpdate::Shift);
  202. } else if (event.button() == MouseButton::Left && m_selection.contains(index) && !m_model->drag_data_type().is_null()) {
  203. // We might be starting a drag, so don't throw away other selected items yet.
  204. m_might_drag = true;
  205. } else if (event.button() == MouseButton::Right) {
  206. set_cursor(index, SelectionUpdate::ClearIfNotSelected);
  207. } else {
  208. set_cursor(index, SelectionUpdate::Set);
  209. m_might_drag = true;
  210. }
  211. update();
  212. }
  213. void AbstractView::set_hovered_index(const ModelIndex& index)
  214. {
  215. if (m_hovered_index == index)
  216. return;
  217. auto old_index = m_hovered_index;
  218. m_hovered_index = index;
  219. did_change_hovered_index(old_index, index);
  220. update();
  221. }
  222. void AbstractView::leave_event(Core::Event& event)
  223. {
  224. AbstractScrollableWidget::leave_event(event);
  225. set_hovered_index({});
  226. }
  227. void AbstractView::mousemove_event(MouseEvent& event)
  228. {
  229. if (!model())
  230. return AbstractScrollableWidget::mousemove_event(event);
  231. auto hovered_index = index_at_event_position(event.position());
  232. set_hovered_index(hovered_index);
  233. auto data_type = m_model->drag_data_type();
  234. if (data_type.is_null())
  235. return AbstractScrollableWidget::mousemove_event(event);
  236. if (!m_might_drag)
  237. return AbstractScrollableWidget::mousemove_event(event);
  238. if (!(event.buttons() & MouseButton::Left) || m_selection.is_empty()) {
  239. m_might_drag = false;
  240. return AbstractScrollableWidget::mousemove_event(event);
  241. }
  242. auto diff = event.position() - m_left_mousedown_position;
  243. auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
  244. constexpr int drag_distance_threshold = 5;
  245. if (distance_travelled_squared <= drag_distance_threshold)
  246. return AbstractScrollableWidget::mousemove_event(event);
  247. VERIFY(!data_type.is_null());
  248. if (m_is_dragging)
  249. return;
  250. // An event might sneak in between us constructing the drag operation and the
  251. // event loop exec at the end of `drag_operation->exec()' if the user is fast enough.
  252. // Prevent this by just ignoring later drag initiations (until the current drag operation ends).
  253. TemporaryChange dragging { m_is_dragging, true };
  254. dbgln("Initiate drag!");
  255. auto drag_operation = DragOperation::construct();
  256. drag_operation->set_mime_data(m_model->mime_data(m_selection));
  257. auto outcome = drag_operation->exec();
  258. switch (outcome) {
  259. case DragOperation::Outcome::Accepted:
  260. dbgln("Drag was accepted!");
  261. break;
  262. case DragOperation::Outcome::Cancelled:
  263. dbgln("Drag was cancelled!");
  264. break;
  265. default:
  266. VERIFY_NOT_REACHED();
  267. break;
  268. }
  269. }
  270. void AbstractView::mouseup_event(MouseEvent& event)
  271. {
  272. AbstractScrollableWidget::mouseup_event(event);
  273. if (!model())
  274. return;
  275. if (m_might_drag) {
  276. // We were unsure about unselecting items other than the current one
  277. // in mousedown_event(), because we could be seeing a start of a drag.
  278. // Since we're here, it was not that; so fix up the selection now.
  279. auto index = index_at_event_position(event.position());
  280. if (index.is_valid()) {
  281. set_selection(index);
  282. set_selection_start_index(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. return;
  302. }
  303. if (!m_selection.contains(index))
  304. set_selection(index);
  305. if (is_editable() && edit_triggers() & EditTrigger::DoubleClicked)
  306. begin_editing(cursor_index());
  307. else
  308. activate(cursor_index());
  309. }
  310. void AbstractView::context_menu_event(ContextMenuEvent& event)
  311. {
  312. if (!model())
  313. return;
  314. auto index = index_at_event_position(event.position());
  315. if (index.is_valid())
  316. add_selection(index);
  317. else
  318. clear_selection();
  319. if (on_context_menu_request)
  320. on_context_menu_request(index, event);
  321. }
  322. void AbstractView::drop_event(DropEvent& event)
  323. {
  324. event.accept();
  325. if (!model())
  326. return;
  327. auto index = index_at_event_position(event.position());
  328. if (on_drop)
  329. on_drop(index, event);
  330. }
  331. void AbstractView::set_selection_mode(SelectionMode selection_mode)
  332. {
  333. if (m_selection_mode == selection_mode)
  334. return;
  335. m_selection_mode = selection_mode;
  336. if (m_selection_mode == SelectionMode::NoSelection)
  337. m_selection.clear();
  338. else if (m_selection_mode != SelectionMode::SingleSelection && m_selection.size() > 1) {
  339. auto first_selected = m_selection.first();
  340. m_selection.clear();
  341. m_selection.set(first_selected);
  342. }
  343. update();
  344. }
  345. void AbstractView::set_key_column_and_sort_order(int column, SortOrder sort_order)
  346. {
  347. m_key_column = column;
  348. m_sort_order = sort_order;
  349. if (model())
  350. model()->sort(column, sort_order);
  351. update();
  352. }
  353. void AbstractView::set_cursor(ModelIndex index, SelectionUpdate selection_update, bool scroll_cursor_into_view)
  354. {
  355. if (!model() || !index.is_valid() || selection_mode() == SelectionMode::NoSelection) {
  356. m_cursor_index = {};
  357. cancel_searching();
  358. return;
  359. }
  360. if (!m_cursor_index.is_valid() || model()->parent_index(m_cursor_index) != model()->parent_index(index))
  361. cancel_searching();
  362. if (selection_mode() == SelectionMode::SingleSelection && (selection_update == SelectionUpdate::Ctrl || selection_update == SelectionUpdate::Shift))
  363. selection_update = SelectionUpdate::Set;
  364. if (model()->is_valid(index)) {
  365. if (selection_update == SelectionUpdate::Set) {
  366. set_selection(index);
  367. set_selection_start_index(index);
  368. } else if (selection_update == SelectionUpdate::Ctrl) {
  369. toggle_selection(index);
  370. } else if (selection_update == SelectionUpdate::ClearIfNotSelected) {
  371. if (!m_selection.contains(index))
  372. clear_selection();
  373. } else if (selection_update == SelectionUpdate::Shift) {
  374. auto min_row = min(selection_start_index().row(), index.row());
  375. auto max_row = max(selection_start_index().row(), index.row());
  376. auto min_column = min(selection_start_index().column(), index.column());
  377. auto max_column = max(selection_start_index().column(), index.column());
  378. clear_selection();
  379. for (auto row = min_row; row <= max_row; ++row) {
  380. for (auto column = min_column; column <= max_column; ++column) {
  381. auto new_index = model()->index(row, column);
  382. if (new_index.is_valid())
  383. toggle_selection(new_index);
  384. }
  385. }
  386. }
  387. // FIXME: Support the other SelectionUpdate types
  388. auto old_cursor_index = m_cursor_index;
  389. m_cursor_index = index;
  390. did_change_cursor_index(old_cursor_index, m_cursor_index);
  391. if (scroll_cursor_into_view)
  392. scroll_into_view(index, true, true);
  393. update();
  394. }
  395. }
  396. void AbstractView::set_edit_triggers(unsigned triggers)
  397. {
  398. m_edit_triggers = triggers;
  399. }
  400. void AbstractView::hide_event(HideEvent& event)
  401. {
  402. stop_editing();
  403. AbstractScrollableWidget::hide_event(event);
  404. }
  405. void AbstractView::keydown_event(KeyEvent& event)
  406. {
  407. if (event.alt()) {
  408. event.ignore();
  409. return;
  410. }
  411. if (event.key() == KeyCode::Key_F2) {
  412. if (is_editable() && edit_triggers() & EditTrigger::EditKeyPressed) {
  413. begin_editing(cursor_index());
  414. event.accept();
  415. return;
  416. }
  417. }
  418. if (event.key() == KeyCode::Key_Return) {
  419. activate_selected();
  420. event.accept();
  421. return;
  422. }
  423. SelectionUpdate selection_update = SelectionUpdate::Set;
  424. if (event.modifiers() == KeyModifier::Mod_Shift) {
  425. selection_update = SelectionUpdate::Shift;
  426. }
  427. if (event.key() == KeyCode::Key_Left) {
  428. move_cursor(CursorMovement::Left, selection_update);
  429. event.accept();
  430. return;
  431. }
  432. if (event.key() == KeyCode::Key_Right) {
  433. move_cursor(CursorMovement::Right, selection_update);
  434. event.accept();
  435. return;
  436. }
  437. if (event.key() == KeyCode::Key_Up) {
  438. move_cursor(CursorMovement::Up, selection_update);
  439. event.accept();
  440. return;
  441. }
  442. if (event.key() == KeyCode::Key_Down) {
  443. move_cursor(CursorMovement::Down, selection_update);
  444. event.accept();
  445. return;
  446. }
  447. if (event.key() == KeyCode::Key_Home) {
  448. move_cursor(CursorMovement::Home, selection_update);
  449. event.accept();
  450. return;
  451. }
  452. if (event.key() == KeyCode::Key_End) {
  453. move_cursor(CursorMovement::End, selection_update);
  454. event.accept();
  455. return;
  456. }
  457. if (event.key() == KeyCode::Key_PageUp) {
  458. move_cursor(CursorMovement::PageUp, selection_update);
  459. event.accept();
  460. return;
  461. }
  462. if (event.key() == KeyCode::Key_PageDown) {
  463. move_cursor(CursorMovement::PageDown, selection_update);
  464. event.accept();
  465. return;
  466. }
  467. if (is_searchable()) {
  468. if (event.key() == KeyCode::Key_Backspace) {
  469. if (is_searching()) {
  470. //if (event.modifiers() == Mod_Ctrl) {
  471. // TODO: delete last word
  472. //}
  473. Utf8View view(m_searching);
  474. size_t n_code_points = view.length();
  475. if (n_code_points > 1) {
  476. n_code_points--;
  477. StringBuilder sb;
  478. for (auto it = view.begin(); it != view.end(); ++it) {
  479. if (n_code_points == 0)
  480. break;
  481. n_code_points--;
  482. sb.append_code_point(*it);
  483. }
  484. do_search(sb.to_string());
  485. start_searching_timer();
  486. } else {
  487. cancel_searching();
  488. }
  489. event.accept();
  490. return;
  491. }
  492. } else if (event.key() == KeyCode::Key_Escape) {
  493. if (is_searching()) {
  494. cancel_searching();
  495. event.accept();
  496. return;
  497. }
  498. } else if (event.key() != KeyCode::Key_Tab && !event.ctrl() && !event.alt() && event.code_point() != 0) {
  499. StringBuilder sb;
  500. sb.append(m_searching);
  501. sb.append_code_point(event.code_point());
  502. do_search(sb.to_string());
  503. start_searching_timer();
  504. event.accept();
  505. return;
  506. }
  507. }
  508. AbstractScrollableWidget::keydown_event(event);
  509. }
  510. void AbstractView::cancel_searching()
  511. {
  512. m_searching = nullptr;
  513. if (m_searching_timer)
  514. m_searching_timer->stop();
  515. if (m_highlighted_search_index.is_valid()) {
  516. m_highlighted_search_index = {};
  517. update();
  518. }
  519. }
  520. void AbstractView::start_searching_timer()
  521. {
  522. if (!m_searching_timer) {
  523. m_searching_timer = add<Core::Timer>();
  524. m_searching_timer->set_single_shot(true);
  525. m_searching_timer->on_timeout = [this] {
  526. cancel_searching();
  527. };
  528. }
  529. m_searching_timer->set_interval(5 * 1000);
  530. m_searching_timer->restart();
  531. }
  532. void AbstractView::do_search(String&& searching)
  533. {
  534. if (searching.is_empty() || !model()) {
  535. cancel_searching();
  536. return;
  537. }
  538. auto found_indices = model()->matches(searching, Model::MatchesFlag::FirstMatchOnly | Model::MatchesFlag::MatchAtStart | Model::MatchesFlag::CaseInsensitive, model()->parent_index(cursor_index()));
  539. if (!found_indices.is_empty() && found_indices[0].is_valid()) {
  540. auto& index = found_indices[0];
  541. m_highlighted_search_index = index;
  542. m_searching = move(searching);
  543. set_selection(index);
  544. scroll_into_view(index);
  545. update();
  546. }
  547. }
  548. bool AbstractView::is_searchable() const
  549. {
  550. if (!m_searchable || !model())
  551. return false;
  552. return model()->is_searchable();
  553. }
  554. void AbstractView::set_searchable(bool searchable)
  555. {
  556. if (m_searchable == searchable)
  557. return;
  558. m_searchable = searchable;
  559. if (!m_searchable)
  560. cancel_searching();
  561. }
  562. bool AbstractView::is_highlighting_searching(const ModelIndex& index) const
  563. {
  564. return index == m_highlighted_search_index;
  565. }
  566. void AbstractView::draw_item_text(Gfx::Painter& painter, const ModelIndex& index, bool is_selected, const Gfx::IntRect& text_rect, const StringView& item_text, const Gfx::Font& font, Gfx::TextAlignment alignment, Gfx::TextElision elision)
  567. {
  568. Color text_color;
  569. if (is_selected)
  570. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  571. else
  572. text_color = index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
  573. if (is_highlighting_searching(index)) {
  574. Utf8View searching_text(searching());
  575. auto searching_length = searching_text.length();
  576. // Highlight the text background first
  577. painter.draw_text([&](const Gfx::IntRect& rect, u32) {
  578. if (searching_length > 0) {
  579. searching_length--;
  580. painter.fill_rect(rect.inflated(0, 2), palette().highlight_searching());
  581. }
  582. },
  583. text_rect, item_text, font, alignment, elision);
  584. // Then draw the text
  585. auto highlight_text_color = palette().highlight_searching_text();
  586. searching_length = searching_text.length();
  587. painter.draw_text([&](const Gfx::IntRect& rect, u32 code_point) {
  588. if (searching_length > 0) {
  589. searching_length--;
  590. painter.draw_glyph_or_emoji(rect.location(), code_point, font, highlight_text_color);
  591. } else {
  592. painter.draw_glyph_or_emoji(rect.location(), code_point, font, text_color);
  593. }
  594. },
  595. text_rect, item_text, font, alignment, elision);
  596. } else {
  597. if (m_draw_item_text_with_shadow) {
  598. painter.draw_text(text_rect.translated(1, 1), item_text, font, alignment, Color::Black, elision);
  599. painter.draw_text(text_rect, item_text, font, alignment, Color::White, elision);
  600. } else {
  601. painter.draw_text(text_rect, item_text, font, alignment, text_color, elision);
  602. }
  603. }
  604. }
  605. void AbstractView::focusin_event(FocusEvent& event)
  606. {
  607. AbstractScrollableWidget::focusin_event(event);
  608. if (model() && !cursor_index().is_valid()) {
  609. move_cursor(CursorMovement::Home, SelectionUpdate::None);
  610. clear_selection();
  611. }
  612. }
  613. void AbstractView::drag_enter_event(DragEvent& event)
  614. {
  615. if (!model())
  616. return;
  617. // NOTE: Right now, AbstractView always accepts drags since we won't get "drag move" events
  618. // unless we accept the "drag enter" event.
  619. // We might be able to reduce event traffic by communicating the set of drag-accepting
  620. // rects in this widget to the windowing system somehow.
  621. event.accept();
  622. dbgln("accepting drag of {}", event.mime_types().first());
  623. }
  624. void AbstractView::drag_move_event(DragEvent& event)
  625. {
  626. if (!model())
  627. return;
  628. auto index = index_at_event_position(event.position());
  629. ModelIndex new_drop_candidate_index;
  630. if (index.is_valid()) {
  631. bool acceptable = model()->accepts_drag(index, event.mime_types());
  632. if (acceptable)
  633. new_drop_candidate_index = index;
  634. }
  635. if (m_drop_candidate_index != new_drop_candidate_index) {
  636. m_drop_candidate_index = new_drop_candidate_index;
  637. update();
  638. }
  639. if (m_drop_candidate_index.is_valid())
  640. event.accept();
  641. }
  642. void AbstractView::drag_leave_event(Event&)
  643. {
  644. if (m_drop_candidate_index.is_valid()) {
  645. m_drop_candidate_index = {};
  646. update();
  647. }
  648. }
  649. }