| 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 |
|
| 28 |
#include <comphelper/diagnose_ex.hxx> |
| 29 |
|
| 30 |
using namespace ::com::sun::star; |
| 31 |
|
| 32 |
using ::com::sun::star::uno::Reference; |
| 33 |
|
| 34 |
namespace chart |
| 35 |
{ |
| 36 |
|
| 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 ) |
| 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 |
|
| 48 |
UndoGuard::~UndoGuard() |
| 49 |
{ |
| 50 |
if ( m_pDocumentSnapshot ) |
| 51 |
discardSnapshot(); |
| 52 |
} |
| 53 |
|
| 54 |
void 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 |
{ |
| 66 |
DBG_UNHANDLED_EXCEPTION("chart2"); |
| 67 |
} |
| 68 |
} |
| 69 |
m_bActionPosted = true; |
| 70 |
} |
| 71 |
|
| 72 |
void UndoGuard::rollback() |
| 73 |
{ |
| 74 |
ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" ); |
| 75 |
m_pDocumentSnapshot->applyToModel( m_xChartModel ); |
| 76 |
discardSnapshot(); |
| 77 |
} |
| 78 |
|
| 79 |
void UndoGuard::discardSnapshot() |
| 80 |
{ |
| 81 |
ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" ); |
| 82 |
m_pDocumentSnapshot->dispose(); |
| 83 |
m_pDocumentSnapshot.reset(); |
| 84 |
} |
| 85 |
|
| 86 |
UndoLiveUpdateGuard::UndoLiveUpdateGuard( const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager ) |
| 87 |
:UndoGuard( i_undoString, i_undoManager, E_MODEL ) |
| 88 |
{ |
| 89 |
} |
| 90 |
|
| 91 |
UndoLiveUpdateGuard::~UndoLiveUpdateGuard() |
| 92 |
{ |
| 93 |
if ( !isActionPosted() ) |
| 94 |
rollback(); |
| 95 |
} |
| 96 |
|
| 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 ) |
| 100 |
{ |
| 101 |
} |
| 102 |
|
| 103 |
UndoLiveUpdateGuardWithData::~UndoLiveUpdateGuardWithData() |
| 104 |
{ |
| 105 |
if ( !isActionPosted() ) |
| 106 |
rollback(); |
| 107 |
} |
| 108 |
|
| 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 ) |
| 112 |
{ |
| 113 |
} |
| 114 |
|
| 115 |
UndoGuardWithSelection::~UndoGuardWithSelection() |
| 116 |
{ |
| 117 |
if ( !isActionPosted() ) |
| 118 |
rollback(); |
| 119 |
} |
| 120 |
|
| 121 |
HiddenUndoContext::HiddenUndoContext( const Reference< document::XUndoManager > & i_undoManager ) |
| 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 |
|
| 137 |
HiddenUndoContext::~HiddenUndoContext() |
| 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: */ |