File indexing completed on 2024-05-12 04:58:26

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "clickablelabel.h"
0019 
0020 #include <QMouseEvent>
0021 
0022 ClickableLabel::ClickableLabel(QWidget* parent)
0023     : QLabel(parent)
0024 {
0025 }
0026 
0027 QString ClickableLabel::themeIcon() const
0028 {
0029     return m_themeIcon;
0030 }
0031 
0032 void ClickableLabel::setThemeIcon(const QString &name)
0033 {
0034     m_themeIcon = name;
0035     updateIcon();
0036 }
0037 
0038 QIcon ClickableLabel::fallbackIcon() const
0039 {
0040     return m_fallbackIcon;
0041 }
0042 
0043 void ClickableLabel::setFallbackIcon(const QIcon &fallbackIcon)
0044 {
0045     m_fallbackIcon = fallbackIcon;
0046     updateIcon();
0047 }
0048 
0049 void ClickableLabel::updateIcon()
0050 {
0051     if (!m_themeIcon.isEmpty()) {
0052         const QIcon icon = QIcon::fromTheme(m_themeIcon);
0053         if (!icon.isNull()) {
0054             setPixmap(icon.pixmap(size()));
0055             return;
0056         }
0057     }
0058 
0059     if (!m_fallbackIcon.isNull())
0060         setPixmap(m_fallbackIcon.pixmap(size()));
0061 }
0062 
0063 void ClickableLabel::resizeEvent(QResizeEvent *ev)
0064 {
0065     QLabel::resizeEvent(ev);
0066     updateIcon();
0067 }
0068 
0069 void ClickableLabel::mouseReleaseEvent(QMouseEvent* ev)
0070 {
0071     if (ev->button() == Qt::LeftButton && rect().contains(ev->pos())) {
0072         if (ev->modifiers() == Qt::ControlModifier) {
0073             Q_EMIT middleClicked(ev->globalPosition().toPoint());
0074         }
0075         else {
0076             Q_EMIT clicked(ev->globalPosition().toPoint());
0077         }
0078     }
0079     else if (ev->button() == Qt::MiddleButton && rect().contains(ev->pos())) {
0080         Q_EMIT middleClicked(ev->globalPosition().toPoint());
0081     }
0082     else {
0083         QLabel::mouseReleaseEvent(ev);
0084     }
0085 }