| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/graphicsview/qgraphicsscene_bsp.cpp |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||
| 2 | - | |||||||||||||
| 3 | - | |||||||||||||
| 4 | - | |||||||||||||
| 5 | - | |||||||||||||
| 6 | - | |||||||||||||
| 7 | class QGraphicsSceneInsertItemBspTreeVisitor : public QGraphicsSceneBspTreeVisitor | - | ||||||||||||
| 8 | { | - | ||||||||||||
| 9 | public: | - | ||||||||||||
| 10 | QGraphicsItem *item; | - | ||||||||||||
| 11 | - | |||||||||||||
| 12 | void visit(QList<QGraphicsItem *> *items) override | - | ||||||||||||
| 13 | { items->prepend(item); } never executed: end of block | 0 | ||||||||||||
| 14 | }; | - | ||||||||||||
| 15 | - | |||||||||||||
| 16 | class QGraphicsSceneRemoveItemBspTreeVisitor : public QGraphicsSceneBspTreeVisitor | - | ||||||||||||
| 17 | { | - | ||||||||||||
| 18 | public: | - | ||||||||||||
| 19 | QGraphicsItem *item; | - | ||||||||||||
| 20 | - | |||||||||||||
| 21 | void visit(QList<QGraphicsItem *> *items) override | - | ||||||||||||
| 22 | { items->removeAll(item); } never executed: end of block | 0 | ||||||||||||
| 23 | }; | - | ||||||||||||
| 24 | - | |||||||||||||
| 25 | class QGraphicsSceneFindItemBspTreeVisitor : public QGraphicsSceneBspTreeVisitor | - | ||||||||||||
| 26 | { | - | ||||||||||||
| 27 | public: | - | ||||||||||||
| 28 | QList<QGraphicsItem *> *foundItems; | - | ||||||||||||
| 29 | bool onlyTopLevelItems; | - | ||||||||||||
| 30 | - | |||||||||||||
| 31 | void visit(QList<QGraphicsItem *> *items) override | - | ||||||||||||
| 32 | { | - | ||||||||||||
| 33 | for (int i = 0; i < items->size()
| 0 | ||||||||||||
| 34 | QGraphicsItem *item = items->at(i); | - | ||||||||||||
| 35 | if (onlyTopLevelItems
| 0 | ||||||||||||
| 36 | item = item->topLevelItem(); never executed: item = item->topLevelItem(); | 0 | ||||||||||||
| 37 | if (!item->d_func()->itemDiscovered
| 0 | ||||||||||||
| 38 | item->d_func()->itemDiscovered = 1; | - | ||||||||||||
| 39 | foundItems->prepend(item); | - | ||||||||||||
| 40 | } never executed: end of block | 0 | ||||||||||||
| 41 | } never executed: end of block | 0 | ||||||||||||
| 42 | } never executed: end of block | 0 | ||||||||||||
| 43 | }; | - | ||||||||||||
| 44 | - | |||||||||||||
| 45 | QGraphicsSceneBspTree::QGraphicsSceneBspTree() | - | ||||||||||||
| 46 | : leafCnt(0) | - | ||||||||||||
| 47 | { | - | ||||||||||||
| 48 | insertVisitor = new QGraphicsSceneInsertItemBspTreeVisitor; | - | ||||||||||||
| 49 | removeVisitor = new QGraphicsSceneRemoveItemBspTreeVisitor; | - | ||||||||||||
| 50 | findVisitor = new QGraphicsSceneFindItemBspTreeVisitor; | - | ||||||||||||
| 51 | } never executed: end of block | 0 | ||||||||||||
| 52 | - | |||||||||||||
| 53 | QGraphicsSceneBspTree::~QGraphicsSceneBspTree() | - | ||||||||||||
| 54 | { | - | ||||||||||||
| 55 | delete insertVisitor; | - | ||||||||||||
| 56 | delete removeVisitor; | - | ||||||||||||
| 57 | delete findVisitor; | - | ||||||||||||
| 58 | } never executed: end of block | 0 | ||||||||||||
| 59 | - | |||||||||||||
| 60 | void QGraphicsSceneBspTree::initialize(const QRectF &rect, int depth) | - | ||||||||||||
| 61 | { | - | ||||||||||||
| 62 | this->rect = rect; | - | ||||||||||||
| 63 | leafCnt = 0; | - | ||||||||||||
| 64 | nodes.resize((1 << (depth + 1)) - 1); | - | ||||||||||||
| 65 | nodes.fill(Node()); | - | ||||||||||||
| 66 | leaves.resize(1 << depth); | - | ||||||||||||
| 67 | leaves.fill(QList<QGraphicsItem *>()); | - | ||||||||||||
| 68 | - | |||||||||||||
| 69 | initialize(rect, depth, 0); | - | ||||||||||||
| 70 | } never executed: end of block | 0 | ||||||||||||
| 71 | - | |||||||||||||
| 72 | void QGraphicsSceneBspTree::clear() | - | ||||||||||||
| 73 | { | - | ||||||||||||
| 74 | leafCnt = 0; | - | ||||||||||||
| 75 | nodes.clear(); | - | ||||||||||||
| 76 | leaves.clear(); | - | ||||||||||||
| 77 | } never executed: end of block | 0 | ||||||||||||
| 78 | - | |||||||||||||
| 79 | void QGraphicsSceneBspTree::insertItem(QGraphicsItem *item, const QRectF &rect) | - | ||||||||||||
| 80 | { | - | ||||||||||||
| 81 | insertVisitor->item = item; | - | ||||||||||||
| 82 | climbTree(insertVisitor, rect); | - | ||||||||||||
| 83 | } never executed: end of block | 0 | ||||||||||||
| 84 | - | |||||||||||||
| 85 | void QGraphicsSceneBspTree::removeItem(QGraphicsItem *item, const QRectF &rect) | - | ||||||||||||
| 86 | { | - | ||||||||||||
| 87 | removeVisitor->item = item; | - | ||||||||||||
| 88 | climbTree(removeVisitor, rect); | - | ||||||||||||
| 89 | } never executed: end of block | 0 | ||||||||||||
| 90 | - | |||||||||||||
| 91 | void QGraphicsSceneBspTree::removeItems(const QSet<QGraphicsItem *> &items) | - | ||||||||||||
| 92 | { | - | ||||||||||||
| 93 | for (int i = 0; i < leaves.size()
| 0 | ||||||||||||
| 94 | QList<QGraphicsItem *> newItemList; | - | ||||||||||||
| 95 | const QList<QGraphicsItem *> &oldItemList = leaves[i]; | - | ||||||||||||
| 96 | for (int j = 0; j < oldItemList.size()
| 0 | ||||||||||||
| 97 | QGraphicsItem *item = oldItemList.at(j); | - | ||||||||||||
| 98 | if (!items.contains(item)
| 0 | ||||||||||||
| 99 | newItemList << item; never executed: newItemList << item; | 0 | ||||||||||||
| 100 | } never executed: end of block | 0 | ||||||||||||
| 101 | leaves[i] = newItemList; | - | ||||||||||||
| 102 | } never executed: end of block | 0 | ||||||||||||
| 103 | } never executed: end of block | 0 | ||||||||||||
| 104 | - | |||||||||||||
| 105 | QList<QGraphicsItem *> QGraphicsSceneBspTree::items(const QRectF &rect, bool onlyTopLevelItems) const | - | ||||||||||||
| 106 | { | - | ||||||||||||
| 107 | QList<QGraphicsItem *> tmp; | - | ||||||||||||
| 108 | findVisitor->foundItems = &tmp; | - | ||||||||||||
| 109 | findVisitor->onlyTopLevelItems = onlyTopLevelItems; | - | ||||||||||||
| 110 | climbTree(findVisitor, rect); | - | ||||||||||||
| 111 | - | |||||||||||||
| 112 | for (int i = 0; i < tmp.size()
| 0 | ||||||||||||
| 113 | tmp.at(i)->d_ptr->itemDiscovered = 0; never executed: tmp.at(i)->d_ptr->itemDiscovered = 0; | 0 | ||||||||||||
| 114 | return never executed: tmp;return tmp;never executed: return tmp; | 0 | ||||||||||||
| 115 | } | - | ||||||||||||
| 116 | - | |||||||||||||
| 117 | int QGraphicsSceneBspTree::leafCount() const | - | ||||||||||||
| 118 | { | - | ||||||||||||
| 119 | return never executed: leafCnt;return leafCnt;never executed: return leafCnt; | 0 | ||||||||||||
| 120 | } | - | ||||||||||||
| 121 | - | |||||||||||||
| 122 | QString QGraphicsSceneBspTree::debug(int index) const | - | ||||||||||||
| 123 | { | - | ||||||||||||
| 124 | const Node *node = &nodes.at(index); | - | ||||||||||||
| 125 | - | |||||||||||||
| 126 | QString tmp; | - | ||||||||||||
| 127 | if (node->type == Node::Leaf
| 0 | ||||||||||||
| 128 | QRectF rect = rectForIndex(index); | - | ||||||||||||
| 129 | if (!leaves[node->leafIndex].isEmpty()
| 0 | ||||||||||||
| 130 | tmp += QString::fromLatin1("[%1, %2, %3, %4] contains %5 items\n") | - | ||||||||||||
| 131 | .arg(rect.left()).arg(rect.top()) | - | ||||||||||||
| 132 | .arg(rect.width()).arg(rect.height()) | - | ||||||||||||
| 133 | .arg(leaves[node->leafIndex].size()); | - | ||||||||||||
| 134 | } never executed: end of block | 0 | ||||||||||||
| 135 | } never executed: else {end of block | 0 | ||||||||||||
| 136 | if (node->type == Node::Horizontal
| 0 | ||||||||||||
| 137 | tmp += debug(firstChildIndex(index)); | - | ||||||||||||
| 138 | tmp += debug(firstChildIndex(index) + 1); | - | ||||||||||||
| 139 | } never executed: else {end of block | 0 | ||||||||||||
| 140 | tmp += debug(firstChildIndex(index)); | - | ||||||||||||
| 141 | tmp += debug(firstChildIndex(index) + 1); | - | ||||||||||||
| 142 | } never executed: end of block | 0 | ||||||||||||
| 143 | } | - | ||||||||||||
| 144 | - | |||||||||||||
| 145 | return never executed: tmp;return tmp;never executed: return tmp; | 0 | ||||||||||||
| 146 | } | - | ||||||||||||
| 147 | - | |||||||||||||
| 148 | void QGraphicsSceneBspTree::initialize(const QRectF &rect, int depth, int index) | - | ||||||||||||
| 149 | { | - | ||||||||||||
| 150 | Node *node = &nodes[index]; | - | ||||||||||||
| 151 | if (index == 0
| 0 | ||||||||||||
| 152 | node->type = Node::Horizontal; | - | ||||||||||||
| 153 | node->offset = rect.center().x(); | - | ||||||||||||
| 154 | } never executed: end of block | 0 | ||||||||||||
| 155 | - | |||||||||||||
| 156 | if (depth
| 0 | ||||||||||||
| 157 | Node::Type type; | - | ||||||||||||
| 158 | QRectF rect1, rect2; | - | ||||||||||||
| 159 | qreal offset1, offset2; | - | ||||||||||||
| 160 | - | |||||||||||||
| 161 | if (node->type == Node::Horizontal
| 0 | ||||||||||||
| 162 | type = Node::Vertical; | - | ||||||||||||
| 163 | rect1.setRect(rect.left(), rect.top(), rect.width(), rect.height() / 2); | - | ||||||||||||
| 164 | rect2.setRect(rect1.left(), rect1.bottom(), rect1.width(), rect.height() - rect1.height()); | - | ||||||||||||
| 165 | offset1 = rect1.center().x(); | - | ||||||||||||
| 166 | offset2 = rect2.center().x(); | - | ||||||||||||
| 167 | } never executed: else {end of block | 0 | ||||||||||||
| 168 | type = Node::Horizontal; | - | ||||||||||||
| 169 | rect1.setRect(rect.left(), rect.top(), rect.width() / 2, rect.height()); | - | ||||||||||||
| 170 | rect2.setRect(rect1.right(), rect1.top(), rect.width() - rect1.width(), rect1.height()); | - | ||||||||||||
| 171 | offset1 = rect1.center().y(); | - | ||||||||||||
| 172 | offset2 = rect2.center().y(); | - | ||||||||||||
| 173 | } never executed: end of block | 0 | ||||||||||||
| 174 | - | |||||||||||||
| 175 | int childIndex = firstChildIndex(index); | - | ||||||||||||
| 176 | - | |||||||||||||
| 177 | Node *child = &nodes[childIndex]; | - | ||||||||||||
| 178 | child->offset = offset1; | - | ||||||||||||
| 179 | child->type = type; | - | ||||||||||||
| 180 | - | |||||||||||||
| 181 | child = &nodes[childIndex + 1]; | - | ||||||||||||
| 182 | child->offset = offset2; | - | ||||||||||||
| 183 | child->type = type; | - | ||||||||||||
| 184 | - | |||||||||||||
| 185 | initialize(rect1, depth - 1, childIndex); | - | ||||||||||||
| 186 | initialize(rect2, depth - 1, childIndex + 1); | - | ||||||||||||
| 187 | } never executed: else {end of block | 0 | ||||||||||||
| 188 | node->type = Node::Leaf; | - | ||||||||||||
| 189 | node->leafIndex = leafCnt++; | - | ||||||||||||
| 190 | } never executed: end of block | 0 | ||||||||||||
| 191 | } | - | ||||||||||||
| 192 | - | |||||||||||||
| 193 | void QGraphicsSceneBspTree::climbTree(QGraphicsSceneBspTreeVisitor *visitor, const QRectF &rect, int index) const | - | ||||||||||||
| 194 | { | - | ||||||||||||
| 195 | if (nodes.isEmpty()
| 0 | ||||||||||||
| 196 | return; never executed: return; | 0 | ||||||||||||
| 197 | - | |||||||||||||
| 198 | const Node &node = nodes.at(index); | - | ||||||||||||
| 199 | const int childIndex = firstChildIndex(index); | - | ||||||||||||
| 200 | - | |||||||||||||
| 201 | switch (node.type) { | - | ||||||||||||
| 202 | case never executed: Node::Leaf:case Node::Leaf:never executed: {case Node::Leaf: | 0 | ||||||||||||
| 203 | visitor->visit(const_cast<QList<QGraphicsItem*>*>(&leaves[node.leafIndex])); | - | ||||||||||||
| 204 | break; never executed: break; | 0 | ||||||||||||
| 205 | } | - | ||||||||||||
| 206 | case never executed: Node::Vertical:case Node::Vertical:never executed: case Node::Vertical: | 0 | ||||||||||||
| 207 | if (rect.left() < node.offset
| 0 | ||||||||||||
| 208 | climbTree(visitor, rect, childIndex); | - | ||||||||||||
| 209 | if (rect.right() >= node.offset
| 0 | ||||||||||||
| 210 | climbTree(visitor, rect, childIndex + 1); never executed: climbTree(visitor, rect, childIndex + 1); | 0 | ||||||||||||
| 211 | } never executed: else {end of block | 0 | ||||||||||||
| 212 | climbTree(visitor, rect, childIndex + 1); | - | ||||||||||||
| 213 | } never executed: end of block | 0 | ||||||||||||
| 214 | break; never executed: break; | 0 | ||||||||||||
| 215 | case never executed: Node::Horizontal:case Node::Horizontal:never executed: case Node::Horizontal: | 0 | ||||||||||||
| 216 | if (rect.top() < node.offset
| 0 | ||||||||||||
| 217 | climbTree(visitor, rect, childIndex); | - | ||||||||||||
| 218 | if (rect.bottom() >= node.offset
| 0 | ||||||||||||
| 219 | climbTree(visitor, rect, childIndex + 1); never executed: climbTree(visitor, rect, childIndex + 1); | 0 | ||||||||||||
| 220 | } never executed: else {end of block | 0 | ||||||||||||
| 221 | climbTree(visitor, rect, childIndex + 1); | - | ||||||||||||
| 222 | } never executed: end of block | 0 | ||||||||||||
| 223 | } | - | ||||||||||||
| 224 | } never executed: end of block | 0 | ||||||||||||
| 225 | - | |||||||||||||
| 226 | QRectF QGraphicsSceneBspTree::rectForIndex(int index) const | - | ||||||||||||
| 227 | { | - | ||||||||||||
| 228 | if (index <= 0
| 0 | ||||||||||||
| 229 | return never executed: rect;return rect;never executed: return rect; | 0 | ||||||||||||
| 230 | - | |||||||||||||
| 231 | int parentIdx = parentIndex(index); | - | ||||||||||||
| 232 | QRectF rect = rectForIndex(parentIdx); | - | ||||||||||||
| 233 | const Node *parent = &nodes.at(parentIdx); | - | ||||||||||||
| 234 | - | |||||||||||||
| 235 | if (parent->type == Node::Horizontal
| 0 | ||||||||||||
| 236 | if (index & 1
| 0 | ||||||||||||
| 237 | rect.setRight(parent->offset); never executed: rect.setRight(parent->offset); | 0 | ||||||||||||
| 238 | else | - | ||||||||||||
| 239 | rect.setLeft(parent->offset); never executed: rect.setLeft(parent->offset); | 0 | ||||||||||||
| 240 | } else { | - | ||||||||||||
| 241 | if (index & 1
| 0 | ||||||||||||
| 242 | rect.setBottom(parent->offset); never executed: rect.setBottom(parent->offset); | 0 | ||||||||||||
| 243 | else | - | ||||||||||||
| 244 | rect.setTop(parent->offset); never executed: rect.setTop(parent->offset); | 0 | ||||||||||||
| 245 | } | - | ||||||||||||
| 246 | - | |||||||||||||
| 247 | return never executed: rect;return rect;never executed: return rect; | 0 | ||||||||||||
| 248 | } | - | ||||||||||||
| 249 | - | |||||||||||||
| 250 | - | |||||||||||||
| Switch to Source code | Preprocessed file |