| Home · All Classes · Grouped Classes · Annotated · Functions |
/**************************************************************************** ** ** Copyright (C) 2000-2007 TROLLTECH ASA. All rights reserved. ** ** This file is part of the Opensource Edition of the Qtopia Toolkit. ** ** $TROLLTECH_DUAL_LICENSE$ ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #include "changelistener_p.h" #include "documentgenerator_p.h" #include <QLabel> #include <QPalette> #include <QSizePolicy> #include <QImage> #include <QContentSet> #include <QTimer> #include <QFile> #include <QContent> #include <QIODevice> #include <QMimeType> #include <QDebug> const int ChangeListenerPrivate::MILLISECONDS_DELAY = 3000; const int ChangeListenerPrivate::NUM_PICS = 4; const QString ChangeListenerPrivate::picNames[NUM_PICS] = { "Clouds.png", "Bubble.png", "Water.png", "Splatters.png"}; const QString ChangeListenerPrivate::category("ChangeListener"); // or any arbitrary string /*! \internal Creates the content set (i.e. reads all the category information from the database) */ ChangeListenerPrivate::ChangeListenerPrivate(QWidget *parent) : QObject(parent) , contentSet(0) , imageLabel(0) , timer(0) , cursor(0) { contentSet = new QContentSet(QContentFilter(QContentFilter::Category,category)); // contentSet->setCriteria(QContentFilter::Category,category); // We'll use a label to display the image. imageLabel = new QLabel; imageLabel->setBackgroundRole(QPalette::Base); imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); imageLabel->setScaledContents(true); // When there's a change to the database, we'll get notified about it so // that if there's a new picture we can change the displayed image. connect(contentSet,SIGNAL(changed(QContentIdList,QContent::ChangeType)), this,SLOT(changed(QContentIdList,QContent::ChangeType))); // The timer will "generate" new image content to put in the database. The timer // is kicked off in startGenerating(). timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(generate())); } ChangeListenerPrivate::~ChangeListenerPrivate() { // delete timer; delete contentSet; } /*! \internal Starts the timer, i.e. causes this class to start generating database objects. */ void ChangeListenerPrivate::startGenerating() { timer->start(MILLISECONDS_DELAY); } /*! \internal Stops the timer, i.e. stops this class from generating database objects. Cleans up all the files. */ void ChangeListenerPrivate::stopGenerating() { timer->stop(); // Get a static snapshot of all the item IDs for this category. QContentIdList idList = contentSet->itemIds(); foreach ( QContentId id, idList ) { QContent content(id); if ( content.isValid() ) { // Uninstall the database object. qDebug() << "Uninstalling " << content.name(); content.removeFiles(); } } } /*! \internal Slot that responds to the periodic timer. Reads in the "next" image file, wraps its contents in a new database object and store it in the database. */ void ChangeListenerPrivate::generate() { // Generate the name of the "new" picture. QString name("XXYYZZ_Pic"); name.append(QString::number(cursor)); // Read in the next image file from the changelistener's private store. int picIndex = cursor % NUM_PICS; QFile file(":image/changelistener/" + picNames[picIndex]); if ( !file.open(QIODevice::ReadOnly) ) { qWarning() << "Cannot open " << picNames[picIndex]; return; } QByteArray bytes = file.readAll(); // Not ideal, since you can't check for errors file.close(); // Create a new database object for the current image. QContent newContent; // At the least, we want to set the object's name and mime type. We'll also // give the object a category, so that all picture objects created by this application // can be grouped together. newContent.setName(name); newContent.setCategories(QStringList(category)); newContent.setType(QMimeType(picNames[picIndex]).id()); // extract mime type from the file name qDebug() << "FILE NAME +++ " << newContent.file(); // Open the new database object, and write the image contents to it. QIODevice *ioDevice = newContent.open(QIODevice::WriteOnly); if ( !ioDevice ) { qWarning("Could not open the new content object to write to!! Document Generator exits."); return; } ioDevice->write(bytes); ioDevice->close(); delete ioDevice; if ( !newContent.commit() ) { qWarning("Could not commit the new content object!! Document generator exits."); } cursor++; // for next time around } /*! \internal Slot to handle a change in the database. Checks to see if the change is what we're interested in, i.e. a new QContent object of the category that has been added during generate(). If it is, we read in the image data from the new database object, and set the label to the new image. */ void ChangeListenerPrivate::changed(const QContentIdList &idList,QContent::ChangeType changeType) { if ( changeType == QContent::Added ) { foreach (QContentId id,idList) { QContent content(id); // Check that we've got a valid content object and that its category is the same // as that which was set in the generate() method. if ( content.isValid() && (content.categories().contains(category)) ) { // Open the database object so that you can read its contents. QIODevice *ioDevice = content.open(QIODevice::ReadOnly); if ( !ioDevice ) { qWarning("Could not open the new content object to read from!!"); break; } // Create the image, and set the label to the new picture. QImage image; image.load(ioDevice,0); imageLabel->setPixmap(QPixmap::fromImage(image)); // Clean up. ioDevice->close(); // It is the user's reponsibility to delete the return value of QContent::open(). delete ioDevice; // For the purposes of this example, we're only interested in one (and there // should only be one). break; } } } }
| Copyright © 2007 Trolltech | Trademarks | Qtopia 4.3.0 |