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: ElasticHook(int hook_threshold)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 // Hold the elastic and prevent automatic decrease. hold()41 void hold() { held_ = true; } 42 43 // Release the elastic. release()44 void release() { held_ = false; } 45 46 // Programmatically force the hook hooked or not. hook(bool hooked)47 void hook( bool hooked ) 48 { hooked_ = hooked; } 49 50 // Return the "length" of the elastic hook. length()51 int length() const { return position_; } isHooked()52 bool isHooked() const { return hooked_; } 53 54 protected: 55 void timerEvent( QTimerEvent *event ); 56 57 signals: 58 // Sent when the length has changed 59 void lengthChanged(); 60 // Sent when the hooked status has changed 61 void hooked( bool is_hooked ); 62 63 private: 64 void decreasePosition(); 65 66 static constexpr int TIMER_PERIOD_MS = 10; 67 static constexpr int DECREASE_RATE = 4; 68 const int hook_threshold_; 69 bool hooked_ = false; 70 bool held_ = false; 71 int position_ = 0; 72 int timer_id_ = 0; 73 std::chrono::time_point<std::chrono::steady_clock> last_update_; 74 }; 75 76 #endif 77