summaryrefslogtreecommitdiffstats
path: root/sd/source/core/ThemeColorChanger.cxx
blob: 70d14bc3743860ce4d3e6818258d0964ab93f6c9 (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
186
187
188
189
190
191
192
/* -*- 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/.
 */

#include <editeng/colritem.hxx>
#include <theme/ThemeColorChanger.hxx>
#include <svx/theme/ThemeColorChangerCommon.hxx>
#include <svx/svdmodel.hxx>
#include <svx/svditer.hxx>
#include <docmodel/theme/Theme.hxx>
#include <DrawDocShell.hxx>
#include <stlsheet.hxx>
#include <svx/xlnclit.hxx>
#include <svx/xflclit.hxx>
#include <svx/xdef.hxx>
#include <svx/dialmgr.hxx>
#include <svx/strings.hrc>
#include <editeng/eeitem.hxx>

#include <unchss.hxx>
#include <ViewShell.hxx>
#include <ViewShellBase.hxx>
#include <undo/undomanager.hxx>
#include <UndoThemeChange.hxx>

using namespace css;

namespace sd
{
ThemeColorChanger::ThemeColorChanger(SdrPage* pMasterPage, sd::DrawDocShell* pDocShell)
    : mpMasterPage(pMasterPage)
    , mpDocShell(pDocShell)
{
}

ThemeColorChanger::~ThemeColorChanger() = default;

namespace
{
void changeThemeColors(sd::DrawDocShell* pDocShell, SdrPage* pMasterPage,
                       std::shared_ptr<model::ColorSet> const& pNewColorSet)
{
    auto pTheme = pMasterPage->getSdrPageProperties().getTheme();
    if (!pTheme)
    {
        pTheme = std::make_shared<model::Theme>("Office");
        pMasterPage->getSdrPageProperties().setTheme(pTheme);
    }

    std::shared_ptr<model::ColorSet> const& pOldColorSet = pTheme->getColorSet();

    auto* pUndoManager = pDocShell->GetUndoManager();
    if (pUndoManager)
    {
        pUndoManager->AddUndoAction(std::make_unique<UndoThemeChange>(
            pDocShell->GetDoc(), pMasterPage, pOldColorSet, pNewColorSet));
    }

    pTheme->setColorSet(pNewColorSet);
}

bool changeStyle(sd::DrawDocShell* pDocShell, SdStyleSheet* pStyle,
                 std::shared_ptr<model::ColorSet> const& pColorSet)
{
    bool bChanged = false;

    auto aItemSet = pStyle->GetItemSet();
    if (const XFillColorItem* pItem = aItemSet.GetItemIfSet(XATTR_FILLCOLOR, false))
    {
        model::ComplexColor const& rComplexColor = pItem->getComplexColor();
        if (rComplexColor.isValidThemeType())
        {
            Color aNewColor = pColorSet->resolveColor(rComplexColor);
            std::unique_ptr<XFillColorItem> pNewItem(pItem->Clone());
            pNewItem->SetColorValue(aNewColor);
            aItemSet.Put(*pNewItem);
            bChanged = true;
        }
    }
    if (const XLineColorItem* pItem = aItemSet.GetItemIfSet(XATTR_LINECOLOR, false))
    {
        model::ComplexColor const& rComplexColor = pItem->getComplexColor();
        if (rComplexColor.isValidThemeType())
        {
            Color aNewColor = pColorSet->resolveColor(rComplexColor);
            std::unique_ptr<XLineColorItem> pNewItem(pItem->Clone());
            pNewItem->SetColorValue(aNewColor);
            aItemSet.Put(*pNewItem);
            bChanged = true;
        }
    }
    if (const SvxColorItem* pItem = aItemSet.GetItemIfSet(EE_CHAR_COLOR, false))
    {
        model::ComplexColor const& rComplexColor = pItem->getComplexColor();
        if (rComplexColor.isValidThemeType())
        {
            Color aNewColor = pColorSet->resolveColor(rComplexColor);
            std::unique_ptr<SvxColorItem> pNewItem(pItem->Clone());
            pNewItem->setColor(aNewColor);
            aItemSet.Put(*pNewItem);
            bChanged = true;
        }
    }
    if (bChanged)
    {
        pDocShell->GetUndoManager()->AddUndoAction(
            std::make_unique<StyleSheetUndoAction>(pDocShell->GetDoc(), pStyle, &aItemSet));
        pStyle->GetItemSet().Put(aItemSet);
        pStyle->Broadcast(SfxHint(SfxHintId::DataChanged));
    }
    return bChanged;
}

bool changeStyles(sd::DrawDocShell* pDocShell, std::shared_ptr<model::ColorSet> const& pColorSet)
{
    bool bChanged = false;
    SfxStyleSheetBasePool* pPool = pDocShell->GetStyleSheetPool();

    SdStyleSheet* pStyle = static_cast<SdStyleSheet*>(pPool->First(SfxStyleFamily::Para));
    while (pStyle)
    {
        bChanged = changeStyle(pDocShell, pStyle, pColorSet) || bChanged;
        pStyle = static_cast<SdStyleSheet*>(pPool->Next());
    }

    return bChanged;
}

} // end anonymous ns

void ThemeColorChanger::apply(std::shared_ptr<model::ColorSet> const& pColorSet)
{
    auto* pUndoManager = mpDocShell->GetUndoManager();
    sd::ViewShell* pViewShell = mpDocShell->GetViewShell();
    if (!pViewShell)
        return;

    SdrView* pView = pViewShell->GetView();
    if (!pView)
        return;

    ViewShellId nViewShellId = pViewShell->GetViewShellBase().GetViewShellId();
    pUndoManager->EnterListAction(SvxResId(RID_SVXSTR_UNDO_THEME_COLOR_CHANGE), "", 0,
                                  nViewShellId);

    changeStyles(mpDocShell, pColorSet);

    SdrModel& rModel = mpMasterPage->getSdrModelFromSdrPage();
    for (sal_uInt16 nPage = 0; nPage < rModel.GetPageCount(); ++nPage)
    {
        SdrPage* pCurrentPage = rModel.GetPage(nPage);

        // TODO - for now change all the objects regardless to which master page it belongs to.
        // Currently we don't have a concept of master slide with a group of layouts as in MSO, but we always only
        // have master pages, which aren't grouped together. In MSO the theme is defined per master slide, so when
        // changing a theme, all the layouts get the new theme, as layouts are synonymous to master pages in LibreOffice,
        // this is not possible to do and we would need to change the theme for each master page separately, which
        // is just annoying for the user.

        // if (!pCurrentPage->TRG_HasMasterPage() || &pCurrentPage->TRG_GetMasterPage() != mpMasterPage)
        //     continue;

        SdrObjListIter aIter(pCurrentPage, SdrIterMode::DeepWithGroups);
        while (aIter.IsMore())
        {
            svx::theme::updateSdrObject(*pColorSet, aIter.Next(), pView, pUndoManager);
        }
    }

    changeThemeColors(mpDocShell, mpMasterPage, pColorSet);

    // See the TODO comment a couple of line above for the explanation - need to change the ThemeColors for all master
    // pages for now, but the following code will need to be changed in the future when we have the concept similar to
    // master slide and layouts
    for (sal_uInt16 nPage = 0; nPage < rModel.GetPageCount(); ++nPage)
    {
        SdrPage* pCurrentPage = rModel.GetPage(nPage);
        if (pCurrentPage->IsMasterPage() && pCurrentPage != mpMasterPage)
            changeThemeColors(mpDocShell, pCurrentPage, pColorSet);
    }

    pUndoManager->LeaveListAction();
}

} // end sd namespace

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