| 58 | |
| 59 | |
| 60 | class NinePatch: |
| 61 | |
| 62 | def __init__(self, fileName): |
| 63 | self.CachedImage = None # 缓存图片 |
| 64 | self.OldWidth = -1 |
| 65 | self.OldHeight = -1 |
| 66 | self.ResizeDistancesX = [] |
| 67 | self.ResizeDistancesY = [] # [(int,int)]数组 |
| 68 | self.setImage(fileName) |
| 69 | |
| 70 | def width(self): |
| 71 | return self.Image.width() |
| 72 | |
| 73 | def height(self): |
| 74 | return self.Image.height() |
| 75 | |
| 76 | def setImage(self, fileName): |
| 77 | self.Image = QImage(fileName) |
| 78 | if self.Image.isNull(): |
| 79 | return |
| 80 | |
| 81 | self.ContentArea = self.GetContentArea() |
| 82 | self.GetResizeArea() |
| 83 | if not self.ResizeDistancesX or not self.ResizeDistancesY: |
| 84 | raise ExceptionNot9Patch |
| 85 | |
| 86 | def __del__(self): |
| 87 | if hasattr(self, "CachedImage"): |
| 88 | del self.CachedImage |
| 89 | if hasattr(self, "Image"): |
| 90 | del self.Image |
| 91 | |
| 92 | def Draw(self, painter, x, y): |
| 93 | painter.drawImage(x, y, self.CachedImage) |
| 94 | |
| 95 | def SetImageSize(self, width, height): |
| 96 | resizeWidth = 0 |
| 97 | resizeHeight = 0 |
| 98 | for i in range(len(self.ResizeDistancesX)): |
| 99 | resizeWidth += self.ResizeDistancesX[i][1] |
| 100 | |
| 101 | for i in range(len(self.ResizeDistancesY)): |
| 102 | resizeHeight += self.ResizeDistancesY[i][1] |
| 103 | |
| 104 | if (width < (self.Image.width() - 2 - resizeWidth) and height < ( |
| 105 | self.Image.height() - 2 - resizeHeight)): |
| 106 | raise ExceptionIncorrectWidthAndHeight( |
| 107 | self.Image.width() - 2, self.Image.height() - 2) |
| 108 | |
| 109 | if (width < (self.Image.width() - 2 - resizeWidth)): |
| 110 | raise ExceptionIncorrectWidth( |
| 111 | self.Image.width() - 2, self.Image.height() - 2) |
| 112 | |
| 113 | if (height < (self.Image.height() - 2 - resizeHeight)): |
| 114 | raise ExceptionIncorrectHeight( |
| 115 | self.Image.width() - 2, self.Image.height() - 2) |
| 116 | |
| 117 | if (width != self.OldWidth or height != self.OldHeight): |