class SortingBox

Constants

RAND_MAX

Public Class Methods

new(parent = nil) click to toggle source
Calls superclass method
# File examples/widgets/tooltips/sortingbox.rb, line 36
def initialize(parent = nil)
        super(parent)
    setAttribute(Qt::WA_StaticContents)
    setMouseTracking(true)
    setBackgroundRole(Qt::Palette::Base)

    @itemInMotion = nil
        @shapeItems = []
        @circlePath = Qt::PainterPath.new
        @squarePath = Qt::PainterPath.new
        @trianglePath = Qt::PainterPath.new

    @newCircleButton = createToolButton(tr("New Circle"),
                                       Qt::Icon.new("images/circle.png"),
                                       SLOT(:createNewCircle))

    @newSquareButton = createToolButton(tr("New Square"),
                                       Qt::Icon.new("images/square.png"),
                                       SLOT(:createNewSquare))

    @newTriangleButton = createToolButton(tr("New Triangle"),
                                         Qt::Icon.new("images/triangle.png"),
                                         SLOT(:createNewTriangle))

    @circlePath.addEllipse(Qt::RectF.new(0.0, 0.0, 100.0, 100.0))
    @squarePath.addRect(Qt::RectF.new(0.0, 0.0, 100.0, 100.0))

    x = @trianglePath.currentPosition().x()
    y = @trianglePath.currentPosition().y()
    @trianglePath.moveTo(x + 120 / 2, y)
    @trianglePath.lineTo(0, 100)
    @trianglePath.lineTo(120, 100)
    @trianglePath.lineTo(x + 120 / 2, y)

    setWindowTitle(tr("Tooltips"))
    resize(500, 300)

    createShapeItem(@circlePath, tr("Circle"), initialItemPosition(@circlePath),
                    initialItemColor())
    createShapeItem(@squarePath, tr("Square"), initialItemPosition(@squarePath),
                    initialItemColor())
    createShapeItem(@trianglePath, tr("Triangle"),
                    initialItemPosition(@trianglePath), initialItemColor())
end

Public Instance Methods

createNewCircle() click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 139
def createNewCircle()
    createShapeItem(@circlePath, tr("Circle"), randomItemPosition(),
                    randomItemColor())
end
createNewSquare() click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 144
def createNewSquare()
    createShapeItem(@squarePath, tr("Square"), randomItemPosition(),
                    randomItemColor())
end
createNewTriangle() click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 149
def createNewTriangle()
    createShapeItem(@trianglePath, tr("Triangle"), randomItemPosition(),
                    randomItemColor())
end
createShapeItem(path, toolTip, pos, color) click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 180
def createShapeItem(path, toolTip, pos, color)
    shapeItem = ShapeItem.new
    shapeItem.path = path
    shapeItem.toolTip = toolTip
    shapeItem.position = pos
    shapeItem.color = color
    @shapeItems.push(shapeItem)
    update()
end
createToolButton(toolTip, icon, member) click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 190
def createToolButton(toolTip, icon, member)
    button = Qt::ToolButton.new(self)
    button.toolTip = toolTip
    button.icon = icon
    button.setIconSize(Qt::Size.new(32, 32))
    connect(button, SIGNAL(:clicked), self, member)

    return button
end
event(event) click to toggle source
Calls superclass method
# File examples/widgets/tooltips/sortingbox.rb, line 81
def event(event)
    if event.type == Qt::Event::ToolTip
        index = itemAt(event.pos())
        if index != -1
            Qt::ToolTip.showText(event.globalPos(), @shapeItems[index].toolTip())
        else
            Qt::ToolTip.showText(event.globalPos(), "")
                end
    end
    super(event)
end
initialItemColor() click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 215
def initialItemColor()
    return Qt::Color::fromHsv(((@shapeItems.size() + 1) * 85) % 256, 255, 190)
end
initialItemPosition(path) click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 200
def initialItemPosition(path)
    y = (height() - path.controlPointRect().height()) / 2
    if @shapeItems.length == 0
        x = ((3 * width()) / 2 - path.controlPointRect.width) / 2
    else
        x = (width() / @shapeItems.size -
             path.controlPointRect.width) / 2
        end
    return Qt::Point.new(x, y)
end
itemAt(pos) click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 154
def itemAt(pos)
        (@shapeItems.length - 1).downto(0) do |i|
        item = @shapeItems[i]
        if item.path.contains(Qt::PointF.new(pos - item.position))
            return i
                end
    end
    return -1
end
mouseMoveEvent(event) click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 126
def mouseMoveEvent(event)
    if (event.buttons & Qt::LeftButton.to_i) && !@itemInMotion.nil?
        moveItemTo(event.pos)
        end
end
mousePressEvent(event) click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 114
def mousePressEvent(event)
    if event.button == Qt::LeftButton
        index = itemAt(event.pos)
        if index != -1
            @itemInMotion = @shapeItems[index]
            @previousPosition = event.pos
            @shapeItems.push @shapeItems.delete_at(index)
            update()
        end
    end
end
mouseReleaseEvent(event) click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 132
def mouseReleaseEvent(event)
    if event.button == Qt::LeftButton && !@itemInMotion.nil?
        moveItemTo(event.pos)
        @itemInMotion = nil
    end
end
moveItemTo(pos) click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 164
def moveItemTo(pos)
    offset = pos - @previousPosition
    @itemInMotion.position = @itemInMotion.position + offset
    @previousPosition = pos
    update()
end
paintEvent(event) click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 103
def paintEvent(event)
    painter = Qt::Painter.new(self)
        @shapeItems.each do |shapeItem|
        painter.translate(shapeItem.position())
        painter.brush = Qt::Brush.new(shapeItem.color)
        painter.drawPath(shapeItem.path())
        painter.translate(-shapeItem.position())
    end
        painter.end
end
randomItemColor() click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 219
def randomItemColor()
    return Qt::Color::fromHsv(rand(RAND_MAX) % 256, 255, 190)
end
randomItemPosition() click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 211
def randomItemPosition()
    return Qt::Point.new(rand(RAND_MAX) % (width() - 120), rand(RAND_MAX) % (height() - 120))
end
resizeEvent(event) click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 93
def resizeEvent(event)
    margin = style().pixelMetric(Qt::Style::PM_DefaultTopLevelMargin)
    x = width() - margin
    y = height() - margin

    y = updateButtonGeometry(@newCircleButton, x, y)
    y = updateButtonGeometry(@newSquareButton, x, y)
    updateButtonGeometry(@newTriangleButton, x, y)
end
updateButtonGeometry(button, x, y) click to toggle source
# File examples/widgets/tooltips/sortingbox.rb, line 171
def updateButtonGeometry(button, x, y)
    size = button.sizeHint()
    button.setGeometry(x - size.rwidth, y - size.rheight,
                        size.rwidth, size.rheight)

    return y - size.rheight - 
                        style().pixelMetric(Qt::Style::PM_DefaultLayoutSpacing)
end