File indexing completed on 2025-01-05 04:35:04

0001 /* ============================================================
0002 * GreaseMonkey plugin for Falkon
0003 * Copyright (C) 2012-2016  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 "gm_settingslistwidget.h"
0019 #include "gm_settingslistdelegate.h"
0020 #include "gm_script.h"
0021 
0022 #include <QMouseEvent>
0023 
0024 GM_SettingsListWidget::GM_SettingsListWidget(QWidget* parent)
0025     : QListWidget(parent)
0026     , m_delegate(new GM_SettingsListDelegate(this))
0027 {
0028     // forced to LTR, see issue#967
0029     setLayoutDirection(Qt::LeftToRight);
0030     setItemDelegate(m_delegate);
0031 }
0032 
0033 void GM_SettingsListWidget::mousePressEvent(QMouseEvent* event)
0034 {
0035     if (containsRemoveIcon(event->position().toPoint())) {
0036         Q_EMIT removeItemRequested(itemAt(event->position().toPoint()));
0037         return;
0038     }
0039 
0040     if (containsUpdateIcon(event->position().toPoint())) {
0041         Q_EMIT updateItemRequested(itemAt(event->position().toPoint()));
0042         return;
0043     }
0044 
0045     QListWidget::mousePressEvent(event);
0046 }
0047 
0048 void GM_SettingsListWidget::mouseDoubleClickEvent(QMouseEvent* event)
0049 {
0050     if (containsRemoveIcon(event->position().toPoint()) || containsUpdateIcon(event->position().toPoint()))
0051         return;
0052 
0053     QListWidget::mouseDoubleClickEvent(event);
0054 }
0055 
0056 bool GM_SettingsListWidget::containsRemoveIcon(const QPoint &pos) const
0057 {
0058     QListWidgetItem* item = itemAt(pos);
0059     if (!item) {
0060         return false;
0061     }
0062 
0063     const QRect rect = visualItemRect(item);
0064     const int removeIconPosition = rect.right() - m_delegate->padding() - 16;
0065     const int center = rect.height() / 2 + rect.top();
0066     const int removeIconYPos = center - (16 / 2);
0067 
0068     QRect removeIconRect(removeIconPosition, removeIconYPos, 16, 16);
0069 
0070     return removeIconRect.contains(pos);
0071 }
0072 
0073 bool GM_SettingsListWidget::containsUpdateIcon(const QPoint &pos) const
0074 {
0075     QListWidgetItem *item = itemAt(pos);
0076     if (!item)
0077         return false;
0078 
0079     GM_Script *script = static_cast<GM_Script*>(item->data(Qt::UserRole + 10).value<void*>());
0080     if (!script || script->downloadUrl().isEmpty())
0081         return false;
0082 
0083     const QRect rect = visualItemRect(item);
0084     const int updateIconPosition = rect.right() - m_delegate->padding() * 2 - 16 * 2;
0085     const int center = rect.height() / 2 + rect.top();
0086     const int updateIconYPos = center - (16 / 2);
0087 
0088     QRect updateIconRect(updateIconPosition, updateIconYPos, 16, 16);
0089 
0090     return updateIconRect.contains(pos);
0091 }