File indexing completed on 2024-04-28 17:04:38
0001 /***************************************************************************** 0002 * Copyright 2010 Craig Drummond <craig.p.drummond@gmail.com> * 0003 * Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com> * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU Lesser General Public License as * 0007 * published by the Free Software Foundation; either version 2.1 of the * 0008 * License, or (at your option) version 3, or any later version accepted * 0009 * by the membership of KDE e.V. (or its successor approved by the * 0010 * membership of KDE e.V.), which shall act as a proxy defined in * 0011 * Section 6 of version 3 of the license. * 0012 * * 0013 * This program is distributed in the hope that it will be useful, * 0014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 0016 * Lesser General Public License for more details. * 0017 * * 0018 * You should have received a copy of the GNU Lesser General Public * 0019 * License along with this library. If not, * 0020 * see <http://www.gnu.org/licenses/>. * 0021 *****************************************************************************/ 0022 0023 #include "qtcurvetogglebutton.h" 0024 #include <QAbstractButton> 0025 #include <QStyle> 0026 #include <QStyleOption> 0027 #include <QBitmap> 0028 #include <QPainter> 0029 #include <QPixmap> 0030 #include <QTimer> 0031 #include <QX11Info> 0032 #include <klocalizedstring.h> 0033 #include "qtcurveclient.h" 0034 #include <common/common.h> 0035 0036 namespace QtCurve { 0037 namespace KWin { 0038 0039 static int 0040 point2Pixel(double point) 0041 { 0042 return (int)(((point * QX11Info::appDpiY()) / 72.0) + 0.5); 0043 } 0044 0045 QtCurveToggleButton::QtCurveToggleButton(bool menubar, QtCurveClient *parent) 0046 : KCommonDecorationButton(AboveButton, parent), 0047 m_client(parent), 0048 isMenuBar(menubar), 0049 m_hover(false) 0050 { 0051 // setAttribute(Qt::WA_PaintOnScreen, false); 0052 setAttribute(Qt::WA_NoSystemBackground, true); 0053 setAutoFillBackground(false); 0054 // setFocusPolicy(Qt::NoFocus); 0055 // setAttribute(Qt::WA_OpaquePaintEvent, false); 0056 // setAttribute(Qt::WA_Hover, true); 0057 setCursor(Qt::ArrowCursor); 0058 setToolTip(menubar ? i18n("Toggle Menubar") : i18n("Toggle Statusbar")); 0059 reset(DecorationReset); 0060 } 0061 0062 void QtCurveToggleButton::reset(unsigned long changed) 0063 { 0064 if (changed&DecorationReset || changed&ManualReset || changed&SizeChange || changed&StateChange) 0065 this->update(); 0066 } 0067 0068 void QtCurveToggleButton::enterEvent(QEvent *e) 0069 { 0070 m_hover = true; 0071 KCommonDecorationButton::enterEvent(e); 0072 update(); 0073 // Hacky NVIDIA fix - sometimes mouseover state gets 'stuck' - but only for some windows!!! 0074 QTimer::singleShot(50, this, SLOT(update())); 0075 } 0076 0077 void QtCurveToggleButton::leaveEvent(QEvent *e) 0078 { 0079 m_hover = false; 0080 KCommonDecorationButton::leaveEvent(e); 0081 update(); 0082 // Hacky NVIDIA fix - sometimes mouseover state gets 'stuck' - but only for some windows!!! 0083 QTimer::singleShot(50, this, SLOT(update())); 0084 } 0085 0086 void QtCurveToggleButton::paintEvent(QPaintEvent *ev) 0087 { 0088 QPainter p(this); 0089 p.setClipRect(rect().intersected(ev->rect())); 0090 drawButton(&p); 0091 } 0092 0093 void QtCurveToggleButton::drawButton(QPainter *painter) 0094 { 0095 QRect r(0, 0, width(), height()); 0096 bool active(m_client->isActive()), 0097 sunken(isDown()); 0098 QColor col(KDecoration::options()->color(KDecoration::ColorFont, active/* || faded*/)); 0099 0100 col.setAlphaF(m_hover ? 0.99 : 0.15); 0101 painter->setRenderHint(QPainter::Antialiasing, true); 0102 //painter->setPen(QPen(col, (isChecked() ? 2.0 : 1.0))); 0103 painter->setPen(col); 0104 r.adjust(1, 1, -1, -1); 0105 0106 QFont font(Handler()->titleFont()); 0107 int maxPixelSize=r.height()-2, 0108 fontPixelSize=font.pixelSize(); 0109 bool drawBorder=true; 0110 0111 if(maxPixelSize<9) 0112 { 0113 maxPixelSize=r.height()+2; 0114 drawBorder=false; 0115 r.adjust(-1, -1, 1, 1); 0116 } 0117 0118 if(-1==fontPixelSize) 0119 fontPixelSize=point2Pixel(font.pointSizeF()); 0120 0121 if(fontPixelSize>maxPixelSize) 0122 font.setPixelSize(maxPixelSize-2); 0123 painter->setFont(font); 0124 0125 QRectF ellipse(r.x()+0.5, r.y()+0.5, r.width(), r.height()); 0126 QColor bgnd(KDecoration::options()->color(KDecoration::ColorTitleBar, active)); 0127 bool round=Handler()->wStyle()->pixelMetric((QStyle::PixelMetric)QtC_TitleBarButtons, 0L, 0L)&TITLEBAR_BUTTON_ROUND; 0128 double squareRad=round || Handler()->wStyle()->pixelMetric((QStyle::PixelMetric)QtC_Round, 0L, 0L)<ROUND_FULL ? 0.0 : 2.0; 0129 QPainterPath path; 0130 0131 bgnd.setAlphaF(m_hover ? 0.9 : 0.4); 0132 0133 0134 if(round) 0135 path.addEllipse(ellipse); 0136 else 0137 path.addRoundedRect(ellipse, squareRad+0.5, squareRad+0.5); 0138 0139 painter->fillPath(path, bgnd); 0140 if(sunken) 0141 { 0142 bgnd=col; 0143 bgnd.setAlphaF(0.2); 0144 painter->fillPath(path, bgnd); 0145 } 0146 if(drawBorder) 0147 { 0148 if(round) 0149 painter->drawEllipse(ellipse); 0150 else 0151 painter->drawRoundedRect(ellipse, squareRad, squareRad); 0152 } 0153 0154 if(sunken) 0155 r.adjust(1, 1, 1, 1); 0156 0157 painter->setPen(col); 0158 painter->drawText(r, Qt::AlignVCenter|Qt::AlignHCenter, isMenuBar ? i18n("M") : i18n("S")); 0159 } 0160 0161 } 0162 }