|
1 #include "ScaleNinePainter.h" |
|
2 #include <QtGui> |
|
3 |
|
4 namespace GVA { |
|
5 |
|
6 ScaleNinePainter::ScaleNinePainter( |
|
7 const QString &topLeftFilename, |
|
8 const QString &topMiddleFilename, |
|
9 const QString &topRightFilename, |
|
10 const QString &middleLeftFilename, |
|
11 const QString &middleMiddleFilename, |
|
12 const QString &middleRightFilename, |
|
13 const QString &bottomLeftFilename, |
|
14 const QString &bottomMiddleFilename, |
|
15 const QString &bottomRightFilename) |
|
16 : m_topLeftFilename(topLeftFilename), |
|
17 m_topMiddleFilename(topMiddleFilename), |
|
18 m_topRightFilename(topRightFilename), |
|
19 m_middleLeftFilename(middleLeftFilename), |
|
20 m_middleMiddleFilename(middleMiddleFilename), |
|
21 m_middleRightFilename(middleRightFilename), |
|
22 m_bottomLeftFilename(bottomLeftFilename), |
|
23 m_bottomMiddleFilename(bottomMiddleFilename), |
|
24 m_bottomRightFilename(bottomRightFilename), |
|
25 m_topLeftPixmap(0), |
|
26 m_topMiddlePixmap(0), |
|
27 m_topRightPixmap(0), |
|
28 m_middleLeftPixmap(0), |
|
29 m_middleMiddlePixmap(0), |
|
30 m_middleRightPixmap(0), |
|
31 m_bottomLeftPixmap(0), |
|
32 m_bottomMiddlePixmap(0), |
|
33 m_bottomRightPixmap(0), |
|
34 m_pixmapsLoaded(false) |
|
35 { |
|
36 } |
|
37 |
|
38 ScaleNinePainter::~ScaleNinePainter() { |
|
39 unloadPixmaps(); |
|
40 } |
|
41 |
|
42 void ScaleNinePainter::loadPixmaps() { |
|
43 Q_ASSERT(!m_pixmapsLoaded); |
|
44 m_topLeftPixmap = new QPixmap(m_topLeftFilename); |
|
45 m_topMiddlePixmap = new QPixmap(m_topMiddleFilename); |
|
46 m_topRightPixmap = new QPixmap(m_topRightFilename); |
|
47 m_middleLeftPixmap = new QPixmap(m_middleLeftFilename); |
|
48 m_middleMiddlePixmap = new QPixmap(m_middleMiddleFilename); |
|
49 m_middleRightPixmap = new QPixmap(m_middleRightFilename); |
|
50 m_bottomLeftPixmap = new QPixmap(m_bottomLeftFilename); |
|
51 m_bottomMiddlePixmap = new QPixmap(m_bottomMiddleFilename); |
|
52 m_bottomRightPixmap = new QPixmap(m_bottomRightFilename); |
|
53 m_pixmapsLoaded = true; |
|
54 } |
|
55 |
|
56 void ScaleNinePainter::unloadPixmaps() { |
|
57 delete m_topLeftPixmap; m_topLeftPixmap = 0; |
|
58 delete m_topMiddlePixmap; m_topMiddlePixmap = 0; |
|
59 delete m_topRightPixmap; m_topRightPixmap = 0; |
|
60 delete m_middleLeftPixmap; m_middleLeftPixmap = 0; |
|
61 delete m_middleMiddlePixmap; m_middleMiddlePixmap = 0; |
|
62 delete m_middleRightPixmap; m_middleRightPixmap = 0; |
|
63 delete m_bottomLeftPixmap; m_bottomLeftPixmap = 0; |
|
64 delete m_bottomMiddlePixmap; m_bottomMiddlePixmap = 0; |
|
65 delete m_bottomRightPixmap; m_bottomRightPixmap = 0; |
|
66 m_pixmapsLoaded = false; |
|
67 } |
|
68 |
|
69 void ScaleNinePainter::paint(QPainter* painter, const QRect &rect, QWidget* widget) { |
|
70 Q_UNUSED(widget) |
|
71 |
|
72 if(!m_pixmapsLoaded) |
|
73 loadPixmaps(); |
|
74 |
|
75 painter->save(); |
|
76 |
|
77 // Draw top left. |
|
78 if(m_topLeftPixmap->height() > rect.height()) |
|
79 painter->drawPixmap(0, 0, m_topLeftPixmap->width(), rect.height(), *m_topLeftPixmap); |
|
80 else |
|
81 painter->drawPixmap(0, 0, *m_topLeftPixmap); |
|
82 |
|
83 // Draw top middle. |
|
84 if(!m_topMiddlePixmap->isNull()) { |
|
85 painter->drawTiledPixmap(m_topLeftPixmap->width(), |
|
86 0, |
|
87 rect.width() - (m_topLeftPixmap->width() + m_topRightPixmap->width()), |
|
88 qMin(m_topMiddlePixmap->height(), rect.height()), |
|
89 *m_topMiddlePixmap); |
|
90 } |
|
91 |
|
92 // Draw top right. |
|
93 painter->drawPixmap(rect.right() - m_topRightPixmap->width() + 1, 0, *m_topRightPixmap); |
|
94 |
|
95 // Draw left border. |
|
96 if(!m_middleLeftPixmap->isNull()) { |
|
97 painter->drawTiledPixmap(0, |
|
98 m_topLeftPixmap->height(), |
|
99 m_middleMiddlePixmap->width(), |
|
100 rect.height() - (m_topLeftPixmap->height() + m_bottomLeftPixmap->height()), |
|
101 *m_middleLeftPixmap); |
|
102 } |
|
103 |
|
104 // Draw middle. |
|
105 if(!m_middleMiddlePixmap->isNull()) { |
|
106 QRect middleRect; |
|
107 middleRect.setLeft(m_middleLeftPixmap->isNull() ? m_topLeftPixmap->width() : m_topLeftPixmap->width()); |
|
108 middleRect.setRight(rect.width() - (m_middleRightPixmap->isNull() ? m_topRightPixmap->width() : m_topRightPixmap->width()) - 1); |
|
109 middleRect.setTop(m_topMiddlePixmap->isNull() ? m_topLeftPixmap->height() : m_topMiddlePixmap->height()); |
|
110 middleRect.setBottom(rect.height() - (m_bottomMiddlePixmap->isNull() ? m_bottomLeftPixmap->height() : m_bottomMiddlePixmap->height()) - 1); |
|
111 painter->drawTiledPixmap(middleRect, *m_middleMiddlePixmap); |
|
112 } |
|
113 |
|
114 // Draw right border. |
|
115 if(!m_middleRightPixmap->isNull()) { |
|
116 painter->drawTiledPixmap(rect.width() - m_middleRightPixmap->width(), |
|
117 m_topRightPixmap->height(), |
|
118 m_middleRightPixmap->width(), |
|
119 rect.height() - (m_topRightPixmap->height() + m_bottomRightPixmap->height()), |
|
120 *m_middleRightPixmap); |
|
121 } |
|
122 |
|
123 // Draw bottom row. |
|
124 if(!m_bottomLeftPixmap->isNull()) { |
|
125 painter->drawPixmap(0, |
|
126 rect.bottom() - m_bottomLeftPixmap->height() + 1, |
|
127 *m_bottomLeftPixmap); |
|
128 } |
|
129 if(!m_bottomMiddlePixmap->isNull()) { |
|
130 QRect bottomRect; |
|
131 bottomRect.setLeft(m_bottomLeftPixmap->width()); |
|
132 bottomRect.setRight(rect.width() - m_bottomRightPixmap->width() - 1); |
|
133 bottomRect.setTop(rect.bottom() - m_bottomMiddlePixmap->height() + 1); |
|
134 bottomRect.setBottom(rect.bottom()); |
|
135 painter->drawTiledPixmap(bottomRect, *m_bottomMiddlePixmap); |
|
136 } |
|
137 if(!m_bottomRightPixmap->isNull()) { |
|
138 painter->drawPixmap(rect.right() - m_bottomLeftPixmap->width() + 1, |
|
139 rect.bottom() - m_bottomRightPixmap->height() + 1, |
|
140 *m_bottomRightPixmap); |
|
141 } |
|
142 painter->restore(); |
|
143 } |
|
144 |
|
145 } // GVA namespace |