summaryrefslogtreecommitdiffstats
path: root/sd/source/ui/dlg/dlgsnap.cxx
blob: 9f16a5fa5394b23fa64291e5181db0d37dcc06e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <svx/svxids.hrc>
#include <svx/svdpagv.hxx>
#include <svl/intitem.hxx>
#include <svl/itempool.hxx>
#include <svtools/unitconv.hxx>
#include <tools/debug.hxx>

#include <sdattr.hrc>
#include <View.hxx>
#include <drawdoc.hxx>
#include <dlgsnap.hxx>
#include <sdenumdef.hxx>

/**
 * dialog to adjust grid (scarcely ESO!)
 */
SdSnapLineDlg::SdSnapLineDlg(weld::Window* pWindow, const SfxItemSet& rInAttrs, ::sd::View const * pView)
    : GenericDialogController(pWindow, "modules/sdraw/ui/dlgsnap.ui", "SnapObjectDialog")
    , aUIScale(pView->GetDoc().GetUIScale())
    , m_xFtX(m_xBuilder->weld_label("xlabel"))
    , m_xMtrFldX(m_xBuilder->weld_metric_spin_button("x", FieldUnit::CM))
    , m_xFtY(m_xBuilder->weld_label("ylabel"))
    , m_xMtrFldY(m_xBuilder->weld_metric_spin_button("y", FieldUnit::CM))
    , m_xRadioGroup(m_xBuilder->weld_widget("radiogroup"))
    , m_xRbPoint(m_xBuilder->weld_radio_button("point"))
    , m_xRbVert(m_xBuilder->weld_radio_button("vert"))
    , m_xRbHorz(m_xBuilder->weld_radio_button("horz"))
    , m_xBtnDelete(m_xBuilder->weld_button("delete"))
{
    m_xRbHorz->connect_toggled(LINK(this, SdSnapLineDlg, ToggleHdl));
    m_xRbVert->connect_toggled(LINK(this, SdSnapLineDlg, ToggleHdl));
    m_xRbPoint->connect_toggled(LINK(this, SdSnapLineDlg, ToggleHdl));

    m_xBtnDelete->connect_clicked(LINK(this, SdSnapLineDlg, ClickHdl));

    FieldUnit eUIUnit = pView->GetDoc().GetUIUnit();
    SetFieldUnit(*m_xMtrFldX, eUIUnit, true);
    SetFieldUnit(*m_xMtrFldY, eUIUnit, true);

    // get WorkArea
    ::tools::Rectangle aWorkArea = pView->GetWorkArea();

    // determine PoolUnit
    SfxItemPool* pPool = rInAttrs.GetPool();
    DBG_ASSERT( pPool, "Where's the Pool?" );
    MapUnit ePoolUnit = pPool->GetMetric( SID_ATTR_FILL_HATCH );

    // #i48497# Consider page origin
    SdrPageView* pPV = pView->GetSdrPageView();
    Point aLeftTop(aWorkArea.Left()+1, aWorkArea.Top()+1);
    pPV->LogicToPagePos(aLeftTop);
    Point aRightBottom(aWorkArea.Right()-2, aWorkArea.Bottom()-2);
    pPV->LogicToPagePos(aRightBottom);

    // determine max and min values depending on
    // WorkArea, PoolUnit and FieldUnit:
    auto const map = [ePoolUnit](std::unique_ptr<weld::MetricSpinButton> const & msb, tools::Long value) {
            auto const n1 = OutputDevice::LogicToLogic(value, ePoolUnit, MapUnit::Map100thMM);
            auto const n2 = msb->normalize(n1);
            auto const n3 = msb->convert_value_from(n2, FieldUnit::MM_100TH);
            auto const n4 = msb->convert_value_to(n3, FieldUnit::NONE);
            return n4;
        };
    m_xMtrFldX->set_range(map(m_xMtrFldX, sal_Int32(aLeftTop.X() / aUIScale)),
                          map(m_xMtrFldX, sal_Int32(aRightBottom.X() / aUIScale)),
                          FieldUnit::NONE);
    m_xMtrFldY->set_range(map(m_xMtrFldY, sal_Int32(aLeftTop.Y() / aUIScale)),
                          map(m_xMtrFldY, sal_Int32(aRightBottom.Y() / aUIScale)),
                          FieldUnit::NONE);

    // set values
    nXValue = rInAttrs.Get(ATTR_SNAPLINE_X).GetValue();
    nYValue = rInAttrs.Get(ATTR_SNAPLINE_Y).GetValue();
    nXValue = sal_Int32(nXValue / aUIScale);
    nYValue = sal_Int32(nYValue / aUIScale);
    SetMetricValue(*m_xMtrFldX, nXValue, MapUnit::Map100thMM);
    SetMetricValue(*m_xMtrFldY, nYValue, MapUnit::Map100thMM);

    m_xRbPoint->set_active(true);
}

SdSnapLineDlg::~SdSnapLineDlg()
{
}

/**
 * fills provided item sets with dialog box attributes
 */
IMPL_LINK(SdSnapLineDlg, ToggleHdl, weld::Toggleable&, rBtn, void)
{
    if (!rBtn.get_active())
        return;
    if (m_xRbPoint->get_active())
        SetInputFields(true, true);
    else if (m_xRbHorz->get_active())
        SetInputFields(false, true);
    else if (m_xRbVert->get_active())
        SetInputFields(true, false);
}

IMPL_LINK( SdSnapLineDlg, ClickHdl, weld::Button&, rBtn, void )
{
    if (&rBtn == m_xBtnDelete.get())
        m_xDialog->response(RET_SNAP_DELETE);
}

/**
 * fills provided item sets with dialog box attributes
 */
void SdSnapLineDlg::GetAttr(SfxItemSet& rOutAttrs)
{
    SnapKind eKind;

    if (m_xRbHorz->get_active())      eKind = SnapKind::Horizontal;
    else if (m_xRbVert->get_active()) eKind = SnapKind::Vertical;
    else                              eKind = SnapKind::Point;

    nXValue = sal_Int32(GetCoreValue(*m_xMtrFldX, MapUnit::Map100thMM) * aUIScale);
    nYValue = sal_Int32(GetCoreValue(*m_xMtrFldY, MapUnit::Map100thMM) * aUIScale);

    rOutAttrs.Put(SfxUInt16Item(ATTR_SNAPLINE_KIND, static_cast<sal_uInt16>(eKind)));
    rOutAttrs.Put(SfxInt32Item(ATTR_SNAPLINE_X, nXValue));
    rOutAttrs.Put(SfxInt32Item(ATTR_SNAPLINE_Y, nYValue));
}

void SdSnapLineDlg::HideRadioGroup()
{
    m_xRadioGroup->hide();
}

/**
 * disable X or Y input fields
 */
void SdSnapLineDlg::SetInputFields(bool bEnableX, bool bEnableY)
{
    if ( bEnableX )
    {
        if (!m_xMtrFldX->get_sensitive())
            m_xMtrFldX->set_value(nXValue, FieldUnit::NONE);
        m_xMtrFldX->set_sensitive(true);
        m_xFtX->set_sensitive(true);
    }
    else if (m_xMtrFldX->get_sensitive())
    {
        nXValue = m_xMtrFldX->get_value(FieldUnit::NONE);
        m_xMtrFldX->set_text(OUString());
        m_xMtrFldX->set_sensitive(false);
        m_xFtX->set_sensitive(false);
    }
    if ( bEnableY )
    {
        if (!m_xMtrFldY->get_sensitive())
            m_xMtrFldY->set_value(nYValue, FieldUnit::NONE);
        m_xMtrFldY->set_sensitive(true);
        m_xFtY->set_sensitive(true);
    }
    else if (m_xMtrFldY->get_sensitive())
    {
        nYValue = m_xMtrFldY->get_value(FieldUnit::NONE);
        m_xMtrFldY->set_text(OUString());
        m_xMtrFldY->set_sensitive(false);
        m_xFtY->set_sensitive(false);
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */