Commit 41dc787d authored by Tomaz  Canabrava's avatar Tomaz Canabrava

Move more code to the HotSpot classes

Add new methods:
mouseLeaveEvent
keyReleaseEvent

as usual, those methods acts in the same way as the Qt ones
but it's internal to the HotSpot
parent 058a8cd9
......@@ -22,6 +22,10 @@
#include <KRun>
#include <QApplication>
#include <QMouseEvent>
#include <QDebug>
#include "widgets/TerminalDisplay.h"
using namespace Konsole;
......@@ -45,3 +49,14 @@ 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);
qDebug() << td->cursor();
}
......@@ -31,6 +31,8 @@ public:
EscapeSequenceUrlHotSpot(int startLine, int startColumn, int endLine, int endColumn,
const QString &text, const QString &url);
void activate(QObject *obj) override;
void mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev) override;
private:
QString _text;
QString _url;
......
......@@ -153,6 +153,13 @@ void FilterChain::leaveEvent(TerminalDisplay *td, QEvent *ev)
{
}
void FilterChain::keyReleaseEvent(TerminalDisplay *td, QKeyEvent *ev, int charLine, int charColumn)
{
auto spot = hotSpotAt(charLine, charColumn);
if (spot) {
spot->keyReleaseEvent(td, ev);
}
}
void FilterChain::keyPressEvent(TerminalDisplay *td, QKeyEvent *ev, int charLine, int charColumn)
{
......
......@@ -91,6 +91,7 @@ public:
void mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev, int charLine, int charColumn);
void keyPressEvent(TerminalDisplay *td, QKeyEvent *ev, int charLine, int charColumn);
void keyReleaseEvent(TerminalDisplay *td, QKeyEvent *ev, int charLine, int charColumn);
void leaveEvent(TerminalDisplay *td, QEvent *ev);
void paint(TerminalDisplay *td, QPainter &painter);
......
......@@ -117,18 +117,11 @@ void HotSpot::mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev)
{
auto r = region(td->fontWidth(), td->fontHeight(), td->columns(), td->contentRect()).first;
td->update(r);
td->setCursor(Qt::IBeamCursor);
td->resetCursor();
}
void HotSpot::mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev)
{
QCursor cursor = td->cursor();
if ((td->openLinksByDirectClick() || ((ev->modifiers() & Qt::ControlModifier) != 0u))
|| (cursor.shape() == Qt::PointingHandCursor)) {
td->setCursor(td->usesMouseTracking() ? Qt::ArrowCursor : Qt::IBeamCursor);
}
auto r = region(td->fontWidth(), td->fontHeight(), td->columns(), td->contentRect()).first;
td->update(r);
}
void HotSpot::debug() {
......
......@@ -110,10 +110,12 @@ public:
QPair<QRegion, QRect> region(int fontWidth, int fontHeight, int columns, QRect terminalDisplayRect) const;
virtual void mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev) { Q_UNUSED(td); Q_UNUSED(ev); };
virtual void mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev);
virtual void mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev);
virtual void keyPressEvent(TerminalDisplay *td, QKeyEvent *ev) {};
virtual void mouseMoveEvent(TerminalDisplay *, QMouseEvent *);
virtual void mouseLeaveEvent(TerminalDisplay *, QMouseEvent *);
virtual void mouseEnterEvent(TerminalDisplay *, QMouseEvent *) {};
virtual void keyPressEvent(TerminalDisplay *, QKeyEvent *) {};
virtual void keyReleaseEvent(TerminalDisplay *, QKeyEvent *) {};
void debug();
protected:
/** Sets the type of a hotspot. This should only be set once */
......
......@@ -127,14 +127,38 @@ QList<QAction *> UrlFilterHotSpot::actions()
return {openAction, copyAction};
}
void Konsole::UrlFilterHotSpot::mouseMoveEvent(Konsole::TerminalDisplay* td, QMouseEvent* ev)
template<typename Event>
void setTerminalCursor(const QRegion &r, TerminalDisplay *td, Event *ev)
{
auto cursor = td->cursor();
auto r = region(td->fontWidth(), td->fontHeight(), td->columns(), td->contentRect()).first;
QCursor cursor = td->cursor();
if ((td->openLinksByDirectClick()|| ((ev->modifiers() & Qt::ControlModifier) != 0u)) && (cursor.shape() != Qt::PointingHandCursor)) {
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::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,7 +45,9 @@ public:
* the capturedTexts() method.
*/
void activate(QObject *object = nullptr) override;
void mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev) override;
void mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev) override;
void keyPressEvent(TerminalDisplay *td, QKeyEvent *ev) override;
void keyReleaseEvent(TerminalDisplay *td, QKeyEvent *ev) override;
private:
enum UrlType {
......
......@@ -2241,7 +2241,6 @@ void TerminalDisplay::mouseMoveEvent(QMouseEvent* ev)
processFilters();
_filterChain->mouseMoveEvent(this, ev, charLine, charColumn);
// for auto-hiding the cursor, we need mouseTracking
if (ev->buttons() == Qt::NoButton) {
return;
......@@ -3052,8 +3051,14 @@ void TerminalDisplay::setWordCharacters(const QString& wc)
void TerminalDisplay::setUsesMouseTracking(bool on)
{
_usesMouseTracking = on;
resetCursor();
}
void TerminalDisplay::resetCursor()
{
setCursor(_usesMouseTracking ? Qt::ArrowCursor : Qt::IBeamCursor);
}
bool TerminalDisplay::usesMouseTracking() const
{
return _usesMouseTracking;
......@@ -3514,7 +3519,7 @@ void TerminalDisplay::keyPressEvent(QKeyEvent* event)
{ // C++17: change getCharacterPosition to return a tuple and use auto [charLine, charColumn] to extract the values.
int charLine;
int charColumn;
getCharacterPosition(QCursor::pos(), charLine, charColumn, !_usesMouseTracking);
getCharacterPosition(mapFromGlobal(QCursor::pos()), charLine, charColumn, !_usesMouseTracking);
_filterChain->keyPressEvent(this, event, charLine, charColumn);
}
......@@ -3562,6 +3567,13 @@ void TerminalDisplay::keyReleaseEvent(QKeyEvent *event)
return;
}
{ // C++17: change getCharacterPosition to return a tuple and use auto [charLine, charColumn] to extract the values.
int charLine;
int charColumn;
getCharacterPosition(mapFromGlobal(QCursor::pos()), charLine, charColumn, !_usesMouseTracking);
_filterChain->keyReleaseEvent(this, event, charLine, charColumn);
}
QWidget::keyReleaseEvent(event);
}
......
......@@ -322,6 +322,8 @@ public:
/** Reset the font size */
void resetFontSize();
void resetCursor();
QRect contentRect() const { return _contentRect; }
bool openLinksByDirectClick() const { return _openLinksByDirectClick; }
......
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