![]() | Loading and manipulating a pixmap |
Prev | Next |
First, we'll add a QPixmap member variable called m_pixmap to CentralView and a setPixmap method that will only do:
void CentralView::setPixmap(const QPixmap &pixmap) { m_pixmap=pixmap; updatePixmap(); }
Now, we implement the method in TheTinterView that KDevelop created as a placeholder for a document loading method.
void TheTinterView::openURL(const KURL& url) { QString tmpFile; if (KIO::NetAccess::download(url, tmpFile)) { // load in the file (target is always local) m_view->setPixmap( tmpFile ); // and remove the temp file KIO::NetAccess::removeTempFile(tmpFile); } }KIO::NetAccess::download is used to download the url url and save it in a local temporal file whose full path will be stored in tmpFile. If it succeed, it will return true and the document will be loaded.
You may have noticed that CentralView::setPixmap accepts a QPixmap parameter, while we've used a QString as a paremeter here, why is that? If you are experienced in C++ and you've read a bit the QPixmap documentation, you'll know that QPixmap has a constructor that accepts a single QString parameter, and so, that line is equivalent to:
QPixmap pixmap( tmpFile ); m_view->setPixmap( pixmap );I forgot to mention that QPixmap's constructor which gets a QString parameter loads the pixmap from the file specified in the parameter.
Finally, KIO::NetAccess::removeTempFile(tmpFile); does something very interesting. It removes the tmpFile file only if it's really a temporal file. You have to note here that in case you're trying to load a local file, KIO::NetAccess::download returns the actual file name instead of copying it to a temporal location (which would be absurd) and KIO::NetAccess::removeTempFile then doesn't do anything (so it's safe to call it even if tmpFile is not temporal because KIO::NetAccess has a list of which files have been downloaded (are really temporal) and which are real files which shouldn't be removed.
Now we'll make CentralView do something with the pixmap. Let's change the implementation of updatePixmap to:
void CentralView::updatePixmap() { QImage image= m_pixmap.convertToImage(); KImageEffect::blend(m_color, image, 0.5); QPixmap pixmap; pixmap.convertFromImage(image); m_label->setPixmap(pixmap); }
The first line, converts the pixmap to an image, which is another class that can store images. The difference is that QImage stores the images locally, while QPixmap stores it in the X Server, so you have to use one or another depending on what you'll do with them. In this case, an image will be easier to tint.
KImageEffect::blend(m_color, image, 0.5); uses KImageEffect (from kdelibs/kdefx) to blend a color with an image. KImageEffect is a class that contains useful methods to apply effects to images. The first parameter is the color, the second one, the image (which will be modified by this method) and the third parameter is an opacity intensity.
We continue by converting the image back to a pixmap and setting it in the label as before.
Prev | Home | Next |
Removing flicker using a timer | Up | Modifying the existing UI |