• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdenetwork
  • Sitemap
  • Contact Us
 

kopete/kopete

infoeventwidget.cpp

Go to the documentation of this file.
00001 /*
00002     infoeventwidget.cpp - Info Event Widget
00003 
00004     Copyright (c) 2008      by Roman Jarosz          <kedgedev@centrum.cz>
00005     Kopete    (c) 2008      by the Kopete developers <kopete-devel@kde.org>
00006 
00007     *************************************************************************
00008     *                                                                       *
00009     * This library is free software; you can redistribute it and/or         *
00010     * modify it under the terms of the GNU Lesser General Public            *
00011     * License as published by the Free Software Foundation; either          *
00012     * version 2 of the License, or (at your option) any later version.      *
00013     *                                                                       *
00014     *************************************************************************
00015 */
00016 #include "infoeventwidget.h"
00017 #include "ui_infoeventbase.h"
00018 
00019 #include <QPointer>
00020 #include <QHash>
00021 #include <QTimeLine>
00022 #include <QLayout>
00023 #include <QTextDocument>
00024 
00025 #include <knotification.h>
00026 
00027 #include <kopeteinfoeventmanager.h>
00028 #include <kopeteinfoevent.h>
00029 
00030 class InfoEventWidget::Private
00031 {
00032 public:
00033     int currentEvent;
00034     QTimeLine *timeline;
00035     Ui::InfoEventBase ui;
00036     bool enableUpdates;
00037     QHash<KNotification*, QPointer<Kopete::InfoEvent> > notifyEventHash;
00038 };
00039 
00040 InfoEventWidget::InfoEventWidget(QWidget *parent)
00041 : QWidget(parent), d( new Private() )
00042 {
00043     d->timeline = new QTimeLine( 150, this );
00044     d->timeline->setCurveShape( QTimeLine::EaseInOutCurve );
00045     connect( d->timeline, SIGNAL(valueChanged(qreal)),
00046              this, SLOT(slotAnimate(qreal)) );
00047 
00048     d->ui.setupUi(this);
00049     static_cast<KSqueezedTextLabel*>(d->ui.lblTitle)->setTextElideMode( Qt::ElideRight );
00050     d->ui.buttonPrev->setIcon( KIcon( "arrow-left" ) );
00051     d->ui.buttonNext->setIcon( KIcon( "arrow-right" ) );
00052     d->ui.buttonClose->setIcon( KIcon( "window-close" ) );
00053     QWidget::setVisible( false );
00054 
00055     d->currentEvent = 0;
00056     d->enableUpdates = false;
00057     connect( Kopete::InfoEventManager::self(), SIGNAL(changed()), this, SLOT(updateInfo()) );
00058     connect( Kopete::InfoEventManager::self(), SIGNAL(eventAdded(Kopete::InfoEvent*)), this, SLOT(eventAdded(Kopete::InfoEvent*)) );
00059     connect( d->ui.lblActions, SIGNAL(linkActivated(const QString&)), this, SLOT(linkClicked(const QString&)) );
00060     connect( d->ui.buttonPrev, SIGNAL(clicked(bool)), this, SLOT(prevInfoEvent()) );
00061     connect( d->ui.buttonNext, SIGNAL(clicked(bool)), this, SLOT(nextInfoEvent()) );
00062     connect( d->ui.buttonClose, SIGNAL(clicked(bool)), this, SLOT(closeInfoEvent()) );
00063 }
00064 
00065 
00066 InfoEventWidget::~InfoEventWidget()
00067 {
00068     delete d;
00069 }
00070 
00071 void InfoEventWidget::setVisible( bool visible )
00072 {
00073     if ( visible == isVisible() )
00074         return;
00075 
00076     d->enableUpdates = visible;
00077     if ( visible )
00078         updateInfo();
00079 
00080     // animate the widget disappearing
00081     d->timeline->setDirection( visible ?  QTimeLine::Forward
00082                                : QTimeLine::Backward );
00083     d->timeline->start();
00084 }
00085 
00086 void InfoEventWidget::prevInfoEvent()
00087 {
00088     if ( d->currentEvent > 0 )
00089     {
00090         d->currentEvent--;
00091         updateInfo();
00092     }
00093 }
00094 
00095 void InfoEventWidget::nextInfoEvent()
00096 {
00097     if ( d->currentEvent + 1 < Kopete::InfoEventManager::self()->eventCount() )
00098     {
00099         d->currentEvent++;
00100         updateInfo();
00101     }
00102 }
00103 
00104 void InfoEventWidget::closeInfoEvent()
00105 {
00106     Kopete::InfoEventManager* ie = Kopete::InfoEventManager::self();
00107     Kopete::InfoEvent* event = ie->event( d->currentEvent );
00108 
00109     Q_ASSERT( event );
00110     event->close();
00111 
00112     if ( ie->eventCount() == 0 )
00113         setVisible( false );
00114 }
00115 
00116 void InfoEventWidget::slotAnimate( qreal amount )
00117 {
00118     if ( amount == 0 )
00119     {
00120         QWidget::setVisible( false );
00121         return;
00122     }
00123     
00124     if ( amount == 1.0 )
00125     {
00126         layout()->setSizeConstraint( QLayout::SetDefaultConstraint );
00127         setFixedHeight( sizeHintHeight() );
00128         return;
00129     }
00130     
00131     setFixedHeight( sizeHintHeight() * amount );
00132     
00133     if ( !isVisible() )
00134         QWidget::setVisible( true );
00135 }
00136 
00137 void InfoEventWidget::linkClicked( const QString& link )
00138 {
00139     Kopete::InfoEvent* event = Kopete::InfoEventManager::self()->event( d->currentEvent );
00140     Q_ASSERT( event );
00141 
00142     event->activate( link.toInt() );
00143 }
00144 
00145 void InfoEventWidget::updateInfo()
00146 {
00147     if ( !d->enableUpdates )
00148         return;
00149 
00150     Kopete::InfoEventManager* ie = Kopete::InfoEventManager::self();
00151 
00152     if ( ie->eventCount() == 0 )
00153     {
00154         d->currentEvent = 0;
00155         d->ui.lblInfo->clear();
00156         d->ui.lblActions->clear();
00157         // Can't use clear
00158         static_cast<KSqueezedTextLabel*>(d->ui.lblTitle)->setText(QString());
00159         d->ui.lblEvent->setText( "0/0" );
00160         d->ui.buttonPrev->setEnabled( false );
00161         d->ui.buttonNext->setEnabled( false );
00162         d->ui.buttonClose->setEnabled( false );
00163         return;
00164     }
00165 
00166     if ( d->currentEvent >= ie->eventCount() )
00167         d->currentEvent = ie->eventCount() - 1;
00168 
00169     d->ui.buttonClose->setEnabled( true );
00170     d->ui.buttonPrev->setEnabled( (d->currentEvent > 0) );
00171     d->ui.buttonNext->setEnabled( (d->currentEvent + 1 < ie->eventCount()) );
00172 
00173     const Kopete::InfoEvent* event = ie->event( d->currentEvent );
00174     Q_ASSERT( event );
00175 
00176     static_cast<KSqueezedTextLabel*>(d->ui.lblTitle)->setText( Qt::escape( event->title() ) );
00177     d->ui.lblEvent->setText( QString("%1/%2").arg( d->currentEvent + 1 ).arg( ie->eventCount() ) );
00178 
00179     QString text = QString( "<p>%1</p>" ).arg( event->text() );
00180     if ( !event->additionalText().isEmpty() )
00181         text += QString( "<p>%1</p>" ).arg( event->additionalText() );
00182 
00183     d->ui.lblInfo->setText( text );
00184 
00185     QString linkCode = QString::fromLatin1( "<p align=\"right\">" );
00186 
00187     QMap<uint, QString> actions = event->actions();
00188     QMapIterator<uint, QString> it(actions);
00189     while ( it.hasNext() )
00190     {
00191         it.next();
00192         linkCode += QString::fromLatin1( "<a href=\"%1\">%2</a> " ).arg( it.key() ).arg( Qt::escape(it.value()) );
00193     }
00194 
00195     d->ui.lblActions->setText( linkCode );
00196 
00197     // Redo the layout otherwise sizeHint() won't be correct.
00198     layout()->activate();
00199     if ( sizeHintHeight() > height() )
00200         setFixedHeight( sizeHintHeight() );
00201 }
00202 
00203 void InfoEventWidget::eventAdded( Kopete::InfoEvent* event )
00204 {
00205     KNotification *notify = new KNotification( QString("kopete_info_event") , 0l );
00206     notify->setActions( QStringList( i18n( "View" ) ) );
00207     notify->setText( event->text() );
00208 
00209     d->notifyEventHash.insert( notify, event );
00210 
00211     connect( notify, SIGNAL(activated(unsigned int)), this, SLOT(notificationActivated()) );
00212     connect( notify, SIGNAL(closed()), this, SLOT(notificationClosed()) );
00213 
00214     notify->sendEvent();
00215 }
00216 
00217 void InfoEventWidget::notificationActivated()
00218 {
00219     KNotification *notify = dynamic_cast<KNotification *>(sender());
00220 
00221     Kopete::InfoEvent* event = d->notifyEventHash.value( notify, 0 );
00222     if ( !event )
00223         return;
00224 
00225     int index = Kopete::InfoEventManager::self()->events().indexOf( event );
00226     if ( index != -1 )
00227     {
00228         d->currentEvent = index;
00229         updateInfo();
00230     }
00231     emit showRequest();
00232 }
00233 
00234 void InfoEventWidget::notificationClosed()
00235 {
00236     KNotification *notify = dynamic_cast<KNotification *>(sender());
00237     d->notifyEventHash.remove( notify );
00238 }
00239 
00240 int InfoEventWidget::sizeHintHeight() const
00241 {
00242     return qMin( sizeHint().height(), 250 );
00243 }
00244 
00245 #include "infoeventwidget.moc"

kopete/kopete

Skip menu "kopete/kopete"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdenetwork

Skip menu "kdenetwork"
  • kget
  • kopete
  •   kopete
  •   libkopete
  •       libpapillon
  • krfb
Generated for kdenetwork by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal