Exercise

Enhance the Page Designer application by adding a new button, Align, which has a pop-up menu of Align Left, Align Right, Align Top, and Align Bottom. Provide a single method, setAlignment(), that takes an alignment argument. Be sure to keep an instance variable with the wrappers so they don't get garbage-collected. To perform the alignment there must be at least two items selected (because items are aligned with respect to each other).

The algorithm in the solution has two phases: First it finds the item to align every other item in relation to; for example, if doing Align Left, it finds the leftmost item. Note that this must be done in terms of the sceneBoundingRect(), not the boundingRect() (which is different if the item is rotated). Second, it works out the x or y difference to be applied to the other items and then applies it. Adding the extra button and its menu will take less than 15 lines of code, and setAlignment() can be written in less than 45 lines, so the whole thing can be done in about 60 lines.

A solution is provided in chap12/pagedesigner_ans.pyw.

• Rich Text Editing

• Printing Documents

Was this article helpful?

+1 0
Tube Traffic Ninja

Tube Traffic Ninja

Discover How You Can Quickly And Easily Dominate Google and YouTube... With Simple Cash Generating Videos. Did you know that YouTube is the second largest search website on the entire Internet? YouTube gets more daily searches than Bing and Yahoo. In fact, there is only one search engine that gets more action.

Get My Free Ebook


Post a comment