92 const QPalette &pal,
bool sunken,
93 int lineWidth,
int midLineWidth)
95 if (Q_UNLIKELY(!p || lineWidth < 0 || midLineWidth < 0)) {
96 qWarning(
"qDrawShadeLine: Invalid parameters");
99 PainterStateGuard painterGuard(p);
100 const qreal devicePixelRatio = p->device()->devicePixelRatio();
101 if (!qFuzzyCompare(devicePixelRatio, qreal(1))) {
103 const qreal inverseScale = qreal(1) / devicePixelRatio;
104 p->scale(inverseScale, inverseScale);
105 x1 = qRound(devicePixelRatio * x1);
106 y1 = qRound(devicePixelRatio * y1);
107 x2 = qRound(devicePixelRatio * x2);
108 y2 = qRound(devicePixelRatio * y2);
109 lineWidth = qRound(devicePixelRatio * lineWidth);
110 midLineWidth = qRound(devicePixelRatio * midLineWidth);
111 p->translate(0.5, 0.5);
113 int tlw = lineWidth*2 + midLineWidth;
114 QPen oldPen = p->pen();
116 p->setPen(pal.color(QPalette::Dark));
118 p->setPen(pal.light().color());
129 for (i=0; i<lineWidth; i++) {
130 a.setPoints(3, x1+i, y+tlw-1-i,
135 if (midLineWidth > 0) {
136 p->setPen(pal.mid().color());
137 for (i=0; i<midLineWidth; i++)
138 p->drawLine(x1+lineWidth, y+lineWidth+i,
139 x2-lineWidth, y+lineWidth+i);
142 p->setPen(pal.light().color());
144 p->setPen(pal.dark().color());
145 for (i=0; i<lineWidth; i++) {
146 a.setPoints(3, x1+i, y+tlw-i-1,
160 for (i=0; i<lineWidth; i++) {
161 a.setPoints(3, x+i, y2,
166 if (midLineWidth > 0) {
167 p->setPen(pal.mid().color());
168 for (i=0; i<midLineWidth; i++)
169 p->drawLine(x+lineWidth+i, y1+lineWidth, x+lineWidth+i, y2);
172 p->setPen(pal.light().color());
174 p->setPen(pal.dark().color());
175 for (i=0; i<lineWidth; i++) {
176 a.setPoints(3, x+lineWidth, y2-i,
178 x+tlw-i-1, y1+lineWidth);
218 const QPalette &pal,
bool sunken,
219 int lineWidth,
int midLineWidth,
222 if (w == 0 || h == 0)
224 if (Q_UNLIKELY(w < 0 || h < 0 || lineWidth < 0 || midLineWidth < 0)) {
225 qWarning(
"qDrawShadeRect: Invalid parameters");
229 PainterStateGuard painterGuard(p);
230 const qreal devicePixelRatio = p->device()->devicePixelRatioF();
231 if (!qFuzzyCompare(devicePixelRatio, qreal(1))) {
233 const qreal inverseScale = qreal(1) / devicePixelRatio;
234 p->scale(inverseScale, inverseScale);
235 x = qRound(devicePixelRatio * x);
236 y = qRound(devicePixelRatio * y);
237 w = qRound(devicePixelRatio * w);
238 h = qRound(devicePixelRatio * h);
239 lineWidth = qRound(devicePixelRatio * lineWidth);
240 midLineWidth = qRound(devicePixelRatio * midLineWidth);
241 p->translate(0.5, 0.5);
244 QPen oldPen = p->pen();
246 p->setPen(pal.dark().color());
248 p->setPen(pal.light().color());
249 int x1=x, y1=y, x2=x+w-1, y2=y+h-1;
251 if (lineWidth == 1 && midLineWidth == 0) {
252 p->drawRect(x1, y1, w-2, h-2);
254 p->setPen(pal.light().color());
256 p->setPen(pal.dark().color());
257 QLineF lines[4] = { QLineF(x1+1, y1+1, x2-2, y1+1),
258 QLineF(x1+1, y1+2, x1+1, y2-2),
259 QLineF(x1, y2, x2, y2),
260 QLineF(x2,y1, x2,y2-1) };
261 p->drawLines(lines, 4);
263 int m = lineWidth+midLineWidth;
265 for (i=0; i<lineWidth; i++) {
266 QLineF lines[4] = { QLineF(x1+i, y2-i, x1+i, y1+i),
267 QLineF(x1+i, y1+i, x2-i, y1+i),
268 QLineF(x1+k, y2-k, x2-k, y2-k),
269 QLineF(x2-k, y2-k, x2-k, y1+k) };
270 p->drawLines(lines, 4);
273 p->setPen(pal.mid().color());
275 for (i=0; i<midLineWidth; i++) {
276 p->drawRect(x1+lineWidth+i, y1+lineWidth+i, w-j-1, h-j-1);
280 p->setPen(pal.light().color());
282 p->setPen(pal.dark().color());
284 for (i=0; i<lineWidth; i++) {
285 QLineF lines[4] = { QLineF(x1+1+i, y2-i, x2-i, y2-i),
286 QLineF(x2-i, y2-i, x2-i, y1+i+1),
287 QLineF(x1+k, y2-k, x1+k, y1+k),
288 QLineF(x1+k, y1+k, x2-k, y1+k) };
289 p->drawLines(lines, 4);
294 QBrush oldBrush = p->brush();
295 int tlw = lineWidth + midLineWidth;
296 p->setPen(Qt::NoPen);
298 p->drawRect(x+tlw, y+tlw, w-2*tlw, h-2*tlw);
299 p->setBrush(oldBrush);
334 const QPalette &pal,
bool sunken,
335 int lineWidth,
const QBrush *fill)
337 if (w == 0 || h == 0)
339 if (Q_UNLIKELY(w < 0 || h < 0 || lineWidth < 0)) {
340 qWarning(
"qDrawShadePanel: Invalid parameters");
343 PainterStateGuard painterGuard(p);
344 const qreal devicePixelRatio = p->device()->devicePixelRatioF();
345 bool isTranslated =
false;
346 if (!qFuzzyCompare(devicePixelRatio, qreal(1))) {
348 const qreal inverseScale = qreal(1) / devicePixelRatio;
349 p->scale(inverseScale, inverseScale);
350 x = qRound(devicePixelRatio * x);
351 y = qRound(devicePixelRatio * y);
352 w = qRound(devicePixelRatio * w);
353 h = qRound(devicePixelRatio * h);
354 lineWidth = qRound(devicePixelRatio * lineWidth);
355 p->translate(0.5, 0.5);
359 QColor shade = pal.dark().color();
360 QColor light = pal.light().color();
362 if (fill->color() == shade)
363 shade = pal.shadow().color();
364 if (fill->color() == light)
365 light = pal.midlight().color();
367 QPen oldPen = p->pen();
369 lines.reserve(2*lineWidth);
380 for (i=0; i<lineWidth; i++) {
381 lines << QLineF(x1, y1++, x2--, y2++);
385 for (i=0; i<lineWidth; i++) {
386 lines << QLineF(x1++, y1, x2++, y2--);
397 for (i=0; i<lineWidth; i++) {
398 lines << QLineF(x1++, y1--, x2, y2--);
402 y2 = y+h-lineWidth-1;
403 for (i=0; i<lineWidth; i++) {
404 lines << QLineF(x1--, y1++, x2--, y2);
409 p->translate(-0.5, -0.5);
410 p->fillRect(x+lineWidth, y+lineWidth, w-lineWidth*2, h-lineWidth*2, *fill);
878 const QPixmap &pixmap,
const QRect &sourceRect,
const QMargins &sourceMargins,
881 , QDrawBorderPixmap::DrawingHints hints
885 QPainter::PixmapFragment d;
893 const int sourceCenterTop = sourceRect.top() + sourceMargins.top();
894 const int sourceCenterLeft = sourceRect.left() + sourceMargins.left();
895 const int sourceCenterBottom = sourceRect.bottom() - sourceMargins.bottom() + 1;
896 const int sourceCenterRight = sourceRect.right() - sourceMargins.right() + 1;
897 const int sourceCenterWidth = sourceCenterRight - sourceCenterLeft;
898 const int sourceCenterHeight = sourceCenterBottom - sourceCenterTop;
900 const int targetCenterTop = targetRect.top() + targetMargins.top();
901 const int targetCenterLeft = targetRect.left() + targetMargins.left();
902 const int targetCenterBottom = targetRect.bottom() - targetMargins.bottom() + 1;
903 const int targetCenterRight = targetRect.right() - targetMargins.right() + 1;
904 const int targetCenterWidth = targetCenterRight - targetCenterLeft;
905 const int targetCenterHeight = targetCenterBottom - targetCenterTop;
907 QVarLengthArray<qreal, 16> xTarget;
908 QVarLengthArray<qreal, 16> yTarget;
912 if (rules.horizontal != Qt::StretchTile && sourceCenterWidth != 0)
913 columns = qMax(3, 2 + qCeil(targetCenterWidth / qreal(sourceCenterWidth)));
914 if (rules.vertical != Qt::StretchTile && sourceCenterHeight != 0)
915 rows = qMax(3, 2 + qCeil(targetCenterHeight / qreal(sourceCenterHeight)));
917 xTarget.resize(columns + 1);
918 yTarget.resize(rows + 1);
920 bool oldAA = painter->testRenderHint(QPainter::Antialiasing);
921 if (painter->paintEngine()->type() != QPaintEngine::OpenGL
922 && painter->paintEngine()->type() != QPaintEngine::OpenGL2
923 && oldAA && painter->combinedTransform().type() != QTransform::TxNone) {
924 painter->setRenderHint(QPainter::Antialiasing,
false);
927 xTarget[0] = targetRect.left();
928 xTarget[1] = targetCenterLeft;
929 xTarget[columns - 1] = targetCenterRight;
930 xTarget[columns] = targetRect.left() + targetRect.width();
932 yTarget[0] = targetRect.top();
933 yTarget[1] = targetCenterTop;
934 yTarget[rows - 1] = targetCenterBottom;
935 yTarget[rows] = targetRect.top() + targetRect.height();
937 qreal dx = targetCenterWidth;
938 qreal dy = targetCenterHeight;
940 switch (rules.horizontal) {
941 case Qt::StretchTile:
942 dx = targetCenterWidth;
945 dx = sourceCenterWidth;
948 dx = targetCenterWidth / qreal(columns - 2);
952 for (
int i = 2; i < columns - 1; ++i)
953 xTarget[i] = xTarget[i - 1] + dx;
955 switch (rules.vertical) {
956 case Qt::StretchTile:
957 dy = targetCenterHeight;
960 dy = sourceCenterHeight;
963 dy = targetCenterHeight / qreal(rows - 2);
967 for (
int i = 2; i < rows - 1; ++i)
968 yTarget[i] = yTarget[i - 1] + dy;
971 if (targetMargins.top() > 0 && targetMargins.left() > 0 && sourceMargins.top() > 0 && sourceMargins.left() > 0) {
972 d.x = (0.5 * (xTarget[1] + xTarget[0]));
973 d.y = (0.5 * (yTarget[1] + yTarget[0]));
974 d.sourceLeft = sourceRect.left();
975 d.sourceTop = sourceRect.top();
976 d.width = sourceMargins.left();
977 d.height = sourceMargins.top();
978 d.scaleX = qreal(xTarget[1] - xTarget[0]) / d.width;
979 d.scaleY = qreal(yTarget[1] - yTarget[0]) / d.height;
981 opaqueData.append(d);
983 translucentData.append(d);
985 if (targetMargins.top() > 0 && targetMargins.right() > 0 && sourceMargins.top() > 0 && sourceMargins.right() > 0) {
986 d.x = (0.5 * (xTarget[columns] + xTarget[columns - 1]));
987 d.y = (0.5 * (yTarget[1] + yTarget[0]));
988 d.sourceLeft = sourceCenterRight;
989 d.sourceTop = sourceRect.top();
990 d.width = sourceMargins.right();
991 d.height = sourceMargins.top();
992 d.scaleX = qreal(xTarget[columns] - xTarget[columns - 1]) / d.width;
993 d.scaleY = qreal(yTarget[1] - yTarget[0]) / d.height;
995 opaqueData.append(d);
997 translucentData.append(d);
999 if (targetMargins.bottom() > 0 && targetMargins.left() > 0 && sourceMargins.bottom() > 0 && sourceMargins.left() > 0) {
1000 d.x = (0.5 * (xTarget[1] + xTarget[0]));
1001 d.y =(0.5 * (yTarget[rows] + yTarget[rows - 1]));
1002 d.sourceLeft = sourceRect.left();
1003 d.sourceTop = sourceCenterBottom;
1004 d.width = sourceMargins.left();
1005 d.height = sourceMargins.bottom();
1006 d.scaleX = qreal(xTarget[1] - xTarget[0]) / d.width;
1007 d.scaleY = qreal(yTarget[rows] - yTarget[rows - 1]) / d.height;
1009 opaqueData.append(d);
1011 translucentData.append(d);
1013 if (targetMargins.bottom() > 0 && targetMargins.right() > 0 && sourceMargins.bottom() > 0 && sourceMargins.right() > 0) {
1014 d.x = (0.5 * (xTarget[columns] + xTarget[columns - 1]));
1015 d.y = (0.5 * (yTarget[rows] + yTarget[rows - 1]));
1016 d.sourceLeft = sourceCenterRight;
1017 d.sourceTop = sourceCenterBottom;
1018 d.width = sourceMargins.right();
1019 d.height = sourceMargins.bottom();
1020 d.scaleX = qreal(xTarget[columns] - xTarget[columns - 1]) / d.width;
1021 d.scaleY = qreal(yTarget[rows] - yTarget[rows - 1]) / d.height;
1023 opaqueData.append(d);
1025 translucentData.append(d);
1029 if (targetCenterWidth > 0 && sourceCenterWidth > 0) {
1030 if (targetMargins.top() > 0 && sourceMargins.top() > 0) {
1032 d.sourceLeft = sourceCenterLeft;
1033 d.sourceTop = sourceRect.top();
1034 d.width = sourceCenterWidth;
1035 d.height = sourceMargins.top();
1036 d.y = (0.5 * (yTarget[1] + yTarget[0]));
1037 d.scaleX = dx / d.width;
1038 d.scaleY = qreal(yTarget[1] - yTarget[0]) / d.height;
1039 for (
int i = 1; i < columns - 1; ++i) {
1040 d.x = (0.5 * (xTarget[i + 1] + xTarget[i]));
1043 if (rules.horizontal == Qt::RepeatTile)
1044 data[data.size() - 1].width = ((xTarget[columns - 1] - xTarget[columns - 2]) / d.scaleX);
1046 if (targetMargins.bottom() > 0 && sourceMargins.bottom() > 0) {
1048 d.sourceLeft = sourceCenterLeft;
1049 d.sourceTop = sourceCenterBottom;
1050 d.width = sourceCenterWidth;
1051 d.height = sourceMargins.bottom();
1052 d.y = (0.5 * (yTarget[rows] + yTarget[rows - 1]));
1053 d.scaleX = dx / d.width;
1054 d.scaleY = qreal(yTarget[rows] - yTarget[rows - 1]) / d.height;
1055 for (
int i = 1; i < columns - 1; ++i) {
1056 d.x = (0.5 * (xTarget[i + 1] + xTarget[i]));
1059 if (rules.horizontal == Qt::RepeatTile)
1060 data[data.size() - 1].width = ((xTarget[columns - 1] - xTarget[columns - 2]) / d.scaleX);
1065 if (targetCenterHeight > 0 && sourceCenterHeight > 0) {
1066 if (targetMargins.left() > 0 && sourceMargins.left() > 0) {
1068 d.sourceLeft = sourceRect.left();
1069 d.sourceTop = sourceCenterTop;
1070 d.width = sourceMargins.left();
1071 d.height = sourceCenterHeight;
1072 d.x = (0.5 * (xTarget[1] + xTarget[0]));
1073 d.scaleX = qreal(xTarget[1] - xTarget[0]) / d.width;
1074 d.scaleY = dy / d.height;
1075 for (
int i = 1; i < rows - 1; ++i) {
1076 d.y = (0.5 * (yTarget[i + 1] + yTarget[i]));
1079 if (rules.vertical == Qt::RepeatTile)
1080 data[data.size() - 1].height = ((yTarget[rows - 1] - yTarget[rows - 2]) / d.scaleY);
1082 if (targetMargins.right() > 0 && sourceMargins.right() > 0) {
1084 d.sourceLeft = sourceCenterRight;
1085 d.sourceTop = sourceCenterTop;
1086 d.width = sourceMargins.right();
1087 d.height = sourceCenterHeight;
1088 d.x = (0.5 * (xTarget[columns] + xTarget[columns - 1]));
1089 d.scaleX = qreal(xTarget[columns] - xTarget[columns - 1]) / d.width;
1090 d.scaleY = dy / d.height;
1091 for (
int i = 1; i < rows - 1; ++i) {
1092 d.y = (0.5 * (yTarget[i + 1] + yTarget[i]));
1095 if (rules.vertical == Qt::RepeatTile)
1096 data[data.size() - 1].height = ((yTarget[rows - 1] - yTarget[rows - 2]) / d.scaleY);
1101 if (targetCenterWidth > 0 && targetCenterHeight > 0 && sourceCenterWidth > 0 && sourceCenterHeight > 0) {
1103 d.sourceLeft = sourceCenterLeft;
1104 d.sourceTop = sourceCenterTop;
1105 d.width = sourceCenterWidth;
1106 d.height = sourceCenterHeight;
1107 d.scaleX = dx / d.width;
1108 d.scaleY = dy / d.height;
1110 qreal repeatWidth = (xTarget[columns - 1] - xTarget[columns - 2]) / d.scaleX;
1111 qreal repeatHeight = (yTarget[rows - 1] - yTarget[rows - 2]) / d.scaleY;
1113 for (
int j = 1; j < rows - 1; ++j) {
1114 d.y = (0.5 * (yTarget[j + 1] + yTarget[j]));
1115 for (
int i = 1; i < columns - 1; ++i) {
1116 d.x = (0.5 * (xTarget[i + 1] + xTarget[i]));
1119 if (rules.horizontal == Qt::RepeatTile)
1120 data[data.size() - 1].width = repeatWidth;
1122 if (rules.vertical == Qt::RepeatTile) {
1123 for (
int i = 1; i < columns - 1; ++i)
1124 data[data.size() - i].height = repeatHeight;
1128 if (opaqueData.size())
1129 painter->drawPixmapFragments(opaqueData.data(), opaqueData.size(), pixmap, QPainter::OpaqueHint);
1130 if (translucentData.size())
1131 painter->drawPixmapFragments(translucentData.data(), translucentData.size(), pixmap);
1134 painter->setRenderHint(QPainter::Antialiasing,
true);