xref: /glogg/src/viewtools.h (revision a9448ba0def110c032d88af29b1533da8ef4aaa2)
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 ) {
35         hook_threshold_ = hook_threshold;
36     }
37 
38     // Instruct the elastic to move by the passed pixels
39     // (a positive value increase the elastic tension)
40     void move( int value );
41 
42     int length() const { return position_; }
43 
44   protected:
45     void timerEvent( QTimerEvent *event );
46 
47   signals:
48     // Sent when the length has changed
49     void lengthChanged();
50     // Sent when the hooked status has changed
51     void hooked( bool is_hooked );
52 
53   private:
54     void decreasePosition();
55 
56     static constexpr int TIMER_PERIOD_MS = 10;
57     static constexpr int DECREASE_RATE = 4;
58     int hook_threshold_;
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