certmanager/lib

kdhorizontalline.cpp
1 /*
2  KD Tools - a set of useful widgets for TQt
3 */
4 
5 /****************************************************************************
6 ** Copyright (C) 2005 Klarälvdalens Datakonsult AB. All rights reserved.
7 **
8 ** This file is part of the KD Tools library.
9 **
10 ** This file may be distributed and/or modified under the terms of the
11 ** GNU General Public License version 2 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.GPL included in the
13 ** packaging of this file.
14 **
15 ** Licensees holding valid commercial KD Tools licenses may use this file in
16 ** accordance with the KD Tools Commercial License Agreement provided with
17 ** the Software.
18 **
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 **
22 ** See http://www.klaralvdalens-datakonsult.se/?page=products for
23 ** information about KD Tools Commercial License Agreements.
24 **
25 ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
26 ** licensing are not clear to you.
27 **
28 ** In addition, as a special exception, the copyright holders give
29 ** permission to link the code of this program with any edition of the
30 ** TQt library by Trolltech AS, Norway (or with modified versions of TQt
31 ** that use the same license as TQt), and distribute linked
32 ** combinations including the two. You must obey the GNU General
33 ** Public License in all respects for all of the code used other than
34 ** TQt. If you modify this file, you may extend this exception to your
35 ** version of the file, but you are not obligated to do so. If you do
36 ** not wish to do so, delete this exception statement from your
37 ** version.
38 **
39 **********************************************************************/
40 
41 #include "kdhorizontalline.h"
42 
43 #include <tqstyle.h>
44 #include <tqpainter.h>
45 #ifdef TQT_ACCESSIBILITY_SUPPORT
46 #include <tqaccessible.h>
47 #endif
48 #include <tqfontmetrics.h>
49 #include <tqapplication.h>
50 
51 KDHorizontalLine::KDHorizontalLine( TQWidget * parent, const char * name, WFlags f )
52  : TQFrame( parent, name, f ),
53  mAlign( TQt::AlignAuto ),
54  mLenVisible( 0 )
55 {
56  TQFrame::setFrameStyle( HLine | Sunken );
57 }
58 
59 KDHorizontalLine::KDHorizontalLine( const TQString & title, TQWidget * parent, const char * name, WFlags f )
60  : TQFrame( parent, name, f ),
61  mAlign( TQt::AlignAuto ),
62  mLenVisible( 0 )
63 {
64  TQFrame::setFrameStyle( HLine | Sunken );
65  setTitle( title );
66 }
67 
68 KDHorizontalLine::~KDHorizontalLine() {}
69 
70 void KDHorizontalLine::setFrameStyle( int style ) {
71  TQFrame::setFrameStyle( ( style & ~MShape ) | HLine ); // force HLine
72 }
73 
74 void KDHorizontalLine::setTitle( const TQString & title ) {
75  if ( mTitle == title )
76  return;
77  mTitle = title;
78  calculateFrame();
79  update();
80  updateGeometry();
81 #ifdef TQT_ACCESSIBILITY_SUPPORT
82  TQAccessible::updateAccessibility( this, 0, TQAccessible::NameChanged );
83 #endif
84 }
85 
86 void KDHorizontalLine::calculateFrame() {
87  mLenVisible = mTitle.length();
88 #if 0
89  if ( mLenVisible ) {
90  const TQFontMetrics fm = fontMetrics();
91  while ( mLenVisible ) {
92  const int tw = fm.width( mTitle, mLenVisible ) + 4*fm.width(TQChar(' '));
93  if ( tw < width() )
94  break;
95  mLenVisible--;
96  }
97  tqDebug( "mLenVisible = %d (of %d)", mLenVisible, mTitle.length() );
98  if ( mLenVisible ) { // but do we also have a visible label?
99  TQRect r = rect();
100  const int va = style().styleHint( TQStyle::SH_GroupBox_TextLabelVerticalAlignment, this );
101  if( va & AlignVCenter )
102  r.setTop( fm.height() / 2 ); // frame rect should be
103  else if( va & AlignTop )
104  r.setTop( fm.ascent() );
105  setFrameRect( r ); // smaller than client rect
106  return;
107  }
108  }
109  // no visible label
110  setFrameRect( TQRect(0,0,0,0) ); // then use client rect
111 #endif
112 }
113 
114 TQSizePolicy KDHorizontalLine::sizePolicy() const {
115  return TQSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Fixed );
116 }
117 
118 TQSize KDHorizontalLine::sizeHint() const {
119  return minimumSizeHint();
120 }
121 
122 TQSize KDHorizontalLine::minimumSizeHint() const {
123  const int w = fontMetrics().width( mTitle, mLenVisible ) +
124  fontMetrics().width( TQChar( ' ' ) );
125  const int h = fontMetrics().height();
126  return TQSize( TQMAX( w, indentHint() ), h ).expandedTo( tqApp->globalStrut() );
127 }
128 
129 void KDHorizontalLine::paintEvent( TQPaintEvent * e ) {
130  TQPainter paint( this );
131 
132  if ( mLenVisible ) { // draw title
133  const TQFontMetrics & fm = paint.fontMetrics();
134  const int h = fm.height();
135  const int tw = fm.width( mTitle, mLenVisible ) + fm.width(TQChar(' '));
136  int x;
137  if ( mAlign & AlignHCenter ) // center alignment
138  x = frameRect().width()/2 - tw/2;
139  else if ( mAlign & AlignRight ) // right alignment
140  x = frameRect().width() - tw;
141  else if ( mAlign & AlignLeft ) // left alignment
142  x = 0;
143  else { // auto align
144  if( TQApplication::reverseLayout() )
145  x = frameRect().width() - tw;
146  else
147  x = 0;
148  }
149  TQRect r( x, 0, tw, h );
150  int va = style().styleHint( TQStyle::SH_GroupBox_TextLabelVerticalAlignment, this );
151  if ( va & AlignTop )
152  r.moveBy( 0, fm.descent() );
153  const TQColor pen( (TQRgb) style().styleHint( TQStyle::SH_GroupBox_TextLabelColor, this ) );
154  if ( !style().styleHint( TQStyle::SH_UnderlineAccelerator, this ) )
155  va |= NoAccel;
156  style().drawItem( &paint, r, ShowPrefix | AlignHCenter | va, colorGroup(),
157  isEnabled(), 0, mTitle, -1, ownPalette() ? 0 : &pen );
158  paint.setClipRegion( e->region().subtract( r ) ); // clip everything but title
159  }
160  drawFrame( &paint );
161  drawContents( &paint );
162 }
163 
164 // static
165 int KDHorizontalLine::indentHint() {
166  return 30;
167 }
168 
169 #include "kdhorizontalline.moc"