LibreOffice Module sd (master) 1
GraphicSizeCheck.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 */
10
11#include <memory>
13#include <svx/strings.hrc>
14#include <svx/svdobj.hxx>
15#include <svx/svdpage.hxx>
16#include <svx/svxids.hrc>
17#include <sfx2/dispatch.hxx>
18
19#include <sdresid.hxx>
20#include <DrawDocShell.hxx>
21#include <ViewShell.hxx>
22
23namespace sd
24{
25namespace
26{
31class ModelTraverseHandler
32{
33public:
34 virtual ~ModelTraverseHandler() {}
35
36 virtual void handleSdrObject(SdrObject* pObject) = 0;
37};
38
43class ModelTraverser
44{
45private:
46 std::vector<std::shared_ptr<ModelTraverseHandler>> m_pNodeHandler;
47 SdDrawDocument* m_pDocument;
48
49public:
50 ModelTraverser(SdDrawDocument* pDocument)
51 : m_pDocument(pDocument)
52 {
53 }
54
55 void traverse()
56 {
57 if (!m_pDocument)
58 return;
59
60 for (sal_uInt16 nPage = 0; nPage < m_pDocument->GetPageCount(); ++nPage)
61 {
62 SdrPage* pPage = m_pDocument->GetPage(nPage);
63 if (pPage)
64 {
65 for (size_t nObject = 0; nObject < pPage->GetObjCount(); ++nObject)
66 {
67 SdrObject* pObject = pPage->GetObj(nObject);
68 if (pObject)
69 {
70 for (auto& pNodeHandler : m_pNodeHandler)
71 {
72 pNodeHandler->handleSdrObject(pObject);
73 }
74 }
75 }
76 }
77 }
78 }
79
80 void addNodeHandler(std::shared_ptr<ModelTraverseHandler> pHandler)
81 {
82 m_pNodeHandler.push_back(pHandler);
83 }
84};
85}
86
88 : m_pGraphicObject(pGraphicObject)
89{
90 constexpr double fLowPercentage = 110;
91 constexpr double fHighPercentage = 50;
92
93 m_nLowDPILimit = sal_Int32(100.0 / fLowPercentage * nDPI);
94 m_nHighDPILimit = sal_Int32(100.0 / fHighPercentage * nDPI);
95}
96
98{
100 Size aSizePixel = aGraphic.GetSizePixel();
101 Size aGraphicSize = m_pGraphicObject->GetLogicRect().GetSize();
102
103 double nSizeXInch
104 = o3tl::convert(double(aGraphicSize.Width()), o3tl::Length::mm100, o3tl::Length::in);
105 double nSizeYInch
106 = o3tl::convert(double(aGraphicSize.Height()), o3tl::Length::mm100, o3tl::Length::in);
107
108 m_nDPIX = sal_Int32(aSizePixel.Width() / nSizeXInch);
109 m_nDPIY = sal_Int32(aSizePixel.Height() / nSizeYInch);
110
111 return isDPITooLow() || isDPITooHigh();
112}
113
115
116namespace
117{
118class GraphicSizeCheckHandler : public ModelTraverseHandler
119{
120 sal_Int32 m_nDPI;
121 std::vector<std::unique_ptr<GraphicSizeViolation>>& m_rGraphicSizeViolationList;
122
123public:
124 GraphicSizeCheckHandler(
125 sal_Int32 nDPI,
126 std::vector<std::unique_ptr<GraphicSizeViolation>>& rGraphicSizeViolationList)
127 : m_nDPI(nDPI)
128 , m_rGraphicSizeViolationList(rGraphicSizeViolationList)
129 {
130 }
131
132 void handleSdrObject(SdrObject* pObject) override
133 {
134 auto* pGraphicObject = dynamic_cast<SdrGrafObj*>(pObject);
135 if (!pGraphicObject)
136 return;
137
138 auto pEntry = std::make_unique<GraphicSizeViolation>(m_nDPI, pGraphicObject);
139 if (pEntry->check())
140 {
141 m_rGraphicSizeViolationList.push_back(std::move(pEntry));
142 }
143 }
144};
145
146} // end anonymous namespace
147
149{
150 if (!m_pDocument)
151 return;
152
153 sal_Int32 nDPI = m_pDocument->getImagePreferredDPI();
154 if (nDPI == 0)
155 return;
156
157 auto pHandler = std::make_shared<GraphicSizeCheckHandler>(nDPI, m_aGraphicSizeViolationList);
158
159 ModelTraverser aModelTraverser(m_pDocument);
160 aModelTraverser.addNodeHandler(pHandler);
161 aModelTraverser.traverse();
162}
163
165{
166 OUString sText;
167
168 if (m_pViolation->isDPITooLow())
169 {
170 sText = SdResId(STR_WARNING_GRAPHIC_PIXEL_COUNT_LOW);
171 }
172 else if (m_pViolation->isDPITooHigh())
173 {
174 sText = SdResId(STR_WARNING_GRAPHIC_PIXEL_COUNT_HIGH);
175 }
176
177 sText = sText.replaceAll("%NAME%", m_pViolation->getGraphicName());
178 sText = sText.replaceAll("%DPIX%", OUString::number(m_pViolation->getDPIX()));
179 sText = sText.replaceAll("%DPIY%", OUString::number(m_pViolation->getDPIY()));
180
181 return sText;
182}
183
185{
186 sd::ViewShell* pViewShell = m_pDocument->GetDocSh()->GetViewShell();
187 SdrView* pView = pViewShell->GetView();
188 pView->ShowSdrPage(m_pViolation->getObject()->getSdrPageFromSdrObject());
189 pView->UnmarkAll();
190 pView->MarkObj(m_pViolation->getObject(), pView->GetSdrPageView());
191}
192
194{
195 markObject();
196 sd::ViewShell* pViewShell = m_pDocument->GetDocSh()->GetViewShell();
197 pViewShell->GetDispatcher()->Execute(SID_ATTR_GRAF_CROP, SfxCallMode::SYNCHRON);
198}
199
201{
202 GraphicSizeCheck aCheck(pDocument);
203 aCheck.check();
204
205 auto& rCollection = getCollection();
206 for (auto& rpViolation : aCheck.getViolationList())
207 {
208 auto rGUIEntry
209 = std::make_unique<GraphicSizeCheckGUIEntry>(pDocument, std::move(rpViolation));
210 rCollection.push_back(std::move(rGUIEntry));
211 }
212}
213
215{
216 return SdResId(STR_GRAPHIC_SIZE_CHECK_DIALOG_TITLE);
217}
218
219} // end of namespace sd
220
221/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
SAL_DLLPRIVATE::sd::DrawDocShell * GetDocSh() const
Definition: drawdoc.hxx:242
sal_Int32 getImagePreferredDPI() const override
Definition: drawdoc.hxx:621
const Graphic & GetGraphic() const
bool MarkObj(const Point &rPnt, short nTol=-2, bool bToggle=false, bool bDeep=false)
const SdrPage * GetPage(sal_uInt16 nPgNum) const
sal_uInt16 GetPageCount() const
SdrPageView * ShowSdrPage(SdrPage *pPage) override
SdrObject * GetObj(size_t nNum) const
size_t GetObjCount() const
virtual const OUString & GetName() const
SdrPageView * GetSdrPageView() const
virtual const tools::Rectangle & GetLogicRect() const override
void UnmarkAll()
const SfxPoolItem * Execute(sal_uInt16 nSlot, SfxCallMode nCall=SfxCallMode::SLOT, const SfxPoolItem **pArgs=nullptr, sal_uInt16 nModi=0, const SfxPoolItem **pInternalArgs=nullptr)
SfxDispatcher * GetDispatcher() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
std::unique_ptr< GraphicSizeViolation > m_pViolation
GraphicSizeCheckGUIResult(SdDrawDocument *m_pDocument)
Run the graphic size checks for all the graphic objects in the DOM and store a list of violations.
std::vector< std::unique_ptr< GraphicSizeViolation > > & getViolationList()
std::vector< std::unique_ptr< GraphicSizeViolation > > m_aGraphicSizeViolationList
SdDrawDocument * m_pDocument
GraphicSizeViolation(sal_Int32 nDPI, SdrGrafObj *pGraphicObject)
const OUString & getGraphicName()
Base class of the stacked shell hierarchy.
Definition: ViewShell.hxx:92
::sd::View * GetView() const
Definition: ViewShell.hxx:144
std::vector< std::unique_ptr< CheckData > > & getCollection()
constexpr Size GetSize() const
EmbeddedObjectRef * pObject
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
OUString SdResId(TranslateId aId)
Definition: sdmod.cxx:83