Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/dialog/contwnd.cxx
Line
Count
Source
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 "contwnd.hxx"
21
#include <svx/svdpage.hxx>
22
#include <svx/svdopath.hxx>
23
#include <svx/xfillit0.hxx>
24
#include <svx/xfltrit.hxx>
25
#include <svx/xflclit.hxx>
26
#include <basegfx/polygon/b2dpolypolygontools.hxx>
27
#include <svx/sdrpaintwindow.hxx>
28
#include <vcl/ptrstyle.hxx>
29
30
using namespace css;
31
32
0
#define TRANSCOL COL_WHITE
33
34
ContourWindow::ContourWindow(weld::Dialog* pDialog)
35
0
    : GraphCtrl(pDialog)
36
0
    , aWorkRect(0, 0, 0, 0)
37
0
    , bPipetteMode(false)
38
0
    , bWorkplaceMode(false)
39
0
    , bClickValid(false)
40
0
{
41
0
}
42
43
void ContourWindow::SetPolyPolygon(const tools::PolyPolygon& rPolyPoly)
44
0
{
45
0
    SdrPage* pPage = mpModel->GetPage(0);
46
0
    const sal_uInt16 nPolyCount = rPolyPoly.Count();
47
48
    // First delete all drawing objects
49
0
    aPolyPoly = rPolyPoly;
50
51
    // To avoid to have destroyed objects which are still selected, it is necessary to deselect
52
    // them first (!)
53
0
    mpView->UnmarkAllObj();
54
55
    // clear SdrObjects with broadcasting
56
0
    pPage->ClearSdrObjList();
57
58
0
    for (sal_uInt16 i = 0; i < nPolyCount; i++)
59
0
    {
60
0
        basegfx::B2DPolyPolygon aPolyPolygon;
61
0
        aPolyPolygon.append(aPolyPoly[ i ].getB2DPolygon());
62
0
        rtl::Reference<SdrPathObj> pPathObj = new SdrPathObj(
63
0
            *mpModel,
64
0
            SdrObjKind::PathFill,
65
0
            std::move(aPolyPolygon));
66
67
0
        SfxItemSet aSet(mpModel->GetItemPool());
68
69
0
        aSet.Put(XFillStyleItem(drawing::FillStyle_SOLID));
70
0
        aSet.Put(XFillColorItem(u""_ustr, TRANSCOL));
71
0
        aSet.Put(XFillTransparenceItem(50) );
72
73
0
        pPathObj->SetMergedItemSetAndBroadcast(aSet);
74
75
0
        pPage->InsertObject( pPathObj.get() );
76
0
    }
77
78
0
    if (nPolyCount)
79
0
    {
80
0
        mpView->MarkAll();
81
0
        mpView->CombineMarkedObjects(false);
82
0
    }
83
84
0
    mpModel->SetChanged(false);
85
0
}
86
87
const tools::PolyPolygon& ContourWindow::GetPolyPolygon()
88
0
{
89
0
    if ( mpModel->IsChanged() )
90
0
    {
91
0
        SdrPage* pPage = mpModel->GetPage( 0 );
92
93
0
        aPolyPoly = tools::PolyPolygon();
94
95
0
        if ( pPage && pPage->GetObjCount() )
96
0
        {
97
0
            SdrPathObj* pPathObj = static_cast<SdrPathObj*>(pPage->GetObj(0));
98
            // Not sure if subdivision is needed for ContourWindow, but maybe it cannot handle
99
            // curves at all. Keeping subdivision here for security
100
0
            const basegfx::B2DPolyPolygon aB2DPolyPolygon(basegfx::utils::adaptiveSubdivideByAngle(pPathObj->GetPathPoly()));
101
0
            aPolyPoly = tools::PolyPolygon(aB2DPolyPolygon);
102
0
        }
103
104
0
        mpModel->SetChanged( false );
105
0
    }
106
107
0
    return aPolyPoly;
108
0
}
109
110
void ContourWindow::InitSdrModel()
111
0
{
112
0
    GraphCtrl::InitSdrModel();
113
114
0
    SfxItemSet aSet( mpModel->GetItemPool() );
115
116
0
    aSet.Put( XFillColorItem( u""_ustr, TRANSCOL ) );
117
0
    aSet.Put( XFillTransparenceItem( 50 ) );
118
0
    mpView->SetAttributes( aSet );
119
0
    mpView->SetFrameDragSingles();
120
0
}
121
122
void ContourWindow::SdrObjCreated( const SdrObject&  )
123
0
{
124
0
    mpView->MarkAll();
125
0
    mpView->CombineMarkedObjects( false );
126
0
}
127
128
bool ContourWindow::IsContourChanged() const
129
0
{
130
0
    SdrPage*    pPage = mpModel->GetPage( 0 );
131
0
    bool        bRet = false;
132
133
0
    if ( pPage && pPage->GetObjCount() )
134
0
        bRet = static_cast<SdrPathObj*>( pPage->GetObj( 0 ) )->GetPathPoly().count() && mpModel->IsChanged();
135
136
0
    return bRet;
137
0
}
138
139
bool ContourWindow::MouseButtonDown( const MouseEvent& rMEvt )
140
0
{
141
0
    if ( bWorkplaceMode )
142
0
    {
143
0
        const Point aLogPt(GetDrawingArea()->get_ref_device().PixelToLogic(rMEvt.GetPosPixel()));
144
145
0
        SetPolyPolygon( tools::PolyPolygon() );
146
0
        aWorkRect = tools::Rectangle( aLogPt, aLogPt );
147
0
        Invalidate(tools::Rectangle(Point(), GetGraphicSize()));
148
0
        SetEditMode( true );
149
0
    }
150
151
0
    if (!bPipetteMode)
152
0
        return GraphCtrl::MouseButtonDown( rMEvt );
153
154
0
    return true;
155
0
}
156
157
bool ContourWindow::MouseMove( const MouseEvent& rMEvt )
158
0
{
159
0
    bClickValid = false;
160
161
0
    if ( bPipetteMode )
162
0
    {
163
0
        const Point aLogPt( GetDrawingArea()->get_ref_device().PixelToLogic( rMEvt.GetPosPixel() ) );
164
165
0
        aPipetteColor = GetDrawingArea()->get_ref_device().GetPixel( aLogPt );
166
0
        weld::CustomWidgetController::MouseMove( rMEvt );
167
168
0
        if ( aPipetteLink.IsSet() && tools::Rectangle( Point(), GetGraphicSize() ).Contains( aLogPt ) )
169
0
        {
170
0
            SetPointer( PointerStyle::RefHand );
171
0
            aPipetteLink.Call( *this );
172
0
        }
173
174
0
        return true;
175
0
    }
176
177
0
    return GraphCtrl::MouseMove( rMEvt );
178
0
}
179
180
bool ContourWindow::MouseButtonUp(const MouseEvent& rMEvt)
181
0
{
182
0
    const tools::Rectangle aGraphRect( Point(), GetGraphicSize() );
183
0
    const Point     aLogPt( GetDrawingArea()->get_ref_device().PixelToLogic( rMEvt.GetPosPixel() ) );
184
185
0
    bClickValid = aGraphRect.Contains( aLogPt );
186
0
    ReleaseMouse();
187
188
0
    if ( bPipetteMode )
189
0
    {
190
0
        weld::CustomWidgetController::MouseButtonUp( rMEvt );
191
192
0
        aPipetteClickLink.Call( *this );
193
194
0
        return true;
195
0
    }
196
0
    else if ( bWorkplaceMode )
197
0
    {
198
0
        GraphCtrl::MouseButtonUp( rMEvt );
199
200
0
        aWorkRect.SetRight( aLogPt.X() );
201
0
        aWorkRect.SetBottom( aLogPt.Y() );
202
0
        aWorkRect.Intersection( aGraphRect );
203
0
        aWorkRect.Normalize();
204
205
0
        if ( aWorkRect.Left() != aWorkRect.Right() && aWorkRect.Top() != aWorkRect.Bottom() )
206
0
        {
207
0
            tools::PolyPolygon _aPolyPoly( GetPolyPolygon() );
208
209
0
            _aPolyPoly.Clip( aWorkRect );
210
0
            SetPolyPolygon( _aPolyPoly );
211
0
            mpView->SetWorkArea( aWorkRect );
212
0
        }
213
0
        else
214
0
            mpView->SetWorkArea( aGraphRect );
215
216
0
        Invalidate( aGraphRect );
217
218
0
        aWorkplaceClickLink.Call( *this );
219
220
0
        return false;
221
0
    }
222
223
0
    return GraphCtrl::MouseButtonUp( rMEvt );
224
0
}
225
226
void ContourWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
227
0
{
228
    // #i75482#
229
    // encapsulate the redraw using Begin/End and use the returned
230
    // data to get the target output device (e.g. when pre-rendering)
231
0
    SdrPaintWindow* pPaintWindow = mpView->BeginCompleteRedraw(&rRenderContext);
232
0
    pPaintWindow->SetOutputToWindow(true);
233
0
    OutputDevice& rTarget = pPaintWindow->GetTargetOutputDevice();
234
235
0
    const Graphic& rGraphic = GetGraphic();
236
0
    rTarget.Push(vcl::PushFlags::LINECOLOR |vcl::PushFlags::FILLCOLOR);
237
0
    rTarget.SetLineColor(COL_BLACK);
238
0
    rTarget.SetFillColor(COL_WHITE);
239
0
    rTarget.DrawRect( tools::Rectangle( Point(), GetGraphicSize() ) );
240
0
    rTarget.Pop();
241
242
0
    if (rGraphic.GetType() != GraphicType::NONE)
243
0
        rGraphic.Draw(rTarget, Point(), GetGraphicSize());
244
245
0
    if (aWorkRect.Left() != aWorkRect.Right() && aWorkRect.Top() != aWorkRect.Bottom())
246
0
    {
247
0
        tools::PolyPolygon _aPolyPoly(2);
248
0
        rTarget.Push(vcl::PushFlags::FILLCOLOR);
249
0
        _aPolyPoly.Insert(tools::Polygon(tools::Rectangle(Point(), GetGraphicSize())));
250
0
        _aPolyPoly.Insert(tools::Polygon(aWorkRect));
251
0
        rTarget.SetFillColor(COL_LIGHTRED);
252
0
        rTarget.DrawTransparent(_aPolyPoly, 50);
253
0
        rTarget.Pop();
254
0
    }
255
256
    // #i75482#
257
0
    const vcl::Region aRepaintRegion(rRect);
258
0
    mpView->DoCompleteRedraw(*pPaintWindow, aRepaintRegion);
259
0
    mpView->EndCompleteRedraw(*pPaintWindow, true);
260
0
}
261
262
void ContourWindow::SetDrawingArea(weld::DrawingArea* pDrawingArea)
263
0
{
264
0
    GraphCtrl::SetDrawingArea(pDrawingArea);
265
0
    Size aSize(pDrawingArea->get_ref_device().LogicToPixel(Size(270, 170), MapMode(MapUnit::MapAppFont)));
266
0
    pDrawingArea->set_size_request(aSize.Width(), aSize.Height());
267
0
    SetOutputSizePixel(aSize);
268
0
    SetSdrMode(true);
269
0
}
270
271
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */