Commit 69865507 authored by Tomaz  Canabrava's avatar Tomaz Canabrava
Browse files

Move mouseMoveEvent handling to hotSpot

parent 83de7c45
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <QMenu> #include <QMenu>
#include <QTimer> #include <QTimer>
#include <QToolTip> #include <QToolTip>
#include <QMouseEvent>
#include <KRun> #include <KRun>
#include <KLocalizedString> #include <KLocalizedString>
...@@ -34,6 +35,7 @@ ...@@ -34,6 +35,7 @@
#include "konsoledebug.h" #include "konsoledebug.h"
#include "KonsoleSettings.h" #include "KonsoleSettings.h"
#include "widgets/TerminalDisplay.h"
using namespace Konsole; using namespace Konsole;
...@@ -194,3 +196,12 @@ KFileItem FileFilterHotSpot::fileItem() const ...@@ -194,3 +196,12 @@ KFileItem FileFilterHotSpot::fileItem() const
{ {
return KFileItem(QUrl::fromLocalFile(_filePath)); return KFileItem(QUrl::fromLocalFile(_filePath));
} }
void FileFilterHotSpot::mouseMoveEvent(TerminalDisplay *td, QMouseEvent* ev)
{
HotSpot::mouseMoveEvent(td, ev);
if (this != HotSpot::currentlyHoveredHotSpot) {
HotSpot::currentlyHoveredHotSpot.reset(this);
requestThumbnail(ev->modifiers(), ev->globalPos());
}
}
...@@ -35,10 +35,11 @@ class QAction; ...@@ -35,10 +35,11 @@ class QAction;
class QPixmap; class QPixmap;
namespace Konsole { namespace Konsole {
class TerminalDisplay;
/** /**
* Hotspot type created by FileFilter instances. * Hotspot type created by FileFilter instances.
*/ */
class FileFilterHotSpot : public RegExpFilterHotSpot class FileFilterHotSpot : public RegExpFilterHotSpot
{ {
public: public:
...@@ -59,6 +60,8 @@ public: ...@@ -59,6 +60,8 @@ public:
void thumbnailRequested(); void thumbnailRequested();
static void stopThumbnailGeneration(); static void stopThumbnailGeneration();
void mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev);
private: private:
void showThumbnail(const KFileItem& item, const QPixmap& preview); void showThumbnail(const KFileItem& item, const QPixmap& preview);
QString _filePath; QString _filePath;
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
#include "widgets/TerminalDisplay.h" #include "widgets/TerminalDisplay.h"
#include <QRect> #include <QRect>
#include <QEvent>
#include <algorithm> #include <algorithm>
using namespace Konsole; using namespace Konsole;
...@@ -135,3 +135,13 @@ int FilterChain::count(HotSpot::Type type) ...@@ -135,3 +135,13 @@ int FilterChain::count(HotSpot::Type type)
return s->type() == type; return s->type() == type;
}); });
} }
void FilterChain::leaveEvent(TerminalDisplay *td, QEvent *ev)
{
Q_UNUSED(ev);
if(!HotSpot::mouseOverHotSpotArea.isEmpty()) {
td->update(HotSpot::mouseOverHotSpotArea);
HotSpot::mouseOverHotSpotArea = QRegion();
td->setCursor(Qt::IBeamCursor);
}
}
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
#include "HotSpot.h" #include "HotSpot.h"
class QLeaveEvent;
namespace Konsole namespace Konsole
{ {
class Filter; class Filter;
...@@ -84,6 +86,8 @@ public: ...@@ -84,6 +86,8 @@ public:
/* Returns the amount of hotspots of the given type */ /* Returns the amount of hotspots of the given type */
int count(HotSpot::Type type); int count(HotSpot::Type type);
void leaveEvent(TerminalDisplay *td, QEvent *ev);
protected: protected:
QList<Filter *> _filters; QList<Filter *> _filters;
TerminalDisplay *_terminalDisplay; TerminalDisplay *_terminalDisplay;
......
...@@ -20,8 +20,16 @@ ...@@ -20,8 +20,16 @@
#include "HotSpot.h" #include "HotSpot.h"
#include <QMouseEvent>
#include "widgets/TerminalDisplay.h"
#include "FileFilterHotspot.h"
using namespace Konsole; using namespace Konsole;
QSharedPointer<HotSpot> HotSpot::currentlyHoveredHotSpot;
QRegion HotSpot::mouseOverHotSpotArea;
HotSpot::~HotSpot() = default; HotSpot::~HotSpot() = default;
HotSpot::HotSpot(int startLine, int startColumn, int endLine, int endColumn) : HotSpot::HotSpot(int startLine, int startColumn, int endLine, int endColumn) :
...@@ -107,3 +115,34 @@ QPair<QRegion, QRect> HotSpot::region(int fontWidth, int fontHeight, int columns ...@@ -107,3 +115,34 @@ QPair<QRegion, QRect> HotSpot::region(int fontWidth, int fontHeight, int columns
} }
return {region, r}; return {region, r};
} }
void HotSpot::mouseMoveEvent(TerminalDisplay *td, QMouseEvent* ev)
{
//TODO: Move this to HotSpot::mouseMoveEvent
QCursor cursor = td->cursor();
if ((type() == HotSpot::Link || type() == HotSpot::EMailAddress || type() == HotSpot::EscapedUrl)) {
QRegion previousHotspotArea = _mouseOverHotspotArea;
_mouseOverHotspotArea = region(td->fontWidth(), td->fontHeight(), td->columns(), td->contentRect()).first;
if ((td->openLinksByDirectClick()|| ((ev->modifiers() & Qt::ControlModifier) != 0u)) && (cursor.shape() != Qt::PointingHandCursor)) {
td->setCursor(Qt::PointingHandCursor);
}
td->update(_mouseOverHotspotArea | previousHotspotArea);
} else if (!_mouseOverHotspotArea.isEmpty()) {
if ((td->openLinksByDirectClick() || ((ev->modifiers() & Qt::ControlModifier) != 0u)) || (cursor.shape() == Qt::PointingHandCursor)) {
td->setCursor(td->usesMouseTracking() ? Qt::ArrowCursor : Qt::IBeamCursor);
}
td->update(_mouseOverHotspotArea);
// set hotspot area to an invalid rectangle
_mouseOverHotspotArea = QRegion();
// TODO: We need to move this to a `mouseLeaveEvent`
// HACK: Special case, if we move away from a FileFilterHotSpot, we need to stop the thumbnail.
FileFilterHotSpot::stopThumbnailGeneration();
HotSpot::currentlyHoveredHotSpot.clear();
}
}
...@@ -26,11 +26,15 @@ ...@@ -26,11 +26,15 @@
#include <QList> #include <QList>
#include <QRegion> #include <QRegion>
#include <QRect> #include <QRect>
#include <QSharedPointer>
class QAction; class QAction;
class QMenu; class QMenu;
class QMouseEvent;
namespace Konsole { namespace Konsole {
class TerminalDisplay;
/** /**
* Represents an area of text which matched the pattern a particular filter has been looking for. * Represents an area of text which matched the pattern a particular filter has been looking for.
* *
...@@ -57,6 +61,8 @@ public: ...@@ -57,6 +61,8 @@ public:
HotSpot(int startLine, int startColumn, int endLine, int endColumn); HotSpot(int startLine, int startColumn, int endLine, int endColumn);
virtual ~HotSpot(); virtual ~HotSpot();
static QSharedPointer<HotSpot> currentlyHoveredHotSpot;
static QRegion mouseOverHotSpotArea;
enum Type { enum Type {
// the type of the hotspot is not specified // the type of the hotspot is not specified
NotSpecified, NotSpecified,
...@@ -104,6 +110,9 @@ public: ...@@ -104,6 +110,9 @@ public:
virtual void setupMenu(QMenu *menu); virtual void setupMenu(QMenu *menu);
QPair<QRegion, QRect> region(int fontWidth, int fontHeight, int columns, QRect terminalDisplayRect) const; QPair<QRegion, QRect> region(int fontWidth, int fontHeight, int columns, QRect terminalDisplayRect) const;
void mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev);
protected: protected:
/** Sets the type of a hotspot. This should only be set once */ /** Sets the type of a hotspot. This should only be set once */
void setType(Type type); void setType(Type type);
...@@ -114,6 +123,7 @@ private: ...@@ -114,6 +123,7 @@ private:
int _endLine; int _endLine;
int _endColumn; int _endColumn;
Type _type; Type _type;
QRegion _mouseOverHotspotArea;
}; };
} }
......
...@@ -498,7 +498,6 @@ TerminalDisplay::TerminalDisplay(QWidget* parent) ...@@ -498,7 +498,6 @@ TerminalDisplay::TerminalDisplay(QWidget* parent)
, _blendColor(qRgba(0, 0, 0, 0xff)) , _blendColor(qRgba(0, 0, 0, 0xff))
, _wallpaper(nullptr) , _wallpaper(nullptr)
, _filterChain(new TerminalImageFilterChain(this)) , _filterChain(new TerminalImageFilterChain(this))
, _mouseOverHotspotArea(QRegion())
, _filterUpdateRequired(true) , _filterUpdateRequired(true)
, _cursorShape(Enum::BlockCursor) , _cursorShape(Enum::BlockCursor)
, _cursorColor(QColor()) , _cursorColor(QColor())
...@@ -2358,35 +2357,10 @@ void TerminalDisplay::mouseMoveEvent(QMouseEvent* ev) ...@@ -2358,35 +2357,10 @@ void TerminalDisplay::mouseMoveEvent(QMouseEvent* ev)
processFilters(); processFilters();
// handle filters // handle filters
// change link hot-spot appearance on mouse-over // change link hot-spot appearance on mouse-over
// TODO: Move this to filterChain::mouseMoveEvent
auto spot = _filterChain->hotSpotAt(charLine, charColumn); auto spot = _filterChain->hotSpotAt(charLine, charColumn);
if ((spot != nullptr) && (spot->type() == HotSpot::Link || spot->type() == HotSpot::EMailAddress || spot->type() == HotSpot::EscapedUrl)) { if (spot) {
QRegion previousHotspotArea = _mouseOverHotspotArea; spot->mouseMoveEvent(this, ev);
_mouseOverHotspotArea = spot->region(_fontWidth, _fontHeight, _columns, _contentRect).first;
if ((_openLinksByDirectClick || ((ev->modifiers() & Qt::ControlModifier) != 0u)) && (cursor().shape() != Qt::PointingHandCursor)) {
setCursor(Qt::PointingHandCursor);
}
/* can't use qobject_cast because moc is broken for inner classes */
auto fileSpot = spot.dynamicCast<FileFilterHotSpot>();
if (fileSpot != _currentlyHoveredHotspot) {
_currentlyHoveredHotspot = fileSpot;
if (fileSpot != nullptr) {
fileSpot->requestThumbnail(ev->modifiers(), ev->globalPos());
}
}
update(_mouseOverHotspotArea | previousHotspotArea);
} else if (!_mouseOverHotspotArea.isEmpty()) {
if ((_openLinksByDirectClick || ((ev->modifiers() & Qt::ControlModifier) != 0u)) || (cursor().shape() == Qt::PointingHandCursor)) {
setCursor(_usesMouseTracking ? Qt::ArrowCursor : Qt::IBeamCursor);
}
update(_mouseOverHotspotArea);
// set hotspot area to an invalid rectangle
_mouseOverHotspotArea = QRegion();
FileFilterHotSpot::stopThumbnailGeneration();
_currentlyHoveredHotspot.clear();
} }
// for auto-hiding the cursor, we need mouseTracking // for auto-hiding the cursor, we need mouseTracking
...@@ -2448,15 +2422,11 @@ void TerminalDisplay::mouseMoveEvent(QMouseEvent* ev) ...@@ -2448,15 +2422,11 @@ void TerminalDisplay::mouseMoveEvent(QMouseEvent* ev)
extendSelection(ev->pos()); extendSelection(ev->pos());
} }
void TerminalDisplay::leaveEvent(QEvent *) void TerminalDisplay::leaveEvent(QEvent *ev)
{ {
// remove underline from an active link when cursor leaves the widget area, // remove underline from an active link when cursor leaves the widget area,
// also restore regular mouse cursor shape // also restore regular mouse cursor shape
if(!_mouseOverHotspotArea.isEmpty()) { _filterChain->leaveEvent(this, ev);
update(_mouseOverHotspotArea);
_mouseOverHotspotArea = QRegion();
setCursor(Qt::IBeamCursor);
}
} }
void TerminalDisplay::extendSelection(const QPoint& position) void TerminalDisplay::extendSelection(const QPoint& position)
...@@ -3665,8 +3635,9 @@ void TerminalDisplay::keyPressEvent(QKeyEvent* event) ...@@ -3665,8 +3635,9 @@ void TerminalDisplay::keyPressEvent(QKeyEvent* event)
} }
} }
if (_currentlyHoveredHotspot != nullptr) { // TODO: Move this to hotSpot::keyPressEvent.
auto fileHotspot = _currentlyHoveredHotspot.dynamicCast<FileFilterHotSpot>(); if (HotSpot::currentlyHoveredHotSpot != nullptr) {
auto fileHotspot = HotSpot::currentlyHoveredHotSpot.dynamicCast<FileFilterHotSpot>();
if (!fileHotspot) { if (!fileHotspot) {
return; return;
} }
......
...@@ -322,6 +322,9 @@ public: ...@@ -322,6 +322,9 @@ public:
/** Reset the font size */ /** Reset the font size */
void resetFontSize(); void resetFontSize();
QRect contentRect() const { return _contentRect; }
bool openLinksByDirectClick() const { return _openLinksByDirectClick; }
/** /**
* Sets the terminal screen section which is displayed in this widget. * Sets the terminal screen section which is displayed in this widget.
* When updateImage() is called, the display fetches the latest character image from the * When updateImage() is called, the display fetches the latest character image from the
...@@ -844,7 +847,6 @@ private: ...@@ -844,7 +847,6 @@ private:
// list of filters currently applied to the display. used for links and // list of filters currently applied to the display. used for links and
// search highlight // search highlight
TerminalImageFilterChain *_filterChain; TerminalImageFilterChain *_filterChain;
QRegion _mouseOverHotspotArea;
bool _filterUpdateRequired; bool _filterUpdateRequired;
Enum::CursorShapeEnum _cursorShape; Enum::CursorShapeEnum _cursorShape;
...@@ -914,8 +916,6 @@ private: ...@@ -914,8 +916,6 @@ private:
bool _hasCompositeFocus; bool _hasCompositeFocus;
bool _displayVerticalLine; bool _displayVerticalLine;
int _displayVerticalLineAtChar; int _displayVerticalLineAtChar;
QSharedPointer<HotSpot> _currentlyHoveredHotspot;
}; };
} }
......
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