1 /* This file is part of the KDE project
2  * Copyright (C) 2007, 2009-2010 Thomas Zander <zander@kde.org>
3  * Copyright (C) 2010 Ko Gmbh <cbo@kogmbh.com>
4  * Copyright (C) 2011 Matus Hanzes <matus.hanzes@ixonos.com>
5  * Copyright (C) 2013 C. Boemann <cbo@boemann.dk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include "KoAnchorTextRange.h"
24 #include "KoShapeAnchor.h"
25 
26 #include <KoShapeSavingContext.h>
27 #include <KoShape.h>
28 
29 #include "TextDebug.h"
30 
31 
32 class KoAnchorTextRangePrivate
33 {
34 public:
KoAnchorTextRangePrivate(KoShapeAnchor * p)35     KoAnchorTextRangePrivate(KoShapeAnchor *p)
36         : parent(p)
37     {
38     }
39 
40     KoShapeAnchor *parent;
41 };
42 
KoAnchorTextRange(KoShapeAnchor * parent,const QTextCursor & cursor)43 KoAnchorTextRange::KoAnchorTextRange(KoShapeAnchor *parent, const QTextCursor &cursor)
44     : KoTextRange(cursor)
45     , d_ptr(new KoAnchorTextRangePrivate(parent))
46 {
47     Q_ASSERT(parent);
48     parent->setTextLocation(this);
49 }
50 
~KoAnchorTextRange()51 KoAnchorTextRange::~KoAnchorTextRange()
52 {
53 }
54 
anchor() const55 KoShapeAnchor *KoAnchorTextRange::anchor() const
56 {
57     Q_D(const KoAnchorTextRange);
58     return d->parent;
59 }
60 
document() const61 const QTextDocument *KoAnchorTextRange::document() const
62 {
63     return KoTextRange::document();
64 }
65 
position() const66 int KoAnchorTextRange::position() const
67 {
68     return rangeStart();
69 }
70 
updateContainerModel()71 void KoAnchorTextRange::updateContainerModel()
72 {
73     Q_D(KoAnchorTextRange);
74 
75     if (!d->parent->shape()->isVisible()) {
76         // Per default the shape this anchor presents is hidden and we only make it visible once an
77         // explicit placement is made. This prevents shapes that are anchored at e.g. hidden
78         // textboxes to not become visible.
79         d->parent->shape()->setVisible(true);
80     }
81 
82     if (d->parent->placementStrategy() != 0) {
83         d->parent->placementStrategy()->updateContainerModel();
84     }
85 }
86 
loadOdf(const KoXmlElement &,KoShapeLoadingContext &)87 bool KoAnchorTextRange::loadOdf(const KoXmlElement &, KoShapeLoadingContext &)
88 {
89     return true;
90 }
91 
saveOdf(KoShapeSavingContext & context,int position,KoTextRange::TagType tagType) const92 void KoAnchorTextRange::saveOdf(KoShapeSavingContext &context, int position, KoTextRange::TagType tagType) const
93 {
94     Q_UNUSED(position);
95     Q_UNUSED(tagType);
96 
97     Q_D(const KoAnchorTextRange);
98     if (tagType == KoTextRange::StartTag) {
99         d->parent->saveOdf(context);
100     }
101 }
102