[KLF Backend]
[KLF Tools]
[KLF Home]
KLatexFormula Project
src
klftools
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$ */
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
47
QSize
KLFPixmapButton::minimumSizeHint
()
const
48
{
49
return
sizeHint
();
50
}
51
52
QSize
KLFPixmapButton::sizeHint
()
const
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
75
void
KLFPixmapButton::paintEvent
(
QPaintEvent
*event)
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
KLFPixmapButton::KLFPixmapButton
KLFPixmapButton(const QPixmap &pix, QWidget *parent=0)
Definition
klfpixmapbutton.cpp:37
KLFPixmapButton::minimumSizeHint
virtual QSize minimumSizeHint() const
Definition
klfpixmapbutton.cpp:47
KLFPixmapButton::sizeHint
virtual QSize sizeHint() const
Definition
klfpixmapbutton.cpp:52
KLFPixmapButton::paintEvent
virtual void paintEvent(QPaintEvent *event)
Definition
klfpixmapbutton.cpp:75
style
const char * style
Definition
klfdatautil.cpp:56
klfdefs.h
Base declarations for klatexformula and some utilities.
klfpixmapbutton.h
QIcon
QPainter
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPainter::setClipRect
void setClipRect(const QRectF &rectangle, Qt::ClipOperation operation)
QPaintEvent
QPaintEvent::rect
const QRect & rect() const
QPixmap
QPointF
QRectF
QSize
QSizeF
QSizeF::height
qreal height() const
QSizeF::width
qreal width() const
QString
Generated by
doxygen
1.14.0