xref: /glogg/src/viewtools.h (revision b297d2f4e1a22f57de6661d89a7b26d59bcd56dc)
10b05c6eaSNicolas Bonnefon /*
20b05c6eaSNicolas Bonnefon  * Copyright (C) 2015 Nicolas Bonnefon and other contributors
30b05c6eaSNicolas Bonnefon  *
40b05c6eaSNicolas Bonnefon  * This file is part of glogg.
50b05c6eaSNicolas Bonnefon  *
60b05c6eaSNicolas Bonnefon  * glogg is free software: you can redistribute it and/or modify
70b05c6eaSNicolas Bonnefon  * it under the terms of the GNU General Public License as published by
80b05c6eaSNicolas Bonnefon  * the Free Software Foundation, either version 3 of the License, or
90b05c6eaSNicolas Bonnefon  * (at your option) any later version.
100b05c6eaSNicolas Bonnefon  *
110b05c6eaSNicolas Bonnefon  * glogg is distributed in the hope that it will be useful,
120b05c6eaSNicolas Bonnefon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
130b05c6eaSNicolas Bonnefon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
140b05c6eaSNicolas Bonnefon  * GNU General Public License for more details.
150b05c6eaSNicolas Bonnefon  *
160b05c6eaSNicolas Bonnefon  * You should have received a copy of the GNU General Public License
170b05c6eaSNicolas Bonnefon  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
180b05c6eaSNicolas Bonnefon  */
190b05c6eaSNicolas Bonnefon 
200b05c6eaSNicolas Bonnefon #ifndef VIEWTOOLS_H
210b05c6eaSNicolas Bonnefon #define VIEWTOOLS_H
220b05c6eaSNicolas Bonnefon 
23a9448ba0SNicolas Bonnefon #include <chrono>
24a9448ba0SNicolas Bonnefon 
250b05c6eaSNicolas Bonnefon #include <QObject>
260b05c6eaSNicolas Bonnefon 
270b05c6eaSNicolas Bonnefon // This class is a controller for an elastic hook manipulated with
280b05c6eaSNicolas Bonnefon // the mouse wheel or touchpad.
290b05c6eaSNicolas Bonnefon // It is used for the "follow" line at the end of the file.
300b05c6eaSNicolas Bonnefon class ElasticHook : public QObject {
310b05c6eaSNicolas Bonnefon   Q_OBJECT
320b05c6eaSNicolas Bonnefon 
330b05c6eaSNicolas Bonnefon   public:
34ac6602a5SNicolas Bonnefon     ElasticHook( int hook_threshold ) : hook_threshold_( hook_threshold ) {}
350b05c6eaSNicolas Bonnefon 
360b05c6eaSNicolas Bonnefon     // Instruct the elastic to move by the passed pixels
370b05c6eaSNicolas Bonnefon     // (a positive value increase the elastic tension)
380b05c6eaSNicolas Bonnefon     void move( int value );
390b05c6eaSNicolas Bonnefon 
40*b297d2f4SNicolas Bonnefon     // Programmatically force the hook hooked or not.
41*b297d2f4SNicolas Bonnefon     void hook( bool hooked )
42*b297d2f4SNicolas Bonnefon     { hooked_ = hooked; }
43*b297d2f4SNicolas Bonnefon 
44*b297d2f4SNicolas Bonnefon     // Return the "length" of the elastic hook.
450b05c6eaSNicolas Bonnefon     int length() const { return position_; }
46ac6602a5SNicolas Bonnefon     bool isHooked() const { return hooked_; }
470b05c6eaSNicolas Bonnefon 
480b05c6eaSNicolas Bonnefon   protected:
490b05c6eaSNicolas Bonnefon     void timerEvent( QTimerEvent *event );
500b05c6eaSNicolas Bonnefon 
510b05c6eaSNicolas Bonnefon   signals:
520b05c6eaSNicolas Bonnefon     // Sent when the length has changed
530b05c6eaSNicolas Bonnefon     void lengthChanged();
540b05c6eaSNicolas Bonnefon     // Sent when the hooked status has changed
550b05c6eaSNicolas Bonnefon     void hooked( bool is_hooked );
560b05c6eaSNicolas Bonnefon 
570b05c6eaSNicolas Bonnefon   private:
58a9448ba0SNicolas Bonnefon     void decreasePosition();
59a9448ba0SNicolas Bonnefon 
600b05c6eaSNicolas Bonnefon     static constexpr int TIMER_PERIOD_MS = 10;
61a9448ba0SNicolas Bonnefon     static constexpr int DECREASE_RATE = 4;
62ac6602a5SNicolas Bonnefon     const int hook_threshold_;
63ac6602a5SNicolas Bonnefon     bool hooked_  = false;
640b05c6eaSNicolas Bonnefon     int position_ = 0;
650b05c6eaSNicolas Bonnefon     int timer_id_ = 0;
66a9448ba0SNicolas Bonnefon     std::chrono::time_point<std::chrono::steady_clock> last_update_;
670b05c6eaSNicolas Bonnefon };
680b05c6eaSNicolas Bonnefon 
690b05c6eaSNicolas Bonnefon #endif
70