Commit 86303f57 authored by Ahmad Samir's avatar Ahmad Samir Committed by Kurt Hindenburg

HotSpot: move input events implementation to the base class

Then if one of the classes that inherit HotSpot need to reimplement an
input event, it can do so. I think this way we get less code duplication,
and if we need to change those methods, there is a chance only changing the
base implementation is necessary.

This also fixes file filter.

Initialise  member variables in FilterChain ctor, particularly initialising
_showUrlHint prevents an issue where url hints are displayed when moving
the mouse for the first time in a newly opened konsole window even if no
url hint modifiers are pressed.

In keyPressEvent it seems calling update() on the TerminalDisplay isn't
needed.
parent 756c3806
......@@ -49,19 +49,3 @@ void EscapeSequenceUrlHotSpot::activate(QObject *obj)
new KRun(QUrl(_url), QApplication::activeWindow());
}
void EscapeSequenceUrlHotSpot::mouseEnterEvent(TerminalDisplay* td, QMouseEvent* ev)
{
auto cursor = td->cursor();
auto r = region(td->fontWidth(), td->fontHeight(), td->columns(), td->contentRect()).first;
td->setCursor(Qt::PointingHandCursor);
td->update(r);
}
void EscapeSequenceUrlHotSpot::mouseReleaseEvent(TerminalDisplay *td, QMouseEvent *ev)
{
if ((td->openLinksByDirectClick() || ((ev->modifiers() & Qt::ControlModifier) != 0u))) {
activate(nullptr);
}
}
......@@ -32,8 +32,6 @@ public:
const QString &text, const QString &url);
void activate(QObject *obj) override;
void mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev) override;
void mouseReleaseEvent(TerminalDisplay *td, QMouseEvent *ev) override;
private:
QString _text;
QString _url;
......
......@@ -192,9 +192,9 @@ KFileItem FileFilterHotSpot::fileItem() const
return KFileItem(QUrl::fromLocalFile(_filePath));
}
void FileFilterHotSpot::mouseMoveEvent(TerminalDisplay *td, QMouseEvent* ev)
void FileFilterHotSpot::mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev)
{
HotSpot::mouseMoveEvent(td, ev);
HotSpot::mouseEnterEvent(td, ev);
requestThumbnail(ev->modifiers(), ev->globalPos());
}
......@@ -206,5 +206,6 @@ void FileFilterHotSpot::mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev)
void Konsole::FileFilterHotSpot::keyPressEvent(Konsole::TerminalDisplay* td, QKeyEvent* ev)
{
HotSpot::keyPressEvent(td, ev);
requestThumbnail(ev->modifiers(), QCursor::pos());
}
......@@ -63,8 +63,9 @@ public:
static void stopThumbnailGeneration();
void mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev) override;
void mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev) override;
void mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev) override;
void keyPressEvent(TerminalDisplay *td, QKeyEvent *ev) override;
private:
......
......@@ -34,7 +34,10 @@
using namespace Konsole;
FilterChain::FilterChain(TerminalDisplay *terminalDisplay)
: _terminalDisplay(terminalDisplay)
: _terminalDisplay(terminalDisplay),
_showUrlHint(false),
_reverseUrlHints(false),
_urlHintsModifiers(Qt::NoModifier)
{
}
......
......@@ -114,15 +114,82 @@ QPair<QRegion, QRect> HotSpot::region(int fontWidth, int fontHeight, int columns
return {region, r};
}
void HotSpot::mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev)
{
Q_UNUSED(td);
Q_UNUSED(ev);
}
bool HotSpot::isUrl()
{
return (_type == HotSpot::Link || _type == HotSpot::EMailAddress || _type == HotSpot::EscapedUrl);
}
void HotSpot::mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev)
{
if (!isUrl()) {
return;
}
if (td->cursor().shape() != Qt::PointingHandCursor
&& ((td->openLinksByDirectClick() || ((ev->modifiers() & Qt::ControlModifier) != 0u)))) {
td->setCursor(Qt::PointingHandCursor);
}
auto r = region(td->fontWidth(), td->fontHeight(), td->columns(), td->contentRect()).first;
td->update(r);
}
void HotSpot::mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev)
{
Q_UNUSED(ev);
if (!isUrl()) {
return;
}
auto r = region(td->fontWidth(), td->fontHeight(), td->columns(), td->contentRect()).first;
td->update(r);
td->resetCursor();
}
void HotSpot::mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev)
void HotSpot::mouseReleaseEvent(TerminalDisplay *td, QMouseEvent *ev)
{
if (!isUrl()) {
return;
}
if ((td->openLinksByDirectClick() || ((ev->modifiers() & Qt::ControlModifier) != 0u))) {
activate(nullptr);
}
}
void HotSpot::keyPressEvent(TerminalDisplay* td, QKeyEvent *ev)
{
if (!isUrl()) {
return;
}
// If td->openLinksByDirectClick() is true, the shape has already been changed by the
// mouseEnterEvent
if (td->cursor().shape() != Qt::PointingHandCursor && (ev->modifiers() & Qt::ControlModifier) != 0u) {
td->setCursor(Qt::PointingHandCursor);
}
}
void HotSpot::keyReleaseEvent(TerminalDisplay *td, QKeyEvent *ev)
{
Q_UNUSED(ev);
if (!isUrl()) {
return;
}
if (td->openLinksByDirectClick()) {
return;
}
td->resetCursor();
}
void HotSpot::debug() {
......
......@@ -110,14 +110,55 @@ public:
QPair<QRegion, QRect> region(int fontWidth, int fontHeight, int columns, QRect terminalDisplayRect) const;
virtual void mouseMoveEvent(TerminalDisplay *, QMouseEvent *);
virtual void mouseLeaveEvent(TerminalDisplay *, QMouseEvent *);
virtual void mouseEnterEvent(TerminalDisplay *, QMouseEvent *) {};
virtual void mouseReleaseEvent(TerminalDisplay *, QMouseEvent *){};
virtual void keyPressEvent(TerminalDisplay *, QKeyEvent *) {};
virtual void keyReleaseEvent(TerminalDisplay *, QKeyEvent *) {};
/**
* Returns true if the type of the HotSpot is Link, EMailAddress, or EscapedUrl, (see
* Type enum), otherwise returns false; mainly used in the input events, e.g. to not
* change the shape of the mouse pointer to a pointing hand if the HotSpot doesn't
* represent a clickable URI.
*/
bool isUrl();
/** The base implementation does nothing */
virtual void mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev);
/**
* The mouse pointer shape is changed to a pointing hand; also because the underline
* is painted under the link, update() is called on the TerminalDisplay widget.
*/
virtual void mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev);
/**
* The mouse pointer is reset to the default shape, see TerminalDisplay::resetCursor();
* also because the underline is hidden from under the link, update() is called on the
* TerminalDisplay widget.
*/
virtual void mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev);
/**
* If the Ctrl key is pressed or TerminalDisplay::openLinksByDirectClick() is
* true, the activate() method is called to handle/open the link, see activate().
*/
virtual void mouseReleaseEvent(TerminalDisplay *td, QMouseEvent *ev);
/**
* If the Ctrl key is pressed, the mouse pointer shape is changed to a pointing hand.
*
* Note that if TerminalDisplay::openLinksByDirectClick() is true the mouse pointer shape is always
* changed to a pointing hand when hovering over a link, regardless of the state of the Ctrl key.
*/
virtual void keyPressEvent(TerminalDisplay *td, QKeyEvent *ev);
/**
* This resets the mouse pointer to the default shape (if e.g. the Ctrl key had been pressed
* and now has been released).
*
* Note that if TerminalDisplay::openLinksByDirectClick() is true the mouse pointer shape is always
* changed to a pointing hand when hovering over a link, regardless of the state of the Ctrl key.
*/
virtual void keyReleaseEvent(TerminalDisplay *, QKeyEvent *ev);
void debug();
protected:
/** Sets the type of a hotspot. This should only be set once */
void setType(Type type);
......
......@@ -126,46 +126,3 @@ QList<QAction *> UrlFilterHotSpot::actions()
return {openAction, copyAction};
}
template<typename Event>
void setTerminalCursor(const QRegion &r, TerminalDisplay *td, Event *ev)
{
QCursor cursor = td->cursor();
if (cursor.shape() == Qt::PointingHandCursor) {
return;
}
if ((td->openLinksByDirectClick() || ((ev->modifiers() & Qt::ControlModifier) != 0u))) {
td->setCursor(Qt::PointingHandCursor);
}
td->update(r);
}
void UrlFilterHotSpot::mouseEnterEvent(TerminalDisplay* td, QMouseEvent* ev)
{
QRegion r = region(td->fontWidth(), td->fontHeight(), td->columns(), td->contentRect()).first;
setTerminalCursor(r, td, ev);
}
void UrlFilterHotSpot::mouseReleaseEvent(TerminalDisplay *td, QMouseEvent *ev)
{
if ((td->openLinksByDirectClick() || ((ev->modifiers() & Qt::ControlModifier) != 0u))) {
activate(nullptr);
}
}
void UrlFilterHotSpot::keyPressEvent(TerminalDisplay* td, QKeyEvent* ev)
{
QRegion r = region(td->fontWidth(), td->fontHeight(), td->columns(), td->contentRect()).first;
setTerminalCursor(r, td, ev);
}
void UrlFilterHotSpot::keyReleaseEvent(TerminalDisplay* td, QKeyEvent*)
{
if (td->openLinksByDirectClick()) {
return;
}
td->resetCursor();
}
......@@ -45,10 +45,6 @@ public:
* the capturedTexts() method.
*/
void activate(QObject *object = nullptr) override;
void mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev) override;
void mouseReleaseEvent(TerminalDisplay *td, QMouseEvent *ev) override;
void keyPressEvent(TerminalDisplay *td, QKeyEvent *ev) override;
void keyReleaseEvent(TerminalDisplay *td, QKeyEvent *ev) override;
private:
enum UrlType {
......
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