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