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