comparison Applications/Samples/SingleFrameEditorApplication.h @ 355:d2468dd75b3f am-2

undo redo for all tools
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Oct 2018 14:29:45 +0100
parents f806779bd40f
children 885f0a5eaa49
comparison
equal deleted inserted replaced
354:f806779bd40f 355:d2468dd75b3f
57 Corner_BottomLeft, 57 Corner_BottomLeft,
58 Corner_BottomRight 58 Corner_BottomRight
59 }; 59 };
60 60
61 61
62
62 class Bitmap : public boost::noncopyable 63 class Bitmap : public boost::noncopyable
63 { 64 {
64 private: 65 private:
65 size_t index_; 66 size_t index_;
66 bool hasSize_; 67 bool hasSize_;
471 } 472 }
472 473
473 void GetCenter(double& centerX, 474 void GetCenter(double& centerX,
474 double& centerY) const 475 double& centerY) const
475 { 476 {
476 #if 0
477 unsigned int x, y, width, height;
478 GetCrop(x, y, width, height);
479
480 centerX = static_cast<double>(width) / 2.0;
481 centerY = static_cast<double>(height) / 2.0;
482 #else
483 centerX = static_cast<double>(width_) / 2.0; 477 centerX = static_cast<double>(width_) / 2.0;
484 centerY = static_cast<double>(height_) / 2.0; 478 centerY = static_cast<double>(height_) / 2.0;
485 #endif
486
487 ApplyTransform(centerX, centerY, transform_); 479 ApplyTransform(centerX, centerY, transform_);
488 } 480 }
489 481
490 482
491 void DrawBorders(CairoContext& context, 483 void DrawBorders(CairoContext& context,
1341 stack_(stack), 1333 stack_(stack),
1342 bitmap_(bitmap) 1334 bitmap_(bitmap)
1343 { 1335 {
1344 } 1336 }
1345 1337
1346 BitmapCommandBase(BitmapStack::BitmapAccessor& accessor) : 1338 BitmapCommandBase(const BitmapStack::BitmapAccessor& accessor) :
1347 stack_(accessor.GetStack()), 1339 stack_(accessor.GetStack()),
1348 bitmap_(accessor.GetIndex()) 1340 bitmap_(accessor.GetIndex())
1349 { 1341 {
1350 } 1342 }
1351 1343
1427 LOG(INFO) << "Redo - Set angle to " << ToDegrees(sourceAngle_) << " degrees"; 1419 LOG(INFO) << "Redo - Set angle to " << ToDegrees(sourceAngle_) << " degrees";
1428 bitmap.SetAngle(targetAngle_); 1420 bitmap.SetAngle(targetAngle_);
1429 } 1421 }
1430 1422
1431 public: 1423 public:
1432 UndoRedoCommand(BitmapStack::BitmapAccessor& accessor, 1424 UndoRedoCommand(const RotateBitmapTracker& tracker) :
1433 double sourceAngle, 1425 BitmapCommandBase(tracker.accessor_),
1434 double targetAngle) : 1426 sourceAngle_(tracker.originalAngle_),
1435 BitmapCommandBase(accessor), 1427 targetAngle_(tracker.accessor_.GetBitmap().GetAngle())
1436 sourceAngle_(sourceAngle),
1437 targetAngle_(targetAngle)
1438 { 1428 {
1439 } 1429 }
1440 }; 1430 };
1441 1431
1442 1432
1480 1470
1481 virtual void MouseUp() 1471 virtual void MouseUp()
1482 { 1472 {
1483 if (accessor_.IsValid()) 1473 if (accessor_.IsValid())
1484 { 1474 {
1485 undoRedoStack_.Add(new UndoRedoCommand(accessor_, originalAngle_, 1475 undoRedoStack_.Add(new UndoRedoCommand(*this));
1486 accessor_.GetBitmap().GetAngle()));
1487 } 1476 }
1488 } 1477 }
1489 1478
1490 virtual void MouseMove(int displayX, 1479 virtual void MouseMove(int displayX,
1491 int displayY, 1480 int displayY,
1541 { 1530 {
1542 bitmap.SetPan(targetX_, targetY_); 1531 bitmap.SetPan(targetX_, targetY_);
1543 } 1532 }
1544 1533
1545 public: 1534 public:
1546 UndoRedoCommand(BitmapStack::BitmapAccessor& accessor, 1535 UndoRedoCommand(const MoveBitmapTracker& tracker) :
1547 double sourceX, 1536 BitmapCommandBase(tracker.accessor_),
1548 double sourceY, 1537 sourceX_(tracker.panX_),
1549 double targetX, 1538 sourceY_(tracker.panY_),
1550 double targetY) : 1539 targetX_(tracker.accessor_.GetBitmap().GetPanX()),
1551 BitmapCommandBase(accessor), 1540 targetY_(tracker.accessor_.GetBitmap().GetPanY())
1552 sourceX_(sourceX),
1553 sourceY_(sourceY),
1554 targetX_(targetX),
1555 targetY_(targetY)
1556 { 1541 {
1557 } 1542 }
1558 }; 1543 };
1559 1544
1560 1545
1591 1576
1592 virtual void MouseUp() 1577 virtual void MouseUp()
1593 { 1578 {
1594 if (accessor_.IsValid()) 1579 if (accessor_.IsValid())
1595 { 1580 {
1596 undoRedoStack_.Add(new UndoRedoCommand(accessor_, panX_, panY_, 1581 undoRedoStack_.Add(new UndoRedoCommand(*this));
1597 accessor_.GetBitmap().GetPanX(),
1598 accessor_.GetBitmap().GetPanY()));
1599 } 1582 }
1600 } 1583 }
1601 1584
1602 virtual void MouseMove(int displayX, 1585 virtual void MouseMove(int displayX,
1603 int displayY, 1586 int displayY,
1630 1613
1631 1614
1632 class CropBitmapTracker : public IWorldSceneMouseTracker 1615 class CropBitmapTracker : public IWorldSceneMouseTracker
1633 { 1616 {
1634 private: 1617 private:
1618 UndoRedoStack& undoRedoStack_;
1635 BitmapStack::BitmapAccessor accessor_; 1619 BitmapStack::BitmapAccessor accessor_;
1636 BitmapStack::Corner corner_; 1620 BitmapStack::Corner corner_;
1637 unsigned int cropX_; 1621 unsigned int cropX_;
1638 unsigned int cropY_; 1622 unsigned int cropY_;
1639 unsigned int cropWidth_; 1623 unsigned int cropWidth_;
1640 unsigned int cropHeight_; 1624 unsigned int cropHeight_;
1641 1625
1626 class UndoRedoCommand : public BitmapCommandBase
1627 {
1628 private:
1629 unsigned int sourceCropX_;
1630 unsigned int sourceCropY_;
1631 unsigned int sourceCropWidth_;
1632 unsigned int sourceCropHeight_;
1633 unsigned int targetCropX_;
1634 unsigned int targetCropY_;
1635 unsigned int targetCropWidth_;
1636 unsigned int targetCropHeight_;
1637
1638 protected:
1639 virtual void UndoInternal(BitmapStack::Bitmap& bitmap) const
1640 {
1641 bitmap.SetCrop(sourceCropX_, sourceCropY_, sourceCropWidth_, sourceCropHeight_);
1642 }
1643
1644 virtual void RedoInternal(BitmapStack::Bitmap& bitmap) const
1645 {
1646 bitmap.SetCrop(targetCropX_, targetCropY_, targetCropWidth_, targetCropHeight_);
1647 }
1648
1649 public:
1650 UndoRedoCommand(const CropBitmapTracker& tracker) :
1651 BitmapCommandBase(tracker.accessor_),
1652 sourceCropX_(tracker.cropX_),
1653 sourceCropY_(tracker.cropY_),
1654 sourceCropWidth_(tracker.cropWidth_),
1655 sourceCropHeight_(tracker.cropHeight_)
1656 {
1657 tracker.accessor_.GetBitmap().GetCrop(targetCropX_, targetCropY_,
1658 targetCropWidth_, targetCropHeight_);
1659 }
1660 };
1661
1662
1642 public: 1663 public:
1643 CropBitmapTracker(BitmapStack& stack, 1664 CropBitmapTracker(UndoRedoStack& undoRedoStack,
1665 BitmapStack& stack,
1644 const ViewportGeometry& view, 1666 const ViewportGeometry& view,
1645 size_t bitmap, 1667 size_t bitmap,
1646 double x, 1668 double x,
1647 double y, 1669 double y,
1648 BitmapStack::Corner corner) : 1670 BitmapStack::Corner corner) :
1671 undoRedoStack_(undoRedoStack),
1649 accessor_(stack, bitmap), 1672 accessor_(stack, bitmap),
1650 corner_(corner) 1673 corner_(corner)
1651 { 1674 {
1652 if (accessor_.IsValid()) 1675 if (accessor_.IsValid())
1653 { 1676 {
1666 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 1689 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
1667 } 1690 }
1668 1691
1669 virtual void MouseUp() 1692 virtual void MouseUp()
1670 { 1693 {
1694 if (accessor_.IsValid())
1695 {
1696 undoRedoStack_.Add(new UndoRedoCommand(*this));
1697 }
1671 } 1698 }
1672 1699
1673 virtual void MouseMove(int displayX, 1700 virtual void MouseMove(int displayX,
1674 int displayY, 1701 int displayY,
1675 double sceneX, 1702 double sceneX,
1718 1745
1719 1746
1720 class ResizeBitmapTracker : public IWorldSceneMouseTracker 1747 class ResizeBitmapTracker : public IWorldSceneMouseTracker
1721 { 1748 {
1722 private: 1749 private:
1750 UndoRedoStack& undoRedoStack_;
1723 BitmapStack::BitmapAccessor accessor_; 1751 BitmapStack::BitmapAccessor accessor_;
1724 bool roundScaling_; 1752 bool roundScaling_;
1725 double originalSpacingX_; 1753 double originalSpacingX_;
1726 double originalSpacingY_; 1754 double originalSpacingY_;
1755 double originalPanX_;
1756 double originalPanY_;
1727 BitmapStack::Corner oppositeCorner_; 1757 BitmapStack::Corner oppositeCorner_;
1728 double oppositeX_; 1758 double oppositeX_;
1729 double oppositeY_; 1759 double oppositeY_;
1730 double baseScaling_; 1760 double baseScaling_;
1731 1761
1737 double dx = x1 - x2; 1767 double dx = x1 - x2;
1738 double dy = y1 - y2; 1768 double dy = y1 - y2;
1739 return sqrt(dx * dx + dy * dy); 1769 return sqrt(dx * dx + dy * dy);
1740 } 1770 }
1741 1771
1772 class UndoRedoCommand : public BitmapCommandBase
1773 {
1774 private:
1775 double sourceSpacingX_;
1776 double sourceSpacingY_;
1777 double sourcePanX_;
1778 double sourcePanY_;
1779 double targetSpacingX_;
1780 double targetSpacingY_;
1781 double targetPanX_;
1782 double targetPanY_;
1783
1784 protected:
1785 virtual void UndoInternal(BitmapStack::Bitmap& bitmap) const
1786 {
1787 bitmap.SetPixelSpacing(sourceSpacingX_, sourceSpacingY_);
1788 bitmap.SetPan(sourcePanX_, sourcePanY_);
1789 }
1790
1791 virtual void RedoInternal(BitmapStack::Bitmap& bitmap) const
1792 {
1793 bitmap.SetPixelSpacing(targetSpacingX_, targetSpacingY_);
1794 bitmap.SetPan(targetPanX_, targetPanY_);
1795 }
1796
1797 public:
1798 UndoRedoCommand(const ResizeBitmapTracker& tracker) :
1799 BitmapCommandBase(tracker.accessor_),
1800 sourceSpacingX_(tracker.originalSpacingX_),
1801 sourceSpacingY_(tracker.originalSpacingY_),
1802 sourcePanX_(tracker.originalPanX_),
1803 sourcePanY_(tracker.originalPanY_),
1804 targetSpacingX_(tracker.accessor_.GetBitmap().GetPixelSpacingX()),
1805 targetSpacingY_(tracker.accessor_.GetBitmap().GetPixelSpacingY()),
1806 targetPanX_(tracker.accessor_.GetBitmap().GetPanX()),
1807 targetPanY_(tracker.accessor_.GetBitmap().GetPanY())
1808 {
1809 }
1810 };
1811
1812
1742 public: 1813 public:
1743 ResizeBitmapTracker(BitmapStack& stack, 1814 ResizeBitmapTracker(UndoRedoStack& undoRedoStack,
1815 BitmapStack& stack,
1744 size_t bitmap, 1816 size_t bitmap,
1745 double x, 1817 double x,
1746 double y, 1818 double y,
1747 BitmapStack::Corner corner, 1819 BitmapStack::Corner corner,
1748 bool roundScaling) : 1820 bool roundScaling) :
1821 undoRedoStack_(undoRedoStack),
1749 accessor_(stack, bitmap), 1822 accessor_(stack, bitmap),
1750 roundScaling_(roundScaling) 1823 roundScaling_(roundScaling)
1751 { 1824 {
1752 if (accessor_.IsValid() && 1825 if (accessor_.IsValid() &&
1753 accessor_.GetBitmap().IsResizeable()) 1826 accessor_.GetBitmap().IsResizeable())
1754 { 1827 {
1755 originalSpacingX_ = accessor_.GetBitmap().GetPixelSpacingX(); 1828 originalSpacingX_ = accessor_.GetBitmap().GetPixelSpacingX();
1756 originalSpacingY_ = accessor_.GetBitmap().GetPixelSpacingY(); 1829 originalSpacingY_ = accessor_.GetBitmap().GetPixelSpacingY();
1830 originalPanX_ = accessor_.GetBitmap().GetPanX();
1831 originalPanY_ = accessor_.GetBitmap().GetPanY();
1757 1832
1758 switch (corner) 1833 switch (corner)
1759 { 1834 {
1760 case BitmapStack::Corner_TopLeft: 1835 case BitmapStack::Corner_TopLeft:
1761 oppositeCorner_ = BitmapStack::Corner_BottomRight; 1836 oppositeCorner_ = BitmapStack::Corner_BottomRight;
1803 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 1878 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
1804 } 1879 }
1805 1880
1806 virtual void MouseUp() 1881 virtual void MouseUp()
1807 { 1882 {
1883 if (accessor_.IsValid())
1884 {
1885 undoRedoStack_.Add(new UndoRedoCommand(*this));
1886 }
1808 } 1887 }
1809 1888
1810 virtual void MouseMove(int displayX, 1889 virtual void MouseMove(int displayX,
1811 int displayY, 1890 int displayY,
1812 double sceneX, 1891 double sceneX,
1898 if (accessor.GetBitmap().LookupCorner(corner, x, y, view.GetZoom(), GetHandleSize())) 1977 if (accessor.GetBitmap().LookupCorner(corner, x, y, view.GetZoom(), GetHandleSize()))
1899 { 1978 {
1900 switch (tool_) 1979 switch (tool_)
1901 { 1980 {
1902 case Tool_Crop: 1981 case Tool_Crop:
1903 return new CropBitmapTracker(stack_, view, selected, x, y, corner); 1982 return new CropBitmapTracker(undoRedoStack_, stack_, view, selected, x, y, corner);
1904 1983
1905 case Tool_Resize: 1984 case Tool_Resize:
1906 return new ResizeBitmapTracker(stack_, selected, x, y, corner, 1985 return new ResizeBitmapTracker(undoRedoStack_, stack_, selected, x, y, corner,
1907 (modifiers & KeyboardModifiers_Shift)); 1986 (modifiers & KeyboardModifiers_Shift));
1908 1987
1909 default: 1988 default:
1910 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); 1989 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
1911 } 1990 }