LeechCraft 0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
viewselectiontracker.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
10#include <QAbstractItemView>
11#include <QItemSelectionModel>
12#include <QMouseEvent>
13#include <QTimer>
14
15namespace LC::Util
16{
17 ViewSelectionTracker::ViewSelectionTracker (QAbstractItemView& view, QObject* parent)
18 : QObject { parent }
19 , View_ { view }
20 {
21 View_.viewport ()->installEventFilter (this);
22
23 const auto sm = view.selectionModel ();
24 connect (sm,
25 &QItemSelectionModel::selectionChanged,
26 this,
27 &ViewSelectionTracker::HandleSelectionChange);
28 connect (sm,
29 &QItemSelectionModel::currentRowChanged,
30 this,
31 &ViewSelectionTracker::HandleSelectionChange);
32 connect (view.model (),
33 &QAbstractItemModel::modelReset,
34 this,
35 &ViewSelectionTracker::HandleSelectionChange);
36 }
37
38 bool ViewSelectionTracker::eventFilter (QObject*, QEvent *ev)
39 {
40 switch (ev->type ())
41 {
42 case QEvent::MouseButtonDblClick:
43 case QEvent::MouseButtonPress:
44 if (static_cast<QMouseEvent*> (ev)->button () == Qt::LeftButton)
45 {
46 GestureActive_ = true;
47 emit gestureStarted ();
48 }
49 break;
50 case QEvent::MouseButtonRelease:
51 if (static_cast<QMouseEvent*> (ev)->button () == Qt::LeftButton)
52 EndGesture ();
53 break;
54 case QEvent::UngrabMouse:
55 EndGesture ();
56 break;
57 default:
58 break;
59 }
60
61 return false;
62 }
63
65 {
66 if (std::exchange (GestureActive_, false))
67 ScheduleSyncToSelection ();
68 }
69
70 void ViewSelectionTracker::HandleSelectionChange ()
71 {
73
74 SelectionDirty_ = true;
75 ScheduleSyncToSelection ();
76 }
77
78 void ViewSelectionTracker::ScheduleSyncToSelection ()
79 {
80 if (!std::exchange (ScheduledSyncToSelection_, true))
81 QTimer::singleShot (0, this, &ViewSelectionTracker::SyncToSelection);
82 }
83
84 void ViewSelectionTracker::SyncToSelection ()
85 {
86 if (!std::exchange (ScheduledSyncToSelection_, false))
87 return;
88
89 if (std::exchange (SelectionDirty_, false))
90 emit selectionChanging ();
91
92 if (GestureActive_)
93 return;
94
95 const auto sm = View_.selectionModel ();
96 emit selectionSettled (sm->selectedRows (), sm->currentIndex ());
97 }
98}
void selectionSettled(const QModelIndexList &rows, const QModelIndex &current)
Emitted from a clean stack once the selection has settled.
bool eventFilter(QObject *, QEvent *) override
void gestureStarted()
Emitted on a left mouse button press on the view's viewport.
ViewSelectionTracker(QAbstractItemView &, QObject *=nullptr)
void selectionChanging()
Emitted from a clean stack whenever the selection or the current row changes, or the model is reset.
void EndGesture()
Ends the current left mouse button gesture, if any.
void selectionStartedChanging()
Emitted synchronously when a change is detected that selectionChanging() will report.