Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / sc / source / core / tool / chartlock.cxx
blob6b55b0bf7ac9e765268a805812ea4af339d5ae24
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 <svx/svditer.hxx>
21 #include <svx/svdoole2.hxx>
22 #include <svx/svdpage.hxx>
23 #include <comphelper/diagnose_ex.hxx>
25 #include <chartlock.hxx>
26 #include <document.hxx>
27 #include <drwlayer.hxx>
29 #include <com/sun/star/embed/XEmbeddedObject.hpp>
30 #include <com/sun/star/embed/XComponentSupplier.hpp>
31 #include <com/sun/star/frame/XModel.hpp>
33 using namespace com::sun::star;
34 using ::com::sun::star::uno::Reference;
35 using ::com::sun::star::uno::WeakReference;
37 #define SC_CHARTLOCKTIMEOUT 660
39 namespace
42 std::vector< WeakReference< frame::XModel > > lcl_getAllLivingCharts( ScDocument* pDoc )
44 std::vector< WeakReference< frame::XModel > > aRet;
45 if( !pDoc )
46 return aRet;
47 ScDrawLayer* pDrawLayer = pDoc->GetDrawLayer();
48 if (!pDrawLayer)
49 return aRet;
51 for (SCTAB nTab=0; nTab<=pDoc->GetMaxTableNumber(); nTab++)
53 if (pDoc->HasTable(nTab))
55 SdrPage* pPage = pDrawLayer->GetPage(static_cast<sal_uInt16>(nTab));
56 OSL_ENSURE(pPage,"Page ?");
58 SdrObjListIter aIter( pPage, SdrIterMode::DeepNoGroups );
59 SdrObject* pObject = aIter.Next();
60 while (pObject)
62 if( ScDocument::IsChart( pObject ) )
64 uno::Reference< embed::XEmbeddedObject > xIPObj = static_cast<SdrOle2Obj*>(pObject)->GetObjRef();
65 uno::Reference< embed::XComponentSupplier > xCompSupp = xIPObj;
66 if( xCompSupp.is())
68 Reference< frame::XModel > xModel( xCompSupp->getComponent(), uno::UNO_QUERY );
69 if( xModel.is() )
70 aRet.emplace_back(xModel );
73 pObject = aIter.Next();
77 return aRet;
80 }//end anonymous namespace
82 // ScChartLockGuard
83 ScChartLockGuard::ScChartLockGuard( ScDocument* pDoc ) :
84 maChartModels( lcl_getAllLivingCharts( pDoc ) )
86 for( const auto& rxChartModel : maChartModels )
88 try
90 Reference< frame::XModel > xModel( rxChartModel );
91 if( xModel.is())
92 xModel->lockControllers();
94 catch ( uno::Exception& )
96 TOOLS_WARN_EXCEPTION( "sc", "Unexpected exception in ScChartLockGuard");
101 ScChartLockGuard::~ScChartLockGuard()
103 for( const auto& rxChartModel : maChartModels )
107 Reference< frame::XModel > xModel( rxChartModel );
108 if( xModel.is())
109 xModel->unlockControllers();
111 catch ( uno::Exception& )
113 TOOLS_WARN_EXCEPTION( "sc", "Unexpected exception in ScChartLockGuard");
118 void ScChartLockGuard::AlsoLockThisChart( const Reference< frame::XModel >& xModel )
120 if(!xModel.is())
121 return;
123 WeakReference< frame::XModel > xWeakModel(xModel);
125 std::vector< WeakReference< frame::XModel > >::iterator aFindIter(
126 ::std::find( maChartModels.begin(), maChartModels.end(), xWeakModel ) );
128 if( aFindIter == maChartModels.end() )
132 xModel->lockControllers();
133 maChartModels.emplace_back(xModel );
135 catch ( uno::Exception& )
137 TOOLS_WARN_EXCEPTION( "sc", "Unexpected exception in ScChartLockGuard");
142 // ScTemporaryChartLock
143 ScTemporaryChartLock::ScTemporaryChartLock( ScDocument* pDocP ) :
144 mpDoc( pDocP ), maTimer("ScTemporaryChartLock maTimer")
146 maTimer.SetTimeout( SC_CHARTLOCKTIMEOUT );
147 maTimer.SetInvokeHandler( LINK( this, ScTemporaryChartLock, TimeoutHdl ) );
150 ScTemporaryChartLock::~ScTemporaryChartLock()
152 mpDoc = nullptr;
153 StopLocking();
156 void ScTemporaryChartLock::StartOrContinueLocking()
158 if (!mapScChartLockGuard)
159 mapScChartLockGuard.reset( new ScChartLockGuard(mpDoc) );
160 maTimer.Start();
163 void ScTemporaryChartLock::StopLocking()
165 maTimer.Stop();
166 mapScChartLockGuard.reset();
169 void ScTemporaryChartLock::AlsoLockThisChart( const Reference< frame::XModel >& xModel )
171 if (mapScChartLockGuard)
172 mapScChartLockGuard->AlsoLockThisChart( xModel );
175 IMPL_LINK_NOARG(ScTemporaryChartLock, TimeoutHdl, Timer *, void)
177 mapScChartLockGuard.reset();
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */