kdhorizontalline.cpp
00001 /* -*- Mode: C++ -*- 00002 KD Tools - a set of useful widgets for TQt 00003 */ 00004 00005 /**************************************************************************** 00006 ** Copyright (C) 2005 Klarälvdalens Datakonsult AB. All rights reserved. 00007 ** 00008 ** This file is part of the KD Tools library. 00009 ** 00010 ** This file may be distributed and/or modified under the terms of the 00011 ** GNU General Public License version 2 as published by the Free Software 00012 ** Foundation and appearing in the file LICENSE.GPL included in the 00013 ** packaging of this file. 00014 ** 00015 ** Licensees holding valid commercial KD Tools licenses may use this file in 00016 ** accordance with the KD Tools Commercial License Agreement provided with 00017 ** the Software. 00018 ** 00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00021 ** 00022 ** See http://www.klaralvdalens-datakonsult.se/?page=products for 00023 ** information about KD Tools Commercial License Agreements. 00024 ** 00025 ** Contact info@klaralvdalens-datakonsult.se if any conditions of this 00026 ** licensing are not clear to you. 00027 ** 00028 ** In addition, as a special exception, the copyright holders give 00029 ** permission to link the code of this program with any edition of the 00030 ** TQt library by Trolltech AS, Norway (or with modified versions of TQt 00031 ** that use the same license as TQt), and distribute linked 00032 ** combinations including the two. You must obey the GNU General 00033 ** Public License in all respects for all of the code used other than 00034 ** TQt. If you modify this file, you may extend this exception to your 00035 ** version of the file, but you are not obligated to do so. If you do 00036 ** not wish to do so, delete this exception statement from your 00037 ** version. 00038 ** 00039 **********************************************************************/ 00040 00041 #include "kdhorizontalline.h" 00042 00043 #include <tqstyle.h> 00044 #include <tqpainter.h> 00045 #ifdef TQT_ACCESSIBILITY_SUPPORT 00046 #include <tqaccessible.h> 00047 #endif 00048 #include <tqfontmetrics.h> 00049 #include <tqapplication.h> 00050 00051 KDHorizontalLine::KDHorizontalLine( TQWidget * parent, const char * name, WFlags f ) 00052 : TQFrame( parent, name, f ), 00053 mAlign( TQt::AlignAuto ), 00054 mLenVisible( 0 ) 00055 { 00056 TQFrame::setFrameStyle( HLine | Sunken ); 00057 } 00058 00059 KDHorizontalLine::KDHorizontalLine( const TQString & title, TQWidget * parent, const char * name, WFlags f ) 00060 : TQFrame( parent, name, f ), 00061 mAlign( TQt::AlignAuto ), 00062 mLenVisible( 0 ) 00063 { 00064 TQFrame::setFrameStyle( HLine | Sunken ); 00065 setTitle( title ); 00066 } 00067 00068 KDHorizontalLine::~KDHorizontalLine() {} 00069 00070 void KDHorizontalLine::setFrameStyle( int style ) { 00071 TQFrame::setFrameStyle( ( style & ~MShape ) | HLine ); // force HLine 00072 } 00073 00074 void KDHorizontalLine::setTitle( const TQString & title ) { 00075 if ( mTitle == title ) 00076 return; 00077 mTitle = title; 00078 calculateFrame(); 00079 update(); 00080 updateGeometry(); 00081 #ifdef TQT_ACCESSIBILITY_SUPPORT 00082 TQAccessible::updateAccessibility( this, 0, TQAccessible::NameChanged ); 00083 #endif 00084 } 00085 00086 void KDHorizontalLine::calculateFrame() { 00087 mLenVisible = mTitle.length(); 00088 #if 0 00089 if ( mLenVisible ) { 00090 const TQFontMetrics fm = fontMetrics(); 00091 while ( mLenVisible ) { 00092 const int tw = fm.width( mTitle, mLenVisible ) + 4*fm.width(TQChar(' ')); 00093 if ( tw < width() ) 00094 break; 00095 mLenVisible--; 00096 } 00097 tqDebug( "mLenVisible = %d (of %d)", mLenVisible, mTitle.length() ); 00098 if ( mLenVisible ) { // but do we also have a visible label? 00099 TQRect r = rect(); 00100 const int va = style().styleHint( TQStyle::SH_GroupBox_TextLabelVerticalAlignment, this ); 00101 if( va & AlignVCenter ) 00102 r.setTop( fm.height() / 2 ); // frame rect should be 00103 else if( va & AlignTop ) 00104 r.setTop( fm.ascent() ); 00105 setFrameRect( r ); // smaller than client rect 00106 return; 00107 } 00108 } 00109 // no visible label 00110 setFrameRect( TQRect(0,0,0,0) ); // then use client rect 00111 #endif 00112 } 00113 00114 TQSizePolicy KDHorizontalLine::sizePolicy() const { 00115 return TQSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Fixed ); 00116 } 00117 00118 TQSize KDHorizontalLine::sizeHint() const { 00119 return minimumSizeHint(); 00120 } 00121 00122 TQSize KDHorizontalLine::minimumSizeHint() const { 00123 const int w = fontMetrics().width( mTitle, mLenVisible ) + 00124 fontMetrics().width( TQChar( ' ' ) ); 00125 const int h = fontMetrics().height(); 00126 return TQSize( TQMAX( w, indentHint() ), h ).expandedTo( tqApp->globalStrut() ); 00127 } 00128 00129 void KDHorizontalLine::paintEvent( TQPaintEvent * e ) { 00130 TQPainter paint( this ); 00131 00132 if ( mLenVisible ) { // draw title 00133 const TQFontMetrics & fm = paint.fontMetrics(); 00134 const int h = fm.height(); 00135 const int tw = fm.width( mTitle, mLenVisible ) + fm.width(TQChar(' ')); 00136 int x; 00137 if ( mAlign & AlignHCenter ) // center alignment 00138 x = frameRect().width()/2 - tw/2; 00139 else if ( mAlign & AlignRight ) // right alignment 00140 x = frameRect().width() - tw; 00141 else if ( mAlign & AlignLeft ) // left alignment 00142 x = 0; 00143 else { // auto align 00144 if( TQApplication::reverseLayout() ) 00145 x = frameRect().width() - tw; 00146 else 00147 x = 0; 00148 } 00149 TQRect r( x, 0, tw, h ); 00150 int va = style().styleHint( TQStyle::SH_GroupBox_TextLabelVerticalAlignment, this ); 00151 if ( va & AlignTop ) 00152 r.moveBy( 0, fm.descent() ); 00153 const TQColor pen( (TQRgb) style().styleHint( TQStyle::SH_GroupBox_TextLabelColor, this ) ); 00154 if ( !style().styleHint( TQStyle::SH_UnderlineAccelerator, this ) ) 00155 va |= NoAccel; 00156 style().drawItem( &paint, r, ShowPrefix | AlignHCenter | va, colorGroup(), 00157 isEnabled(), 0, mTitle, -1, ownPalette() ? 0 : &pen ); 00158 paint.setClipRegion( e->region().subtract( r ) ); // clip everything but title 00159 } 00160 drawFrame( &paint ); 00161 drawContents( &paint ); 00162 } 00163 00164 // static 00165 int KDHorizontalLine::indentHint() { 00166 return 30; 00167 } 00168 00169 #include "kdhorizontalline.moc"