[KLF Backend][KLF Tools][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) 2011 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$ */
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), _pixscale(1.f)
00039 {
00040   setText(QString());
00041   setIcon(QIcon());
00042   if (parent != NULL) {
00043     _pixscale = parent->devicePixelRatio();
00044   }
00045 }
00046 
00047 QSize KLFPixmapButton::minimumSizeHint() const
00048 {
00049   return sizeHint();
00050 }
00051 
00052 QSize KLFPixmapButton::sizeHint() const
00053 {
00054   // inspired by QPushButton::sizeHint() in qpushbutton.cpp
00055 
00056   ensurePolished();
00057 
00058   int w = 0, h = 0;
00059   QStyleOptionButton opt;
00060   initStyleOption(&opt);
00061 
00062   // calculate contents size...
00063   w = _pix.width() / _pixscale + _pixmargin;
00064   h = _pix.height() / _pixscale + _pixmargin;
00065 
00066   if (menu()) {
00067     w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
00068   }
00069 
00070   return (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this).
00071           expandedTo(QApplication::globalStrut()).expandedTo(QSize(50, 30)));
00072   // (50,30) is minimum non-square buttons on Qt/Mac
00073 }
00074 
00075 void KLFPixmapButton::paintEvent(QPaintEvent *event)
00076 {
00077   QPushButton::paintEvent(event);
00078   QPainter p(this);
00079   p.setClipRect(event->rect());
00080   QSizeF pixsz = _pix.size(); pixsz /= _pixscale;
00081   p.drawPixmap(QRectF(QPointF( _xalignfactor*(width()-(2*_pixmargin+pixsz.width())) + _pixmargin,
00082                                _yalignfactor*(height()-(2*_pixmargin+pixsz.height())) + _pixmargin ),
00083                       pixsz),
00084                _pix,
00085                QRectF(QPointF(0,0), _pix.size()));
00086 }
00087 
00088 
00089 

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net