insilmaril@0: #include "headingobj.h" 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@0: HeadingObj::HeadingObj(QCanvas* c) :MapObj(c) insilmaril@0: { insilmaril@0: // cout << "Const HeadingObj\n"; insilmaril@0: init (); insilmaril@0: } insilmaril@0: insilmaril@0: HeadingObj::~HeadingObj() insilmaril@0: { insilmaril@0: textline.clear(); insilmaril@0: } insilmaril@0: insilmaril@0: void HeadingObj::init() insilmaril@0: { insilmaril@0: textline.setAutoDelete (TRUE); insilmaril@0: textwidth=40; insilmaril@0: color=QColor ("black"); insilmaril@0: font=QFont(); insilmaril@0: } insilmaril@0: insilmaril@0: void HeadingObj::copy(HeadingObj *other) insilmaril@0: { insilmaril@0: MapObj::copy (other); insilmaril@0: textwidth=other->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@0: int h; // height of a textline insilmaril@0: int ho; // offset of height while drawing all lines insilmaril@0: if (textline.first() ) insilmaril@0: h=textline.first()->boundingRect().height(); insilmaril@0: else insilmaril@0: h=2; insilmaril@0: QCanvasText *t; insilmaril@0: ho=0; insilmaril@0: for (t=textline.first(); t; t=textline.next() ) insilmaril@0: { insilmaril@0: t->move(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@0: int w=0; insilmaril@0: int 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@0: QCanvasText *t; insilmaril@0: for (t=textline.first(); t; t=textline.next() ) insilmaril@0: { insilmaril@0: h+=t->boundingRect().height(); insilmaril@0: if (wboundingRect().width() ) insilmaril@0: w=t->boundingRect().width(); insilmaril@0: } insilmaril@0: } insilmaril@0: bbox.setSize (QSize(w,h)); insilmaril@0: } insilmaril@0: insilmaril@0: QCanvasText* HeadingObj::newLine(QString s) insilmaril@0: { insilmaril@0: QCanvasText *t; insilmaril@0: t = new QCanvasText(canvas); insilmaril@0: t->setFont (font); insilmaril@0: t->setColor (color); insilmaril@0: t->setZ(Z_TEXT); insilmaril@0: t->setText(s); insilmaril@166: 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@0: textline.clear(); 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@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@0: k=s.find ("
",i,false); insilmaril@0: if (k>=0) insilmaril@0: { insilmaril@0: br=4; 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@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@0: QCanvasText *t; insilmaril@0: for (t=textline.first(); t; t=textline.next() ) insilmaril@0: t->setColor(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@0: QCanvasText *t; insilmaril@0: for (t=textline.first(); t; t=textline.next() ) insilmaril@0: if (v) insilmaril@0: t->show(); insilmaril@0: else insilmaril@0: t->hide(); insilmaril@0: } insilmaril@0: insilmaril@0: int HeadingObj::getHeight () insilmaril@0: { insilmaril@0: return bbox.height(); insilmaril@0: } insilmaril@0: insilmaril@0: int HeadingObj::getWidth() insilmaril@0: { insilmaril@0: return bbox.width(); insilmaril@0: } insilmaril@0: