insilmaril@0: #include "headingobj.h" insilmaril@185: #include insilmaril@0: insilmaril@0: ///////////////////////////////////////////////////////////////// insilmaril@0: // HeadingObj insilmaril@0: ///////////////////////////////////////////////////////////////// insilmaril@0: HeadingObj::HeadingObj() : MapObj() insilmaril@0: { insilmaril@0: // cout << "Const HeadingObj ()\n"; insilmaril@0: init (); insilmaril@0: } insilmaril@0: insilmaril@408: HeadingObj::HeadingObj(QGraphicsScene *s) :MapObj(s) insilmaril@0: { insilmaril@408: // cout << "Const HeadingObj (s)\n"; insilmaril@0: init (); insilmaril@0: } insilmaril@0: insilmaril@0: HeadingObj::~HeadingObj() insilmaril@0: { insilmaril@406: // cout << "Destr. HeadingObj "<textwidth; insilmaril@0: color=other->color; insilmaril@0: font=other->font; insilmaril@0: setText (other->text() ); insilmaril@0: } insilmaril@0: insilmaril@0: void HeadingObj::move(double x, double y) insilmaril@0: { insilmaril@0: MapObj::move(x,y); insilmaril@173: insilmaril@408: qreal h; // height of a textline insilmaril@408: qreal ho; // offset of height while drawing all lines insilmaril@173: insilmaril@406: if (!textline.isEmpty() ) insilmaril@0: h=textline.first()->boundingRect().height(); insilmaril@0: else insilmaril@0: h=2; insilmaril@0: ho=0; insilmaril@406: for (int i=0; isetPos(x,y+ho); insilmaril@0: ho=ho+h; insilmaril@0: } insilmaril@0: } insilmaril@0: insilmaril@0: insilmaril@0: void HeadingObj::moveBy(double x, double y) insilmaril@0: { insilmaril@0: move (x+absPos.x(),y+absPos.y() ); insilmaril@0: } insilmaril@0: insilmaril@0: void HeadingObj::positionBBox() insilmaril@0: { insilmaril@0: bbox.setX (absPos.x()); insilmaril@0: bbox.setY (absPos.y()); insilmaril@0: } insilmaril@0: insilmaril@0: void HeadingObj::calcBBoxSize() insilmaril@0: { insilmaril@408: qreal w=0; insilmaril@408: qreal h=0; insilmaril@0: // Using Backspace an empty heading might easily be created, then there insilmaril@0: // would be textline.first()==NULL This can be worked around by the following, but insilmaril@0: // then no selection would be visible, thus we prevent it in ::setText() insilmaril@0: if (!textline.isEmpty() ) insilmaril@0: { insilmaril@406: for (int i=0; iboundingRect().height(); insilmaril@406: if (wboundingRect().width() ) insilmaril@406: w=textline.at(i)->boundingRect().width(); insilmaril@0: } insilmaril@0: } insilmaril@408: bbox.setSize (QSizeF(w,h)); insilmaril@0: } insilmaril@0: insilmaril@408: QGraphicsTextItem* HeadingObj::newLine(QString s) insilmaril@0: { insilmaril@408: QGraphicsTextItem *t=scene->addText(""); insilmaril@0: t->setFont (font); insilmaril@408: t->setZValue(Z_TEXT); insilmaril@408: t->setDefaultTextColor(color); insilmaril@408: t->setPlainText(s); insilmaril@408: //t->setTextFlags(Qt::AlignLeft); insilmaril@0: t->show(); insilmaril@0: return t; insilmaril@0: } insilmaril@0: insilmaril@0: void HeadingObj::setText (QString s) insilmaril@0: { insilmaril@0: heading=s; insilmaril@0: insilmaril@0: // remove old textlines and prepare generating new ones insilmaril@406: while (!textline.isEmpty()) insilmaril@406: delete textline.takeFirst(); insilmaril@0: insilmaril@0: // prevent empty textline, so at least a small selection stays insilmaril@0: // visible for this heading insilmaril@0: if (s.length()==0) s=" "; insilmaril@0: insilmaril@0: int i=0; // index for actual search for ws insilmaril@0: int j=0; // index of last ws insilmaril@0: int k=0; // index of "
" or similar linebreak insilmaril@0: int br=0; // width of found break, e.g. for
it is 4 insilmaril@185: QRegExp re(""); insilmaril@185: re.setMinimal (true); insilmaril@0: insilmaril@0: // set the text and wrap lines insilmaril@0: while (s.length()>0) insilmaril@0: { insilmaril@0: // ok, some people wanted manual linebreaks, here we go insilmaril@185: k=re.search (s,i); insilmaril@0: if (k>=0) insilmaril@0: { insilmaril@185: br=re.cap(0).length(); insilmaril@0: i=k; insilmaril@0: } else insilmaril@0: i=s.find (" ",i,false); insilmaril@0: if (i<0 && j==0) insilmaril@0: { // no ws found at all in s insilmaril@0: // append whole s insilmaril@0: textline.append (newLine(s)); insilmaril@0: s=""; insilmaril@0: } else insilmaril@0: { insilmaril@0: if (i<0 && j>0) insilmaril@0: { // no ws found in actual search insilmaril@0: if (s.length()<=textwidth) insilmaril@0: { insilmaril@0: textline.append (newLine(s)); insilmaril@0: s=""; insilmaril@0: } else insilmaril@0: { insilmaril@0: textline.append (newLine(s.left(j))); insilmaril@0: s=s.mid(j+1,s.length()); insilmaril@0: j=0; insilmaril@0: } insilmaril@0: } else insilmaril@0: { insilmaril@0: if (i>= 0 && i<=static_cast (textwidth)) insilmaril@0: { // there is a ws in textwidth insilmaril@0: if (br>0) insilmaril@0: { insilmaril@0: // here is a linebreak insilmaril@0: textline.append (newLine(s.left(i))); insilmaril@0: s=s.mid(i+br,s.length()); insilmaril@0: i=0; insilmaril@0: j=0; insilmaril@0: br=0; insilmaril@0: } else insilmaril@0: { insilmaril@0: j=i; insilmaril@0: i++; insilmaril@0: } insilmaril@0: } else insilmaril@0: { insilmaril@0: if (i>static_cast (textwidth) ) insilmaril@0: { insilmaril@0: if (j>0) insilmaril@0: { // a ws out of textwidth, but we have also one in insilmaril@0: textline.append (newLine(s.left(j))); insilmaril@0: s=s.mid(j+1,s.length()); insilmaril@0: i=0; insilmaril@0: j=0; insilmaril@0: } else insilmaril@0: { // a ws out of text, but none in insilmaril@0: textline.append (newLine(s.left(i))); insilmaril@0: s=s.mid(i+1,s.length()); insilmaril@0: i=0; insilmaril@0: } insilmaril@0: } insilmaril@0: } insilmaril@0: } insilmaril@0: } insilmaril@0: } insilmaril@0: setVisibility (visible); insilmaril@173: move (absPos.x(),absPos.y()); insilmaril@0: calcBBoxSize(); insilmaril@0: } insilmaril@0: insilmaril@0: QString HeadingObj::text () insilmaril@0: { insilmaril@0: return heading; insilmaril@0: } insilmaril@0: insilmaril@0: void HeadingObj::setFont (QFont f) insilmaril@0: { insilmaril@0: if (font!=f) insilmaril@0: { insilmaril@0: font=f; insilmaril@0: setText (text()); insilmaril@0: } insilmaril@0: } insilmaril@0: insilmaril@0: QFont HeadingObj::getFont() insilmaril@0: { insilmaril@0: return font; insilmaril@0: } insilmaril@0: insilmaril@0: insilmaril@0: void HeadingObj::setColor (QColor c) insilmaril@0: { insilmaril@0: if (color!=c) insilmaril@0: { insilmaril@0: color=c; insilmaril@406: for (int i=0; isetDefaultTextColor(c); insilmaril@0: } insilmaril@0: } insilmaril@0: insilmaril@0: QColor HeadingObj::getColor() insilmaril@0: { insilmaril@0: return color; insilmaril@0: } insilmaril@0: insilmaril@0: void HeadingObj::setVisibility (bool v) insilmaril@0: { insilmaril@0: MapObj::setVisibility(v); insilmaril@406: for (int i=0; ishow(); insilmaril@0: else insilmaril@406: textline.at(i)->hide(); insilmaril@0: } insilmaril@0: insilmaril@408: qreal HeadingObj::getHeight () insilmaril@0: { insilmaril@0: return bbox.height(); insilmaril@0: } insilmaril@0: insilmaril@408: qreal HeadingObj::getWidth() insilmaril@0: { insilmaril@0: return bbox.width(); insilmaril@0: } insilmaril@0: