Warning, file /network/ktp-contact-list/contact-view-hover-button.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 Qt item view mouse hover button 0003 0004 Copyright (C) 2008 Peter Penz <peter dot penz at gmx dot at> 0005 Copyright (C) 2009 Marcel Wiesweg <marcel dot wiesweg at gmx dot de> 0006 Copyright (C) 2011 Martin Klapetek <martin dot klapetek at gmail dot com> 0007 0008 This library is free software; you can redistribute it and/or 0009 modify it under the terms of the GNU Lesser General Public 0010 License as published by the Free Software Foundation; either 0011 version 2.1 of the License, or (at your option) any later version. 0012 0013 This library 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, write to the Free Software 0020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 0021 */ 0022 0023 #include "contact-view-hover-button.h" 0024 0025 // Qt includes 0026 0027 #include <QPainter> 0028 #include <QPaintEvent> 0029 #include <QtCore/QRect> 0030 #include <QtCore/QTimer> 0031 #include <QtCore/QTimeLine> 0032 0033 // KDE includes 0034 0035 #include <QIcon> 0036 #include <kiconloader.h> 0037 #include <kiconeffect.h> 0038 0039 #include <KLocalizedString> 0040 0041 ContactViewHoverButton::ContactViewHoverButton(QAbstractItemView *view) 0042 : QAbstractButton(view->viewport()), 0043 m_isHovered(false), 0044 m_fadingValue(0), 0045 m_icon(), 0046 m_fadingTimeLine(0) 0047 { 0048 const int duration = 300; 0049 m_fadingTimeLine = new QTimeLine(duration, this); 0050 m_fadingTimeLine->setFrameRange(0, 255); 0051 0052 setCheckable(true); 0053 setChecked(false); 0054 0055 connect(m_fadingTimeLine, SIGNAL(frameChanged(int)), 0056 this, SLOT(setFadingValue(int))); 0057 0058 connect(this, SIGNAL(toggled(bool)), 0059 this, SLOT(refreshIcon())); 0060 0061 //FIXME: what should replace this? 0062 // connect(KGlobalSettings::self(), SIGNAL(iconChanged(int)), 0063 // this, SLOT(refreshIcon())); 0064 } 0065 0066 void ContactViewHoverButton::initIcon() 0067 { 0068 // virtual, cannot call from constructor 0069 refreshIcon(); 0070 resize(sizeHint()); 0071 } 0072 0073 void ContactViewHoverButton::updateToolTip() 0074 { 0075 } 0076 0077 void ContactViewHoverButton::reset() 0078 { 0079 m_index = QModelIndex(); 0080 // hide(); 0081 } 0082 0083 void ContactViewHoverButton::setIndex(const QModelIndex& index) 0084 { 0085 m_index = index; 0086 0087 if (index.isValid()) { 0088 startFading(); 0089 } 0090 } 0091 0092 QModelIndex ContactViewHoverButton::index() const 0093 { 0094 return m_index; 0095 } 0096 0097 void ContactViewHoverButton::setVisible(bool visible) 0098 { 0099 QAbstractButton::setVisible(visible); 0100 0101 stopFading(); 0102 0103 if (visible) { 0104 startFading(); 0105 } 0106 0107 } 0108 0109 void ContactViewHoverButton::enterEvent(QEvent* event) 0110 { 0111 QAbstractButton::enterEvent(event); 0112 0113 // if the mouse cursor is above the button, display 0114 // it immediately without fading timer 0115 m_isHovered = true; 0116 m_fadingTimeLine->stop(); 0117 m_fadingValue = 255; 0118 updateToolTip(); 0119 update(); 0120 } 0121 0122 void ContactViewHoverButton::leaveEvent(QEvent* event) 0123 { 0124 QAbstractButton::leaveEvent(event); 0125 m_isHovered = false; 0126 update(); 0127 } 0128 0129 void ContactViewHoverButton::paintEvent(QPaintEvent* event) 0130 { 0131 QPainter painter(this); 0132 painter.setClipRect(event->rect()); 0133 painter.setRenderHint(QPainter::Antialiasing); 0134 0135 // draw an alpha blended circle as background 0136 const QPalette& palette = parentWidget()->palette(); 0137 0138 const QBrush& backgroundBrush = palette.brush(QPalette::Normal, QPalette::Window); 0139 QColor background = backgroundBrush.color(); 0140 background.setAlpha(m_fadingValue / 2); 0141 painter.setBrush(background); 0142 0143 const QBrush& foregroundBrush = palette.brush(QPalette::Normal, QPalette::WindowText); 0144 QColor foreground = foregroundBrush.color(); 0145 foreground.setAlpha(m_fadingValue / 4); 0146 painter.setPen(foreground); 0147 0148 painter.drawEllipse(0, 0, width(), height()); 0149 0150 qreal opacity = painter.opacity(); 0151 0152 // draw the icon overlay 0153 if (m_isHovered) { 0154 KIconEffect iconEffect; 0155 QPixmap activeIcon = iconEffect.apply(m_icon, KIconLoader::Desktop, KIconLoader::ActiveState); 0156 painter.drawPixmap(0, 0, activeIcon); 0157 } else { 0158 if (m_fadingValue < 255) { 0159 // apply an alpha mask respecting the fading value to the icon 0160 QPixmap icon = m_icon; 0161 painter.setOpacity((1.0 / 255) * m_fadingValue); 0162 painter.drawPixmap(0, 0, icon); 0163 // reset the opacity to the previous value 0164 painter.setOpacity(opacity); 0165 } else { 0166 // no fading is required 0167 painter.drawPixmap(0, 0, m_icon); 0168 } 0169 } 0170 } 0171 0172 void ContactViewHoverButton::setFadingValue(int value) 0173 { 0174 m_fadingValue = value; 0175 0176 if (m_fadingValue >= 255) { 0177 m_fadingTimeLine->stop(); 0178 } 0179 0180 update(); 0181 } 0182 0183 /* 0184 void ContactViewHoverButton::setIconOverlay() 0185 { 0186 const char* icon = isChecked() ? "list-remove" : "list-add"; 0187 m_icon = KIconLoader::global()->loadIcon(icon, 0188 KIconLoader::NoGroup, 0189 KIconLoader::SizeSmall); 0190 } 0191 */ 0192 0193 void ContactViewHoverButton::refreshIcon() 0194 { 0195 m_icon = icon(); 0196 update(); 0197 } 0198 0199 void ContactViewHoverButton::startFading() 0200 { 0201 if (m_fadingTimeLine->state() != QTimeLine::Running) { 0202 m_fadingTimeLine->start(); 0203 } 0204 0205 m_fadingValue = 0; 0206 } 0207 0208 void ContactViewHoverButton::stopFading() 0209 { 0210 m_fadingTimeLine->stop(); 0211 m_fadingValue = 0; 0212 }