├── .gitignore ├── LICENSE ├── QTileLayout ├── __init__.py ├── tile.py └── tileLayout.py ├── README.md ├── requirements.txt ├── setup.py ├── showoff.gif ├── test.py └── testLink.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /QTileLayout/__init__.py: -------------------------------------------------------------------------------- 1 | from .tileLayout import QTileLayout 2 | -------------------------------------------------------------------------------- /QTileLayout/tile.py: -------------------------------------------------------------------------------- 1 | from json import JSONDecodeError 2 | from PyQt5 import QtWidgets, QtGui 3 | from PyQt5.QtCore import Qt, QMimeData, QByteArray 4 | from PyQt5.QtGui import QDrag 5 | from PyQt5.QtWidgets import QVBoxLayout 6 | import json 7 | 8 | 9 | class Tile(QtWidgets.QWidget): 10 | """ 11 | The basic component of a tileLayout 12 | """ 13 | 14 | def __init__(self, tileLayout, fromRow, fromColumn, rowSpan, columnSpan, verticalSpan, horizontalSpan, *args, 15 | **kwargs): 16 | super(Tile, self).__init__(*args, **kwargs) 17 | self.tileLayout = tileLayout 18 | self.originTileLayout = self.tileLayout 19 | self.fromRow = fromRow 20 | self.fromColumn = fromColumn 21 | self.rowSpan = rowSpan 22 | self.columnSpan = columnSpan 23 | self.verticalSpan = verticalSpan 24 | self.horizontalSpan = horizontalSpan 25 | self.resizeMargin = 5 26 | 27 | self.filled = False 28 | self.widget = None 29 | self.lock = None 30 | self.dragInProcess = False 31 | self.currentTileNumber = 0 32 | self.layout = QVBoxLayout() 33 | self.layout.setSpacing(0) 34 | self.layout.setContentsMargins(0, 0, 0, 0) 35 | 36 | self.__mouseMovePos = None 37 | self.__updateSizeLimit() 38 | self.setAcceptDrops(True) 39 | self.setMouseTracking(True) 40 | self.setLayout(self.layout) 41 | 42 | def updateSize(self, fromRow=None, fromColumn=None, rowSpan=None, columnSpan=None, verticalSpan=None, 43 | horizontalSpan=None): 44 | """changes the tile size""" 45 | self.fromRow = fromRow if fromRow is not None else self.fromRow 46 | self.fromColumn = fromColumn if fromColumn is not None else self.fromColumn 47 | self.rowSpan = rowSpan if rowSpan is not None else self.rowSpan 48 | self.columnSpan = columnSpan if columnSpan is not None else self.columnSpan 49 | self.verticalSpan = verticalSpan if verticalSpan is not None else self.verticalSpan 50 | self.horizontalSpan = horizontalSpan if horizontalSpan is not None else self.horizontalSpan 51 | self.__updateSizeLimit() 52 | 53 | def addWidget(self, widget): 54 | """adds a widget in the tile""" 55 | self.layout.addWidget(widget) 56 | self.widget = widget 57 | self.filled = True 58 | 59 | def getFromRow(self): 60 | """returns the tile from row""" 61 | return self.fromRow 62 | 63 | def getFromColumn(self): 64 | """returns the tile from column""" 65 | return self.fromColumn 66 | 67 | def getRowSpan(self): 68 | """returns the tile row span""" 69 | return self.rowSpan 70 | 71 | def getColumnSpan(self): 72 | """returns the tile column span""" 73 | return self.columnSpan 74 | 75 | def isFilled(self): 76 | """returns True if there is a widget in the tile, else False""" 77 | return self.filled 78 | 79 | def changeColor(self, color): 80 | """Changes the tile background color""" 81 | self.setAutoFillBackground(True) 82 | self.setPalette(color) 83 | 84 | def mouseMoveEvent(self, event): 85 | """actions to do when the mouse is moved""" 86 | if event.buttons() == Qt.LeftButton: 87 | 88 | # adjust offset from clicked point to origin of widget 89 | if self.__mouseMovePos and not self.dragInProcess and self.lock is None: 90 | globalPos = event.globalPos() 91 | lastpos = self.mapToGlobal(self.__mouseMovePos) 92 | # calculate the difference when moving the mouse 93 | diff = globalPos - lastpos 94 | 95 | if diff.manhattanLength() > 3: 96 | 97 | if self.filled and self.tileLayout.dragAndDrop: 98 | drag = self.__prepareDropData(event) 99 | self.__dragAndDropProcess(drag) 100 | for key, value in self.tileLayout.linkedLayout.items(): 101 | value.changeTilesColor('idle') 102 | 103 | if self.filled and self.tileLayout.focus: 104 | self.widget.setFocus() 105 | 106 | if not self.filled: 107 | self.setCursor(QtGui.QCursor(self.tileLayout.cursorIdle)) 108 | 109 | elif self.lock is None: 110 | 111 | westCondition = 0 <= event.pos().x() < self.resizeMargin 112 | eastCondition = self.width() >= event.pos().x() > self.width() - self.resizeMargin 113 | northCondition = 0 <= event.pos().y() < self.resizeMargin 114 | southCondition = self.height() >= event.pos().y() > self.height() - self.resizeMargin 115 | 116 | if (westCondition or eastCondition) and self.tileLayout.resizable: 117 | self.setCursor(QtGui.QCursor(self.tileLayout.cursorResizeHorizontal)) 118 | 119 | elif (northCondition or southCondition) and self.tileLayout.resizable: 120 | self.setCursor(QtGui.QCursor(self.tileLayout.cursorResizeVertical)) 121 | 122 | elif self.tileLayout.dragAndDrop and self.rect().contains(event.pos()): 123 | self.setCursor(QtGui.QCursor(self.tileLayout.cursorGrab)) 124 | 125 | else: 126 | self.setCursor(QtGui.QCursor(self.tileLayout.cursorIdle)) 127 | 128 | else: 129 | # highlight tiles that are going to be merged in the resizing 130 | x, y = event.pos().x(), event.pos().y() 131 | tileNumber = self.__getResizeTileNumber(x, y) 132 | 133 | if tileNumber != self.currentTileNumber: 134 | self.currentTileNumber = tileNumber 135 | self.tileLayout.changeTilesColor('resize') 136 | self.tileLayout.highlightTiles(self.lock, self.fromRow, self.fromColumn, tileNumber) 137 | 138 | super().mouseMoveEvent(event) 139 | 140 | def mousePressEvent(self, event): 141 | """actions to do when the mouse button is pressed""" 142 | if event.button() == Qt.LeftButton: 143 | self.__mouseMovePos = event.pos() 144 | if event.pos().x() < self.resizeMargin and self.tileLayout.resizable: 145 | self.lock = (-1, 0) # 'west' 146 | elif event.pos().x() > self.width() - self.resizeMargin and self.tileLayout.resizable: 147 | self.lock = (1, 0) # 'east' 148 | elif event.pos().y() < self.resizeMargin and self.tileLayout.resizable: 149 | self.lock = (0, -1) # 'north' 150 | elif event.pos().y() > self.height() - self.resizeMargin and self.tileLayout.resizable: 151 | self.lock = (0, 1) # 'south' 152 | if self.lock is not None: 153 | self.tileLayout.changeTilesColor('resize') 154 | else: 155 | self.__mouseMovePos = None 156 | super().mousePressEvent(event) 157 | 158 | def mouseReleaseEvent(self, event): 159 | """actions to do when the mouse button is released""" 160 | if self.lock is None: 161 | super().mouseReleaseEvent(event) 162 | return 163 | 164 | x, y = event.pos().x(), event.pos().y() 165 | tileNumber = self.__getResizeTileNumber(x, y) 166 | 167 | self.tileLayout.resizeTile(self.lock, self.fromRow, self.fromColumn, tileNumber) 168 | self.tileLayout.changeTilesColor('idle') 169 | self.currentTileNumber = 0 170 | self.lock = None 171 | super().mouseReleaseEvent(event) 172 | 173 | def dragEnterEvent(self, event): 174 | """checks if a tile can be drop on this one""" 175 | if self.tileLayout.dragAndDrop and event.mimeData().hasFormat('TileData') and self.__isDropPossible(event): 176 | event.acceptProposedAction() 177 | 178 | def dropEvent(self, event): 179 | """actions to do when a tile is dropped on this one""" 180 | dropData = json.loads(event.mimeData().data('TileData').data()) 181 | widget = self.originTileLayout.getWidgetToDrop() 182 | 183 | self.tileLayout.addWidget( 184 | widget, 185 | self.fromRow - dropData['row_offset'], 186 | self.fromColumn - dropData['column_offset'], 187 | dropData['row_span'], 188 | dropData['column_span'] 189 | ) 190 | self.tileLayout.tileMoved.emit( 191 | widget, 192 | dropData['id'], 193 | self.tileLayout.id, 194 | dropData['from_row'], 195 | dropData['from_column'], 196 | self.fromRow - dropData['row_offset'], 197 | self.fromColumn - dropData['column_offset'], 198 | ) 199 | 200 | event.acceptProposedAction() 201 | 202 | def __prepareDropData(self, event): 203 | """prepares data for the drag and drop process""" 204 | drag = QDrag(self) 205 | 206 | dropData = QMimeData() 207 | data = { 208 | 'id': self.tileLayout.id, 209 | 'from_row': self.fromRow, 210 | 'from_column': self.fromColumn, 211 | 'row_span': self.rowSpan, 212 | 'column_span': self.columnSpan, 213 | 'row_offset': event.pos().y() // (self.verticalSpan + self.tileLayout.verticalSpacing()), 214 | 'column_offset': event.pos().x() // (self.horizontalSpan + self.tileLayout.horizontalSpacing()), 215 | } 216 | dataToText = json.dumps(data) 217 | dropData.setData('TileData', QByteArray(dataToText.encode())) 218 | dragIcon = self.widget.grab() 219 | 220 | drag.setPixmap(dragIcon) 221 | drag.setMimeData(dropData) 222 | drag.setHotSpot(event.pos() - self.rect().topLeft()) 223 | 224 | return drag 225 | 226 | def __dragAndDropProcess(self, drag): 227 | """manages the drag and drop process""" 228 | self.dragInProcess = True 229 | previousRowSpan = self.rowSpan 230 | previousColumnSpan = self.columnSpan 231 | 232 | self.tileLayout.setWidgetToDrop(self.widget) 233 | self.widget.clearFocus() 234 | self.tileLayout.removeWidget(self.widget) 235 | self.setVisible(False) 236 | for key, value in self.tileLayout.linkedLayout.items(): 237 | if value.dragAndDrop: 238 | value.changeTilesColor('drag_and_drop') 239 | 240 | if drag.exec_() != 2: 241 | self.__removeWidget() 242 | widget = self.tileLayout.getWidgetToDrop() 243 | self.tileLayout.addWidget( 244 | widget, 245 | self.fromRow, 246 | self.fromColumn, 247 | previousRowSpan, 248 | previousColumnSpan 249 | ) 250 | if self.tileLayout.focus: 251 | widget.setFocus() 252 | 253 | self.originTileLayout = self.tileLayout 254 | self.setVisible(True) 255 | self.dragInProcess = False 256 | 257 | def __isDropPossible(self, event): 258 | """checks if this tile can accept the drop""" 259 | try: 260 | dropData = json.loads(event.mimeData().data('TileData').data()) 261 | except JSONDecodeError: 262 | return False 263 | 264 | if dropData['id'] not in self.tileLayout.linkedLayout: 265 | return False 266 | else: 267 | self.originTileLayout = self.tileLayout.linkedLayout[dropData['id']] 268 | 269 | for key, value in self.originTileLayout.linkedLayout.items(): 270 | if value.dragAndDrop: 271 | value.changeTilesColor('drag_and_drop') 272 | 273 | return self.tileLayout.isAreaEmpty( 274 | self.fromRow - dropData['row_offset'], 275 | self.fromColumn - dropData['column_offset'], 276 | dropData['row_span'], 277 | dropData['column_span'], 278 | color='drag_and_drop' 279 | ) 280 | 281 | def __getResizeTileNumber(self, x, y): 282 | """finds the tile number when resizing""" 283 | (dirX, dirY) = self.lock 284 | 285 | span = self.horizontalSpan * (dirX != 0) + self.verticalSpan * (dirY != 0) 286 | tileSpan = self.columnSpan * (dirX != 0) + self.rowSpan * (dirY != 0) 287 | spacing = self.tileLayout.verticalSpacing() * (dirX != 0) + self.tileLayout.horizontalSpacing() * ( 288 | dirY != 0) 289 | 290 | return int( 291 | (x * (dirX != 0) + y * (dirY != 0) + (span / 2) - span * tileSpan * ((dirX + dirY) == 1)) 292 | // (span + spacing) 293 | ) 294 | 295 | def __removeWidget(self): 296 | """removes the tile widget""" 297 | self.layout.removeWidget(self.widget) 298 | self.widget = None 299 | self.filled = False 300 | 301 | def __updateSizeLimit(self): 302 | """refreshes the tile size limit""" 303 | self.setFixedHeight( 304 | self.rowSpan * self.verticalSpan + (self.rowSpan - 1) * self.tileLayout.verticalSpacing() 305 | ) 306 | self.setFixedWidth( 307 | self.columnSpan * self.horizontalSpan + (self.columnSpan - 1) * self.tileLayout.horizontalSpacing() 308 | ) 309 | -------------------------------------------------------------------------------- /QTileLayout/tileLayout.py: -------------------------------------------------------------------------------- 1 | from PyQt5 import QtWidgets, QtCore, QtGui 2 | from PyQt5.QtCore import QRect 3 | from PyQt5.QtGui import QPalette 4 | from PyQt5.QtWidgets import QWidget 5 | import uuid 6 | 7 | from .tile import Tile 8 | 9 | 10 | class QTileLayout(QtWidgets.QGridLayout): 11 | """ 12 | A layout where the user can drag and drop widgets and resize them 13 | """ 14 | 15 | tileResized = QtCore.pyqtSignal(QWidget, int, int, int, int) 16 | tileMoved = QtCore.pyqtSignal(QWidget, str, str, int, int, int, int) 17 | 18 | def __init__(self, rowNumber, columnNumber, verticalSpan, horizontalSpan, verticalSpacing=5, horizontalSpacing=5, 19 | *args, **kwargs): 20 | super(QTileLayout, self).__init__(*args, **kwargs) 21 | 22 | # geometric parameters 23 | super().setVerticalSpacing(verticalSpacing) 24 | super().setHorizontalSpacing(horizontalSpacing) 25 | self.rowNumber = rowNumber 26 | self.columnNumber = columnNumber 27 | self.verticalSpan = verticalSpan 28 | self.horizontalSpan = horizontalSpan 29 | self.minVerticalSpan = verticalSpan 30 | self.minHorizontalSpan = horizontalSpan 31 | 32 | # logic parameters 33 | self.dragAndDrop = True 34 | self.resizable = True 35 | self.focus = False 36 | self.widgetToDrop = None 37 | self.tileMap = [] 38 | self.widgetTileCouple = {'widget': [], 'tile': []} 39 | self.id = str(uuid.uuid4()) 40 | self.linkedLayout = {self.id: self} 41 | 42 | # design parameters 43 | self.cursorIdle = QtCore.Qt.ArrowCursor 44 | self.cursorGrab = QtCore.Qt.OpenHandCursor 45 | self.cursorResizeHorizontal = QtCore.Qt.SizeHorCursor 46 | self.cursorResizeVertical = QtCore.Qt.SizeVerCursor 47 | self.colorMap = { 48 | 'drag_and_drop': (211, 211, 211), 49 | 'idle': (240, 240, 240), 50 | 'resize': (211, 211, 211), 51 | 'empty_check': (150, 150, 150), 52 | } 53 | 54 | self.setRowStretch(self.rowNumber, 1) 55 | self.setColumnStretch(self.columnNumber, 1) 56 | self.__createTileMap() 57 | 58 | def addWidget(self, widget: QWidget, fromRow: int, fromColumn: int, rowSpan: int = 1, columnSpan: int = 1): 59 | """adds a widget in the layout: works like the addWidget method in a gridLayout""" 60 | assert widget not in self.widgetTileCouple['widget'] 61 | assert self.isAreaEmpty(fromRow, fromColumn, rowSpan, columnSpan) 62 | 63 | tile = self.tileMap[fromRow][fromColumn] 64 | self.widgetTileCouple['widget'].append(widget) 65 | self.widgetTileCouple['tile'].append(tile) 66 | 67 | # if the widget is on more than 1 tile, the tiles must be merged 68 | if rowSpan > 1 or columnSpan > 1: 69 | tilesToMerge = [ 70 | (fromRow + row, fromColumn + column) 71 | for row in range(rowSpan) 72 | for column in range(columnSpan) 73 | ] 74 | self.__mergeTiles(tile, fromRow, fromColumn, rowSpan, columnSpan, tilesToMerge[1:]) 75 | 76 | widget.setMouseTracking(True) 77 | tile.addWidget(widget) 78 | 79 | def removeWidget(self, widget: QWidget): 80 | """removes the given widget""" 81 | assert widget in self.widgetTileCouple['widget'] 82 | 83 | index = self.widgetTileCouple['widget'].index(widget) 84 | tile = self.widgetTileCouple['tile'][index] 85 | 86 | fromRow = tile.getFromRow() 87 | fromColumn = tile.getFromColumn() 88 | rowSpan = tile.getRowSpan() 89 | columnSpan = tile.getColumnSpan() 90 | tilesToSplit = [ 91 | (fromRow + row, fromColumn + column) 92 | for row in range(rowSpan) 93 | for column in range(columnSpan) 94 | ] 95 | 96 | widget.setMouseTracking(False) 97 | self.hardSplitTiles(fromRow, fromColumn, tilesToSplit) 98 | self.widgetTileCouple['widget'].pop(index) 99 | self.widgetTileCouple['tile'].pop(index) 100 | self.changeTilesColor('idle') 101 | 102 | def addRows(self, rowNumber: int): 103 | """adds rows at the bottom of the layout""" 104 | assert rowNumber > 0 105 | self.setRowStretch(self.rowNumber, 0) 106 | 107 | for row in range(self.rowNumber, self.rowNumber + rowNumber): 108 | self.tileMap.append([]) 109 | for column in range(self.columnNumber): 110 | tile = self.__createTile(row, column) 111 | self.tileMap[-1].append(tile) 112 | 113 | self.rowNumber += rowNumber 114 | self.setRowStretch(self.rowNumber, 1) 115 | 116 | def addColumns(self, columnNumber: int): 117 | """adds columns at the right of the layout""" 118 | assert columnNumber > 0 119 | self.setColumnStretch(self.columnNumber, 0) 120 | 121 | for row in range(self.rowNumber): 122 | for column in range(self.columnNumber, self.columnNumber + columnNumber): 123 | tile = self.__createTile(row, column) 124 | self.tileMap[row].append(tile) 125 | 126 | self.columnNumber += columnNumber 127 | self.setColumnStretch(self.columnNumber, 1) 128 | 129 | def removeRows(self, rowNumber: int): 130 | """removes rows from the layout bottom""" 131 | assert self.isAreaEmpty(self.rowNumber - rowNumber, 0, rowNumber, self.columnNumber) 132 | 133 | for row in range(self.rowNumber - rowNumber, self.rowNumber): 134 | for column in range(self.columnNumber): 135 | super().removeWidget(self.tileMap[row][column]) 136 | self.tileMap[row][column].deleteLater() 137 | self.setRowMinimumHeight(row, 0) 138 | self.setRowStretch(row, 0) 139 | 140 | self.rowNumber -= rowNumber 141 | self.tileMap = self.tileMap[:self.rowNumber] 142 | 143 | def removeColumns(self, columnNumber: int): 144 | """removes columns from the layout right""" 145 | assert self.isAreaEmpty(0, self.columnNumber - columnNumber, self.rowNumber, columnNumber) 146 | 147 | for column in range(self.columnNumber - columnNumber, self.columnNumber): 148 | for row in range(self.rowNumber): 149 | super().removeWidget(self.tileMap[row][column]) 150 | self.tileMap[row][column].deleteLater() 151 | 152 | self.setColumnMinimumWidth(column, 0) 153 | self.setColumnStretch(column, 0) 154 | 155 | self.columnNumber -= columnNumber 156 | self.tileMap = [row[:self.columnNumber] for row in self.tileMap] 157 | 158 | def acceptDragAndDrop(self, value: bool): 159 | """is the user allowed to drag and drop tiles ?""" 160 | self.dragAndDrop = value 161 | 162 | def acceptResizing(self, value: bool): 163 | """is the user allowed to resize tiles ?""" 164 | self.resizable = value 165 | 166 | def setCursorIdle(self, value: QtCore.Qt.CursorShape): 167 | """the default cursor shape on the tiles""" 168 | self.cursorIdle = value 169 | 170 | def setCursorGrab(self, value: QtCore.Qt.CursorShape): 171 | """the cursor shape when the user can grab the tile""" 172 | self.cursorGrab = value 173 | 174 | def setCursorResizeHorizontal(self, value: QtCore.Qt.CursorShape): 175 | """the cursor shape when the user can resize the tile horizontally""" 176 | self.cursorResizeHorizontal = value 177 | 178 | def setCursorResizeVertical(self, value: QtCore.Qt.CursorShape): 179 | """the cursor shape when the user can resize the tile vertically""" 180 | self.cursorResizeVertical = value 181 | 182 | def setColorIdle(self, color: tuple): 183 | """the default tile color""" 184 | self.colorMap['idle'] = color 185 | self.changeTilesColor('idle') 186 | 187 | def setColorResize(self, color: tuple): 188 | """the tile color during resizing""" 189 | self.colorMap['resize'] = color 190 | 191 | def setColorDragAndDrop(self, color: tuple): 192 | """the tile color during drag and drop""" 193 | self.colorMap['drag_and_drop'] = color 194 | 195 | def setColorEmptyCheck(self, color: tuple): 196 | """the tile color, if empty, during drag and drop""" 197 | self.colorMap['empty_check'] = color 198 | 199 | def rowCount(self) -> int: 200 | """Returns the number of rows""" 201 | return self.rowNumber 202 | 203 | def columnCount(self) -> int: 204 | """Returns the number of columns""" 205 | return self.columnNumber 206 | 207 | def tileRect(self, row: int, column: int) -> QRect: 208 | """Returns the geometry of the tile at (row, column)""" 209 | return self.tileMap[row][column].rect() 210 | 211 | def rowsMinimumHeight(self) -> int: 212 | """Returns the minimum height""" 213 | return self.minVerticalSpan 214 | 215 | def columnsMinimumWidth(self) -> int: 216 | """Returns the minimum width""" 217 | return self.minHorizontalSpan 218 | 219 | def setRowsMinimumHeight(self, height: int): 220 | """Sets the minimum tiles height""" 221 | self.minVerticalSpan = height 222 | if self.minVerticalSpan > self.verticalSpan: 223 | self.verticalSpan = self.minVerticalSpan 224 | self.__updateAllTiles() 225 | 226 | def setColumnsMinimumWidth(self, width: int): 227 | """Sets the minimum tiles width""" 228 | self.minHorizontalSpan = width 229 | if self.minHorizontalSpan > self.horizontalSpan: 230 | self.horizontalSpan = self.minHorizontalSpan 231 | self.__updateAllTiles() 232 | 233 | def setRowsHeight(self, height: int): 234 | """Sets the tiles height""" 235 | assert self.minVerticalSpan <= height 236 | self.verticalSpan = height 237 | self.__updateAllTiles() 238 | 239 | def setColumnsWidth(self, width: int): 240 | """Sets the tiles width""" 241 | assert self.minHorizontalSpan <= width 242 | self.horizontalSpan = width 243 | self.__updateAllTiles() 244 | 245 | def setVerticalSpacing(self, spacing: int): 246 | """Sets the vertical spacing between two tiles""" 247 | super().setVerticalSpacing(spacing) 248 | self.__updateAllTiles() 249 | 250 | def setHorizontalSpacing(self, spacing: int): 251 | """Sets the horizontal spacing between two tiles""" 252 | super().setHorizontalSpacing(spacing) 253 | self.__updateAllTiles() 254 | 255 | def getId(self): 256 | """Returns the layout id""" 257 | return self.id 258 | 259 | def activateFocus(self, focus: bool): 260 | """Activates or not the widget focus after drag & drop or resize""" 261 | self.focus = focus 262 | 263 | def widgetList(self) -> list: 264 | """Returns the widgets currently in the layout""" 265 | return self.widgetTileCouple['widget'] 266 | 267 | def linkLayout(self, layout: QtWidgets.QLayout): 268 | """Links this layout with another one to allow drag and drop between them""" 269 | assert isinstance(layout, QTileLayout) 270 | assert layout.id not in self.linkedLayout 271 | assert self.id not in layout.linkedLayout 272 | self.linkedLayout[layout.id] = layout 273 | layout.linkedLayout[self.id] = self 274 | 275 | def unLinkLayout(self, layout: QtWidgets.QLayout): 276 | """Unlinks this layout with another one to forbid drag and drop between them""" 277 | assert isinstance(layout, QTileLayout) 278 | assert layout.id != self.id 279 | assert layout.id in self.linkedLayout 280 | assert self.id in layout.linkedLayout 281 | self.linkedLayout.pop(layout.id) 282 | layout.linkedLayout.pop(self.id) 283 | 284 | def highlightTiles(self, direction, fromRow, fromColumn, tileNumber): 285 | """highlights tiles that will be merged during resizing""" 286 | tile = self.tileMap[fromRow][fromColumn] 287 | tilesToMerge, increase, fromRow, fromColumn, rowSpan, columnSpan = self.__getTilesToBeResized( 288 | tile, direction, fromRow, fromColumn, tileNumber 289 | ) 290 | 291 | if tilesToMerge: 292 | self.changeTilesColor('empty_check', (fromRow, fromColumn), (rowSpan, columnSpan)) 293 | 294 | def resizeTile(self, direction, fromRow, fromColumn, tileNumber): 295 | """called when a tile is resized""" 296 | tile = self.tileMap[fromRow][fromColumn] 297 | tilesToMerge, increase, fromRow, fromColumn, rowSpan, columnSpan = self.__getTilesToBeResized( 298 | tile, direction, fromRow, fromColumn, tileNumber 299 | ) 300 | 301 | if tilesToMerge: 302 | if increase: 303 | self.__mergeTiles(tile, fromRow, fromColumn, rowSpan, columnSpan, tilesToMerge) 304 | else: 305 | self.__splitTiles(tile, fromRow, fromColumn, rowSpan, columnSpan, tilesToMerge) 306 | index = self.widgetTileCouple['tile'].index(tile) 307 | widget = self.widgetTileCouple['widget'][index] 308 | self.tileResized.emit(widget, fromRow, fromColumn, rowSpan, columnSpan) 309 | 310 | def hardSplitTiles(self, fromRow, fromColumn, tilesToSplit): 311 | """splits the tiles and return the new one at (fromRow, fromColumn)""" 312 | assert (fromRow, fromColumn) in tilesToSplit 313 | tilesToRecycle = set() 314 | 315 | for row, column in tilesToSplit: 316 | tilesToRecycle.add(self.tileMap[row][column]) 317 | self.__createTile(row, column, updateTileMap=True) 318 | 319 | for tile in tilesToRecycle: 320 | super().removeWidget(tile) 321 | tile.deleteLater() 322 | 323 | tile = self.tileMap[fromRow][fromColumn] 324 | super().addWidget(tile, fromRow, fromColumn) 325 | return tile 326 | 327 | def isAreaEmpty(self, fromRow, fromColumn, rowSpan, columnSpan, color=''): 328 | """checks if the given space is free from widgets""" 329 | if isinstance(color, str) and color in self.colorMap.keys(): 330 | self.changeTilesColor(color) 331 | if ((fromRow + rowSpan > self.rowNumber) or (fromColumn + columnSpan > self.columnNumber) or 332 | (fromRow < 0) or (fromColumn < 0)): 333 | isEmpty = False 334 | else: 335 | isEmpty = all([not self.tileMap[fromRow + row][fromColumn + column].isFilled() 336 | for row in range(rowSpan) for column in range(columnSpan)]) 337 | if isEmpty and isinstance(color, str) and color in self.colorMap.keys(): 338 | self.changeTilesColor('empty_check', (fromRow, fromColumn), (rowSpan, columnSpan)) 339 | return isEmpty 340 | 341 | def getWidgetToDrop(self): 342 | """gets the widget that the user is dragging""" 343 | widget = self.widgetToDrop 344 | self.widgetToDrop = None 345 | return widget 346 | 347 | def setWidgetToDrop(self, widget): 348 | """sets the widget that the user is dragging""" 349 | self.widgetToDrop = widget 350 | 351 | def changeTilesColor(self, colorChoice, from_tile=(0, 0), to_tile=None): 352 | """changes the color of all tiles""" 353 | palette = QPalette() 354 | palette.setColor(QPalette.Background, QtGui.QColor(*self.colorMap[colorChoice])) 355 | palette_idle = QPalette() 356 | palette_idle.setColor(QPalette.Background, QtGui.QColor(*self.colorMap['idle'])) 357 | if to_tile is None: 358 | to_tile = (self.rowNumber, self.columnNumber) 359 | for row in range(from_tile[0], from_tile[0] + to_tile[0]): 360 | for column in range(from_tile[1], from_tile[1] + to_tile[1]): 361 | if not self.tileMap[row][column].isFilled(): 362 | self.tileMap[row][column].changeColor(palette) 363 | else: 364 | self.tileMap[row][column].changeColor(palette_idle) 365 | 366 | def updateGlobalSize(self, newSize: QtGui.QResizeEvent): 367 | """update the size of the layout""" 368 | verticalMargins = self.contentsMargins().top() + self.contentsMargins().bottom() 369 | verticalSpan = int( 370 | (newSize.size().height() - (self.rowNumber - 1) * self.verticalSpacing() - verticalMargins) 371 | // self.rowNumber 372 | ) 373 | 374 | horizontalMargins = self.contentsMargins().left() + self.contentsMargins().right() 375 | horizontalSpan = int( 376 | (newSize.size().width() - (self.columnNumber - 1) * self.horizontalSpacing() - horizontalMargins) 377 | // self.columnNumber 378 | ) 379 | 380 | self.verticalSpan = max(verticalSpan, self.minVerticalSpan) 381 | self.horizontalSpan = max(horizontalSpan, self.minHorizontalSpan) 382 | self.__updateAllTiles() 383 | 384 | def __mergeTiles(self, tile, fromRow, fromColumn, rowSpan, columnSpan, tilesToMerge): 385 | """merges the tilesToMerge with tile""" 386 | for row, column in tilesToMerge: 387 | super().removeWidget(self.tileMap[row][column]) 388 | self.tileMap[row][column].deleteLater() 389 | self.tileMap[row][column] = tile 390 | 391 | super().removeWidget(tile) 392 | super().addWidget(tile, fromRow, fromColumn, rowSpan, columnSpan) 393 | tile.updateSize(fromRow, fromColumn, rowSpan, columnSpan) 394 | 395 | def __splitTiles(self, tile, fromRow, fromColumn, rowSpan, columnSpan, tilesToSplit): 396 | """splits the tilesToSplit from tile""" 397 | for row, column in tilesToSplit: 398 | self.__createTile(row, column, updateTileMap=True) 399 | 400 | super().removeWidget(tile) 401 | super().addWidget(tile, fromRow, fromColumn, rowSpan, columnSpan) 402 | tile.updateSize(fromRow, fromColumn, rowSpan, columnSpan) 403 | 404 | def __createTile(self, fromRow, fromColumn, rowSpan=1, columnSpan=1, updateTileMap=False): 405 | """creates a tile: a tile is basically a place holder that can contain a widget or not""" 406 | tile = Tile( 407 | self, 408 | fromRow, 409 | fromColumn, 410 | rowSpan, 411 | columnSpan, 412 | self.verticalSpan, 413 | self.horizontalSpan, 414 | ) 415 | super().addWidget(tile, fromRow, fromColumn, rowSpan, columnSpan) 416 | 417 | if updateTileMap: 418 | tilePositions = [ 419 | (fromRow + row, fromColumn + column) 420 | for row in range(rowSpan) 421 | for column in range(columnSpan) 422 | ] 423 | for (row, column) in tilePositions: 424 | self.tileMap[row][column] = tile 425 | 426 | return tile 427 | 428 | def __getTilesToBeResized(self, tile, direction, fromRow, fromColumn, tileNumber): 429 | """recovers the tiles that will be merged or split during resizing""" 430 | rowSpan = tile.getRowSpan() 431 | columnSpan = tile.getColumnSpan() 432 | increase = True 433 | (dirX, dirY) = direction 434 | 435 | # increase tile size 436 | if tileNumber * (dirX + dirY) > 0: 437 | tileNumber, tilesToMerge = self.__getTilesToMerge(direction, fromRow, fromColumn, tileNumber) 438 | # decrease tile size 439 | else: 440 | tileNumber, tilesToMerge = self.__getTilesToSplit(direction, fromRow, fromColumn, tileNumber) 441 | increase = False 442 | 443 | columnSpan += tileNumber * dirX 444 | fromColumn += tileNumber * (dirX == -1) 445 | rowSpan += tileNumber * dirY 446 | fromRow += tileNumber * (dirY == -1) 447 | 448 | return tilesToMerge, increase, fromRow, fromColumn, rowSpan, columnSpan 449 | 450 | def __getTilesToSplit(self, direction, fromRow, fromColumn, tileNumber): 451 | """finds the tiles to split when a tile is decreased""" 452 | tile = self.tileMap[fromRow][fromColumn] 453 | rowSpan = tile.getRowSpan() 454 | columnSpan = tile.getColumnSpan() 455 | (dirX, dirY) = direction 456 | 457 | tileNumber = ( 458 | tileNumber 459 | if -tileNumber * (dirX + dirY) < columnSpan * (dirX != 0) + rowSpan * (dirY != 0) 460 | else (1 - columnSpan) * dirX + (1 - rowSpan) * dirY 461 | ) 462 | tilesToMerge = [ 463 | ( 464 | fromRow + row + (rowSpan - 2 * row - 1) * (dirY == 1), 465 | fromColumn + column + (columnSpan - 2 * column - 1) * (dirX == 1) 466 | ) 467 | for row in range(-tileNumber * dirY + rowSpan * (dirX != 0)) 468 | for column in range(-tileNumber * dirX + columnSpan * (dirY != 0)) 469 | ] 470 | 471 | return tileNumber, tilesToMerge 472 | 473 | def __getTilesToMerge(self, direction, fromRow, fromColumn, tileNumber): 474 | """finds the tiles to merge when a tile is increased""" 475 | tile = self.tileMap[fromRow][fromColumn] 476 | rowSpan = tile.getRowSpan() 477 | columnSpan = tile.getColumnSpan() 478 | tileNumberAvailable = 0 479 | tilesToMerge = [] 480 | (dirX, dirY) = direction 481 | 482 | tileNumber = ( 483 | max(tileNumber, -fromColumn * (dirX != 0) - fromRow * (dirY != 0)) 484 | if (dirX + dirY == -1) 485 | else min( 486 | tileNumber, 487 | ((self.columnNumber - fromColumn - columnSpan) * (dirX != 0) 488 | + (self.rowNumber - fromRow - rowSpan) * (dirY != 0)) 489 | ) 490 | ) 491 | 492 | # west or east 493 | if dirX != 0: 494 | for column in range(tileNumber * dirX): 495 | tilesToCheck = [] 496 | columnDelta = (columnSpan + column) * (dirX == 1) + (-column - 1) * (dirX == -1) 497 | for row in range(rowSpan): 498 | tilesToCheck.append((fromRow + row, fromColumn + columnDelta)) 499 | if self.tileMap[fromRow + row][fromColumn + columnDelta].isFilled(): 500 | return tileNumberAvailable, self.__flattenList(tilesToMerge) 501 | tileNumberAvailable += dirX 502 | tilesToMerge.append(tilesToCheck) 503 | 504 | # north or south 505 | else: 506 | for row in range(tileNumber * dirY): 507 | tilesToCheck = [] 508 | rowDelta = (rowSpan + row) * (dirY == 1) + (-row - 1) * (dirY == -1) 509 | for column in range(columnSpan): 510 | tilesToCheck.append((fromRow + rowDelta, fromColumn + column)) 511 | if self.tileMap[fromRow + rowDelta][fromColumn + column].isFilled(): 512 | return tileNumberAvailable, self.__flattenList(tilesToMerge) 513 | tileNumberAvailable += dirY 514 | tilesToMerge.append(tilesToCheck) 515 | 516 | return tileNumberAvailable, self.__flattenList(tilesToMerge) 517 | 518 | def __createTileMap(self): 519 | """Creates a map to be able to locate each tile on the grid""" 520 | for row in range(self.rowNumber): 521 | self.tileMap.append([]) 522 | for column in range(self.columnNumber): 523 | tile = self.__createTile(row, column) 524 | self.tileMap[-1].append(tile) 525 | 526 | def __updateAllTiles(self): 527 | """Forces the tiles to update their geometry""" 528 | for row in range(self.rowNumber): 529 | for column in range(self.columnNumber): 530 | self.tileMap[row][column].updateSize( 531 | verticalSpan=self.verticalSpan, 532 | horizontalSpan=self.horizontalSpan 533 | ) 534 | 535 | @staticmethod 536 | def __flattenList(toFlatten): 537 | """returns a 1D list given a 2D list""" 538 | return [item for aList in toFlatten for item in aList] 539 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # qt-tile-layout 2 | 3 | ![PyPI](https://img.shields.io/pypi/v/pyqt5-tile-layout) 4 | 5 | A tile layout for PyQt where you can put any widget in a tile. The user is then able to drag and drop the tiles and resize them 6 | 7 | ![](https://github.com/arnaudframmery/qt-tile-layout/blob/main/showoff.gif?raw=true) 8 | 9 | # Quick example 10 | 11 | Just launch the ```test.py``` script to have a first look (be sure to have installed PyQt5 from the `requirements.txt` file). 12 | You can have an overview of how to use the different methods in this script. 13 | 14 | Moreover, you can change the value of ```static_layout``` variable to ```False``` to experiment a tile layout where the tile sizes are dynamics with the parent widget size (like a classic layout) 15 | 16 | If you are interested about drag and drop widgets between several QTileLayouts, check the ```testLink.py``` script. 17 | 18 | # Create and use a tile layout 19 | 20 | First, you have to install the PyPi package: 21 | 22 | ```shell script 23 | pip install pyqt5-tile-layout 24 | ``` 25 | 26 | And import the QTileLayout: 27 | 28 | ```python 29 | from QTileLayout import QTileLayout 30 | ``` 31 | 32 | Then, let's create a tile layout with 8 rows and 5 columns. 33 | We also give the vertical and horizontal spawn in pixel: 34 | 35 | ```python 36 | layout = QTileLayout( 37 | rowNumber=8, 38 | columnNumber=5, 39 | verticalSpawn=100, 40 | horizontalSpan=150, 41 | verticalSpacing=5, 42 | horizontalSpacing=5, 43 | ) 44 | ``` 45 | 46 | We can now add a widget in a specific position: it's the same as the grid layout: 47 | 48 | ```python 49 | layout.addWidget( 50 | widget=QtWidgets.QLabel('Hello world'), 51 | fromRow=3, 52 | fromColumn=2, 53 | rowSpan=1, 54 | columnSpan=2, 55 | ) 56 | ``` 57 | 58 | Finally, if you put your layout into a window, you will be able to drag and drop the above widget and resize its 59 | 60 | # Documentation 61 | 62 | ```QTileLayout(int fromRow, int fromColumn, int rowSpan, int columnSpan, int verticalSpacing, int horizontalSpacing)``` 63 | 64 | _Constructs a new tile layout_ 65 | 66 | ##### Methods: 67 | 68 | - ```acceptDragAndDrop(bool value)``` 69 | 70 | _Allows or not the drag and drop of tiles in the layout_ 71 |   72 | 73 | - ```acceptResizing(bool value)``` 74 | 75 | _Allows or not the resizing of tiles in the layout_ 76 |   77 | 78 | - ```activateFocus(bool focus)``` 79 | 80 | _Activates or not the widget focus after drag & drop or resize. This feature can lead to unexpected behaviours in some cases, please set focus on false if you notice any of them_ 81 |   82 | 83 | - ```addcolumns(int columnNumber)``` 84 | 85 | _Adds columns at the right of the layout_ 86 |   87 | 88 | - ```addRows(int rowNumber)``` 89 | 90 | _Adds rows at the bottom of the layout_ 91 |   92 | 93 | - ```addWidget(QWidget widget, int fromRow, int fromColumn, int rowSpan, int columnSpan)``` 94 | 95 | _Adds the given widget to the layout, spanning multiple rows/columns. The tile will start at fromRow, fromColumn spanning rowSpan rows and columnSpan columns_ 96 |   97 | 98 | - ```columnCount() -> int``` 99 | 100 | _Returns the number of column in the layout_ 101 |   102 | 103 | - ```columnsMinimumwidth() -> int``` 104 | 105 | _Returns the minimal tile width of span one_ 106 |   107 | 108 | - ```getId() -> str``` 109 | 110 | _Returns the layout id_ 111 |   112 | 113 | - ```horizontalSpacing() -> int``` 114 | 115 | _Returns the horizontal spacing between two tiles_ 116 |   117 | 118 | - ```linkLayout(QTileLayout layout)``` 119 | 120 | _Allows the drag and drop between several layouts (see testLink.py)_ 121 |   122 | 123 | - ```removecolumns(int columnNumber)``` 124 | 125 | _Removes columns at the right of the layout, raises an error if a widget is in the target area_ 126 |   127 | 128 | - ```removeRows(int rowNumber)``` 129 | 130 | _Adds rows at the bottom of the layout, raises an error if a widget is in the target area_ 131 |   132 | 133 | - ```removeWidget(QWidget widget)``` 134 | 135 | _Removes the given widget from the layout_ 136 |   137 | 138 | - ```rowCount() -> int``` 139 | 140 | _Returns the number of row in the layout_ 141 |   142 | 143 | - ```rowsMinimumHeight() -> int``` 144 | 145 | _Returns the minimal tile height of span one_ 146 |   147 | 148 | - ```setColorDragAndDrop(tuple color)``` 149 | 150 | _Sets the RGB color of the tiles during drag and drop_ 151 |   152 | 153 | - ```setColorIdle(tuple color)``` 154 | 155 | _Sets the default RGB color of the tiles_ 156 |   157 | 158 | - ```setColorResize(tuple color)``` 159 | 160 | _Sets the RGB color of the tiles during resizing_ 161 |   162 | 163 | - ```setColorEmptyCheck(tuple color)``` 164 | 165 | _Sets the RGB color of the tiles where the dragged tile fits during drag and drop_ 166 |   167 | 168 | - ```setColumnsWidth(int width)``` 169 | 170 | _Sets the tiles width (in pixels) of span one_ 171 |   172 | 173 | - ```setColumnsMinimumWidth(int width)``` 174 | 175 | _Sets the minimum tiles width (in pixels) of span one_ 176 |   177 | 178 | - ```setCursorGrab(QtCore.Qt.CursorShape value)``` 179 | 180 | _Changes the cursor shape when it is possible to drag a tile_ 181 |   182 | 183 | - ```setCursorIdle(QtCore.Qt.CursorShape value)``` 184 | 185 | _Changes the cursor shape when it is over a tile_ 186 |   187 | 188 | - ```setCursorResizeHorizontal(QtCore.Qt.CursorShape value)``` 189 | 190 | _Changes the cursor shape when it is possible to resize a tile horizontally_ 191 |   192 | 193 | - ```setCursorResizeVertical(QtCore.Qt.CursorShape value)``` 194 | 195 | _Changes the cursor shape when it is possible to resize a tile vertically_ 196 |   197 | 198 | - ```setHorizontalSpacing(int spacing)``` 199 | 200 | _Changes the horizontal spacing between two tiles_ 201 |   202 | 203 | - ```setRowsHeight(int height)``` 204 | 205 | _Sets the tiles height (in pixels) of span one_ 206 |   207 | 208 | - ```setRowsMinimumHeight(int height)``` 209 | 210 | _Sets the minimum tiles height (in pixels) of span one_ 211 |   212 | 213 | - ```setVerticalSpacing(int spacing)``` 214 | 215 | _Changes the vertical spacing between two tiles_ 216 |   217 | 218 | - ```tileRect(int row, int column) -> QRect``` 219 | 220 | _Returns the geometry of the tile at (row, column)_ 221 |   222 | 223 | - ```unLinkLayout(QTileLayout layout)``` 224 | 225 | _Forbids the drag and drop between several layouts (see testLink.py)_ 226 |   227 | 228 | - ```verticalSpacing() -> int``` 229 | 230 | _Returns the vertical spacing between two tiles_ 231 |   232 | 233 | - ```widgetList() -> list``` 234 | 235 | _Returns the widgets that are currently in the layout_ 236 |   237 | 238 | ##### Signals: 239 | 240 | - ```tileMoved(QWidget widget, str fromLayoutId, str toLayoutId, int fromRow, int fromColumn, int toRow, int toColumn)``` 241 | 242 | _Emits when a tile is moved successfully. When the source layout is not the same than the destination one, the signal is emitted from the destination layout_ 243 |   244 | 245 | - ```tileResized(QWidget widget, int fromRow, int fromColumn, int rowSpan, int columnSpan)``` 246 | 247 | _Emits when a tile is resized successfully_ 248 |   249 | 250 | # Last word 251 | 252 | Feel free to use this layout and to notice me if there are some bugs or useful features to add -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | PyQt5~=5.12 -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | with open('./README.md', encoding='utf-8') as f: 4 | long_description = f.read() 5 | 6 | setup( 7 | name="pyqt5-tile-layout", 8 | version="0.1.7", 9 | author="Arnaud Frammery", 10 | author_email="arnaud.frammery@gmail.com", 11 | description="A tile layout for PyQt5", 12 | long_description=long_description, 13 | long_description_content_type='text/markdown', 14 | license="GPL3", 15 | url="https://github.com/arnaudframmery/qt-tile-layout", 16 | packages=["QTileLayout"], 17 | install_requires=["PyQt5~=5.12"], 18 | ) 19 | -------------------------------------------------------------------------------- /showoff.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arnaudframmery/qt-tile-layout/17259c3061ce69fe9cd53c1e745b8e8520fd2e1d/showoff.gif -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import random 2 | import sys 3 | from PyQt5 import QtWidgets, QtGui, QtCore 4 | from PyQt5.QtGui import QPalette, QFont 5 | 6 | from QTileLayout import QTileLayout 7 | 8 | 9 | possible_text = [ 10 | 'Hello', 11 | 'Salut', 12 | 'Hallo', 13 | 'Hola', 14 | 'Ciao', 15 | 'Ola', 16 | 'Hej', 17 | 'Saluton', 18 | 'Szia', 19 | ] 20 | 21 | possible_colors = [ 22 | (255, 153, 51), # orange 23 | (153, 0, 153), # purple 24 | (204, 204, 0), # yellow 25 | (51, 102, 204), # blue 26 | (0, 204, 102), # green 27 | (153, 102, 51), # brown 28 | (255, 51, 51), # red 29 | ] 30 | 31 | 32 | class Label(QtWidgets.QLabel): 33 | """ 34 | Label with some logs about focus 35 | => the last widget that has been resized or dragged and dropped should have the focus 36 | """ 37 | 38 | def focusInEvent(self, event): 39 | print("WIDGET FOCUS IN EVENT", self) 40 | super().focusInEvent(event) 41 | 42 | def focusOutEvent(self, event): 43 | print("WIDGET FOCUS OUT EVENT", self) 44 | super().focusOutEvent(event) 45 | 46 | def keyPressEvent(self, ev: QtGui.QKeyEvent): 47 | print("WIDGET KEY PRESSED", self) 48 | super().keyPressEvent(ev) 49 | 50 | 51 | class MainWindow(QtWidgets.QMainWindow): 52 | """ 53 | creates a window and spawns some widgets to be able to test the tile layout 54 | """ 55 | 56 | def __init__(self, *args, **kwargs): 57 | super(MainWindow, self).__init__(*args, **kwargs) 58 | 59 | self.font = QFont('Latin', 15) 60 | self.font.setBold(True) 61 | 62 | row_number = 6 63 | column_number = 4 64 | vertical_span = 100 65 | horizontal_span = 150 66 | spacing = 5 67 | static_layout = True 68 | 69 | # create the tile layout 70 | self.tile_layout = QTileLayout( 71 | rowNumber=row_number, 72 | columnNumber=column_number, 73 | verticalSpan=vertical_span, 74 | horizontalSpan=horizontal_span, 75 | verticalSpacing=spacing, 76 | horizontalSpacing=spacing, 77 | ) 78 | 79 | # allow the user to drag and drop tiles or not 80 | self.tile_layout.acceptDragAndDrop(True) 81 | # allow the user to resize tiles or not 82 | self.tile_layout.acceptResizing(True) 83 | 84 | # set the default cursor shape on the tiles 85 | self.tile_layout.setCursorIdle(QtCore.Qt.ArrowCursor) 86 | # set the cursor shape when the user can grab the tile 87 | self.tile_layout.setCursorGrab(QtCore.Qt.OpenHandCursor) 88 | # set the cursor shape when the user can resize the tile horizontally 89 | self.tile_layout.setCursorResizeHorizontal(QtCore.Qt.SizeHorCursor) 90 | # set the cursor shape when the user can resize the tile vertically 91 | self.tile_layout.setCursorResizeVertical(QtCore.Qt.SizeVerCursor) 92 | 93 | # set default tile color 94 | self.tile_layout.setColorIdle((240, 240, 240)) 95 | # set tile color during resize 96 | self.tile_layout.setColorResize((211, 211, 211)) 97 | # set tile color of all tiles during drag and drop 98 | self.tile_layout.setColorDragAndDrop((211, 211, 211)) 99 | # set tile color of hovered tiles during drag and drop, if empty 100 | self.tile_layout.setColorEmptyCheck((150, 150, 150)) 101 | 102 | # add widgets in the tile layout 103 | for i_row in range(row_number - 2): 104 | for i_column in range(column_number): 105 | self.tile_layout.addWidget( 106 | widget=self.__spawnWidget(), 107 | fromRow=i_row, 108 | fromColumn=i_column, 109 | ) 110 | self.tile_layout.addWidget( 111 | widget=self.__spawnWidget(), 112 | fromRow=row_number - 2, 113 | fromColumn=1, 114 | rowSpan=2, 115 | columnSpan=2 116 | ) 117 | 118 | # remove a widget from the tile layout 119 | last_widget = self.__spawnWidget() 120 | self.tile_layout.addWidget( 121 | widget=last_widget, 122 | fromRow=row_number - 1, 123 | fromColumn=0, 124 | rowSpan=1, 125 | columnSpan=1 126 | ) 127 | self.tile_layout.removeWidget(last_widget) 128 | 129 | # return the number of rows 130 | print(f'Row number: {self.tile_layout.rowCount()}') 131 | # return the number of columns 132 | print(f'Column number: {self.tile_layout.columnCount()}') 133 | # return the geometry of a specific tile 134 | print(f'One tile geometry: {self.tile_layout.tileRect(row_number - 1, 1)}') 135 | # return the tile height 136 | print(f'Tile height: {self.tile_layout.rowsMinimumHeight()}') 137 | # return the tile width 138 | print(f'Tile width: {self.tile_layout.columnsMinimumWidth()}') 139 | # return the vertical spacing between two tiles 140 | print(f'Layout vertical spacing: {self.tile_layout.verticalSpacing()}') 141 | # return the horizontal spacing between two tiles 142 | print(f'Layout horizontal spacing: {self.tile_layout.horizontalSpacing()}') 143 | # return the widgets currently in the layout 144 | print(f'Number of widget: {len(self.tile_layout.widgetList())}') 145 | 146 | # set the vertical spacing between two tiles 147 | self.tile_layout.setVerticalSpacing(5) 148 | # set the horizontal spacing between two tiles 149 | self.tile_layout.setHorizontalSpacing(5) 150 | # set the minimum tiles height 151 | self.tile_layout.setRowsMinimumHeight(100) 152 | # set the minimum tiles width 153 | self.tile_layout.setColumnsMinimumWidth(150) 154 | # set the tiles height 155 | self.tile_layout.setRowsHeight(100) 156 | # set the tiles width 157 | self.tile_layout.setColumnsWidth(150) 158 | 159 | # set the focus on widget after drag & drop or resize 160 | self.tile_layout.activateFocus(False) 161 | 162 | # actions to do when a tile is resized 163 | self.tile_layout.tileResized.connect(self.__tileHasBeenResized) 164 | # actions to do when a tile is moved 165 | self.tile_layout.tileMoved.connect(self.__tileHasBeenMoved) 166 | 167 | # adds rows at the bottom of the layout 168 | self.tile_layout.addRows(1) 169 | # adds columns at the right of the layout 170 | self.tile_layout.addColumns(1) 171 | # removes rows from the layout bottom 172 | self.tile_layout.removeRows(1) 173 | # removes columns from the layout right 174 | self.tile_layout.removeColumns(1) 175 | 176 | # insert the layout in the window 177 | self.central_widget = QtWidgets.QWidget() 178 | self.central_widget.setContentsMargins(0, 0, 0, 0) 179 | self.central_widget.setLayout(self.tile_layout) 180 | 181 | if static_layout: 182 | self.setCentralWidget(self.central_widget) 183 | 184 | else: 185 | self.scroll = QtWidgets.QScrollArea() 186 | self.scroll.setWidgetResizable(True) 187 | self.scroll.setContentsMargins(0, 0, 0, 0) 188 | self.scroll.setWidget(self.central_widget) 189 | self.setCentralWidget(self.scroll) 190 | self.scroll.resizeEvent = self.__centralWidgetResize 191 | 192 | # if you are not in static layout mode, think to change the scrollArea minimum height and width if you 193 | # change tiles minimum height or width 194 | vertical_margins = self.tile_layout.contentsMargins().top() + self.tile_layout.contentsMargins().bottom() 195 | horizontal_margins = self.tile_layout.contentsMargins().left() + self.tile_layout.contentsMargins().right() 196 | self.scroll.setMinimumHeight( 197 | row_number * vertical_span + (row_number - 1) * spacing + vertical_margins + 2 198 | ) 199 | self.scroll.setMinimumWidth( 200 | column_number * horizontal_span + (column_number - 1) * spacing + horizontal_margins + 2 201 | ) 202 | 203 | def __spawnWidget(self): 204 | """spawns a little colored widget with text""" 205 | label = Label(self) 206 | label.setText(random.choice(possible_text)) 207 | label.setFont(self.font) 208 | label.setAlignment(QtCore.Qt.AlignCenter) 209 | label.setAutoFillBackground(True) 210 | label.setPalette(self.__spawnColor()) 211 | return label 212 | 213 | @staticmethod 214 | def __spawnColor(): 215 | """spawns a random color""" 216 | palette = QPalette() 217 | palette.setColor(QPalette.Background, QtGui.QColor(*random.choice(possible_colors))) 218 | return palette 219 | 220 | @staticmethod 221 | def __tileHasBeenResized(widget, from_row, from_column, row_span, column_span): 222 | print(f'{widget} has been resized and is now at the position ({from_row}, {from_column}) ' 223 | f'with a span of ({row_span}, {column_span})') 224 | 225 | @staticmethod 226 | def __tileHasBeenMoved(widget, from_layout_id, to_layout_id, from_row, from_column, to_row, to_column): 227 | print(f'{widget} has been moved from position ({from_row}, {from_column}) to ({to_row}, {to_column})') 228 | 229 | def __centralWidgetResize(self, a0): 230 | self.tile_layout.updateGlobalSize(a0) 231 | 232 | 233 | if __name__ == "__main__": 234 | app = QtWidgets.QApplication(sys.argv) 235 | window = MainWindow() 236 | window.show() 237 | app.exec() 238 | -------------------------------------------------------------------------------- /testLink.py: -------------------------------------------------------------------------------- 1 | import random 2 | import sys 3 | from PyQt5 import QtWidgets, QtGui, QtCore 4 | from PyQt5.QtGui import QPalette, QFont 5 | 6 | from QTileLayout import QTileLayout 7 | from test import Label, possible_text, possible_colors 8 | 9 | 10 | class MainWindow(QtWidgets.QMainWindow): 11 | """ 12 | creates a window and spawns some widgets in two tile layout which are linked together 13 | """ 14 | 15 | def __init__(self, *args, **kwargs): 16 | super(MainWindow, self).__init__(*args, **kwargs) 17 | 18 | self.font = QFont('Latin', 15) 19 | self.font.setBold(True) 20 | 21 | row_number = 6 22 | column_number = 4 23 | vertical_span = 100 24 | horizontal_span = 150 25 | spacing = 5 26 | 27 | # create the tile layouts 28 | self.tile_layout_1 = QTileLayout( 29 | rowNumber=row_number, 30 | columnNumber=column_number, 31 | verticalSpan=vertical_span, 32 | horizontalSpan=horizontal_span, 33 | verticalSpacing=spacing, 34 | horizontalSpacing=spacing, 35 | ) 36 | self.tile_layout_2 = QTileLayout( 37 | rowNumber=row_number, 38 | columnNumber=column_number, 39 | verticalSpan=vertical_span + 50, 40 | horizontalSpan=horizontal_span - 50, 41 | verticalSpacing=spacing, 42 | horizontalSpacing=spacing, 43 | ) 44 | self.tile_layout_3 = QTileLayout( 45 | rowNumber=row_number, 46 | columnNumber=column_number, 47 | verticalSpan=vertical_span, 48 | horizontalSpan=horizontal_span - 50, 49 | verticalSpacing=spacing, 50 | horizontalSpacing=spacing, 51 | ) 52 | 53 | # add widgets in the tile layouts 54 | for i_row in range(row_number - 2): 55 | for i_column in range(column_number): 56 | self.tile_layout_1.addWidget( 57 | widget=self.__spawnWidget(), 58 | fromRow=i_row, 59 | fromColumn=i_column, 60 | ) 61 | self.tile_layout_2.addWidget( 62 | widget=self.__spawnWidget(), 63 | fromRow=i_row, 64 | fromColumn=i_column, 65 | ) 66 | self.tile_layout_3.addWidget( 67 | widget=self.__spawnWidget(), 68 | fromRow=i_row, 69 | fromColumn=i_column, 70 | ) 71 | 72 | # link the layouts together 73 | self.tile_layout_1.linkLayout(self.tile_layout_2) 74 | self.tile_layout_1.linkLayout(self.tile_layout_3) 75 | 76 | # impossible to drop on the second layout if drag and drop is not allowed on it (to test with False) 77 | self.tile_layout_2.acceptDragAndDrop(True) 78 | 79 | # actions to do when a tile is moved 80 | self.tile_layout_1.tileMoved.connect(self.__tileHasBeenMoved) 81 | self.tile_layout_2.tileMoved.connect(self.__tileHasBeenMoved) 82 | self.tile_layout_3.tileMoved.connect(self.__tileHasBeenMoved) 83 | 84 | # insert the layouts in the window 85 | self.central_widget = QtWidgets.QWidget() 86 | self.central_layout = QtWidgets.QHBoxLayout() 87 | self.central_widget.setContentsMargins(0, 0, 0, 0) 88 | self.central_widget.setLayout(self.central_layout) 89 | self.left_widget = QtWidgets.QWidget() 90 | self.middle_widget = QtWidgets.QWidget() 91 | self.right_widget = QtWidgets.QWidget() 92 | self.central_layout.addWidget(self.left_widget) 93 | self.central_layout.addWidget(self.middle_widget) 94 | self.central_layout.addWidget(self.right_widget) 95 | self.left_widget.setLayout(self.tile_layout_1) 96 | self.middle_widget.setLayout(self.tile_layout_2) 97 | self.right_widget.setLayout(self.tile_layout_3) 98 | 99 | self.setCentralWidget(self.central_widget) 100 | 101 | def __spawnWidget(self): 102 | """spawns a little colored widget with text""" 103 | label = Label(self) 104 | label.setText(random.choice(possible_text)) 105 | label.setFont(self.font) 106 | label.setAlignment(QtCore.Qt.AlignCenter) 107 | label.setAutoFillBackground(True) 108 | label.setPalette(self.__spawnColor()) 109 | return label 110 | 111 | @staticmethod 112 | def __spawnColor(): 113 | """spawns a random color""" 114 | palette = QPalette() 115 | palette.setColor(QPalette.Background, QtGui.QColor(*random.choice(possible_colors))) 116 | return palette 117 | 118 | @staticmethod 119 | def __tileHasBeenMoved(widget, from_layout_id, to_layout_id, from_row, from_column, to_row, to_column): 120 | print(f'{widget} has been moved from position ({from_row}, {from_column}) in layout {from_layout_id} to ({to_row}, {to_column}) in layout {to_layout_id}') 121 | 122 | 123 | if __name__ == "__main__": 124 | app = QtWidgets.QApplication(sys.argv) 125 | window = MainWindow() 126 | window.show() 127 | app.exec() 128 | --------------------------------------------------------------------------------