tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / view / gridmerg.cxx
blob117b3e1ad79a53a4caf987168180b176df97c438
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 <sal/config.h>
22 #include <vcl/lineinfo.hxx>
23 #include <vcl/outdev.hxx>
25 #include <gridmerg.hxx>
27 #define PAGEBREAK_LINE_DISTANCE_PIXEL 5
28 #define PAGEBREAK_LINE_DASH_LEN_PIXEL 5
29 #define PAGEBREAK_LINE_DASH_COUNT 1
31 ScGridMerger::ScGridMerger( OutputDevice* pOutDev, tools::Long nOnePixelX, tools::Long nOnePixelY )
32 : pDev(pOutDev)
33 , nOneX(nOnePixelX)
34 , nOneY(nOnePixelY)
35 , nFixStart(0)
36 , nFixEnd(0)
37 , nVarStart(0)
38 , nVarDiff(0)
39 , nCount(0)
40 , bVertical(false)
42 // optimize (DrawGrid) only for pixel MapMode,
43 // to avoid rounding errors
45 bOptimize = ( pDev->GetMapMode().GetMapUnit() == MapUnit::MapPixel );
48 ScGridMerger::~ScGridMerger()
50 Flush();
53 void ScGridMerger::AddLine( tools::Long nStart, tools::Long nEnd, tools::Long nPos )
55 if ( nCount )
57 // not first line - test fix position
58 // more than one previous line - test distance
60 if ( nStart != nFixStart || nEnd != nFixEnd )
62 if ( nCount == 1 && nPos == nVarStart &&
63 ( nStart == nFixEnd ||
64 nStart == nFixEnd + ( bVertical ? nOneY : nOneX ) ) )
66 // additional optimization: extend connected lines
67 // keep nCount at 1
68 nFixEnd = nEnd;
70 else
71 Flush();
73 else if ( nCount == 1 )
75 nVarDiff = nPos - nVarStart;
76 ++nCount;
78 else if ( nPos != nVarStart + nCount * nVarDiff ) //! keep VarEnd?
79 Flush();
80 else
81 ++nCount;
84 if ( !nCount )
86 // first line (or flushed above) - just store
88 nFixStart = nStart;
89 nFixEnd = nEnd;
90 nVarStart = nPos;
91 nVarDiff = 0;
92 nCount = 1;
96 void ScGridMerger::AddHorLine(bool bWorksInPixels, tools::Long nX1, tools::Long nX2, tools::Long nY, bool bDashed)
98 if ( bWorksInPixels )
100 Point aPoint(pDev->PixelToLogic(Point(nX1, nY)));
101 nX1 = aPoint.X();
102 nY = aPoint.Y();
103 nX2 = pDev->PixelToLogic(Point(nX2, 0)).X();
106 if ( bDashed )
108 // If there are some unflushed lines they must be flushed since
109 // new line is of different style
110 if (bOptimize) {
111 Flush();
112 bVertical = false;
115 LineInfo aLineInfo(LineStyle::Dash, 1);
116 aLineInfo.SetDashCount( PAGEBREAK_LINE_DASH_COUNT );
118 // Calculating logic values of DashLen and Distance from fixed pixel values
119 Size aDashDistanceLen( pDev->PixelToLogic( Size( PAGEBREAK_LINE_DISTANCE_PIXEL,
120 PAGEBREAK_LINE_DASH_LEN_PIXEL )));
122 aLineInfo.SetDistance( aDashDistanceLen.Width() );
123 aLineInfo.SetDashLen( aDashDistanceLen.Height() );
125 pDev->DrawLine( Point( nX1, nY ), Point( nX2, nY ), aLineInfo );
127 else if ( bOptimize )
129 if ( bVertical )
131 Flush();
132 bVertical = false;
134 AddLine( nX1, nX2, nY );
136 else
137 pDev->DrawLine( Point( nX1, nY ), Point( nX2, nY ) );
140 void ScGridMerger::AddVerLine(bool bWorksInPixels, tools::Long nX, tools::Long nY1, tools::Long nY2, bool bDashed)
142 if (bWorksInPixels)
144 Point aPoint(pDev->PixelToLogic(Point(nX, nY1)));
145 nX = aPoint.X();
146 nY1 = aPoint.Y();
147 nY2 = pDev->PixelToLogic(Point(0, nY2)).Y();
150 if ( bDashed )
152 // If there are some unflushed lines they must be flushed since
153 // new line is of different style
154 if (bOptimize) {
155 Flush();
156 bVertical = false;
159 LineInfo aLineInfo(LineStyle::Dash, 1);
160 aLineInfo.SetDashCount( PAGEBREAK_LINE_DASH_COUNT );
162 // Calculating logic values of DashLen and Distance from fixed pixel values
163 Size aDashDistanceLen( pDev->PixelToLogic( Size( PAGEBREAK_LINE_DISTANCE_PIXEL,
164 PAGEBREAK_LINE_DASH_LEN_PIXEL )));
166 aLineInfo.SetDistance( aDashDistanceLen.Width() );
167 aLineInfo.SetDashLen( aDashDistanceLen.Height() );
169 pDev->DrawLine( Point( nX, nY1 ), Point( nX, nY2 ), aLineInfo);
171 else if ( bOptimize )
173 if ( !bVertical )
175 Flush();
176 bVertical = true;
178 AddLine( nY1, nY2, nX );
180 else
181 pDev->DrawLine( Point( nX, nY1 ), Point( nX, nY2 ) );
184 void ScGridMerger::Flush()
186 if (!nCount)
187 return;
189 if (bVertical)
191 if ( nCount == 1 )
192 pDev->DrawLine( Point( nVarStart, nFixStart ), Point( nVarStart, nFixEnd ) );
193 else
195 tools::Long nVarEnd = nVarStart + ( nCount - 1 ) * nVarDiff;
196 if ( nVarDiff < 0 )
198 // nVarDiff is negative in RTL layout mode
199 // Change the positions so DrawGrid is called with a positive distance
200 // (nVarStart / nVarDiff can be modified, aren't used after Flush)
202 nVarDiff = -nVarDiff;
203 std::swap( nVarStart, nVarEnd );
205 pDev->DrawGrid( tools::Rectangle( nVarStart, nFixStart, nVarEnd, nFixEnd ),
206 Size( nVarDiff, nFixEnd - nFixStart ),
207 DrawGridFlags::VertLines );
210 else
212 if ( nCount == 1 )
213 pDev->DrawLine( Point( nFixStart, nVarStart ), Point( nFixEnd, nVarStart ) );
214 else
216 tools::Long nVarEnd = nVarStart + ( nCount - 1 ) * nVarDiff;
217 pDev->DrawGrid( tools::Rectangle( nFixStart, nVarStart, nFixEnd, nVarEnd ),
218 Size( nFixEnd - nFixStart, nVarDiff ),
219 DrawGridFlags::HorzLines );
222 nCount = 0;
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */