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 #pragma once
20 
21 #include "TablePropertiesHandler.hxx"
22 #include "TablePositionHandler.hxx"
23 
24 #include "TableManager.hxx"
25 #include "PropertyMap.hxx"
26 #include <vector>
27 #include <memory>
28 #include <comphelper/sequenceashashmap.hxx>
29 
30 namespace writerfilter::dmapper {
31 
32 class DomainMapper;
33 
34 class DomainMapperTableManager : public TableManager
35 {
36     typedef std::shared_ptr< std::vector<sal_Int32> > IntVectorPtr;
37 
38     sal_uInt32      m_nRow;
39     ::std::vector< sal_uInt32 > m_nCell;
40     sal_uInt32      m_nGridSpan;
41     sal_Int32       m_nHeaderRepeat; //counter of repeated headers - if == -1 then the repeating stops
42     sal_Int32       m_nTableWidth; //might be set directly or has to be calculated from the column positions
43     /// Are we in a shape (text append stack is not empty) or in the body document?
44     bool m_bIsInShape;
45     std::vector< OUString > m_aTableStyleNames;
46     /// Moved table (in moveRangeFromStart...moveRangeFromEnd or moveRangeToStart...moveRangeToEnd)
47     std::vector< OUString > m_aMoved;
48     /// Grab-bag of table look attributes for preserving.
49     comphelper::SequenceAsHashMap m_aTableLook;
50     std::vector< TablePositionHandlerPtr > m_aTablePositions;
51     std::vector< TablePositionHandlerPtr > m_aTmpPosition; ///< Temporarily stores the position to compare it later
52     std::vector< TablePropertyMapPtr > m_aTmpTableProperties; ///< Temporarily stores the table properties until end of row
53 
54     ::std::vector< IntVectorPtr >  m_aTableGrid;
55     /// If this is true, then we pushed a width before the next level started, and that should be carried over when starting the next level.
56     bool            m_bPushCurrentWidth;
57     /// Individual table cell width values, used only in case the number of cells doesn't match the table grid.
58     ::std::vector< IntVectorPtr >  m_aCellWidths;
59     /// Remember if table width was already set, when we lack a w:tblW, it should be set manually at the end.
60     bool m_bTableSizeTypeInserted;
61     /// Table layout algorithm, IOW if we should consider fixed column width or not.
62     sal_uInt32 m_nLayoutType;
63     /// Collected table paragraphs for table style handling
64     std::stack< TableParagraphVectorPtr > m_aParagraphsToEndTable;
65 
66     std::unique_ptr<TablePropertiesHandler> m_pTablePropsHandler;
67     PropertyMapPtr            m_pStyleProps;
68 
69     bool shouldInsertRow(IntVectorPtr pCellWidths, IntVectorPtr pTableGrid, size_t nGrids, bool& rIsIncompleteGrid);
70 
71     virtual void clearData() override;
72 
73 public:
74 
75     DomainMapperTableManager();
76     virtual ~DomainMapperTableManager() override;
77 
78     // use this method to avoid adding the properties for the table
79     // but in the provided properties map.
SetStyleProperties(PropertyMapPtr pProperties)80     void SetStyleProperties( PropertyMapPtr pProperties ) { m_pStyleProps = pProperties; };
81 
82     virtual bool sprm(Sprm & rSprm) override;
83     bool attribute(Id nName, Value const & val);
84 
85     virtual void startLevel( ) override;
86     virtual void endLevel( ) override;
87 
88     virtual void endOfCellAction() override;
89     virtual void endOfRowAction() override;
90 
91     IntVectorPtr const & getCurrentGrid( );
92     IntVectorPtr const & getCurrentCellWidths( );
93     const TableParagraphVectorPtr & getCurrentParagraphs( );
94 
95     /// Turn the attributes collected so far in m_aTableLook into a property and clear the container.
96     void finishTableLook();
97     css::uno::Sequence<css::beans::PropertyValue> getCurrentTablePosition();
98     TablePositionHandler* getCurrentTableRealPosition();
99 
cellProps(const TablePropertyMapPtr & pProps)100     virtual void cellProps(const TablePropertyMapPtr& pProps) override
101     {
102         if ( m_pStyleProps )
103             m_pStyleProps->InsertProps(pProps.get());
104         else
105            TableManager::cellProps( pProps );
106     };
107 
insertRowProps(const TablePropertyMapPtr & pProps)108     virtual void insertRowProps(const TablePropertyMapPtr& pProps) override
109     {
110         if ( m_pStyleProps )
111             m_pStyleProps->InsertProps(pProps.get());
112         else
113            TableManager::insertRowProps( pProps );
114     };
115 
insertTableProps(const TablePropertyMapPtr & pProps)116     virtual void insertTableProps(const TablePropertyMapPtr& pProps) override
117     {
118         if ( m_pStyleProps )
119             m_pStyleProps->InsertProps(pProps.get());
120         else
121             m_aTmpTableProperties.back()->InsertProps(pProps.get());
122     };
123 
SetLayoutType(sal_uInt32 nLayoutType)124     void SetLayoutType(sal_uInt32 nLayoutType)
125     {
126         m_nLayoutType = nLayoutType;
127     }
128 
129     using TableManager::isInCell;
130 
131     void setIsInShape(bool bIsInShape);
132 
133     // moveFromRangeStart and moveToRangeStart are there
134     // in the first paragraph in the first cell of the
135     // table moved by drag & drop with track changes, but
136     // moveFromRangeEnd and moveToRangeEnd follow the
137     // table element w:tbl in the same level (not in paragraph).
138     // (Special indexing is related to the load of the tables:
139     // first-level tables handled by two levels during the
140     // import, to support table join etc. In the first cell,
141     // setMoved() writes the first level from these two levels
142     // i.e. second startLevel() hasn't been called, yet.)
143     // TODO: check drag & drop of only a part of the tables.
setMoved(OUString sMoved)144     void setMoved(OUString sMoved)
145     {
146         if ( m_aMoved.empty() )
147             return;
148 
149         if ( !sMoved.isEmpty() )
150             m_aMoved.end()[-1] = sMoved;
151         else if ( m_aMoved.size() >= 2 )
152             // next table rows weren't moved
153             m_aMoved.end()[-2] = "";
154         else
155             m_aMoved.end()[-1] = "";
156     }
157 
getMoved() const158     OUString getMoved() const
159     {
160         if ( m_aMoved.size() >= 2 && !m_aMoved.end()[-2].isEmpty() )
161            return m_aMoved.end()[-2];
162         else if ( !m_aMoved.empty() )
163            return m_aMoved.end()[-1];
164 
165         return OUString();
166     }
167 
168 };
169 
170 }
171 
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
173