insilmaril@0
|
1 |
#include "headingobj.h"
|
insilmaril@2
|
2 |
#include <qregexp.h>
|
insilmaril@0
|
3 |
|
insilmaril@0
|
4 |
/////////////////////////////////////////////////////////////////
|
insilmaril@0
|
5 |
// HeadingObj
|
insilmaril@0
|
6 |
/////////////////////////////////////////////////////////////////
|
insilmaril@0
|
7 |
HeadingObj::HeadingObj() : MapObj()
|
insilmaril@0
|
8 |
{
|
insilmaril@0
|
9 |
// cout << "Const HeadingObj ()\n";
|
insilmaril@0
|
10 |
init ();
|
insilmaril@0
|
11 |
}
|
insilmaril@0
|
12 |
|
insilmaril@2
|
13 |
HeadingObj::HeadingObj(Q3Canvas* c) :MapObj(c)
|
insilmaril@0
|
14 |
{
|
insilmaril@0
|
15 |
// cout << "Const HeadingObj\n";
|
insilmaril@0
|
16 |
init ();
|
insilmaril@0
|
17 |
}
|
insilmaril@0
|
18 |
|
insilmaril@0
|
19 |
HeadingObj::~HeadingObj()
|
insilmaril@0
|
20 |
{
|
insilmaril@0
|
21 |
textline.clear();
|
insilmaril@0
|
22 |
}
|
insilmaril@0
|
23 |
|
insilmaril@0
|
24 |
void HeadingObj::init()
|
insilmaril@0
|
25 |
{
|
insilmaril@0
|
26 |
textline.setAutoDelete (TRUE);
|
insilmaril@0
|
27 |
textwidth=40;
|
insilmaril@0
|
28 |
color=QColor ("black");
|
insilmaril@0
|
29 |
font=QFont();
|
insilmaril@2
|
30 |
heading="";
|
insilmaril@0
|
31 |
}
|
insilmaril@0
|
32 |
|
insilmaril@0
|
33 |
void HeadingObj::copy(HeadingObj *other)
|
insilmaril@0
|
34 |
{
|
insilmaril@0
|
35 |
MapObj::copy (other);
|
insilmaril@0
|
36 |
textwidth=other->textwidth;
|
insilmaril@0
|
37 |
color=other->color;
|
insilmaril@0
|
38 |
font=other->font;
|
insilmaril@0
|
39 |
setText (other->text() );
|
insilmaril@0
|
40 |
}
|
insilmaril@0
|
41 |
|
insilmaril@0
|
42 |
void HeadingObj::move(double x, double y)
|
insilmaril@0
|
43 |
{
|
insilmaril@0
|
44 |
MapObj::move(x,y);
|
insilmaril@2
|
45 |
|
insilmaril@0
|
46 |
int h; // height of a textline
|
insilmaril@0
|
47 |
int ho; // offset of height while drawing all lines
|
insilmaril@2
|
48 |
|
insilmaril@0
|
49 |
if (textline.first() )
|
insilmaril@0
|
50 |
h=textline.first()->boundingRect().height();
|
insilmaril@0
|
51 |
else
|
insilmaril@0
|
52 |
h=2;
|
insilmaril@2
|
53 |
Q3CanvasText *t;
|
insilmaril@0
|
54 |
ho=0;
|
insilmaril@0
|
55 |
for (t=textline.first(); t; t=textline.next() )
|
insilmaril@0
|
56 |
{
|
insilmaril@0
|
57 |
t->move(x,y+ho);
|
insilmaril@0
|
58 |
ho=ho+h;
|
insilmaril@0
|
59 |
}
|
insilmaril@0
|
60 |
}
|
insilmaril@0
|
61 |
|
insilmaril@0
|
62 |
|
insilmaril@0
|
63 |
void HeadingObj::moveBy(double x, double y)
|
insilmaril@0
|
64 |
{
|
insilmaril@0
|
65 |
move (x+absPos.x(),y+absPos.y() );
|
insilmaril@0
|
66 |
}
|
insilmaril@0
|
67 |
|
insilmaril@0
|
68 |
void HeadingObj::positionBBox()
|
insilmaril@0
|
69 |
{
|
insilmaril@0
|
70 |
bbox.setX (absPos.x());
|
insilmaril@0
|
71 |
bbox.setY (absPos.y());
|
insilmaril@0
|
72 |
}
|
insilmaril@0
|
73 |
|
insilmaril@0
|
74 |
void HeadingObj::calcBBoxSize()
|
insilmaril@0
|
75 |
{
|
insilmaril@0
|
76 |
int w=0;
|
insilmaril@0
|
77 |
int h=0;
|
insilmaril@0
|
78 |
// Using Backspace an empty heading might easily be created, then there
|
insilmaril@0
|
79 |
// would be textline.first()==NULL This can be worked around by the following, but
|
insilmaril@0
|
80 |
// then no selection would be visible, thus we prevent it in ::setText()
|
insilmaril@0
|
81 |
if (!textline.isEmpty() )
|
insilmaril@0
|
82 |
{
|
insilmaril@2
|
83 |
Q3CanvasText *t;
|
insilmaril@0
|
84 |
for (t=textline.first(); t; t=textline.next() )
|
insilmaril@0
|
85 |
{
|
insilmaril@0
|
86 |
h+=t->boundingRect().height();
|
insilmaril@0
|
87 |
if (w<t->boundingRect().width() )
|
insilmaril@0
|
88 |
w=t->boundingRect().width();
|
insilmaril@0
|
89 |
}
|
insilmaril@0
|
90 |
}
|
insilmaril@0
|
91 |
bbox.setSize (QSize(w,h));
|
insilmaril@0
|
92 |
}
|
insilmaril@0
|
93 |
|
insilmaril@2
|
94 |
Q3CanvasText* HeadingObj::newLine(QString s)
|
insilmaril@0
|
95 |
{
|
insilmaril@2
|
96 |
Q3CanvasText *t;
|
insilmaril@2
|
97 |
t = new Q3CanvasText(canvas);
|
insilmaril@0
|
98 |
t->setFont (font);
|
insilmaril@0
|
99 |
t->setColor (color);
|
insilmaril@0
|
100 |
t->setZ(Z_TEXT);
|
insilmaril@0
|
101 |
t->setText(s);
|
insilmaril@2
|
102 |
t->setTextFlags(Qt::AlignLeft);
|
insilmaril@0
|
103 |
t->show();
|
insilmaril@0
|
104 |
return t;
|
insilmaril@0
|
105 |
}
|
insilmaril@0
|
106 |
|
insilmaril@0
|
107 |
void HeadingObj::setText (QString s)
|
insilmaril@0
|
108 |
{
|
insilmaril@0
|
109 |
heading=s;
|
insilmaril@0
|
110 |
|
insilmaril@0
|
111 |
// remove old textlines and prepare generating new ones
|
insilmaril@0
|
112 |
textline.clear();
|
insilmaril@0
|
113 |
|
insilmaril@0
|
114 |
// prevent empty textline, so at least a small selection stays
|
insilmaril@0
|
115 |
// visible for this heading
|
insilmaril@0
|
116 |
if (s.length()==0) s=" ";
|
insilmaril@0
|
117 |
|
insilmaril@0
|
118 |
int i=0; // index for actual search for ws
|
insilmaril@0
|
119 |
int j=0; // index of last ws
|
insilmaril@0
|
120 |
int k=0; // index of "<br>" or similar linebreak
|
insilmaril@0
|
121 |
int br=0; // width of found break, e.g. for <br> it is 4
|
insilmaril@2
|
122 |
QRegExp re("<br.*/>");
|
insilmaril@2
|
123 |
re.setMinimal (true);
|
insilmaril@0
|
124 |
|
insilmaril@0
|
125 |
// set the text and wrap lines
|
insilmaril@0
|
126 |
while (s.length()>0)
|
insilmaril@0
|
127 |
{
|
insilmaril@0
|
128 |
// ok, some people wanted manual linebreaks, here we go
|
insilmaril@2
|
129 |
k=re.search (s,i);
|
insilmaril@0
|
130 |
if (k>=0)
|
insilmaril@0
|
131 |
{
|
insilmaril@2
|
132 |
br=re.cap(0).length();
|
insilmaril@0
|
133 |
i=k;
|
insilmaril@0
|
134 |
} else
|
insilmaril@0
|
135 |
i=s.find (" ",i,false);
|
insilmaril@0
|
136 |
if (i<0 && j==0)
|
insilmaril@0
|
137 |
{ // no ws found at all in s
|
insilmaril@0
|
138 |
// append whole s
|
insilmaril@0
|
139 |
textline.append (newLine(s));
|
insilmaril@0
|
140 |
s="";
|
insilmaril@0
|
141 |
} else
|
insilmaril@0
|
142 |
{
|
insilmaril@0
|
143 |
if (i<0 && j>0)
|
insilmaril@0
|
144 |
{ // no ws found in actual search
|
insilmaril@0
|
145 |
if (s.length()<=textwidth)
|
insilmaril@0
|
146 |
{
|
insilmaril@0
|
147 |
textline.append (newLine(s));
|
insilmaril@0
|
148 |
s="";
|
insilmaril@0
|
149 |
} else
|
insilmaril@0
|
150 |
{
|
insilmaril@0
|
151 |
textline.append (newLine(s.left(j)));
|
insilmaril@0
|
152 |
s=s.mid(j+1,s.length());
|
insilmaril@0
|
153 |
j=0;
|
insilmaril@0
|
154 |
}
|
insilmaril@0
|
155 |
} else
|
insilmaril@0
|
156 |
{
|
insilmaril@0
|
157 |
if (i>= 0 && i<=static_cast <int> (textwidth))
|
insilmaril@0
|
158 |
{ // there is a ws in textwidth
|
insilmaril@0
|
159 |
if (br>0)
|
insilmaril@0
|
160 |
{
|
insilmaril@0
|
161 |
// here is a linebreak
|
insilmaril@0
|
162 |
textline.append (newLine(s.left(i)));
|
insilmaril@0
|
163 |
s=s.mid(i+br,s.length());
|
insilmaril@0
|
164 |
i=0;
|
insilmaril@0
|
165 |
j=0;
|
insilmaril@0
|
166 |
br=0;
|
insilmaril@0
|
167 |
} else
|
insilmaril@0
|
168 |
{
|
insilmaril@0
|
169 |
j=i;
|
insilmaril@0
|
170 |
i++;
|
insilmaril@0
|
171 |
}
|
insilmaril@0
|
172 |
} else
|
insilmaril@0
|
173 |
{
|
insilmaril@0
|
174 |
if (i>static_cast <int> (textwidth) )
|
insilmaril@0
|
175 |
{
|
insilmaril@0
|
176 |
if (j>0)
|
insilmaril@0
|
177 |
{ // a ws out of textwidth, but we have also one in
|
insilmaril@0
|
178 |
textline.append (newLine(s.left(j)));
|
insilmaril@0
|
179 |
s=s.mid(j+1,s.length());
|
insilmaril@0
|
180 |
i=0;
|
insilmaril@0
|
181 |
j=0;
|
insilmaril@0
|
182 |
} else
|
insilmaril@0
|
183 |
{ // a ws out of text, but none in
|
insilmaril@0
|
184 |
textline.append (newLine(s.left(i)));
|
insilmaril@0
|
185 |
s=s.mid(i+1,s.length());
|
insilmaril@0
|
186 |
i=0;
|
insilmaril@0
|
187 |
}
|
insilmaril@0
|
188 |
}
|
insilmaril@0
|
189 |
}
|
insilmaril@0
|
190 |
}
|
insilmaril@0
|
191 |
}
|
insilmaril@0
|
192 |
}
|
insilmaril@0
|
193 |
setVisibility (visible);
|
insilmaril@2
|
194 |
move (absPos.x(),absPos.y());
|
insilmaril@0
|
195 |
calcBBoxSize();
|
insilmaril@0
|
196 |
}
|
insilmaril@0
|
197 |
|
insilmaril@0
|
198 |
QString HeadingObj::text ()
|
insilmaril@0
|
199 |
{
|
insilmaril@0
|
200 |
return heading;
|
insilmaril@0
|
201 |
}
|
insilmaril@0
|
202 |
|
insilmaril@0
|
203 |
void HeadingObj::setFont (QFont f)
|
insilmaril@0
|
204 |
{
|
insilmaril@0
|
205 |
if (font!=f)
|
insilmaril@0
|
206 |
{
|
insilmaril@0
|
207 |
font=f;
|
insilmaril@0
|
208 |
setText (text());
|
insilmaril@0
|
209 |
}
|
insilmaril@0
|
210 |
}
|
insilmaril@0
|
211 |
|
insilmaril@0
|
212 |
QFont HeadingObj::getFont()
|
insilmaril@0
|
213 |
{
|
insilmaril@0
|
214 |
return font;
|
insilmaril@0
|
215 |
}
|
insilmaril@0
|
216 |
|
insilmaril@0
|
217 |
|
insilmaril@0
|
218 |
void HeadingObj::setColor (QColor c)
|
insilmaril@0
|
219 |
{
|
insilmaril@0
|
220 |
if (color!=c)
|
insilmaril@0
|
221 |
{
|
insilmaril@0
|
222 |
color=c;
|
insilmaril@2
|
223 |
Q3CanvasText *t;
|
insilmaril@0
|
224 |
for (t=textline.first(); t; t=textline.next() )
|
insilmaril@0
|
225 |
t->setColor(c);
|
insilmaril@0
|
226 |
}
|
insilmaril@0
|
227 |
}
|
insilmaril@0
|
228 |
|
insilmaril@0
|
229 |
QColor HeadingObj::getColor()
|
insilmaril@0
|
230 |
{
|
insilmaril@0
|
231 |
return color;
|
insilmaril@0
|
232 |
}
|
insilmaril@0
|
233 |
|
insilmaril@0
|
234 |
void HeadingObj::setVisibility (bool v)
|
insilmaril@0
|
235 |
{
|
insilmaril@0
|
236 |
MapObj::setVisibility(v);
|
insilmaril@2
|
237 |
Q3CanvasText *t;
|
insilmaril@0
|
238 |
for (t=textline.first(); t; t=textline.next() )
|
insilmaril@0
|
239 |
if (v)
|
insilmaril@0
|
240 |
t->show();
|
insilmaril@0
|
241 |
else
|
insilmaril@0
|
242 |
t->hide();
|
insilmaril@0
|
243 |
}
|
insilmaril@0
|
244 |
|
insilmaril@0
|
245 |
int HeadingObj::getHeight ()
|
insilmaril@0
|
246 |
{
|
insilmaril@0
|
247 |
return bbox.height();
|
insilmaril@0
|
248 |
}
|
insilmaril@0
|
249 |
|
insilmaril@0
|
250 |
int HeadingObj::getWidth()
|
insilmaril@0
|
251 |
{
|
insilmaril@0
|
252 |
return bbox.width();
|
insilmaril@0
|
253 |
}
|
insilmaril@0
|
254 |
|