Class: PiecesModel
- Defined in:
- ext/ruby/qtruby/examples/itemviews/puzzle/piecesmodel.rb
Overview
** ** Copyright © 2004-2005 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
- RAND_MAX =
2147483647
Instance Method Summary collapse
- #addPiece(pixmap, location) ⇒ Object
- #data(index, role) ⇒ Object
- #dropMimeData(data, action, row, column, parent) ⇒ Object
- #flags(index) ⇒ Object
-
#initialize(parent) ⇒ PiecesModel
constructor
A new instance of PiecesModel.
- #mimeData(indexes) ⇒ Object
- #mimeTypes ⇒ Object
- #removeRows(row, count, parent) ⇒ Object
- #rowCount(parent) ⇒ Object
- #supportedDropActions ⇒ Object
Constructor Details
#initialize(parent) ⇒ PiecesModel
Returns a new instance of PiecesModel.
30 31 32 33 34 |
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/piecesmodel.rb', line 30 def initialize(parent) super(parent) @locations = [] @pixmaps = [] end |
Instance Method Details
#addPiece(pixmap, location) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/piecesmodel.rb', line 53 def addPiece(pixmap, location) if (2.0*rand(RAND_MAX)/(RAND_MAX+1.0)).to_i == 1 row = 0 else row = @pixmaps.size end beginInsertRows(Qt::ModelIndex.new, row, row) @pixmaps[row] = pixmap @locations[row] = location endInsertRows() end |
#data(index, role) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/piecesmodel.rb', line 36 def data(index, role) if !index.valid? return Qt::Variant.new end if role == Qt::DecorationRole return qVariantFromValue(Qt::Icon.new(@pixmaps[index.row].scaled(60, 60, Qt::KeepAspectRatio, Qt::SmoothTransformation))) elsif role == Qt::UserRole return qVariantFromValue(@pixmaps[index.row]) elsif role == Qt::UserRole + 1 return qVariantFromValue(@locations[index.row]) end return Qt::Variant.new end |
#dropMimeData(data, action, row, column, parent) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/piecesmodel.rb', line 123 def dropMimeData(data, action, row, column, parent) if !data.hasFormat("image/x-puzzle-piece") return false end if action == Qt::IgnoreAction return true end if column > 0 return false end if !parent.valid? && row < 0 endRow = @pixmaps.size elsif !parent.valid? endRow = [row, pixmaps.size()].min else endRow = parent.row end encodedData = data.data("image/x-puzzle-piece") stream = Qt::DataStream.new(encodedData, Qt::IODevice::ReadOnly) while !stream.atEnd pixmap = Qt::Pixmap.new location = Qt::Point.new stream >> pixmap >> location beginInsertRows(Qt::ModelIndex.new, endRow, endRow) @pixmaps.insert(endRow, pixmap) @locations.insert(endRow, location) endInsertRows() endRow += 1 end return true end |
#flags(index) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/piecesmodel.rb', line 66 def flags(index) if index.valid? return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled) end return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled end |
#mimeData(indexes) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/piecesmodel.rb', line 105 def mimeData(indexes) mimeData = Qt::MimeData.new encodedData = Qt::ByteArray.new stream = Qt::DataStream.new(encodedData, Qt::IODevice::WriteOnly) indexes.each do |index| if index.valid? pixmap = qVariantValue(Qt::Pixmap, data(index, Qt::UserRole)) location = data(index, Qt::UserRole+1).toPoint stream << pixmap << location end end mimeData.setData("image/x-puzzle-piece", encodedData) return mimeData end |
#mimeTypes ⇒ Object
99 100 101 102 103 |
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/piecesmodel.rb', line 99 def mimeTypes() types = [] types << "image/x-puzzle-piece" return types end |
#removeRows(row, count, parent) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/piecesmodel.rb', line 75 def removeRows(row, count, parent) if parent.valid? return false end if row >= @pixmaps.size() || row + count <= 0 return false end beginRow = [0, row].max endRow = [row + count - 1, @pixmaps.size() - 1].min beginRemoveRows(parent, beginRow, endRow) while beginRow <= endRow @pixmaps.delete(beginRow) @locations.delete(beginRow) beginRow += 1 end endRemoveRows() return true end |
#rowCount(parent) ⇒ Object
163 164 165 166 167 168 169 |
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/piecesmodel.rb', line 163 def rowCount(parent) if parent.valid? return 0 else return @pixmaps.size end end |