Subversion Repositories Scribus

Rev

Rev 8382 | Rev 8455 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6733 avox 1
/*
2
 For general Scribus (>=1.3.2) copyright and licensing information please refer
3
 to the COPYING file provided with the program. Following this notice may exist
4
 a copyright and/or license notice that predates the release of Scribus 1.3.2
5
 for which a new license (GPL+exception) is in place.
6
 */
7
/***************************************************************************
8
*                                                                         *
9
*   This program is free software; you can redistribute it and/or modify  *
10
*   it under the terms of the GNU General Public License as published by  *
11
*   the Free Software Foundation; either version 2 of the License, or     *
12
*   (at your option) any later version.                                   *
13
*                                                                         *
14
***************************************************************************/
6443 avox 15
 
16
 
6733 avox 17
 
8382 avox 18
#include "styles/style.h"
8337 avox 19
#include "paragraphstyle.h"
8193 avox 20
#include "desaxe/saxiohelper.h"
8266 avox 21
#include "desaxe/simple_actions.h"
6733 avox 22
 
8193 avox 23
 
8134 avox 24
ParagraphStyle::ParagraphStyle() : Style(), cstyleContext(NULL), cstyleContextIsInh(true), cstyle()
6443 avox 25
{
6805 avox 26
        setParent("");
8134 avox 27
        cstyleContext.setDefaultStyle( &cstyle );
28
//      qDebug(QString("ParagraphStyle() %1 pcontext %2 ccontext %3").arg(reinterpret_cast<uint>(this)).arg(reinterpret_cast<uint>(context())).arg(reinterpret_cast<uint>(defaultStyle()->context())));
6443 avox 29
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
30
        m_##attr_NAME = attr_DEFAULT; \
31
        inh_##attr_NAME = true;
32
#include "paragraphstyle.attrdefs.cxx"
33
#undef ATTRDEF
34
}
35
 
36
 
8134 avox 37
ParagraphStyle::ParagraphStyle(const ParagraphStyle& other) : Style(other), cstyleContext(NULL), cstyleContextIsInh(other.cstyleContextIsInh), cstyle(other.charStyle())
6443 avox 38
{
8134 avox 39
        if (cstyleContextIsInh)
40
                cstyle.setContext(NULL);
8058 avox 41
        else
8134 avox 42
                cstyle.setContext(other.charStyle().context());
43
        cstyleContext.setDefaultStyle( &cstyle );
6735 avox 44
//      qDebug(QString("ParagraphStyle(%2) %1").arg(reinterpret_cast<uint>(&other)).arg(reinterpret_cast<uint>(this)));
7003 avox 45
        other.validate();
6733 avox 46
 
6443 avox 47
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
48
        m_##attr_NAME = other.m_##attr_NAME; \
49
        inh_##attr_NAME = other.inh_##attr_NAME;
50
#include "paragraphstyle.attrdefs.cxx"
51
#undef ATTRDEF
52
}
53
 
6765 avox 54
ParagraphStyle::~ParagraphStyle()
55
{
56
//      qDebug(QString("~ParagraphStyle %1").arg(reinterpret_cast<uint>(this)));
57
}
58
 
8193 avox 59
 
6443 avox 60
QString ParagraphStyle::displayName() const
61
{
8134 avox 62
        if ( hasName() || !hasParent() || !m_context)
6443 avox 63
                return name();
64
        //      else if ( inheritsAll() )
65
        //              return parent()->displayName();
66
        else
8058 avox 67
                return parentStyle()->displayName() + "+";
6443 avox 68
}
69
 
70
 
71
bool ParagraphStyle::equiv(const Style& other) const
72
{
6805 avox 73
        other.validate();
6443 avox 74
        const ParagraphStyle* oth = dynamic_cast<const ParagraphStyle*> ( & other );
75
        return  oth &&
8058 avox 76
                parent() == oth->parent() && cstyle.equiv(oth->charStyle())
6443 avox 77
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
6733 avox 78
                && (inh_##attr_NAME == oth->inh_##attr_NAME) \
79
                && (inh_##attr_NAME || m_##attr_NAME == oth->m_##attr_NAME)
6443 avox 80
#include "paragraphstyle.attrdefs.cxx"
81
#undef ATTRDEF
82
                ;
83
}      
84
 
85
 
86
 
87
ParagraphStyle& ParagraphStyle::operator=(const ParagraphStyle& other)
88
{
6805 avox 89
        other.validate();
6765 avox 90
        static_cast<Style&>(*this) = static_cast<const Style&>(other);
7059 avox 91
 
6443 avox 92
        cstyle = other.charStyle();
7059 avox 93
 
8134 avox 94
        // we dont want cstyleContext to point to other's charstyle...
95
        cstyleContext.setDefaultStyle( &cstyle );
8058 avox 96
 
8134 avox 97
        if (cstyleContextIsInh)
8058 avox 98
        {
99
                const ParagraphStyle * parent = dynamic_cast<const ParagraphStyle*> ( parentStyle() );
8134 avox 100
                cstyle.setContext(parent ? parent->charStyleContext() : NULL);
8058 avox 101
        }
102
        else
103
        {
8134 avox 104
                cstyle.setContext(other.charStyle().context());
8058 avox 105
        }
106
 
6443 avox 107
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
6733 avox 108
        m_##attr_NAME = other.m_##attr_NAME; \
109
        inh_##attr_NAME = other.inh_##attr_NAME;
6443 avox 110
#include "paragraphstyle.attrdefs.cxx"
111
#undef ATTRDEF
112
        return *this;
113
}
114
 
115
 
8134 avox 116
void ParagraphStyle::setContext(const StyleContext* context)
8058 avox 117
{
8134 avox 118
        Style::setContext(context);
119
//      qDebug(QString("ParagraphStyle::setContext(%1) parent=%2").arg((unsigned long int)context).arg((unsigned long int)oth));
8058 avox 120
        repairImplicitCharStyleInheritance();
121
}
6443 avox 122
 
8058 avox 123
void ParagraphStyle::repairImplicitCharStyleInheritance()
124
{
8134 avox 125
        if (cstyleContextIsInh) {
8058 avox 126
                const ParagraphStyle * newParent = dynamic_cast<const ParagraphStyle*> ( parentStyle() );
8134 avox 127
                cstyle.setContext(newParent ? newParent->charStyleContext() : NULL);
8058 avox 128
        }
129
}
130
 
131
 
132
void ParagraphStyle::breakImplicitCharStyleInheritance(bool val)
133
{
8134 avox 134
        cstyleContextIsInh = !val;
8058 avox 135
        repairImplicitCharStyleInheritance();
136
}
137
 
8134 avox 138
void ParagraphStyle::update(const StyleContext* context)
6443 avox 139
{
8134 avox 140
        Style::update(context);
141
        assert ( &cstyleContext != cstyle.context());
8058 avox 142
 
143
        repairImplicitCharStyleInheritance();
8134 avox 144
        assert ( &cstyleContext != cstyle.context());
145
        cstyle.validate();
146
        cstyleContext.invalidate();
8058 avox 147
 
6443 avox 148
        const ParagraphStyle * oth = dynamic_cast<const ParagraphStyle*> ( parentStyle() );
8134 avox 149
//      qDebug(QString("ParagraphStyle::update(%1) parent=%2").arg((unsigned long int)context).arg((unsigned long int)oth));
6443 avox 150
        if (oth) {
151
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
152
                if (inh_##attr_NAME) \
6733 avox 153
                        m_##attr_NAME = oth->attr_GETTER();
6443 avox 154
#include "paragraphstyle.attrdefs.cxx"
155
#undef ATTRDEF
156
        }
157
}
158
 
159
 
160
 
161
void ParagraphStyle::applyStyle(const ParagraphStyle& other)
162
{
163
        Style::applyStyle(other);
164
        cstyle.applyCharStyle(other.charStyle());
8134 avox 165
        cstyleContext.invalidate();
6443 avox 166
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
167
        if (! other.inh_##attr_NAME) \
168
                set##attr_NAME(other.m_##attr_NAME);
169
#include "paragraphstyle.attrdefs.cxx"
170
#undef ATTRDEF
171
}
172
 
173
 
174
void ParagraphStyle::eraseStyle(const ParagraphStyle& other)
175
{
6805 avox 176
        other.validate();
6443 avox 177
        Style::eraseStyle(other);
178
        cstyle.eraseCharStyle(other.charStyle());
8134 avox 179
        cstyleContext.invalidate();
6443 avox 180
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
6936 avox 181
        if (!inh_##attr_NAME && m_##attr_NAME == other.m_##attr_NAME) \
6443 avox 182
                reset##attr_NAME();
183
#include "paragraphstyle.attrdefs.cxx"
184
#undef ATTRDEF
185
}
186
 
7242 avox 187
void ParagraphStyle::setStyle(const ParagraphStyle & other)
188
{
189
        other.validate();
7993 avox 190
        setParent(other.parent());
7242 avox 191
        cstyle.setStyle(other.charStyle());
8134 avox 192
        cstyleContext.invalidate();
7242 avox 193
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
7993 avox 194
        inh_##attr_NAME = other.inh_##attr_NAME; \
7242 avox 195
        m_##attr_NAME = other.m_##attr_NAME;
196
#include "paragraphstyle.attrdefs.cxx"
197
#undef ATTRDEF
198
}
199
 
8266 avox 200
 
201
static QString toXMLString(ParagraphStyle::AlignmentType val)
202
{
203
        return QString::number(static_cast<int>(val));
204
}
205
 
206
 
207
static QString toXMLString(const QValueList<ParagraphStyle::TabRecord> & )
208
{
209
        return "dummy";
210
}
211
 
8347 jghali 212
void ParagraphStyle::saxx(SaxHandler& handler, const Xml_string elemtag) const
8266 avox 213
{
214
        Xml_attr att;
8337 avox 215
        Style::saxxAttributes(att);
8266 avox 216
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
8348 avox 217
        if (!inh_##attr_NAME && strcmp(# attr_NAME, "TabValues") != 0) \
8266 avox 218
                att.insert(# attr_NAME, toXMLString(m_##attr_NAME)); 
219
#include "paragraphstyle.attrdefs.cxx"
220
#undef ATTRDEF
8382 avox 221
        if (!name().isEmpty())
8390 avox 222
                att["id"] = mkXMLName(elemtag + name());
8266 avox 223
        handler.begin(elemtag, att);
8382 avox 224
        if (parentStyle() && hasParent())
225
                parentStyle()->saxx(handler);
8266 avox 226
        QValueList<ParagraphStyle::TabRecord>::const_iterator it;
227
        for (it=m_TabValues.begin(); it != m_TabValues.end(); ++it)
228
        {
229
                const ParagraphStyle::TabRecord& tb(*it);
230
                Xml_attr tab;
231
                tab.insert("pos", toXMLString(tb.tabPosition));
232
                tab.insert("fillChar", toXMLString(tb.tabFillChar.unicode()));
233
                tab.insert("type", toXMLString(tb.tabType));
234
                handler.beginEnd("tabstop", tab);
235
        }
8337 avox 236
        if (charStyle() != CharStyle())
237
                charStyle().saxx(handler);
8266 avox 238
        handler.end(elemtag);
239
}
240
 
241
///   PageItem StoryText -> PageItem StoryText
242
class SetCharStyle_body : public desaxe::Action_body
243
{
244
        void end (const Xml_string /*tagname*/)
245
        {
246
                ParagraphStyle* pstyle = this->dig->top<ParagraphStyle>(1);
247
                CharStyle* cstyle = this->dig->top<CharStyle>(0);
248
                pstyle->charStyle() = *cstyle;
249
        }
250
};
251
 
252
class SetCharStyle : public desaxe::MakeAction<SetCharStyle_body>
253
{};
254
 
255
 
256
 
8289 avox 257
class SetTabStop_body : public desaxe::Action_body
258
{
259
        void begin (const Xml_string /*tagname*/, Xml_attr attr)
260
        {
261
                ParagraphStyle* pstyle = this->dig->top<ParagraphStyle>();
262
                ParagraphStyle::TabRecord tb;
263
                tb.tabPosition = parseDouble(attr["pos"]);
264
                tb.tabFillChar = QChar(parseInt(attr["fillChar"]));
265
                tb.tabType = parseInt(attr["type"]);
266
                QValueList<ParagraphStyle::TabRecord> tabs = pstyle->tabValues();
267
                tabs.append(tb);
268
                pstyle->setTabValues(tabs);
269
        }
270
};
8266 avox 271
 
8289 avox 272
class SetTabStop : public desaxe::MakeAction<SetTabStop_body>
273
{};
274
 
275
 
276
 
277
 
8266 avox 278
template<>
8289 avox 279
ParagraphStyle::AlignmentType parse<ParagraphStyle::AlignmentType>(Xml_string str)
8266 avox 280
{
281
        return parseEnum<ParagraphStyle::AlignmentType>(str);
282
}
283
 
284
 
285
template<>
8289 avox 286
ParagraphStyle::LineSpacingMode parse<ParagraphStyle::LineSpacingMode>(Xml_string str)
8266 avox 287
{
288
        return parseEnum<ParagraphStyle::LineSpacingMode>(str);
289
}
290
 
291
 
292
typedef QValueList<ParagraphStyle::TabRecord> Tablist;
293
 
294
template<>
8289 avox 295
Tablist parse<Tablist>(Xml_string str)
8266 avox 296
{
297
        return Tablist();
298
}
299
 
300
 
301
using namespace desaxe;
302
 
303
const Xml_string ParagraphStyle::saxxDefaultElem("style");
304
 
305
void ParagraphStyle::desaxeRules(Xml_string prefixPattern, Digester& ruleset, Xml_string elemtag)
306
{
307
        typedef ParagraphStyle::TabRecord TabRecord;
308
 
309
        Xml_string stylePrefix(Digester::concat(prefixPattern, elemtag));
310
        ruleset.addRule(stylePrefix, Factory<ParagraphStyle>());
8382 avox 311
        ruleset.addRule(stylePrefix, IdRef<ParagraphStyle>());
312
        Style::desaxeRules<ParagraphStyle>(prefixPattern, ruleset, elemtag);
8266 avox 313
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
8348 avox 314
        if ( strcmp(# attr_NAME, "TabValues") != 0) \
8266 avox 315
                ruleset.addRule(stylePrefix, SetAttributeWithConversion<ParagraphStyle, attr_TYPE> ( & ParagraphStyle::set##attr_NAME,  # attr_NAME, &parse<attr_TYPE> ));
316
#include "paragraphstyle.attrdefs.cxx"
317
#undef ATTRDEF
318
        Xml_string charstylePrefix(Digester::concat(stylePrefix, CharStyle::saxxDefaultElem));
319
        CharStyle::desaxeRules(stylePrefix, ruleset);
320
        ruleset.addRule(charstylePrefix, SetCharStyle());
8289 avox 321
 
322
        Xml_string tabPrefix(Digester::concat(stylePrefix, "tabstop"));
323
        ruleset.addRule(tabPrefix, SetTabStop());
8266 avox 324
}