xref: /glogg/src/viewtools.h (revision a9448ba0def110c032d88af29b1533da8ef4aaa2)
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 
23*a9448ba0SNicolas Bonnefon #include <chrono>
24*a9448ba0SNicolas 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:
340b05c6eaSNicolas Bonnefon     ElasticHook( int hook_threshold ) {
350b05c6eaSNicolas Bonnefon         hook_threshold_ = hook_threshold;
360b05c6eaSNicolas Bonnefon     }
370b05c6eaSNicolas Bonnefon 
380b05c6eaSNicolas Bonnefon     // Instruct the elastic to move by the passed pixels
390b05c6eaSNicolas Bonnefon     // (a positive value increase the elastic tension)
400b05c6eaSNicolas Bonnefon     void move( int value );
410b05c6eaSNicolas Bonnefon 
420b05c6eaSNicolas Bonnefon     int length() const { return position_; }
430b05c6eaSNicolas Bonnefon 
440b05c6eaSNicolas Bonnefon   protected:
450b05c6eaSNicolas Bonnefon     void timerEvent( QTimerEvent *event );
460b05c6eaSNicolas Bonnefon 
470b05c6eaSNicolas Bonnefon   signals:
480b05c6eaSNicolas Bonnefon     // Sent when the length has changed
490b05c6eaSNicolas Bonnefon     void lengthChanged();
500b05c6eaSNicolas Bonnefon     // Sent when the hooked status has changed
510b05c6eaSNicolas Bonnefon     void hooked( bool is_hooked );
520b05c6eaSNicolas Bonnefon 
530b05c6eaSNicolas Bonnefon   private:
54*a9448ba0SNicolas Bonnefon     void decreasePosition();
55*a9448ba0SNicolas Bonnefon 
560b05c6eaSNicolas Bonnefon     static constexpr int TIMER_PERIOD_MS = 10;
57*a9448ba0SNicolas Bonnefon     static constexpr int DECREASE_RATE = 4;
580b05c6eaSNicolas Bonnefon     int hook_threshold_;
590b05c6eaSNicolas Bonnefon     int position_ = 0;
600b05c6eaSNicolas Bonnefon     int timer_id_ = 0;
61*a9448ba0SNicolas Bonnefon     std::chrono::time_point<std::chrono::steady_clock> last_update_;
620b05c6eaSNicolas Bonnefon };
630b05c6eaSNicolas Bonnefon 
640b05c6eaSNicolas Bonnefon #endif
65