1 #include "headingobj.h"
4 /////////////////////////////////////////////////////////////////
6 /////////////////////////////////////////////////////////////////
7 HeadingObj::HeadingObj() : MapObj()
9 // cout << "Const HeadingObj ()\n";
13 HeadingObj::HeadingObj(QGraphicsScene *s) :MapObj(s)
15 // cout << "Const HeadingObj (s)\n";
19 HeadingObj::~HeadingObj()
21 // cout << "Destr. HeadingObj "<<heading.ascii()<<endl;
22 while (!textline.isEmpty())
23 delete textline.takeFirst();
26 void HeadingObj::init()
29 color=QColor ("black");
34 void HeadingObj::copy(HeadingObj *other)
37 textwidth=other->textwidth;
40 setText (other->text() );
43 void HeadingObj::move(double x, double y)
47 qreal h; // height of a textline
48 qreal ho; // offset of height while drawing all lines
50 if (!textline.isEmpty() )
51 h=textline.first()->boundingRect().height();
55 for (int i=0; i<textline.size(); ++i)
57 textline.at(i)->setPos(x,y+ho);
63 void HeadingObj::moveBy(double x, double y)
65 move (x+absPos.x(),y+absPos.y() );
68 void HeadingObj::positionBBox()
70 bbox.setX (absPos.x());
71 bbox.setY (absPos.y());
74 void HeadingObj::calcBBoxSize()
78 // Using Backspace an empty heading might easily be created, then there
79 // would be textline.first()==NULL This can be worked around by the following, but
80 // then no selection would be visible, thus we prevent it in ::setText()
81 if (!textline.isEmpty() )
83 for (int i=0; i<textline.size(); i++)
85 h+=textline.at(i)->boundingRect().height();
86 if (w<textline.at(i)->boundingRect().width() )
87 w=textline.at(i)->boundingRect().width();
90 bbox.setSize (QSizeF(w,h));
93 //QGraphicsSimpleTextItem* HeadingObj::newLine(QString s)
94 QGraphicsTextItem* HeadingObj::newLine(QString s)
96 //QGraphicsSimpleTextItem *t=new QGraphicsSimpleTextItem (s,0,scene);
97 QGraphicsTextItem *t=new QGraphicsTextItem (s,0,scene);
101 t->setDefaultTextColor(color);
103 //t->setBrush(color);
107 void HeadingObj::setText (QString s)
111 // remove old textlines and prepare generating new ones
112 while (!textline.isEmpty())
113 delete textline.takeFirst();
115 if (s.startsWith("<html>"))
117 QGraphicsTextItem *t=new QGraphicsTextItem ();
119 t->setZValue(Z_TEXT);
121 t->setDefaultTextColor(color);
124 setVisibility (visible);
125 move (absPos.x(),absPos.y());
130 // prevent empty textline, so at least a small selection stays
131 // visible for this heading
132 if (s.length()==0) s=" ";
134 int i=0; // index for actual search for ws
135 int j=0; // index of last ws
136 int k=0; // index of "<br>" or similar linebreak
137 int br=0; // width of found break, e.g. for <br> it is 4
138 QRegExp re("<br.*/>");
139 re.setMinimal (true);
141 // set the text and wrap lines
144 // ok, some people wanted manual linebreaks, here we go
148 br=re.cap(0).length();
151 i=s.find (" ",i,false);
153 { // no ws found at all in s
155 textline.append (newLine(s));
160 { // no ws found in actual search
161 if (s.length()<=textwidth)
163 textline.append (newLine(s));
167 textline.append (newLine(s.left(j)));
168 s=s.mid(j+1,s.length());
173 if (i>= 0 && i<=static_cast <int> (textwidth))
174 { // there is a ws in textwidth
177 // here is a linebreak
178 textline.append (newLine(s.left(i)));
179 s=s.mid(i+br,s.length());
190 if (i>static_cast <int> (textwidth) )
193 { // a ws out of textwidth, but we have also one in
194 textline.append (newLine(s.left(j)));
195 s=s.mid(j+1,s.length());
199 { // a ws out of text, but none in
200 textline.append (newLine(s.left(i)));
201 s=s.mid(i+1,s.length());
209 setVisibility (visible);
210 move (absPos.x(),absPos.y());
214 QString HeadingObj::text ()
219 void HeadingObj::setFont (QFont f)
228 QFont HeadingObj::getFont()
234 void HeadingObj::setColor (QColor c)
239 for (int i=0; i<textline.size(); ++i)
241 textline.at(i)->setDefaultTextColor(c);
243 //textline.at(i)->setBrush(c);
247 QColor HeadingObj::getColor()
252 void HeadingObj::setVisibility (bool v)
254 MapObj::setVisibility(v);
255 for (int i=0; i<textline.size(); ++i)
257 textline.at(i)->show();
259 textline.at(i)->hide();
262 qreal HeadingObj::getHeight ()
264 return bbox.height();
267 qreal HeadingObj::getWidth()