xref: /glogg/src/viewtools.h (revision b297d2f4e1a22f57de6661d89a7b26d59bcd56dc)
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     // Programmatically force the hook hooked or not.
41     void hook( bool hooked )
42     { hooked_ = hooked; }
43 
44     // Return the "length" of the elastic hook.
45     int length() const { return position_; }
46     bool isHooked() const { return hooked_; }
47 
48   protected:
49     void timerEvent( QTimerEvent *event );
50 
51   signals:
52     // Sent when the length has changed
53     void lengthChanged();
54     // Sent when the hooked status has changed
55     void hooked( bool is_hooked );
56 
57   private:
58     void decreasePosition();
59 
60     static constexpr int TIMER_PERIOD_MS = 10;
61     static constexpr int DECREASE_RATE = 4;
62     const int hook_threshold_;
63     bool hooked_  = false;
64     int position_ = 0;
65     int timer_id_ = 0;
66     std::chrono::time_point<std::chrono::steady_clock> last_update_;
67 };
68 
69 #endif
70