[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project

src/klftools/klfpixmapbutton.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfpixmapbutton.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2010 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id: klfpixmapbutton.cpp 442 2010-08-13 17:34:38Z philippe $ */
00023 
00024 #include <QApplication>
00025 #include <QPushButton>
00026 #include <QStyleOption>
00027 #include <QPainter>
00028 #include <QPixmap>
00029 #include <QStyle>
00030 #include <QPaintEvent>
00031 
00032 #include <klfdefs.h>
00033 #include "klfpixmapbutton.h"
00034 
00035 
00036 
00037 KLFPixmapButton::KLFPixmapButton(const QPixmap& pix, QWidget *parent)
00038   : QPushButton(parent), _pix(pix), _pixmargin(2), _xalignfactor(0.5f), _yalignfactor(0.5f)
00039 {
00040   setText(QString());
00041   setIcon(QIcon());
00042 }
00043 
00044 QSize KLFPixmapButton::minimumSizeHint() const
00045 {
00046   return sizeHint();
00047 }
00048 
00049 QSize KLFPixmapButton::sizeHint() const
00050 {
00051   // inspired by QPushButton::sizeHint() in qpushbutton.cpp
00052 
00053   ensurePolished();
00054 
00055   int w = 0, h = 0;
00056   QStyleOptionButton opt;
00057   initStyleOption(&opt);
00058 
00059   // calculate contents size...
00060   w = _pix.width() + _pixmargin;
00061   h = _pix.height() + _pixmargin;
00062 
00063   if (menu())
00064     w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
00065 
00066   return (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this).
00067           expandedTo(QApplication::globalStrut()).expandedTo(QSize(50, 30)));
00068   // (50,30) is minimum non-square buttons on Qt/Mac
00069 }
00070 
00071 void KLFPixmapButton::paintEvent(QPaintEvent *event)
00072 {
00073   QPushButton::paintEvent(event);
00074   QPainter p(this);
00075   p.setClipRect(event->rect());
00076   p.drawPixmap(QPointF( _xalignfactor*(width()-(2*_pixmargin+_pix.width())) + _pixmargin,
00077                         _yalignfactor*(height()-(2*_pixmargin+_pix.height())) + _pixmargin ),
00078                _pix);
00079 }
00080 
00081 
00082 

Generated by doxygen 1.7.3