Added clickable label.

This commit is contained in:
Martin Rotter 2017-07-19 12:03:41 +02:00
parent f02f8bab09
commit b6d7e8be2e
5 changed files with 294 additions and 160 deletions

View file

@ -334,7 +334,8 @@ HEADERS += src/core/feeddownloader.h \
src/core/messagesmodelsqllayer.h \ src/core/messagesmodelsqllayer.h \
src/gui/treeviewcolumnsmenu.h \ src/gui/treeviewcolumnsmenu.h \
src/services/abstract/labelsrootitem.h \ src/services/abstract/labelsrootitem.h \
src/services/abstract/label.h src/services/abstract/label.h \
src/gui/clickablelabel.h
SOURCES += src/core/feeddownloader.cpp \ SOURCES += src/core/feeddownloader.cpp \
src/core/feedsmodel.cpp \ src/core/feedsmodel.cpp \
@ -460,7 +461,8 @@ SOURCES += src/core/feeddownloader.cpp \
src/core/messagesmodelsqllayer.cpp \ src/core/messagesmodelsqllayer.cpp \
src/gui/treeviewcolumnsmenu.cpp \ src/gui/treeviewcolumnsmenu.cpp \
src/services/abstract/labelsrootitem.cpp \ src/services/abstract/labelsrootitem.cpp \
src/services/abstract/label.cpp src/services/abstract/label.cpp \
src/gui/clickablelabel.cpp
OBJECTIVE_SOURCES += src/miscellaneous/disablewindowtabbing.mm OBJECTIVE_SOURCES += src/miscellaneous/disablewindowtabbing.mm

85
src/gui/clickablelabel.cpp Executable file
View file

@ -0,0 +1,85 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#include "gui/clickablelabel.h"
#include "miscellaneous/application.h"
#include "miscellaneous/iconfactory.h"
#include <QMouseEvent>
ClickableLabel::ClickableLabel(QWidget* parent)
: QLabel(parent) {
}
QString ClickableLabel::themeIcon() const {
return m_themeIcon;
}
void ClickableLabel::setThemeIcon(const QString &name) {
m_themeIcon = name;
updateIcon();
}
QIcon ClickableLabel::fallbackIcon() const {
return m_fallbackIcon;
}
void ClickableLabel::setFallbackIcon(const QIcon &fallbackIcon) {
m_fallbackIcon = fallbackIcon;
updateIcon();
}
void ClickableLabel::updateIcon() {
if (!m_themeIcon.isEmpty()) {
const QIcon icon = qApp->icons()->fromTheme(m_themeIcon);
if (!icon.isNull()) {
setPixmap(icon.pixmap(size()));
return;
}
}
if (!m_fallbackIcon.isNull()) {
setPixmap(m_fallbackIcon.pixmap(size()));
}
}
void ClickableLabel::resizeEvent(QResizeEvent *ev) {
QLabel::resizeEvent(ev);
updateIcon();
}
void ClickableLabel::mouseReleaseEvent(QMouseEvent* ev) {
if (ev->button() == Qt::LeftButton && rect().contains(ev->pos())) {
if (ev->modifiers() == Qt::ControlModifier) {
emit middleClicked(ev->globalPos());
}
else {
emit clicked(ev->globalPos());
}
}
else if (ev->button() == Qt::MiddleButton && rect().contains(ev->pos())) {
emit middleClicked(ev->globalPos());
}
else {
QLabel::mouseReleaseEvent(ev);
}
}

60
src/gui/clickablelabel.h Executable file
View file

@ -0,0 +1,60 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H
#include <QLabel>
#include <QIcon>
class QMouseEvent;
class ClickableLabel : public QLabel{
Q_OBJECT
Q_PROPERTY(QSize fixedsize READ size WRITE setFixedSize)
Q_PROPERTY(int fixedwidth READ width WRITE setFixedWidth)
Q_PROPERTY(int fixedheight READ height WRITE setFixedHeight)
Q_PROPERTY(QString themeIcon READ themeIcon WRITE setThemeIcon)
Q_PROPERTY(QIcon fallbackIcon READ fallbackIcon WRITE setFallbackIcon)
public:
explicit ClickableLabel(QWidget *parent = 0);
QString themeIcon() const;
void setThemeIcon(const QString &name);
QIcon fallbackIcon() const;
void setFallbackIcon(const QIcon &fallbackIcon);
signals:
void clicked(QPoint);
void middleClicked(QPoint);
private:
void updateIcon();
void resizeEvent(QResizeEvent *ev);
void mouseReleaseEvent(QMouseEvent* ev);
QString m_themeIcon;
QIcon m_fallbackIcon;
};
#endif // CLICKABLELABEL_H

View file

@ -20,199 +20,183 @@
#include "adblockrule.h" #include "adblockrule.h"
#include "adblockmanager.h" #include "adblockmanager.h"
#include "adblocksubscription.h" #include "adblocksubscription.h"
#include "mainapplication.h"
#include "browserwindow.h"
#include "webpage.h"
#include "tabbedwebview.h"
#include "tabwidget.h" #include "tabwidget.h"
#include "desktopnotificationsfactory.h"
#include "qztools.h"
#include <QMenu> #include <QMenu>
#include <QTimer> #include <QTimer>
AdBlockIcon::AdBlockIcon(BrowserWindow* window, QWidget* parent) AdBlockIcon::AdBlockIcon(BrowserWindow* window, QWidget* parent)
: ClickableLabel(parent) : ClickableLabel(parent), m_window(window), m_menuAction(0), m_flashTimer(0), m_timerTicks(0), m_enabled(false) {
, m_window(window) setCursor(Qt::PointingHandCursor);
, m_menuAction(0) setToolTip(tr("AdBlock lets you block unwanted content on web pages"));
, m_flashTimer(0) setFixedSize(16, 16);
, m_timerTicks(0)
, m_enabled(false)
{
setCursor(Qt::PointingHandCursor);
setToolTip(tr("AdBlock lets you block unwanted content on web pages"));
setFixedSize(16, 16);
connect(this, SIGNAL(clicked(QPoint)), this, SLOT(showMenu(QPoint))); connect(this, SIGNAL(clicked(QPoint)), this, SLOT(showMenu(QPoint)));
connect(AdBlockManager::instance(), SIGNAL(enabledChanged(bool)), this, SLOT(setEnabled(bool))); connect(AdBlockManager::instance(), SIGNAL(enabledChanged(bool)), this, SLOT(setEnabled(bool)));
} }
AdBlockIcon::~AdBlockIcon() AdBlockIcon::~AdBlockIcon() {
{ for (int i = 0; i < m_blockedPopups.count(); ++i) {
for (int i = 0; i < m_blockedPopups.count(); ++i) delete m_blockedPopups.at(i).first;
delete m_blockedPopups.at(i).first; }
} }
void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url) void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url) {
{ int index = ruleString.lastIndexOf(QLatin1String(" ("));
int index = ruleString.lastIndexOf(QLatin1String(" ("));
const QString subscriptionName = ruleString.left(index); const QString subscriptionName = ruleString.left(index);
const QString filter = ruleString.mid(index + 2, ruleString.size() - index - 3); const QString filter = ruleString.mid(index + 2, ruleString.size() - index - 3);
AdBlockSubscription* subscription = AdBlockManager::instance()->subscriptionByName(subscriptionName); AdBlockSubscription *subscription = AdBlockManager::instance()->subscriptionByName(subscriptionName);
if (filter.isEmpty() || !subscription) { if (filter.isEmpty() || !subscription) {
return; return;
} }
QPair<AdBlockRule*, QUrl> pair; QPair<AdBlockRule*, QUrl> pair;
pair.first = new AdBlockRule(filter, subscription); pair.first = new AdBlockRule(filter, subscription);
pair.second = url; pair.second = url;
m_blockedPopups.append(pair); m_blockedPopups.append(pair);
mApp->desktopNotifications()->showNotification(QPixmap(":html/adblock_big.png"), tr("Blocked popup window"), tr("AdBlock blocked unwanted popup window.")); mApp->desktopNotifications()->showNotification(QPixmap(":html/adblock_big.png"), tr("Blocked popup window"), tr("AdBlock blocked unwanted popup window."));
if (!m_flashTimer) { if (!m_flashTimer) {
m_flashTimer = new QTimer(this); m_flashTimer = new QTimer(this);
} }
if (m_flashTimer->isActive()) { if (m_flashTimer->isActive()) {
stopAnimation(); stopAnimation();
} }
m_flashTimer->setInterval(500); m_flashTimer->setInterval(500);
m_flashTimer->start(); m_flashTimer->start();
connect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon())); connect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon()));
} }
QAction* AdBlockIcon::menuAction() QAction *AdBlockIcon::menuAction() {
{ if (!m_menuAction) {
if (!m_menuAction) { m_menuAction = new QAction(tr("AdBlock"), this);
m_menuAction = new QAction(tr("AdBlock"), this); m_menuAction->setMenu(new QMenu);
m_menuAction->setMenu(new QMenu); connect(m_menuAction->menu(), SIGNAL(aboutToShow()), this, SLOT(createMenu()));
connect(m_menuAction->menu(), SIGNAL(aboutToShow()), this, SLOT(createMenu())); }
}
m_menuAction->setIcon(QIcon(m_enabled ? ":icons/other/adblock.png" : ":icons/other/adblock-disabled.png")); m_menuAction->setIcon(QIcon(m_enabled ? ":icons/other/adblock.png" : ":icons/other/adblock-disabled.png"));
return m_menuAction; return m_menuAction;
} }
void AdBlockIcon::createMenu(QMenu* menu) void AdBlockIcon::createMenu(QMenu* menu) {
{ if (!menu) {
menu = qobject_cast<QMenu*>(sender());
if (!menu) { if (!menu) {
menu = qobject_cast<QMenu*>(sender()); return;
if (!menu) {
return;
}
} }
}
menu->clear(); menu->clear();
AdBlockManager* manager = AdBlockManager::instance(); AdBlockManager* manager = AdBlockManager::instance();
AdBlockCustomList* customList = manager->customList(); AdBlockCustomList* customList = manager->customList();
WebPage* page = m_window->weView()->page(); WebPage *page = m_window->weView()->page();
const QUrl pageUrl = page->url(); const QUrl pageUrl = page->url();
menu->addAction(tr("Show AdBlock &Settings"), manager, SLOT(showDialog()));
menu->addSeparator();
if (!pageUrl.host().isEmpty() && m_enabled && manager->canRunOnScheme(pageUrl.scheme())) {
const QString host = page->url().host().contains(QLatin1String("www.")) ? pageUrl.host().mid(4) : pageUrl.host();
const QString hostFilter = QString("@@||%1^$document").arg(host);
const QString pageFilter = QString("@@|%1|$document").arg(pageUrl.toString());
QAction *act = menu->addAction(tr("Disable on %1").arg(host));
act->setCheckable(true);
act->setChecked(customList->containsFilter(hostFilter));
act->setData(hostFilter);
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter()));
act = menu->addAction(tr("Disable only on this page"));
act->setCheckable(true);
act->setChecked(customList->containsFilter(pageFilter));
act->setData(pageFilter);
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter()));
menu->addAction(tr("Show AdBlock &Settings"), manager, SLOT(showDialog()));
menu->addSeparator(); menu->addSeparator();
}
if (!pageUrl.host().isEmpty() && m_enabled && manager->canRunOnScheme(pageUrl.scheme())) { if (!m_blockedPopups.isEmpty()) {
const QString host = page->url().host().contains(QLatin1String("www.")) ? pageUrl.host().mid(4) : pageUrl.host(); menu->addAction(tr("Blocked Popup Windows"))->setEnabled(false);
const QString hostFilter = QString("@@||%1^$document").arg(host);
const QString pageFilter = QString("@@|%1|$document").arg(pageUrl.toString());
QAction* act = menu->addAction(tr("Disable on %1").arg(host)); for (int i = 0; i < m_blockedPopups.count(); i++) {
act->setCheckable(true); const QPair<AdBlockRule*, QUrl> &pair = m_blockedPopups.at(i);
act->setChecked(customList->containsFilter(hostFilter));
act->setData(hostFilter);
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter()));
act = menu->addAction(tr("Disable only on this page")); QString address = pair.second.toString().right(55);
act->setCheckable(true); QString actionText = tr("%1 with (%2)").arg(address, pair.first->filter()).replace(QLatin1Char('&'), QLatin1String("&&"));
act->setChecked(customList->containsFilter(pageFilter));
act->setData(pageFilter);
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter()));
menu->addSeparator(); QAction *action = menu->addAction(actionText, manager, SLOT(showRule()));
} action->setData(QVariant::fromValue((void*)pair.first));
if (!m_blockedPopups.isEmpty()) {
menu->addAction(tr("Blocked Popup Windows"))->setEnabled(false);
for (int i = 0; i < m_blockedPopups.count(); i++) {
const QPair<AdBlockRule*, QUrl> &pair = m_blockedPopups.at(i);
QString address = pair.second.toString().right(55);
QString actionText = tr("%1 with (%2)").arg(address, pair.first->filter()).replace(QLatin1Char('&'), QLatin1String("&&"));
QAction* action = menu->addAction(actionText, manager, SLOT(showRule()));
action->setData(QVariant::fromValue((void*)pair.first));
}
} }
}
} }
void AdBlockIcon::showMenu(const QPoint &pos) void AdBlockIcon::showMenu(const QPoint &pos) {
{ QMenu menu;
QMenu menu; createMenu(&menu);
createMenu(&menu);
menu.exec(pos); menu.exec(pos);
} }
void AdBlockIcon::toggleCustomFilter() void AdBlockIcon::toggleCustomFilter() {
{ QAction* action = qobject_cast<QAction*>(sender());
QAction* action = qobject_cast<QAction*>(sender());
if (!action) {
return;
}
const QString filter = action->data().toString(); if (!action) {
AdBlockManager* manager = AdBlockManager::instance(); return;
AdBlockCustomList* customList = manager->customList(); }
if (customList->containsFilter(filter)) { const QString filter = action->data().toString();
customList->removeFilter(filter); AdBlockManager *manager = AdBlockManager::instance();
} AdBlockCustomList *customList = manager->customList();
else {
AdBlockRule* rule = new AdBlockRule(filter, customList); if (customList->containsFilter(filter)) {
customList->addRule(rule); customList->removeFilter(filter);
} }
else {
AdBlockRule *rule = new AdBlockRule(filter, customList);
customList->addRule(rule);
}
} }
void AdBlockIcon::animateIcon() void AdBlockIcon::animateIcon() {
{ ++m_timerTicks;
++m_timerTicks;
if (m_timerTicks > 10) {
stopAnimation();
return;
}
if (pixmap()->isNull()) { if (m_timerTicks > 10) {
setPixmap(QIcon(QSL(":icons/other/adblock.png")).pixmap(16)); stopAnimation();
} return;
else { }
setPixmap(QPixmap());
} if (pixmap()->isNull()) {
setPixmap(QIcon(QSL(":icons/other/adblock.png")).pixmap(16));
}
else {
setPixmap(QPixmap());
}
} }
void AdBlockIcon::stopAnimation() void AdBlockIcon::stopAnimation() {
{ m_timerTicks = 0;
m_timerTicks = 0; m_flashTimer->stop();
m_flashTimer->stop(); disconnect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon()));
disconnect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon()));
setEnabled(m_enabled); setEnabled(m_enabled);
} }
void AdBlockIcon::setEnabled(bool enabled) void AdBlockIcon::setEnabled(bool enabled) {
{ if (enabled) {
if (enabled) { setPixmap(QIcon(QSL(":icons/other/adblock.png")).pixmap(16));
setPixmap(QIcon(QSL(":icons/other/adblock.png")).pixmap(16)); }
} else {
else { setPixmap(QIcon(QSL(":icons/other/adblock-disabled.png")).pixmap(16));
setPixmap(QIcon(QSL(":icons/other/adblock-disabled.png")).pixmap(16)); }
}
m_enabled = enabled; m_enabled = enabled;
} }

View file

@ -19,42 +19,45 @@
#ifndef ADBLOCKICON_H #ifndef ADBLOCKICON_H
#define ADBLOCKICON_H #define ADBLOCKICON_H
#include "qzcommon.h" #include "gui/clickablelabel.h"
#include "clickablelabel.h"
#include "adblockrule.h" #include "network-web/adblock/adblockrule.h"
class QMenu; class QMenu;
class QUrl; class QUrl;
class QAction;
class QTimer;
class BrowserWindow; class BrowserWindow;
class QUPZILLA_EXPORT AdBlockIcon : public ClickableLabel class AdBlockIcon : public ClickableLabel {
{
Q_OBJECT Q_OBJECT
public:
public:
explicit AdBlockIcon(BrowserWindow* window, QWidget* parent = 0); explicit AdBlockIcon(BrowserWindow* window, QWidget* parent = 0);
~AdBlockIcon(); virtual ~AdBlockIcon();
void popupBlocked(const QString &ruleString, const QUrl &url); void popupBlocked(const QString &ruleString, const QUrl &url);
QAction* menuAction(); QAction *menuAction();
public slots: public slots:
void setEnabled(bool enabled); void setEnabled(bool enabled);
void createMenu(QMenu* menu = 0); void createMenu(QMenu *menu = 0);
private slots: private slots:
void showMenu(const QPoint &pos); void showMenu(const QPoint &pos);
void toggleCustomFilter(); void toggleCustomFilter();
void animateIcon(); void animateIcon();
void stopAnimation(); void stopAnimation();
private: private:
BrowserWindow* m_window; BrowserWindow *m_window;
QAction* m_menuAction; QAction *m_menuAction;
QVector<QPair<AdBlockRule*, QUrl> > m_blockedPopups; QVector<QPair<AdBlockRule*, QUrl> > m_blockedPopups;
QTimer* m_flashTimer; QTimer *m_flashTimer;
int m_timerTicks; int m_timerTicks;
bool m_enabled; bool m_enabled;