LibreOffice Module svx (master)  1
connctrl.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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <vcl/svapp.hxx>
21 
22 #include <svx/connctrl.hxx>
23 #include <svx/dlgutil.hxx>
24 
27 #include <svx/svdmark.hxx>
28 #include <svx/svdoedge.hxx>
29 #include <svx/svdpage.hxx>
30 #include <svx/svdview.hxx>
31 #include <svx/sxelditm.hxx>
32 #include <sxmkitm.hxx>
33 
34 #include <vcl/settings.hxx>
35 #include <memory>
36 
38  : pEdgeObj(nullptr)
39  , pView(nullptr)
40 {
41  SetMapMode(MapMode(MapUnit::Map100thMM));
42 }
43 
45 {
47  Size aSize(pDrawingArea->get_ref_device().LogicToPixel(Size(118 , 121), MapMode(MapUnit::MapAppFont)));
48  pDrawingArea->set_size_request(aSize.Width(), aSize.Height());
49  SetOutputSizePixel(aSize);
50 }
51 
53 {
54 }
55 
57 {
58  AdaptSize();
59 
60  Invalidate();
61 }
62 
64 {
65  // Adapt size
66  if( !pSdrPage )
67  return;
68 
69  SetMapMode(MapMode(MapUnit::Map100thMM));
70 
71  OutputDevice* pOD = pView->GetFirstOutputDevice(); // GetWin( 0 );
72  tools::Rectangle aRect = pSdrPage->GetAllObjBoundRect();
73 
74  MapMode aMapMode = GetMapMode();
75  aMapMode.SetMapUnit( pOD->GetMapMode().GetMapUnit() );
76  SetMapMode( aMapMode );
77 
78  MapMode aDisplayMap( aMapMode );
79  Point aNewPos;
80  Size aNewSize;
81  const Size aWinSize = GetDrawingArea()->get_ref_device().PixelToLogic(GetOutputSizePixel(), aDisplayMap);
82  const long nWidth = aWinSize.Width();
83  const long nHeight = aWinSize.Height();
84  if (aRect.GetHeight() == 0)
85  return;
86  double fRectWH = static_cast<double>(aRect.GetWidth()) / aRect.GetHeight();
87  if (nHeight == 0)
88  return;
89  double fWinWH = static_cast<double>(nWidth) / nHeight;
90 
91  // Adapt bitmap to Thumb size (not here!)
92  if ( fRectWH < fWinWH)
93  {
94  aNewSize.setWidth( static_cast<long>( static_cast<double>(nHeight) * fRectWH ) );
95  aNewSize.setHeight( nHeight );
96  }
97  else
98  {
99  aNewSize.setWidth( nWidth );
100  aNewSize.setHeight( static_cast<long>( static_cast<double>(nWidth) / fRectWH ) );
101  }
102 
103  Fraction aFrac1( aWinSize.Width(), aRect.GetWidth() );
104  Fraction aFrac2( aWinSize.Height(), aRect.GetHeight() );
105  Fraction aMinFrac( aFrac1 <= aFrac2 ? aFrac1 : aFrac2 );
106 
107  // Implement MapMode
108  aDisplayMap.SetScaleX( aMinFrac );
109  aDisplayMap.SetScaleY( aMinFrac );
110 
111  // Centering
112  aNewPos.setX( ( nWidth - aNewSize.Width() ) >> 1 );
113  aNewPos.setY( ( nHeight - aNewSize.Height() ) >> 1 );
114 
115  aDisplayMap.SetOrigin(OutputDevice::LogicToLogic(aNewPos, aMapMode, aDisplayMap));
116  SetMapMode( aDisplayMap );
117 
118  // Origin
119  aNewPos = aDisplayMap.GetOrigin();
120  aNewPos -= Point( aRect.TopLeft().X(), aRect.TopLeft().Y() );
121  aDisplayMap.SetOrigin( aNewPos );
122  SetMapMode( aDisplayMap );
123 
124  MouseEvent aMEvt( Point(), 1, MouseEventModifiers::NONE, MOUSE_RIGHT );
125  MouseButtonDown( aMEvt );
126 }
127 
129 {
130  DBG_ASSERT( pView, "No valid view is passed on! ");
131 
132  const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
133  const size_t nMarkCount = rMarkList.GetMarkCount();
134 
135  if( nMarkCount >= 1 )
136  {
137  bool bFound = false;
138 
139  for( size_t i = 0; i < nMarkCount && !bFound; ++i )
140  {
141  const SdrObject* pObj = rMarkList.GetMark( i )->GetMarkedSdrObj();
142  SdrInventor nInv = pObj->GetObjInventor();
143  sal_uInt16 nId = pObj->GetObjIdentifier();
144  if( nInv == SdrInventor::Default && nId == OBJ_EDGE )
145  {
146  bFound = true;
147 
148  // potential memory leak here (!). Create SdrObjList only when there is
149  // not yet one.
150  if(!pSdrPage)
151  {
152  pSdrPage.reset( new SdrPage(
154  false) );
155  }
156 
157  const SdrEdgeObj* pTmpEdgeObj = static_cast<const SdrEdgeObj*>(pObj);
158  pEdgeObj = pTmpEdgeObj->CloneSdrObject(pSdrPage->getSdrModelFromSdrPage());
159 
160  SdrObjConnection& rConn1 = pEdgeObj->GetConnection( true );
161  SdrObjConnection& rConn2 = pEdgeObj->GetConnection( false );
162 
163  rConn1 = pTmpEdgeObj->GetConnection( true );
164  rConn2 = pTmpEdgeObj->GetConnection( false );
165 
166  SdrObject* pTmpObj1 = pTmpEdgeObj->GetConnectedNode( true );
167  SdrObject* pTmpObj2 = pTmpEdgeObj->GetConnectedNode( false );
168 
169  if( pTmpObj1 )
170  {
171  SdrObject* pObj1 = pTmpObj1->CloneSdrObject(pSdrPage->getSdrModelFromSdrPage());
172  pSdrPage->InsertObject( pObj1 );
173  pEdgeObj->ConnectToNode( true, pObj1 );
174  }
175 
176  if( pTmpObj2 )
177  {
178  SdrObject* pObj2 = pTmpObj2->CloneSdrObject(pSdrPage->getSdrModelFromSdrPage());
179  pSdrPage->InsertObject( pObj2 );
180  pEdgeObj->ConnectToNode( false, pObj2 );
181  }
182 
183  pSdrPage->InsertObject( pEdgeObj );
184  }
185  }
186  }
187 
188  if( !pEdgeObj )
189  {
191  }
192 
193  AdaptSize();
194 }
195 
197 {
198  rRenderContext.Push(PushFlags::ALL);
199 
200  rRenderContext.SetMapMode(GetMapMode());
201 
204  rRenderContext.SetBackground(Wallpaper(rStyles.GetFieldColor()));
205 
206  if (pSdrPage)
207  {
208  // This will not work anymore. To not start at Adam and Eve, i will
209  // ATM not try to change all this stuff to really using an own model
210  // and a view. I will just try to provide a mechanism to paint such
211  // objects without own model and without a page/view with the new
212  // mechanism.
213 
214  // New stuff: Use an ObjectContactOfObjListPainter.
215  sdr::contact::SdrObjectVector aObjectVector;
216 
217  for (size_t a = 0; a < pSdrPage->GetObjCount(); ++a)
218  {
219  SdrObject* pObject = pSdrPage->GetObj(a);
220  DBG_ASSERT(pObject,
221  "SvxXConnectionPreview::Paint: Corrupt ObjectList (!)");
222  aObjectVector.push_back(pObject);
223  }
224 
225  sdr::contact::ObjectContactOfObjListPainter aPainter(rRenderContext, aObjectVector, nullptr);
226  sdr::contact::DisplayInfo aDisplayInfo;
227 
228  // do processing
229  aPainter.ProcessDisplay(aDisplayInfo);
230  }
231 
232  rRenderContext.Pop();
233 }
234 
236 {
238 
239  Invalidate();
240 }
241 
242 // Get number of lines which are offset based on the preview object
243 
245 {
246  const SfxItemSet& rSet = pEdgeObj->GetMergedItemSet();
247  sal_uInt16 nCount(0);
248 
249  if(SfxItemState::DONTCARE != rSet.GetItemState(SDRATTR_EDGELINEDELTACOUNT))
250  nCount = rSet.Get(SDRATTR_EDGELINEDELTACOUNT).GetValue();
251 
252  return nCount;
253 }
254 
256 {
257  bool bZoomIn = rMEvt.IsLeft() && !rMEvt.IsShift();
258  bool bZoomOut = rMEvt.IsRight() || rMEvt.IsShift();
259  bool bCtrl = rMEvt.IsMod1();
260 
261  if( bZoomIn || bZoomOut )
262  {
263  MapMode aMapMode = GetMapMode();
264  Fraction aXFrac = aMapMode.GetScaleX();
265  Fraction aYFrac = aMapMode.GetScaleY();
266  std::unique_ptr<Fraction> pMultFrac;
267 
268  if( bZoomIn )
269  {
270  if( bCtrl )
271  pMultFrac.reset(new Fraction( 3, 2 ));
272  else
273  pMultFrac.reset(new Fraction( 11, 10 ));
274  }
275  else
276  {
277  if( bCtrl )
278  pMultFrac.reset(new Fraction( 2, 3 ));
279  else
280  pMultFrac.reset(new Fraction( 10, 11 ));
281  }
282 
283  aXFrac *= *pMultFrac;
284  aYFrac *= *pMultFrac;
285  if( static_cast<double>(aXFrac) > 0.001 && static_cast<double>(aXFrac) < 1000.0 &&
286  static_cast<double>(aYFrac) > 0.001 && static_cast<double>(aYFrac) < 1000.0 )
287  {
288  aMapMode.SetScaleX( aXFrac );
289  aMapMode.SetScaleY( aYFrac );
290  SetMapMode( aMapMode );
291 
292  Size aOutSize(GetOutputSizePixel());
293  aOutSize = GetDrawingArea()->get_ref_device().PixelToLogic(aOutSize);
294 
295  Point aPt( aMapMode.GetOrigin() );
296  long nX = static_cast<long>( ( static_cast<double>(aOutSize.Width()) - ( static_cast<double>(aOutSize.Width()) * static_cast<double>(*pMultFrac) ) ) / 2.0 + 0.5 );
297  long nY = static_cast<long>( ( static_cast<double>(aOutSize.Height()) - ( static_cast<double>(aOutSize.Height()) * static_cast<double>(*pMultFrac) ) ) / 2.0 + 0.5 );
298  aPt.AdjustX(nX );
299  aPt.AdjustY(nY );
300 
301  aMapMode.SetOrigin( aPt );
302  SetMapMode( aMapMode );
303 
304  Invalidate();
305  }
306  }
307 
308  return true;
309 }
310 
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Point TopLeft() const
const Fraction & GetScaleX() const
long Width() const
long GetWidth() const
Utility class SdrObjConnection.
Definition: svdoedge.hxx:38
size_t GetMarkCount() const
Definition: svdmark.hxx:179
long GetHeight() const
virtual void Resize() override
Definition: connctrl.cxx:56
SVX_DLLPRIVATE const MapMode & GetMapMode() const
Definition: connctrl.hxx:50
long AdjustX(long nHorzMove)
Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
long Height() const
sal_uInt16 GetLineDeltaCount() const
Definition: connctrl.cxx:244
const StyleSettings & GetStyleSettings() const
static const AllSettings & GetSettings()
sal_Int16 nId
bool IsShift() const
const MapMode & GetMapMode() const
SdrInventor
Definition: svdobj.hxx:152
virtual ~SvxXConnectionPreview() override
Definition: connctrl.cxx:52
SdrMark * GetMark(size_t nNum) const
Definition: svdmark.cxx:234
OLE object.
Definition: svdobj.hxx:138
constexpr DrawModeFlags OUTPUT_DRAWMODE_COLOR
Definition: dlgutil.hxx:33
EmbeddedObjectRef * pObject
void SetMapMode()
virtual void SetDrawingArea(weld::DrawingArea *pDrawingArea) override
Definition: connctrl.cxx:44
Size const & GetOutputSizePixel() const
void ConnectToNode(bool bTail1, SdrObject *pObj) override
Definition: svdoedge.cxx:454
void SetDrawMode(DrawModeFlags nDrawMode)
::std::vector< SdrObject * > SdrObjectVector
void SetBackground()
SVX_DLLPRIVATE void AdaptSize()
Definition: connctrl.cxx:63
void setX(long nX)
const Fraction & GetScaleY() const
int nCount
bool GetHighContrastMode() const
SVX_DLLPRIVATE void SetMapMode(const MapMode &rNewMapMode)
Definition: connctrl.hxx:49
OutputDevice * GetFirstOutputDevice() const
Definition: svdpntv.cxx:93
virtual OutputDevice & get_ref_device()=0
void setY(long nY)
SdrObject * GetConnectedNode(bool bTail1) const override
Definition: svdoedge.cxx:478
virtual SdrEdgeObj * CloneSdrObject(SdrModel &rTargetModel) const override
Definition: svdoedge.cxx:1649
virtual void ProcessDisplay(DisplayInfo &rDisplayInfo) override
void SetMapUnit(MapUnit eUnit)
SdrObject * GetMarkedSdrObj() const
Definition: svdmark.hxx:67
void SetScaleX(const Fraction &rScaleX)
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect) override
Definition: connctrl.cxx:196
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
#define DBG_ASSERT(sCon, aError)
uno_Any a
virtual bool MouseButtonDown(const MouseEvent &rMEvt) override
Definition: connctrl.cxx:255
virtual sal_uInt16 GetObjIdentifier() const
Definition: svdobj.cxx:585
SdrEdgeObj * pEdgeObj
Definition: connctrl.hxx:44
int i
const SdrMarkList & GetMarkedObjectList() const
Definition: svdmrkv.hxx:243
void SetOrigin(const Point &rOrigin)
const Color & GetFieldColor() const
void SetAttributes(const SfxItemSet &rInAttrs)
Definition: connctrl.cxx:235
void SetMergedItemSetAndBroadcast(const SfxItemSet &rSet, bool bClearAllItems=false)
Definition: svdobj.cxx:1951
void SetScaleY(const Fraction &rScaleY)
MapUnit GetMapUnit() const
Abstract DrawObject.
Definition: svdobj.hxx:312
SdrObjConnection & GetConnection(bool bTail1)
Definition: svdoedge.hxx:197
void SetOutputSizePixel(const Size &rSize)
long X() const
virtual SdrInventor GetObjInventor() const
Definition: svdobj.cxx:580
Point PixelToLogic(const Point &rDevicePt) const
Point LogicToPixel(const Point &rLogicPt) const
weld::DrawingArea * GetDrawingArea() const
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
const SfxItemSet & GetMergedItemSet() const
Definition: svdobj.cxx:1911
bool IsLeft() const
virtual SdrObject * CloneSdrObject(SdrModel &rTargetModel) const
Definition: svdobj.cxx:1001
virtual void SetDrawingArea(weld::DrawingArea *pDrawingArea)
const Point & GetOrigin() const
const SdrView * pView
Definition: connctrl.hxx:46
std::unique_ptr< SdrPage > pSdrPage
Definition: connctrl.hxx:45
SdrModel & getSdrModelFromSdrView() const
Definition: svdpntv.hxx:268
bool IsRight() const
A SdrPage contains exactly one SdrObjList and a description of the physical page dimensions (size / m...
Definition: svdpage.hxx:366
#define SDRATTR_EDGELINEDELTACOUNT
Definition: svddef.hxx:230
virtual void set_size_request(int nWidth, int nHeight)=0
void Push(PushFlags nFlags=PushFlags::ALL)
void setWidth(long nWidth)
#define MOUSE_RIGHT
long Y() const
Utility class SdrEdgeObj.
Definition: svdoedge.hxx:128
constexpr DrawModeFlags OUTPUT_DRAWMODE_CONTRAST
Definition: dlgutil.hxx:34
bool IsMod1() const
void setHeight(long nHeight)