comparison widgets/Panner.cpp @ 173:9c40dc10c88c

* Rename the existing Panner to Overview (big duh!) * Fixes to the new Panner.
author Chris Cannam
date Thu, 19 Oct 2006 09:57:27 +0000
parents d0b95a8cac96
children 96b8a790730a
comparison
equal deleted inserted replaced
172:d0b95a8cac96 173:9c40dc10c88c
25 Panner::Panner(QWidget *parent) : 25 Panner::Panner(QWidget *parent) :
26 QWidget(parent), 26 QWidget(parent),
27 m_rectX(0), 27 m_rectX(0),
28 m_rectY(0), 28 m_rectY(0),
29 m_rectWidth(1), 29 m_rectWidth(1),
30 m_rectHeight(1) 30 m_rectHeight(1),
31 m_defaultCentreX(0),
32 m_defaultCentreY(0),
33 m_defaultsSet(false),
34 m_clicked(false)
31 { 35 {
32 } 36 }
33 37
34 Panner::~Panner() 38 Panner::~Panner()
35 { 39 {
36 } 40 }
37 41
38 void 42 void
39 Panner::mousePressEvent(QMouseEvent *e) 43 Panner::mousePressEvent(QMouseEvent *e)
40 { 44 {
45 if (e->button() == Qt::LeftButton) {
46 m_clicked = true;
47 m_clickPos = e->pos();
48 m_dragStartX = m_rectX;
49 m_dragStartY = m_rectY;
50 } else if (e->button() == Qt::MidButton) {
51 resetToDefault();
52 }
41 } 53 }
42 54
43 void 55 void
44 Panner::mouseDoubleClickEvent(QMouseEvent *e) 56 Panner::mouseDoubleClickEvent(QMouseEvent *e)
45 { 57 {
58 resetToDefault();
46 } 59 }
47 60
48 void 61 void
49 Panner::mouseMoveEvent(QMouseEvent *e) 62 Panner::mouseMoveEvent(QMouseEvent *e)
50 { 63 {
64 if (!m_clicked) return;
65
66 float dx = float(e->pos().x() - m_clickPos.x()) / float(width());
67 float dy = float(e->pos().y() - m_clickPos.y()) / float(height());
68
69 m_rectX = m_dragStartX + dx;
70 m_rectY = m_dragStartY + dy;
71
72 normalise();
73 emitAndUpdate();
51 } 74 }
52 75
53 void 76 void
54 Panner::mouseReleaseEvent(QMouseEvent *e) 77 Panner::mouseReleaseEvent(QMouseEvent *e)
55 { 78 {
79 if (!m_clicked) return;
80
81 mouseMoveEvent(e);
82 m_clicked = false;
56 } 83 }
57 84
58 void 85 void
59 Panner::wheelEvent(QWheelEvent *e) 86 Panner::wheelEvent(QWheelEvent *e)
60 { 87 {
88 if (e->delta() > 0) {
89 m_rectY += 0.1;
90 } else {
91 m_rectY -= 0.1;
92 }
93
94 normalise();
95 emitAndUpdate();
61 } 96 }
62 97
63 void 98 void
64 Panner::paintEvent(QPaintEvent *e) 99 Panner::paintEvent(QPaintEvent *e)
65 { 100 {
66 QPainter paint(this); 101 QPainter paint(this);
67 paint.fillRect(rect(), palette().background().color());
68 paint.setRenderHint(QPainter::Antialiasing, false); 102 paint.setRenderHint(QPainter::Antialiasing, false);
103
104 QColor bg(palette().background().color());
105 bg.setAlpha(80);
106
69 paint.setPen(palette().dark().color()); 107 paint.setPen(palette().dark().color());
70 paint.setBrush(palette().highlight().color()); 108 paint.setBrush(bg);
71 paint.drawRect(QRectF(width() * m_rectX, height() - height() * m_rectY, 109 paint.drawRect(0, 0, width(), height());
72 width() * m_rectWidth, height() * m_rectHeight)); 110
111 QColor hl(palette().highlight().color());
112 hl.setAlpha(80);
113
114 paint.setBrush(hl);
115
116 paint.drawRect(lrintf(width() * m_rectX),
117 lrintf(height() * m_rectY),
118 lrintf(width() * m_rectWidth),
119 lrintf(height() * m_rectHeight));
73 } 120 }
74 121
75 void 122 void
76 Panner::normalise() 123 Panner::normalise()
77 { 124 {
79 if (m_rectHeight > 1.0) m_rectHeight = 1.0; 126 if (m_rectHeight > 1.0) m_rectHeight = 1.0;
80 if (m_rectX + m_rectWidth > 1.0) m_rectX = 1.0 - m_rectWidth; 127 if (m_rectX + m_rectWidth > 1.0) m_rectX = 1.0 - m_rectWidth;
81 if (m_rectX < 0) m_rectX = 0; 128 if (m_rectX < 0) m_rectX = 0;
82 if (m_rectY + m_rectHeight > 1.0) m_rectY = 1.0 - m_rectHeight; 129 if (m_rectY + m_rectHeight > 1.0) m_rectY = 1.0 - m_rectHeight;
83 if (m_rectY < 0) m_rectY = 0; 130 if (m_rectY < 0) m_rectY = 0;
131
132 if (!m_defaultsSet) {
133 m_defaultCentreX = centreX();
134 m_defaultCentreY = centreY();
135 m_defaultsSet = true;
136 }
84 } 137 }
85 138
86 void 139 void
87 Panner::emitAndUpdate() 140 Panner::emitAndUpdate()
88 { 141 {
89 emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight); 142 emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight);
90 emit rectCentreMoved(m_rectX + (m_rectWidth/2), m_rectY + (m_rectHeight/2)); 143 emit rectCentreMoved(centreX(), centreY());
91 update(); 144 update();
92 } 145 }
93 146
94 void 147 void
95 Panner::setRectExtents(float x0, float y0, float width, float height) 148 Panner::setRectExtents(float x0, float y0, float width, float height)
98 m_rectY == y0 && 151 m_rectY == y0 &&
99 m_rectWidth == width && 152 m_rectWidth == width &&
100 m_rectHeight == height) { 153 m_rectHeight == height) {
101 return; 154 return;
102 } 155 }
156
103 m_rectX = x0; 157 m_rectX = x0;
104 m_rectY = y0; 158 m_rectY = y0;
105 m_rectWidth = width; 159 m_rectWidth = width;
106 m_rectHeight = height; 160 m_rectHeight = height;
161
107 normalise(); 162 normalise();
108 emitAndUpdate(); 163 emitAndUpdate();
109 } 164 }
110 165
111 void 166 void
137 } 192 }
138 193
139 void 194 void
140 Panner::setRectCentreY(float y) 195 Panner::setRectCentreY(float y)
141 { 196 {
142 float y0 = y - m_rectWidth/2; 197 float y0 = y - m_rectHeight/2;
143 if (y0 == m_rectY) return; 198 if (y0 == m_rectY) return;
144 m_rectY = y0; 199 m_rectY = y0;
145 normalise(); 200 normalise();
146 emitAndUpdate(); 201 emitAndUpdate();
147 } 202 }
150 Panner::sizeHint() const 205 Panner::sizeHint() const
151 { 206 {
152 return QSize(30, 30); 207 return QSize(30, 30);
153 } 208 }
154 209
155 210 void
156 211 Panner::setDefaultRectCentre(float cx, float cy)
212 {
213 m_defaultCentreX = cx;
214 m_defaultCentreY = cy;
215 m_defaultsSet = true;
216 }
217
218 void
219 Panner::resetToDefault()
220 {
221 float x0 = m_defaultCentreX - m_rectWidth/2;
222 float y0 = m_defaultCentreY - m_rectHeight/2;
223 if (x0 == m_rectX && y0 == m_rectY) return;
224 m_rectX = x0;
225 m_rectY = y0;
226 normalise();
227 emitAndUpdate();
228 }
229
230