1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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
| QPixmap ScaleByNinePatch(const QPixmap& src, QSize destSize, bool keepVertexImageSize, int pos1Width, int pos1Height, int pos3Width, int pos7Height, Qt::TransformationMode mode = Qt::SmoothTransformation) { pos1Width = qMax(pos1Width, 0); pos1Height = qMax(pos1Height, 0); pos3Width = qMax(pos3Width, 0); pos7Height = qMax(pos7Height, 0);
int handlePixW = 0, handlePixH = 0; const QPixmap* handlePix = nullptr; QPixmap keepRatioScaledPix;
if (keepVertexImageSize) { if (pos1Width + pos3Width > destSize.width() || pos1Height + pos7Height > destSize.height()) return src;
handlePixW = src.width(); handlePixH = src.height(); handlePix = &src; } else { const qreal srcW = src.width(); const qreal srcH = src.height();
keepRatioScaledPix = src.scaled(destSize, Qt::KeepAspectRatio, mode); handlePixW = keepRatioScaledPix.width(); handlePixH = keepRatioScaledPix.height(); qreal krsPixWidth = handlePixW; qreal krsPixHeight = handlePixH;
pos1Width = krsPixWidth * (qreal)pos1Width / srcW; pos1Height = krsPixHeight * (qreal)pos1Height / srcH; pos3Width = krsPixWidth * (qreal)pos3Width / srcW; pos7Height = krsPixHeight * (qreal)pos7Height / srcH;
handlePix = &keepRatioScaledPix; }
QPixmap pix1 = handlePix->copy(0, 0, pos1Width, pos1Height); QPixmap pix2 = handlePix->copy(pos1Width, 0, qMax(handlePixW - pos1Width - pos3Width, 0), pos1Height); QPixmap pix3 = handlePix->copy(qMax(handlePixW - pos3Width, 0), 0, pos3Width, pos1Height); QPixmap pix4 = handlePix->copy(0, pos1Height, pos1Width, qMax(handlePixH - pos1Height - pos7Height, 0)); QPixmap pix5 = handlePix->copy(pos1Width, pos1Height, qMax(handlePixW - pos1Width - pos3Width, 0), qMax(handlePixH - pos1Height - pos7Height, 0)); QPixmap pix6 = handlePix->copy(qMax(handlePixW - pos3Width, 0), pos1Height, pos3Width, qMax(handlePixH - pos1Height - pos7Height, 0)); QPixmap pix7 = handlePix->copy(0, qMax(handlePixH - pos7Height, 0), pos1Width, pos7Height); QPixmap pix8 = handlePix->copy(pos1Width, qMax(handlePixH - pos7Height, 0), qMax(handlePixW - pos1Width - pos3Width, 0), pos7Height); QPixmap pix9 = handlePix->copy(qMax(handlePixW - pos3Width, 0), qMax(handlePixH - pos7Height, 0), pos3Width, pos7Height);
pix2 = pix2.scaled(qMax(destSize.width() - pos1Width - pos3Width, 0), pos1Height, Qt::IgnoreAspectRatio, mode); pix4 = pix4.scaled(pos1Width, qMax(destSize.height() - pos1Height - pos7Height, 0), Qt::IgnoreAspectRatio, mode); pix5 = pix5.scaled(qMax(destSize.width() - pos1Width - pos3Width, 0), qMax(destSize.height() - pos1Height - pos7Height, 0), Qt::IgnoreAspectRatio, mode); pix6 = pix6.scaled(pos3Width, qMax(destSize.height() - pos1Height - pos7Height, 0), Qt::IgnoreAspectRatio, mode); pix8 = pix8.scaled(qMax(destSize.width() - pos1Width - pos3Width, 0), pos7Height, Qt::IgnoreAspectRatio, mode);
QPixmap dest(destSize); dest.fill(Qt::transparent);
QPainter painter(&dest);
painter.drawPixmap(0, 0, pix1); painter.drawPixmap(pos1Width, 0, pix2); painter.drawPixmap(qMax(destSize.width() - pos3Width, 0), 0, pix3); painter.drawPixmap(0, pos1Height, pix4); painter.drawPixmap(pos1Width, pos1Height, pix5); painter.drawPixmap(qMax(destSize.width() - pos3Width, 0), pos1Height, pix6); painter.drawPixmap(0, qMax(destSize.height() - pos7Height, 0), pix7); painter.drawPixmap(pos1Width, qMax(destSize.height() - pos7Height, 0), pix8); painter.drawPixmap(qMax(destSize.width() - pos3Width, 0), qMax(destSize.height() - pos7Height, 0), pix9); painter.end();
return dest; }
|