Changeset View
Changeset View
Standalone View
Standalone View
ui/pagepainter.cpp
Show First 20 Lines • Show All 983 Lines • ▼ Show 20 Line(s) | 983 | if ( ( la->lineClosed() || la->transformedLinePoints().count() == 2 ) && | |||
---|---|---|---|---|---|
984 | la->lineInnerColor().isValid() ) | 984 | la->lineInnerColor().isValid() ) | ||
985 | { | 985 | { | ||
986 | fillBrush = QBrush( la->lineInnerColor() ); | 986 | fillBrush = QBrush( la->lineInnerColor() ); | ||
987 | } | 987 | } | ||
988 | } | 988 | } | ||
989 | 989 | | |||
990 | void LineAnnotPainter::draw( QImage &image ) const | 990 | void LineAnnotPainter::draw( QImage &image ) const | ||
991 | { | 991 | { | ||
992 | if ( la->transformedLinePoints().count() == 2 ) | | |||
993 | { | | |||
994 | const Okular::NormalizedPoint delta { | 992 | const Okular::NormalizedPoint delta { | ||
995 | la->transformedLinePoints().last().x - la->transformedLinePoints().first().x, | 993 | la->transformedLinePoints().last().x - la->transformedLinePoints().first().x, | ||
996 | la->transformedLinePoints().first().y - la->transformedLinePoints().last().y | 994 | la->transformedLinePoints().first().y - la->transformedLinePoints().last().y | ||
997 | }; | 995 | }; | ||
996 | const double deaspectedY { delta.y * aspectRatio }; | ||||
998 | const double angle { atan2( delta.y * aspectRatio, delta.x ) }; | 997 | const double angle { atan2( delta.y * aspectRatio, delta.x ) }; | ||
998 | const double mainSegmentLength { sqrt( delta.x * delta.x + deaspectedY * deaspectedY ) }; | ||||
999 | | ||||
1000 | if ( la->showCaption() ) | ||||
1001 | drawCaption( image, mainSegmentLength, angle ); | ||||
1002 | | ||||
1003 | if ( la->transformedLinePoints().count() == 2 ) | ||||
1004 | { | ||||
999 | const double cosA { cos( -angle ) }; | 1005 | const double cosA { cos( -angle ) }; | ||
1000 | const double sinA { sin( -angle ) }; | 1006 | const double sinA { sin( -angle ) }; | ||
1001 | const QTransform tmpMatrix = QTransform { | 1007 | const QTransform tmpMatrix = QTransform { | ||
1002 | cosA, sinA / aspectRatio, | 1008 | cosA, sinA / aspectRatio, | ||
1003 | -sinA, cosA / aspectRatio, | 1009 | -sinA, cosA / aspectRatio, | ||
1004 | la->transformedLinePoints().first().x, | 1010 | la->transformedLinePoints().first().x, | ||
1005 | la->transformedLinePoints().first().y }; | 1011 | la->transformedLinePoints().first().y }; | ||
1006 | const double deaspectedY { delta.y * aspectRatio }; | | |||
1007 | const double mainSegmentLength { sqrt( delta.x * delta.x + deaspectedY * deaspectedY ) }; | | |||
1008 | const double lineendSize { std::min( 6. * la->style().width() / pageSize.width(), mainSegmentLength / 2. ) }; | 1012 | const double lineendSize { std::min( 6. * la->style().width() / pageSize.width(), mainSegmentLength / 2. ) }; | ||
1009 | 1013 | | |||
1010 | drawShortenedLine( mainSegmentLength, lineendSize, image, tmpMatrix ); | 1014 | drawShortenedLine( mainSegmentLength, lineendSize, image, tmpMatrix ); | ||
1011 | drawLineEnds( mainSegmentLength, lineendSize, image, tmpMatrix ); | 1015 | drawLineEnds( mainSegmentLength, lineendSize, image, tmpMatrix ); | ||
1012 | drawLeaderLine( 0., image, tmpMatrix ); | 1016 | drawLeaderLine( 0., image, tmpMatrix ); | ||
1013 | drawLeaderLine( mainSegmentLength, image, tmpMatrix ); | 1017 | drawLeaderLine( mainSegmentLength, image, tmpMatrix ); | ||
1014 | } | 1018 | } | ||
1015 | else if ( la->transformedLinePoints().count() > 2 ) | 1019 | else if ( la->transformedLinePoints().count() > 2 ) | ||
1016 | { | 1020 | { | ||
1017 | drawMainLine( image ); | 1021 | drawMainLine( image ); | ||
1018 | } | 1022 | } | ||
1019 | } | 1023 | } | ||
1020 | 1024 | | |||
1021 | void LineAnnotPainter::drawMainLine( QImage &image ) const | 1025 | void LineAnnotPainter::drawMainLine( QImage &image ) const | ||
1022 | { | 1026 | { | ||
1023 | // draw the line as normalized path into image | 1027 | // draw the line as normalized path into image | ||
1024 | PagePainter::drawShapeOnImage( image, transformPath( | 1028 | PagePainter::drawShapeOnImage( image, transformPath( | ||
1025 | la->transformedLinePoints(), paintMatrix ), la->lineClosed(), | 1029 | la->transformedLinePoints(), paintMatrix ), la->lineClosed(), | ||
1026 | linePen, fillBrush, pageScale, PagePainter::Multiply ); | 1030 | linePen, fillBrush, pageScale, PagePainter::Multiply ); | ||
1027 | } | 1031 | } | ||
1028 | 1032 | | |||
1033 | void LineAnnotPainter::drawCaption( QImage &image, double mainSegmentLength, double angle ) const | ||||
1034 | { | ||||
1035 | QPainter painter( &image ); | ||||
1036 | QFont captionFont; | ||||
1037 | const QFontMetrics captionFontMetrics { captionFont }; | ||||
1038 | const double textHeight = static_cast<double>( captionFontMetrics.height() ); | ||||
1039 | const int flags = Qt::AlignHCenter | Qt::AlignBottom | Qt::TextDontClip; | ||||
1040 | painter.setFont( captionFont ); | ||||
1041 | painter.setRenderHint( QPainter::TextAntialiasing ); | ||||
1042 | | ||||
1043 | /* | ||||
1044 | * The only sane way to draw arbitrary rotated text is using QPainter::rotate. | ||||
1045 | * But that doesn't work with normalized coordinates, | ||||
1046 | * because we can't scale the font size reliably to something in 0..1. | ||||
1047 | * So we denormalize our local construction coordinate system prior to using drawText. | ||||
1048 | */ | ||||
1049 | const QRectF denormalizedRectangle { | ||||
1050 | QPointF { 0, -textHeight }, | ||||
1051 | QSizeF { mainSegmentLength * pageSize.width(), textHeight } | ||||
1052 | }; | ||||
1053 | | ||||
1054 | /* Then let QPainter do all further transformation. */ | ||||
1055 | painter.setTransform( | ||||
1056 | QTransform().rotate( -angle * 180. / M_PI ) * | ||||
1057 | QTransform::fromScale( 1. / pageSize.width(), 1. / pageSize.height() ) * | ||||
1058 | QTransform::fromTranslate( la->transformedLinePoints().first().x, la->transformedLinePoints().first().y ) * | ||||
1059 | paintMatrix * | ||||
1060 | QTransform::fromScale( image.width(), image.height() ) | ||||
1061 | ); | ||||
1062 | painter.drawText( denormalizedRectangle, flags, la->contents() ); | ||||
1063 | } | ||||
1064 | | ||||
1029 | void LineAnnotPainter::drawShortenedLine( double mainSegmentLength, double size, QImage &image, const QTransform& constructionMatrix ) const | 1065 | void LineAnnotPainter::drawShortenedLine( double mainSegmentLength, double size, QImage &image, const QTransform& constructionMatrix ) const | ||
1030 | { | 1066 | { | ||
1031 | const QTransform combinedTransform { constructionMatrix * paintMatrix }; | 1067 | const QTransform combinedTransform { constructionMatrix * paintMatrix }; | ||
1032 | const QList<Okular::NormalizedPoint> path { | 1068 | const QList<Okular::NormalizedPoint> path { | ||
1033 | { shortenForArrow(size, la->lineStartStyle()), 0 }, | 1069 | { shortenForArrow(size, la->lineStartStyle()), 0 }, | ||
1034 | { mainSegmentLength - shortenForArrow(size, la->lineEndStyle()), 0 } | 1070 | { mainSegmentLength - shortenForArrow(size, la->lineEndStyle()), 0 } | ||
1035 | }; | 1071 | }; | ||
1036 | PagePainter::drawShapeOnImage( image, transformPath(path, combinedTransform), | 1072 | PagePainter::drawShapeOnImage( image, transformPath(path, combinedTransform), | ||
▲ Show 20 Lines • Show All 184 Lines • ▼ Show 20 Line(s) | 1250 | { | |||
1221 | { | 1257 | { | ||
1222 | shortenBy = size; | 1258 | shortenBy = size; | ||
1223 | } | 1259 | } | ||
1224 | 1260 | | |||
1225 | return shortenBy; | 1261 | return shortenBy; | ||
1226 | } | 1262 | } | ||
1227 | 1263 | | |||
1228 | /* kate: replace-tabs on; indent-width 4; */ | 1264 | /* kate: replace-tabs on; indent-width 4; */ | ||
1229 | |