xref: /glogg/src/viewtools.h (revision ac6602a5bafa9823a03710ce4fc2a79551a0ce64)
1 /*
2  * Copyright (C) 2015 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef VIEWTOOLS_H
21 #define VIEWTOOLS_H
22 
23 #include <chrono>
24 
25 #include <QObject>
26 
27 // This class is a controller for an elastic hook manipulated with
28 // the mouse wheel or touchpad.
29 // It is used for the "follow" line at the end of the file.
30 class ElasticHook : public QObject {
31   Q_OBJECT
32 
33   public:
34     ElasticHook( int hook_threshold ) : hook_threshold_( hook_threshold ) {}
35 
36     // Instruct the elastic to move by the passed pixels
37     // (a positive value increase the elastic tension)
38     void move( int value );
39 
40     int length() const { return position_; }
41     bool isHooked() const { return hooked_; }
42 
43   protected:
44     void timerEvent( QTimerEvent *event );
45 
46   signals:
47     // Sent when the length has changed
48     void lengthChanged();
49     // Sent when the hooked status has changed
50     void hooked( bool is_hooked );
51 
52   private:
53     void decreasePosition();
54 
55     static constexpr int TIMER_PERIOD_MS = 10;
56     static constexpr int DECREASE_RATE = 4;
57     const int hook_threshold_;
58     bool hooked_  = false;
59     int position_ = 0;
60     int timer_id_ = 0;
61     std::chrono::time_point<std::chrono::steady_clock> last_update_;
62 };
63 
64 #endif
65