tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / chart2 / source / controller / main / ChartController_TextEdit.cxx
blobbfbacd95a90ce8a4423ef25774389dd3fa89e304
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_wasm_strip.h>
22 #include <ChartController.hxx>
24 #include <ResId.hxx>
25 #include "UndoGuard.hxx"
26 #include <DrawViewWrapper.hxx>
27 #include <ChartWindow.hxx>
28 #include <ChartModel.hxx>
29 #include <ChartView.hxx>
30 #include <TitleHelper.hxx>
31 #include <ObjectIdentifier.hxx>
32 #include <ControllerLockGuard.hxx>
33 #include <comphelper/diagnose_ex.hxx>
34 #if !ENABLE_WASM_STRIP_ACCESSIBILITY
35 #include <AccessibleTextHelper.hxx>
36 #endif
37 #include <strings.hrc>
38 #include <chartview/DrawModelWrapper.hxx>
39 #include <osl/diagnose.h>
41 #include <svx/svdoutl.hxx>
42 #include <svx/svxdlg.hxx>
43 #include <svx/svxids.hrc>
44 #include <editeng/editids.hrc>
45 #include <vcl/svapp.hxx>
46 #include <com/sun/star/beans/XPropertySet.hpp>
47 #include <com/sun/star/beans/XPropertySetInfo.hpp>
48 #include <com/sun/star/text/XTextCursor.hpp>
49 #include <com/sun/star/chart2/FormattedString.hpp>
50 #include <svl/stritem.hxx>
51 #include <editeng/fontitem.hxx>
52 #include <editeng/section.hxx>
53 #include <memory>
55 namespace chart
57 using namespace ::com::sun::star;
59 void ChartController::executeDispatch_EditText( const Point* pMousePixel )
61 StartTextEdit( pMousePixel );
64 void ChartController::StartTextEdit( const Point* pMousePixel )
66 //the first marked object will be edited
68 SolarMutexGuard aGuard;
69 SdrObject* pTextObj = m_pDrawViewWrapper->getTextEditObject();
70 if(!pTextObj)
71 return;
73 OSL_PRECOND(!m_pTextActionUndoGuard,
74 "ChartController::StartTextEdit: already have a TextUndoGuard!?");
75 m_pTextActionUndoGuard.reset( new UndoGuard(
76 SchResId( STR_ACTION_EDIT_TEXT ), m_xUndoManager ) );
77 SdrOutliner* pOutliner = m_pDrawViewWrapper->getOutliner();
79 //#i77362 change notification for changes on additional shapes are missing
80 if( m_xChartView.is() )
81 m_xChartView->setPropertyValue( u"SdrViewIsInEditMode"_ustr, uno::Any(true) );
83 auto pChartWindow(GetChartWindow());
85 bool bEdit = m_pDrawViewWrapper->SdrBeginTextEdit( pTextObj
86 , m_pDrawViewWrapper->GetPageView()
87 , pChartWindow
88 , false //bIsNewObj
89 , pOutliner
90 , nullptr //pOutlinerView
91 , true //bDontDeleteOutliner
92 , true //bOnlyOneView
94 if(!bEdit)
95 return;
97 m_pDrawViewWrapper->SetEditMode();
99 // #i12587# support for shapes in chart
100 if ( pMousePixel )
102 OutlinerView* pOutlinerView = m_pDrawViewWrapper->GetTextEditOutlinerView();
103 if ( pOutlinerView )
105 MouseEvent aEditEvt( *pMousePixel, 1, MouseEventModifiers::SYNTHETIC, MOUSE_LEFT, 0 );
106 pOutlinerView->MouseButtonDown( aEditEvt );
107 pOutlinerView->MouseButtonUp( aEditEvt );
111 if (pChartWindow)
113 //we invalidate the outliner region because the outliner has some
114 //paint problems (some characters are painted twice a little bit shifted)
115 pChartWindow->Invalidate( m_pDrawViewWrapper->GetMarkedObjBoundRect() );
119 bool ChartController::EndTextEdit()
121 m_pDrawViewWrapper->SdrEndTextEdit();
123 //#i77362 change notification for changes on additional shapes are missing
124 if( m_xChartView.is() )
125 m_xChartView->setPropertyValue( u"SdrViewIsInEditMode"_ustr, uno::Any(false) );
127 SdrObject* pTextObject = m_pDrawViewWrapper->getTextEditObject();
128 if(!pTextObject)
129 return false;
131 OutlinerParaObject* pParaObj = pTextObject->GetOutlinerParaObject();
132 if( !pParaObj )
133 return true;
135 OUString aObjectCID = m_aSelection.getSelectedCID();
136 if ( !aObjectCID.isEmpty() )
138 uno::Reference< beans::XPropertySet > xPropSet =
139 ObjectIdentifier::getObjectPropertySet( aObjectCID, getChartModel() );
141 // lock controllers till end of block
142 ControllerLockGuardUNO aCLGuard( getChartModel() );
144 uno::Sequence< uno::Reference< chart2::XFormattedString > > aNewFormattedTitle =
145 GetFormattedTitle(pParaObj->GetTextObject(), pTextObject->getUnoShape());
147 Title* pTitle = dynamic_cast<Title*>(xPropSet.get());
148 TitleHelper::setFormattedString(pTitle, aNewFormattedTitle);
150 OSL_ENSURE(m_pTextActionUndoGuard, "ChartController::EndTextEdit: no TextUndoGuard!");
151 if (m_pTextActionUndoGuard)
152 m_pTextActionUndoGuard->commit();
154 m_pTextActionUndoGuard.reset();
155 return true;
158 uno::Sequence< uno::Reference< chart2::XFormattedString > > ChartController::GetFormattedTitle(
159 const EditTextObject& aEdit, const uno::Reference< drawing::XShape >& xShape )
161 std::vector < uno::Reference< chart2::XFormattedString > > aNewStrings;
162 if (!xShape.is())
163 return comphelper::containerToSequence(aNewStrings);
165 uno::Reference< text::XText > xText(xShape, uno::UNO_QUERY);
166 if (!xText.is())
167 return comphelper::containerToSequence(aNewStrings);
169 uno::Reference< text::XTextCursor > xSelectionCursor(xText->createTextCursor());
170 if (!xSelectionCursor.is())
171 return comphelper::containerToSequence(aNewStrings);
173 xSelectionCursor->gotoStart(false);
175 std::vector<editeng::Section> aSecAttrs;
176 aEdit.GetAllSections(aSecAttrs);
178 for (editeng::Section const& rSection : aSecAttrs)
180 if (!xSelectionCursor->isCollapsed())
181 xSelectionCursor->collapseToEnd();
183 xSelectionCursor->goRight(rSection.mnEnd - rSection.mnStart, true);
185 OUString aNewString = xSelectionCursor->getString();
187 bool bNextPara = (aEdit.GetParagraphCount() > 1 && rSection.mnParagraph != aEdit.GetParagraphCount() - 1 &&
188 aEdit.GetTextLen(rSection.mnParagraph) <= rSection.mnEnd);
190 uno::Reference< chart2::XFormattedString2 > xFmtStr = chart2::FormattedString::create(m_xCC);
191 if (bNextPara)
192 aNewString = aNewString + OUStringChar('\n');
193 xFmtStr->setString(aNewString);
194 aNewStrings.emplace_back(xFmtStr);
196 uno::Reference< beans::XPropertySetInfo > xInfo = xFmtStr->getPropertySetInfo();
197 uno::Reference< beans::XPropertySet > xSelectionProp(xSelectionCursor, uno::UNO_QUERY);
200 for (const beans::Property& rProp : xSelectionProp->getPropertySetInfo()->getProperties())
202 if (xInfo.is() && xInfo->hasPropertyByName(rProp.Name))
204 const uno::Any aValue = xSelectionProp->getPropertyValue(rProp.Name);
205 xFmtStr->setPropertyValue(rProp.Name, aValue);
209 catch ( const uno::Exception& )
211 DBG_UNHANDLED_EXCEPTION("chart2");
212 aNewStrings.clear();
215 if (bNextPara)
216 xSelectionCursor->goRight(1, false); // next paragraph
219 return comphelper::containerToSequence(aNewStrings);
222 void ChartController::executeDispatch_InsertSpecialCharacter()
224 SolarMutexGuard aGuard;
225 if( !m_pDrawViewWrapper)
227 OSL_ENSURE( m_pDrawViewWrapper, "No DrawViewWrapper for ChartController" );
228 return;
230 if( !m_pDrawViewWrapper->IsTextEdit() )
231 StartTextEdit();
233 SvxAbstractDialogFactory * pFact = SvxAbstractDialogFactory::Create();
235 SfxAllItemSet aSet( m_pDrawModelWrapper->GetItemPool() );
236 aSet.Put( SfxBoolItem( FN_PARAM_1, false ) );
238 //set fixed current font
239 aSet.Put( SfxBoolItem( FN_PARAM_2, true ) ); //maybe not necessary in future
241 vcl::Font aCurFont = m_pDrawViewWrapper->getOutliner()->GetRefDevice()->GetFont();
242 aSet.Put( SvxFontItem( aCurFont.GetFamilyType(), aCurFont.GetFamilyName(), aCurFont.GetStyleName(), aCurFont.GetPitch(), aCurFont.GetCharSet(), SID_ATTR_CHAR_FONT ) );
244 VclPtr<SfxAbstractDialog> pDlg(pFact->CreateCharMapDialog(GetChartFrame(), aSet, nullptr));
245 pDlg->StartExecuteAsync(
246 [this, pDlg] (sal_Int32 nResult)->void
248 if (nResult == RET_OK)
250 const SfxItemSet* pSet = pDlg->GetOutputItemSet();
251 OUString aString;
252 if (pSet)
253 if (const SfxStringItem* pCharMapItem = pSet->GetItemIfSet(SID_CHARMAP))
254 aString = pCharMapItem->GetValue();
256 OutlinerView* pOutlinerView = m_pDrawViewWrapper->GetTextEditOutlinerView();
257 SdrOutliner* pOutliner = m_pDrawViewWrapper->getOutliner();
259 if(pOutliner && pOutlinerView)
261 // insert string to outliner
263 // prevent flicker
264 pOutlinerView->HideCursor();
265 pOutliner->SetUpdateLayout(false);
267 // delete current selection by inserting empty String, so current
268 // attributes become unique (sel. has to be erased anyway)
269 pOutlinerView->InsertText(OUString());
271 pOutlinerView->InsertText(aString, true);
273 ESelection aSel = pOutlinerView->GetSelection();
274 aSel.CollapseToEnd();
275 pOutlinerView->SetSelection(aSel);
277 // show changes
278 pOutliner->SetUpdateLayout(true);
279 pOutlinerView->ShowCursor();
282 pDlg->disposeOnce();
288 rtl::Reference< ::chart::AccessibleTextHelper >
289 ChartController::createAccessibleTextContext()
291 #if !ENABLE_WASM_STRIP_ACCESSIBILITY
292 return new AccessibleTextHelper( m_pDrawViewWrapper.get() );
293 #else
294 return {};
295 #endif
298 } //namespace chart
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */