00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "composition.h"
00025 #include <QBoxLayout>
00026 #include <QRadioButton>
00027 #include <QTimer>
00028 #include <QDateTime>
00029 #include <QSlider>
00030 #include <QMouseEvent>
00031 #include <math.h>
00032
00033 CompositionWidget::CompositionWidget(QWidget *parent)
00034 : QWidget(parent)
00035 {
00036 CompositionRenderer *view = new CompositionRenderer(this);
00037
00038 QGroupBox *mainGroup = new QGroupBox(parent);
00039 mainGroup->setAttribute(Qt::WA_ContentsPropagated);
00040 mainGroup->setTitle("Composition Modes");
00041
00042 QGroupBox *modesGroup = new QGroupBox(mainGroup);
00043 modesGroup->setAttribute(Qt::WA_ContentsPropagated);
00044 modesGroup->setTitle("Mode");
00045
00046 rbClear = new QRadioButton("Clear", modesGroup);
00047 connect(rbClear, SIGNAL(clicked()), view, SLOT(setClearMode()));
00048 rbSource = new QRadioButton("Source", modesGroup);
00049 connect(rbSource, SIGNAL(clicked()), view, SLOT(setSourceMode()));
00050 rbDest = new QRadioButton("Destination", modesGroup);
00051 connect(rbDest, SIGNAL(clicked()), view, SLOT(setDestMode()));
00052 rbSourceOver = new QRadioButton("Source Over", modesGroup);
00053 connect(rbSourceOver, SIGNAL(clicked()), view, SLOT(setSourceOverMode()));
00054 rbDestOver = new QRadioButton("Destination Over", modesGroup);
00055 connect(rbDestOver, SIGNAL(clicked()), view, SLOT(setDestOverMode()));
00056 rbSourceIn = new QRadioButton("Source In", modesGroup);
00057 connect(rbSourceIn, SIGNAL(clicked()), view, SLOT(setSourceInMode()));
00058 rbDestIn = new QRadioButton("Dest In", modesGroup);
00059 connect(rbDestIn, SIGNAL(clicked()), view, SLOT(setDestInMode()));
00060 rbSourceOut = new QRadioButton("Source Out", modesGroup);
00061 connect(rbSourceOut, SIGNAL(clicked()), view, SLOT(setSourceOutMode()));
00062 rbDestOut = new QRadioButton("Dest Out", modesGroup);
00063 connect(rbDestOut, SIGNAL(clicked()), view, SLOT(setDestOutMode()));
00064 rbSourceAtop = new QRadioButton("Source Atop", modesGroup);
00065 connect(rbSourceAtop, SIGNAL(clicked()), view, SLOT(setSourceAtopMode()));
00066 rbDestAtop = new QRadioButton("Dest Atop", modesGroup);
00067 connect(rbDestAtop, SIGNAL(clicked()), view, SLOT(setDestAtopMode()));
00068 rbXor = new QRadioButton("Xor", modesGroup);
00069 connect(rbXor, SIGNAL(clicked()), view, SLOT(setXorMode()));
00070
00071 QGroupBox *circleColorGroup = new QGroupBox(mainGroup);
00072 circleColorGroup->setAttribute(Qt::WA_ContentsPropagated);
00073 circleColorGroup->setTitle("Circle color");
00074 QSlider *circleColorSlider = new QSlider(Qt::Horizontal, circleColorGroup);
00075 circleColorSlider->setRange(0, 359);
00076 circleColorSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
00077 connect(circleColorSlider, SIGNAL(valueChanged(int)), view, SLOT(setCircleColor(int)));
00078
00079 QGroupBox *circleAlphaGroup = new QGroupBox(mainGroup);
00080 circleAlphaGroup->setAttribute(Qt::WA_ContentsPropagated);
00081 circleAlphaGroup->setTitle("Circle alpha");
00082 QSlider *circleAlphaSlider = new QSlider(Qt::Horizontal, circleAlphaGroup);
00083 circleAlphaSlider->setRange(0, 255);
00084 circleAlphaSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
00085 connect(circleAlphaSlider, SIGNAL(valueChanged(int)), view, SLOT(setCircleAlpha(int)));
00086
00087 QPushButton *showSourceButton = new QPushButton(mainGroup);
00088 showSourceButton->setText("Show Source");
00089 #ifdef QT_OPENGL_SUPPORT
00090 QPushButton *enableOpenGLButton = new QPushButton(mainGroup);
00091 enableOpenGLButton->setText("Use OpenGL");
00092 enableOpenGLButton->setCheckable(true);
00093 enableOpenGLButton->setChecked(view->usesOpenGL());
00094
00095 if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers())
00096 enableOpenGLButton->hide();
00097 #endif
00098 QPushButton *whatsThisButton = new QPushButton(mainGroup);
00099 whatsThisButton->setText("What's This?");
00100 whatsThisButton->setCheckable(true);
00101
00102 QPushButton *animateButton = new QPushButton(mainGroup);
00103 animateButton->setText("Animated");
00104 animateButton->setCheckable(true);
00105 animateButton->setChecked(true);
00106
00107 QHBoxLayout *viewLayout = new QHBoxLayout(this);
00108 viewLayout->addWidget(view);
00109 viewLayout->addWidget(mainGroup);
00110
00111 QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup);
00112 mainGroupLayout->addWidget(circleColorGroup);
00113 mainGroupLayout->addWidget(circleAlphaGroup);
00114 mainGroupLayout->addWidget(modesGroup);
00115 mainGroupLayout->addStretch();
00116 mainGroupLayout->addWidget(animateButton);
00117 mainGroupLayout->addWidget(whatsThisButton);
00118 mainGroupLayout->addWidget(showSourceButton);
00119 #ifdef QT_OPENGL_SUPPORT
00120 mainGroupLayout->addWidget(enableOpenGLButton);
00121 #endif
00122
00123 QVBoxLayout *modesLayout = new QVBoxLayout(modesGroup);
00124 modesLayout->addWidget(rbClear);
00125 modesLayout->addWidget(rbSource);
00126 modesLayout->addWidget(rbDest);
00127 modesLayout->addWidget(rbSourceOver);
00128 modesLayout->addWidget(rbDestOver);
00129 modesLayout->addWidget(rbSourceIn);
00130 modesLayout->addWidget(rbDestIn);
00131 modesLayout->addWidget(rbSourceOut);
00132 modesLayout->addWidget(rbDestOut);
00133 modesLayout->addWidget(rbSourceAtop);
00134 modesLayout->addWidget(rbDestAtop);
00135 modesLayout->addWidget(rbXor);
00136
00137 QVBoxLayout *circleColorLayout = new QVBoxLayout(circleColorGroup);
00138 circleColorLayout->addWidget(circleColorSlider);
00139
00140 QVBoxLayout *circleAlphaLayout = new QVBoxLayout(circleAlphaGroup);
00141 circleAlphaLayout->addWidget(circleAlphaSlider);
00142
00143 view->loadDescription(":res/composition.html");
00144 view->loadSourceFile(":res/composition.cpp");
00145
00146 connect(whatsThisButton, SIGNAL(clicked(bool)), view, SLOT(setDescriptionEnabled(bool)));
00147 connect(view, SIGNAL(descriptionEnabledChanged(bool)), whatsThisButton, SLOT(setChecked(bool)));
00148 connect(showSourceButton, SIGNAL(clicked()), view, SLOT(showSource()));
00149 #ifdef QT_OPENGL_SUPPORT
00150 connect(enableOpenGLButton, SIGNAL(clicked(bool)), view, SLOT(enableOpenGL(bool)));
00151 #endif
00152 connect(animateButton, SIGNAL(toggled(bool)), view, SLOT(setAnimationEnabled(bool)));
00153
00154 circleColorSlider->setValue(270);
00155 circleAlphaSlider->setValue(200);
00156 rbSourceOut->animateClick();
00157
00158 setWindowTitle(tr("Composition Modes"));
00159 }
00160
00161
00162 void CompositionWidget::nextMode()
00163 {
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 }
00181
00182 CompositionRenderer::CompositionRenderer(QWidget *parent)
00183 : ArthurFrame(parent)
00184 {
00185 m_animation_enabled = true;
00186 m_image = QImage(":res/flower_2.png");
00187 m_circle_alpha = 127;
00188 m_circle_hue = 255;
00189 m_current_object = NoObject;
00190 m_composition_mode = QPainter::CompositionMode_SourceOut;
00191
00192 m_circle_pos = QPoint(200, 100);
00193
00194 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00195 #ifdef QT_OPENGL_SUPPORT
00196 m_pbuffer = 0;
00197 m_pbuffer_size = 1024;
00198 #endif
00199 }
00200
00201 QRectF rectangle_around(const QPointF &p, const QSizeF &size = QSize(250, 200))
00202 {
00203 QRectF rect(p, size);
00204 rect.translate(-size.width()/2, -size.height()/2);
00205 return rect;
00206 }
00207
00208 void CompositionRenderer::updateCirclePos()
00209 {
00210 if (m_current_object != NoObject)
00211 return;
00212 QDateTime dt = QDateTime::currentDateTime();
00213 qreal t = (dt.toTime_t() * 1000 + dt.time().msec()) / 1000.0;
00214
00215 qreal x = width() / 2.0 + (cos(t) + sin(-t*2)) * width() / 2.0;
00216 qreal y = height() / 2.0 + (sin(t) + cos(t * 3)) * height() / 2.0;
00217
00218 m_circle_pos = QLineF(m_circle_pos, QPointF(x, y)).pointAt(0.01);
00219 }
00220
00221 void CompositionRenderer::drawBase(QPainter &p)
00222 {
00223 p.setPen(Qt::NoPen);
00224
00225 QLinearGradient rect_gradient(0, 0, 0, height());
00226 rect_gradient.setColorAt(0, Qt::red);
00227 rect_gradient.setColorAt(.17, Qt::yellow);
00228 rect_gradient.setColorAt(.33, Qt::green);
00229 rect_gradient.setColorAt(.50, Qt::cyan);
00230 rect_gradient.setColorAt(.66, Qt::blue);
00231 rect_gradient.setColorAt(.81, Qt::magenta);
00232 rect_gradient.setColorAt(1, Qt::red);
00233 p.setBrush(rect_gradient);
00234 p.drawRect(width() / 2, 0, width() / 2, height());
00235
00236 QLinearGradient alpha_gradient(0, 0, width(), 0);
00237 alpha_gradient.setColorAt(0, Qt::white);
00238 alpha_gradient.setColorAt(0.2, Qt::white);
00239 alpha_gradient.setColorAt(0.5, Qt::transparent);
00240 alpha_gradient.setColorAt(0.8, Qt::white);
00241 alpha_gradient.setColorAt(1, Qt::white);
00242
00243 p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
00244 p.setBrush(alpha_gradient);
00245 p.drawRect(0, 0, width(), height());
00246
00247 p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
00248
00249 p.setPen(Qt::NoPen);
00250 p.setRenderHint(QPainter::SmoothPixmapTransform);
00251 p.drawImage(rect(), m_image);
00252 }
00253
00254 void CompositionRenderer::drawSource(QPainter &p)
00255 {
00256 p.setPen(Qt::NoPen);
00257 p.setRenderHint(QPainter::Antialiasing);
00258 p.setCompositionMode(m_composition_mode);
00259
00260 QRectF circle_rect = rectangle_around(m_circle_pos);
00261 QColor color = QColor::fromHsvF(m_circle_hue / 360.0, 1, 1, m_circle_alpha / 255.0);
00262 QLinearGradient circle_gradient(circle_rect.topLeft(), circle_rect.bottomRight());
00263 circle_gradient.setColorAt(0, color.light());
00264 circle_gradient.setColorAt(0.5, color);
00265 circle_gradient.setColorAt(1, color.dark());
00266 p.setBrush(circle_gradient);
00267
00268 p.drawEllipse(circle_rect);
00269 }
00270
00271 void CompositionRenderer::paint(QPainter *painter)
00272 {
00273 if (m_animation_enabled)
00274 updateCirclePos();
00275
00276 #ifdef QT_OPENGL_SUPPORT
00277 if (usesOpenGL()) {
00278
00279 int new_pbuf_size = m_pbuffer_size;
00280 if (size().width() > m_pbuffer_size ||
00281 size().height() > m_pbuffer_size)
00282 new_pbuf_size *= 2;
00283
00284 if (size().width() < m_pbuffer_size/2 &&
00285 size().height() < m_pbuffer_size/2)
00286 new_pbuf_size /= 2;
00287
00288 if (!m_pbuffer || new_pbuf_size != m_pbuffer_size) {
00289 if (m_pbuffer) {
00290 m_pbuffer->deleteTexture(m_base_tex);
00291 m_pbuffer->deleteTexture(m_compositing_tex);
00292 delete m_pbuffer;
00293 }
00294
00295 m_pbuffer = new QGLPixelBuffer(QSize(new_pbuf_size, new_pbuf_size), QGLFormat::defaultFormat(), glWidget());
00296 m_pbuffer->makeCurrent();
00297 m_base_tex = m_pbuffer->generateDynamicTexture();
00298 m_compositing_tex = m_pbuffer->generateDynamicTexture();
00299 m_pbuffer_size = new_pbuf_size;
00300 }
00301
00302 if (size() != m_previous_size) {
00303 m_previous_size = size();
00304 QPainter p(m_pbuffer);
00305 p.setCompositionMode(QPainter::CompositionMode_Source);
00306 p.fillRect(QRect(0, 0, m_pbuffer->width(), m_pbuffer->height()), Qt::transparent);
00307 drawBase(p);
00308 m_pbuffer->updateDynamicTexture(m_base_tex);
00309 }
00310
00311 qreal x_fraction = width()/float(m_pbuffer->width());
00312 qreal y_fraction = height()/float(m_pbuffer->height());
00313
00314 {
00315 QPainter p(m_pbuffer);
00316 p.setCompositionMode(QPainter::CompositionMode_Source);
00317 p.fillRect(QRect(0, 0, m_pbuffer->width(), m_pbuffer->height()), Qt::transparent);
00318
00319 p.save();
00320 glBindTexture(GL_TEXTURE_2D, m_base_tex);
00321 glEnable(GL_TEXTURE_2D);
00322 glColor4f(1.,1.,1.,1.);
00323
00324 glBegin(GL_QUADS);
00325 {
00326 glTexCoord2f(0, 1.0);
00327 glVertex2f(0, 0);
00328
00329 glTexCoord2f(x_fraction, 1.0);
00330 glVertex2f(width(), 0);
00331
00332 glTexCoord2f(x_fraction, 1.0-y_fraction);
00333 glVertex2f(width(), height());
00334
00335 glTexCoord2f(0, 1.0-y_fraction);
00336 glVertex2f(0, height());
00337 }
00338 glEnd();
00339
00340 glDisable(GL_TEXTURE_2D);
00341 p.restore();
00342
00343 drawSource(p);
00344 m_pbuffer->updateDynamicTexture(m_compositing_tex);
00345 }
00346
00347 glWidget()->makeCurrent();
00348 glBindTexture(GL_TEXTURE_2D, m_compositing_tex);
00349 glEnable(GL_TEXTURE_2D);
00350 glColor4f(1.,1.,1.,1.);
00351 glBegin(GL_QUADS);
00352 {
00353 glTexCoord2f(0, 1.0);
00354 glVertex2f(0, 0);
00355
00356 glTexCoord2f(x_fraction, 1.0);
00357 glVertex2f(width(), 0);
00358
00359 glTexCoord2f(x_fraction, 1.0-y_fraction);
00360 glVertex2f(width(), height());
00361
00362 glTexCoord2f(0, 1.0-y_fraction);
00363 glVertex2f(0, height());
00364 }
00365 glEnd();
00366 glDisable(GL_TEXTURE_2D);
00367 } else
00368 #endif
00369 {
00370
00371 if (m_buffer.size() != size()) {
00372 m_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
00373 m_base_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
00374
00375 m_base_buffer.fill(0);
00376
00377 QPainter p(&m_base_buffer);
00378
00379 drawBase(p);
00380 }
00381
00382 memcpy(m_buffer.bits(), m_base_buffer.bits(), m_buffer.numBytes());
00383
00384 {
00385 QPainter p(&m_buffer);
00386 drawSource(p);
00387 }
00388
00389 painter->drawImage(0, 0, m_buffer);
00390 }
00391
00392 if (m_animation_enabled)
00393 update();
00394 }
00395
00396 void CompositionRenderer::mousePressEvent(QMouseEvent *e)
00397 {
00398 setDescriptionEnabled(false);
00399
00400 QRectF circle = rectangle_around(m_circle_pos);
00401
00402 if (circle.contains(e->pos())) {
00403 m_current_object = Circle;
00404 m_offset = circle.center() - e->pos();
00405 } else {
00406 m_current_object = NoObject;
00407 }
00408 }
00409
00410 void CompositionRenderer::mouseMoveEvent(QMouseEvent *e)
00411 {
00412 if (m_current_object == Circle) setCirclePos(e->pos() + m_offset);
00413 }
00414
00415 void CompositionRenderer::mouseReleaseEvent(QMouseEvent *)
00416 {
00417 m_current_object = NoObject;
00418 }