Falkon Develop
Cross-platform Qt-based web browser
siteicon.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2018 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* ============================================================ */
18#include "siteicon.h"
19#include "siteinfowidget.h"
20#include "locationbar.h"
21#include "tabbedwebview.h"
22#include "qztools.h"
23#include "siteinfo.h"
24
25#include <QDrag>
26#include <QTimer>
27#include <QMimeData>
28#include <QApplication>
29#include <QContextMenuEvent>
30
32 : ToolButton(parent)
33 , m_window(nullptr)
34 , m_locationBar(parent)
35 , m_view(nullptr)
36{
37 setObjectName("locationbar-siteicon");
38 setToolButtonStyle(Qt::ToolButtonIconOnly);
39 setCursor(Qt::ArrowCursor);
40 setToolTip(LocationBar::tr("Show information about this page"));
41 setFocusPolicy(Qt::NoFocus);
42
43 m_updateTimer = new QTimer(this);
44 m_updateTimer->setInterval(100);
45 m_updateTimer->setSingleShot(true);
46 connect(m_updateTimer, &QTimer::timeout, this, &SiteIcon::updateIcon);
47}
48
50{
51 m_window = window;
52}
53
55{
56 m_view = view;
57}
58
59void SiteIcon::setIcon(const QIcon &icon)
60{
61 bool wasNull = m_icon.isNull();
62
63 m_icon = icon;
64
65 if (wasNull) {
66 updateIcon();
67 }
68 else {
69 m_updateTimer->start();
70 }
71}
72
73void SiteIcon::updateIcon()
74{
75 ToolButton::setIcon(m_icon);
76}
77
78void SiteIcon::popupClosed()
79{
80 setDown(false);
81}
82
83void SiteIcon::contextMenuEvent(QContextMenuEvent* e)
84{
85 // Prevent propagating to LocationBar
86 e->accept();
87}
88
89void SiteIcon::mousePressEvent(QMouseEvent* e)
90{
91 if (e->buttons() == Qt::LeftButton) {
92 m_dragStartPosition = e->pos();
93 }
94
95 // Prevent propagating to LocationBar
96 e->accept();
97
99}
100
101void SiteIcon::mouseReleaseEvent(QMouseEvent* e)
102{
103 // Mouse release event is restoring Down state
104 // So we pause updates to prevent flicker
105
106 bool activated = false;
107
108 if (e->button() == Qt::LeftButton && rect().contains(e->pos())) {
109 // Popup may not be always shown, eg. on falkon: pages
110 activated = showPopup();
111 }
112
113 if (activated) {
114 setUpdatesEnabled(false);
115 }
116
118
119 if (activated) {
120 setDown(true);
121 setUpdatesEnabled(true);
122 }
123}
124
125void SiteIcon::mouseMoveEvent(QMouseEvent* e)
126{
127 if (!m_locationBar || e->buttons() != Qt::LeftButton) {
128 ToolButton::mouseMoveEvent(e);
129 return;
130 }
131
132 int manhattanLength = (e->pos() - m_dragStartPosition).manhattanLength();
133 if (manhattanLength <= QApplication::startDragDistance()) {
134 ToolButton::mouseMoveEvent(e);
135 return;
136 }
137
138 const QUrl url = m_locationBar->webView()->url();
139 const QString title = m_locationBar->webView()->title();
140
141 if (url.isEmpty() || title.isEmpty()) {
142 ToolButton::mouseMoveEvent(e);
143 return;
144 }
145
146 auto* drag = new QDrag(this);
147 auto* mime = new QMimeData;
148 mime->setUrls(QList<QUrl>() << url);
149 mime->setText(title);
150 mime->setImageData(icon().pixmap(16).toImage());
151
152 drag->setMimeData(mime);
153 drag->setPixmap(QzTools::createPixmapForSite(icon(), title, url.toString()));
154 drag->exec();
155
156 // Restore Down state
157 setDown(false);
158}
159
160bool SiteIcon::showPopup()
161{
162 if (!m_view || !m_window) {
163 return false;
164 }
165
166 QUrl url = m_view->url();
167
169 return false;
170
171 setDown(true);
172
173 auto* info = new SiteInfoWidget(m_window);
174 info->showAt(parentWidget());
175
176 connect(info, &QObject::destroyed, this, &SiteIcon::popupClosed);
177
178 return true;
179}
TabbedWebView * webView() const
static QPixmap createPixmapForSite(const QIcon &icon, const QString &title, const QString &url)
Definition: qztools.cpp:398
void setIcon(const QIcon &icon)
Definition: siteicon.cpp:59
SiteIcon(LocationBar *parent)
Definition: siteicon.cpp:31
void setBrowserWindow(BrowserWindow *window)
Definition: siteicon.cpp:49
void setWebView(WebView *view)
Definition: siteicon.cpp:54
static bool canShowSiteInfo(const QUrl &url)
Definition: siteinfo.cpp:162
void mousePressEvent(QMouseEvent *e) override
Definition: toolbutton.cpp:186
void mouseReleaseEvent(QMouseEvent *e) override
Definition: toolbutton.cpp:203
QIcon icon
Definition: toolbutton.h:34
void setIcon(const QIcon &icon)
Definition: toolbutton.cpp:85
QString title(bool allowEmpty=false) const
Definition: webview.cpp:107