tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / chart2 / source / controller / main / UndoGuard.cxx
blob4e870c36d07df288ae2d20ac1ba91f05bd7ee6e5
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 "UndoGuard.hxx"
21 #include "ChartModelClone.hxx"
22 #include "UndoActions.hxx"
23 #include <ChartModel.hxx>
25 #include <com/sun/star/document/XUndoManager.hpp>
26 #include <utility>
28 #include <comphelper/diagnose_ex.hxx>
30 using namespace ::com::sun::star;
32 using ::com::sun::star::uno::Reference;
34 namespace chart
37 UndoGuard::UndoGuard( OUString i_undoString, const uno::Reference< document::XUndoManager > & i_undoManager,
38 const ModelFacet i_facet )
39 :m_xUndoManager( i_undoManager )
40 ,m_aUndoString(std::move( i_undoString ))
41 ,m_bActionPosted( false )
43 m_xChartModel = dynamic_cast<::chart::ChartModel*>(i_undoManager->getParent().get());
44 assert(m_xChartModel);
45 m_pDocumentSnapshot = std::make_shared<ChartModelClone>( m_xChartModel, i_facet );
48 UndoGuard::~UndoGuard()
50 if ( m_pDocumentSnapshot )
51 discardSnapshot();
54 void UndoGuard::commit()
56 if ( !m_bActionPosted && m_pDocumentSnapshot )
58 try
60 const Reference< document::XUndoAction > xAction( new impl::UndoElement( m_aUndoString, m_xChartModel, m_pDocumentSnapshot ) );
61 m_pDocumentSnapshot.reset(); // don't dispose, it's data went over to the UndoElement
62 m_xUndoManager->addUndoAction( xAction );
64 catch( const uno::Exception& )
66 DBG_UNHANDLED_EXCEPTION("chart2");
69 m_bActionPosted = true;
72 void UndoGuard::rollback()
74 ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" );
75 m_pDocumentSnapshot->applyToModel( m_xChartModel );
76 discardSnapshot();
79 void UndoGuard::discardSnapshot()
81 ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" );
82 m_pDocumentSnapshot->dispose();
83 m_pDocumentSnapshot.reset();
86 UndoLiveUpdateGuard::UndoLiveUpdateGuard( const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
87 :UndoGuard( i_undoString, i_undoManager, E_MODEL )
91 UndoLiveUpdateGuard::~UndoLiveUpdateGuard()
93 if ( !isActionPosted() )
94 rollback();
97 UndoLiveUpdateGuardWithData::UndoLiveUpdateGuardWithData(
98 const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
99 :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_DATA )
103 UndoLiveUpdateGuardWithData::~UndoLiveUpdateGuardWithData()
105 if ( !isActionPosted() )
106 rollback();
109 UndoGuardWithSelection::UndoGuardWithSelection(
110 const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
111 :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_SELECTION )
115 UndoGuardWithSelection::~UndoGuardWithSelection()
117 if ( !isActionPosted() )
118 rollback();
121 HiddenUndoContext::HiddenUndoContext( const Reference< document::XUndoManager > & i_undoManager )
122 :m_xUndoManager( i_undoManager )
124 ENSURE_OR_THROW( m_xUndoManager.is(), "invalid undo manager!" );
127 m_xUndoManager->enterHiddenUndoContext();
129 catch( const uno::Exception& )
131 DBG_UNHANDLED_EXCEPTION("chart2");
132 m_xUndoManager.clear();
133 // prevents the leaveUndoContext in the dtor
137 HiddenUndoContext::~HiddenUndoContext()
141 if ( m_xUndoManager.is() )
142 m_xUndoManager->leaveUndoContext();
144 catch( const uno::Exception& )
146 DBG_UNHANDLED_EXCEPTION("chart2");
150 } // namespace chart
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */