Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / graphic / GraphicSizeCheck.cxx
blobe35f31507c49762203f27d8592ef802e79428c72
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 */
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>
18 #include <ndgrf.hxx>
19 #include <IDocumentSettingAccess.hxx>
20 #include <fmtfsize.hxx>
21 #include <view.hxx>
22 #include <wrtsh.hxx>
23 #include <cmdid.h>
25 using namespace css;
27 namespace sw
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());
46 double nSizeXInch
47 = o3tl::convert(double(aFrameSize.Width()), o3tl::Length::twip, o3tl::Length::in);
48 double nSizeYInch
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();
62 namespace
64 class GraphicSizeCheckHandler : public ModelTraverseHandler
66 private:
67 sal_Int32 m_nDPI;
68 std::vector<std::unique_ptr<GraphicSizeViolation>>& m_rGraphicSizeViolationList;
70 public:
71 GraphicSizeCheckHandler(
72 sal_Int32 nDPI,
73 std::vector<std::unique_ptr<GraphicSizeViolation>>& rGraphicSizeViolationList)
74 : m_nDPI(nDPI)
75 , m_rGraphicSizeViolationList(rGraphicSizeViolationList)
79 void handleNode(SwNode* pNode) override
81 if (!pNode->IsGrfNode())
82 return;
84 auto pEntry = std::make_unique<GraphicSizeViolation>(m_nDPI, pNode->GetGrfNode());
85 if (pEntry->check())
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();
99 if (nDPI == 0)
100 return;
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()
110 OUString sText;
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()));
125 return sText;
128 void GraphicSizeCheckGUIEntry::markObject()
130 SwWrtShell* pWrtShell = m_pDocument->GetDocShell()->GetWrtShell();
131 pWrtShell->GotoFly(m_pViolation->getGraphicName(), FLYCNTTYPE_ALL, true);
134 void GraphicSizeCheckGUIEntry::runProperties()
136 markObject();
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);
145 aCheck.check();
147 auto& rCollection = getCollection();
148 for (auto& rpViolation : aCheck.getViolationList())
150 auto rGUIEntry
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: */