1121 p.setClipRegion(region); |
1156 p.setClipRegion(region); |
1122 p.drawRect(110, 110, 10, 10); |
1157 p.drawRect(110, 110, 10, 10); |
1123 } |
1158 } |
1124 } |
1159 } |
1125 |
1160 |
|
1161 QTransform tst_QPainter::transformForAngle(qreal angle) |
|
1162 { |
|
1163 const qreal inv_dist_to_plane = 1. / 1024.; |
|
1164 |
|
1165 QTransform transform; |
|
1166 |
|
1167 QTransform rotTrans; |
|
1168 rotTrans.translate(-40, 0); |
|
1169 QTransform rotTrans2; |
|
1170 rotTrans2.translate(40, 0); |
|
1171 |
|
1172 qreal rad = angle * 2. * M_PI / 360.; |
|
1173 qreal c = ::cos(rad); |
|
1174 qreal s = ::sin(rad); |
|
1175 |
|
1176 qreal x = 0; |
|
1177 qreal y = 80; |
|
1178 qreal z = 0; |
|
1179 |
|
1180 qreal len = x * x + y * y + z * z; |
|
1181 if (len != 1.) { |
|
1182 len = ::sqrt(len); |
|
1183 x /= len; |
|
1184 y /= len; |
|
1185 z /= len; |
|
1186 } |
|
1187 |
|
1188 QTransform rot(x*x*(1-c)+c, x*y*(1-c)-z*s, x*z*(1-c)+y*s*inv_dist_to_plane, |
|
1189 y*x*(1-c)+z*s, y*y*(1-c)+c, y*z*(1-c)-x*s*inv_dist_to_plane, |
|
1190 0, 0, 1); |
|
1191 |
|
1192 transform *= rotTrans; |
|
1193 transform *= rot; |
|
1194 transform *= rotTrans2; |
|
1195 |
|
1196 return transform; |
|
1197 } |
|
1198 |
|
1199 void tst_QPainter::drawRoundedRect() |
|
1200 { |
|
1201 QImage surface(100, 100, QImage::Format_RGB16); |
|
1202 surface.fill(QColor(255,255,255).rgb()); |
|
1203 QPainter p(&surface); |
|
1204 |
|
1205 p.setPen(QPen(Qt::black, 1)); |
|
1206 p.setBrush(Qt::red); |
|
1207 |
|
1208 QBENCHMARK { |
|
1209 p.drawRoundedRect(QRectF(.5, .5, 80, 80), 10, 10); |
|
1210 } |
|
1211 } |
|
1212 |
|
1213 void tst_QPainter::drawScaledRoundedRect() |
|
1214 { |
|
1215 QImage surface(400, 400, QImage::Format_RGB16); |
|
1216 surface.fill(QColor(255,255,255).rgb()); |
|
1217 QPainter p(&surface); |
|
1218 |
|
1219 p.setPen(QPen(Qt::black, 1)); |
|
1220 p.setBrush(Qt::red); |
|
1221 p.scale(3, 3); |
|
1222 |
|
1223 QBENCHMARK { |
|
1224 p.drawRoundedRect(10, 10, 80, 80, 10, 10); |
|
1225 } |
|
1226 } |
|
1227 |
|
1228 void tst_QPainter::drawTransformedRoundedRect() |
|
1229 { |
|
1230 QImage surface(400, 400, QImage::Format_RGB16); |
|
1231 surface.fill(QColor(255,255,255).rgb()); |
|
1232 QPainter p(&surface); |
|
1233 |
|
1234 p.setPen(QPen(Qt::black, 1)); |
|
1235 p.setBrush(Qt::red); |
|
1236 |
|
1237 QBENCHMARK { |
|
1238 p.setWorldTransform(QTransform(0.956957, 0, 0.000704124, 0, 1, 0, 16.141, 0, 0.735953)); |
|
1239 p.drawRoundedRect(100, 100, 80, 80, 10, 10); |
|
1240 } |
|
1241 } |
|
1242 |
|
1243 void tst_QPainter::drawAntialiasedRoundedRect() |
|
1244 { |
|
1245 QImage surface(100, 100, QImage::Format_RGB16); |
|
1246 surface.fill(QColor(255,255,255).rgb()); |
|
1247 QPainter p(&surface); |
|
1248 |
|
1249 p.setRenderHint(QPainter::Antialiasing, true); |
|
1250 p.setPen(QPen(Qt::black, 1)); |
|
1251 p.setBrush(Qt::red); |
|
1252 |
|
1253 QBENCHMARK { |
|
1254 p.drawRoundedRect(QRectF(.5, .5, 80, 80), 10, 10); |
|
1255 } |
|
1256 } |
|
1257 |
|
1258 void tst_QPainter::drawScaledAntialiasedRoundedRect_data() |
|
1259 { |
|
1260 QTest::addColumn<float>("scale"); |
|
1261 |
|
1262 for (float i = 0; i < 3; i += .1) |
|
1263 QTest::newRow(QString(QLatin1String("scale=%1")).arg(i).toLatin1()) << i; |
|
1264 } |
|
1265 |
|
1266 void tst_QPainter::drawScaledAntialiasedRoundedRect() |
|
1267 { |
|
1268 QFETCH(float, scale); |
|
1269 |
|
1270 QImage surface(400, 400, QImage::Format_RGB16); |
|
1271 surface.fill(QColor(255,255,255).rgb()); |
|
1272 QPainter p(&surface); |
|
1273 |
|
1274 p.setRenderHint(QPainter::Antialiasing, true); |
|
1275 p.setPen(QPen(Qt::black, 1)); |
|
1276 p.setBrush(Qt::red); |
|
1277 p.scale(scale, scale); |
|
1278 |
|
1279 QBENCHMARK { |
|
1280 p.drawRoundedRect(10, 10, 80, 80, 10, 10); |
|
1281 } |
|
1282 } |
|
1283 |
|
1284 void tst_QPainter::drawTransformedAntialiasedRoundedRect_data() |
|
1285 { |
|
1286 QTest::addColumn<QTransform>("transform"); |
|
1287 |
|
1288 for (float angle = 0; angle < 360; angle += 10) |
|
1289 QTest::newRow(QString(QLatin1String("angle=%1")).arg(angle).toLatin1()) << transformForAngle(angle); |
|
1290 } |
|
1291 |
|
1292 void tst_QPainter::drawTransformedAntialiasedRoundedRect() |
|
1293 { |
|
1294 QFETCH(QTransform, transform); |
|
1295 |
|
1296 QImage surface(400, 400, QImage::Format_RGB16); |
|
1297 surface.fill(QColor(255,255,255).rgb()); |
|
1298 QPainter p(&surface); |
|
1299 |
|
1300 p.setRenderHint(QPainter::Antialiasing, true); |
|
1301 p.setPen(QPen(Qt::black, 1)); |
|
1302 p.setBrush(Qt::red); |
|
1303 |
|
1304 QBENCHMARK { |
|
1305 p.setWorldTransform(transform); |
|
1306 p.drawRoundedRect(100, 100, 80, 80, 10, 10); |
|
1307 } |
|
1308 } |
|
1309 |
|
1310 void tst_QPainter::drawImageRoundedRect() |
|
1311 { |
|
1312 //setup image |
|
1313 const int radius = 10; |
|
1314 QImage rectImage(81, 81, QImage::Format_ARGB32_Premultiplied); |
|
1315 rectImage.fill(0); |
|
1316 QPainter rp(&rectImage); |
|
1317 rp.setRenderHint(QPainter::Antialiasing); |
|
1318 rp.setPen(Qt::black); |
|
1319 rp.setBrush(Qt::red); |
|
1320 rp.drawRoundedRect(QRectF(.5, .5, 80, 80), radius, radius); |
|
1321 |
|
1322 //setup surface |
|
1323 QImage surface(100, 100, QImage::Format_RGB16); |
|
1324 surface.fill(QColor(255,255,255).rgb()); |
|
1325 QPainter p(&surface); |
|
1326 |
|
1327 QBENCHMARK { |
|
1328 p.drawImage(0,0, rectImage); |
|
1329 } |
|
1330 } |
|
1331 |
|
1332 void tst_QPainter::drawScaledImageRoundedRect_data() |
|
1333 { |
|
1334 QTest::addColumn<int>("imageType"); |
|
1335 |
|
1336 QTest::newRow("imagetype=ARGB32_Pre") << (int)QImage::Format_ARGB32_Premultiplied; |
|
1337 QTest::newRow("imagetype=ARGB8565_Pre") << (int)QImage::Format_ARGB8565_Premultiplied; |
|
1338 } |
|
1339 |
|
1340 void tst_QPainter::drawScaledImageRoundedRect() |
|
1341 { |
|
1342 QFETCH(int, imageType); |
|
1343 |
|
1344 //setup image |
|
1345 const int radius = 10; |
|
1346 QImage rectImage(81, 81, (QImage::Format)imageType); |
|
1347 rectImage.fill(0); |
|
1348 QPainter rp(&rectImage); |
|
1349 rp.setRenderHint(QPainter::Antialiasing); |
|
1350 rp.setPen(Qt::black); |
|
1351 rp.setBrush(Qt::red); |
|
1352 rp.drawRoundedRect(QRectF(.5, .5, 80, 80), radius, radius); |
|
1353 |
|
1354 //setup surface |
|
1355 QImage surface(400, 400, QImage::Format_RGB16); |
|
1356 surface.fill(QColor(255,255,255).rgb()); |
|
1357 QPainter p(&surface); |
|
1358 p.scale(3, 3); |
|
1359 |
|
1360 QBENCHMARK { |
|
1361 p.drawImage(0,0, rectImage); |
|
1362 } |
|
1363 } |
|
1364 |
|
1365 void tst_QPainter::drawTransformedImageRoundedRect_data() |
|
1366 { |
|
1367 QTest::addColumn<int>("imageType"); |
|
1368 |
|
1369 QTest::newRow("imagetype=ARGB32_Pre") << (int)QImage::Format_ARGB32_Premultiplied; |
|
1370 QTest::newRow("imagetype=ARGB8565_Pre") << (int)QImage::Format_ARGB8565_Premultiplied; |
|
1371 } |
|
1372 |
|
1373 void tst_QPainter::drawTransformedImageRoundedRect() |
|
1374 { |
|
1375 QFETCH(int, imageType); |
|
1376 |
|
1377 //setup image |
|
1378 const int radius = 10; |
|
1379 QImage rectImage(81, 81, (QImage::Format)imageType); |
|
1380 rectImage.fill(0); |
|
1381 QPainter rp(&rectImage); |
|
1382 rp.setRenderHint(QPainter::Antialiasing); |
|
1383 rp.setPen(Qt::black); |
|
1384 rp.setBrush(Qt::red); |
|
1385 rp.drawRoundedRect(QRectF(.5, .5, 80, 80), radius, radius); |
|
1386 |
|
1387 //setup surface |
|
1388 QImage surface(400, 400, QImage::Format_RGB16); |
|
1389 surface.fill(QColor(255,255,255).rgb()); |
|
1390 QPainter p(&surface); |
|
1391 |
|
1392 QBENCHMARK { |
|
1393 p.setWorldTransform(QTransform(0.956957, 0, 0.000704124, 0, 1, 0, 16.141, 0, 0.735953)); |
|
1394 p.drawImage(100,100, rectImage); |
|
1395 } |
|
1396 } |
|
1397 |
|
1398 //code from QmlGraphicsRectangle for drawing rounded rects |
|
1399 void tst_QPainter::drawBorderPixmapRoundedRect() |
|
1400 { |
|
1401 //setup image |
|
1402 const int pw = 1; |
|
1403 const int radius = 10; |
|
1404 QImage rectImage(radius*2 + 3 + pw*2, radius*2 + 3 + pw*2, QImage::Format_ARGB32_Premultiplied); |
|
1405 rectImage.fill(0); |
|
1406 QPainter rp(&rectImage); |
|
1407 rp.setRenderHint(QPainter::Antialiasing); |
|
1408 rp.setPen(Qt::black); |
|
1409 rp.setBrush(Qt::red); |
|
1410 if (pw%2) |
|
1411 rp.drawRoundedRect(QRectF(qreal(pw)/2+1, qreal(pw)/2+1, rectImage.width()-(pw+1), rectImage.height()-(pw+1)), radius, radius); |
|
1412 else |
|
1413 rp.drawRoundedRect(QRectF(qreal(pw)/2, qreal(pw)/2, rectImage.width()-pw, rectImage.height()-pw), radius, radius); |
|
1414 QPixmap rectPixmap = QPixmap::fromImage(rectImage); |
|
1415 |
|
1416 //setup surface |
|
1417 QImage surface(100, 100, QImage::Format_RGB16); |
|
1418 surface.fill(QColor(255,255,255).rgb()); |
|
1419 QPainter p(&surface); |
|
1420 |
|
1421 QBENCHMARK { |
|
1422 const int pw = 2; |
|
1423 int width = 80; |
|
1424 int height = 80; |
|
1425 |
|
1426 int xOffset = (rectPixmap.width()-1)/2; |
|
1427 int yOffset = (rectPixmap.height()-1)/2; |
|
1428 Q_ASSERT(rectPixmap.width() == 2*xOffset + 1); |
|
1429 Q_ASSERT(rectPixmap.height() == 2*yOffset + 1); |
|
1430 |
|
1431 QMargins margins(xOffset, yOffset, xOffset, yOffset); |
|
1432 QTileRules rules(Qt::StretchTile, Qt::StretchTile); |
|
1433 //NOTE: even though our item may have qreal-based width and height, qDrawBorderPixmap only supports QRects |
|
1434 qDrawBorderPixmap(&p, QRect(-pw/2, -pw/2, width+pw, height+pw), margins, rectPixmap, rectPixmap.rect(), margins, rules); |
|
1435 } |
|
1436 } |
|
1437 |
|
1438 void tst_QPainter::drawScaledBorderPixmapRoundedRect_data() |
|
1439 { |
|
1440 QTest::addColumn<float>("scale"); |
|
1441 QTest::addColumn<int>("imageType"); |
|
1442 |
|
1443 for (float i = 0; i < 3; i += .1) |
|
1444 QTest::newRow(QString(QLatin1String("scale=%1; imagetype=ARGB32_Pre")).arg(i).toLatin1()) << i << (int)QImage::Format_ARGB32_Premultiplied; |
|
1445 //for (float i = 0; i < 3; i += .1) |
|
1446 // QTest::newRow(QString(QLatin1String("scale=%1; imagetype=ARGB8565_Pre")).arg(i).toLatin1()) << i << (int)QImage::Format_ARGB8565_Premultiplied; |
|
1447 } |
|
1448 |
|
1449 //code from QmlGraphicsRectangle for drawing rounded rects |
|
1450 void tst_QPainter::drawScaledBorderPixmapRoundedRect() |
|
1451 { |
|
1452 QFETCH(float, scale); |
|
1453 QFETCH(int, imageType); |
|
1454 |
|
1455 //setup image |
|
1456 const int pw = 1; |
|
1457 const int radius = 10; |
|
1458 QImage rectImage(radius*2 + 3 + pw*2, radius*2 + 3 + pw*2, (QImage::Format)imageType); |
|
1459 rectImage.fill(0); |
|
1460 QPainter rp(&rectImage); |
|
1461 rp.setRenderHint(QPainter::Antialiasing); |
|
1462 rp.setPen(Qt::black); |
|
1463 rp.setBrush(Qt::red); |
|
1464 if (pw%2) |
|
1465 rp.drawRoundedRect(QRectF(qreal(pw)/2+1, qreal(pw)/2+1, rectImage.width()-(pw+1), rectImage.height()-(pw+1)), radius, radius); |
|
1466 else |
|
1467 rp.drawRoundedRect(QRectF(qreal(pw)/2, qreal(pw)/2, rectImage.width()-pw, rectImage.height()-pw), radius, radius); |
|
1468 |
|
1469 QPixmap rectPixmap = QPixmap::fromImage(rectImage); |
|
1470 |
|
1471 //setup surface |
|
1472 QImage surface(400, 400, QImage::Format_RGB16); |
|
1473 surface.fill(QColor(255,255,255).rgb()); |
|
1474 QPainter p(&surface); |
|
1475 p.scale(scale, scale); |
|
1476 |
|
1477 QBENCHMARK { |
|
1478 const int pw = 2; |
|
1479 int width = 80; |
|
1480 int height = 80; |
|
1481 |
|
1482 int xOffset = (rectPixmap.width()-1)/2; |
|
1483 int yOffset = (rectPixmap.height()-1)/2; |
|
1484 Q_ASSERT(rectPixmap.width() == 2*xOffset + 1); |
|
1485 Q_ASSERT(rectPixmap.height() == 2*yOffset + 1); |
|
1486 |
|
1487 QMargins margins(xOffset, yOffset, xOffset, yOffset); |
|
1488 QTileRules rules(Qt::StretchTile, Qt::StretchTile); |
|
1489 qDrawBorderPixmap(&p, QRect(-pw/2, -pw/2, width+pw, height+pw), margins, rectPixmap, rectPixmap.rect(), margins, rules); |
|
1490 } |
|
1491 } |
|
1492 |
|
1493 void tst_QPainter::drawTransformedBorderPixmapRoundedRect_data() |
|
1494 { |
|
1495 QTest::addColumn<QTransform>("transform"); |
|
1496 QTest::addColumn<int>("imageType"); |
|
1497 |
|
1498 for (float angle = 0; angle < 360; angle += 10) |
|
1499 QTest::newRow(QString(QLatin1String("angle=%1; imagetype=ARGB32_Pre")).arg(angle).toLatin1()) << transformForAngle(angle) << (int)QImage::Format_ARGB32_Premultiplied; |
|
1500 //for (float angle = 0; angle < 360; angle += 10) |
|
1501 // QTest::newRow(QString(QLatin1String("angle=%1; imagetype=ARGB8565_Pre")).arg(angle).toLatin1()) << transformForAngle(angle) << (int)QImage::Format_ARGB8565_Premultiplied; |
|
1502 |
|
1503 } |
|
1504 |
|
1505 //code from QmlGraphicsRectangle for drawing rounded rects |
|
1506 void tst_QPainter::drawTransformedBorderPixmapRoundedRect() |
|
1507 { |
|
1508 QFETCH(QTransform, transform); |
|
1509 QFETCH(int, imageType); |
|
1510 |
|
1511 //setup image |
|
1512 const int pw = 1; |
|
1513 const int radius = 10; |
|
1514 QImage rectImage(radius*2 + 3 + pw*2, radius*2 + 3 + pw*2, (QImage::Format)imageType); |
|
1515 rectImage.fill(0); |
|
1516 QPainter rp(&rectImage); |
|
1517 rp.setRenderHint(QPainter::Antialiasing); |
|
1518 rp.setPen(Qt::black); |
|
1519 rp.setBrush(Qt::red); |
|
1520 if (pw%2) |
|
1521 rp.drawRoundedRect(QRectF(qreal(pw)/2+1, qreal(pw)/2+1, rectImage.width()-(pw+1), rectImage.height()-(pw+1)), radius, radius); |
|
1522 else |
|
1523 rp.drawRoundedRect(QRectF(qreal(pw)/2, qreal(pw)/2, rectImage.width()-pw, rectImage.height()-pw), radius, radius); |
|
1524 |
|
1525 QPixmap rectPixmap = QPixmap::fromImage(rectImage); |
|
1526 |
|
1527 //setup surface |
|
1528 QImage surface(400, 400, QImage::Format_RGB16); |
|
1529 surface.fill(QColor(255,255,255).rgb()); |
|
1530 QPainter p(&surface); |
|
1531 |
|
1532 QBENCHMARK { |
|
1533 p.setWorldTransform(transform); |
|
1534 const int pw = 2; |
|
1535 int width = 80; |
|
1536 int height = 80; |
|
1537 |
|
1538 int xOffset = (rectPixmap.width()-1)/2; |
|
1539 int yOffset = (rectPixmap.height()-1)/2; |
|
1540 Q_ASSERT(rectPixmap.width() == 2*xOffset + 1); |
|
1541 Q_ASSERT(rectPixmap.height() == 2*yOffset + 1); |
|
1542 |
|
1543 QMargins margins(xOffset, yOffset, xOffset, yOffset); |
|
1544 QTileRules rules(Qt::StretchTile, Qt::StretchTile); |
|
1545 qDrawBorderPixmap(&p, QRect(-pw/2, -pw/2, width+pw, height+pw), margins, rectPixmap, rectPixmap.rect(), margins, rules); |
|
1546 } |
|
1547 } |
|
1548 |
|
1549 void tst_QPainter::drawTransformedTransparentImage_data() |
|
1550 { |
|
1551 QTest::addColumn<int>("imageType"); |
|
1552 |
|
1553 QTest::newRow("imagetype=ARGB32_Pre") << (int)QImage::Format_ARGB32_Premultiplied; |
|
1554 QTest::newRow("imagetype=ARGB8565_Pre") << (int)QImage::Format_ARGB8565_Premultiplied; |
|
1555 } |
|
1556 |
|
1557 void tst_QPainter::drawTransformedTransparentImage() |
|
1558 { |
|
1559 QFETCH(int, imageType); |
|
1560 |
|
1561 //setup image |
|
1562 QImage transImage(200, 200, (QImage::Format)imageType); |
|
1563 transImage.fill(0); |
|
1564 |
|
1565 //setup surface |
|
1566 QImage surface(200, 200, QImage::Format_RGB16); |
|
1567 surface.fill(QColor(255,255,255).rgb()); |
|
1568 QPainter p(&surface); |
|
1569 |
|
1570 QBENCHMARK { |
|
1571 p.setWorldTransform(QTransform(0.956957, 0, 0.000704124, 0, 1, 0, 16.141, 0, 0.735953)); |
|
1572 p.drawImage(0,0, transImage); |
|
1573 } |
|
1574 } |
|
1575 |
|
1576 void tst_QPainter::drawTransformedSemiTransparentImage_data() |
|
1577 { |
|
1578 QTest::addColumn<int>("imageType"); |
|
1579 |
|
1580 QTest::newRow("imagetype=ARGB32_Pre") << (int)QImage::Format_ARGB32_Premultiplied; |
|
1581 QTest::newRow("imagetype=ARGB8565_Pre") << (int)QImage::Format_ARGB8565_Premultiplied; |
|
1582 } |
|
1583 |
|
1584 void tst_QPainter::drawTransformedSemiTransparentImage() |
|
1585 { |
|
1586 QFETCH(int, imageType); |
|
1587 |
|
1588 //setup image |
|
1589 QImage transImage(200, 200, (QImage::Format)imageType); |
|
1590 transImage.fill(QColor(0,0,0, 128).rgba()); |
|
1591 |
|
1592 //setup surface |
|
1593 QImage surface(200, 200, QImage::Format_RGB16); |
|
1594 surface.fill(QColor(255,255,255).rgb()); |
|
1595 QPainter p(&surface); |
|
1596 |
|
1597 QBENCHMARK { |
|
1598 p.setWorldTransform(QTransform(0.956957, 0, 0.000704124, 0, 1, 0, 16.141, 0, 0.735953)); |
|
1599 p.drawImage(0,0, transImage); |
|
1600 } |
|
1601 } |
|
1602 |
|
1603 void tst_QPainter::drawTransformedFilledImage_data() |
|
1604 { |
|
1605 QTest::addColumn<int>("imageType"); |
|
1606 |
|
1607 QTest::newRow("imagetype=ARGB32_Pre") << (int)QImage::Format_ARGB32_Premultiplied; |
|
1608 QTest::newRow("imagetype=ARGB8565_Pre") << (int)QImage::Format_ARGB8565_Premultiplied; |
|
1609 } |
|
1610 |
|
1611 void tst_QPainter::drawTransformedFilledImage() |
|
1612 { |
|
1613 QFETCH(int, imageType); |
|
1614 |
|
1615 //setup image |
|
1616 QImage filledImage(200, 200, (QImage::Format)imageType); |
|
1617 filledImage.fill(QColor(0,0,0).rgb()); |
|
1618 |
|
1619 //setup surface |
|
1620 QImage surface(200, 200, QImage::Format_RGB16); |
|
1621 surface.fill(QColor(255,255,255).rgb()); |
|
1622 QPainter p(&surface); |
|
1623 |
|
1624 QBENCHMARK { |
|
1625 p.setWorldTransform(QTransform(0.956957, 0, 0.000704124, 0, 1, 0, 16.141, 0, 0.735953)); |
|
1626 p.drawImage(0,0, filledImage); |
|
1627 } |
|
1628 } |
1126 |
1629 |
1127 |
1630 |
1128 QTEST_MAIN(tst_QPainter) |
1631 QTEST_MAIN(tst_QPainter) |
1129 |
1632 |
1130 #include "tst_qpainter.moc" |
1633 #include "tst_qpainter.moc" |