xref: /aoo42x/main/svx/source/table/tablemodel.hxx (revision 3334a7e6)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SVX_TABLE_TABLEMODEL_HXX_
25 #define _SVX_TABLE_TABLEMODEL_HXX_
26 
27 #include <com/sun/star/util/XBroadcaster.hpp>
28 #include <com/sun/star/table/XTable.hpp>
29 #include <basegfx/range/b2irectangle.hxx>
30 #include <basegfx/tuple/b2ituple.hxx>
31 #include <cppuhelper/compbase2.hxx>
32 #include <comphelper/broadcasthelper.hxx>
33 #include <comphelper/listenernotification.hxx>
34 #include <tools/gen.hxx>
35 #include "celltypes.hxx"
36 
37 // -----------------------------------------------------------------------------
38 
39 namespace sdr { namespace table {
40 
41 class SdrTableObj;
42 
43 // -----------------------------------------------------------------------------
44 // ICellRange
45 // -----------------------------------------------------------------------------
46 
47 /** base class for each object implementing an XCellRange */
48 class ICellRange
49 {
50 public:
51 	virtual sal_Int32 getLeft() = 0;
52 	virtual sal_Int32 getTop() = 0;
53 	virtual sal_Int32 getRight() = 0;
54 	virtual sal_Int32 getBottom() = 0;
55 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::table::XTable > getTable() = 0;
56 };
57 
58 // -----------------------------------------------------------------------------
59 // TableModel
60 // -----------------------------------------------------------------------------
61 
62 typedef ::cppu::WeakComponentImplHelper2< ::com::sun::star::table::XTable, ::com::sun::star::util::XBroadcaster > TableModelBase;
63 
64 class TableModel : public TableModelBase,
65 				   public ::comphelper::OBaseMutex,
66 				   public ICellRange
67 {
68 	friend class InsertRowUndo;
69 	friend class RemoveRowUndo;
70 	friend class InsertColUndo;
71 	friend class RemoveColUndo;
72 	friend class TableColumnUndo;
73 	friend class TableRowUndo;
74 	friend class TableColumn;
75 	friend class TableRow;
76 	friend class TableRows;
77 	friend class TableColumns;
78 	friend class TableModelNotifyGuard;
79 
80 public:
81 	TableModel( SdrTableObj* pTableObj );
82 	TableModel( SdrTableObj* pTableObj, const TableModelRef& xSourceTable );
83 	virtual ~TableModel();
84 
85 	void init( sal_Int32 nColumns, sal_Int32 nRows );
86 
87 	SdrTableObj* getSdrTableObj() const { return mpTableObj; }
88 
89 	/** deletes rows and columns that are completly merged. Must be called between BegUndo/EndUndo! */
90 	void optimize();
91 
92     /// merges the cell at the given position with the given span
93     void merge( sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nColSpan, sal_Int32 nRowSpan );
94 
95 	// ICellRange
96 	virtual sal_Int32 getLeft();
97 	virtual sal_Int32 getTop();
98 	virtual sal_Int32 getRight();
99 	virtual sal_Int32 getBottom();
100 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::table::XTable > getTable();
101 
102 	// XTable
103     virtual ::com::sun::star::uno::Reference< ::com::sun::star::table::XCellCursor > SAL_CALL createCursor(  ) throw (::com::sun::star::uno::RuntimeException);
104     virtual ::com::sun::star::uno::Reference< ::com::sun::star::table::XCellCursor > SAL_CALL createCursorByRange( const ::com::sun::star::uno::Reference< ::com::sun::star::table::XCellRange >& Range ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
105     virtual ::sal_Int32 SAL_CALL getRowCount() throw (::com::sun::star::uno::RuntimeException);
106     virtual ::sal_Int32 SAL_CALL getColumnCount() throw (::com::sun::star::uno::RuntimeException);
107 
108     // XComponent
109     virtual void SAL_CALL dispose(  ) throw (::com::sun::star::uno::RuntimeException);
110     virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException);
111     virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException);
112 
113     // XModifiable
114     virtual ::sal_Bool SAL_CALL isModified(  ) throw (::com::sun::star::uno::RuntimeException);
115     virtual void SAL_CALL setModified( ::sal_Bool bModified ) throw (::com::sun::star::beans::PropertyVetoException, ::com::sun::star::uno::RuntimeException);
116 
117     // XModifyBroadcaster
118     virtual void SAL_CALL addModifyListener( const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener ) throw (::com::sun::star::uno::RuntimeException);
119     virtual void SAL_CALL removeModifyListener( const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener ) throw (::com::sun::star::uno::RuntimeException);
120 
121 	// XColumnRowRange
122 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::table::XTableColumns > SAL_CALL getColumns() throw (::com::sun::star::uno::RuntimeException);
123     virtual ::com::sun::star::uno::Reference< ::com::sun::star::table::XTableRows > SAL_CALL getRows() throw (::com::sun::star::uno::RuntimeException);
124 
125 	// XCellRange
126 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::table::XCell > SAL_CALL getCellByPosition( ::sal_Int32 nColumn, ::sal_Int32 nRow ) throw ( ::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
127 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::table::XCellRange > SAL_CALL getCellRangeByPosition( ::sal_Int32 nLeft, ::sal_Int32 nTop, ::sal_Int32 nRight, ::sal_Int32 nBottom ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
128 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::table::XCellRange > SAL_CALL getCellRangeByName( const ::rtl::OUString& aRange ) throw (::com::sun::star::uno::RuntimeException);
129 
130     // XPropertySet
131     virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo(  ) throw (::com::sun::star::uno::RuntimeException);
132     virtual void SAL_CALL setPropertyValue( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Any& aValue ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
133     virtual ::com::sun::star::uno::Any SAL_CALL getPropertyValue( const ::rtl::OUString& PropertyName ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
134     virtual void SAL_CALL addPropertyChangeListener( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
135     virtual void SAL_CALL removePropertyChangeListener( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& aListener ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
136     virtual void SAL_CALL addVetoableChangeListener( const ::rtl::OUString& PropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& aListener ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
137     virtual void SAL_CALL removeVetoableChangeListener( const ::rtl::OUString& PropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& aListener ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
138 
139     // XFastPropertySet
140     virtual void SAL_CALL setFastPropertyValue( ::sal_Int32 nHandle, const ::com::sun::star::uno::Any& aValue ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
141     virtual ::com::sun::star::uno::Any SAL_CALL getFastPropertyValue( ::sal_Int32 nHandle ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
142 
143 	// XBroadcaster
144 	virtual void SAL_CALL lockBroadcasts() throw (::com::sun::star::uno::RuntimeException);
145 	virtual void SAL_CALL unlockBroadcasts() throw (::com::sun::star::uno::RuntimeException);
146 
147 protected:
148 	void notifyModification();
149 
150 	void insertColumns( sal_Int32 nIndex, sal_Int32 nCount );
151 	void removeColumns( sal_Int32 nIndex, sal_Int32 nCount );
152 	void insertRows( sal_Int32 nIndex, sal_Int32 nCount );
153 	void removeRows( sal_Int32 nIndex, sal_Int32 nCount );
154 
155 	sal_Int32 getRowCountImpl() const;
156 	sal_Int32 getColumnCountImpl() const;
157 
158 	CellRef createCell();
159 	CellRef getCell( ::sal_Int32 nCol, ::sal_Int32 nRow ) const;
160 
161 	void UndoInsertRows( sal_Int32 nIndex, sal_Int32 nCount );
162 	void UndoRemoveRows( sal_Int32 nIndex, RowVector& aNewRows );
163 
164 	void UndoInsertColumns( sal_Int32 nIndex, sal_Int32 nCount );
165 	void UndoRemoveColumns( sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells );
166 
167 private:
168 	/** this function is called upon disposing the component
169     */
170     virtual void SAL_CALL disposing();
171 
172 	TableRowRef getRow( sal_Int32 nRow ) const throw (::com::sun::star::lang::IndexOutOfBoundsException);
173 	TableColumnRef getColumn( sal_Int32 nColumn ) const throw (::com::sun::star::lang::IndexOutOfBoundsException);
174 
175     void updateRows();
176     void updateColumns();
177 
178 	RowVector		maRows;
179 	ColumnVector	maColumns;
180 
181 	TableColumnsRef mxTableColumns;
182 	TableRowsRef mxTableRows;
183 
184 	SdrTableObj* mpTableObj;
185 
186 	sal_Bool mbModified;
187 	bool mbNotifyPending;
188 
189 	sal_Int32 mnNotifyLock;
190 };
191 
192 class TableModelNotifyGuard
193 {
194 public:
195 	TableModelNotifyGuard( TableModel* pModel )
196 	: mxBroadcaster( static_cast< ::com::sun::star::util::XBroadcaster* >( pModel ) )
197 	{
198 		if( mxBroadcaster.is() )
199 			mxBroadcaster->lockBroadcasts();
200 	}
201 
202 	TableModelNotifyGuard( ::com::sun::star::uno::XInterface* pInterface )
203 	: mxBroadcaster( pInterface, ::com::sun::star::uno::UNO_QUERY )
204 	{
205 		if( mxBroadcaster.is() )
206 			mxBroadcaster->lockBroadcasts();
207 	}
208 
209 	~TableModelNotifyGuard()
210 	{
211 		if( mxBroadcaster.is() )
212 			mxBroadcaster->unlockBroadcasts();
213 	}
214 
215 private:
216 	com::sun::star::uno::Reference< ::com::sun::star::util::XBroadcaster > mxBroadcaster;
217 };
218 
219 } }
220 
221 #endif
222