summaryrefslogtreecommitdiffstats
path: root/kugar/lib/mlabelobject.cpp
blob: bc01c22d5268601fd83352dbe7f92d01a46cfdb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/***************************************************************************
             mlabelobject.cpp  -  Kugar report label object
             -------------------
   begin     : Wed Aug 11 1999
   copyright : (C) 1999 by Mutiny Bay Software
   email     : info@mutinybaysoftware.com
   copyright : (C) 2002 Alexander Dymo
   email     : cloudtemple@mksat.net
***************************************************************************/

//#include <kglobalsettings.h>
#include <qfont.h>

#include "mlabelobject.h"

namespace Kugar
{

/** Constructor */
MLabelObject::MLabelObject() : MReportObject(), xMargin( 0 ), yMargin( 0 )
{
    // Set the default label text
    text = "";

    // Set the default font
    QFont defaultFont;
    fontFamily = defaultFont.family();
    fontSize = 10;
    fontWeight = MLabelObject::Normal;
    fontItalic = false;

    // Set the default alignment
    hAlignment = MLabelObject::Left;
    vAlignment = MLabelObject::Top;
    wordWrap = false;
}

/** Copy constructor */
MLabelObject::MLabelObject( const MLabelObject& mLabelObject ) :
        MReportObject( ( MReportObject & ) mLabelObject ), xMargin( 0 ), yMargin( 0 )
{
    copy( &mLabelObject );
}

/** Assignment operator */
MLabelObject MLabelObject::operator=( const MLabelObject& mLabelObject )
{
    if ( &mLabelObject == this )
        return * this;

    // Copy the derived class's data
    copy( &mLabelObject );

    // Copy the base class's data
    ( ( MReportObject & ) * this ) = mLabelObject;

    return *this;
}

/** Destructor */
MLabelObject::~MLabelObject()
{}

/** Sets the label's text string */
void MLabelObject::setText( const QString txt )
{
    text = txt;
}

/** Sets the label's text font */
void MLabelObject::setFont( const QString family, int size, int weight, bool italic )
{
    fontFamily = family;
    fontSize = size;
    fontWeight = weight;
    fontItalic = italic;
}

/** Sets the label's horizontal alignment */
void MLabelObject::setHorizontalAlignment( int a )
{
    hAlignment = a;
}

/** Sets the label's vertical alignment */
void MLabelObject::setVerticalAlignment( int a )
{
    vAlignment = a;
}

/** Sets the label's word wrap flag */
void MLabelObject::setWordWrap( bool state )
{
    wordWrap = state;
}

/** Draws the label using the specificed painter & x/y-offsets */
void MLabelObject::draw( QPainter* p, int xoffset, int yoffset )
{
    QFont font( fontFamily, fontSize, fontWeight, fontItalic );
    QPen textPen( foregroundColor, 0, QPen::NoPen );

    int tf;

    // Set the offsets
    int xcalc = xpos + xoffset;
    int ycalc = ypos + yoffset;

    // Draw the base
    drawBase( p, xoffset, yoffset );

    // Set the font
    p->setFont( font );
    QFontMetrics fm = p->fontMetrics();

    // Set the text alignment flags

    // Horizontal
    switch ( hAlignment )
    {
    case MLabelObject::Left:
        tf = QPainter::AlignLeft;
        break;
    case MLabelObject::Center:
        tf = QPainter::AlignHCenter;
        break;
    case MLabelObject::Right:
        tf = QPainter::AlignRight;
    }

    // Vertical
    switch ( vAlignment )
    {
    case MLabelObject::Top:
        tf = tf | QPainter::AlignTop;
        break;
    case MLabelObject::Bottom:
        tf = tf | QPainter::AlignBottom;
        break;
    case MLabelObject::Middle:
        tf = tf | QPainter::AlignVCenter;
    }

    // Word wrap
    if ( wordWrap )
        tf = tf | QPainter::WordBreak;

    // Draw the text
    p->setPen( textPen );
    p->drawText( xcalc + xMargin, ycalc + yMargin,
                 width - xMargin, height - yMargin,
                 tf, text );
}

/** Copies member data from one object to another.
      Used by the copy constructor and assignment operator */
void MLabelObject::copy( const MLabelObject* mLabelObject )
{
    // Copy the label's text
    text = mLabelObject->text;

    // Copy the label's font data
    fontFamily = mLabelObject->fontFamily;
    fontSize = mLabelObject->fontSize;
    fontWeight = mLabelObject->fontWeight;
    fontItalic = mLabelObject->fontItalic;

    // Copy the label's alignment data
    vAlignment = mLabelObject->vAlignment;
    hAlignment = mLabelObject->hAlignment;
    wordWrap = mLabelObject->wordWrap;
}

}