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 :
10 :
11 : #include <svl/IndexedStyleSheets.hxx>
12 : #include <svl/style.hxx>
13 :
14 : #include <stdexcept>
15 : #include <utility>
16 :
17 : using rtl::OUString;
18 :
19 : namespace svl {
20 :
21 2982 : IndexedStyleSheets::IndexedStyleSheets()
22 2982 : {;}
23 :
24 :
25 : void
26 48955 : IndexedStyleSheets::Register(const rtl::OUString& name, unsigned pos)
27 : {
28 48955 : mPositionsByName.insert(std::make_pair(name, pos));
29 48955 : }
30 :
31 : void
32 436 : IndexedStyleSheets::Reindex()
33 : {
34 436 : mPositionsByName.clear();
35 436 : unsigned i = 0;
36 105663 : for (VectorType::const_iterator it = mStyleSheets.begin();
37 70442 : it != mStyleSheets.end(); ++it) {
38 34785 : SfxStyleSheetBase* p = it->get();
39 34785 : Register(p->GetName(), i);
40 34785 : ++i;
41 : }
42 436 : }
43 :
44 : unsigned
45 784 : IndexedStyleSheets::GetNumberOfStyleSheets() const
46 : {
47 784 : return mStyleSheets.size();
48 : }
49 :
50 : void
51 14171 : IndexedStyleSheets::AddStyleSheet(rtl::Reference< SfxStyleSheetBase > style)
52 : {
53 14171 : if (!HasStyleSheet(style)) {
54 14170 : mStyleSheets.push_back(style);
55 : // since we just added an element to the vector, we can safely do -1 as it will always be >= 1
56 14170 : Register(style->GetName(), mStyleSheets.size()-1);
57 : }
58 14171 : }
59 :
60 : bool
61 3 : IndexedStyleSheets::RemoveStyleSheet(rtl::Reference< SfxStyleSheetBase > style)
62 : {
63 3 : rtl::OUString styleName = style->GetName();
64 6 : std::vector<unsigned> positions = FindPositionsByName(styleName);
65 3 : bool found = false;
66 3 : unsigned stylePosition = 0;
67 9 : for (std::vector<unsigned>::const_iterator it = positions.begin();
68 6 : it != positions.end(); ++it) {
69 2 : if (mStyleSheets.at(*it) == style) {
70 2 : found = true;
71 2 : stylePosition = *it;
72 2 : break;
73 : }
74 : }
75 :
76 3 : if (found) {
77 2 : mStyleSheets.erase(mStyleSheets.begin() + stylePosition);
78 2 : Reindex();
79 : }
80 6 : return found;
81 : }
82 :
83 : std::vector<unsigned>
84 14180 : IndexedStyleSheets::FindPositionsByName(const rtl::OUString& name) const
85 : {
86 14180 : std::vector<unsigned> r;
87 14180 : std::pair<MapType::const_iterator, MapType::const_iterator> range = mPositionsByName.equal_range(name);
88 14815 : for (MapType::const_iterator it = range.first; it != range.second; ++it) {
89 635 : r.push_back(it->second);
90 : }
91 14180 : return r;
92 : }
93 :
94 : std::vector<unsigned>
95 177921 : IndexedStyleSheets::FindPositionsByNameAndPredicate(const rtl::OUString& name,
96 : StyleSheetPredicate& predicate) const
97 : {
98 177921 : std::vector<unsigned> r;
99 177921 : MapType::const_iterator it = mPositionsByName.find(name);
100 2091778 : for (/**/; it != mPositionsByName.end(); ++it) {
101 1913857 : unsigned pos = it->second;
102 1913857 : SfxStyleSheetBase *ssheet = mStyleSheets.at(pos).get();
103 1913857 : if (predicate.Check(*ssheet)) {
104 489224 : r.push_back(pos);
105 : }
106 : }
107 177921 : return r;
108 : }
109 :
110 :
111 : unsigned
112 456 : IndexedStyleSheets::GetNumberOfStyleSheetsWithPredicate(StyleSheetPredicate& predicate) const
113 : {
114 456 : unsigned r = 0;
115 3976 : for (VectorType::const_iterator it = mStyleSheets.begin(); it != mStyleSheets.end(); ++it) {
116 3520 : const SfxStyleSheetBase *ssheet = it->get();
117 3520 : if (predicate.Check(*ssheet)) {
118 2451 : ++r;
119 : }
120 : }
121 456 : return r;
122 : }
123 :
124 : rtl::Reference<SfxStyleSheetBase>
125 34801 : IndexedStyleSheets::GetNthStyleSheetThatMatchesPredicate(
126 : unsigned n,
127 : StyleSheetPredicate& predicate,
128 : unsigned startAt)
129 : {
130 34801 : rtl::Reference<SfxStyleSheetBase> retval;
131 34801 : unsigned matching = 0;
132 113565 : for (VectorType::iterator it = mStyleSheets.begin()+startAt; it != mStyleSheets.end(); ++it) {
133 112783 : SfxStyleSheetBase *ssheet = it->get();
134 112783 : if (predicate.Check(*ssheet)) {
135 34796 : if (matching == n) {
136 34019 : retval = *it;
137 34019 : break;
138 : }
139 777 : ++matching;
140 : }
141 : }
142 34801 : return retval;
143 : }
144 :
145 : unsigned
146 34020 : IndexedStyleSheets::FindStyleSheetPosition(const SfxStyleSheetBase& style) const
147 : {
148 34020 : VectorType::const_iterator it = std::find(mStyleSheets.begin(), mStyleSheets.end(), &style);
149 34020 : if (it == mStyleSheets.end()) {
150 0 : throw std::runtime_error("IndexedStyleSheets::FindStylePosition Looked for style not in index");
151 : }
152 34020 : return std::distance(mStyleSheets.begin(), it);
153 : }
154 :
155 : void
156 2859 : IndexedStyleSheets::Clear(StyleSheetDisposer& disposer)
157 : {
158 15064 : for (VectorType::iterator it = mStyleSheets.begin(); it != mStyleSheets.end(); ++it) {
159 12205 : disposer.Dispose(*it);
160 : }
161 2859 : mStyleSheets.clear();
162 2859 : mPositionsByName.clear();
163 2859 : }
164 :
165 2755 : IndexedStyleSheets::~IndexedStyleSheets()
166 2755 : {;}
167 :
168 : bool
169 14175 : IndexedStyleSheets::HasStyleSheet(rtl::Reference< SfxStyleSheetBase > style) const
170 : {
171 14175 : rtl::OUString styleName = style->GetName();
172 28350 : std::vector<unsigned> positions = FindPositionsByName(styleName);
173 44403 : for (std::vector<unsigned>::const_iterator it = positions.begin();
174 29602 : it != positions.end(); ++it) {
175 629 : if (mStyleSheets.at(*it) == style) {
176 3 : return true;
177 : }
178 : }
179 28347 : return false;
180 : }
181 :
182 : rtl::Reference< SfxStyleSheetBase >
183 138235 : IndexedStyleSheets::GetStyleSheetByPosition(unsigned pos)
184 : {
185 138235 : if( pos < mStyleSheets.size() )
186 138235 : return mStyleSheets.at(pos);
187 0 : return NULL;
188 : }
189 :
190 : void
191 0 : IndexedStyleSheets::ApplyToAllStyleSheets(StyleSheetCallback& callback) const
192 : {
193 0 : for (VectorType::const_iterator it = mStyleSheets.begin(); it != mStyleSheets.end(); ++it) {
194 0 : callback.DoIt(**it);
195 : }
196 0 : }
197 :
198 : std::vector<unsigned>
199 0 : IndexedStyleSheets::FindPositionsByPredicate(StyleSheetPredicate& predicate) const
200 : {
201 0 : std::vector<unsigned> r;
202 0 : for (VectorType::const_iterator it = mStyleSheets.begin(); it != mStyleSheets.end(); ++it) {
203 0 : if (predicate.Check(**it)) {
204 0 : r.push_back(std::distance(mStyleSheets.begin(), it));
205 : }
206 : }
207 0 : return r;
208 : }
209 :
210 : } /* namespace svl */
211 :
212 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|