~ubuntu-branches/ubuntu/precise/ipe/precise

« back to all changes in this revision

Viewing changes to src/ipelib/ipemark.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2004-06-08 00:44:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040608004402-72yu51xlh7vt6p9m
Tags: upstream-6.0pre16
ImportĀ upstreamĀ versionĀ 6.0pre16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// --------------------------------------------------------------------
 
2
// The Mark object.
 
3
// --------------------------------------------------------------------
 
4
/*
 
5
 
 
6
    This file is part of the extensible drawing editor Ipe.
 
7
    Copyright (C) 1993-2004  Otfried Cheong
 
8
 
 
9
    Ipe is free software; you can redistribute it and/or modify it
 
10
    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
    As a special exception, you have permission to link Ipe with the
 
15
    CGAL library and distribute executables, as long as you follow the
 
16
    requirements of the Gnu General Public License in regard to all of
 
17
    the software in the executable aside from CGAL.
 
18
 
 
19
    Ipe is distributed in the hope that it will be useful, but WITHOUT
 
20
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
21
    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 
22
    License for more details.
 
23
 
 
24
    You should have received a copy of the GNU General Public License
 
25
    along with Ipe; if not, you can find it at
 
26
    "http://www.gnu.org/copyleft/gpl.html", or write to the Free
 
27
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
28
 
 
29
*/
 
30
 
 
31
#include "ipemark.h"
 
32
#include "ipevisitor.h"
 
33
#include "ipepainter.h"
 
34
 
 
35
/*! \class IpeMark
 
36
  \ingroup obj
 
37
  \brief The mark object.
 
38
 
 
39
  A mark is a little marker for a "point". Several shapes are
 
40
  available: disks, circles, squares, crosses, etc.
 
41
 
 
42
*/
 
43
 
 
44
//! Create from XML stream.
 
45
IpeMark::IpeMark(IpeRepository *rep, const IpeXmlAttributes &attr,
 
46
                 IpeString /* data */)
 
47
  : IpeObject(rep, attr)
 
48
{
 
49
  iSize = rep->MakeScalar(IpeAttribute::EMarkSize, attr["size"]);
 
50
  IpeLex shapeLex(attr["shape"]);
 
51
  int shape;
 
52
  shapeLex >> shape;
 
53
  iShape = shape;
 
54
  // parse position
 
55
  IpeLex lex(attr["pos"]);
 
56
  lex >> iPos.iX >> iPos.iY;
 
57
}
 
58
 
 
59
//! Create at position.
 
60
IpeMark::IpeMark(const IpeAllAttributes &attr, const IpeVector &pos)
 
61
  : IpeObject(attr)
 
62
{
 
63
  iSize = attr.iMarkSize;
 
64
  iShape = attr.iMarkShape;
 
65
  iPos = pos;
 
66
}
 
67
 
 
68
//! Clone object
 
69
IpeObject *IpeMark::Clone() const
 
70
{
 
71
  return new IpeMark(*this);
 
72
}
 
73
 
 
74
//! Copy constructor.
 
75
IpeMark::IpeMark(const IpeMark &rhs)
 
76
  : IpeObject(rhs)
 
77
{
 
78
  iPos = rhs.iPos;
 
79
  iShape = rhs.iShape;
 
80
  iSize = rhs.iSize;
 
81
}
 
82
 
 
83
//! Return pointer to this object.
 
84
IpeMark *IpeMark::AsMark()
 
85
{
 
86
  return this;
 
87
}
 
88
 
 
89
//! Call VisitMark of visitor.
 
90
void IpeMark::Accept(IpeVisitor &visitor) const
 
91
{
 
92
  visitor.VisitMark(this);
 
93
}
 
94
 
 
95
void IpeMark::SaveAsXml(IpePainter &painter, IpeStream &stream,
 
96
                        IpeString layer) const
 
97
{
 
98
  stream << "<mark";
 
99
  SaveAttributesAsXml(painter, stream, layer);
 
100
  stream << " pos=\"" << iPos.iX << " " << iPos.iY << "\"";
 
101
  if (painter.MarkShape() == 0)
 
102
    stream << " shape=\"" << int(iShape) << "\"";
 
103
  if (painter.MarkSize().IsNull())
 
104
    stream << " size=\"" << painter.Repository()->String(iSize) << "\"/>\n";
 
105
}
 
106
 
 
107
//! Draw marker.
 
108
void IpeMark::Draw(IpePainter &painter) const
 
109
{
 
110
  painter.Push();
 
111
  painter.Transform(Matrix());
 
112
  painter.Translate(iPos);
 
113
  painter.Untransform(false);
 
114
 
 
115
  painter.SetMarkSize(iSize);
 
116
  painter.SetMarkShape(iShape);
 
117
  painter.SetStroke(Stroke());
 
118
  painter.SetDashStyle(IpeAttribute::Solid());
 
119
 
 
120
  const IpeRepository *rep = painter.Repository();
 
121
  int shape = painter.MarkShape();
 
122
  double absSize = rep->ToScalar(painter.MarkSize());
 
123
 
 
124
  painter.SetLineWidth(IpeAttribute(IpeAttribute::ELineWidth, 0.2 * absSize));
 
125
 
 
126
  if (shape == EDisc || shape == ESquare) {
 
127
    painter.SetFill(painter.Stroke());
 
128
  } else {
 
129
    painter.SetFill(IpeAttribute::Void());
 
130
  }
 
131
 
 
132
  IpeVector size(absSize, absSize);
 
133
  IpeVector ll = IpeVector::Zero - 0.5 * size;
 
134
  if (shape == EBox || shape == ESquare) {
 
135
    IpeRect re(ll);
 
136
    re.AddPoint(ll + size);
 
137
    painter.Rect(re);
 
138
  } else if (shape == ECross) {
 
139
    painter.BeginPath(ll);
 
140
    painter.LineTo(ll + size);
 
141
    painter.EndPath();
 
142
    painter.BeginPath(ll + IpeVector(absSize, 0));
 
143
    painter.LineTo(ll + IpeVector(0, absSize));
 
144
    painter.EndPath();
 
145
  } else {
 
146
    IpeMatrix m; // = 1
 
147
    m.iA[0] = m.iA[3] = 0.5 * absSize;
 
148
    painter.Transform(m);
 
149
    painter.DrawEllipse();
 
150
  }
 
151
  painter.DrawPath();
 
152
  painter.Pop();
 
153
}
 
154
 
 
155
double IpeMark::Distance(const IpeVector &v, const IpeMatrix &m,
 
156
                         double /* bound */) const
 
157
{
 
158
  return (v - m * (Matrix() * Position())).Len();
 
159
}
 
160
 
 
161
void IpeMark::AddToBBox(IpeRect &box, const IpeMatrix &m) const
 
162
{
 
163
  IpeVector p = m * (Matrix() * Position());
 
164
  box.AddPoint(p);
 
165
}
 
166
 
 
167
void IpeMark::SnapVtx(const IpeVector &mouse, const IpeMatrix &m,
 
168
                      IpeVector &pos, double &bound) const
 
169
{
 
170
  SnapVertex(mouse, m * Matrix() * Position(), pos, bound);
 
171
}
 
172
 
 
173
//! Set shape of mark.
 
174
void IpeMark::SetShape(int shape)
 
175
{
 
176
  iShape = shape;
 
177
}
 
178
 
 
179
//! Set size of mark.
 
180
void IpeMark::SetSize(IpeAttribute size)
 
181
{
 
182
  iSize = size;
 
183
}
 
184
 
 
185
void IpeMark::CheckStyle(const IpeStyleSheet *sheet,
 
186
                          IpeAttributeSeq &seq) const
 
187
{
 
188
  IpeObject::CheckStyle(sheet, seq);
 
189
  CheckSymbol(iSize, sheet, seq);
 
190
}
 
191
 
 
192
// --------------------------------------------------------------------