certmanager/lib

kdhorizontalline.cpp

00001 /* -*- Mode: C++ -*-
00002    KD Tools - a set of useful widgets for Qt
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 ** Qt library by Trolltech AS, Norway (or with modified versions of Qt
00031 ** that use the same license as Qt), 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 ** Qt.  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 <qstyle.h>
00044 #include <qpainter.h>
00045 #ifdef QT_ACCESSIBILITY_SUPPORT
00046 #include <qaccessible.h>
00047 #endif
00048 #include <qfontmetrics.h>
00049 #include <qapplication.h>
00050 
00051 KDHorizontalLine::KDHorizontalLine( QWidget * parent, const char * name, WFlags f )
00052   : QFrame( parent, name, f ),
00053     mAlign( Qt::AlignAuto ),
00054     mLenVisible( 0 )
00055 {
00056   QFrame::setFrameStyle( HLine | Sunken );
00057 }
00058 
00059 KDHorizontalLine::KDHorizontalLine( const QString & title, QWidget * parent, const char * name, WFlags f )
00060   : QFrame( parent, name, f ),
00061     mAlign( Qt::AlignAuto ),
00062     mLenVisible( 0 )
00063 {
00064   QFrame::setFrameStyle( HLine | Sunken );
00065   setTitle( title );
00066 }
00067 
00068 KDHorizontalLine::~KDHorizontalLine() {}
00069 
00070 void KDHorizontalLine::setFrameStyle( int style ) {
00071   QFrame::setFrameStyle( ( style & ~MShape ) | HLine ); // force HLine
00072 }
00073 
00074 void KDHorizontalLine::setTitle( const QString & title ) {
00075   if ( mTitle == title )
00076     return;
00077   mTitle = title;
00078   calculateFrame();
00079   update();
00080   updateGeometry();
00081 #ifdef QT_ACCESSIBILITY_SUPPORT
00082   QAccessible::updateAccessibility( this, 0, QAccessible::NameChanged );
00083 #endif
00084 }
00085 
00086 void KDHorizontalLine::calculateFrame() {
00087   mLenVisible = mTitle.length();
00088 #if 0
00089   if ( mLenVisible ) {
00090     const QFontMetrics fm = fontMetrics();
00091     while ( mLenVisible ) {
00092       const int tw = fm.width( mTitle, mLenVisible ) + 4*fm.width(QChar(' '));
00093       if ( tw < width() )
00094         break;
00095       mLenVisible--;
00096     }
00097     qDebug( "mLenVisible = %d (of %d)", mLenVisible, mTitle.length() );
00098     if ( mLenVisible ) { // but do we also have a visible label?
00099       QRect r = rect();
00100       const int va = style().styleHint( QStyle::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( QRect(0,0,0,0) );       //  then use client rect
00111 #endif
00112 }
00113 
00114 QSizePolicy KDHorizontalLine::sizePolicy() const {
00115   return QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
00116 }
00117 
00118 QSize KDHorizontalLine::sizeHint() const {
00119   return minimumSizeHint();
00120 }
00121 
00122 QSize KDHorizontalLine::minimumSizeHint() const {
00123   const int w = fontMetrics().width( mTitle, mLenVisible ) +
00124                 fontMetrics().width( QChar( ' ' ) );
00125   const int h = fontMetrics().height();
00126   return QSize( QMAX( w, indentHint() ), h ).expandedTo( qApp->globalStrut() );
00127 }
00128 
00129 void KDHorizontalLine::paintEvent( QPaintEvent * e ) {
00130   QPainter paint( this );
00131 
00132   if ( mLenVisible ) {  // draw title
00133     const QFontMetrics & fm = paint.fontMetrics();
00134     const int h = fm.height();
00135     const int tw = fm.width( mTitle, mLenVisible ) + fm.width(QChar(' '));
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( QApplication::reverseLayout() )
00145         x = frameRect().width() - tw;
00146       else
00147         x = 0;
00148     }
00149     QRect r( x, 0, tw, h );
00150     int va = style().styleHint( QStyle::SH_GroupBox_TextLabelVerticalAlignment, this );
00151     if ( va & AlignTop )
00152       r.moveBy( 0, fm.descent() );
00153     const QColor pen( (QRgb) style().styleHint( QStyle::SH_GroupBox_TextLabelColor, this ) );
00154 #if QT_VERSION >= 0x030300
00155     if ( !style().styleHint( QStyle::SH_UnderlineAccelerator, this ) )
00156       va |= NoAccel;
00157 #endif
00158     style().drawItem( &paint, r, ShowPrefix | AlignHCenter | va, colorGroup(),
00159                       isEnabled(), 0, mTitle, -1, ownPalette() ? 0 : &pen );
00160     paint.setClipRegion( e->region().subtract( r ) ); // clip everything but title
00161   }
00162   drawFrame( &paint );
00163   drawContents( &paint );
00164 }
00165 
00166 // static
00167 int KDHorizontalLine::indentHint() {
00168   return 30;
00169 }
00170 
00171 #include "kdhorizontalline.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys