• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Plasma

toolbutton.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2008 Marco Martin <notmart@gmail.com>
00003  *   Copyright 2008 Aaron Seigo <aseigo@kde.org>
00004  *
00005  *   This program is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU Library General Public License as
00007  *   published by the Free Software Foundation; either version 2, or
00008  *   (at your option) any later version.
00009  *
00010  *   This program is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details
00014  *
00015  *   You should have received a copy of the GNU Library General Public
00016  *   License along with this program; if not, write to the
00017  *   Free Software Foundation, Inc.,
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  */
00020 
00021 #include "toolbutton.h"
00022 
00023 #include <QAction>
00024 #include <QPainter>
00025 #include <QPaintEvent>
00026 #include <QStyle>
00027 #include <QStyleOptionToolButton>
00028 
00029 #include <plasma/paintutils.h>
00030 #include <plasma/theme.h>
00031 
00032 ToolButton::ToolButton(QWidget *parent)
00033     : QToolButton(parent),
00034       m_action(0)
00035 {
00036 }
00037 
00038 void ToolButton::setAction(QAction *action)
00039 {
00040     if (!action) {
00041         return;
00042     }
00043 
00044     if (m_action) {
00045         disconnect(m_action, SIGNAL(changed()), this, SLOT(syncToAction()));
00046         disconnect(this, SIGNAL(clicked()), m_action, SLOT(trigger()));
00047     }
00048 
00049     m_action = action;
00050     connect(m_action, SIGNAL(changed()), this, SLOT(syncToAction()));
00051     connect(this, SIGNAL(clicked()), m_action, SLOT(trigger()));
00052     connect(m_action, SIGNAL(destroyed(QObject*)), this, SLOT(actionDestroyed(QObject*)));
00053     syncToAction();
00054 }
00055 
00056 void ToolButton::syncToAction()
00057 {
00058     if (!m_action) {
00059         return;
00060     }
00061 
00062     setIcon(m_action->icon());
00063     setText(m_action->text());
00064 
00065     if (toolButtonStyle() == Qt::ToolButtonIconOnly) {
00066         setToolTip(m_action->text());
00067     }
00068 
00069     setCheckable(m_action->isCheckable());
00070     if (m_action->actionGroup()) {
00071         setAutoExclusive(m_action->actionGroup()->isExclusive());
00072     }
00073 
00074     setEnabled(m_action->isEnabled());
00075 }
00076 
00077 void ToolButton::actionDestroyed(QObject *)
00078 {
00079     m_action = 0;
00080 }
00081 
00082 void ToolButton::paintEvent(QPaintEvent *event)
00083 {
00084     QPainter painter(this);
00085     painter.setRenderHint(QPainter::Antialiasing, true);
00086 
00087     painter.translate(0.5, 0.5);
00088 
00089     QStyleOptionToolButton buttonOpt;
00090     initStyleOption(&buttonOpt);
00091 
00092     QColor backgroundColor;
00093     if ((buttonOpt.state & QStyle::State_MouseOver) || isChecked()) {
00094         backgroundColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
00095     } else {
00096         backgroundColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor);
00097     }
00098 
00099     backgroundColor.setAlphaF(0.4);
00100     QColor textColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
00101 
00102     buttonOpt.palette.setColor(QPalette::Foreground, textColor);
00103     buttonOpt.palette.setColor(QPalette::ButtonText, textColor);
00104 
00105     textColor.setAlphaF(0.4);
00106     painter.setPen(textColor);
00107     painter.setBrush(backgroundColor);
00108     painter.drawPath(Plasma::PaintUtils::roundedRectangle(event->rect().adjusted(1,1,-1,-1), 4));
00109 
00110     style()->drawControl(QStyle::CE_ToolButtonLabel, &buttonOpt, &painter, this);
00111 }
00112 
00113 #include "toolbutton.moc"
00114 

Plasma

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

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal