113 delete m_material.texture();
114 m_material.setTexture(texture);
119 if (xlen > 0 && ylen > 0) {
120 const int quads = (xlen - 1) * (ylen - 1);
121 static const int verticesPerQuad = 6;
122 m_geometry.allocate(xlen * ylen, verticesPerQuad * quads);
124 QSGGeometry::TexturedPoint2D *vertices = m_geometry.vertexDataAsTexturedPoint2D();
125 QList<qreal> xCoords = xDivs.coordsForSize(targetSize.width());
126 QList<qreal> yCoords = yDivs.coordsForSize(targetSize.height());
128 for (
int y = 0; y < ylen; ++y) {
129 for (
int x = 0; x < xlen; ++x, ++vertices)
130 vertices->set(xCoords[x] / dpr, yCoords[y] / dpr,
131 xDivs.at(x) / sourceSize.width(),
132 yDivs.at(y) / sourceSize.height());
135 quint16 *indices = m_geometry.indexDataAsUShort();
137 for (
int q = 0; n--; ++q) {
138 if ((q + 1) % xlen == 0)
142 indices[1] = q + xlen;
143 indices[2] = q + xlen + 1;
147 indices[4] = q + xlen + 1;
150 indices += verticesPerQuad;
154 markDirty(QSGNode::DirtyGeometry | QSGNode::DirtyMaterial);
239 if (ninePatch.isNull())
242 int w = ninePatch.width();
243 int h = ninePatch.height();
244 const QRgb *data =
reinterpret_cast<
const QRgb *>(ninePatch.constBits());
246 const QRgb black = qRgb(0,0,0);
247 const QRgb red = qRgb(255,0,0);
249 xDivs.fill(readCoords(data, 1, w - 1, 1, black), w - 2);
250 yDivs.fill(readCoords(data, w, h - 1, w, black), h - 2);
252 QList<qreal> hInsets = readCoords(data, (h - 1) * w + 1, w - 1, 1, red);
253 QList<qreal> vInsets = readCoords(data, 2 * w - 1, h - 1, w, red);
254 updateInsets(hInsets, vInsets);
256 const QSizeF sz(w - leftInset - rightInset, h - topInset - bottomInset);
257 QList<qreal> hPaddings = readCoords(data, (h - 1) * w + leftInset + 1, sz.width() - 2, 1, black);
258 QList<qreal> vPaddings = readCoords(data, (2 + topInset) * w - 1, sz.height() - 2, w, black);
259 updatePaddings(sz, hPaddings, vPaddings);
264 Q_Q(QQuickNinePatchImage);
265 qreal oldTopPadding = topPadding;
266 qreal oldLeftPadding = leftPadding;
267 qreal oldRightPadding = rightPadding;
268 qreal oldBottomPadding = bottomPadding;
270 if (horizontal.size() >= 2) {
271 leftPadding = horizontal.first();
272 rightPadding = size.width() - horizontal.last() - 2;
278 if (vertical.size() >= 2) {
279 topPadding = vertical.first();
280 bottomPadding = size.height() - vertical.last() - 2;
286 if (!qFuzzyCompare(oldTopPadding, topPadding))
287 emit q->topPaddingChanged();
288 if (!qFuzzyCompare(oldBottomPadding, bottomPadding))
289 emit q->bottomPaddingChanged();
290 if (!qFuzzyCompare(oldLeftPadding, leftPadding))
291 emit q->leftPaddingChanged();
292 if (!qFuzzyCompare(oldRightPadding, rightPadding))
293 emit q->rightPaddingChanged();
298 Q_Q(QQuickNinePatchImage);
299 qreal oldTopInset = topInset;
300 qreal oldLeftInset = leftInset;
301 qreal oldRightInset = rightInset;
302 qreal oldBottomInset = bottomInset;
304 if (horizontal.size() >= 2 && horizontal.first() == 0)
305 leftInset = horizontal.at(1);
309 if (horizontal.size() == 2 && horizontal.first() > 0)
310 rightInset = horizontal.last() - horizontal.first();
311 else if (horizontal.size() == 4)
312 rightInset = horizontal.last() - horizontal.at(2);
316 if (vertical.size() >= 2 && vertical.first() == 0)
317 topInset = vertical.at(1);
321 if (vertical.size() == 2 && vertical.first() > 0)
322 bottomInset = vertical.last() - vertical.first();
323 else if (vertical.size() == 4)
324 bottomInset = vertical.last() - vertical.at(2);
328 if (!qFuzzyCompare(oldTopInset, topInset))
329 emit q->topInsetChanged();
330 if (!qFuzzyCompare(oldBottomInset, bottomInset))
331 emit q->bottomInsetChanged();
332 if (!qFuzzyCompare(oldLeftInset, leftInset))
333 emit q->leftInsetChanged();
334 if (!qFuzzyCompare(oldRightInset, rightInset))
335 emit q->rightInsetChanged();
395 Q_D(QQuickNinePatchImage);
396 if (QFileInfo(d->url.fileName()).completeSuffix().toLower() == QLatin1String(
"9.png")) {
401 d->resetNode = d->ninePatch.isNull();
403 d->ninePatch = d->currentPix->image();
404 if (d->ninePatch.depth() != 32)
405 d->ninePatch = std::move(d->ninePatch).convertToFormat(QImage::Format_ARGB32);
407 int w = d->ninePatch.width();
408 int h = d->ninePatch.height();
409 d->currentPix->setImage(QImage(d->ninePatch.constBits() + 4 * (w + 1), w - 2, h - 2, d->ninePatch.bytesPerLine(), d->ninePatch.format()));
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
430 d->resetNode = !d->ninePatch.isNull();
431 d->ninePatch = QImage();
433 QQuickImage::pixmapChange();
438 Q_D(QQuickNinePatchImage);
444 d->resetNode =
false;
447 if (d->ninePatch.isNull())
448 return QQuickImage::updatePaintNode(oldNode, data);
451 QImage image = d->currentPix->image();
452 if (!sz.isValid() || image.isNull()) {
454 d->provider->updateTexture(
nullptr);
463#ifdef QSG_RUNTIME_DESCRIPTION
464 qsgnode_set_description(patchNode, QString::fromLatin1(
"QQuickNinePatchImage: '%1'").arg(d->url.toString()));
474 QSGTexture *texture = window()->createTextureFromImage(image);
475 patchNode->initialize(texture, sz * d->devicePixelRatio, image.size(), d->xDivs, d->yDivs, d->devicePixelRatio);
476 auto patchNodeMaterial =
static_cast<QSGTextureMaterial *>(patchNode->material());
477 patchNodeMaterial->setFiltering(d->smooth ? QSGTexture::Linear : QSGTexture::Nearest);
void initialize(QSGTexture *texture, const QSizeF &targetSize, const QSize &sourceSize, const QQuickNinePatchData &xDivs, const QQuickNinePatchData &yDivs, qreal dpr)