Branch data Line data Source code
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 <modeltoviewhelper.hxx>
21 : :
22 : : /** Converts a model position into a view position
23 : : */
24 : 5410 : sal_uInt32 ModelToViewHelper::ConvertToViewPosition( sal_uInt32 nModelPos ) const
25 : : {
26 : 5410 : sal_uInt32 nRet = nModelPos;
27 : :
28 : : // Search for entry after nPos:
29 : 5410 : ConversionMap::const_iterator aIter;
30 [ + - ][ + - ]: 7251 : for ( aIter = m_aMap.begin(); aIter != m_aMap.end(); ++aIter )
[ + + ]
31 : : {
32 [ + - ][ + + ]: 4277 : if ( (*aIter).first >= nModelPos )
33 : : {
34 [ + - ]: 2436 : const sal_uInt32 nPosModel = (*aIter).first;
35 [ + - ]: 2436 : const sal_uInt32 nPosExpand = (*aIter).second;
36 : :
37 : 2436 : const sal_uInt32 nDistToNextModel = nPosModel - nModelPos;
38 : 2436 : nRet = nPosExpand - nDistToNextModel;
39 : 2436 : break;
40 : : }
41 : : }
42 : :
43 : 5410 : return nRet;
44 : : }
45 : :
46 : :
47 : : /** Converts a view position into a model position
48 : : */
49 : 119393 : ModelToViewHelper::ModelPosition ModelToViewHelper::ConvertToModelPosition( sal_uInt32 nViewPos ) const
50 : : {
51 : 119393 : ModelPosition aRet;
52 : 119393 : aRet.mnPos = nViewPos;
53 : :
54 : : // Search for entry after nPos:
55 : 119393 : ConversionMap::const_iterator aIter;
56 [ + - ][ + - ]: 123864 : for ( aIter = m_aMap.begin(); aIter != m_aMap.end(); ++aIter )
[ + + ]
57 : : {
58 [ + - ][ + + ]: 4471 : if ( (*aIter).second > nViewPos )
59 : : {
60 [ + - ]: 1483 : const sal_uInt32 nPosModel = (*aIter).first;
61 [ + - ]: 1483 : const sal_uInt32 nPosExpand = (*aIter).second;
62 : :
63 : : // If nViewPos is in front of first field, we are finished.
64 [ + - ][ + + ]: 1483 : if ( aIter == m_aMap.begin() )
65 : 253 : break;
66 : :
67 [ + - ]: 1230 : --aIter;
68 : :
69 : : // nPrevPosModel is the field position
70 [ + - ]: 1230 : const sal_uInt32 nPrevPosModel = (*aIter).first;
71 [ + - ]: 1230 : const sal_uInt32 nPrevPosExpand = (*aIter).second;
72 : :
73 : 1230 : const sal_uInt32 nLengthModel = nPosModel - nPrevPosModel;
74 : 1230 : const sal_uInt32 nLengthExpand = nPosExpand - nPrevPosExpand;
75 : :
76 : 1230 : const sal_uInt32 nFieldLengthExpand = nLengthExpand - nLengthModel + 1;
77 : 1230 : const sal_uInt32 nFieldEndExpand = nPrevPosExpand + nFieldLengthExpand;
78 : :
79 : : // Check if nPos is outside of field:
80 [ + + ]: 1230 : if ( nFieldEndExpand <= nViewPos )
81 : : {
82 : : // nPos is outside of field:
83 : 1099 : const sal_uInt32 nDistToField = nViewPos - nFieldEndExpand + 1;
84 : 1099 : aRet.mnPos = nPrevPosModel + nDistToField;
85 : : }
86 : : else
87 : : {
88 : : // nViewPos is inside a field:
89 : 131 : aRet.mnPos = nPrevPosModel;
90 : 131 : aRet.mnSubPos = nViewPos - nPrevPosExpand;
91 : 131 : aRet.mbIsField = true;
92 : : }
93 : :
94 : 1230 : break;
95 : : }
96 : : }
97 : :
98 : 119393 : return aRet;
99 : : }
100 : :
101 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|