Plasma
toolbutton.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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