1 /****************************************************************************
3 ** Copyright (C) 2008 Trolltech ASA. All rights reserved.
5 ** This file is part of the Qt Script Generator project on Trolltech Labs.
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
24 function tr(s) { return s; }
27 function ImageComposer(parent)
29 QWidget.call(this, parent);
31 this.sourceButton = new QToolButton();
32 this.sourceButton.setIconSize(ImageComposer.resultSize);
34 this.operatorComboBox = new QComboBox();
35 this.addOp(QPainter.CompositionMode_SourceOver, tr("SourceOver"));
36 this.addOp(QPainter.CompositionMode_DestinationOver, tr("DestinationOver"));
37 this.addOp(QPainter.CompositionMode_Clear, tr("Clear"));
38 this.addOp(QPainter.CompositionMode_Source, tr("Source"));
39 this.addOp(QPainter.CompositionMode_Destination, tr("Destination"));
40 this.addOp(QPainter.CompositionMode_SourceIn, tr("SourceIn"));
41 this.addOp(QPainter.CompositionMode_DestinationIn, tr("DestinationIn"));
42 this.addOp(QPainter.CompositionMode_SourceOut, tr("SourceOut"));
43 this.addOp(QPainter.CompositionMode_DestinationOut, tr("DestinationOut"));
44 this.addOp(QPainter.CompositionMode_SourceAtop, tr("SourceAtop"));
45 this.addOp(QPainter.CompositionMode_DestinationAtop, tr("DestinationAtop"));
46 this.addOp(QPainter.CompositionMode_Xor, tr("Xor"));
48 this.destinationButton = new QToolButton();
49 this.destinationButton.setIconSize(ImageComposer.resultSize);
51 this.equalLabel = new QLabel(tr("="));
53 this.resultLabel = new QLabel();
54 this.resultLabel.minimumWidth = ImageComposer.resultSize.width();
56 this.sourceButton["clicked()"].connect(this, this.chooseSource);
57 this.operatorComboBox["activated(int)"].connect(
58 this, this.recalculateResult);
59 this.destinationButton["clicked()"].connect(
60 this, this.chooseDestination);
62 var mainLayout = new QGridLayout();
63 mainLayout.addWidget(this.sourceButton, 0, 0, 3, 1);
64 mainLayout.addWidget(this.operatorComboBox, 1, 1);
65 mainLayout.addWidget(this.destinationButton, 0, 2, 3, 1);
66 mainLayout.addWidget(this.equalLabel, 1, 3);
67 mainLayout.addWidget(this.resultLabel, 0, 4, 3, 1);
68 mainLayout.sizeConstraint = QLayout.SetFixedSize;
69 this.setLayout(mainLayout);
71 this.sourceImage = new QImage();
72 this.destinationImage = new QImage();
73 this.resultImage = new QImage(ImageComposer.resultSize, QImage.Format_ARGB32_Premultiplied);
75 this.loadImage("images/butterfly.png", "sourceImage", this.sourceButton);
76 this.loadImage("images/checker.png", "destinationImage", this.destinationButton);
78 this.windowTitle = tr("Image Composition");
81 ImageComposer.resultSize = new QSize(200, 200);
83 ImageComposer.prototype = new QWidget();
85 ImageComposer.prototype.chooseSource = function()
87 this.chooseImage(tr("Choose Source Image"), "sourceImage", this.sourceButton);
90 ImageComposer.prototype.chooseDestination = function()
92 this.chooseImage(tr("Choose Destination Image"), "destinationImage", this.destinationButton);
95 ImageComposer.prototype.recalculateResult = function()
97 var mode = this.currentMode();
99 var painter = new QPainter(this.resultImage);
100 painter.setCompositionMode(QPainter.CompositionMode_Source);
101 painter.fillRect(this.resultImage.rect(), new QBrush(new QColor(Qt.transparent)));
102 painter.setCompositionMode(QPainter.CompositionMode_SourceOver);
103 painter.drawImage(0, 0, this.destinationImage);
104 painter.setCompositionMode(mode);
105 painter.drawImage(0, 0, this.sourceImage);
106 painter.setCompositionMode(QPainter.CompositionMode_DestinationOver);
107 painter.fillRect(this.resultImage.rect(), new QBrush(new QColor(Qt.white)));
110 this.resultLabel.setPixmap(QPixmap.fromImage(this.resultImage));
113 ImageComposer.prototype.addOp = function(mode, name)
115 this.operatorComboBox.addItem(name, mode);
118 ImageComposer.prototype.chooseImage = function(title, property, button)
120 var fileName = QFileDialog.getOpenFileName(this, title);
121 if (!fileName.isEmpty())
122 loadImage(fileName, property, button);
125 ImageComposer.prototype.loadImage = function(fileName, property, button)
127 var image = new QImage(fileName);
129 var fixedImage = new QImage(ImageComposer.resultSize, QImage.Format_ARGB32_Premultiplied);
130 var painter = new QPainter(fixedImage);
131 painter.setCompositionMode(QPainter.CompositionMode_Source);
132 painter.fillRect(fixedImage.rect(), new QBrush(new QColor(Qt.transparent)));
133 painter.setCompositionMode(QPainter.CompositionMode_SourceOver);
134 painter.drawImage(this.imagePos(image), image);
136 button.icon = new QIcon(QPixmap.fromImage(fixedImage));
138 this[property] = fixedImage;
140 this.recalculateResult();
143 ImageComposer.prototype.currentMode = function()
145 return this.operatorComboBox.itemData(this.operatorComboBox.currentIndex);
148 ImageComposer.prototype.imagePos = function(image)
150 return new QPoint((ImageComposer.resultSize.width() - image.width()) / 2,
151 (ImageComposer.resultSize.height() - image.height()) / 2);
154 var composer = new ImageComposer();
156 QCoreApplication.exec();