Commit be969f45 authored by Gustavo Carneiro's avatar Gustavo Carneiro
Browse files

Add a new color filter to preview color in hotspot.

parent e6b4c1f7
......@@ -152,6 +152,8 @@ set(konsoleprivate_SRCS ${windowadaptors_SRCS}
filterHotSpots/TerminalImageFilterChain.cpp
filterHotSpots/UrlFilter.cpp
filterHotSpots/UrlFilterHotspot.cpp
filterHotSpots/ColorFilter.cpp
filterHotSpots/ColorFilterHotSpot.cpp
history/HistoryFile.cpp
history/HistoryScroll.cpp
history/HistoryScrollFile.cpp
......
/*
SPDX-FileCopyrightText: 2020-2020 Gustavo Carneiro <gcarneiroa@hotmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "ColorFilter.h"
using namespace Konsole;
#include "ColorFilterHotSpot.h"
const QRegularExpression ColorFilter::ColorRegExp(QStringLiteral("(#([[:xdigit:]]{12}|[[:xdigit:]]{9}|[[:xdigit:]]{6}))(?:[^[:alnum:]]|$)"));
ColorFilter::ColorFilter()
{
setRegExp(ColorRegExp);
}
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));
}
/*
SPDX-FileCopyrightText: 2020-2020 Gustavo Carneiro <gcarneiroa@hotmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef COLORFILTER_H
#define COLORFILTER_H
#include "RegExpFilter.h"
namespace Konsole
{
class ColorFilter : public RegExpFilter
{
public:
ColorFilter();
static const QRegularExpression ColorRegExp;
protected:
QSharedPointer<HotSpot> newHotSpot(int startLine, int startColumn, int endLine, int endColumn, const QStringList &capturedTexts) override;
};
}
#endif
/*
SPDX-FileCopyrightText: 2020-2020 Gustavo Carneiro <gcarneiroa@hotmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "ColorFilterHotSpot.h"
#include <QApplication>
#include <QTimer>
#include <QBuffer>
#include <QToolTip>
#include <QMouseEvent>
#include <QPixmap>
#include <QPainter>
#include "KonsoleSettings.h"
using namespace Konsole;
bool ColorFilterHotSpot::_canGenerateTooltip = false;
ColorFilterHotSpot::ColorFilterHotSpot(int startLine, int startColumn, int endLine, int endColumn, const QStringList &capturedTexts, const QString &colorName)
: RegExpFilterHotSpot(startLine, startColumn, endLine, endColumn, capturedTexts)
, _colorName(colorName)
{
setType(Color);
}
void ColorFilterHotSpot::mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev)
{
HotSpot::mouseEnterEvent(td, ev);
_toolPos = ev->globalPos();
_canGenerateTooltip = true;
QTimer::singleShot(100, this, [&]{
tooltipRequested();
});
}
void ColorFilterHotSpot::mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev)
{
HotSpot::mouseLeaveEvent(td, ev);
_canGenerateTooltip = false;
QToolTip::hideText();
}
void ColorFilterHotSpot::tooltipRequested()
{
if (!_canGenerateTooltip) {
return;
}
QColor toolColor(_colorName);
QPoint tooltipPosition = QPoint(_toolPos.x(), _toolPos.y());
QPixmap pix(100, 100);
QPainter paint(&pix);
paint.setPen(toolColor);
paint.drawRect(0, 0, 100, 100);
paint.fillRect(0, 0, 100, 100, toolColor);
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()));
QToolTip::showText(tooltipPosition, tooltipString, qApp->focusWidget());
}
/*
SPDX-FileCopyrightText: 2020-2020 Gustavo Carneiro <gcarneiroa@hotmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef COLORFILTERHOTSPOT_H
#define COLORFILTERHOTSPOT_H
#include "RegExpFilterHotspot.h"
#include <QPoint>
#include <QPointer>
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() = default;
void mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev) override;
void mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev) override;
private:
void tooltipRequested();
QString _colorName;
QPoint _toolPos;
static bool _canGenerateTooltip;
};
}
#endif
......@@ -60,6 +60,8 @@ public:
Marker,
// this spot represents a Escaped URL
EscapedUrl,
// this hotspot represents a color of text
Color,
};
/** Returns the line when the hotspot area starts */
......
......@@ -121,6 +121,7 @@ const Profile::PropertyInfo Profile::DefaultPropertyNames[] = {
, { AlternateScrolling, "AlternateScrolling", INTERACTION_GROUP, QVariant::Bool }
, { AllowEscapedLinks, "AllowEscapedLinks", INTERACTION_GROUP, QVariant::Bool }
, { EscapedLinksSchema, "EscapedLinksSchema", INTERACTION_GROUP, QVariant::String }
, { ColorFilterEnabled, "ColorFilterEnabled", INTERACTION_GROUP, QVariant::Bool }
// Encoding
, { DefaultEncoding , "DefaultEncoding" , ENCODING_GROUP , QVariant::String }
......@@ -204,6 +205,7 @@ void Profile::useFallback()
setProperty(PasteFromClipboardEnabled, false);
setProperty(MiddleClickPasteMode, Enum::PasteFromX11Selection);
setProperty(TripleClickMode, Enum::SelectWholeLine);
setProperty(ColorFilterEnabled, true);
setProperty(BlinkingCursorEnabled, false);
setProperty(BidiRenderingEnabled, true);
......
......@@ -323,7 +323,12 @@ public:
VerticalLineAtChar,
/** Shortcut for peeking primary screen */
PeekPrimaryKeySequence
PeekPrimaryKeySequence,
/** (bool) If true, text that matches a color in hex format
* when hovered by the mouse pointer.
*/
ColorFilterEnabled
};
Q_ENUM(Property)
......@@ -690,6 +695,12 @@ public:
return property<int>(Profile::VerticalLineAtChar);
}
/** Convenience method for property<bool>(Profile::colorFilterEnabled) */
bool colorFilterEnabled() const
{
return property<bool>(Profile::ColorFilterEnabled);
}
QKeySequence peekPrimaryKeySequence() const
{
return QKeySequence::fromString(property<QString>(Profile::PeekPrimaryKeySequence));
......
......@@ -71,6 +71,7 @@
#include "filterHotSpots/HotSpot.h"
#include "filterHotSpots/RegExpFilter.h"
#include "filterHotSpots/UrlFilter.h"
#include "filterHotSpots/ColorFilter.h"
#include "history/HistoryType.h"
#include "history/HistoryTypeFile.h"
......@@ -105,6 +106,7 @@ SessionController::SessionController(Session* session, TerminalDisplay* view, QO
, _searchFilter(nullptr)
, _urlFilter(nullptr)
, _fileFilter(nullptr)
, _colorFilter(nullptr)
, _copyInputToAllTabsAction(nullptr)
, _findAction(nullptr)
, _findNextAction(nullptr)
......@@ -1299,6 +1301,16 @@ void SessionController::updateFilterList(Profile::Ptr profile)
_escapedUrlFilter = new EscapeSequenceUrlFilter(_sessionDisplayConnection->session(), _sessionDisplayConnection->view());
filterChain->addFilter(_escapedUrlFilter);
}
const bool allowColorFilters = profile->colorFilterEnabled();
if (!allowColorFilters && (_colorFilter != nullptr)) {
filterChain->removeFilter(_colorFilter);
delete _colorFilter;
_colorFilter = nullptr;
} else if (allowColorFilters && (_colorFilter == nullptr)) {
_colorFilter = new ColorFilter();
filterChain->addFilter(_colorFilter);
}
}
void SessionController::setSearchStartToWindowCurrentLine()
......
......@@ -46,6 +46,7 @@ class Session;
class SessionGroup;
class TerminalDisplay;
class UrlFilter;
class ColorFilter;
/**
* Provides the menu actions to manipulate a single terminal session and view pair.
......@@ -316,6 +317,7 @@ private:
RegExpFilter *_searchFilter;
UrlFilter *_urlFilter;
FileFilter *_fileFilter;
ColorFilter *_colorFilter;
QAction *_copyInputToAllTabsAction;
......
......@@ -899,6 +899,11 @@ void EditProfileDialog::toggleAlternateScrolling(bool enable)
updateTempProfileProperty(Profile::AlternateScrolling, enable);
}
void EditProfileDialog::toggleAllowColorFilter(bool enable)
{
updateTempProfileProperty(Profile::ColorFilterEnabled, enable);
}
void EditProfileDialog::updateColorSchemeList(const QString &selectedColorSchemeName)
{
if (_appearanceUi->colorSchemeList->model() == nullptr) {
......@@ -1661,6 +1666,8 @@ void EditProfileDialog::setupMousePage(const Profile::Ptr &profile)
connect(_mouseUi->dropUrlsAsText, &QPushButton::toggled, this, &EditProfileDialog::toggleDropUrlsAsText);
_mouseUi->enableAlternateScrollingButton->setChecked(profile->property<bool>(Profile::AlternateScrolling));
connect(_mouseUi->enableAlternateScrollingButton, &QPushButton::toggled, this, &EditProfileDialog::toggleAlternateScrolling);
_mouseUi->allowColorFilters->setChecked(profile->property<bool>(Profile::ColorFilterEnabled));
connect(_mouseUi->allowColorFilters, &QPushButton::toggled, this, &EditProfileDialog::toggleAllowColorFilter);
// setup middle click paste mode
const auto middleClickPasteMode = profile->property<int>(Profile::MiddleClickPasteMode);
......
......@@ -186,6 +186,7 @@ private Q_SLOTS:
void pasteFromX11Selection();
void pasteFromClipboard();
void toggleAlternateScrolling(bool enable);
void toggleAllowColorFilter(bool enable);
void TripleClickModeChanged(int);
void wordCharactersChanged(const QString &);
......
......@@ -340,9 +340,6 @@
<property name="text">
<string>&lt;b&gt;WARNING&lt;/b&gt;: This has security implications as it allows malicious URLs to be shown as another URL or hidden.&lt;br&gt;Make sure you understand the implications before turning this on.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
......@@ -417,6 +414,16 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="allowColorFilters">
<property name="toolTip">
<string>When positioning the mouse over a hexadecimal color it will be displayed in a frame next to the mouse pointer</string>
</property>
<property name="text">
<string>Allow Color Filters</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
......@@ -459,7 +466,7 @@
</connection>
</connections>
<buttongroups>
<buttongroup name="pasteFrom"/>
<buttongroup name="tripleClickMode"/>
<buttongroup name="pasteFrom"/>
</buttongroups>
</ui>
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