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 
25 using namespace css;
26 
27 namespace sw
28 {
29 GraphicSizeViolation::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
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);
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 {
59  return m_pGraphicNode->GetFlyFormat()->GetName();
60 }
61 
62 namespace
63 {
64 class GraphicSizeCheckHandler : public ModelTraverseHandler
65 {
66 private:
67  sal_Int32 m_nDPI;
68  std::vector<std::unique_ptr<GraphicSizeViolation>>& m_rGraphicSizeViolationList;
69 
70 public:
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 {
130  SwWrtShell* pWrtShell = m_pDocument->GetDocShell()->GetWrtShell();
131  pWrtShell->GotoFly(m_pViolation->getGraphicName(), FLYCNTTYPE_ALL, true);
132 }
133 
135 {
136  markObject();
137  SwWrtShell* pWrtShell = m_pDocument->GetDocShell()->GetWrtShell();
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: */
SfxViewFrame * GetViewFrame() const
Graphic GetGraphic() const
Definition: ndnotxt.cxx:229
std::vector< std::unique_ptr< GraphicSizeViolation > > & getViolationList()
bool IsGrfNode() const
Definition: node.hxx:666
SwDocShell * GetDocShell()
Definition: doc.hxx:1351
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
SfxDispatcher * GetDispatcher()
const OUString & getGraphicName()
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
Definition: doc.hxx:187
Dialog to specify the properties of date form field.
#define FN_FORMAT_GRAFIC_DLG
Definition: cmdid.h:332
Used by the UI to modify the document model.
Definition: wrtsh.hxx:96
OUString SwResId(TranslateId aId)
Definition: swmodule.cxx:164
constexpr tools::Long Width() const
bool GotoFly(const OUString &rName, FlyCntType eType=FLYCNTTYPE_ALL, bool bSelFrame=true)
Definition: move.cxx:599
const SwView & GetView() const
Definition: wrtsh.hxx:437
const OUString & GetName() const
Definition: format.hxx:131
virtual sal_Int32 getImagePreferredDPI()=0
SwWrtShell * GetWrtShell()
Access to the SwWrtShell belonging to SwView.
Definition: docsh.hxx:225
GraphicSizeCheckGUIResult(SwDoc *pDocument)
constexpr tools::Long Height() const
Run the graphic size checks for all the graphic objects in the DOM and store a list of violations...
IDocumentSettingAccess const & getIDocumentSettingAccess() const
Definition: doc.cxx:176
const SwGrfNode * m_pGraphicNode
std::vector< std::unique_ptr< GraphicSizeViolation > > m_aGraphicSizeViolationList
std::unique_ptr< GraphicSizeViolation > m_pViolation
const SfxPoolItem * Execute(sal_uInt16 nSlot, SfxCallMode nCall=SfxCallMode::SLOT, const SfxPoolItem **pArgs=nullptr, sal_uInt16 nModi=0, const SfxPoolItem **pInternalArgs=nullptr)
SwGrfNode * GetGrfNode()
Definition: ndgrf.hxx:154
SwFrameFormat * GetFlyFormat() const
If node is in a fly return the respective format.
Definition: node.cxx:719
std::vector< std::unique_ptr< CheckData > > & getCollection()
Base class of the Writer document model elements.
Definition: node.hxx:81