Commit 78394332 authored by Edwin Pujols's avatar Edwin Pujols Committed by Tomaz Canabrava
Browse files

filters: Match all colors recognized by QColor.

Allow `ColorFilter` to match all color names recognized by `QColor` (as
per the docs on `QColor::setNamedColor`).
parent ea51d6da
......@@ -10,15 +10,32 @@ using namespace Konsole;
#include "ColorFilterHotSpot.h"
const QRegularExpression ColorFilter::ColorRegExp(QStringLiteral("(#([[:xdigit:]]{12}|[[:xdigit:]]{9}|[[:xdigit:]]{6}))(?:[^[:alnum:]]|$)"));
ColorFilter::ColorFilter()
// This matches:
// - an RGB-style string (e.g., #3e3, #feed) delimited by non-alphanumerics;
// - or, a sequence of ASCII characters (e.g., foobar, Aquamarine, TOMATO).
// See the docs for `QColor::setNamedColor`.
const QRegularExpression ColorFilter::ColorRegExp(
QStringLiteral("((?<![[:alnum:]])"
"#[[:xdigit:]]{3,12}"
"(?![[:alnum:]])|"
"\\b[a-zA-Z]{3,20}\\b)"));
ColorFilter::ColorFilter()
{
setRegExp(ColorRegExp);
}
QSharedPointer<HotSpot> ColorFilter::newHotSpot(int startLine, int startColumn, int endLine, int endColumn, const QStringList &capturedTexts)
QSharedPointer<HotSpot> ColorFilter::newHotSpot(int startLine, int startColumn, int endLine,
int endColumn, const QStringList &capturedTexts)
{
QString colorName = capturedTexts.at(1);
return QSharedPointer<HotSpot>(new ColorFilterHotSpot(startLine, startColumn, endLine, endColumn, capturedTexts, colorName));
QColor color(capturedTexts.at(1));
// Make sure we've actually matched a color.
if (!color.isValid()) {
return nullptr;
}
return QSharedPointer<HotSpot>(
new ColorFilterHotSpot(startLine, startColumn, endLine, endColumn, capturedTexts, color));
}
......@@ -17,8 +17,10 @@ namespace Konsole
ColorFilter();
static const QRegularExpression ColorRegExp;
protected:
QSharedPointer<HotSpot> newHotSpot(int startLine, int startColumn, int endLine, int endColumn, const QStringList &capturedTexts) override;
QSharedPointer<HotSpot> newHotSpot(int startLine, int startColumn, int endLine,
int endColumn, const QStringList &capturedTexts) override;
};
}
......
......@@ -20,9 +20,10 @@ using namespace Konsole;
bool ColorFilterHotSpot::_canGenerateTooltip = false;
ColorFilterHotSpot::ColorFilterHotSpot(int startLine, int startColumn, int endLine, int endColumn, const QStringList &capturedTexts, const QString &colorName)
ColorFilterHotSpot::ColorFilterHotSpot(int startLine, int startColumn, int endLine, int endColumn,
const QStringList &capturedTexts, const QColor &color)
: RegExpFilterHotSpot(startLine, startColumn, endLine, endColumn, capturedTexts)
, _colorName(colorName)
, _color(color)
{
setType(Color);
}
......@@ -38,7 +39,7 @@ void ColorFilterHotSpot::mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev)
});
}
void ColorFilterHotSpot::mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev)
void ColorFilterHotSpot::mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev)
{
HotSpot::mouseLeaveEvent(td, ev);
_canGenerateTooltip = false;
......@@ -50,21 +51,37 @@ void ColorFilterHotSpot::tooltipRequested()
if (!_canGenerateTooltip) {
return;
}
QColor toolColor(_colorName);
QPoint tooltipPosition = QPoint(_toolPos.x(), _toolPos.y());
QPixmap pix(100, 100);
Q_ASSERT(_color.isValid());
int sideUnit = 10;
int sideLength = sideUnit * sideUnit;
QRect square(0, 0, sideLength, sideLength);
QPixmap pix(square.size());
QPainter paint(&pix);
paint.setPen(toolColor);
paint.drawRect(0, 0, 100, 100);
paint.fillRect(0, 0, 100, 100, toolColor);
// Add a nice checkerboard pattern for translucent colors.
if (_color.alpha() < 255) {
// Base background
paint.fillRect(square, Qt::lightGray);
// Checkerboard pattern
QBrush brush(Qt::white, Qt::Dense4Pattern);
brush.setTransform(QTransform::fromScale(sideUnit, sideUnit));
paint.setBrush(brush);
paint.drawRect(square);
}
paint.fillRect(square, _color);
QByteArray data;
QBuffer buffer(&data);
pix.save(&buffer, "PNG");
const auto tooltipString = QStringLiteral("<img src='data:image/png;base64, %0'>")
.arg(QString::fromLocal8Bit(data.toBase64()));
QPoint tooltipPosition = QPoint(_toolPos.x(), _toolPos.y());
QToolTip::showText(tooltipPosition, tooltipString, qApp->focusWidget());
}
......@@ -11,24 +11,24 @@
#include <QPoint>
#include <QPointer>
#include <QColor>
class QAction;
namespace Konsole
{
class ColorFilterHotSpot : public RegExpFilterHotSpot
{
public:
ColorFilterHotSpot(int startLine, int startColumn, int endLine, int endColumn, const QStringList &capturedTexts, const QString &colorName);
ColorFilterHotSpot(int startLine, int startColumn, int endLine, int endColumn, const QStringList &capturedTexts, const QColor &color);
~ColorFilterHotSpot() override = default;
void mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev) override;
void mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev) override;
private:
void tooltipRequested();
QString _colorName;
QColor _color;
QPoint _toolPos;
static bool _canGenerateTooltip;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment