comparison view/View.cpp @ 227:6dab3ac2fe73

* Add scale to image
author Chris Cannam
date Mon, 12 Mar 2007 16:02:56 +0000
parents 2ccd02015530
children 387f2f6fc333
comparison
equal deleted inserted replaced
226:2ccd02015530 227:6dab3ac2fe73
1464 } 1464 }
1465 1465
1466 QFrame::paintEvent(e); 1466 QFrame::paintEvent(e);
1467 } 1467 }
1468 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
1598 void 1469 void
1599 View::drawSelections(QPainter &paint) 1470 View::drawSelections(QPainter &paint)
1600 { 1471 {
1601 if (!hasTopLayerTimeXAxis()) return; 1472 if (!hasTopLayerTimeXAxis()) return;
1602 1473
1738 } 1609 }
1739 1610
1740 paint.restore(); 1611 paint.restore();
1741 } 1612 }
1742 1613
1614 bool
1615 View::render(QPainter &paint, QRect rect)
1616 {
1617 size_t f0 = getModelsStartFrame();
1618 size_t f1 = getModelsEndFrame();
1619
1620 size_t x0 = f0 / m_zoomLevel;
1621 size_t x1 = f1 / m_zoomLevel;
1622
1623 size_t w = x1 - x0;
1624
1625 size_t origCentreFrame = m_centreFrame;
1626
1627 bool someLayersIncomplete = false;
1628
1629 for (LayerList::iterator i = m_layers.begin();
1630 i != m_layers.end(); ++i) {
1631
1632 int c = (*i)->getCompletion(this);
1633 if (c < 100) {
1634 someLayersIncomplete = true;
1635 break;
1636 }
1637 }
1638
1639 if (someLayersIncomplete) {
1640
1641 QProgressDialog progress(tr("Waiting for layers to be ready..."),
1642 tr("Cancel"), 0, 100, this);
1643
1644 int layerCompletion = 0;
1645
1646 while (layerCompletion < 100) {
1647
1648 for (LayerList::iterator i = m_layers.begin();
1649 i != m_layers.end(); ++i) {
1650
1651 int c = (*i)->getCompletion(this);
1652 if (i == m_layers.begin() || c < layerCompletion) {
1653 layerCompletion = c;
1654 }
1655 }
1656
1657 if (layerCompletion >= 100) break;
1658
1659 progress.setValue(layerCompletion);
1660 qApp->processEvents();
1661 if (progress.wasCanceled()) {
1662 update();
1663 return false;
1664 }
1665
1666 usleep(50000);
1667 }
1668 }
1669
1670 QProgressDialog progress(tr("Rendering image..."),
1671 tr("Cancel"), 0, w / width(), this);
1672
1673 for (size_t x = 0; x < w; x += width()) {
1674
1675 progress.setValue(x / width());
1676 qApp->processEvents();
1677 if (progress.wasCanceled()) {
1678 m_centreFrame = origCentreFrame;
1679 update();
1680 return false;
1681 }
1682
1683 m_centreFrame = (x + width()/2) * m_zoomLevel;
1684
1685 QRect chunk(0, 0, width(), height());
1686
1687 if (hasLightBackground()) {
1688 paint.setPen(Qt::white);
1689 paint.setBrush(Qt::white);
1690 } else {
1691 paint.setPen(Qt::black);
1692 paint.setBrush(Qt::black);
1693 }
1694
1695 paint.drawRect(QRect(rect.x() + x, rect.y(), width(), height()));
1696
1697 paint.setPen(Qt::black);
1698 paint.setBrush(Qt::NoBrush);
1699
1700 for (LayerList::iterator i = m_layers.begin();
1701 i != m_layers.end(); ++i) {
1702
1703 paint.setRenderHint(QPainter::Antialiasing, false);
1704
1705 paint.save();
1706 paint.translate(rect.x() + x, rect.y());
1707
1708 // std::cerr << "Centre frame now: " << m_centreFrame << " drawing to " << chunk.x() << ", " << chunk.width() << std::endl;
1709
1710 (*i)->paint(this, paint, chunk);
1711
1712 paint.restore();
1713 }
1714 }
1715
1716 m_centreFrame = origCentreFrame;
1717 update();
1718 return true;
1719 }
1720
1721 QImage *
1722 View::toNewImage()
1723 {
1724 size_t f0 = getModelsStartFrame();
1725 size_t f1 = getModelsEndFrame();
1726
1727 size_t x0 = f0 / getZoomLevel();
1728 size_t x1 = f1 / getZoomLevel();
1729
1730 QImage *image = new QImage(x1 - x0, height(), QImage::Format_RGB32);
1731
1732 QPainter *paint = new QPainter(image);
1733 if (!render(*paint, image->rect())) {
1734 delete paint;
1735 delete image;
1736 return 0;
1737 } else {
1738 delete paint;
1739 return image;
1740 }
1741 }
1742
1743 QString 1743 QString
1744 View::toXmlString(QString indent, QString extraAttributes) const 1744 View::toXmlString(QString indent, QString extraAttributes) const
1745 { 1745 {
1746 QString s; 1746 QString s;
1747 1747