Falkon Develop
Cross-platform Qt-based web browser
gm_settingslistwidget.cpp
Go to the documentation of this file.
1/* ============================================================
2* GreaseMonkey plugin for Falkon
3* Copyright (C) 2012-2016 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
20#include "gm_script.h"
21
22#include <QMouseEvent>
23
25 : QListWidget(parent)
26 , m_delegate(new GM_SettingsListDelegate(this))
27{
28 // forced to LTR, see issue#967
29 setLayoutDirection(Qt::LeftToRight);
30 setItemDelegate(m_delegate);
31}
32
33void GM_SettingsListWidget::mousePressEvent(QMouseEvent* event)
34{
35 if (containsRemoveIcon(event->position().toPoint())) {
36 Q_EMIT removeItemRequested(itemAt(event->position().toPoint()));
37 return;
38 }
39
40 if (containsUpdateIcon(event->position().toPoint())) {
41 Q_EMIT updateItemRequested(itemAt(event->position().toPoint()));
42 return;
43 }
44
45 QListWidget::mousePressEvent(event);
46}
47
48void GM_SettingsListWidget::mouseDoubleClickEvent(QMouseEvent* event)
49{
50 if (containsRemoveIcon(event->position().toPoint()) || containsUpdateIcon(event->position().toPoint()))
51 return;
52
53 QListWidget::mouseDoubleClickEvent(event);
54}
55
56bool GM_SettingsListWidget::containsRemoveIcon(const QPoint &pos) const
57{
58 QListWidgetItem* item = itemAt(pos);
59 if (!item) {
60 return false;
61 }
62
63 const QRect rect = visualItemRect(item);
64 const int removeIconPosition = rect.right() - m_delegate->padding() - 16;
65 const int center = rect.height() / 2 + rect.top();
66 const int removeIconYPos = center - (16 / 2);
67
68 QRect removeIconRect(removeIconPosition, removeIconYPos, 16, 16);
69
70 return removeIconRect.contains(pos);
71}
72
73bool GM_SettingsListWidget::containsUpdateIcon(const QPoint &pos) const
74{
75 QListWidgetItem *item = itemAt(pos);
76 if (!item)
77 return false;
78
79 GM_Script *script = static_cast<GM_Script*>(item->data(Qt::UserRole + 10).value<void*>());
80 if (!script || script->downloadUrl().isEmpty())
81 return false;
82
83 const QRect rect = visualItemRect(item);
84 const int updateIconPosition = rect.right() - m_delegate->padding() * 2 - 16 * 2;
85 const int center = rect.height() / 2 + rect.top();
86 const int updateIconYPos = center - (16 / 2);
87
88 QRect updateIconRect(updateIconPosition, updateIconYPos, 16, 16);
89
90 return updateIconRect.contains(pos);
91}
QUrl downloadUrl() const
Definition: gm_script.cpp:92
GM_SettingsListWidget(QWidget *parent=nullptr)
void removeItemRequested(QListWidgetItem *item)
void updateItemRequested(QListWidgetItem *item)