LibreOffice Module sw (master)  1
chartins.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 <sfx2/viewfrm.hxx>
21 #include <sfx2/dispatch.hxx>
23 #include <osl/diagnose.h>
24 
25 #include <swmodule.hxx>
26 #include <wrtsh.hxx>
27 #include <docsh.hxx>
28 #include <view.hxx>
29 #include <chartins.hxx>
30 #include <tablemgr.hxx>
31 #include <frmfmt.hxx>
32 #include <unochart.hxx>
33 
34 #include <edtwin.hxx>
35 
36 #include <cmdid.h>
37 #include <anchoredobject.hxx>
38 
39 #include <cppuhelper/bootstrap.hxx>
41 #include <com/sun/star/awt/Point.hpp>
42 #include <com/sun/star/awt/Size.hpp>
43 #include <com/sun/star/awt/XWindow.hpp>
45 #include <com/sun/star/chart2/data/XDataProvider.hpp>
46 #include <com/sun/star/frame/XModel.hpp>
47 #include <com/sun/star/lang/XInitialization.hpp>
48 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
49 #include <com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.hpp>
50 
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
53 
54 Point SwGetChartDialogPos( const vcl::Window *pParentWin, const Size& rDialogSize, const tools::Rectangle& rLogicChart )
55 {
56  // positioning code according to spec; similar to Calc fuins2.cxx
57  Point aRet;
58 
59  OSL_ENSURE( pParentWin, "Window not found" );
60  if (pParentWin)
61  {
62  tools::Rectangle aObjPixel = pParentWin->LogicToPixel( rLogicChart, pParentWin->GetMapMode() );
63  tools::Rectangle aObjAbs( pParentWin->OutputToAbsoluteScreenPixel( aObjPixel.TopLeft() ),
64  pParentWin->OutputToAbsoluteScreenPixel( aObjPixel.BottomRight() ) );
65 
66  tools::Rectangle aDesktop = pParentWin->GetDesktopRectPixel();
67  Size aSpace = pParentWin->LogicToPixel(Size(8, 12), MapMode(MapUnit::MapAppFont));
68 
69  bool bLayoutRTL = ::GetActiveView()->GetWrtShell().IsTableRightToLeft();
70  bool bCenterHor = false;
71 
72  if ( aDesktop.Bottom() - aObjAbs.Bottom() >= rDialogSize.Height() + aSpace.Height() )
73  {
74  // first preference: below the chart
75  aRet.setY( aObjAbs.Bottom() + aSpace.Height() );
76  bCenterHor = true;
77  }
78  else if ( aObjAbs.Top() - aDesktop.Top() >= rDialogSize.Height() + aSpace.Height() )
79  {
80  // second preference: above the chart
81  aRet.setY( aObjAbs.Top() - rDialogSize.Height() - aSpace.Height() );
82  bCenterHor = true;
83  }
84  else
85  {
86  bool bFitLeft = ( aObjAbs.Left() - aDesktop.Left() >= rDialogSize.Width() + aSpace.Width() );
87  bool bFitRight = ( aDesktop.Right() - aObjAbs.Right() >= rDialogSize.Width() + aSpace.Width() );
88 
89  if ( bFitLeft || bFitRight )
90  {
91  // if both fit, prefer right in RTL mode, left otherwise
92  bool bPutRight = bFitRight && ( bLayoutRTL || !bFitLeft );
93  if ( bPutRight )
94  aRet.setX( aObjAbs.Right() + aSpace.Width() );
95  else
96  aRet.setX( aObjAbs.Left() - rDialogSize.Width() - aSpace.Width() );
97 
98  // center vertically
99  aRet.setY( aObjAbs.Top() + ( aObjAbs.GetHeight() - rDialogSize.Height() ) / 2 );
100  }
101  else
102  {
103  // doesn't fit on any edge - put at the bottom of the screen
104  aRet.setY( aDesktop.Bottom() - rDialogSize.Height() );
105  bCenterHor = true;
106  }
107  }
108  if ( bCenterHor )
109  aRet.setX( aObjAbs.Left() + ( aObjAbs.GetWidth() - rDialogSize.Width() ) / 2 );
110 
111  // limit to screen (centering might lead to invalid positions)
112  if ( aRet.X() + rDialogSize.Width() - 1 > aDesktop.Right() )
113  aRet.setX( aDesktop.Right() - rDialogSize.Width() + 1 );
114  if ( aRet.X() < aDesktop.Left() )
115  aRet.setX( aDesktop.Left() );
116  if ( aRet.Y() + rDialogSize.Height() - 1 > aDesktop.Bottom() )
117  aRet.setY( aDesktop.Bottom() - rDialogSize.Height() + 1 );
118  if ( aRet.Y() < aDesktop.Top() )
119  aRet.setY( aDesktop.Top() );
120  }
121 
122  return aRet;
123 }
124 
126 {
127  SwView *pView = ::GetActiveView();
128 
129  // get range string of marked data
130  SwWrtShell &rWrtShell = pView->GetWrtShell();
131  uno::Reference< chart2::data::XDataProvider > xDataProvider;
132  uno::Reference< frame::XModel > xChartModel;
133  OUString aRangeString;
134 
135  if( rWrtShell.IsCursorInTable())
136  {
137  if (!rWrtShell.IsTableMode())
138  {
139  // select whole table
140  rWrtShell.GetView().GetViewFrame()->GetDispatcher()->
141  Execute(FN_TABLE_SELECT_ALL, SfxCallMode::SYNCHRON);
142  }
143  if( ! rWrtShell.IsTableComplexForChart())
144  {
145  SwFrameFormat* pTableFormat = rWrtShell.GetTableFormat();
146  aRangeString = pTableFormat->GetName() + "." + rWrtShell.GetBoxNms();
147 
148  // get table data provider
149  xDataProvider.set( pView->GetDocShell()->getIDocumentChartDataProviderAccess().GetChartDataProvider( true ) );
150  }
151  }
152 
153  SwFlyFrameFormat *pFlyFrameFormat = nullptr;
154  xChartModel.set( SwTableFUNC( &rWrtShell ).InsertChart( xDataProvider, xDataProvider.is(), aRangeString, &pFlyFrameFormat ));
155 
156  //open wizard
157  //@todo get context from writer if that has one
158  uno::Reference< uno::XComponentContext > xContext(
160  if( !(xContext.is() && xChartModel.is() && xDataProvider.is()))
161  return;
162 
163  uno::Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
164  if(!xMCF.is())
165  return;
166 
167  uno::Reference< ui::dialogs::XAsynchronousExecutableDialog > xDialog(
168  xMCF->createInstanceWithContext(
169  "com.sun.star.comp.chart2.WizardDialog", xContext),
170  uno::UNO_QUERY);
171  uno::Reference< lang::XInitialization > xInit( xDialog, uno::UNO_QUERY );
172  if( xInit.is() )
173  {
174  // initialize dialog
175  uno::Sequence<uno::Any> aSeq(comphelper::InitAnyPropertySequence(
176  {
177  {"ParentWindow", uno::Any(uno::Reference< awt::XWindow >())},
178  {"ChartModel", uno::Any(xChartModel)}
179  }));
180  xInit->initialize( aSeq );
181 
182  // try to set the dialog's position so it doesn't hide the chart
183  uno::Reference < beans::XPropertySet > xDialogProps( xDialog, uno::UNO_QUERY );
184  if ( xDialogProps.is() )
185  {
186  try
187  {
188  //get dialog size:
189  awt::Size aDialogAWTSize;
190  if( xDialogProps->getPropertyValue("Size")
191  >>= aDialogAWTSize )
192  {
193  Size aDialogSize( aDialogAWTSize.Width, aDialogAWTSize.Height );
194  if ( !aDialogSize.IsEmpty() )
195  {
196  //calculate and set new position
197  SwRect aSwRect;
198  if (pFlyFrameFormat)
199  aSwRect = pFlyFrameFormat->GetAnchoredObj()->GetObjRectWithSpaces();
200  tools::Rectangle aRect( aSwRect.SVRect() );
201  Point aDialogPos = SwGetChartDialogPos( &rWrtShell.GetView().GetEditWin(), aDialogSize, aRect );
202  xDialogProps->setPropertyValue("Position",
203  uno::makeAny( awt::Point(aDialogPos.getX(),aDialogPos.getY()) ) );
204  }
205  }
206  }
207  catch (const uno::Exception&)
208  {
209  OSL_FAIL("Chart wizard couldn't be positioned automatically" );
210  }
211  }
212 
213  rtl::Reference<::svt::DialogClosedListener> pListener = new ::svt::DialogClosedListener();
214  pListener->SetDialogClosedLink( rLink );
215 
216  xDialog->startExecuteModal( pListener );
217  }
218  else
219  {
220  uno::Reference< lang::XComponent > xComponent( xDialog, uno::UNO_QUERY );
221  if( xComponent.is())
222  xComponent->dispose();
223  }
224 }
225 
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxViewFrame * GetViewFrame() const
SwView * GetActiveView()
Definition: swmodul1.cxx:116
constexpr tools::Long Left() const
bool IsTableMode() const
Definition: crsrsh.hxx:643
SfxDispatcher * GetDispatcher()
const ::avmedia::MediaItem * Execute(const SdrMarkView *pSdrView, SfxRequest const &rReq)
SwAnchoredObject * GetAnchoredObj() const
Definition: atrfrm.cxx:3137
SwEditWin & GetEditWin()
Definition: view.hxx:416
tools::Rectangle GetDesktopRectPixel() const
Of course Writer needs its own rectangles.
Definition: swrect.hxx:34
SwWrtShell & GetWrtShell() const
Definition: view.hxx:413
Used by the UI to modify the document model.
Definition: wrtsh.hxx:93
Point SwGetChartDialogPos(const vcl::Window *pParentWin, const Size &rDialogSize, const tools::Rectangle &rLogicChart)
Definition: chartins.cxx:54
constexpr tools::Long Width() const
const SwView & GetView() const
Definition: wrtsh.hxx:431
#define FN_TABLE_SELECT_ALL
Definition: cmdid.h:361
bool IsTableRightToLeft() const
Definition: fetab.cxx:2328
const OUString & GetName() const
Definition: format.hxx:115
constexpr tools::Long GetWidth() const
const SwTableNode * IsCursorInTable() const
Definition: crsrsh.hxx:889
Style of a layout element.
Definition: frmfmt.hxx:59
Point LogicToPixel(const Point &rLogicPt) const
bool IsEmpty() const
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
virtual SwChartDataProvider * GetChartDataProvider(bool bCreate=false) const =0
returns or creates the data-provider for chart
const MapMode & GetMapMode() const
constexpr tools::Long Right() const
const SwRect & GetObjRectWithSpaces() const
method to determine object area inclusive its spacing
constexpr tools::Long Top() const
CPPUHELPER_DLLPUBLIC css::uno::Reference< css::uno::XComponentContext > SAL_CALL defaultBootstrap_InitialComponentContext()
Point OutputToAbsoluteScreenPixel(const Point &rPos) const
OUString GetBoxNms() const
Definition: trvltbl.cxx:746
SwDocShell * GetDocShell()
Definition: view.cxx:1127
SwFrameFormat * GetTableFormat()
Definition: edws.cxx:183
constexpr Point TopLeft() const
constexpr tools::Long Bottom() const
tools::Rectangle SVRect() const
Definition: swrect.hxx:272
constexpr tools::Long Height() const
IDocumentChartDataProviderAccess & getIDocumentChartDataProviderAccess()
Definition: docshini.cxx:700
Sequence< sal_Int8 > aSeq
SwInsertChart(const Link< css::ui::dialogs::DialogClosedEvent *, void > &rLink)
Definition: chartins.cxx:125
constexpr Point BottomRight() const
bool IsTableComplexForChart()
Definition: trvltbl.cxx:726
Definition: view.hxx:144
constexpr tools::Long GetHeight() const