Falkon Develop
Cross-platform Qt-based web browser
bookmarksicon.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2014 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 "bookmarksicon.h"
19#include "bookmarkswidget.h"
20#include "bookmarks.h"
21#include "mainapplication.h"
22#include "webview.h"
23#include "locationbar.h"
24#include "pluginproxy.h"
25#include "speeddial.h"
26
27#include <QStyle>
28#include <QContextMenuEvent>
29
31 : ClickableLabel(parent)
32 , m_view(nullptr)
33 , m_bookmark(nullptr)
34{
35 setObjectName("locationbar-bookmarkicon");
36 setCursor(Qt::PointingHandCursor);
37 setToolTip(tr("Bookmark this Page"));
38 setFocusPolicy(Qt::ClickFocus);
39
40 connect(mApp->bookmarks(), SIGNAL(bookmarkAdded(BookmarkItem*)), this, SLOT(bookmarksChanged()));
41 connect(mApp->bookmarks(), SIGNAL(bookmarkRemoved(BookmarkItem*)), this, SLOT(bookmarksChanged()));
42 connect(mApp->bookmarks(), SIGNAL(bookmarkChanged(BookmarkItem*)), this, SLOT(bookmarksChanged()));
43 connect(mApp->plugins()->speedDial(), SIGNAL(pagesChanged()), this, SLOT(speedDialChanged()));
44
45 connect(this, SIGNAL(clicked(QPoint)), this, SLOT(iconClicked()));
46}
47
49{
50 m_view = view;
51 connect(view, &WebView::urlChanged, this, [this](const QUrl &url) {
52 checkBookmark(url);
53 });
54}
55
56void BookmarksIcon::checkBookmark(const QUrl &url, bool forceCheck)
57{
58 if (!forceCheck && m_lastUrl == url) {
59 return;
60 }
61
62 QList<BookmarkItem*> items = mApp->bookmarks()->searchBookmarks(url);
63 m_bookmark = items.isEmpty() ? nullptr : items.at(0);
64
65 if (m_bookmark || mApp->plugins()->speedDial()->pageForUrl(url).isValid()) {
66 setBookmarkSaved();
67 }
68 else {
69 setBookmarkDisabled();
70 }
71
72 m_lastUrl = url;
73}
74
75void BookmarksIcon::bookmarksChanged()
76{
77 checkBookmark(m_lastUrl, true);
78}
79
80void BookmarksIcon::speedDialChanged()
81{
82 checkBookmark(m_lastUrl, true);
83}
84
85void BookmarksIcon::iconClicked()
86{
87 if (!m_view) {
88 return;
89 }
90
91 auto* widget = new BookmarksWidget(m_view, m_bookmark, parentWidget());
92 widget->showAt(parentWidget());
93}
94
95void BookmarksIcon::setBookmarkSaved()
96{
97 setProperty("bookmarked", QVariant(true));
98 style()->unpolish(this);
99 style()->polish(this);
100 setToolTip(tr("Edit this bookmark"));
101}
102
103void BookmarksIcon::setBookmarkDisabled()
104{
105 setProperty("bookmarked", QVariant(false));
106 style()->unpolish(this);
107 style()->polish(this);
108 setToolTip(tr("Bookmark this Page"));
109}
110
111void BookmarksIcon::contextMenuEvent(QContextMenuEvent* ev)
112{
113 // Prevent propagating to LocationBar
114 ev->accept();
115}
116
117void BookmarksIcon::mousePressEvent(QMouseEvent* ev)
118{
119 ClickableLabel::mousePressEvent(ev);
120
121 // Prevent propagating to LocationBar
122 ev->accept();
123}
BookmarksIcon(QWidget *parent=nullptr)
void checkBookmark(const QUrl &url, bool forceCheck=false)
void setWebView(WebView *view)
void clicked(QPoint)
#define mApp