kmdi Library API Documentation

kmdichildfrmcaption.cpp

00001 //----------------------------------------------------------------------------
00002 //    filename             : kmdichildfrmcaption.cpp
00003 //----------------------------------------------------------------------------
00004 //    Project              : KDE MDI extension
00005 //
00006 //    begin                : 07/1999       by Szymon Stefanek as part of kvirc
00007 //                                         (an IRC application)
00008 //    changes              : 09/1999       by Falk Brettschneider to create an
00009 //                           - 06/2000     stand-alone Qt extension set of
00010 //                                         classes and a Qt-based library
00011 //                           2000-2003     maintained by the KDevelop project
00012 //
00013 //    copyright            : (C) 1999-2003 by Szymon Stefanek (stefanek@tin.it)
00014 //                                         and
00015 //                                         Falk Brettschneider
00016 //    email                :  falkbr@kdevelop.org (Falk Brettschneider)
00017 //----------------------------------------------------------------------------
00018 //
00019 //----------------------------------------------------------------------------
00020 //
00021 //    This program is free software; you can redistribute it and/or modify
00022 //    it under the terms of the GNU Library General Public License as
00023 //    published by the Free Software Foundation; either version 2 of the
00024 //    License, or (at your option) any later version.
00025 //
00026 //----------------------------------------------------------------------------
00027 
00028 #include "kmdichildfrmcaption.h"
00029 #include "kmdichildfrmcaption.moc"
00030 
00031 #include <qpainter.h>
00032 #include <qapplication.h>
00033 #include <qcursor.h>
00034 #include <qtoolbutton.h>
00035 #include <qpopupmenu.h>
00036 
00037 #include "kmdidefines.h"
00038 #include "kmdichildfrm.h"
00039 #include "kmdichildarea.h"
00040 #include "kmdimainfrm.h"
00041 #include <klocale.h>
00042 #include <iostream>
00044 // Class   : KMdiChildFrmCaption
00045 // Purpose : An MDI label that draws the title
00046 //
00047 //
00049 
00050 //============== KMdiChildFrmCaption =============//
00051 
00052 KMdiChildFrmCaption::KMdiChildFrmCaption(KMdiChildFrm *parent)
00053 :QWidget(parent, "kmdi_childfrmcaption")
00054 {
00055    m_szCaption    = i18n("Unnamed");
00056    m_bActive      = false;
00057    m_pParent      = parent;
00058    setBackgroundMode(NoBackground);
00059    setFocusPolicy(NoFocus);
00060    m_bChildInDrag = false;
00061 }
00062 
00063 //============== ~KMdiChildFrmCaption =============//
00064 
00065 KMdiChildFrmCaption::~KMdiChildFrmCaption()
00066 {
00067 }
00068 
00069 //============= mousePressEvent ==============//
00070 
00071 void KMdiChildFrmCaption::mousePressEvent(QMouseEvent *e)
00072 {
00073    if ( e->button() == LeftButton) {
00074       setMouseTracking(false);
00075       if (KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look) {
00076          QApplication::setOverrideCursor(Qt::sizeAllCursor,true);
00077       }
00078       m_pParent->m_bDragging = true;
00079       m_offset = mapToParent( e->pos());
00080    }
00081    else if ( e->button() == RightButton) {
00082       m_pParent->systemMenu()->popup( mapToGlobal( e->pos()));
00083    }
00084 }
00085 
00086 //============= mouseReleaseEvent ============//
00087 
00088 void KMdiChildFrmCaption::mouseReleaseEvent(QMouseEvent *e)
00089 {
00090    if ( e->button() == LeftButton) {
00091       if (KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look)
00092          QApplication::restoreOverrideCursor();
00093       releaseMouse();
00094       if(m_pParent->m_bDragging) {
00095          m_pParent->m_bDragging = false;
00096          if (m_bChildInDrag) {
00097             //notify child view
00098             KMdiChildFrmDragEndEvent ue(e);
00099             if( m_pParent->m_pClient != 0L) {
00100                QApplication::sendEvent( m_pParent->m_pClient, &ue);
00101             }
00102             m_bChildInDrag = false;
00103          }
00104       }
00105    }
00106 }
00107 
00108 //============== mouseMoveEvent =============//
00109 void KMdiChildFrmCaption::mouseMoveEvent(QMouseEvent *e)
00110 {
00111    if ( !m_pParent->m_bDragging ) {
00112       return;
00113    }
00114 
00115    if (!m_bChildInDrag) {
00116       //notify child view
00117       KMdiChildFrmDragBeginEvent ue(e);
00118       if( m_pParent->m_pClient != 0L) {
00119          QApplication::sendEvent( m_pParent->m_pClient, &ue);
00120       }
00121       m_bChildInDrag = true;
00122    }
00123 
00124    QPoint relMousePosInChildArea = m_pParent->m_pManager->mapFromGlobal( e->globalPos() );
00125 
00126    // mouse out of child area? stop child frame dragging
00127    if ( !m_pParent->m_pManager->rect().contains( relMousePosInChildArea)) {
00128       if ( relMousePosInChildArea.x() < 0)
00129          relMousePosInChildArea.rx() = 0;
00130       if ( relMousePosInChildArea.y() < 0)
00131          relMousePosInChildArea.ry() = 0;
00132       if ( relMousePosInChildArea.x() > m_pParent->m_pManager->width())
00133          relMousePosInChildArea.rx() = m_pParent->m_pManager->width();
00134       if ( relMousePosInChildArea.y() > m_pParent->m_pManager->height())
00135          relMousePosInChildArea.ry() = m_pParent->m_pManager->height();
00136    }
00137    QPoint mousePosInChildArea = relMousePosInChildArea - m_offset;
00138 
00139    // set new child frame position
00140    parentWidget()->move( mousePosInChildArea);
00141 }
00142 
00143 //=============== setActive ===============//
00144 
00145 void KMdiChildFrmCaption::setActive(bool bActive)
00146 {
00147    if( m_bActive == bActive)
00148       return;
00149 
00150    //    Ensure the icon's pixmap has the correct bg color
00151    m_pParent->m_pWinIcon->setBackgroundColor(bActive
00152    ? m_pParent->m_pManager->m_captionActiveBackColor
00153    : m_pParent->m_pManager->m_captionInactiveBackColor);
00154    m_pParent->m_pUnixIcon->setBackgroundColor(bActive
00155    ? m_pParent->m_pManager->m_captionActiveBackColor
00156    : m_pParent->m_pManager->m_captionInactiveBackColor);
00157 
00158    m_bActive = bActive;
00159    repaint( false);
00160 }
00161 
00162 //=============== setCaption ===============//
00163 
00164 void KMdiChildFrmCaption::setCaption(const QString& text)
00165 {
00166    m_szCaption = text;
00167    repaint(false);
00168 }
00169 
00170 //============== heightHint ===============//
00171 
00172 int KMdiChildFrmCaption::heightHint()
00173 {
00174    int hght=m_pParent->m_pManager->m_captionFontLineSpacing+3;
00175    if (KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::Win95Look) {
00176       if(hght<18)hght=18;
00177    }
00178    else if (KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDE1Look) {
00179       if(hght<20)hght=20;
00180    }
00181    else if (KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDELook) {
00182       if(hght<16)hght=16;
00183    }
00184    else {   // kde2laptop look
00185       hght -= 4;
00186       if(hght<14)hght=14;
00187    }
00188    return hght;
00189 }
00190 
00191 //=============== paintEvent ==============//
00192 
00193 void KMdiChildFrmCaption::paintEvent(QPaintEvent *)
00194 {
00195    QPainter p(this);
00196    QRect r=rect();
00197    p.setFont(m_pParent->m_pManager->m_captionFont);
00198    if(m_bActive){
00199       p.fillRect(r,m_pParent->m_pManager->m_captionActiveBackColor);
00200       p.setPen(m_pParent->m_pManager->m_captionActiveForeColor);
00201    } else {
00202       p.fillRect(r,m_pParent->m_pManager->m_captionInactiveBackColor);
00203       p.setPen(m_pParent->m_pManager->m_captionInactiveForeColor);
00204    }
00205    //Shift the text after the icon
00206    if (KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::Win95Look)
00207       r.setLeft(r.left()+m_pParent->icon()->width()+3);
00208    else if (KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDE1Look)
00209       r.setLeft(r.left()+22);
00210    else if (KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDELook)
00211       r.setLeft(r.left()+m_pParent->icon()->width()+3);
00212    else  // kde2laptop look
00213       r.setLeft(r.left()+30);
00214 
00215    int captionWidthForText = width() - 4*m_pParent->m_pClose->width() - m_pParent->icon()->width() - 5;
00216    QString text = abbreviateText( m_szCaption, captionWidthForText);
00217    p.drawText( r, AlignVCenter|AlignLeft|SingleLine, text);
00218 
00219 }
00220 
00221 //=============== abbreviateText ===============//
00222 
00223 QString KMdiChildFrmCaption::abbreviateText(QString origStr, int maxWidth)
00224 {
00225    QFontMetrics fm = fontMetrics();
00226    int actualWidth = fm.width( origStr);
00227 
00228    int realLetterCount = origStr.length();
00229    int newLetterCount;
00230    if (actualWidth != 0) {
00231       newLetterCount = (maxWidth * realLetterCount) / actualWidth;
00232    }
00233    else {
00234       newLetterCount = realLetterCount; // should be 0 anyway
00235    }
00236    int w = maxWidth+1;
00237    QString s = origStr;
00238    if (newLetterCount <= 0) {
00239       s = "";
00240    }
00241    while((w > maxWidth) && (newLetterCount >= 1)) {
00242       if( newLetterCount < realLetterCount) {
00243          if(newLetterCount > 3)
00244             s = origStr.left( newLetterCount/2) + "..." + origStr.right( newLetterCount/2);
00245          else {
00246             if(newLetterCount > 1)
00247                s = origStr.left( newLetterCount) + "..";
00248             else
00249                s = origStr.left(1);
00250          }
00251       }
00252       QFontMetrics fm = fontMetrics();
00253       w = fm.width(s);
00254       newLetterCount--;
00255    }
00256    return s;
00257 }
00258 
00259 //============= mouseDoubleClickEvent ===========//
00260 
00261 void KMdiChildFrmCaption::mouseDoubleClickEvent(QMouseEvent *)
00262 {
00263    m_pParent->maximizePressed();
00264 }
00265 
00266 //============= slot_moveViaSystemMenu ===========//
00267 
00268 void KMdiChildFrmCaption::slot_moveViaSystemMenu()
00269 {
00270    setMouseTracking(true);
00271    grabMouse();
00272    if (KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look)
00273       QApplication::setOverrideCursor(Qt::sizeAllCursor,true);
00274    m_pParent->m_bDragging = true;
00275    m_offset = mapFromGlobal( QCursor::pos());
00276 }
00277 
00278 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE Logo
This file is part of the documentation for kmdi Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Jul 22 10:18:15 2005 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003