1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

Highlight host in locationbar.

To simplify color computation I simply imported the "blib/colors.*" from bespin.
This commit is contained in:
Franz Fellner 2012-09-19 20:24:41 +02:00
parent 8723382b5c
commit 4bb87822cf
4 changed files with 333 additions and 7 deletions

View File

@ -89,6 +89,7 @@ SOURCES += \
preferences/sslmanager.cpp \
tools/animatedwidget.cpp \
tools/htmlhighlighter.cpp \
tools/colors.cpp \
other/sourceviewersearch.cpp \
adblock/adblocksubscription.cpp \
adblock/adblockrule.cpp \

View File

@ -38,10 +38,12 @@
#include "globalfunctions.h"
#include "iconprovider.h"
#include "qzsettings.h"
#include "colors.h"
#include <QClipboard>
#include <QTimer>
#include <QContextMenuEvent>
#include <QDebug>
LocationBar::LocationBar(QupZilla* mainClass)
: LineEdit(mainClass)
@ -584,7 +586,7 @@ void LocationBar::hideProgress()
void LocationBar::paintEvent(QPaintEvent* event)
{
if (hasFocus()) {
if (hasFocus() || text().isEmpty()) {
LineEdit::paintEvent(event);
return;
}
@ -614,11 +616,9 @@ void LocationBar::paintEvent(QPaintEvent* event)
if (qzSettings->showLoadingProgress && m_progressVisible) {
QColor bg = m_progressColor;
if (!bg.isValid() || bg.alpha() == 0) {
bg = palette().color(QPalette::Base).darker(110);
if (!bg.isValid() || bg.alpha() == 0) {
bg = p_QupZilla->palette().color(QPalette::Base).darker(110);
}
bg = Colors::mid(palette().color(QPalette::Base),
palette().color(QPalette::Text),
10, 1);
}
p.setBrush(QBrush(bg));
@ -658,5 +658,37 @@ void LocationBar::paintEvent(QPaintEvent* event)
p.setPen(oldPen);
QTextOption opt;
opt.setWrapMode(QTextOption::NoWrap);
p.drawText(textRect, text(), opt);
const QString hostName = m_webView->url().host();
QString currentText = text();
QRect currentRect = textRect;
if (!hostName.isEmpty()) {
const int hostPos = currentText.indexOf(hostName);
if (hostPos > 0) {
QPen lightPen = oldPen;
QColor lightColor = Colors::mid(palette().color(QPalette::Base),
palette().color(QPalette::Text),
1, 1);
lightPen.setColor(lightColor);
p.setPen(lightPen);
currentText = text().mid(0, hostPos);
currentRect.setWidth(fm.width(currentText));
p.drawText(currentRect, currentText, opt);
p.setPen(oldPen);
currentRect.setX(currentRect.x() + currentRect.width());
const int hostWidth = fm.width(hostName);
currentRect.setWidth(hostWidth);
p.drawText(currentRect, hostName, opt);
p.setFont(font());
currentText = text().mid(hostPos + hostName.length());
currentRect.setX(currentRect.x() + hostWidth);
currentRect.setWidth(textRect.width() - currentRect.x() + textRect.x());
p.setPen(lightPen);
}
}
p.drawText(currentRect, currentText, opt);
}

248
src/lib/tools/colors.cpp Normal file
View File

@ -0,0 +1,248 @@
/*
* Bespin library for Qt style, KWin decoration and everythng else
* Copyright 2007-2012 by Thomas Lübking <thomas.luebking@gmail.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2
*
* This program 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 Library General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "colors.h"
#define CLAMP(x,l,u) (x) < (l) ? (l) :\
(x) > (u) ? (u) :\
(x)
#include <QWidget>
#include <QApplication>
// using namespace Bespin;
const QColor &
Colors::bg(const QPalette &pal, const QWidget* w)
{
QPalette::ColorRole role;
if (!w) {
role = QPalette::Window;
}
else if (w->parentWidget()) {
role = w->parentWidget()->backgroundRole();
}
else {
role = w->backgroundRole();
}
// if (pal.brush(role).style() > 1)
return pal.color(role);
// return QApplication::palette().color(role);
}
int
Colors::contrast(const QColor &a, const QColor &b)
{
int ar, ag, ab, br, bg, bb;
a.getRgb(&ar, &ag, &ab);
b.getRgb(&br, &bg, &bb);
int diff = 299 * (ar - br) + 587 * (ag - bg) + 114 * (ab - bb);
diff = (diff < 0) ? -diff : 90 * diff / 100;
int perc = diff / 2550;
diff = qMax(ar, br) + qMax(ag, bg) + qMax(ab, bb)
- (qMin(ar, br) + qMin(ag, bg) + qMin(ab, bb));
perc += diff / 765;
perc /= 2;
return perc;
}
QPalette::ColorRole
Colors::counterRole(QPalette::ColorRole role)
{
switch (role) {
case QPalette::ButtonText: //8
return QPalette::Button;
case QPalette::WindowText: //0
return QPalette::Window;
case QPalette::HighlightedText: //13
return QPalette::Highlight;
case QPalette::Window: //10
return QPalette::WindowText;
case QPalette::Base: //9
return QPalette::Text;
case QPalette::Text: //6
return QPalette::Base;
case QPalette::Highlight: //12
return QPalette::HighlightedText;
case QPalette::Button: //1
return QPalette::ButtonText;
default:
return QPalette::Window;
}
}
bool
Colors::counterRole(QPalette::ColorRole &from, QPalette::ColorRole &to, QPalette::ColorRole defFrom,
QPalette::ColorRole defTo)
{
switch (from) {
case QPalette::WindowText: //0
to = QPalette::Window;
break;
case QPalette::Window: //10
to = QPalette::WindowText;
break;
case QPalette::Base: //9
to = QPalette::Text;
break;
case QPalette::Text: //6
to = QPalette::Base;
break;
case QPalette::Button: //1
to = QPalette::ButtonText;
break;
case QPalette::ButtonText: //8
to = QPalette::Button;
break;
case QPalette::Highlight: //12
to = QPalette::HighlightedText;
break;
case QPalette::HighlightedText: //13
to = QPalette::Highlight;
break;
default:
from = defFrom;
to = defTo;
return false;
}
return true;
}
QColor
Colors::emphasize(const QColor &c, int value)
{
int h, s, v, a;
QColor ret;
c.getHsv(&h, &s, &v, &a);
if (v < 75 + value) {
ret.setHsv(h, s, CLAMP(85 + value, 85, 255), a);
return ret;
}
if (v > 200) {
if (s > 30) {
h -= 5;
if (h < 0) {
h = 360 + h;
}
s = (s << 3) / 9;
v += value;
ret.setHsv(h, CLAMP(s, 30, 255), CLAMP(v, 0, 255), a);
return ret;
}
if (v > 230) {
ret.setHsv(h, s, CLAMP(v - value, 0, 255), a);
return ret;
}
}
if (v > 128) {
ret.setHsv(h, s, CLAMP(v + value, 0, 255), a);
}
else {
ret.setHsv(h, s, CLAMP(v - value, 0, 255), a);
}
return ret;
}
bool
Colors::haveContrast(const QColor &a, const QColor &b)
{
int ar, ag, ab, br, bg, bb;
a.getRgb(&ar, &ag, &ab);
b.getRgb(&br, &bg, &bb);
int diff = (299 * (ar - br) + 587 * (ag - bg) + 114 * (ab - bb));
if (qAbs(diff) < 91001) {
return false;
}
diff = qMax(ar, br) + qMax(ag, bg) + qMax(ab, bb)
- (qMin(ar, br) + qMin(ag, bg) + qMin(ab, bb));
return (diff > 300);
}
QColor
Colors::light(const QColor &c, int value)
{
int h, s, v, a;
c.getHsv(&h, &s, &v, &a);
QColor ret;
if (v < 255 - value) {
ret.setHsv(h, s, CLAMP(v + value, 0, 255), a); //value could be negative
return ret;
}
// psychovisual uplightning, i.e. shift hue and lower saturation
if (s > 30) {
h -= (value * 5 / 20);
if (h < 0) {
h = 400 + h;
}
s = CLAMP((s << 3) / 9, 30, 255);
ret.setHsv(h, s, 255, a);
return ret;
}
else { // hue shifting has no sense, half saturation (btw, white won't get brighter :)
ret.setHsv(h, s >> 1, 255, a);
}
return ret;
}
QColor
Colors::mid(const QColor &c1, const QColor &c2, int w1, int w2)
{
int sum = (w1 + w2);
if (!sum) {
return Qt::black;
}
int r, g, b, a;
#if 0
QColor c1 = oc1;
b = value(c1);
if (b < 70) {
c1.getHsv(&r, &g, &b, &a);
c1.setHsv(r, g, 70, a);
}
#endif
r = (w1 * c1.red() + w2 * c2.red()) / sum;
r = CLAMP(r, 0, 255);
g = (w1 * c1.green() + w2 * c2.green()) / sum;
g = CLAMP(g, 0, 255);
b = (w1 * c1.blue() + w2 * c2.blue()) / sum;
b = CLAMP(b, 0, 255);
a = (w1 * c1.alpha() + w2 * c2.alpha()) / sum;
a = CLAMP(a, 0, 255);
return QColor(r, g, b, a);
}
int
Colors::value(const QColor &c)
{
int v = c.red();
if (c.green() > v) {
v = c.green();
}
if (c.blue() > v) {
v = c.blue();
}
return v;
}

45
src/lib/tools/colors.h Normal file
View File

@ -0,0 +1,45 @@
/*
* Bespin library for Qt style, KWin decoration and everythng else
* Copyright 2007-2012 by Thomas Lübking <thomas.luebking@gmail.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2
*
* This program 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 Library General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef COLORS_H
#define COLORS_H
class QWidget;
#include <QColor>
#include <QPalette>
// namespace Bespin {
namespace Colors
{
const QColor &bg(const QPalette &pal, const QWidget* w);
int contrast(const QColor &a, const QColor &b);
QPalette::ColorRole counterRole(QPalette::ColorRole role);
bool counterRole(QPalette::ColorRole &from, QPalette::ColorRole &to,
QPalette::ColorRole defFrom = QPalette::WindowText,
QPalette::ColorRole defTo = QPalette::Window);
QColor emphasize(const QColor &c, int value = 10);
bool haveContrast(const QColor &a, const QColor &b);
QColor light(const QColor &c, int value);
QColor mid(const QColor &oc1, const QColor &c2, int w1 = 1, int w2 = 1);
int value(const QColor &c);
}
// }
#endif //COLORS_H