1 ////////////////////////////////////////////////////////////////////////////// 2 // breezestackedwidgetdata.cpp 3 // data container for QStackedWidget transition 4 // ------------------- 5 // 6 // SPDX-FileCopyrightText: 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr> 7 // 8 // SPDX-License-Identifier: MIT 9 ////////////////////////////////////////////////////////////////////////////// 10 11 #include "breezestackedwidgetdata.h" 12 13 namespace Breeze 14 { 15 16 //______________________________________________________ StackedWidgetData(QObject * parent,QStackedWidget * target,int duration)17 StackedWidgetData::StackedWidgetData( QObject* parent, QStackedWidget* target, int duration ): 18 TransitionData( parent, target, duration ), 19 _target( target ), 20 _index( target->currentIndex() ) 21 { 22 23 // configure transition 24 connect( _target.data(), &QObject::destroyed, this, &StackedWidgetData::targetDestroyed ); 25 connect( _target.data(), SIGNAL(currentChanged(int)), SLOT(animate()) ); 26 27 // disable focus 28 transition().data()->setAttribute(Qt::WA_NoMousePropagation, true); 29 transition().data()->setFlag(TransitionWidget::PaintOnWidget, true); 30 31 setMaxRenderTime( 50 ); 32 33 } 34 35 //___________________________________________________________________ initializeAnimation()36 bool StackedWidgetData::initializeAnimation() 37 { 38 39 // check enability 40 if( !( _target && _target.data()->isVisible() ) ) 41 { return false; } 42 43 // check index 44 if( _target.data()->currentIndex() == _index ) 45 { return false; } 46 47 // do not animate if either index or currentIndex is not valid 48 // but update _index none the less 49 if( _target.data()->currentIndex() < 0 || _index < 0 ) 50 { 51 _index = _target.data()->currentIndex(); 52 return false; 53 } 54 55 // get old widget (matching _index) and initialize transition 56 if( QWidget *widget = _target.data()->widget( _index ) ) 57 { 58 59 transition().data()->setOpacity( 0 ); 60 startClock(); 61 transition().data()->setGeometry( widget->geometry() ); 62 transition().data()->setStartPixmap( transition().data()->grab( widget ) ); 63 64 _index = _target.data()->currentIndex(); 65 return !slow(); 66 67 } else { 68 69 _index = _target.data()->currentIndex(); 70 return false; 71 72 } 73 74 } 75 76 //___________________________________________________________________ animate()77 bool StackedWidgetData::animate() 78 { 79 80 // check enability 81 if( !enabled() ) return false; 82 83 // initialize animation 84 if( !initializeAnimation() ) return false; 85 86 // show transition widget 87 transition().data()->show(); 88 transition().data()->raise(); 89 transition().data()->animate(); 90 return true; 91 92 } 93 94 //___________________________________________________________________ finishAnimation()95 void StackedWidgetData::finishAnimation() 96 { 97 // disable updates on currentWidget 98 if( _target && _target.data()->currentWidget() ) 99 { _target.data()->currentWidget()->setUpdatesEnabled( false ); } 100 101 // hide transition 102 transition().data()->hide(); 103 104 // reenable updates and repaint 105 if( _target && _target.data()->currentWidget() ) 106 { 107 _target.data()->currentWidget()->setUpdatesEnabled( true ); 108 _target.data()->currentWidget()->repaint(); 109 } 110 111 // invalidate start widget 112 transition().data()->resetStartPixmap(); 113 114 } 115 116 //___________________________________________________________________ targetDestroyed()117 void StackedWidgetData::targetDestroyed() 118 { 119 setEnabled( false ); 120 _target.clear(); 121 } 122 123 } 124