summaryrefslogtreecommitdiff
path: root/qmap/decode_click_coalescer.cpp
blob: b1c2725e99bc2997f398153f712f9c950a6fb6c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "decode_click_coalescer.h"

#include <utility>

#include <QApplication>
#include <QObject>

DecodeClickCoalescer::DecodeClickCoalescer (QObject * context, Dispatch dispatch, int interval)
  : dispatch_ {std::move (dispatch)}
{
  timer_.setSingleShot (true);
  timer_.setTimerType (Qt::PreciseTimer);
  timer_.setInterval (interval >= 0 ? interval : QApplication::doubleClickInterval ());
  QObject::connect (&timer_, &QTimer::timeout, context, [this] { dispatchPending (); });
}

void DecodeClickCoalescer::press (QString const& identity, QByteArray const& decodeRow)
{
  // Qt reports the first press before deciding whether the gesture is a double-click.
  if (timer_.isActive () && pendingIdentity_ != identity) {
    timer_.stop ();
    dispatchPending ();
  }
  dispatch_ (decodeRow, DecodeClickGesture::Press);
  pendingIdentity_ = identity;
  pending_ = decodeRow;
  timer_.start ();
}

void DecodeClickCoalescer::doubleClick (QString const& identity, QByteArray const& decodeRow)
{
  if (timer_.isActive ()) {
    timer_.stop ();
    if (pendingIdentity_ != identity) dispatchPending ();
    else {
      pendingIdentity_.clear ();
      pending_.clear ();
    }
  }
  dispatch_ (decodeRow, DecodeClickGesture::DoubleClick);
}

void DecodeClickCoalescer::dispatchPending ()
{
  if (pending_.isEmpty ()) return;
  auto const decodeRow = pending_;
  pendingIdentity_.clear ();
  pending_.clear ();
  dispatch_ (decodeRow, DecodeClickGesture::SingleClick);
}