[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klfpixmapbutton.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfpixmapbutton.cpp
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist at bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klfpixmapbutton.cpp 996 2017-01-15 09:38:25Z phfaist $ */
23 
24 #include <QApplication>
25 #include <QPushButton>
26 #include <QStyleOption>
27 #include <QPainter>
28 #include <QPixmap>
29 #include <QStyle>
30 #include <QPaintEvent>
31 
32 #include <klfdefs.h>
33 #include "klfpixmapbutton.h"
34 
35 
36 
37 KLFPixmapButton::KLFPixmapButton(const QPixmap& pix, QWidget *parent)
38  : QPushButton(parent), _pix(pix), _pixmargin(2), _xalignfactor(0.5f), _yalignfactor(0.5f), _pixscale(1.f)
39 {
40  setText(QString());
41  setIcon(QIcon());
42  if (parent != NULL) {
43  _pixscale = parent->devicePixelRatio();
44  }
45 }
46 
48 {
49  return sizeHint();
50 }
51 
53 {
54  // inspired by QPushButton::sizeHint() in qpushbutton.cpp
55 
56  ensurePolished();
57 
58  int w = 0, h = 0;
59  QStyleOptionButton opt;
60  initStyleOption(&opt);
61 
62  // calculate contents size...
63  w = _pix.width() / _pixscale + _pixmargin;
64  h = _pix.height() / _pixscale + _pixmargin;
65 
66  if (menu()) {
67  w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
68  }
69 
70  return (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this).
71  expandedTo(QApplication::globalStrut()).expandedTo(QSize(50, 30)));
72  // (50,30) is minimum non-square buttons on Qt/Mac
73 }
74 
76 {
77  QPushButton::paintEvent(event);
78  QPainter p(this);
79  p.setClipRect(event->rect());
80  QSizeF pixsz = _pix.size(); pixsz /= _pixscale;
81  p.drawPixmap(QRectF(QPointF( _xalignfactor*(width()-(2*_pixmargin+pixsz.width())) + _pixmargin,
82  _yalignfactor*(height()-(2*_pixmargin+pixsz.height())) + _pixmargin ),
83  pixsz),
84  _pix,
85  QRectF(QPointF(0,0), _pix.size()));
86 }
87 
88 
89 
QSize size() const
int width() const
const char * style
Definition: klfdatautil.cpp:56
Base declarations for klatexformula and some utilities.
virtual QSize minimumSizeHint() const
KLFPixmapButton(const QPixmap &pix, QWidget *parent=0)
const QRect & rect() const
virtual QSize sizeHint() const
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
int height() const
void setClipRect(const QRectF &rectangle, Qt::ClipOperation operation)
virtual void paintEvent(QPaintEvent *event)
qreal height() const
qreal width() const

Generated by doxygen 1.8.13