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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
|
/***************************************************************************
kimearea.cpp - description
-------------------
begin : Thu Jun 14 2001
copyright : (C) 2001 by Jan Schaefer
email : janschaefer@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "kimearea.h"
#include <QBitmap>
#include <QBrush>
#include <QColor>
#include <QImage>
#include <QPainter>
#include <QPalette>
#include <QPen>
#include <QPixmap>
#include <QPolygon>
#include "kimagemapeditor_debug.h"
#include "kimecommon.h"
// The size of Selection Points
SelectionPoint::SelectionPoint(QPoint p, QCursor c)
{
point = p;
state = Normal;
_cursor = c;
}
SelectionPoint::~SelectionPoint() {
}
void SelectionPoint::setState(SelectionPoint::State s) {
state = s;
}
SelectionPoint::State SelectionPoint::getState() const {
return state;
}
void SelectionPoint::setPoint(QPoint p) {
point = p;
}
void SelectionPoint::translate(int dx, int dy) {
point += QPoint(dx,dy);
}
QPoint SelectionPoint::getPoint() const {
return point;
}
QRect SelectionPoint::getRect() const {
QRect r(0,0,SELSIZE,SELSIZE);
r.moveCenter(point);
return r;
}
QCursor SelectionPoint::cursor() {
return _cursor;
}
void SelectionPoint::setCursor(QCursor c) {
_cursor = c;
}
void SelectionPoint::draw(QPainter* p, double scalex) {
QColor brushColor;
switch (state) {
case Normal:
brushColor = Qt::white;
break;
case HighLighted:
brushColor = Qt::green;
break;
case AboutToRemove:
brushColor = Qt::red;
break;
case Inactive:
brushColor = Qt::gray;
break;
}
QPoint scaledCenter((int)(point.x()*scalex),
(int)(point.y()*scalex));
if (state == HighLighted || state == AboutToRemove) {
QRect r2(0,0,SELSIZE+4,SELSIZE+4);
r2.moveCenter(scaledCenter);
QColor color(brushColor);
color.setAlpha(100);
p->setPen(QPen(color,4,Qt::SolidLine));
p->setBrush(Qt::NoBrush);
p->drawRect(r2);
}
// brushColor.setAlpha(230);
brushColor.setAlpha(200);
p->setBrush(QBrush(brushColor,Qt::SolidPattern));
QColor penColor = Qt::black;
penColor.setAlpha(120);
QPen pen(penColor, 2, Qt::SolidLine);
QRect r(0,0,SELSIZE,SELSIZE);
r.moveCenter( scaledCenter );
p->setPen(pen);
p->drawRect(r);
}
bool Area::highlightArea;
bool Area::showAlt;
Area::Area()
{
_finished=false;
_isSelected=false;
_name=i18n("noname");
_listViewItem = nullptr;
currentHighlighted=-1;
_type=Area::None;
}
Area* Area::clone() const
{
Area* areaClone = new Area();
areaClone->setArea( *this );
return areaClone;
}
QPolygon Area::coords() const {
return _coords;
}
QString Area::getHTMLAttributes() const
{
QString retStr="";
AttributeIterator it = attributeIterator();
while (it.hasNext())
{
it.next();
retStr+=it.key()+"=\""+it.value()+"\" ";
}
return retStr;
}
void Area::resetSelectionPointState() {
setSelectionPointStates(SelectionPoint::Normal);
}
void Area::setSelectionPointStates(SelectionPoint::State st) {
for (int i=0;i<_selectionPoints.size();i++) {
_selectionPoints.at(i)->setState(st);
}
}
void Area::deleteSelectionPoints() {
for (int i=0;i<_selectionPoints.size();i++) {
delete _selectionPoints.at(i);
}
_selectionPoints.clear();
}
Area::~Area() {
deleteSelectionPoints();
}
bool Area::contains(const QPoint &) const {
return false;
}
QString Area::getHTMLCode() const {
return "";
}
QString Area::attribute(const QString & name) const
{
return _attributes[name.toLower()];
}
void Area::setAttribute(const QString & name, const QString & value)
{
_attributes.insert(name.toLower(), value);
if (value.isEmpty())
_attributes.remove(name.toLower());
}
AttributeIterator Area::attributeIterator() const
{
return AttributeIterator(_attributes);
}
bool Area::setCoords(const QString &) {
return true;
}
void Area::moveSelectionPoint(SelectionPoint*, const QPoint &)
{}
// Default implementation; is specified by subclasses
QString Area::coordsToString() const
{
return "";
}
Area::ShapeType Area::type() const {
return _type;
}
void Area::setArea(const Area & copy)
{
deleteSelectionPoints();
_coords.clear();
_coords += copy.coords();
currentHighlighted=-1;
SelectionPointList points = copy.selectionPoints();
for (int i=0; i<points.size(); i++) {
SelectionPoint* np =
new SelectionPoint(points.at(i)->getPoint(),points.at(i)->cursor());
_selectionPoints.append(np);
}
_finished=copy.finished();
_isSelected=copy.isSelected();
_rect = copy.rect();
AttributeIterator it = copy.attributeIterator();
while (it.hasNext()) {
it.next();
setAttribute(it.key(),it.value());
}
setMoving(copy.isMoving());
}
void Area::setFinished(bool b, bool ) {
_finished=b;
}
void Area::setListViewItem(QTreeWidgetItem* item) {
_listViewItem=item;
}
void Area::deleteListViewItem()
{
delete _listViewItem;
_listViewItem = nullptr;
}
void Area::setRect(const QRect & r)
{
_rect=r;
updateSelectionPoints();
}
QRect Area::rect() const {
return _rect;
}
void Area::setMoving(bool b) {
_isMoving=b;
}
void Area::moveBy(int dx, int dy) {
_rect.translate(dx,dy);
_coords.translate(dx,dy);
for (int i=0;i < _selectionPoints.size(); i++) {
_selectionPoints.at(i)->translate(dx,dy);
}
}
void Area::moveTo(int x, int y) {
int dx = x-rect().left();
int dy = y-rect().top();
moveBy(dx,dy);
}
int Area::countSelectionPoints() const
{
return selectionPoints().size();
}
int Area::addCoord(const QPoint & p)
{
_coords.resize(_coords.size()+1);
_coords.setPoint(_coords.size()-1,p);
_selectionPoints.append(new SelectionPoint(p,QCursor(Qt::PointingHandCursor)));
setRect(_coords.boundingRect());
return _coords.size()-1;
}
void Area::insertCoord(int pos, const QPoint & p)
{
_coords.resize(_coords.size()+1);
for (int i=_coords.size()-1;i>pos;i--) {
_coords.setPoint(i,_coords.point(i-1));
}
_coords.setPoint(pos, p);
_selectionPoints.insert(pos,new SelectionPoint(p,QCursor(Qt::PointingHandCursor)));
setRect(_coords.boundingRect());
}
void Area::removeCoord(int pos) {
int count =_coords.size();
if (count<4){
qCDebug(KIMAGEMAPEDITOR_LOG) << "Danger : trying to remove coordinate from Area with less than 4 coordinates !";
return;
}
for (int i=pos;i<(count-1);i++)
_coords.setPoint(i, _coords.point(i+1));
_coords.resize(count-1);
delete _selectionPoints.takeAt(pos);
setRect(_coords.boundingRect());
}
bool Area::removeSelectionPoint(SelectionPoint * p)
{
if (_selectionPoints.contains(p))
{
removeCoord(_selectionPoints.indexOf(p));
return true;
}
return false;
}
void Area::moveCoord(int pos, const QPoint & p) {
_coords.setPoint(pos,p);
_selectionPoints.at(pos)->setPoint(p);
setRect(_coords.boundingRect());
}
void Area::setSelected(bool b)
{
_isSelected=b;
if (_listViewItem) {
_listViewItem->setSelected(b);
}
}
void Area::highlightSelectionPoint(int number){
currentHighlighted=number;
}
QRect Area::selectionRect() const {
QRect r = rect();
r.translate(-SELSIZE*2,-SELSIZE*2);
r.setSize(r.size()+QSize(SELSIZE*4,SELSIZE*4));
return r;
}
void Area::setPenAndBrush(QPainter* p) {
QBrush brush(Qt::NoBrush);
if (highlightArea) {
QColor back = Qt::white;
back.setAlpha(80);
brush = QBrush(back,Qt::SolidPattern);
}
p->setBrush(brush);
QColor front = Qt::white;
front.setAlpha(200);
p->setPen(QPen(front,1));
}
void Area::drawAlt(QPainter* p)
{
double x,y;
const double scalex = p->transform().m11();
// double scaley = p.matrix().m12();
const QTransform oldTransform = p->transform();
p->setTransform(QTransform(1,oldTransform.m12(), oldTransform.m21(), 1, oldTransform.dx(), oldTransform.dy() ));
x = (rect().x()+rect().width()/2)*scalex;
y = (rect().y()+rect().height()/2)*scalex;
const QFontMetrics metrics = p->fontMetrics();
const int w = metrics.boundingRect(attribute("alt")).width();
x -= w/2;
y += metrics.height()/4;
if (highlightArea) {
p->setPen(Qt::black);
} else {
p->setPen(QPen(QColor("white"),1));
}
p->drawText(myround(x),myround(y),attribute("alt"));
p->setTransform(oldTransform);
}
void Area::draw(QPainter * p)
{
// Only draw the selection points at base class
// the rest is done in the derived classes
if (_isSelected) {
// We do not want to have the selection points
// scaled, so calculate the unscaled version
const double scalex = p->transform().m11();
const QTransform oldTransform = p->transform();
p->setTransform(QTransform(1,oldTransform.m12(),
oldTransform.m21(), 1,
oldTransform.dx(),
oldTransform.dy() ));
for (int i=0; i<_selectionPoints.size(); i++) {
_selectionPoints.at(i)->draw(p,scalex);
}
p->setTransform(oldTransform);
}
if (showAlt) {
drawAlt(p);
}
}
SelectionPoint* Area::onSelectionPoint(const QPoint & p, double zoom) const
{
for (int i=0; i<_selectionPoints.size(); i++) {
SelectionPoint* sp = _selectionPoints.at(i);
QRect r = sp->getRect();
r.moveCenter(sp->getPoint()*zoom);
if (r.contains(p))
{
return sp;
}
}
return nullptr;
}
/**
* returns only the part of the image which is
* covered by the area
*/
QPixmap Area::cutOut(const QImage & image)
{
if ( 0>=rect().width() ||
0>=rect().height() ||
!rect().intersects(image.rect()) )
{
QPixmap dummyPix(10,10);
dummyPix.fill();
return dummyPix;
}
// Get the mask from the subclasses
QBitmap mask=getMask();
// The rectangle which is part of the image
QRect partOfImage=rect();
QRect partOfMask(0,0,mask.width(),mask.height());
// If the area is outside of the image make the
// preview smaller
if ( (rect().x()+rect().width()) > image.width() ) {
partOfImage.setWidth( image.width()-rect().x() );
partOfMask.setWidth( image.width()-rect().x() );
}
if ( (rect().x() < 0) ) {
partOfImage.setX(0);
partOfMask.setX(myabs(rect().x()));
}
if ( (rect().y()+rect().height()) > image.height() ) {
partOfImage.setHeight( image.height()-rect().y() );
partOfMask.setHeight ( image.height()-rect().y() );
}
if ( (rect().y() < 0) ) {
partOfImage.setY(0);
partOfMask.setY(myabs(rect().y()));
}
QImage tempImage=mask.toImage().copy(partOfMask);
mask = QPixmap::fromImage(tempImage);
// partOfImage = partOfImage.normalize();
QImage cut=image.copy(partOfImage);
QPixmap pix;
// partOfMask = partOfMask.normalize();
if (!partOfMask.isValid())
qCDebug(KIMAGEMAPEDITOR_LOG) << "PartofMask not valid : " << partOfMask.x() << "," << partOfMask.y() << ","
<< partOfMask.width() << "," << partOfMask.height() << ",";
/*
QBitmap mask2(partOfMask.width(), partOfMask.height());
QPainter p4(&mask2);
p4.drawPixmap( QPoint(0,0) ,mask,partOfMask);
p4.flush();
p4.end();
*/
pix = QPixmap::fromImage(cut);
// setHighlightedPixmap(cut, mask);
QPixmap retPix(pix.width(),pix.height());
QPainter p3(&retPix);
// if transparent image fill the background
// with gimp-like rectangles
if (!pix.mask().isNull()) {
QPixmap backPix(32,32);
// Gimp like transparent rectangle
QPainter p2(&backPix);
p2.fillRect(0,0,32,32,QColor(156,149,156));
p2.fillRect(0,16,16,16,QColor(98,105,98));
p2.fillRect(16,0,16,16,QColor(98,105,98));
p3.setPen(QPen());
p3.fillRect(0,0,pix.width(),pix.height(),QBrush(QColor("black"),backPix));
}
p3.drawPixmap(QPoint(0,0),pix);
p3.end();
retPix.setMask(mask);
return retPix;
}
QBitmap Area::getMask() const
{
QBitmap b;
return b;
}
/********************************************************************
* RECTANGLE
*******************************************************************/
RectArea::RectArea()
: Area()
{
_type=Area::Rectangle;
QPoint p(0,0);
_selectionPoints.append(new SelectionPoint(p,Qt::SizeFDiagCursor));
_selectionPoints.append(new SelectionPoint(p,Qt::SizeBDiagCursor));
_selectionPoints.append(new SelectionPoint(p,Qt::SizeBDiagCursor));
_selectionPoints.append(new SelectionPoint(p,Qt::SizeFDiagCursor));
_selectionPoints.append(new SelectionPoint(p,Qt::SizeVerCursor));
_selectionPoints.append(new SelectionPoint(p,Qt::SizeHorCursor));
_selectionPoints.append(new SelectionPoint(p,Qt::SizeVerCursor));
_selectionPoints.append(new SelectionPoint(p,Qt::SizeHorCursor));
}
RectArea::~RectArea() {
}
Area* RectArea::clone() const
{
Area* areaClone = new RectArea();
areaClone->setArea( *this );
return areaClone;
}
void RectArea::draw(QPainter * p)
{
setPenAndBrush(p);
QRect r(rect());
r.setWidth(r.width()+1);
r.setHeight(r.height()+1);
p->drawRect(r);
Area::draw(p);
}
QBitmap RectArea::getMask() const
{
QBitmap mask(rect().width(),rect().height());
mask.fill(Qt::color0);
QPainter p(&mask);
p.setBackground(QBrush(Qt::color0));
p.setPen(Qt::color1);
p.setBrush(Qt::color1);
mask.fill(Qt::color1);
p.end();
return mask;
}
QString RectArea::coordsToString() const
{
QString retStr=QString("%1,%2,%3,%4")
.arg(rect().left())
.arg(rect().top())
.arg(rect().right())
.arg(rect().bottom());
return retStr;
}
bool RectArea::contains(const QPoint & p) const{
return rect().contains(p);
}
void RectArea::moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p)
{
selectionPoint->setPoint(p);
int i = _selectionPoints.indexOf(selectionPoint);
QRect r2(_rect);
switch (i) {
case 0 :
_rect.setLeft(p.x());
_rect.setTop(p.y());
break;
case 1 :
_rect.setRight(p.x());
_rect.setTop(p.y());
break;
case 2 :
_rect.setLeft(p.x());
_rect.setBottom(p.y());
break;
case 3 :
_rect.setRight(p.x());
_rect.setBottom(p.y());
break;
case 4 : // top line
_rect.setTop(p.y());
break;
case 5 : // right line
_rect.setRight(p.x());
break;
case 6 : // bottom
_rect.setBottom(p.y());
break;
case 7 : // left
_rect.setLeft(p.x());
break;
}
if (! _rect.isValid())
_rect=r2;
updateSelectionPoints();
}
void RectArea::updateSelectionPoints()
{
int d = 2;
QRect r(_rect);
r.adjust(0,0,1,1);
int xmid = r.left()+(r.width()/d);
int ymid = r.top()+(r.height()/d);
_selectionPoints[0]->setPoint(r.topLeft());
_selectionPoints[1]->setPoint(r.topRight());
_selectionPoints[2]->setPoint(r.bottomLeft());
_selectionPoints[3]->setPoint(r.bottomRight());
_selectionPoints[4]->setPoint(QPoint(xmid,r.top()));
_selectionPoints[5]->setPoint(QPoint(r.right(),ymid));
_selectionPoints[6]->setPoint(QPoint(xmid,r.bottom()));
_selectionPoints[7]->setPoint(QPoint(r.left(),ymid));
}
bool RectArea::setCoords(const QString & s)
{
_finished=true;
const QStringList list = s.split(',');
QRect r;
bool ok=true;
QStringList::ConstIterator it = list.begin();
r.setLeft((*it).toInt(&ok,10));it++;
r.setTop((*it).toInt(&ok,10));it++;
r.setRight((*it).toInt(&ok,10));it++;
r.setBottom((*it).toInt(&ok,10));
if (ok) {
setRect(r);
return true;
} else {
return false;
}
}
QString RectArea::getHTMLCode() const {
QString retStr;
retStr+="<area ";
retStr+="shape=\"rect\" ";
retStr+=getHTMLAttributes();
retStr+="coords=\""+coordsToString()+"\" ";
retStr+="/>";
return retStr;
}
/********************************************************************
* CIRCLE
*******************************************************************/
CircleArea::CircleArea()
: Area()
{
_type = Area::Circle;
QPoint p(0,0);
_selectionPoints.append(new SelectionPoint(p,Qt::SizeFDiagCursor));
_selectionPoints.append(new SelectionPoint(p,Qt::SizeBDiagCursor));
_selectionPoints.append(new SelectionPoint(p,Qt::SizeBDiagCursor));
_selectionPoints.append(new SelectionPoint(p,Qt::SizeFDiagCursor));
}
CircleArea::~CircleArea() {
}
Area* CircleArea::clone() const
{
Area* areaClone = new CircleArea();
areaClone->setArea( *this );
return areaClone;
}
void CircleArea::draw(QPainter * p)
{
setPenAndBrush(p);
QRect r(_rect);
r.setWidth(r.width()+1);
r.setHeight(r.height()+1);
p->drawEllipse(r);
Area::draw(p);
}
QBitmap CircleArea::getMask() const
{
QBitmap mask(_rect.width(),_rect.height());
mask.fill(Qt::color0);
QPainter p(&mask);
p.setBackground(QBrush(Qt::color0));
p.setPen(Qt::color1);
p.setBrush(Qt::color1);
p.drawPie(QRect(0,0,_rect.width(),_rect.height()),0,5760);
p.end();
return mask;
}
QString CircleArea::coordsToString() const
{
QString retStr=QString("%1,%2,%3")
.arg(_rect.center().x())
.arg(_rect.center().y())
.arg(_rect.width()/2);
return retStr;
}
bool CircleArea::contains(const QPoint & p) const
{
QRegion r(_rect,QRegion::Ellipse);
return r.contains(p);
}
void CircleArea::moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p)
{
selectionPoint->setPoint(p);
int i = _selectionPoints.indexOf(selectionPoint);
// The code below really sucks, but I have no better idea.
// it only makes sure that the circle is perfectly round
QPoint newPoint;
int diff=myabs(p.x()-_rect.center().x());
if (myabs(p.y()-_rect.center().y())>diff)
diff=myabs(p.y()-_rect.center().y());
newPoint.setX( p.x()-_rect.center().x()<0
? _rect.center().x()-diff
: _rect.center().x()+diff);
newPoint.setY( p.y()-_rect.center().y()<0
? _rect.center().y()-diff
: _rect.center().y()+diff);
switch (i) {
case 0 : if (newPoint.x() < _rect.center().x() &&
newPoint.y() < _rect.center().y())
{
_rect.setLeft(newPoint.x());
_rect.setTop(newPoint.y());
}
break;
case 1 : if (newPoint.x() > _rect.center().x() &&
newPoint.y() < _rect.center().y())
{
_rect.setRight(newPoint.x());
_rect.setTop(newPoint.y());
}
break;
case 2 : if (newPoint.x() < _rect.center().x() &&
newPoint.y() > _rect.center().y())
{
_rect.setLeft(newPoint.x());
_rect.setBottom(newPoint.y());
}
break;
case 3 : if (newPoint.x() > _rect.center().x() &&
newPoint.y() > _rect.center().y())
{
_rect.setRight(newPoint.x());
_rect.setBottom(newPoint.y());
}
break;
}
updateSelectionPoints();
}
void CircleArea::setRect(const QRect & r)
{
QRect r2 = r;
if ( r2.height() != r2.width() )
r2.setHeight( r2.width() );
Area::setRect(r2);
}
void CircleArea::updateSelectionPoints()
{
_selectionPoints[0]->setPoint(_rect.topLeft());
_selectionPoints[1]->setPoint(_rect.topRight());
_selectionPoints[2]->setPoint(_rect.bottomLeft());
_selectionPoints[3]->setPoint(_rect.bottomRight());
}
bool CircleArea::setCoords(const QString & s)
{
_finished=true;
const QStringList list = s.split(',');
bool ok=true;
QStringList::ConstIterator it = list.begin();
int x=(*it).toInt(&ok,10);it++;
int y=(*it).toInt(&ok,10);it++;
int rad=(*it).toInt(&ok,10);
if (!ok) return false;
QRect r;
r.setWidth(rad*2);
r.setHeight(rad*2);
r.moveCenter(QPoint(x,y));
setRect(r);
return true;
}
QString CircleArea::getHTMLCode() const {
QString retStr;
retStr+="<area ";
retStr+="shape=\"circle\" ";
retStr+=getHTMLAttributes();
retStr+="coords=\""+coordsToString()+"\" ";
retStr+="/>";
return retStr;
}
/********************************************************************
* POLYGON
*******************************************************************/
PolyArea::PolyArea()
: Area()
{
_type = Area::Polygon;
}
PolyArea::~PolyArea() {
}
Area* PolyArea::clone() const
{
Area* areaClone = new PolyArea();
areaClone->setArea( *this );
return areaClone;
}
void PolyArea::draw(QPainter * p)
{
setPenAndBrush(p);
if (_finished)
p->drawPolygon( _coords.constData(),_coords.count());
else {
p->drawPolyline(_coords.constData(),_coords.count());
}
Area::draw(p);
}
QBitmap PolyArea::getMask() const
{
QBitmap mask(_rect.width(),_rect.height());
mask.fill(Qt::color0);
QPainter p(&mask);
p.setBackground(QBrush(Qt::color0));
p.setPen(Qt::color1);
p.setBrush(Qt::color1);
p.setClipping(true);
QRegion r(_coords);
r.translate(-_rect.left(),-_rect.top());
p.setClipRegion(r);
p.fillRect(QRect(0,0,_rect.width(),_rect.height()),Qt::color1);
p.end();
return mask;
}
QString PolyArea::coordsToString() const
{
QString retStr;
for (int i=0;i<_coords.count();i++) {
retStr.append(QString("%1,%2,")
.arg(_coords.point(i).x())
.arg(_coords.point(i).y()));
}
retStr.remove(retStr.length()-1,1);
return retStr;
}
int PolyArea::distance(const QPoint &p1, const QPoint &p2)
{
QPoint temp = p1-p2;
return temp.manhattanLength();
}
bool PolyArea::isBetween(const QPoint &p, const QPoint &p1, const QPoint &p2)
{
int dist = distance(p,p1)+distance(p,p2)-distance(p1,p2);
if (myabs(dist)<1)
return true;
else
return false;
}
void PolyArea::simplifyCoords()
{
if (_coords.size()<4)
return;
QPoint p = _coords.point(0) - _coords.point(1);
int i = 1;
while( (i<_coords.size()) && (_coords.size() > 3) )
{
p = _coords.point(i-1) - _coords.point(i);
if (p.manhattanLength() < 3)
removeCoord(i);
else
i++;
}
p = _coords.point(0) - _coords.point(1);
double angle2;
double angle1;
if (p.y()==0)
angle1 = 1000000000;
else
angle1 = (double) p.x() / (double) p.y();
i=2;
while( (i<_coords.size()) && (_coords.size() > 3) )
{
p = _coords.point(i-1) - _coords.point(i);
if (p.y()==0)
angle2 = 1000000000;
else
angle2 = (double) p.x() / (double) p.y();
if ( angle2==angle1 )
{
qCDebug(KIMAGEMAPEDITOR_LOG) << "removing " << i-1;
removeCoord(i-1);
}
else
{
i++;
qCDebug(KIMAGEMAPEDITOR_LOG) << "skipping " << i-1 << " cause " << angle1 << "!= " << angle2;
angle1 = angle2;
}
}
}
int PolyArea::addCoord(const QPoint & p)
{
if (_coords.size()<3)
{
return Area::addCoord(p);
}
if (_coords.point(_coords.size()-1) == p)
{
qCDebug(KIMAGEMAPEDITOR_LOG) << "equal Point added";
return -1;
}
int n=_coords.size();
// QPoint temp = p-_coords.point(0);
int nearest = 0;
int olddist = distance(p,_coords.point(0));
int mindiff = 999999999;
// find the two points, which are the nearest one to the new point
for (int i=1; i <= n; i++)
{
int dist = distance(p,_coords.point(i%n));
int dist2 = distance(_coords.point(i-1),_coords.point(i%n));
int diff = myabs(dist+olddist-dist2);
if ( diff<mindiff )
{
mindiff = diff;
nearest = i%n;
}
olddist=dist;
}
insertCoord(nearest, p);
return nearest;
}
bool PolyArea::contains(const QPoint & p) const
{
// A line can't contain a point
if (_coords.count() >2 ) {
QRegion r(_coords);
return r.contains(p);
}
else
return false;
}
void PolyArea::moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p)
{
selectionPoint->setPoint(p);
int i = _selectionPoints.indexOf(selectionPoint);
_coords.setPoint(i,p);
_rect=_coords.boundingRect();
}
void PolyArea::updateSelectionPoints()
{
for (int i = 0; i < _selectionPoints.size(); ++i) {
_selectionPoints.at(i)->setPoint(_coords.point(i));
}
}
bool PolyArea::setCoords(const QString & s)
{
_finished=true;
const QStringList list = s.split(',');
_coords.clear();
_selectionPoints.clear();
for (QStringList::ConstIterator it = list.begin(); it !=list.end(); ++it)
{
bool ok=true;
int newXCoord=(*it).toInt(&ok,10);
if (!ok) return false;
it++;
if (it==list.end()) break;
int newYCoord=(*it).toInt(&ok,10);
if (!ok) return false;
insertCoord(_coords.size(), QPoint(newXCoord,newYCoord));
}
return true;
}
QString PolyArea::getHTMLCode() const {
QString retStr;
retStr+="<area ";
retStr+="shape=\"poly\" ";
retStr+=getHTMLAttributes();
retStr+="coords=\""+coordsToString()+"\" ";
retStr+="/>";
return retStr;
}
void PolyArea::setFinished(bool b, bool removeLast = true)
{
// The last Point is the same as the first
// so delete it
if (b && removeLast) {
_coords.resize(_coords.size()-1);
_selectionPoints.removeLast();
}
_finished = b;
}
QRect PolyArea::selectionRect() const
{
QRect r = _rect;
r.translate(-10,-10);
r.setSize(r.size()+QSize(21,21));
return r;
}
/********************************************************************
* DEFAULT
*******************************************************************/
DefaultArea::DefaultArea()
: Area()
{
_type=Area::Default;
}
DefaultArea::~DefaultArea() {
}
Area* DefaultArea::clone() const
{
Area* areaClone = new DefaultArea();
areaClone->setArea( *this );
return areaClone;
}
void DefaultArea::draw(QPainter *)
{}
QString DefaultArea::getHTMLCode() const {
QString retStr;
retStr+="<area ";
retStr+="shape=\"default\" ";
retStr+=getHTMLAttributes();
retStr+="/>";
return retStr;
}
/********************************************************************
* AreaSelection
*******************************************************************/
AreaSelection::AreaSelection()
: Area()
{
_areas = new AreaList();
_name = "Selection";
invalidate();
}
AreaSelection::~AreaSelection() {
delete _areas;
}
Area* AreaSelection::clone() const
{
AreaSelection* areaClone = new AreaSelection();
// we want a deep copy of the Areas
AreaListIterator it=getAreaListIterator();
while (it.hasNext()) {
areaClone->add( it.next()->clone() );
}
return areaClone;
}
void AreaSelection::add(Area *a)
{
// if a selection of areas was added get the areas of it
AreaSelection *selection = nullptr;
if ( (selection = dynamic_cast <AreaSelection*> ( a ) ) ) {
AreaList list = selection->getAreaList();
Area* area;
foreach(area,list) {
if ( !_areas->contains( area )) {
_areas->append( area ); // Must come before area->setSelected
area->setSelected( true );
}
}
} else {
if ( !_areas->contains( a )) {
_areas->append( a ); // Must come before a->setSelected
a->setSelected( true );
}
}
invalidate();
}
void AreaSelection::setSelectionPointStates(SelectionPoint::State st) {
AreaListIterator it=getAreaListIterator();
while(it.hasNext()) {
it.next()->setSelectionPointStates( st );
}
}
void AreaSelection::updateSelectionPointStates() {
SelectionPoint::State st = SelectionPoint::Normal;
if (_areas->count() > 1)
st = SelectionPoint::Inactive;
setSelectionPointStates(st);
}
void AreaSelection::remove(Area *a)
{
if (!_areas->contains(a))
return;
a->setSelected( false );
_areas->removeAt(_areas->indexOf(a));
invalidate();
}
void AreaSelection::reset()
{
AreaListIterator it=getAreaListIterator();
while (it.hasNext()) {
it.next()->setSelected( false );
}
_areas->clear();
invalidate();
}
bool AreaSelection::contains(const QPoint & p) const
{
AreaListIterator it=getAreaListIterator();
while (it.hasNext()) {
if ( it.next()->contains( p ) ) {
return true;
}
}
return false;
}
SelectionPoint* AreaSelection::onSelectionPoint(const QPoint & p, double zoom) const
{
if (_areas->count() != 1)
return nullptr;
return _areas->first()->onSelectionPoint(p,zoom);
}
void AreaSelection::moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p)
{
// It's only possible to move a SelectionPoint if only one Area is selected
if (_areas->count() != 1)
return;
_areas->first()->moveSelectionPoint(selectionPoint,p);
invalidate();
}
void AreaSelection::moveBy(int dx, int dy)
{
AreaListIterator it=getAreaListIterator();
while (it.hasNext()) {
it.next()->moveBy(dx,dy);
}
Area::moveBy( dx, dy );
invalidate();
}
QString AreaSelection::typeString() const
{
// if there is only one Area selected
// show the name of that Area
if ( _areas->count()==0 )
return "";
else if ( _areas->count()==1 )
return _areas->first()->typeString();
else
return i18n("Number of Areas");
}
Area::ShapeType AreaSelection::type() const
{
// if there is only one Area selected
// take the type of that Area
if ( _areas->count()==0 )
return Area::None;
else if ( _areas->count()==1 )
return _areas->first()->type();
else
return Area::Selection;
}
void AreaSelection::resetSelectionPointState() {
updateSelectionPointStates();
}
void AreaSelection::updateSelectionPoints()
{
AreaListIterator it=getAreaListIterator();
while (it.hasNext()) {
it.next()->updateSelectionPoints();
}
invalidate();
}
QRect AreaSelection::selectionRect() const
{
if (!_selectionCacheValid) {
_selectionCacheValid=true;
QRect r;
AreaListIterator it=getAreaListIterator();
while (it.hasNext()) {
r = r | it.next()->selectionRect();
}
_cachedSelectionRect=r;
}
return _cachedSelectionRect;
}
int AreaSelection::count() const {
return _areas->count();
}
bool AreaSelection::isEmpty() const
{
return _areas->isEmpty();
}
AreaList AreaSelection::getAreaList() const {
AreaList list(*_areas);
return list;
}
AreaListIterator AreaSelection::getAreaListIterator() const {
AreaListIterator it(*_areas);
return it;
}
void AreaSelection::setArea(const Area & copy)
{
Area *area = copy.clone();
AreaSelection *selection = dynamic_cast<AreaSelection*>(area);
if (selection)
setAreaSelection(*selection);
else {
Area::setArea(copy);
invalidate();
}
}
void AreaSelection::setAreaSelection(const AreaSelection & copy)
{
AreaList* areasCopy = copy._areas;
if (_areas->count() != areasCopy->count())
return;
AreaListIterator it(*_areas);
AreaListIterator it2(*areasCopy);
while (it.hasNext()) {
it.next()->setArea(*it2.next());
}
Area::setArea(copy);
invalidate();
}
void AreaSelection::setAreaList( const AreaList & areas )
{
delete _areas;
_areas = new AreaList(areas);
invalidate();
}
void AreaSelection::setRect(const QRect & r)
{
if ( _areas->count()==1 )
{
_areas->first()->setRect(r);
}
invalidate();
_rect=rect();
updateSelectionPoints();
}
QRect AreaSelection::rect() const
{
if (!_rectCacheValid)
{
_rectCacheValid=true;
QRect r;
AreaListIterator it=getAreaListIterator();
while (it.hasNext()) {
r = r | it.next()->rect();
}
_cachedRect=r;
}
return _cachedRect;
}
int AreaSelection::addCoord(const QPoint & p)
{
if ( _areas->count()==1 )
{
return _areas->first()->addCoord(p);
invalidate();
}
return 0;
}
void AreaSelection::insertCoord(int pos, const QPoint & p)
{
if ( _areas->count()==1 )
{
_areas->first()->insertCoord(pos, p);
invalidate();
}
}
void AreaSelection::removeCoord(int pos)
{
if ( _areas->count()==1 )
{
_areas->first()->removeCoord(pos);
invalidate();
}
}
bool AreaSelection::removeSelectionPoint(SelectionPoint* p)
{
bool result=false;
if ( _areas->count()==1 )
{
result = _areas->first()->removeSelectionPoint(p);
invalidate();
}
return result;
}
const SelectionPointList & AreaSelection::selectionPoints() const
{
if ( _areas->count()==1 )
{
return _areas->first()->selectionPoints();
}
return _selectionPoints;
}
void AreaSelection::moveCoord(int pos,const QPoint & p)
{
if ( _areas->count()==1 )
{
_areas->first()->moveCoord(pos,p);
invalidate();
}
}
void AreaSelection::highlightSelectionPoint(int i)
{
if ( _areas->count()==1 )
{
_areas->first()->highlightSelectionPoint(i);
invalidate();
}
}
QPolygon AreaSelection::coords() const
{
if ( _areas->count()==1 )
{
return _areas->first()->coords();
}
return Area::coords();
}
QString AreaSelection::attribute(const QString & name) const
{
if ( _areas->count()==1 )
{
return _areas->first()->attribute(name);
}
return Area::attribute(name);
}
void AreaSelection::setAttribute(const QString & name, const QString & value)
{
AreaListIterator it=getAreaListIterator();
while (it.hasNext()) {
it.next()->setAttribute(name,value);
}
Area::setAttribute(name,value);
}
AttributeIterator AreaSelection::attributeIterator() const
{
if ( _areas->count()==1 )
{
return _areas->first()->attributeIterator();
}
return AttributeIterator(_attributes);
}
void AreaSelection::setMoving(bool b)
{
AreaListIterator it=getAreaListIterator();
while (it.hasNext()) {
it.next()->setMoving(b);
}
Area::setMoving(b);
}
bool AreaSelection::isMoving() const
{
if ( _areas->count()==1 )
{
return _areas->first()->isMoving();
}
return Area::isMoving();
}
/**
* Checks if an area is outside the rectangle parameter
* returns false if an area has no pixel in common with the rectangle parameter
**/
bool AreaSelection::allAreasWithin(const QRect & r) const
{
if ( ! r.contains(rect()) )
{
AreaListIterator it=getAreaListIterator();
while (it.hasNext()) {
if (!it.next()->rect().intersects(r))
return false;
}
}
return true;
}
void AreaSelection::draw(QPainter *)
{}
void AreaSelection::invalidate() {
_selectionCacheValid=false;
_rectCacheValid=false;
updateSelectionPointStates();
}
|