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
|
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the Qwt License
# Copyright (c) 2002 Uwe Rathmann, for the original C++ code
# Copyright (c) 2015 Pierre Raybaut, for the Python translation/optimization
# (see LICENSE file for more details)
"""
QwtPlotLayout
-------------
.. autoclass:: QwtPlotLayout
:members:
"""
import math
from qtpy.QtCore import QObject, QRectF, QSize, Qt
from qtpy.QtGui import QFont, QRegion
from qwt.plot import QwtPlot
from qwt.scale_draw import QwtAbstractScaleDraw
from qwt.scale_widget import QwtScaleWidget
from qwt.text import QwtText
QWIDGETSIZE_MAX = (1 << 24) - 1
class LegendData(object):
def __init__(self):
self.frameWidth = None
self.hScrollExtent = None
self.vScrollExtent = None
self.hint = QSize()
class TitleData(object):
def __init__(self):
self.text = QwtText()
self.frameWidth = None
class FooterData(object):
def __init__(self):
self.text = QwtText()
self.frameWidth = None
class ScaleData(object):
def __init__(self):
self.isEnabled = None
self.scaleWidget = QwtScaleWidget()
self.scaleFont = QFont()
self.start = None
self.end = None
self.baseLineOffset = None
self.tickOffset = None
self.dimWithoutTitle = None
class CanvasData(object):
def __init__(self):
self.contentsMargins = [0 for _i in QwtPlot.AXES]
class QwtPlotLayout_LayoutData(object):
def __init__(self):
self.legend = LegendData()
self.title = TitleData()
self.footer = FooterData()
self.scale = [ScaleData() for _i in QwtPlot.AXES]
self.canvas = CanvasData()
def init(self, plot, rect):
"""Extract all layout relevant data from the plot components"""
# legend
legend = plot.legend()
if legend:
self.legend.frameWidth = legend.frameWidth()
self.legend.hScrollExtent = legend.scrollExtent(Qt.Horizontal)
self.legend.vScrollExtent = legend.scrollExtent(Qt.Vertical)
hint = legend.sizeHint()
w = min([hint.width(), math.floor(rect.width())])
h = legend.heightForWidth(w)
if h <= 0:
h = hint.height()
self.legend.hint = QSize(w, h)
# title
self.title.frameWidth = 0
self.title.text = QwtText()
if plot.titleLabel():
label = plot.titleLabel()
self.title.text = label.text()
if not self.title.text.testPaintAttribute(QwtText.PaintUsingTextFont):
self.title.text.setFont(label.font())
self.title.frameWidth = plot.titleLabel().frameWidth()
# footer
self.footer.frameWidth = 0
self.footer.text = QwtText()
if plot.footerLabel():
label = plot.footerLabel()
self.footer.text = label.text()
if not self.footer.text.testPaintAttribute(QwtText.PaintUsingTextFont):
self.footer.text.setFont(label.font())
self.footer.frameWidth = plot.footerLabel().frameWidth()
# scales
for axis in QwtPlot.AXES:
if plot.axisEnabled(axis):
scaleWidget = plot.axisWidget(axis)
self.scale[axis].isEnabled = True
self.scale[axis].scaleWidget = scaleWidget
self.scale[axis].scaleFont = scaleWidget.font()
self.scale[axis].start = scaleWidget.startBorderDist()
self.scale[axis].end = scaleWidget.endBorderDist()
self.scale[axis].baseLineOffset = scaleWidget.margin()
self.scale[axis].tickOffset = scaleWidget.margin()
if scaleWidget.scaleDraw().hasComponent(QwtAbstractScaleDraw.Ticks):
self.scale[
axis
].tickOffset += scaleWidget.scaleDraw().maxTickLength()
self.scale[axis].dimWithoutTitle = scaleWidget.dimForLength(
QWIDGETSIZE_MAX, self.scale[axis].scaleFont
)
if not scaleWidget.title().isEmpty():
self.scale[axis].dimWithoutTitle -= scaleWidget.titleHeightForWidth(
QWIDGETSIZE_MAX
)
else:
self.scale[axis].isEnabled = False
self.scale[axis].start = 0
self.scale[axis].end = 0
self.scale[axis].baseLineOffset = 0
self.scale[axis].tickOffset = 0.0
self.scale[axis].dimWithoutTitle = 0
layout = plot.canvas().layout()
if layout is not None:
mgn = layout.contentsMargins()
self.canvas.contentsMargins = [
mgn.left(),
mgn.top(),
mgn.right(),
mgn.bottom(),
]
class QwtPlotLayout_PrivateData(QObject):
def __init__(self):
QObject.__init__(self)
self.spacing = 5
self.titleRect = QRectF()
self.footerRect = QRectF()
self.legendRect = QRectF()
self.scaleRect = [QRectF() for _i in QwtPlot.AXES]
self.canvasRect = QRectF()
self.layoutData = QwtPlotLayout_LayoutData()
self.legendPos = None
self.legendRatio = None
self.canvasMargin = [0] * len(QwtPlot.AXES)
self.alignCanvasToScales = [False] * len(QwtPlot.AXES)
class QwtPlotLayout(object):
"""
Layout engine for QwtPlot.
It is used by the `QwtPlot` widget to organize its internal widgets
or by `QwtPlot.print()` to render its content to a QPaintDevice like
a QPrinter, QPixmap/QImage or QSvgRenderer.
.. seealso::
:py:meth:`qwt.plot.QwtPlot.setPlotLayout()`
Valid options:
* `QwtPlotLayout.AlignScales`: Unused
* `QwtPlotLayout.IgnoreScrollbars`: Ignore the dimension of the scrollbars. There are no scrollbars, when the plot is not rendered to widgets.
* `QwtPlotLayout.IgnoreFrames`: Ignore all frames.
* `QwtPlotLayout.IgnoreLegend`: Ignore the legend.
* `QwtPlotLayout.IgnoreTitle`: Ignore the title.
* `QwtPlotLayout.IgnoreFooter`: Ignore the footer.
"""
# enum Option
AlignScales = 0x01
IgnoreScrollbars = 0x02
IgnoreFrames = 0x04
IgnoreLegend = 0x08
IgnoreTitle = 0x10
IgnoreFooter = 0x20
def __init__(self):
self.__data = QwtPlotLayout_PrivateData()
self.setLegendPosition(QwtPlot.BottomLegend)
self.setCanvasMargin(4)
self.setAlignCanvasToScales(False)
self.invalidate()
def setCanvasMargin(self, margin, axis=-1):
"""
Change a margin of the canvas. The margin is the space
above/below the scale ticks. A negative margin will
be set to -1, excluding the borders of the scales.
:param int margin: New margin
:param int axisId: Axis index
.. seealso::
:py:meth:`canvasMargin()`
.. warning::
The margin will have no effect when `alignCanvasToScale()` is True
"""
if margin < 1:
margin = -1
if axis == -1:
for axis in QwtPlot.AXES:
self.__data.canvasMargin[axis] = margin
elif axis in QwtPlot.AXES:
self.__data.canvasMargin[axis] = margin
def canvasMargin(self, axisId):
"""
:param int axisId: Axis index
:return: Margin around the scale tick borders
.. seealso::
:py:meth:`setCanvasMargin()`
"""
if axisId not in QwtPlot.AXES:
return 0
return self.__data.canvasMargin[axisId]
def setAlignCanvasToScales(self, *args):
"""
Change the align-canvas-to-axis-scales setting.
.. py:method:: setAlignCanvasToScales(on):
Set the align-canvas-to-axis-scales flag for all axes
:param bool on: True/False
.. py:method:: setAlignCanvasToScales(axisId, on):
Change the align-canvas-to-axis-scales setting.
The canvas may:
- extend beyond the axis scale ends to maximize its size,
- align with the axis scale ends to control its size.
The axisId parameter is somehow confusing as it identifies a
border of the plot and not the axes, that are aligned. F.e when
`QwtPlot.yLeft` is set, the left end of the the x-axes
(`QwtPlot.xTop`, `QwtPlot.xBottom`) is aligned.
:param int axisId: Axis index
:param bool on: True/False
.. seealso::
:py:meth:`setAlignCanvasToScale()`,
:py:meth:`alignCanvasToScale()`
"""
if len(args) == 1:
(on,) = args
for axis in QwtPlot.AXES:
self.__data.alignCanvasToScales[axis] = on
elif len(args) == 2:
axisId, on = args
if axisId in QwtPlot.AXES:
self.__data.alignCanvasToScales[axisId] = on
else:
raise TypeError(
"%s().setAlignCanvasToScales() takes 1 or 2 "
"argument(s) (%s given)" % (self.__class__.__name__, len(args))
)
def alignCanvasToScale(self, axisId):
"""
Return the align-canvas-to-axis-scales setting.
The canvas may:
- extend beyond the axis scale ends to maximize its size
- align with the axis scale ends to control its size.
:param int axisId: Axis index
:return: align-canvas-to-axis-scales setting
.. seealso::
:py:meth:`setAlignCanvasToScale()`, :py:meth:`setCanvasMargin()`
"""
if axisId not in QwtPlot.AXES:
return False
return self.__data.alignCanvasToScales[axisId]
def setSpacing(self, spacing):
"""
Change the spacing of the plot. The spacing is the distance
between the plot components.
:param int spacing: New spacing
.. seealso::
:py:meth:`setCanvasMargin()`, :py:meth:`spacing()`
"""
self.__data.spacing = max([0, spacing])
def spacing(self):
"""
:return: Spacing
.. seealso::
:py:meth:`margin()`, :py:meth:`setSpacing()`
"""
return self.__data.spacing
def setLegendPosition(self, *args):
"""
Specify the position of the legend
.. py:method:: setLegendPosition(pos, [ratio=0.]):
Specify the position of the legend
:param QwtPlot.LegendPosition pos: Legend position
:param float ratio: Ratio between legend and the bounding rectangle of title, footer, canvas and axes
The legend will be shrunk if it would need more space than the
given ratio. The ratio is limited to ]0.0 .. 1.0]. In case of
<= 0.0 it will be reset to the default ratio. The default
vertical/horizontal ratio is 0.33/0.5.
Valid position values:
* `QwtPlot.LeftLegend`,
* `QwtPlot.RightLegend`,
* `QwtPlot.TopLegend`,
* `QwtPlot.BottomLegend`
.. seealso::
:py:meth:`setLegendPosition()`
"""
if len(args) == 2:
pos, ratio = args
if ratio > 1.0:
ratio = 1.0
if pos in (QwtPlot.TopLegend, QwtPlot.BottomLegend):
if ratio <= 0.0:
ratio = 0.33
self.__data.legendRatio = ratio
self.__data.legendPos = pos
elif pos in (QwtPlot.LeftLegend, QwtPlot.RightLegend):
if ratio <= 0.0:
ratio = 0.5
self.__data.legendRatio = ratio
self.__data.legendPos = pos
elif len(args) == 1:
(pos,) = args
self.setLegendPosition(pos, 0.0)
else:
raise TypeError(
"%s().setLegendPosition() takes 1 or 2 argument(s)"
"(%s given)" % (self.__class__.__name__, len(args))
)
def legendPosition(self):
"""
:return: Position of the legend
.. seealso::
:py:meth:`legendPosition()`
"""
return self.__data.legendPos
def setLegendRatio(self, ratio):
"""
Specify the relative size of the legend in the plot
:param float ratio: Ratio between legend and the bounding rectangle of title, footer, canvas and axes
The legend will be shrunk if it would need more space than the
given ratio. The ratio is limited to ]0.0 .. 1.0]. In case of
<= 0.0 it will be reset to the default ratio. The default
vertical/horizontal ratio is 0.33/0.5.
.. seealso::
:py:meth:`legendRatio()`
"""
self.setLegendPosition(self.legendPosition(), ratio)
def legendRatio(self):
"""
:return: The relative size of the legend in the plot.
.. seealso::
:py:meth:`setLegendRatio()`
"""
return self.__data.legendRatio
def setTitleRect(self, rect):
"""
Set the geometry for the title
This method is intended to be used from derived layouts
overloading `activate()`
:param QRectF rect: Rectangle
.. seealso::
:py:meth:`titleRect()`, :py:meth:`activate()`
"""
self.__data.titleRect = rect
def titleRect(self):
"""
:return: Geometry for the title
.. seealso::
:py:meth:`invalidate()`, :py:meth:`activate()`
"""
return self.__data.titleRect
def setFooterRect(self, rect):
"""
Set the geometry for the footer
This method is intended to be used from derived layouts
overloading `activate()`
:param QRectF rect: Rectangle
.. seealso::
:py:meth:`footerRect()`, :py:meth:`activate()`
"""
self.__data.footerRect = rect
def footerRect(self):
"""
:return: Geometry for the footer
.. seealso::
:py:meth:`invalidate()`, :py:meth:`activate()`
"""
return self.__data.footerRect
def setLegendRect(self, rect):
"""
Set the geometry for the legend
This method is intended to be used from derived layouts
overloading `activate()`
:param QRectF rect: Rectangle for the legend
.. seealso::
:py:meth:`footerRect()`, :py:meth:`activate()`
"""
self.__data.legendRect = rect
def legendRect(self):
"""
:return: Geometry for the legend
.. seealso::
:py:meth:`invalidate()`, :py:meth:`activate()`
"""
return self.__data.legendRect
def setScaleRect(self, axis, rect):
"""
Set the geometry for an axis
This method is intended to be used from derived layouts
overloading `activate()`
:param int axisId: Axis index
:param QRectF rect: Rectangle for the scale
.. seealso::
:py:meth:`scaleRect()`, :py:meth:`activate()`
"""
if axis in QwtPlot.AXES:
self.__data.scaleRect[axis] = rect
def scaleRect(self, axis):
"""
:param int axisId: Axis index
:return: Geometry for the scale
.. seealso::
:py:meth:`invalidate()`, :py:meth:`activate()`
"""
if axis not in QwtPlot.AXES:
return QRectF()
return self.__data.scaleRect[axis]
def setCanvasRect(self, rect):
"""
Set the geometry for the canvas
This method is intended to be used from derived layouts
overloading `activate()`
:param QRectF rect: Rectangle
.. seealso::
:py:meth:`canvasRect()`, :py:meth:`activate()`
"""
self.__data.canvasRect = rect
def canvasRect(self):
"""
:return: Geometry for the canvas
.. seealso::
:py:meth:`invalidate()`, :py:meth:`activate()`
"""
return self.__data.canvasRect
def invalidate(self):
"""
Invalidate the geometry of all components.
.. seealso::
:py:meth:`activate()`
"""
self.__data.titleRect = QRectF()
self.__data.footerRect = QRectF()
self.__data.legendRect = QRectF()
self.__data.canvasRect = QRectF()
for axis in QwtPlot.AXES:
self.__data.scaleRect[axis] = QRectF()
def minimumSizeHint(self, plot):
"""
:param qwt.plot.QwtPlot plot: Plot widget
:return: Minimum size hint
.. seealso::
:py:meth:`qwt.plot.QwtPlot.minimumSizeHint()`
"""
class _ScaleData(object):
def __init__(self):
self.w = 0
self.h = 0
self.minLeft = 0
self.minRight = 0
self.tickOffset = 0
scaleData = [_ScaleData() for _i in QwtPlot.AXES]
canvasBorder = [0 for _i in QwtPlot.AXES]
layout = plot.canvas().layout()
if layout is None:
left, top, right, bottom = 0, 0, 0, 0
else:
mgn = layout.contentsMargins()
left, top, right, bottom = (
mgn.left(),
mgn.top(),
mgn.right(),
mgn.bottom(),
)
for axis in QwtPlot.AXES:
if plot.axisEnabled(axis):
scl = plot.axisWidget(axis)
sd = scaleData[axis]
hint = scl.minimumSizeHint()
sd.w = hint.width()
sd.h = hint.height()
sd.minLeft, sd.minLeft = scl.getBorderDistHint()
sd.tickOffset = scl.margin()
if scl.scaleDraw().hasComponent(QwtAbstractScaleDraw.Ticks):
sd.tickOffset += math.ceil(scl.scaleDraw().maxTickLength())
canvasBorder[axis] = left + self.__data.canvasMargin[axis] + 1
for axis in QwtPlot.AXES:
sd = scaleData[axis]
if sd.w and axis in (QwtPlot.xBottom, QwtPlot.xTop):
if (
sd.minLeft > canvasBorder[QwtPlot.yLeft]
and scaleData[QwtPlot.yLeft].w
):
shiftLeft = sd.minLeft - canvasBorder[QwtPlot.yLeft]
if shiftLeft > scaleData[QwtPlot.yLeft].w:
shiftLeft = scaleData[QwtPlot.yLeft].w
sd.w -= shiftLeft
if (
sd.minRight > canvasBorder[QwtPlot.yRight]
and scaleData[QwtPlot.yRight].w
):
shiftRight = sd.minRight - canvasBorder[QwtPlot.yRight]
if shiftRight > scaleData[QwtPlot.yRight].w:
shiftRight = scaleData[QwtPlot.yRight].w
sd.w -= shiftRight
if sd.h and axis in (QwtPlot.yLeft, QwtPlot.yRight):
if (
sd.minLeft > canvasBorder[QwtPlot.xBottom]
and scaleData[QwtPlot.xBottom].h
):
shiftBottom = sd.minLeft - canvasBorder[QwtPlot.xBottom]
if shiftBottom > scaleData[QwtPlot.xBottom].tickOffset:
shiftBottom = scaleData[QwtPlot.xBottom].tickOffset
sd.h -= shiftBottom
if (
sd.minLeft > canvasBorder[QwtPlot.xTop]
and scaleData[QwtPlot.xTop].h
):
shiftTop = sd.minRight - canvasBorder[QwtPlot.xTop]
if shiftTop > scaleData[QwtPlot.xTop].tickOffset:
shiftTop = scaleData[QwtPlot.xTop].tickOffset
sd.h -= shiftTop
canvas = plot.canvas()
minCanvasSize = canvas.minimumSize()
w = scaleData[QwtPlot.yLeft].w + scaleData[QwtPlot.yRight].w
cw = (
max([scaleData[QwtPlot.xBottom].w, scaleData[QwtPlot.xTop].w])
+ left
+ 1
+ right
+ 1
)
w += max([cw, minCanvasSize.width()])
h = scaleData[QwtPlot.xBottom].h + scaleData[QwtPlot.xTop].h
ch = (
max([scaleData[QwtPlot.yLeft].h, scaleData[QwtPlot.yRight].h])
+ top
+ 1
+ bottom
+ 1
)
h += max([ch, minCanvasSize.height()])
for label in [plot.titleLabel(), plot.footerLabel()]:
if label and not label.text().isEmpty():
centerOnCanvas = not plot.axisEnabled(
QwtPlot.yLeft
) and plot.axisEnabled(QwtPlot.yRight)
labelW = w
if centerOnCanvas:
labelW -= scaleData[QwtPlot.yLeft].w + scaleData[QwtPlot.yRight].w
labelH = label.heightForWidth(labelW)
if labelH > labelW:
w = labelW = labelH
if centerOnCanvas:
w += scaleData[QwtPlot.yLeft].w + scaleData[QwtPlot.yRight].w
labelH = label.heightForWidth(labelW)
h += labelH + self.__data.spacing
legend = plot.legend()
if legend and not legend.isEmpty():
if self.__data.legendPos in (QwtPlot.LeftLegend, QwtPlot.RightLegend):
legendW = legend.sizeHint().width()
legendH = legend.heightForWidth(legendW)
if legend.frameWidth() > 0:
w += self.__data.spacing
if legendH > h:
legendW += legend.scrollExtent(Qt.Horizontal)
if self.__data.legendRatio < 1.0:
legendW = min([legendW, int(w / (1.0 - self.__data.legendRatio))])
w += legendW + self.__data.spacing
else:
legendW = min([legend.sizeHint().width(), w])
legendH = legend.heightForWidth(legendW)
if legend.frameWidth() > 0:
h += self.__data.spacing
if self.__data.legendRatio < 1.0:
legendH = min([legendH, int(h / (1.0 - self.__data.legendRatio))])
h += legendH + self.__data.spacing
return QSize(int(w), int(h))
def layoutLegend(self, options, rect):
"""
Find the geometry for the legend
:param options: Options how to layout the legend
:param QRectF rect: Rectangle where to place the legend
:return: Geometry for the legend
"""
hint = self.__data.layoutData.legend.hint
if self.__data.legendPos in (QwtPlot.LeftLegend, QwtPlot.RightLegend):
dim = min([hint.width(), int(rect.width() * self.__data.legendRatio)])
if not (options & self.IgnoreScrollbars):
if hint.height() > rect.height():
dim += self.__data.layoutData.legend.hScrollExtent
else:
dim = min([hint.height(), int(rect.height() * self.__data.legendRatio)])
dim = max([dim, self.__data.layoutData.legend.vScrollExtent])
legendRect = QRectF(rect)
if self.__data.legendPos == QwtPlot.LeftLegend:
legendRect.setWidth(dim)
elif self.__data.legendPos == QwtPlot.RightLegend:
legendRect.setX(rect.right() - dim)
legendRect.setWidth(dim)
elif self.__data.legendPos == QwtPlot.TopLegend:
legendRect.setHeight(dim)
elif self.__data.legendPos == QwtPlot.BottomLegend:
legendRect.setY(rect.bottom() - dim)
legendRect.setHeight(dim)
return legendRect
def alignLegend(self, canvasRect, legendRect):
"""
Align the legend to the canvas
:param QRectF canvasRect: Geometry of the canvas
:param QRectF legendRect: Maximum geometry for the legend
:return: Geometry for the aligned legend
"""
alignedRect = legendRect
if self.__data.legendPos in (QwtPlot.BottomLegend, QwtPlot.TopLegend):
if self.__data.layoutData.legend.hint.width() < canvasRect.width():
alignedRect.setX(canvasRect.x())
alignedRect.setWidth(canvasRect.width())
else:
if self.__data.layoutData.legend.hint.height() < canvasRect.height():
alignedRect.setY(canvasRect.y())
alignedRect.setHeight(canvasRect.height())
return alignedRect
def expandLineBreaks(self, options, rect):
"""
Expand all line breaks in text labels, and calculate the height
of their widgets in orientation of the text.
:param options: Options how to layout the legend
:param QRectF rect: Bounding rectangle for title, footer, axes and canvas.
:return: tuple `(dimTitle, dimFooter, dimAxes)`
Returns:
* `dimTitle`: Expanded height of the title widget
* `dimFooter`: Expanded height of the footer widget
* `dimAxes`: Expanded heights of the axis in axis orientation.
"""
dimTitle = dimFooter = 0
dimAxes = [0 for axis in QwtPlot.AXES]
backboneOffset = [0 for _i in QwtPlot.AXES]
for axis in QwtPlot.AXES:
if not (options & self.IgnoreFrames):
backboneOffset[axis] += self.__data.layoutData.canvas.contentsMargins[
axis
]
if not self.__data.alignCanvasToScales[axis]:
backboneOffset[axis] += self.__data.canvasMargin[axis]
done = False
while not done:
done = True
# the size for the 4 axis depend on each other. Expanding
# the height of a horizontal axis will shrink the height
# for the vertical axis, shrinking the height of a vertical
# axis will result in a line break what will expand the
# width and results in shrinking the width of a horizontal
# axis what might result in a line break of a horizontal
# axis ... . So we loop as long until no size changes.
if not (
(options & self.IgnoreTitle)
or self.__data.layoutData.title.text.isEmpty()
):
w = rect.width()
if (
self.__data.layoutData.scale[QwtPlot.yLeft].isEnabled
!= self.__data.layoutData.scale[QwtPlot.yRight].isEnabled
):
w -= dimAxes[QwtPlot.yLeft] + dimAxes[QwtPlot.yRight]
d = math.ceil(self.__data.layoutData.title.text.heightForWidth(w))
if not (options & self.IgnoreFrames):
d += 2 * self.__data.layoutData.title.frameWidth
if d > dimTitle:
dimTitle = d
done = False
if not (
(options & self.IgnoreFooter)
or self.__data.layoutData.footer.text.isEmpty()
):
w = rect.width()
if (
self.__data.layoutData.scale[QwtPlot.yLeft].isEnabled
!= self.__data.layoutData.scale[QwtPlot.yRight].isEnabled
):
w -= dimAxes[QwtPlot.yLeft] + dimAxes[QwtPlot.yRight]
d = math.ceil(self.__data.layoutData.footer.text.heightForWidth(w))
if not (options & self.IgnoreFrames):
d += 2 * self.__data.layoutData.footer.frameWidth
if d > dimFooter:
dimFooter = d
done = False
for axis in QwtPlot.AXES:
scaleData = self.__data.layoutData.scale[axis]
if scaleData.isEnabled:
if axis in (QwtPlot.xTop, QwtPlot.xBottom):
length = (
rect.width()
- dimAxes[QwtPlot.yLeft]
- dimAxes[QwtPlot.yRight]
)
length -= scaleData.start + scaleData.end
if dimAxes[QwtPlot.yRight] > 0:
length -= 1
length += min(
[
dimAxes[QwtPlot.yLeft],
scaleData.start - backboneOffset[QwtPlot.yLeft],
]
)
length += min(
[
dimAxes[QwtPlot.yRight],
scaleData.end - backboneOffset[QwtPlot.yRight],
]
)
else:
length = (
rect.height()
- dimAxes[QwtPlot.xTop]
- dimAxes[QwtPlot.xBottom]
)
length -= scaleData.start + scaleData.end
length -= 1
if dimAxes[QwtPlot.xBottom] <= 0:
length -= 1
if dimAxes[QwtPlot.xTop] <= 0:
length -= 1
if dimAxes[QwtPlot.xBottom] > 0:
length += min(
[
self.__data.layoutData.scale[
QwtPlot.xBottom
].tickOffset,
float(
scaleData.start
- backboneOffset[QwtPlot.xBottom]
),
]
)
if dimAxes[QwtPlot.xTop] > 0:
length += min(
[
self.__data.layoutData.scale[
QwtPlot.xTop
].tickOffset,
float(scaleData.end - backboneOffset[QwtPlot.xTop]),
]
)
if dimTitle > 0:
length -= dimTitle + self.__data.spacing
d = scaleData.dimWithoutTitle
if not scaleData.scaleWidget.title().isEmpty():
d += scaleData.scaleWidget.titleHeightForWidth(
math.floor(length)
)
if d > dimAxes[axis]:
dimAxes[axis] = d
done = False
return dimTitle, dimFooter, dimAxes
def alignScales(self, options, canvasRect, scaleRect):
"""
Align the ticks of the axis to the canvas borders using
the empty corners.
:param options: Options how to layout the legend
:param QRectF canvasRect: Geometry of the canvas ( IN/OUT )
:param QRectF scaleRect: Geometry of the scales ( IN/OUT )
"""
backboneOffset = [0 for _i in QwtPlot.AXES]
for axis in QwtPlot.AXES:
backboneOffset[axis] = 0
if not self.__data.alignCanvasToScales[axis]:
backboneOffset[axis] += self.__data.canvasMargin[axis]
if not options & self.IgnoreFrames:
backboneOffset[axis] += self.__data.layoutData.canvas.contentsMargins[
axis
]
for axis in QwtPlot.AXES:
if not scaleRect[axis].isValid():
continue
startDist = self.__data.layoutData.scale[axis].start
endDist = self.__data.layoutData.scale[axis].end
axisRect = scaleRect[axis]
if axis in (QwtPlot.xTop, QwtPlot.xBottom):
leftScaleRect = scaleRect[QwtPlot.yLeft]
leftOffset = backboneOffset[QwtPlot.yLeft] - startDist
if leftScaleRect.isValid():
dx = leftOffset + leftScaleRect.width()
if self.__data.alignCanvasToScales[QwtPlot.yLeft] and dx < 0.0:
cLeft = canvasRect.left()
canvasRect.setLeft(max([cLeft, axisRect.left() - dx]))
else:
minLeft = leftScaleRect.left()
left = axisRect.left() + leftOffset
axisRect.setLeft(max([left, minLeft]))
else:
if (
self.__data.alignCanvasToScales[QwtPlot.yLeft]
and leftOffset < 0
):
canvasRect.setLeft(
max([canvasRect.left(), axisRect.left() - leftOffset])
)
else:
if leftOffset > 0:
axisRect.setLeft(axisRect.left() + leftOffset)
rightScaleRect = scaleRect[QwtPlot.yRight]
rightOffset = backboneOffset[QwtPlot.yRight] - endDist + 1
if rightScaleRect.isValid():
dx = rightOffset + rightScaleRect.width()
if self.__data.alignCanvasToScales[QwtPlot.yRight] and dx < 0:
cRight = canvasRect.right()
canvasRect.setRight(min([cRight, axisRect.right() + dx]))
maxRight = rightScaleRect.right()
right = axisRect.right() - rightOffset
axisRect.setRight(min([right, maxRight]))
else:
if (
self.__data.alignCanvasToScales[QwtPlot.yRight]
and rightOffset < 0
):
canvasRect.setRight(
min([canvasRect.right(), axisRect.right() + rightOffset])
)
else:
if rightOffset > 0:
axisRect.setRight(axisRect.right() - rightOffset)
else:
bottomScaleRect = scaleRect[QwtPlot.xBottom]
bottomOffset = backboneOffset[QwtPlot.xBottom] - endDist + 1
if bottomScaleRect.isValid():
dy = bottomOffset + bottomScaleRect.height()
if self.__data.alignCanvasToScales[QwtPlot.xBottom] and dy < 0:
cBottom = canvasRect.bottom()
canvasRect.setBottom(min([cBottom, axisRect.bottom() + dy]))
else:
maxBottom = (
bottomScaleRect.top()
+ self.__data.layoutData.scale[QwtPlot.xBottom].tickOffset
)
bottom = axisRect.bottom() - bottomOffset
axisRect.setBottom(min([bottom, maxBottom]))
else:
if (
self.__data.alignCanvasToScales[QwtPlot.xBottom]
and bottomOffset < 0
):
canvasRect.setBottom(
min([canvasRect.bottom(), axisRect.bottom() + bottomOffset])
)
else:
if bottomOffset > 0:
axisRect.setBottom(axisRect.bottom() - bottomOffset)
topScaleRect = scaleRect[QwtPlot.xTop]
topOffset = backboneOffset[QwtPlot.xTop] - startDist
if topScaleRect.isValid():
dy = topOffset + topScaleRect.height()
if self.__data.alignCanvasToScales[QwtPlot.xTop] and dy < 0:
cTop = canvasRect.top()
canvasRect.setTop(max([cTop, axisRect.top() - dy]))
else:
minTop = (
topScaleRect.bottom()
- self.__data.layoutData.scale[QwtPlot.xTop].tickOffset
)
top = axisRect.top() + topOffset
axisRect.setTop(max([top, minTop]))
else:
if self.__data.alignCanvasToScales[QwtPlot.xTop] and topOffset < 0:
canvasRect.setTop(
max([canvasRect.top(), axisRect.top() - topOffset])
)
else:
if topOffset > 0:
axisRect.setTop(axisRect.top() + topOffset)
for axis in QwtPlot.AXES:
sRect = scaleRect[axis]
if not sRect.isValid():
continue
if axis in (QwtPlot.xBottom, QwtPlot.xTop):
if self.__data.alignCanvasToScales[QwtPlot.yLeft]:
y = canvasRect.left() - self.__data.layoutData.scale[axis].start
if not options & self.IgnoreFrames:
y += self.__data.layoutData.canvas.contentsMargins[
QwtPlot.yLeft
]
sRect.setLeft(y)
if self.__data.alignCanvasToScales[QwtPlot.yRight]:
y = canvasRect.right() - 1 + self.__data.layoutData.scale[axis].end
if not options & self.IgnoreFrames:
y -= self.__data.layoutData.canvas.contentsMargins[
QwtPlot.yRight
]
sRect.setRight(y)
if self.__data.alignCanvasToScales[axis]:
if axis == QwtPlot.xTop:
sRect.setBottom(canvasRect.top())
else:
sRect.setTop(canvasRect.bottom())
else:
if self.__data.alignCanvasToScales[QwtPlot.xTop]:
x = canvasRect.top() - self.__data.layoutData.scale[axis].start
if not options & self.IgnoreFrames:
x += self.__data.layoutData.canvas.contentsMargins[QwtPlot.xTop]
sRect.setTop(x)
if self.__data.alignCanvasToScales[QwtPlot.xBottom]:
x = canvasRect.bottom() - 1 + self.__data.layoutData.scale[axis].end
if not options & self.IgnoreFrames:
x -= self.__data.layoutData.canvas.contentsMargins[
QwtPlot.xBottom
]
sRect.setBottom(x)
if self.__data.alignCanvasToScales[axis]:
if axis == QwtPlot.yLeft:
sRect.setRight(canvasRect.left())
else:
sRect.setLeft(canvasRect.right())
def activate(self, plot, plotRect, options=0x00):
"""
Recalculate the geometry of all components.
:param qwt.plot.QwtPlot plot: Plot to be layout
:param QRectF plotRect: Rectangle where to place the components
:param options: Layout options
"""
self.invalidate()
rect = QRectF(plotRect)
self.__data.layoutData.init(plot, rect)
if (
not (options & self.IgnoreLegend)
and plot.legend()
and not plot.legend().isEmpty()
):
self.__data.legendRect = self.layoutLegend(options, rect)
region = QRegion(rect.toRect())
rect = QRectF(
region.subtracted(
QRegion(self.__data.legendRect.toRect())
).boundingRect()
)
if self.__data.legendPos == QwtPlot.LeftLegend:
rect.setLeft(rect.left() + self.__data.spacing)
elif self.__data.legendPos == QwtPlot.RightLegend:
rect.setRight(rect.right() - self.__data.spacing)
elif self.__data.legendPos == QwtPlot.TopLegend:
rect.setTop(rect.top() + self.__data.spacing)
elif self.__data.legendPos == QwtPlot.BottomLegend:
rect.setBottom(rect.bottom() - self.__data.spacing)
# +---+-----------+---+
# | Title |
# +---+-----------+---+
# | | Axis | |
# +---+-----------+---+
# | A | | A |
# | x | Canvas | x |
# | i | | i |
# | s | | s |
# +---+-----------+---+
# | | Axis | |
# +---+-----------+---+
# | Footer |
# +---+-----------+---+
# title, footer and axes include text labels. The height of each
# label depends on its line breaks, that depend on the width
# for the label. A line break in a horizontal text will reduce
# the available width for vertical texts and vice versa.
# expandLineBreaks finds the height/width for title, footer and axes
# including all line breaks.
dimTitle, dimFooter, dimAxes = self.expandLineBreaks(options, rect)
if dimTitle > 0:
self.__data.titleRect.setRect(
rect.left(), rect.top(), rect.width(), dimTitle
)
rect.setTop(self.__data.titleRect.bottom() + self.__data.spacing)
if (
self.__data.layoutData.scale[QwtPlot.yLeft].isEnabled
!= self.__data.layoutData.scale[QwtPlot.yRight].isEnabled
):
self.__data.titleRect.setX(rect.left() + dimAxes[QwtPlot.yLeft])
self.__data.titleRect.setWidth(
rect.width() - dimAxes[QwtPlot.yLeft] - dimAxes[QwtPlot.yRight]
)
if dimFooter > 0:
self.__data.footerRect.setRect(
rect.left(), rect.bottom() - dimFooter, rect.width(), dimFooter
)
rect.setBottom(self.__data.footerRect.top() - self.__data.spacing)
if (
self.__data.layoutData.scale[QwtPlot.yLeft].isEnabled
!= self.__data.layoutData.scale[QwtPlot.yRight].isEnabled
):
self.__data.footerRect.setX(rect.left() + dimAxes[QwtPlot.yLeft])
self.__data.footerRect.setWidth(
rect.width() - dimAxes[QwtPlot.yLeft] - dimAxes[QwtPlot.yRight]
)
self.__data.canvasRect.setRect(
rect.x() + dimAxes[QwtPlot.yLeft],
rect.y() + dimAxes[QwtPlot.xTop],
rect.width() - dimAxes[QwtPlot.yRight] - dimAxes[QwtPlot.yLeft],
rect.height() - dimAxes[QwtPlot.xBottom] - dimAxes[QwtPlot.xTop],
)
for axis in QwtPlot.AXES:
if dimAxes[axis]:
dim = dimAxes[axis]
scaleRect = self.__data.scaleRect[axis]
scaleRect.setRect(*self.__data.canvasRect.getRect())
if axis == QwtPlot.yLeft:
scaleRect.setX(self.__data.canvasRect.left() - dim)
scaleRect.setWidth(dim)
elif axis == QwtPlot.yRight:
scaleRect.setX(self.__data.canvasRect.right())
scaleRect.setWidth(dim)
elif axis == QwtPlot.xBottom:
scaleRect.setY(self.__data.canvasRect.bottom())
scaleRect.setHeight(dim)
elif axis == QwtPlot.xTop:
scaleRect.setY(self.__data.canvasRect.top() - dim)
scaleRect.setHeight(dim)
scaleRect = scaleRect.normalized()
# +---+-----------+---+
# | <- Axis -> |
# +-^-+-----------+-^-+
# | | | | | |
# | | | |
# | A | | A |
# | x | Canvas | x |
# | i | | i |
# | s | | s |
# | | | |
# | | | | | |
# +-V-+-----------+-V-+
# | <- Axis -> |
# +---+-----------+---+
# The ticks of the axes - not the labels above - should
# be aligned to the canvas. So we try to use the empty
# corners to extend the axes, so that the label texts
# left/right of the min/max ticks are moved into them.
self.alignScales(options, self.__data.canvasRect, self.__data.scaleRect)
if not self.__data.legendRect.isEmpty():
self.__data.legendRect = self.alignLegend(
self.__data.canvasRect, self.__data.legendRect
)
|