kexi

kexiarrowtip.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexiarrowtip.h"
00021 
00022 #include <qpixmap.h>
00023 #include <qbitmap.h>
00024 #include <qpainter.h>
00025 #include <qimage.h>
00026 #include <qtooltip.h>
00027 #include <qfont.h>
00028 #include <qfontmetrics.h>
00029 #include <qtimer.h>
00030 
00031 
00032 KexiArrowTip::KexiArrowTip(const QString& text, QWidget* parent)
00033  : KexiToolTip(text, parent)
00034  , m_opacity(0.0)
00035 {
00036     QPalette pal( palette() );
00037     QColorGroup cg(pal.active());
00038     cg.setColor(QColorGroup::Foreground, Qt::red);
00039     pal.setActive(cg);
00040     setPalette(pal);
00041 
00042     QFontMetrics fm(font());
00043     QSize sz(fm.boundingRect(m_value.toString()).size());
00044     sz += QSize(14, 10); //+margins
00045     m_arrowHeight = sz.height()/2;
00046     sz += QSize(0, m_arrowHeight); //+arrow height
00047     resize(sz);
00048 
00049     setAutoMask( false );
00050 
00051     //generate mask
00052     QPixmap maskPm(size());
00053     maskPm.fill( black );
00054     QPainter maskPainter(&maskPm);
00055     drawFrame(maskPainter);
00056     QImage maskImg( maskPm.convertToImage() );
00057     QBitmap bm;
00058     bm = maskImg.createHeuristicMask();
00059     setMask( bm );
00060 }
00061 
00062 KexiArrowTip::~KexiArrowTip()
00063 {
00064 }
00065 
00066 void KexiArrowTip::show()
00067 {
00068     if (isVisible())
00069         return;
00070 
00071     m_opacity = 0.0;
00072     setWindowOpacity(0.0);
00073     KexiToolTip::show();
00074     increaseOpacity();
00075 }
00076 
00077 void KexiArrowTip::hide()
00078 {
00079     if (!isVisible())
00080         return;
00081 
00082     decreaseOpacity();
00083 }
00084 
00085 void KexiArrowTip::increaseOpacity()
00086 {
00087     m_opacity += 0.10;
00088     setWindowOpacity(m_opacity);
00089     if (m_opacity < 1.0)
00090         QTimer::singleShot(25, this, SLOT(increaseOpacity()));
00091 }
00092 
00093 void KexiArrowTip::decreaseOpacity()
00094 {
00095     if (m_opacity<=0.0) {
00096         KexiToolTip::close();
00097         m_opacity = 0.0;
00098         return;
00099     }
00100     m_opacity -= 0.10;
00101     setWindowOpacity(m_opacity);
00102     QTimer::singleShot(25, this, SLOT(decreaseOpacity()));
00103 }
00104 
00105 bool KexiArrowTip::close ( bool alsoDelete )
00106 {
00107     if (!isVisible()) {
00108         return KexiToolTip::close(alsoDelete);
00109     }
00110     if (m_opacity>0.0)
00111         decreaseOpacity();
00112     else
00113         return KexiToolTip::close(alsoDelete);
00114     return m_opacity<=0.0;
00115 }
00116 
00117 void KexiArrowTip::drawContents(QPainter& p)
00118 {
00119     p.setPen( QPen(palette().active().foreground(), 1) );
00120     p.drawText(QRect(0,m_arrowHeight,width(),height()-m_arrowHeight), 
00121         Qt::AlignCenter, m_value.toString());
00122 }
00123 
00124 void KexiArrowTip::drawFrame(QPainter& p)
00125 {
00126     QPen pen(palette().active().foreground(), 1, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
00127     p.setPen( pen );
00128     /*
00129        /\
00130      +-  -----+
00131      |  text  |
00132      +--------+
00133     */
00134     //1st line
00135     const int arrowOffset = 5; //5 pixels to right
00136     QPointArray pa(8);
00137     pa.setPoint(0, 0, m_arrowHeight-1);
00138     pa.setPoint(1, 0, height()-1);
00139     pa.setPoint(2, width()-1, height()-1);
00140     pa.setPoint(3, width()-1, m_arrowHeight-1);
00141     pa.setPoint(4, arrowOffset+m_arrowHeight+m_arrowHeight-2, m_arrowHeight-1);
00142     pa.setPoint(5, arrowOffset+m_arrowHeight-1, 0);
00143     pa.setPoint(6, arrowOffset, m_arrowHeight-1);
00144     pa.setPoint(7, 0, m_arrowHeight-1);
00145     p.drawPolyline(pa);
00146     //-2nd, internal line
00147     pa.resize(12);
00148     pa.setPoint(0, 1, m_arrowHeight);
00149     pa.setPoint(1, 1, height()-2);
00150     pa.setPoint(2, width()-2, height()-2);
00151     pa.setPoint(3, width()-2, m_arrowHeight);
00152     pa.setPoint(4, arrowOffset+m_arrowHeight+m_arrowHeight-2, m_arrowHeight);
00153     pa.setPoint(5, arrowOffset+m_arrowHeight-1, 1);
00154     pa.setPoint(6, arrowOffset, m_arrowHeight);
00155     pa.setPoint(7, 0, m_arrowHeight);
00156     pa.setPoint(8, arrowOffset+1, m_arrowHeight);
00157     pa.setPoint(9, arrowOffset+m_arrowHeight-1, 2);
00158     pa.setPoint(10, arrowOffset+m_arrowHeight+m_arrowHeight-3, m_arrowHeight);
00159     pa.setPoint(11, width()-2, m_arrowHeight);
00160     p.drawPolyline(pa);
00161 }
00162 
00163 #include "kexiarrowtip.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys