Qt ui: improve toggle buttons (#2639)
	
		
	
				
					
				
			* works, but is still a bit ugly
* works
* looks much better now
* fix compile error
* reduce diff
* write to params
* cleanup
old-commit-hash: 61884d2c50
			
			
				vw-mqb-aeb
			
			
		
							parent
							
								
									19855c90f1
								
							
						
					
					
						commit
						3bfc92bafc
					
				
				 4 changed files with 124 additions and 14 deletions
			
			
		| @ -0,0 +1,66 @@ | |||||||
|  | #include "toggle.hpp" | ||||||
|  | 
 | ||||||
|  | #include <QAbstractButton> | ||||||
|  | #include <QPropertyAnimation> | ||||||
|  | #include <QWidget> | ||||||
|  | #include <QDebug> | ||||||
|  | #include "common/params.h"  | ||||||
|  | 
 | ||||||
|  | Toggle::Toggle(QWidget *parent) : QAbstractButton(parent), | ||||||
|  | _height(60), | ||||||
|  | _height_rect(45), | ||||||
|  | _on(false), | ||||||
|  | _anim(new QPropertyAnimation(this, "offset_circle", this)) | ||||||
|  | { | ||||||
|  |   _radius = _height / 2; | ||||||
|  |   _x_circle = _radius; | ||||||
|  |   _y_circle = _radius; | ||||||
|  |   _y_rect = (_height - _height_rect)/2; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | void Toggle::paintEvent(QPaintEvent *e) { | ||||||
|  |   this->setFixedHeight(_height); | ||||||
|  |   QPainter p(this); | ||||||
|  |   p.setPen(Qt::NoPen); | ||||||
|  |   p.setRenderHint(QPainter::Antialiasing, true); | ||||||
|  | 
 | ||||||
|  |   // Draw toggle background left
 | ||||||
|  |   p.setBrush(QColor("#33ab4c")); | ||||||
|  |   p.drawRoundedRect(QRect(0, _y_rect, _x_circle + _radius, _height_rect), _height_rect/2, _height_rect/2); | ||||||
|  |   // Draw toggle background right
 | ||||||
|  |   p.setBrush(QColor("#0a1a26")); | ||||||
|  |   p.drawRoundedRect(QRect(_x_circle - _radius, _y_rect, width() -(_x_circle - _radius), _height_rect), _height_rect/2, _height_rect/2); | ||||||
|  | 
 | ||||||
|  |   // Draw toggle circle
 | ||||||
|  |   p.setBrush(QColor("#fafafa")); | ||||||
|  |   p.drawEllipse(QRectF(_x_circle - _radius, _y_circle - _radius, 2 * _radius, 2 * _radius)); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void Toggle::mouseReleaseEvent(QMouseEvent *e) { | ||||||
|  |   if (e->button() & Qt::LeftButton) { | ||||||
|  |     togglePosition(); | ||||||
|  |     emit stateChanged(_on); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void Toggle::togglePosition(){ | ||||||
|  |   _on = !_on; | ||||||
|  |   if (_on) { | ||||||
|  |     _anim->setStartValue(_radius); | ||||||
|  |     _anim->setEndValue(width() - _radius); | ||||||
|  |     _anim->setDuration(120); | ||||||
|  |     _anim->start(); | ||||||
|  |   } else { | ||||||
|  |     _anim->setStartValue(width() - _radius); | ||||||
|  |     _anim->setEndValue(_radius); | ||||||
|  |     _anim->setDuration(120); | ||||||
|  |     _anim->start(); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void Toggle::enterEvent(QEvent *e) { | ||||||
|  |   setCursor(Qt::PointingHandCursor); | ||||||
|  |   QAbstractButton::enterEvent(e); | ||||||
|  | } | ||||||
| @ -0,0 +1,34 @@ | |||||||
|  | #pragma once | ||||||
|  | #include <QtWidgets> | ||||||
|  | 
 | ||||||
|  | class Toggle : public QAbstractButton { | ||||||
|  |     Q_OBJECT | ||||||
|  |     Q_PROPERTY(int offset_circle READ offset_circle WRITE set_offset_circle) | ||||||
|  | 
 | ||||||
|  | public: | ||||||
|  |     Toggle(QWidget* parent = nullptr); | ||||||
|  |     void togglePosition();//Toggles the toggle
 | ||||||
|  | 
 | ||||||
|  |     int offset_circle() const { | ||||||
|  |         return _x_circle; | ||||||
|  |     } | ||||||
|  |     void set_offset_circle(int o) { | ||||||
|  |         _x_circle = o; | ||||||
|  |         update(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | protected: | ||||||
|  |     void paintEvent(QPaintEvent*) override; | ||||||
|  |     void mouseReleaseEvent(QMouseEvent*) override; | ||||||
|  |     void enterEvent(QEvent*) override; | ||||||
|  | 
 | ||||||
|  | private: | ||||||
|  |     bool _on; | ||||||
|  |     int _x_circle, _y_circle; | ||||||
|  |     int _height, _radius; | ||||||
|  |     int _height_rect, _y_rect; | ||||||
|  |     QPropertyAnimation *_anim = nullptr; | ||||||
|  | 
 | ||||||
|  | signals: | ||||||
|  |   void stateChanged(int new_state); | ||||||
|  | }; | ||||||
					Loading…
					
					
				
		Reference in new issue