LibreOffice Module sw (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 <GraphicSizeCheck.hxx>
12#include <svx/strings.hrc>
13#include <svx/svdobj.hxx>
14#include <sfx2/viewfrm.hxx>
15#include <sfx2/dispatch.hxx>
16
17#include <ModelTraverser.hxx>
18#include <ndgrf.hxx>
20#include <fmtfsize.hxx>
21#include <view.hxx>
22#include <wrtsh.hxx>
23#include <cmdid.h>
24
25using namespace css;
26
27namespace sw
28{
29GraphicSizeViolation::GraphicSizeViolation(sal_Int32 nDPI, const SwGrfNode* pGraphicNode)
30 : m_pGraphicNode(pGraphicNode)
31{
32 constexpr double fLowPercentage = 110;
33 constexpr double fHighPercentage = 50;
34
35 m_nLowDPILimit = sal_Int32(100.0 / fLowPercentage * nDPI);
36 m_nHighDPILimit = sal_Int32(100.0 / fHighPercentage * nDPI);
37}
38
40{
41 auto pFrameFormat = m_pGraphicNode->GetFlyFormat();
42 Graphic aGraphic = m_pGraphicNode->GetGraphic();
43 Size aSizePixel = aGraphic.GetSizePixel();
44 Size aFrameSize(pFrameFormat->GetFrameSize().GetSize());
45
46 double nSizeXInch
48 double nSizeYInch
50
51 m_nDPIX = sal_Int32(aSizePixel.Width() / nSizeXInch);
52 m_nDPIY = sal_Int32(aSizePixel.Height() / nSizeYInch);
53
54 return isDPITooLow() || isDPITooHigh();
55}
56
58{
60}
61
62namespace
63{
64class GraphicSizeCheckHandler : public ModelTraverseHandler
65{
66private:
67 sal_Int32 m_nDPI;
68 std::vector<std::unique_ptr<GraphicSizeViolation>>& m_rGraphicSizeViolationList;
69
70public:
71 GraphicSizeCheckHandler(
72 sal_Int32 nDPI,
73 std::vector<std::unique_ptr<GraphicSizeViolation>>& rGraphicSizeViolationList)
74 : m_nDPI(nDPI)
75 , m_rGraphicSizeViolationList(rGraphicSizeViolationList)
76 {
77 }
78
79 void handleNode(SwNode* pNode) override
80 {
81 if (!pNode->IsGrfNode())
82 return;
83
84 auto pEntry = std::make_unique<GraphicSizeViolation>(m_nDPI, pNode->GetGrfNode());
85 if (pEntry->check())
86 {
87 m_rGraphicSizeViolationList.push_back(std::move(pEntry));
88 }
89 }
90
91 void handleSdrObject(SdrObject* /*pObject*/) override {}
92};
93
94} // end anonymous namespace
95
97{
99 if (nDPI == 0)
100 return;
101
102 auto pHandler = std::make_shared<GraphicSizeCheckHandler>(nDPI, m_aGraphicSizeViolationList);
103 ModelTraverser aModelTraverser(m_pDocument);
104 aModelTraverser.addNodeHandler(pHandler);
105 aModelTraverser.traverse();
106}
107
109{
110 OUString sText;
111
112 if (m_pViolation->isDPITooLow())
113 {
114 sText = SwResId(STR_WARNING_GRAPHIC_PIXEL_COUNT_LOW);
115 }
116 else if (m_pViolation->isDPITooHigh())
117 {
118 sText = SwResId(STR_WARNING_GRAPHIC_PIXEL_COUNT_HIGH);
119 }
120
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()));
124
125 return sText;
126}
127
129{
131 pWrtShell->GotoFly(m_pViolation->getGraphicName(), FLYCNTTYPE_ALL, true);
132}
133
135{
136 markObject();
139 SfxCallMode::SYNCHRON);
140}
141
143{
144 GraphicSizeCheck aCheck(pDocument);
145 aCheck.check();
146
147 auto& rCollection = getCollection();
148 for (auto& rpViolation : aCheck.getViolationList())
149 {
150 auto rGUIEntry
151 = std::make_unique<GraphicSizeCheckGUIEntry>(pDocument, std::move(rpViolation));
152 rCollection.push_back(std::move(rGUIEntry));
153 }
154}
155
157{
158 return SwResId(STR_GRAPHIC_SIZE_CHECK_DIALOG_TITLE);
159}
160
161} // end sw namespace
162
163/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
virtual sal_Int32 getImagePreferredDPI()=0
const SfxPoolItem * Execute(sal_uInt16 nSlot, SfxCallMode nCall=SfxCallMode::SLOT, const SfxPoolItem **pArgs=nullptr, sal_uInt16 nModi=0, const SfxPoolItem **pInternalArgs=nullptr)
SfxDispatcher * GetDispatcher()
SfxViewFrame & GetViewFrame() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
SwWrtShell * GetWrtShell()
Access to the SwWrtShell belonging to SwView.
Definition: docsh.hxx:225
Definition: doc.hxx:197
IDocumentSettingAccess const & getIDocumentSettingAccess() const
Definition: doc.cxx:190
SwDocShell * GetDocShell()
Definition: doc.hxx:1370
const OUString & GetName() const
Definition: format.hxx:131
Graphic GetGraphic() const
Definition: ndnotxt.cxx:229
Base class of the Writer document model elements.
Definition: node.hxx:98
bool IsGrfNode() const
Definition: node.hxx:195
SwFrameFormat * GetFlyFormat() const
If node is in a fly return the respective format.
Definition: node.cxx:738
SwGrfNode * GetGrfNode()
Definition: ndgrf.hxx:150
Used by the UI to modify the document model.
Definition: wrtsh.hxx:97
bool GotoFly(const OUString &rName, FlyCntType eType=FLYCNTTYPE_ALL, bool bSelFrame=true)
Definition: move.cxx:637
const SwView & GetView() const
Definition: wrtsh.hxx:443
std::vector< std::unique_ptr< CheckData > > & getCollection()
std::unique_ptr< GraphicSizeViolation > m_pViolation
GraphicSizeCheckGUIResult(SwDoc *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
const OUString & getGraphicName()
GraphicSizeViolation(sal_Int32 nDPI, const SwGrfNode *pGraphicNode)
const SwGrfNode * m_pGraphicNode
void addNodeHandler(std::shared_ptr< ModelTraverseHandler > pHandler)
#define FN_FORMAT_GRAFIC_DLG
Definition: cmdid.h:354
@ FLYCNTTYPE_ALL
Definition: flyenum.hxx:25
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
Dialog to specify the properties of date form field.
OUString SwResId(TranslateId aId)
Definition: swmodule.cxx:168