2 #include <QGraphicsScene>
5 #include "headingobj.h"
9 /////////////////////////////////////////////////////////////////
11 /////////////////////////////////////////////////////////////////
12 HeadingObj::HeadingObj(QGraphicsItem *parent) : MapObj(parent)
14 // qDebug() << "Const HeadingObj (s) ";
18 HeadingObj::~HeadingObj()
20 // qDebug() << "Destr. HeadingObj "<<heading;
21 while (!textline.isEmpty())
22 delete textline.takeFirst();
25 void HeadingObj::init()
28 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) {
56 textline.at(i)->setPos(x, y + ho);
61 void HeadingObj::moveBy(double x, double y)
63 move(x + absPos.x(), y + absPos.y());
66 void HeadingObj::positionBBox()
68 bbox.setX(absPos.x());
69 bbox.setY(absPos.y());
72 void HeadingObj::calcBBoxSize()
76 // Using Backspace an empty heading might easily be created, then there
77 // would be textline.first()==NULL This can be worked around by the
78 // following, but then no selection would be visible, thus we prevent it in
80 if (!textline.isEmpty()) {
81 for (int i = 0; i < textline.size(); i++) {
82 h += textline.at(i)->boundingRect().height();
83 if (w < textline.at(i)->boundingRect().width())
84 w = textline.at(i)->boundingRect().width();
87 bbox.setSize(QSizeF(w, h));
90 QGraphicsTextItem *HeadingObj::newLine(QString s)
92 QGraphicsTextItem *t = new QGraphicsTextItem(s, parentItem());
94 t->setZValue(dZ_TEXT);
95 t->setDefaultTextColor(color);
96 t->setRotation(angle);
100 void HeadingObj::setTransformOriginPoint(const QPointF &p)
102 for (int i = 0; i < textline.size(); i++) {
103 textline.at(i)->setTransformOriginPoint(p);
107 void HeadingObj::setRotation(const qreal &a)
110 for (int i = 0; i < textline.size(); i++)
111 textline.at(i)->setRotation(angle);
114 qreal HeadingObj::getRotation() { return angle; }
116 void HeadingObj::setText(QString s)
120 // remove old textlines and prepare generating new ones
121 while (!textline.isEmpty())
122 delete textline.takeFirst();
124 if (s.startsWith("<html>") ||
125 s.startsWith("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" "
126 "\"http://www.w3.org/TR/REC-html40/strict.dtd\">")) {
127 QGraphicsTextItem *t = new QGraphicsTextItem();
129 t->setZValue(dZ_TEXT);
131 t->setDefaultTextColor(color);
132 t->setRotation(angle);
137 // prevent empty textline, so at least a small selection stays
138 // visible for this heading
142 int i = 0; // index for actual search for ws
143 int j = 0; // index of last ws
144 int k = 0; // index of "<br>" or similar linebreak
145 int br = 0; // width of found break, e.g. for <br> it is 4
146 QRegExp re("<br.*/>");
149 // set the text and wrap lines
150 while (s.length() > 0) {
151 // ok, some people wanted manual linebreaks, here we go
152 k = re.indexIn(s, i);
154 br = re.cap(0).length();
158 i = s.indexOf(" ", i);
159 if (i < 0 && j == 0) { // no ws found at all in s
161 textline.append(newLine(s));
165 if (i < 0 && j > 0) { // no ws found in actual search
166 if (s.length() <= textwidth) {
167 textline.append(newLine(s));
171 textline.append(newLine(s.left(j)));
172 s = s.mid(j + 1, s.length());
178 i <= static_cast<int>(
179 textwidth)) { // there is a ws in textwidth
181 // here is a linebreak
182 textline.append(newLine(s.left(i)));
183 s = s.mid(i + br, s.length());
194 if (i > static_cast<int>(textwidth)) {
195 if (j > 0) { // a ws out of textwidth, but we have
197 textline.append(newLine(s.left(j)));
198 s = s.mid(j + 1, s.length());
202 else { // a ws out of text, but none in
203 textline.append(newLine(s.left(i)));
204 s = s.mid(i + 1, s.length());
212 } // ASCII heading with multiple lines
213 setVisibility(visible);
214 move(absPos.x(), absPos.y());
218 QString HeadingObj::text() { return heading; }
220 void HeadingObj::setFont(QFont f)
228 QFont HeadingObj::getFont() { return font; }
230 void HeadingObj::setColor(QColor c)
234 for (int i = 0; i < textline.size(); ++i)
236 textline.at(i)->setDefaultTextColor(c);
238 // textline.at(i)->setBrush(c);
242 QColor HeadingObj::getColor() { return color; }
244 void HeadingObj::setZValue(double z)
246 for (int i = 0; i < textline.size(); ++i)
247 textline.at(i)->setZValue(z);
250 void HeadingObj::setVisibility(bool v)
252 MapObj::setVisibility(v);
253 for (int i = 0; i < textline.size(); ++i)
255 textline.at(i)->show();
257 textline.at(i)->hide();
260 qreal HeadingObj::getHeight() { return bbox.height(); }
262 qreal HeadingObj::getWidth() { return bbox.width(); }