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