Class: ImageComposer

Inherits:
Qt::Widget show all
Defined in:
ext/ruby/qtruby/examples/painting/imagecomposition/imagecomposer.rb

Overview

** ** Copyright © 2004-2006 Trolltech AS. All rights reserved. ** ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License version 2.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of ** this file. Please review the following information to ensure GNU ** General Public Licensing requirements will be met: ** www.trolltech.com/products/qt/opensource.html ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** www.trolltech.com/products/qt/licensing.html or contact the ** sales department at [email protected]. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. **

** Translated to QtRuby by Richard Dale

Constant Summary collapse

@@resultSize =
Qt::Size.new(200, 200)

Instance Method Summary collapse

Methods inherited from Qt::Widget

#raise

Methods inherited from Qt::Base

#%, #&, #*, #**, #+, #-, #-@, #/, #<, #<<, #<=, #==, #>, #>=, #>>, #QCOMPARE, #QEXPECT_FAIL, #QFAIL, #QSKIP, #QTEST, #QVERIFY, #QVERIFY2, #QWARN, #^, ancestors, #is_a?, #methods, private_slots, #protected_methods, #public_methods, q_classinfo, q_signal, q_slot, signals, #singleton_methods, slots, #|, #~

Constructor Details

#initializeImageComposer

Returns a new instance of ImageComposer.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'ext/ruby/qtruby/examples/painting/imagecomposition/imagecomposer.rb', line 32

def initialize()
    super()

    @sourceButton = Qt::ToolButton.new
    @sourceButton.iconSize = @@resultSize

    @operatorComboBox = Qt::ComboBox.new
    addOp(Qt::Painter::CompositionMode_SourceOver, tr("SourceOver"))
    addOp(Qt::Painter::CompositionMode_DestinationOver, tr("DestinationOver"))
    addOp(Qt::Painter::CompositionMode_Clear, tr("Clear"))
    addOp(Qt::Painter::CompositionMode_Source, tr("Source"))
    addOp(Qt::Painter::CompositionMode_Destination, tr("Destination"))
    addOp(Qt::Painter::CompositionMode_SourceIn, tr("SourceIn"))
    addOp(Qt::Painter::CompositionMode_DestinationIn, tr("DestinationIn"))
    addOp(Qt::Painter::CompositionMode_SourceOut, tr("SourceOut"))
    addOp(Qt::Painter::CompositionMode_DestinationOut, tr("DestinationOut"))
    addOp(Qt::Painter::CompositionMode_SourceAtop, tr("SourceAtop"))
    addOp(Qt::Painter::CompositionMode_DestinationAtop, tr("DestinationAtop"))
    addOp(Qt::Painter::CompositionMode_Xor, tr("Xor"))

    @destinationButton = Qt::ToolButton.new
    @destinationButton.iconSize = @@resultSize

    @equalLabel = Qt::Label.new(tr("="))

    @resultLabel = Qt::Label.new
    @resultLabel.minimumWidth = @@resultSize.width()

    connect(@sourceButton, SIGNAL(:clicked), self, SLOT(:chooseSource))
    connect(@operatorComboBox, SIGNAL('activated(int)'),
            self, SLOT('recalculateResult()'))
    connect(@destinationButton, SIGNAL(:clicked),
            self, SLOT(:chooseDestination))

    self.layout = Qt::GridLayout.new do |m|
        m.addWidget(@sourceButton, 0, 0, 3, 1)
        m.addWidget(@operatorComboBox, 1, 1)
        m.addWidget(@destinationButton, 0, 2, 3, 1)
        m.addWidget(@equalLabel, 1, 3)
        m.addWidget(@resultLabel, 0, 4, 3, 1)
        m.sizeConstraint = Qt::Layout::SetFixedSize
    end

    @resultImage = Qt::Image.new(@@resultSize, Qt::Image::Format_ARGB32_Premultiplied)

    @sourceImage = Qt::Image.new
    @destinationImage = Qt::Image.new

    loadImage(":/images/butterfly.png", @sourceImage, @sourceButton)
    loadImage(":/images/checker.png", @destinationImage, @destinationButton)


    setWindowTitle(tr("Image Composition"))
end

Instance Method Details

#addOp(mode, name) ⇒ Object



113
114
115
# File 'ext/ruby/qtruby/examples/painting/imagecomposition/imagecomposer.rb', line 113

def addOp(mode, name)
    @operatorComboBox.addItem(name, Qt::Variant.new(mode.to_i))
end

#chooseDestinationObject



91
92
93
94
# File 'ext/ruby/qtruby/examples/painting/imagecomposition/imagecomposer.rb', line 91

def chooseDestination()
    chooseImage(tr("Choose Destination Image"), @destinationImage,
                @destinationButton)
end

#chooseImage(title, image, button) ⇒ Object



117
118
119
120
121
122
# File 'ext/ruby/qtruby/examples/painting/imagecomposition/imagecomposer.rb', line 117

def chooseImage(title, image, button)
    fileName = Qt::FileDialog.getOpenFileName(self, title)
    if !fileName.nil?
        loadImage(fileName, image, button)
    end
end

#chooseSourceObject



87
88
89
# File 'ext/ruby/qtruby/examples/painting/imagecomposition/imagecomposer.rb', line 87

def chooseSource()
    chooseImage(tr("Choose Source Image"), @sourceImage, @sourceButton)
end

#currentModeObject



144
145
146
# File 'ext/ruby/qtruby/examples/painting/imagecomposition/imagecomposer.rb', line 144

def currentMode()
    return @operatorComboBox.itemData(@operatorComboBox.currentIndex()).to_i
end

#imagePos(image) ⇒ Object



148
149
150
151
# File 'ext/ruby/qtruby/examples/painting/imagecomposition/imagecomposer.rb', line 148

def imagePos(image)
    return Qt::Point.new((@@resultSize.width() - image.width()) / 2,
                  (@@resultSize.height() - image.height()) / 2)
end

#loadImage(fileName, image, button) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'ext/ruby/qtruby/examples/painting/imagecomposition/imagecomposer.rb', line 124

def loadImage(fileName, image, button)
    image.load(fileName)

    fixedImage = Qt::Image.new(@@resultSize, Qt::Image::Format_ARGB32_Premultiplied)
    painter = Qt::Painter.new(fixedImage)
    painter.compositionMode = Qt::Painter::CompositionMode_Source
    painter.fillRect(fixedImage.rect(), Qt::Brush.new(Qt::transparent))
    painter.compositionMode = Qt::Painter::CompositionMode_SourceOver
    painter.drawImage(imagePos(image), image)
    painter.end
    button.icon = Qt::Icon.new(Qt::Pixmap.fromImage(fixedImage))

#       The 'QImage::operator=()' method to use as an assignment isn't easily called
#       in QtRuby, so use the convenience method Qt::Image.fromImage() instead
#       image = fixedImage
    image.fromImage(fixedImage)

    recalculateResult()
end

#recalculateResultObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'ext/ruby/qtruby/examples/painting/imagecomposition/imagecomposer.rb', line 96

def recalculateResult()
    mode = currentMode()

    painter = Qt::Painter.new(@resultImage)
    painter.compositionMode = Qt::Painter::CompositionMode_Source
    painter.fillRect(@resultImage.rect(), Qt::Brush.new(Qt::transparent))
    painter.compositionMode = Qt::Painter::CompositionMode_SourceOver
    painter.drawImage(0, 0, @destinationImage)
    painter.compositionMode = mode
    painter.drawImage(0, 0, @sourceImage)
    painter.compositionMode = Qt::Painter::CompositionMode_DestinationOver
    painter.fillRect(@resultImage.rect(), Qt::Brush.new(Qt::white))
    painter.end

    @resultLabel.pixmap = Qt::Pixmap.fromImage(@resultImage)
end