• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

knotes

  • sources
  • kde-4.14
  • kdepim
  • knotes
  • notes
knotebutton.cpp
Go to the documentation of this file.
1 /*******************************************************************
2  KNotes -- Notes for the KDE project
3 
4  Copyright (c) 2002-2013, The KNotes Developers
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  as published by the Free Software Foundation; either version 2
9  of the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************/
20 
21 #include "knotebutton.h"
22 
23 #include <kiconloader.h>
24 #include <kicontheme.h>
25 
26 #include <QIcon>
27 #include <QPainter>
28 #include <QPixmap>
29 #include <QSizePolicy>
30 #include <QStyle>
31 #include <QStyleOption>
32 
33 
34 KNoteButton::KNoteButton( const QString &icon, QWidget *parent )
35  : QPushButton( parent )
36 {
37  setFocusPolicy( Qt::NoFocus );
38  setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
39 
40  setFlat( true );
41 
42  if ( !icon.isEmpty() ) {
43  setIcon( KIconLoader::global()->loadIcon( icon, KIconLoader::Small, 10 ) );
44  }
45 }
46 
47 KNoteButton::~KNoteButton()
48 {
49 }
50 
51 void KNoteButton::enterEvent( QEvent * )
52 {
53  setFlat( false );
54  repaint();
55 }
56 
57 void KNoteButton::leaveEvent( QEvent * )
58 {
59  setFlat( true );
60  repaint();
61 }
62 
63 int KNoteButton::heightForWidth( int w ) const
64 {
65  return w;
66 }
67 
68 QSize KNoteButton::sizeHint() const
69 {
70  return QSize( QPushButton::sizeHint().height(),
71  QPushButton::sizeHint().height() );
72 }
73 
74 void KNoteButton::paintEvent( QPaintEvent * )
75 {
76  QPainter p( this );
77 
78  // the button
79  QStyleOption opt;
80  opt.initFrom( this );
81 
82  if ( isEnabled() ) {
83  opt.state |= QStyle::State_Enabled;
84  }
85  if ( isDown() ) {
86  opt.state |= QStyle::State_DownArrow;
87  }
88  if ( isCheckable() && isChecked() ) {
89  opt.state |= QStyle::State_On;
90  }
91  if ( !isFlat() ) {
92  if ( !isDown() ) {
93  opt.state |= QStyle::State_Raised;
94  } else {
95  opt.state |= QStyle::State_MouseOver;
96  }
97  }
98 
99  if ( ( opt.state & QStyle::State_MouseOver ) && ( ! ( opt.state & QStyle::State_DownArrow ) ) ) {
100  style()->drawPrimitive( QStyle::PE_PanelButtonTool, &opt, &p, this );
101  }
102 
103  // the button label
104  if ( !icon().isNull() ) {
105  QIcon::Mode mode = QIcon::Disabled;
106  QIcon::State state = QIcon::Off;
107 
108  if ( opt.state & QStyle::State_Enabled ) {
109  mode = hasFocus() ? QIcon::Active : QIcon::Normal;
110  }
111  if ( opt.state & QStyle::State_On ) {
112  state = QIcon::On;
113  }
114 
115  const QPixmap pix = icon().pixmap( style()->pixelMetric(
116  QStyle::PM_SmallIconSize ), mode, state );
117 
118  int dx = ( width() - pix.width() ) / 2;
119  int dy = ( height() - pix.height() ) / 2;
120 
121  // Shift button contents if pushed.
122  if ( ( opt.state & QStyle::State_On ) || ( opt.state & QStyle::State_DownArrow ) ) {
123  dx += style()->pixelMetric( QStyle::PM_ButtonShiftHorizontal );
124  dy += style()->pixelMetric( QStyle::PM_ButtonShiftVertical );
125  }
126 
127  p.drawPixmap( dx, dy, pix );
128  }
129 }
130 
QAbstractButton::isDown
bool isDown() const
QEvent
QWidget
QPixmap::width
int width() const
QPushButton::sizeHint
virtual QSize sizeHint() const
KNoteButton::enterEvent
void enterEvent(QEvent *)
Definition: knotebutton.cpp:51
QWidget::setFocusPolicy
void setFocusPolicy(Qt::FocusPolicy policy)
QWidget::style
QStyle * style() const
QStyle::pixelMetric
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const =0
QSizePolicy
QWidget::hasFocus
bool hasFocus() const
QPushButton::setFlat
void setFlat(bool)
QAbstractButton::setIcon
void setIcon(const QIcon &icon)
QIcon::pixmap
QPixmap pixmap(const QSize &size, Mode mode, State state) const
QStyleOption::initFrom
void initFrom(const QWidget *widget)
QWidget::width
int width() const
QStyleOption
KNoteButton::heightForWidth
virtual int heightForWidth(int w) const
Definition: knotebutton.cpp:63
QWidget::isEnabled
bool isEnabled() const
knotebutton.h
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPainter
QString::isEmpty
bool isEmpty() const
QAbstractButton::isCheckable
bool isCheckable() const
QString
QWidget::setSizePolicy
void setSizePolicy(QSizePolicy)
QPixmap
KNoteButton::sizeHint
virtual QSize sizeHint() const
Definition: knotebutton.cpp:68
QSize
QPixmap::height
int height() const
QWidget::repaint
void repaint()
KNoteButton::~KNoteButton
~KNoteButton()
Definition: knotebutton.cpp:47
QAbstractButton::isChecked
bool isChecked() const
KNoteButton::paintEvent
void paintEvent(QPaintEvent *e)
Definition: knotebutton.cpp:74
KNoteButton::KNoteButton
KNoteButton(const QString &icon, QWidget *parent=0)
Definition: knotebutton.cpp:34
KNoteButton::leaveEvent
void leaveEvent(QEvent *)
Definition: knotebutton.cpp:57
QStyle::drawPrimitive
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const =0
QPushButton
QPaintEvent
QWidget::height
int height() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

knotes

Skip menu "knotes"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal