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 <IDocumentUndoRedo.hxx>
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>
44 #include <com/sun/star/chart2/data/XDataProvider.hpp>
45 #include <com/sun/star/frame/XModel.hpp>
46 #include <com/sun/star/lang/XInitialization.hpp>
47 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
48 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
49 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.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  {
162  uno::Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
163  if(xMCF.is())
164  {
165  uno::Reference< ui::dialogs::XExecutableDialog > xDialog(
166  xMCF->createInstanceWithContext(
167  "com.sun.star.comp.chart2.WizardDialog", xContext),
168  uno::UNO_QUERY);
169  uno::Reference< lang::XInitialization > xInit( xDialog, uno::UNO_QUERY );
170  if( xInit.is() )
171  {
172  // initialize dialog
173  uno::Sequence<uno::Any> aSeq(comphelper::InitAnyPropertySequence(
174  {
175  {"ParentWindow", uno::Any(uno::Reference< awt::XWindow >())},
176  {"ChartModel", uno::Any(xChartModel)}
177  }));
178  xInit->initialize( aSeq );
179 
180  // try to set the dialog's position so it doesn't hide the chart
181  uno::Reference < beans::XPropertySet > xDialogProps( xDialog, uno::UNO_QUERY );
182  if ( xDialogProps.is() )
183  {
184  try
185  {
186  //get dialog size:
187  awt::Size aDialogAWTSize;
188  if( xDialogProps->getPropertyValue("Size")
189  >>= aDialogAWTSize )
190  {
191  Size aDialogSize( aDialogAWTSize.Width, aDialogAWTSize.Height );
192  if ( !aDialogSize.IsEmpty() )
193  {
194  //calculate and set new position
195  SwRect aSwRect;
196  if (pFlyFrameFormat)
197  aSwRect = pFlyFrameFormat->GetAnchoredObj()->GetObjRectWithSpaces();
198  tools::Rectangle aRect( aSwRect.SVRect() );
199  Point aDialogPos = SwGetChartDialogPos( &rWrtShell.GetView().GetEditWin(), aDialogSize, aRect );
200  xDialogProps->setPropertyValue("Position",
201  uno::makeAny( awt::Point(aDialogPos.getX(),aDialogPos.getY()) ) );
202  }
203  }
204  }
205  catch (const uno::Exception&)
206  {
207  OSL_FAIL("Chart wizard couldn't be positioned automatically" );
208  }
209  }
210 
211  sal_Int16 nDialogRet = xDialog->execute();
212  if( nDialogRet == ui::dialogs::ExecutableDialogResults::CANCEL )
213  {
214  rWrtShell.Undo();
215  rWrtShell.GetIDocumentUndoRedo().ClearRedo();
216  }
217  else
218  {
219  OSL_ENSURE( nDialogRet == ui::dialogs::ExecutableDialogResults::OK,
220  "dialog execution failed" );
221  }
222  }
223  uno::Reference< lang::XComponent > xComponent( xDialog, uno::UNO_QUERY );
224  if( xComponent.is())
225  xComponent->dispose();
226  }
227  }
228 }
229 
230 /* 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:115
long Height() const
bool IsTableMode() const
Definition: crsrsh.hxx:647
SfxDispatcher * GetDispatcher()
SwAnchoredObject * GetAnchoredObj() const
Definition: atrfrm.cxx:3065
const MapMode & GetMapMode() const
SwEditWin & GetEditWin()
Definition: view.hxx:403
tools::Rectangle GetDesktopRectPixel() const
Of course Writer needs its own rectangles.
Definition: swrect.hxx:35
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:54
long Right() const
const SwView & GetView() const
Definition: wrtsh.hxx:428
#define FN_TABLE_SELECT_ALL
Definition: cmdid.h:356
bool IsTableRightToLeft() const
Definition: fetab.cxx:2298
const OUString & GetName() const
Definition: format.hxx:111
long Top() const
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
bool IsEmpty() const
void SwInsertChart()
Definition: chartins.cxx:125
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()
Point OutputToAbsoluteScreenPixel(const Point &rPos) const
OUString GetBoxNms() const
Definition: trvltbl.cxx:745
SwDocShell * GetDocShell()
Definition: view.cxx:1107
SwFrameFormat * GetTableFormat()
Definition: edws.cxx:183
Point LogicToPixel(const Point &rLogicPt) const
IDocumentUndoRedo const & GetIDocumentUndoRedo() const
Provides access to the document undo/redo interface.
Definition: viewsh.cxx:2603
tools::Rectangle SVRect() const
Definition: swrect.hxx:280
IDocumentChartDataProviderAccess & getIDocumentChartDataProviderAccess()
Definition: docshini.cxx:694
Sequence< sal_Int8 > aSeq
long Left() const
bool IsTableComplexForChart()
Definition: trvltbl.cxx:725
void Undo(sal_uInt16 const nCount=1)
Definition: edundo.cxx:97
Definition: view.hxx:146