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/gui/treeviewcolumnsmenu.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 \
src/core/feedsmodel.cpp \
@ -460,7 +461,8 @@ SOURCES += src/core/feeddownloader.cpp \
src/core/messagesmodelsqllayer.cpp \
src/gui/treeviewcolumnsmenu.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

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 "adblockmanager.h"
#include "adblocksubscription.h"
#include "mainapplication.h"
#include "browserwindow.h"
#include "webpage.h"
#include "tabbedwebview.h"
#include "tabwidget.h"
#include "desktopnotificationsfactory.h"
#include "qztools.h"
#include <QMenu>
#include <QTimer>
AdBlockIcon::AdBlockIcon(BrowserWindow* window, QWidget* parent)
: ClickableLabel(parent)
, m_window(window)
, m_menuAction(0)
, m_flashTimer(0)
, m_timerTicks(0)
, m_enabled(false)
{
setCursor(Qt::PointingHandCursor);
setToolTip(tr("AdBlock lets you block unwanted content on web pages"));
setFixedSize(16, 16);
: ClickableLabel(parent), m_window(window), m_menuAction(0), m_flashTimer(0), 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(AdBlockManager::instance(), SIGNAL(enabledChanged(bool)), this, SLOT(setEnabled(bool)));
connect(this, SIGNAL(clicked(QPoint)), this, SLOT(showMenu(QPoint)));
connect(AdBlockManager::instance(), SIGNAL(enabledChanged(bool)), this, SLOT(setEnabled(bool)));
}
AdBlockIcon::~AdBlockIcon()
{
for (int i = 0; i < m_blockedPopups.count(); ++i)
delete m_blockedPopups.at(i).first;
AdBlockIcon::~AdBlockIcon() {
for (int i = 0; i < m_blockedPopups.count(); ++i) {
delete m_blockedPopups.at(i).first;
}
}
void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url)
{
int index = ruleString.lastIndexOf(QLatin1String(" ("));
void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url) {
int index = ruleString.lastIndexOf(QLatin1String(" ("));
const QString subscriptionName = ruleString.left(index);
const QString filter = ruleString.mid(index + 2, ruleString.size() - index - 3);
AdBlockSubscription* subscription = AdBlockManager::instance()->subscriptionByName(subscriptionName);
if (filter.isEmpty() || !subscription) {
return;
}
const QString subscriptionName = ruleString.left(index);
const QString filter = ruleString.mid(index + 2, ruleString.size() - index - 3);
AdBlockSubscription *subscription = AdBlockManager::instance()->subscriptionByName(subscriptionName);
if (filter.isEmpty() || !subscription) {
return;
}
QPair<AdBlockRule*, QUrl> pair;
pair.first = new AdBlockRule(filter, subscription);
pair.second = url;
m_blockedPopups.append(pair);
QPair<AdBlockRule*, QUrl> pair;
pair.first = new AdBlockRule(filter, subscription);
pair.second = url;
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) {
m_flashTimer = new QTimer(this);
}
if (!m_flashTimer) {
m_flashTimer = new QTimer(this);
}
if (m_flashTimer->isActive()) {
stopAnimation();
}
if (m_flashTimer->isActive()) {
stopAnimation();
}
m_flashTimer->setInterval(500);
m_flashTimer->start();
m_flashTimer->setInterval(500);
m_flashTimer->start();
connect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon()));
connect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon()));
}
QAction* AdBlockIcon::menuAction()
{
if (!m_menuAction) {
m_menuAction = new QAction(tr("AdBlock"), this);
m_menuAction->setMenu(new QMenu);
connect(m_menuAction->menu(), SIGNAL(aboutToShow()), this, SLOT(createMenu()));
}
QAction *AdBlockIcon::menuAction() {
if (!m_menuAction) {
m_menuAction = new QAction(tr("AdBlock"), this);
m_menuAction->setMenu(new QMenu);
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) {
menu = qobject_cast<QMenu*>(sender());
if (!menu) {
return;
}
return;
}
}
menu->clear();
menu->clear();
AdBlockManager* manager = AdBlockManager::instance();
AdBlockCustomList* customList = manager->customList();
AdBlockManager* manager = AdBlockManager::instance();
AdBlockCustomList* customList = manager->customList();
WebPage* page = m_window->weView()->page();
const QUrl pageUrl = page->url();
WebPage *page = m_window->weView()->page();
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();
}
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());
if (!m_blockedPopups.isEmpty()) {
menu->addAction(tr("Blocked Popup Windows"))->setEnabled(false);
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()));
for (int i = 0; i < m_blockedPopups.count(); i++) {
const QPair<AdBlockRule*, QUrl> &pair = m_blockedPopups.at(i);
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()));
QString address = pair.second.toString().right(55);
QString actionText = tr("%1 with (%2)").arg(address, pair.first->filter()).replace(QLatin1Char('&'), QLatin1String("&&"));
menu->addSeparator();
}
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));
}
QAction *action = menu->addAction(actionText, manager, SLOT(showRule()));
action->setData(QVariant::fromValue((void*)pair.first));
}
}
}
void AdBlockIcon::showMenu(const QPoint &pos)
{
QMenu menu;
createMenu(&menu);
void AdBlockIcon::showMenu(const QPoint &pos) {
QMenu menu;
createMenu(&menu);
menu.exec(pos);
menu.exec(pos);
}
void AdBlockIcon::toggleCustomFilter()
{
QAction* action = qobject_cast<QAction*>(sender());
if (!action) {
return;
}
void AdBlockIcon::toggleCustomFilter() {
QAction* action = qobject_cast<QAction*>(sender());
const QString filter = action->data().toString();
AdBlockManager* manager = AdBlockManager::instance();
AdBlockCustomList* customList = manager->customList();
if (!action) {
return;
}
if (customList->containsFilter(filter)) {
customList->removeFilter(filter);
}
else {
AdBlockRule* rule = new AdBlockRule(filter, customList);
customList->addRule(rule);
}
const QString filter = action->data().toString();
AdBlockManager *manager = AdBlockManager::instance();
AdBlockCustomList *customList = manager->customList();
if (customList->containsFilter(filter)) {
customList->removeFilter(filter);
}
else {
AdBlockRule *rule = new AdBlockRule(filter, customList);
customList->addRule(rule);
}
}
void AdBlockIcon::animateIcon()
{
++m_timerTicks;
if (m_timerTicks > 10) {
stopAnimation();
return;
}
void AdBlockIcon::animateIcon() {
++m_timerTicks;
if (pixmap()->isNull()) {
setPixmap(QIcon(QSL(":icons/other/adblock.png")).pixmap(16));
}
else {
setPixmap(QPixmap());
}
if (m_timerTicks > 10) {
stopAnimation();
return;
}
if (pixmap()->isNull()) {
setPixmap(QIcon(QSL(":icons/other/adblock.png")).pixmap(16));
}
else {
setPixmap(QPixmap());
}
}
void AdBlockIcon::stopAnimation()
{
m_timerTicks = 0;
m_flashTimer->stop();
disconnect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon()));
void AdBlockIcon::stopAnimation() {
m_timerTicks = 0;
m_flashTimer->stop();
disconnect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon()));
setEnabled(m_enabled);
setEnabled(m_enabled);
}
void AdBlockIcon::setEnabled(bool enabled)
{
if (enabled) {
setPixmap(QIcon(QSL(":icons/other/adblock.png")).pixmap(16));
}
else {
setPixmap(QIcon(QSL(":icons/other/adblock-disabled.png")).pixmap(16));
}
void AdBlockIcon::setEnabled(bool enabled) {
if (enabled) {
setPixmap(QIcon(QSL(":icons/other/adblock.png")).pixmap(16));
}
else {
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
#define ADBLOCKICON_H
#include "qzcommon.h"
#include "clickablelabel.h"
#include "adblockrule.h"
#include "gui/clickablelabel.h"
#include "network-web/adblock/adblockrule.h"
class QMenu;
class QUrl;
class QAction;
class QTimer;
class BrowserWindow;
class QUPZILLA_EXPORT AdBlockIcon : public ClickableLabel
{
class AdBlockIcon : public ClickableLabel {
Q_OBJECT
public:
public:
explicit AdBlockIcon(BrowserWindow* window, QWidget* parent = 0);
~AdBlockIcon();
virtual ~AdBlockIcon();
void popupBlocked(const QString &ruleString, const QUrl &url);
QAction* menuAction();
QAction *menuAction();
public slots:
public slots:
void setEnabled(bool enabled);
void createMenu(QMenu* menu = 0);
void createMenu(QMenu *menu = 0);
private slots:
private slots:
void showMenu(const QPoint &pos);
void toggleCustomFilter();
void animateIcon();
void stopAnimation();
private:
BrowserWindow* m_window;
QAction* m_menuAction;
private:
BrowserWindow *m_window;
QAction *m_menuAction;
QVector<QPair<AdBlockRule*, QUrl> > m_blockedPopups;
QTimer* m_flashTimer;
QTimer *m_flashTimer;
int m_timerTicks;
bool m_enabled;