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