1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 #include <GraphicSizeCheck.hxx>
12 #include <svx/strings.hrc>
13 #include <svx/svdobj.hxx>
14 #include <sfx2/viewfrm.hxx>
15 #include <sfx2/dispatch.hxx>
17 #include <ModelTraverser.hxx>
19 #include <IDocumentSettingAccess.hxx>
20 #include <fmtfsize.hxx>
29 GraphicSizeViolation::GraphicSizeViolation(sal_Int32 nDPI
, const SwGrfNode
* pGraphicNode
)
30 : m_pGraphicNode(pGraphicNode
)
32 constexpr double fLowPercentage
= 110;
33 constexpr double fHighPercentage
= 50;
35 m_nLowDPILimit
= sal_Int32(100.0 / fLowPercentage
* nDPI
);
36 m_nHighDPILimit
= sal_Int32(100.0 / fHighPercentage
* nDPI
);
39 bool GraphicSizeViolation::check()
41 auto pFrameFormat
= m_pGraphicNode
->GetFlyFormat();
42 Graphic aGraphic
= m_pGraphicNode
->GetGraphic();
43 Size aSizePixel
= aGraphic
.GetSizePixel();
44 Size
aFrameSize(pFrameFormat
->GetFrameSize().GetSize());
47 = o3tl::convert(double(aFrameSize
.Width()), o3tl::Length::twip
, o3tl::Length::in
);
49 = o3tl::convert(double(aFrameSize
.Height()), o3tl::Length::twip
, o3tl::Length::in
);
51 m_nDPIX
= sal_Int32(aSizePixel
.Width() / nSizeXInch
);
52 m_nDPIY
= sal_Int32(aSizePixel
.Height() / nSizeYInch
);
54 return isDPITooLow() || isDPITooHigh();
57 const OUString
& GraphicSizeViolation::getGraphicName()
59 return m_pGraphicNode
->GetFlyFormat()->GetName();
64 class GraphicSizeCheckHandler
: public ModelTraverseHandler
68 std::vector
<std::unique_ptr
<GraphicSizeViolation
>>& m_rGraphicSizeViolationList
;
71 GraphicSizeCheckHandler(
73 std::vector
<std::unique_ptr
<GraphicSizeViolation
>>& rGraphicSizeViolationList
)
75 , m_rGraphicSizeViolationList(rGraphicSizeViolationList
)
79 void handleNode(SwNode
* pNode
) override
81 if (!pNode
->IsGrfNode())
84 auto pEntry
= std::make_unique
<GraphicSizeViolation
>(m_nDPI
, pNode
->GetGrfNode());
87 m_rGraphicSizeViolationList
.push_back(std::move(pEntry
));
91 void handleSdrObject(SdrObject
* /*pObject*/) override
{}
94 } // end anonymous namespace
96 void GraphicSizeCheck::check()
98 sal_Int32 nDPI
= m_pDocument
->getIDocumentSettingAccess().getImagePreferredDPI();
102 auto pHandler
= std::make_shared
<GraphicSizeCheckHandler
>(nDPI
, m_aGraphicSizeViolationList
);
103 ModelTraverser
aModelTraverser(m_pDocument
);
104 aModelTraverser
.addNodeHandler(pHandler
);
105 aModelTraverser
.traverse();
108 OUString
GraphicSizeCheckGUIEntry::getText()
112 if (m_pViolation
->isDPITooLow())
114 sText
= SwResId(STR_WARNING_GRAPHIC_PIXEL_COUNT_LOW
);
116 else if (m_pViolation
->isDPITooHigh())
118 sText
= SwResId(STR_WARNING_GRAPHIC_PIXEL_COUNT_HIGH
);
121 sText
= sText
.replaceAll("%NAME%", m_pViolation
->getGraphicName());
122 sText
= sText
.replaceAll("%DPIX%", OUString::number(m_pViolation
->getDPIX()));
123 sText
= sText
.replaceAll("%DPIY%", OUString::number(m_pViolation
->getDPIY()));
128 void GraphicSizeCheckGUIEntry::markObject()
130 SwWrtShell
* pWrtShell
= m_pDocument
->GetDocShell()->GetWrtShell();
131 pWrtShell
->GotoFly(m_pViolation
->getGraphicName(), FLYCNTTYPE_ALL
, true);
134 void GraphicSizeCheckGUIEntry::runProperties()
137 SwWrtShell
* pWrtShell
= m_pDocument
->GetDocShell()->GetWrtShell();
138 pWrtShell
->GetView().GetViewFrame().GetDispatcher()->Execute(FN_FORMAT_GRAFIC_DLG
,
139 SfxCallMode::SYNCHRON
);
142 GraphicSizeCheckGUIResult::GraphicSizeCheckGUIResult(SwDoc
* pDocument
)
144 GraphicSizeCheck
aCheck(pDocument
);
147 auto& rCollection
= getCollection();
148 for (auto& rpViolation
: aCheck
.getViolationList())
151 = std::make_unique
<GraphicSizeCheckGUIEntry
>(pDocument
, std::move(rpViolation
));
152 rCollection
.push_back(std::move(rGUIEntry
));
156 OUString
GraphicSizeCheckGUIResult::getTitle()
158 return SwResId(STR_GRAPHIC_SIZE_CHECK_DIALOG_TITLE
);
161 } // end sw namespace
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */