|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | + |
| 4 | +############################################################################# |
| 5 | +## |
| 6 | +# Copyright (C) 2013 Riverbank Computing Limited. |
| 7 | +# Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
| 8 | +# All rights reserved. |
| 9 | +## |
| 10 | +# This file is part of the examples of PyQt. |
| 11 | +## |
| 12 | +# $QT_BEGIN_LICENSE:BSD$ |
| 13 | +# You may use this file under the terms of the BSD license as follows: |
| 14 | +## |
| 15 | +# "Redistribution and use in source and binary forms, with or without |
| 16 | +# modification, are permitted provided that the following conditions are |
| 17 | +# met: |
| 18 | +# * Redistributions of source code must retain the above copyright |
| 19 | +# notice, this list of conditions and the following disclaimer. |
| 20 | +# * Redistributions in binary form must reproduce the above copyright |
| 21 | +# notice, this list of conditions and the following disclaimer in |
| 22 | +# the documentation and/or other materials provided with the |
| 23 | +# distribution. |
| 24 | +# * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
| 25 | +# the names of its contributors may be used to endorse or promote |
| 26 | +# products derived from this software without specific prior written |
| 27 | +# permission. |
| 28 | +## |
| 29 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 30 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 31 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 32 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 33 | +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 34 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 35 | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 36 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 37 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 38 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 39 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 40 | +# $QT_END_LICENSE$ |
| 41 | +## |
| 42 | +############################################################################# |
| 43 | + |
| 44 | + |
| 45 | +from PyQt5.QtCore import QPoint, QRect, QSize, Qt |
| 46 | +from PyQt5.QtWidgets import (QApplication, QLayout, QPushButton, QSizePolicy, |
| 47 | + QWidget) |
| 48 | + |
| 49 | + |
| 50 | +class Window(QWidget): |
| 51 | + def __init__(self): |
| 52 | + super(Window, self).__init__() |
| 53 | + |
| 54 | + flowLayout = FlowLayout() |
| 55 | + flowLayout.addWidget(QPushButton("Short")) |
| 56 | + flowLayout.addWidget(QPushButton("Longer")) |
| 57 | + flowLayout.addWidget(QPushButton("Different text")) |
| 58 | + flowLayout.addWidget(QPushButton("More text")) |
| 59 | + flowLayout.addWidget(QPushButton("Even longer button text")) |
| 60 | + self.setLayout(flowLayout) |
| 61 | + |
| 62 | + self.setWindowTitle("Flow Layout") |
| 63 | + |
| 64 | + |
| 65 | +class FlowLayout(QLayout): |
| 66 | + def __init__(self, parent=None, margin=0, spacing=-1): |
| 67 | + super(FlowLayout, self).__init__(parent) |
| 68 | + |
| 69 | + if parent is not None: |
| 70 | + self.setContentsMargins(margin, margin, margin, margin) |
| 71 | + |
| 72 | + self.setSpacing(spacing) |
| 73 | + |
| 74 | + self.itemList = [] |
| 75 | + |
| 76 | + def __del__(self): |
| 77 | + item = self.takeAt(0) |
| 78 | + while item: |
| 79 | + item = self.takeAt(0) |
| 80 | + |
| 81 | + def addItem(self, item): |
| 82 | + self.itemList.append(item) |
| 83 | + |
| 84 | + def count(self): |
| 85 | + return len(self.itemList) |
| 86 | + |
| 87 | + def itemAt(self, index): |
| 88 | + if index >= 0 and index < len(self.itemList): |
| 89 | + return self.itemList[index] |
| 90 | + |
| 91 | + return None |
| 92 | + |
| 93 | + def takeAt(self, index): |
| 94 | + if index >= 0 and index < len(self.itemList): |
| 95 | + return self.itemList.pop(index) |
| 96 | + |
| 97 | + return None |
| 98 | + |
| 99 | + def expandingDirections(self): |
| 100 | + return Qt.Orientations(Qt.Orientation(0)) |
| 101 | + |
| 102 | + def hasHeightForWidth(self): |
| 103 | + return True |
| 104 | + |
| 105 | + def heightForWidth(self, width): |
| 106 | + height = self.doLayout(QRect(0, 0, width, 0), True) |
| 107 | + return height |
| 108 | + |
| 109 | + def setGeometry(self, rect): |
| 110 | + super(FlowLayout, self).setGeometry(rect) |
| 111 | + self.doLayout(rect, False) |
| 112 | + |
| 113 | + def sizeHint(self): |
| 114 | + return self.minimumSize() |
| 115 | + |
| 116 | + def minimumSize(self): |
| 117 | + size = QSize() |
| 118 | + |
| 119 | + for item in self.itemList: |
| 120 | + size = size.expandedTo(item.minimumSize()) |
| 121 | + |
| 122 | + margin, _, _, _ = self.getContentsMargins() |
| 123 | + |
| 124 | + size += QSize(2 * margin, 2 * margin) |
| 125 | + return size |
| 126 | + |
| 127 | + def doLayout(self, rect, testOnly): |
| 128 | + x = rect.x() |
| 129 | + y = rect.y() |
| 130 | + lineHeight = 0 |
| 131 | + |
| 132 | + for item in self.itemList: |
| 133 | + wid = item.widget() |
| 134 | + spaceX = self.spacing() + wid.style().layoutSpacing(QSizePolicy.PushButton, |
| 135 | + QSizePolicy.PushButton, Qt.Horizontal) |
| 136 | + spaceY = self.spacing() + wid.style().layoutSpacing(QSizePolicy.PushButton, |
| 137 | + QSizePolicy.PushButton, Qt.Vertical) |
| 138 | + nextX = x + item.sizeHint().width() + spaceX |
| 139 | + if nextX - spaceX > rect.right() and lineHeight > 0: |
| 140 | + x = rect.x() |
| 141 | + y = y + lineHeight + spaceY |
| 142 | + nextX = x + item.sizeHint().width() + spaceX |
| 143 | + lineHeight = 0 |
| 144 | + |
| 145 | + if not testOnly: |
| 146 | + item.setGeometry(QRect(QPoint(x, y), item.sizeHint())) |
| 147 | + |
| 148 | + x = nextX |
| 149 | + lineHeight = max(lineHeight, item.sizeHint().height()) |
| 150 | + |
| 151 | + return y + lineHeight - rect.y() |
| 152 | + |
| 153 | + |
| 154 | +if __name__ == '__main__': |
| 155 | + |
| 156 | + import sys |
| 157 | + |
| 158 | + app = QApplication(sys.argv) |
| 159 | + mainWin = Window() |
| 160 | + mainWin.show() |
| 161 | + sys.exit(app.exec_()) |
0 commit comments