![]() |
Home · All Namespaces · All Classes · Main Classes · Grouped Classes · Modules · Functions |
Files:
The Pixmap Filters example uses the QPixmapFilters that come with Qt to process pixmaps.
The abstract QPixmapFilter class provides an interface for implementing filters for QPixmaps. Qt comes with three ready-made subclasses: QPixmapConvolutionFilter, QPixmapColorizeFilter, and QPixmapDropShadowFilter.
The example lets the user select a filter from the Pixmap Filter combo box. The left copy of the pixmap will remain unchanged, while the right will be applied to the selected filter. The effect of the filter can be customized by handling the widgets below the pixmaps. More on this later.
The example consists of two classes: FilterExample and FilterWidget. We will now go through the code concerning pixmap filters; it lives in the FilterWidget class.
The FilterWidget class sets up the selected filter and draws the pixmaps on itself each time it receives a paint event. The event is triggered when any of the example's widgets change their value.
private: QPixmap sourcePixmap; QPixmap backgroundPixmap; QPixmapFilter *setupFilter(QPixmapFilter::FilterType type); Ui::ValueControls controls; QPixmapFilter::FilterType type;
We will apply sourcePixmap to the filters. Since we need room for the shadow of QPixmapDropShadowFilter, we draw the source on a background pixmap (the grid of squares as seen in the image above).
The GUI is set up using Qt Designer (check out its manual if you need to beef up your designer skills). The designer form has three parent widgets, which contain the widgets necessary to customize each filter respectively.
The constructor simply assigns the private variables discussed in the previous section, so we dive right into the painting code.
void FilterWidget::paintEvent(QPaintEvent* /*event*/)
{
QPainter painter(this);
painter.setClipRect(QRect(0, 0, resultSize.width(), resultSize.height()));
painter.drawPixmap(0, 0, backgroundPixmap);
QPixmapFilter *currentFilter = setupFilter(type);
currentFilter->draw(&painter, pixmapPos(sourcePixmap), sourcePixmap);
delete currentFilter;
painter.end();
}
The setupFilter() function sets up the selected filter based on the values of the widgets; we'll examine it shortly. The filter then draws the processed pixmap over the backgroundPixmap.
Note that resultSize is a constant value, and that the positions of the pixmaps are fixed.
QPixmapFilter* FilterWidget::setupFilter(QPixmapFilter::FilterType type) { QPixmapFilter *filter = 0;
First, we make sure to hide the widget that customizes the previous filter (of course, chances are that it is the same as the one we will use now). We then set up the selected filter. Let's look at the code for one filter at a time.
qreal kernel[] = { controls.kernel_1x1->text().toFloat(), controls.kernel_2x1->text().toFloat(), controls.kernel_3x1->text().toFloat(), controls.kernel_1x2->text().toFloat(), controls.kernel_2x2->text().toFloat(), controls.kernel_3x2->text().toFloat(), controls.kernel_1x3->text().toFloat(), controls.kernel_2x3->text().toFloat(), controls.kernel_3x3->text().toFloat() }; switch(type) { case QPixmapFilter::ConvolutionFilter: filter = new QPixmapConvolutionFilter(); reinterpret_cast<QPixmapConvolutionFilter*>(filter)->setConvolutionKernel(kernel, 3,3); break;
The convolution kernel is fetched from the line edits, and then set on the convolution filter. The kernel can be changed with the line edits, which are connected to the update() slot of our FilterWidget.
case QPixmapFilter::ColorizeFilter: { filter = new QPixmapColorizeFilter(); QColor color = QColor(controls.colorizeRedSlider->value(), controls.colorizeGreenSlider->value(), controls.colorizeBlueSlider->value()); reinterpret_cast<QPixmapColorizeFilter*>(filter)->setColor(color); break; }
With the colorize filter, we control the shades of red, green, and blue that we want the pixmap to have.
case QPixmapFilter::DropShadowFilter: { filter = new QPixmapDropShadowFilter(); QColor color = QColor(controls.dropShadowRedSlider->value(), controls.dropShadowGreenSlider->value(), controls.dropShadowBlueSlider->value(), controls.dropShadowAlphaSlider->value()); reinterpret_cast<QPixmapDropShadowFilter*>(filter)->setColor(color); reinterpret_cast<QPixmapDropShadowFilter*>(filter)->setBlurRadius((qreal)(controls.dropShadowRadiusSlider->value())/20.0); reinterpret_cast<QPixmapDropShadowFilter*>(filter)->setOffset( QPointF((qreal)(controls.dropShadowXSlider->value()-500)/10.0, (qreal)(controls.dropShadowYSlider->value()-500)/10.0 )); break; }
The drop shadow filter lets us change the position, color, and the edge-blur of the shadow.
return filter; }
Finally, we return the filter.
Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) | Trademarks | Qt 4.5.0-rc1 |