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: ElasticHook(int hook_threshold)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*819e4a32SNicolas Bonnefon // Hold the elastic and prevent automatic decrease. hold()41*819e4a32SNicolas Bonnefon void hold() { held_ = true; } 42*819e4a32SNicolas Bonnefon 43*819e4a32SNicolas Bonnefon // Release the elastic. release()44*819e4a32SNicolas Bonnefon void release() { held_ = false; } 45*819e4a32SNicolas Bonnefon 46b297d2f4SNicolas Bonnefon // Programmatically force the hook hooked or not. hook(bool hooked)47b297d2f4SNicolas Bonnefon void hook( bool hooked ) 48b297d2f4SNicolas Bonnefon { hooked_ = hooked; } 49b297d2f4SNicolas Bonnefon 50b297d2f4SNicolas Bonnefon // Return the "length" of the elastic hook. length()510b05c6eaSNicolas Bonnefon int length() const { return position_; } isHooked()52ac6602a5SNicolas Bonnefon bool isHooked() const { return hooked_; } 530b05c6eaSNicolas Bonnefon 540b05c6eaSNicolas Bonnefon protected: 550b05c6eaSNicolas Bonnefon void timerEvent( QTimerEvent *event ); 560b05c6eaSNicolas Bonnefon 570b05c6eaSNicolas Bonnefon signals: 580b05c6eaSNicolas Bonnefon // Sent when the length has changed 590b05c6eaSNicolas Bonnefon void lengthChanged(); 600b05c6eaSNicolas Bonnefon // Sent when the hooked status has changed 610b05c6eaSNicolas Bonnefon void hooked( bool is_hooked ); 620b05c6eaSNicolas Bonnefon 630b05c6eaSNicolas Bonnefon private: 64a9448ba0SNicolas Bonnefon void decreasePosition(); 65a9448ba0SNicolas Bonnefon 660b05c6eaSNicolas Bonnefon static constexpr int TIMER_PERIOD_MS = 10; 67a9448ba0SNicolas Bonnefon static constexpr int DECREASE_RATE = 4; 68ac6602a5SNicolas Bonnefon const int hook_threshold_; 69ac6602a5SNicolas Bonnefon bool hooked_ = false; 70*819e4a32SNicolas Bonnefon bool held_ = false; 710b05c6eaSNicolas Bonnefon int position_ = 0; 720b05c6eaSNicolas Bonnefon int timer_id_ = 0; 73a9448ba0SNicolas Bonnefon std::chrono::time_point<std::chrono::steady_clock> last_update_; 740b05c6eaSNicolas Bonnefon }; 750b05c6eaSNicolas Bonnefon 760b05c6eaSNicolas Bonnefon #endif 77