libyui-qt  2.53.0
YQLabel.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YQLabel.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <qlabel.h>
27 
28 #define YUILogComponent "qt-ui"
29 #include <yui/YUILog.h>
30 
31 #include "utf8.h"
32 #include "YQUI.h"
33 #include "YQApplication.h"
34 #include "YQLabel.h"
35 
36 #define AUTO_WRAP_WIDTH 150
37 #define AUTO_WRAP_HEIGHT 10
38 
39 using std::string;
40 
41 
42 
43 YQLabel::YQLabel( YWidget * parent,
44  const string & text,
45  bool isHeading,
46  bool isOutputField )
47  : QLabel( (QWidget *) parent->widgetRep() )
48  , YLabel( parent, text, isHeading, isOutputField )
49  , _layoutPass1Width( 0 )
50 {
51  setWidgetRep( this );
52 
53  setTextInteractionFlags( Qt::TextSelectableByMouse );
54  setTextFormat( Qt::PlainText );
55  QLabel::setText( fromUTF8( text ) );
56  setIndent(0);
57 
58  if ( isHeading )
59  {
60  setFont( YQUI::yqApp()->headingFont() );
61  }
62  else if ( isOutputField )
63  {
64  setFrameStyle ( QFrame::Panel | QFrame::Sunken );
65  setLineWidth(2);
66  setMidLineWidth(2);
67  }
68 
69  setMargin( YQWidgetMargin );
70  setAlignment( Qt::AlignLeft | Qt::AlignTop );
71 }
72 
73 
75 {
76  // NOP
77 }
78 
79 
80 void YQLabel::setText( const string & newText )
81 {
82  YLabel::setText( newText );
83  QLabel::setText( fromUTF8( newText ) );
84 }
85 
86 
87 void YQLabel::setUseBoldFont( bool useBold )
88 {
89  setFont( useBold ?
90  YQUI::yqApp()->boldFont() :
91  YQUI::yqApp()->currentFont() );
92 
93  YLabel::setUseBoldFont( useBold );
94 }
95 
96 
97 void YQLabel::setAutoWrap( bool autoWrap )
98 {
99  YLabel::setAutoWrap( autoWrap );
100  QLabel::setWordWrap( autoWrap );
101 }
102 
103 
104 void YQLabel::setEnabled( bool enabled )
105 {
106  QLabel::setEnabled( enabled );
107  YWidget::setEnabled( enabled );
108 }
109 
110 
112 {
113  int width;
114 
115  if ( autoWrap() )
116  {
117  if ( layoutPass() == 2 )
118  {
119  // Use the width passed down to us from the parent layout manager
120  // in the last setSize() call. This is the definitive width that
121  // will be used after taking all other children of the layout into
122  // consideration, also including making widgets smaller due to size
123  // restrictions, or redistributing excess space.
124  //
125  // Since this widget can auto-wrap its contents, we accept
126  // basically any width; we just need to adapt the preferred height
127  // accordingly.
128  width = _layoutPass1Width;
129  }
130  else
131  {
132  // Use a preliminary width. Typically, this widget should be
133  // wrapped into a MinSize or MinWidth which hopefully gives us a
134  // much more useful width than this.
135  //
136  // We would also just use 0 here, but that would make debugging
137  // really hard since the widget might completly disappear.
138  //
139  // The real width that will be used will be set in the setSize()
140  // call following the recursive preferredWidth() /
141  // preferredHeight() calls in the widget tree.
142  width = AUTO_WRAP_WIDTH;
143  }
144  }
145  else // ! autoWrap()
146  {
147  width = sizeHint().width();
148  }
149 
150  return width;
151 }
152 
153 
155 {
156  int height;
157 
158  if ( autoWrap() )
159  {
160  if ( layoutPass() == 2 )
161  {
162  // This is where the magic happens:
163  //
164  // setSize() in the first layout pass gave us the real width which
165  // we stored in _layoutPass1Width. We can now calculate the height
166  // that is really needed based on that width. QLabel provides this
167  // handy function that takes word wrapping and font metrics into
168  // account (remember, we are using a proportional font, so every
169  // letter has a different width).
170 
171  height = heightForWidth( _layoutPass1Width );
172  }
173  else
174  {
175  height = AUTO_WRAP_HEIGHT;
176  }
177  }
178  else // ! autoWrap()
179  {
180  height = sizeHint().height();
181  }
182 
183  return height;
184 }
185 
186 
187 void YQLabel::setSize( int newWidth, int newHeight )
188 {
189  if ( autoWrap() )
190  {
191  _layoutPass1Width = layoutPass() == 1 ?
192  newWidth : 0;
193  }
194 
195  resize( newWidth, newHeight );
196 }
YQLabel::preferredWidth
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQLabel.cc:111
YQLabel::~YQLabel
virtual ~YQLabel()
Destructor.
Definition: YQLabel.cc:74
YQLabel::setEnabled
virtual void setEnabled(bool enabled)
Set enabled / disabled state.
Definition: YQLabel.cc:104
YQLabel::setText
virtual void setText(const std::string &newText)
Set the text the widget displays.
Definition: YQLabel.cc:80
YQUI::yqApp
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
Definition: YQUI.cc:268
YQLabel::preferredHeight
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQLabel.cc:154
YQLabel::setUseBoldFont
virtual void setUseBoldFont(bool bold)
Switch bold font on or off.
Definition: YQLabel.cc:87
YQLabel::setSize
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQLabel.cc:187
YQLabel::YQLabel
YQLabel(YWidget *parent, const std::string &text, bool isHeading=false, bool isOutputField=false)
Constructor.
Definition: YQLabel.cc:43