File indexing completed on 2024-05-05 04:49:22

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  * Copyright (c) 2005 Max Howell <max.howell@methylblue.com>                            *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017  
0018 #include "LongMessageWidget.h"
0019 
0020 #include "core/support/Debug.h"
0021 
0022 #include <KStandardGuiItem>
0023 
0024 #include <QPushButton>
0025 
0026 #include <QLabel>
0027 #include <QLayout>
0028 #include <QPainter>
0029 #include <QToolTip>
0030 
0031 
0032 LongMessageWidget::LongMessageWidget( const QString &message )
0033         : m_counter( 0 )
0034         , m_timeout( 6000 )
0035 {
0036     DEBUG_BLOCK
0037 
0038     setFrameStyle( QFrame::StyledPanel | QFrame::Raised );
0039 
0040     setContentsMargins( 4, 4, 4, 4 );
0041 
0042     setMinimumWidth( 26 );
0043     setMinimumHeight( 26 );
0044     setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
0045 
0046     QPalette p = QToolTip::palette();
0047     setPalette( p );
0048 
0049     BoxWidget *hbox = new BoxWidget( false, this );
0050     hbox->layout()->setSpacing( 12 );
0051 
0052     m_countdownFrame = new CountdownFrame( hbox );
0053     m_countdownFrame->setObjectName( QStringLiteral("counterVisual") );
0054     m_countdownFrame->setFixedWidth( fontMetrics().horizontalAdvance( QStringLiteral("X") ) );
0055     m_countdownFrame->setFrameStyle( QFrame::Plain | QFrame::Box );
0056     QPalette pal;
0057     pal.setColor( m_countdownFrame->foregroundRole(), p.dark().color() );
0058     m_countdownFrame->setPalette( pal );
0059 
0060     QLabel *alabel = new QLabel( message, hbox );
0061     alabel->setWordWrap( true );
0062     alabel->setOpenExternalLinks( true );
0063     alabel->setObjectName( QStringLiteral("label") );
0064     alabel->setTextFormat( Qt::RichText );
0065     alabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
0066     alabel->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
0067     alabel->setPalette( p );
0068 
0069     hbox = new BoxWidget( false, this );
0070 
0071     QPushButton *button = new QPushButton( hbox );
0072     KStandardGuiItem::assign( button, KStandardGuiItem::Close );
0073     connect( button, &QAbstractButton::clicked, this, &LongMessageWidget::close );
0074 
0075     reposition();
0076 
0077     show();
0078     m_timerId = startTimer( m_timeout / m_countdownFrame->height() );
0079 }
0080 
0081 LongMessageWidget::~LongMessageWidget()
0082 {}
0083 
0084 void LongMessageWidget::close()
0085 {
0086     hide();
0087     Q_EMIT( closed() );
0088 }
0089 
0090 void LongMessageWidget::timerEvent( QTimerEvent* )
0091 {
0092     if( !m_timeout )
0093     {
0094         killTimer( m_timerId );
0095         return;
0096     }
0097 
0098     CountdownFrame *&h = m_countdownFrame;
0099 
0100     if( m_counter < h->height() - 3 )
0101     {
0102         h->setFilledRatio(( float ) m_counter / ( float ) h->height() );
0103         h->repaint();
0104     }
0105 
0106     if( !testAttribute( Qt::WA_UnderMouse ) )
0107         m_counter++;
0108 
0109     if( m_counter > h->height() )
0110     {
0111         killTimer( m_timerId );
0112         h->setFilledRatio( 1 );
0113         h->repaint();
0114         close();
0115     }
0116     else
0117     {
0118         killTimer( m_timerId );
0119         m_timerId = startTimer( m_timeout / h->height() );
0120     }
0121 }
0122 
0123 //////////////////////////////////////////////////////////////////////////////
0124 // class CountdownFrame 
0125 //////////////////////////////////////////////////////////////////////////////
0126 
0127 CountdownFrame::CountdownFrame( QWidget *parent )
0128         : QFrame( parent )
0129         , m_filled( 0.0 )
0130 {}
0131 
0132 void CountdownFrame::setFilledRatio( float filled )
0133 {
0134     m_filled = filled;
0135 }
0136 
0137 void CountdownFrame::paintEvent( QPaintEvent *e )
0138 {
0139     QFrame::paintEvent( e );
0140 
0141     QPalette p = palette();
0142     p.setCurrentColorGroup( QPalette::Active );
0143 
0144     QPainter( this ).fillRect( 2, m_filled * height() , width() - 4, height()
0145                                - ( m_filled * height() ) , p.highlight()
0146                              );
0147 }
0148