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