comparison widgets/LevelPanWidget.cpp @ 929:20698aa6a517 tonioni

Introduce level/pan toolbar buttons
author Chris Cannam
date Wed, 25 Mar 2015 10:33:19 +0000
parents 271e729c818b
children 86df7de08e03
comparison
equal deleted inserted replaced
928:3fb91da7d98d 929:20698aa6a517
38 { 38 {
39 } 39 }
40 40
41 LevelPanWidget::~LevelPanWidget() 41 LevelPanWidget::~LevelPanWidget()
42 { 42 {
43 }
44
45 QSize
46 LevelPanWidget::sizeHint() const
47 {
48 static double ratio = 0.0;
49 if (ratio == 0.0) {
50 double baseEm;
51 #ifdef Q_OS_MAC
52 baseEm = 17.0;
53 #else
54 baseEm = 15.0;
55 #endif
56 double em = QFontMetrics(QFont()).height();
57 ratio = em / baseEm;
58 }
59
60 int pixels = 40;
61 int scaled = int(pixels * ratio + 0.5);
62 if (pixels != 0 && scaled == 0) scaled = 1;
63 return QSize(scaled, scaled);
43 } 64 }
44 65
45 void 66 void
46 LevelPanWidget::setLevel(float flevel) 67 LevelPanWidget::setLevel(float flevel)
47 { 68 {
112 LevelPanWidget::mouseMoveEvent(QMouseEvent *e) 133 LevelPanWidget::mouseMoveEvent(QMouseEvent *e)
113 { 134 {
114 if (!m_editable) return; 135 if (!m_editable) return;
115 136
116 int level, pan; 137 int level, pan;
117 toCell(e->pos(), level, pan); 138 toCell(rect(), e->pos(), level, pan);
118 if (level == m_level && pan == m_pan) { 139 if (level == m_level && pan == m_pan) {
119 return; 140 return;
120 } 141 }
121 if (level != m_level) { 142 if (level != m_level) {
122 m_level = level; 143 m_level = level;
168 } 189 }
169 } 190 }
170 } 191 }
171 192
172 void 193 void
173 LevelPanWidget::toCell(QPointF loc, int &level, int &pan) const 194 LevelPanWidget::toCell(QRectF rect, QPointF loc, int &level, int &pan) const
174 { 195 {
175 double w = width(), h = height(); 196 double w = rect.width(), h = rect.height();
197
176 int npan = maxPan * 2 + 1; 198 int npan = maxPan * 2 + 1;
177 int nlevel = maxLevel + 1; 199 int nlevel = maxLevel + 1;
200
178 double wcell = w / npan, hcell = h / nlevel; 201 double wcell = w / npan, hcell = h / nlevel;
202
179 level = int((h - loc.y()) / hcell); 203 level = int((h - loc.y()) / hcell);
180 if (level < 0) level = 0; 204 if (level < 0) level = 0;
181 if (level > maxLevel) level = maxLevel; 205 if (level > maxLevel) level = maxLevel;
206
182 pan = int(loc.x() / wcell) - maxPan; 207 pan = int(loc.x() / wcell) - maxPan;
183 if (pan < -maxPan) pan = -maxPan; 208 if (pan < -maxPan) pan = -maxPan;
184 if (pan > maxPan) pan = maxPan; 209 if (pan > maxPan) pan = maxPan;
185 } 210 }
186 211
187 QSizeF 212 QSizeF
188 LevelPanWidget::cellSize() const 213 LevelPanWidget::cellSize(QRectF rect) const
189 { 214 {
190 double w = width(), h = height(); 215 double w = rect.width(), h = rect.height();
191 int npan = maxPan * 2 + 1; 216 int npan = maxPan * 2 + 1;
192 int nlevel = maxLevel + 1; 217 int nlevel = maxLevel + 1;
193 double wcell = w / npan, hcell = h / nlevel; 218 double wcell = w / npan, hcell = h / nlevel;
194 return QSizeF(wcell, hcell); 219 return QSizeF(wcell, hcell);
195 } 220 }
196 221
197 QPointF 222 QPointF
198 LevelPanWidget::cellCentre(int level, int pan) const 223 LevelPanWidget::cellCentre(QRectF rect, int level, int pan) const
199 { 224 {
200 QSizeF cs = cellSize(); 225 QSizeF cs = cellSize(rect);
201 return QPointF(cs.width() * (pan + maxPan) + cs.width() / 2., 226 return QPointF(cs.width() * (pan + maxPan) + cs.width() / 2.,
202 height() - cs.height() * (level + 1) + cs.height() / 2.); 227 rect.height() - cs.height() * (level + 1) + cs.height() / 2.);
203 } 228 }
204 229
205 QSizeF 230 QSizeF
206 LevelPanWidget::cellLightSize() const 231 LevelPanWidget::cellLightSize(QRectF rect) const
207 { 232 {
208 double extent = 3. / 4.; 233 double extent = 3. / 4.;
209 QSizeF cs = cellSize(); 234 QSizeF cs = cellSize(rect);
210 double m = std::min(cs.width(), cs.height()); 235 double m = std::min(cs.width(), cs.height());
211 return QSizeF(m * extent, m * extent); 236 return QSizeF(m * extent, m * extent);
212 } 237 }
213 238
214 QRectF 239 QRectF
215 LevelPanWidget::cellLightRect(int level, int pan) const 240 LevelPanWidget::cellLightRect(QRectF rect, int level, int pan) const
216 { 241 {
217 QSizeF cls = cellLightSize(); 242 QSizeF cls = cellLightSize(rect);
218 QPointF cc = cellCentre(level, pan); 243 QPointF cc = cellCentre(rect, level, pan);
219 return QRectF(cc.x() - cls.width() / 2., 244 return QRectF(cc.x() - cls.width() / 2.,
220 cc.y() - cls.height() / 2., 245 cc.y() - cls.height() / 2.,
221 cls.width(), 246 cls.width(),
222 cls.height()); 247 cls.height());
223 } 248 }
224 249
225 double 250 double
226 LevelPanWidget::thinLineWidth() const 251 LevelPanWidget::thinLineWidth(QRectF rect) const
227 { 252 {
228 double tw = ceil(width() / (maxPan * 2. * 10.)); 253 double tw = ceil(rect.width() / (maxPan * 2. * 10.));
229 double th = ceil(height() / (maxLevel * 10.)); 254 double th = ceil(rect.height() / (maxLevel * 10.));
230 return std::min(th, tw); 255 return std::min(th, tw);
231 } 256 }
232 257
233 void 258 void
234 LevelPanWidget::paintEvent(QPaintEvent *) 259 LevelPanWidget::renderTo(QPaintDevice *dev, QRectF rect, bool asIfEditable) const
235 { 260 {
236 QPainter paint(this); 261 QPainter paint(dev);
237 ColourMapper mapper(ColourMapper::Sunset, 0, maxLevel); 262 ColourMapper mapper(ColourMapper::Sunset, 0, maxLevel);
238 263
239 paint.setRenderHint(QPainter::Antialiasing, true); 264 paint.setRenderHint(QPainter::Antialiasing, true);
240 265
241 QPen pen; 266 QPen pen;
242 267
243 double thin = thinLineWidth(); 268 double thin = thinLineWidth(rect);
244 269
245 pen.setColor(QColor(127, 127, 127, 127)); 270 pen.setColor(QColor(127, 127, 127, 127));
246 pen.setWidthF(cellLightSize().width() + thin); 271 pen.setWidthF(cellLightSize(rect).width() + thin);
247 pen.setCapStyle(Qt::RoundCap); 272 pen.setCapStyle(Qt::RoundCap);
248 paint.setPen(pen); 273 paint.setPen(pen);
249 274
250 for (int pan = -maxPan; pan <= maxPan; ++pan) { 275 for (int pan = -maxPan; pan <= maxPan; ++pan) {
251 paint.drawLine(cellCentre(0, pan), cellCentre(maxLevel, pan)); 276 paint.drawLine(cellCentre(rect, 0, pan), cellCentre(rect, maxLevel, pan));
252 } 277 }
253 278
254 if (isEnabled()) { 279 if (isEnabled()) {
255 pen.setColor(Qt::black); 280 pen.setColor(Qt::black);
256 } else { 281 } else {
257 pen.setColor(Qt::darkGray); 282 pen.setColor(Qt::darkGray);
258 } 283 }
284
285 if (!asIfEditable && m_level == 0) {
286 pen.setWidthF(thin * 2);
287 pen.setCapStyle(Qt::RoundCap);
288 paint.setPen(pen);
289 paint.drawLine(rect.topLeft(), rect.bottomRight());
290 paint.drawLine(rect.bottomLeft(), rect.topRight());
291 return;
292 }
259 293
260 pen.setWidthF(thin); 294 pen.setWidthF(thin);
261 pen.setCapStyle(Qt::FlatCap); 295 pen.setCapStyle(Qt::FlatCap);
262 paint.setPen(pen); 296 paint.setPen(pen);
263 297
264 for (int level = 0; level <= m_level; ++level) { 298 for (int level = 0; level <= m_level; ++level) {
265 if (isEnabled()) { 299 if (isEnabled()) {
266 paint.setBrush(mapper.map(level)); 300 paint.setBrush(mapper.map(level));
267 } 301 }
268 QRectF clr = cellLightRect(level, m_pan); 302 QRectF clr = cellLightRect(rect, level, m_pan);
269 if (m_level == 0) { 303 if (m_level == 0) {
270 paint.drawLine(clr.topLeft(), clr.bottomRight()); 304 paint.drawLine(clr.topLeft(), clr.bottomRight());
271 paint.drawLine(clr.bottomLeft(), clr.topRight()); 305 paint.drawLine(clr.bottomLeft(), clr.topRight());
272 } else { 306 } else {
273 paint.drawEllipse(clr); 307 paint.drawEllipse(clr);
274 } 308 }
275 } 309 }
276 } 310 }
277 311
278 312 void
313 LevelPanWidget::paintEvent(QPaintEvent *)
314 {
315 renderTo(this, rect(), m_editable);
316 }
317
318
319