LibreOffice Module chart2 (master) 1
UndoGuard.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 "UndoGuard.hxx"
21#include "ChartModelClone.hxx"
22#include "UndoActions.hxx"
23#include <ChartModel.hxx>
24
25#include <com/sun/star/document/XUndoManager.hpp>
26#include <utility>
27
29
30using namespace ::com::sun::star;
31
32using ::com::sun::star::uno::Reference;
33
34namespace chart
35{
36
37UndoGuard::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 )
42{
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 );
46}
47
48UndoGuard::~UndoGuard()
49{
50 if ( m_pDocumentSnapshot )
51 discardSnapshot();
52}
53
54void UndoGuard::commit()
55{
56 if ( !m_bActionPosted && m_pDocumentSnapshot )
57 {
58 try
59 {
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 );
63 }
64 catch( const uno::Exception& )
65 {
67 }
68 }
69 m_bActionPosted = true;
70}
71
72void UndoGuard::rollback()
73{
74 ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" );
75 m_pDocumentSnapshot->applyToModel( m_xChartModel );
76 discardSnapshot();
77}
78
79void UndoGuard::discardSnapshot()
80{
81 ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" );
82 m_pDocumentSnapshot->dispose();
83 m_pDocumentSnapshot.reset();
84}
85
86UndoLiveUpdateGuard::UndoLiveUpdateGuard( const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
87 :UndoGuard( i_undoString, i_undoManager, E_MODEL )
88{
89}
90
92{
93 if ( !isActionPosted() )
94 rollback();
95}
96
98 const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
99 :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_DATA )
100{
101}
102
104{
105 if ( !isActionPosted() )
106 rollback();
107}
108
110 const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
111 :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_SELECTION )
112{
113}
114
116{
117 if ( !isActionPosted() )
118 rollback();
119}
120
122 :m_xUndoManager( i_undoManager )
123{
124 ENSURE_OR_THROW( m_xUndoManager.is(), "invalid undo manager!" );
125 try
126 {
127 m_xUndoManager->enterHiddenUndoContext();
128 }
129 catch( const uno::Exception& )
130 {
131 DBG_UNHANDLED_EXCEPTION("chart2");
132 m_xUndoManager.clear();
133 // prevents the leaveUndoContext in the dtor
134 }
135}
136
138{
139 try
140 {
141 if ( m_xUndoManager.is() )
142 m_xUndoManager->leaveUndoContext();
143 }
144 catch( const uno::Exception& )
145 {
146 DBG_UNHANDLED_EXCEPTION("chart2");
147 }
148}
149
150} // namespace chart
151
152/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
HiddenUndoContext(const css::uno::Reference< css::document::XUndoManager > &i_undoManager)
Definition: UndoGuard.cxx:121
css::uno::Reference< css::document::XUndoManager > m_xUndoManager
Definition: UndoGuard.hxx:110
UndoGuardWithSelection(const OUString &i_undoMessage, const css::uno::Reference< css::document::XUndoManager > &i_undoManager)
Definition: UndoGuard.cxx:109
A guard which does nothing, unless you explicitly call commitAction.
Definition: UndoGuard.hxx:37
bool isActionPosted() const
Definition: UndoGuard.hxx:50
UndoGuard(OUString i_undoMessage, const css::uno::Reference< css::document::XUndoManager > &i_undoManager, const ModelFacet i_facet=E_MODEL)
UndoLiveUpdateGuardWithData(const OUString &i_undoMessage, const css::uno::Reference< css::document::XUndoManager > &i_undoManager)
Definition: UndoGuard.cxx:97
#define ENSURE_OR_THROW(c, m)
#define DBG_UNHANDLED_EXCEPTION(...)
#define ENSURE_OR_RETURN_VOID(c, m)
@ E_MODEL_WITH_SELECTION
@ E_MODEL_WITH_DATA