comparison view/View.cpp @ 226:2ccd02015530

* Add basics of an Export Image File function
author Chris Cannam
date Mon, 12 Mar 2007 15:36:31 +0000
parents 9465b5375235
children 6dab3ac2fe73
comparison
equal deleted inserted replaced
225:6f46179086c0 226:2ccd02015530
24 24
25 #include <QPainter> 25 #include <QPainter>
26 #include <QPaintEvent> 26 #include <QPaintEvent>
27 #include <QRect> 27 #include <QRect>
28 #include <QApplication> 28 #include <QApplication>
29 #include <QProgressDialog>
29 30
30 #include <iostream> 31 #include <iostream>
31 #include <cassert> 32 #include <cassert>
32 #include <math.h> 33 #include <math.h>
33 34
1463 } 1464 }
1464 1465
1465 QFrame::paintEvent(e); 1466 QFrame::paintEvent(e);
1466 } 1467 }
1467 1468
1469 bool
1470 View::render(QPainter &paint, QRect rect)
1471 {
1472 size_t f0 = getModelsStartFrame();
1473 size_t f1 = getModelsEndFrame();
1474
1475 size_t x0 = f0 / m_zoomLevel;
1476 size_t x1 = f1 / m_zoomLevel;
1477
1478 size_t w = x1 - x0;
1479
1480 size_t origCentreFrame = m_centreFrame;
1481
1482 bool someLayersIncomplete = false;
1483
1484 for (LayerList::iterator i = m_layers.begin();
1485 i != m_layers.end(); ++i) {
1486
1487 int c = (*i)->getCompletion(this);
1488 if (c < 100) {
1489 someLayersIncomplete = true;
1490 break;
1491 }
1492 }
1493
1494 if (someLayersIncomplete) {
1495
1496 QProgressDialog progress(tr("Waiting for layers to be ready..."),
1497 tr("Cancel"), 0, 100, this);
1498
1499 int layerCompletion = 0;
1500
1501 while (layerCompletion < 100) {
1502
1503 for (LayerList::iterator i = m_layers.begin();
1504 i != m_layers.end(); ++i) {
1505
1506 int c = (*i)->getCompletion(this);
1507 if (i == m_layers.begin() || c < layerCompletion) {
1508 layerCompletion = c;
1509 }
1510 }
1511
1512 if (layerCompletion >= 100) break;
1513
1514 progress.setValue(layerCompletion);
1515 qApp->processEvents();
1516 if (progress.wasCanceled()) {
1517 update();
1518 return false;
1519 }
1520
1521 usleep(50000);
1522 }
1523 }
1524
1525 QProgressDialog progress(tr("Rendering image..."),
1526 tr("Cancel"), 0, w / width(), this);
1527
1528 for (size_t x = 0; x < w; x += width()) {
1529
1530 progress.setValue(x / width());
1531 qApp->processEvents();
1532 if (progress.wasCanceled()) {
1533 m_centreFrame = origCentreFrame;
1534 update();
1535 return false;
1536 }
1537
1538 m_centreFrame = (x + width()/2) * m_zoomLevel;
1539
1540 QRect chunk(0, 0, width(), height());
1541
1542 if (hasLightBackground()) {
1543 paint.setPen(Qt::white);
1544 paint.setBrush(Qt::white);
1545 } else {
1546 paint.setPen(Qt::black);
1547 paint.setBrush(Qt::black);
1548 }
1549
1550 paint.drawRect(QRect(rect.x() + x, rect.y(), width(), height()));
1551
1552 paint.setPen(Qt::black);
1553 paint.setBrush(Qt::NoBrush);
1554
1555 for (LayerList::iterator i = m_layers.begin();
1556 i != m_layers.end(); ++i) {
1557
1558 paint.setRenderHint(QPainter::Antialiasing, false);
1559
1560 paint.save();
1561 paint.translate(rect.x() + x, rect.y());
1562
1563 // std::cerr << "Centre frame now: " << m_centreFrame << " drawing to " << chunk.x() << ", " << chunk.width() << std::endl;
1564
1565 (*i)->paint(this, paint, chunk);
1566
1567 paint.restore();
1568 }
1569 }
1570
1571 m_centreFrame = origCentreFrame;
1572 update();
1573 return true;
1574 }
1575
1576 QImage *
1577 View::toNewImage()
1578 {
1579 size_t f0 = getModelsStartFrame();
1580 size_t f1 = getModelsEndFrame();
1581
1582 size_t x0 = f0 / getZoomLevel();
1583 size_t x1 = f1 / getZoomLevel();
1584
1585 QImage *image = new QImage(x1 - x0, height(), QImage::Format_RGB32);
1586
1587 QPainter *paint = new QPainter(image);
1588 if (!render(*paint, image->rect())) {
1589 delete paint;
1590 delete image;
1591 return 0;
1592 } else {
1593 delete paint;
1594 return image;
1595 }
1596 }
1597
1468 void 1598 void
1469 View::drawSelections(QPainter &paint) 1599 View::drawSelections(QPainter &paint)
1470 { 1600 {
1471 if (!hasTopLayerTimeXAxis()) return; 1601 if (!hasTopLayerTimeXAxis()) return;
1472 1602