File indexing completed on 2024-05-19 05:35:25

0001 //////////////////////////////////////////////////////////////////////////////
0002 // oxygenstackedwidgetdata.cpp
0003 // data container for QStackedWidget transition
0004 // -------------------
0005 //
0006 // SPDX-FileCopyrightText: 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0007 //
0008 // SPDX-License-Identifier: MIT
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #include "oxygenstackedwidgetdata.h"
0012 
0013 namespace Oxygen
0014 {
0015 //______________________________________________________
0016 StackedWidgetData::StackedWidgetData(QObject *parent, QStackedWidget *target, int duration)
0017     : TransitionData(parent, target, duration)
0018     , _target(target)
0019     , _index(target->currentIndex())
0020 {
0021     // configure transition
0022     connect(_target.data(), SIGNAL(destroyed()), SLOT(targetDestroyed()));
0023     connect(_target.data(), SIGNAL(currentChanged(int)), SLOT(animate()));
0024 
0025     // disable focus
0026     transition().data()->setAttribute(Qt::WA_NoMousePropagation, true);
0027     transition().data()->setFlag(TransitionWidget::PaintOnWidget, true);
0028 
0029     setMaxRenderTime(50);
0030 }
0031 
0032 //___________________________________________________________________
0033 bool StackedWidgetData::initializeAnimation(void)
0034 {
0035     // check enability
0036     if (!(_target && _target.data()->isVisible())) {
0037         return false;
0038     }
0039 
0040     // check index
0041     if (_target.data()->currentIndex() == _index) {
0042         return false;
0043     }
0044 
0045     // do not animate if either index or currentIndex is not valid
0046     // but update _index none the less
0047     if (_target.data()->currentIndex() < 0 || _index < 0) {
0048         _index = _target.data()->currentIndex();
0049         return false;
0050     }
0051 
0052     // get old widget (matching _index) and initialize transition
0053     if (QWidget *widget = _target.data()->widget(_index)) {
0054         transition().data()->setOpacity(0);
0055         startClock();
0056         transition().data()->setGeometry(widget->geometry());
0057         transition().data()->setStartPixmap(transition().data()->grab(widget));
0058 
0059         _index = _target.data()->currentIndex();
0060         return !slow();
0061 
0062     } else {
0063         _index = _target.data()->currentIndex();
0064         return false;
0065     }
0066 }
0067 
0068 //___________________________________________________________________
0069 bool StackedWidgetData::animate(void)
0070 {
0071     // check enability
0072     if (!enabled())
0073         return false;
0074 
0075     // initialize animation
0076     if (!initializeAnimation())
0077         return false;
0078 
0079     // show transition widget
0080     transition().data()->show();
0081     transition().data()->raise();
0082     transition().data()->animate();
0083     return true;
0084 }
0085 
0086 //___________________________________________________________________
0087 void StackedWidgetData::finishAnimation(void)
0088 {
0089     // disable updates on currentWidget
0090     if (_target && _target.data()->currentWidget()) {
0091         _target.data()->currentWidget()->setUpdatesEnabled(false);
0092     }
0093 
0094     // hide transition
0095     transition().data()->hide();
0096 
0097     // reenable updates and repaint
0098     if (_target && _target.data()->currentWidget()) {
0099         _target.data()->currentWidget()->setUpdatesEnabled(true);
0100         _target.data()->currentWidget()->repaint();
0101     }
0102 
0103     // invalidate start widget
0104     transition().data()->resetStartPixmap();
0105 }
0106 
0107 //___________________________________________________________________
0108 void StackedWidgetData::targetDestroyed(void)
0109 {
0110     setEnabled(false);
0111     _target.clear();
0112 }
0113 }