changelistener.cpp Example File
content/changelistener/changelistener.cpp
#include "changelistener.h"
#include <QSoftMenuBar>
#include <QContentSet>
#include <QTimer>
#include <QDir>
#include <QCategoryManager>
#include <QtDebug>
ChangeListener::ChangeListener( QWidget *parent, Qt::WindowFlags flags )
: QLabel( parent, flags )
, nextIndex( 0 )
, lastContentId( QContent::InvalidId )
{
setScaledContents( true );
QSoftMenuBar::menuFor( this );
imageFiles.append( QFileInfo( ":image/Bubble.png" ) );
imageFiles.append( QFileInfo( ":image/Clouds.png" ) );
imageFiles.append( QFileInfo( ":image/Splatters.png" ) );
imageFiles.append( QFileInfo( ":image/Water.png" ) );
QCategoryManager categoryManager( "Examples" );
categoryId = categoryManager.idForLabel( "Change Listener" );
if( categoryId.isEmpty() )
categoryId = categoryManager.add( "Change Listener" );
QContentSet *contentSet = new QContentSet( QContentFilter::category( categoryId ), this );
connect( contentSet, SIGNAL(changed(QContentIdList,QContent::ChangeType)),
this, SLOT(changed(QContentIdList,QContent::ChangeType)) );
for ( int i = 0; i < contentSet->count(); i++ ) {
QContent::uninstall( contentSet->contentId( i ) );
}
QTimer *timer = new QTimer( this );
connect( timer, SIGNAL(timeout()), this, SLOT(timeout()) );
timer->start( 3000 );
}
ChangeListener::~ChangeListener()
{
if( lastContentId != QContent::InvalidId )
QContent::uninstall( lastContentId );
}
void ChangeListener::timeout()
{
QFileInfo fileInfo = imageFiles.at( nextIndex );
QContent image;
image.setName( fileInfo.baseName() );
image.setFile( fileInfo.absoluteFilePath() );
image.setType( "image/png" );
image.setRole( QContent::Data );
image.setCategories( QStringList( categoryId ) );
if ( image.commit() ) {
if ( lastContentId != QContent::InvalidId ) {
QContent::uninstall( lastContentId );
}
lastContentId = image.id();
} else {
qWarning("Could not commit the new content object!! Document generator exits.");
}
nextIndex = (nextIndex + 1) % imageFiles.count();
}
void ChangeListener::changed(const QContentIdList &idList,QContent::ChangeType changeType)
{
if ( changeType == QContent::Added ) {
foreach ( QContentId id, idList ) {
QContent content( id );
if ( content.isValid() && content.categories().contains( categoryId ) ) {
setWindowTitle( content.name() );
QIODevice *ioDevice = content.open( QIODevice::ReadOnly );
if ( ioDevice ) {
QImage image;
image.load( ioDevice,"PNG" );
setPixmap( QPixmap::fromImage( image ) );
ioDevice->close();
delete ioDevice;
}
break;
}
}
}
}
Copyright © 2009 Trolltech |
Trademarks |
Qt Extended 4.4.3 |