Commit b05880cd authored by Robert Knight's avatar Robert Knight

Implement popup menu when right-clicking on URLs. Display appropriate actions...

Implement popup menu when right-clicking on URLs.  Display appropriate actions for normal URLs and email addresses.  Accept '@' character in URLs - for use when a URL includes a username.  eg. ssh://user@host 

svn path=/branches/work/konsole-split-view/; revision=658642
parent a27cc6ec
......@@ -23,6 +23,7 @@
// Qt
#include <QAction>
#include <QApplication>
#include <QClipboard>
#include <QString>
#include <QtDebug>
#include <QSharedData>
......@@ -273,7 +274,7 @@ RegExpFilter::HotSpot::HotSpot(int startLine,int startColumn,int endLine,int end
setType(Marker);
}
void RegExpFilter::HotSpot::activate()
void RegExpFilter::HotSpot::activate(QObject*)
{
}
......@@ -360,35 +361,67 @@ QString UrlFilter::HotSpot::tooltip() const
{
QString url = capturedTexts().first();
if ( QRegExp(FullUrlRegExp).exactMatch(url) )
const UrlType kind = urlType();
if ( kind == StandardUrl )
return i18n("Click to open '%1' in your browser.",url);
else if ( QRegExp(EmailAddressRegExp).exactMatch(url) )
else if ( kind == Email )
return i18n("Click to send an email to '%1'.",url);
else
return QString::null;
}
void UrlFilter::HotSpot::activate()
UrlFilter::HotSpot::UrlType UrlFilter::HotSpot::urlType() const
{
const QStringList& texts = capturedTexts();
QString url = capturedTexts().first();
if ( QRegExp(FullUrlRegExp).exactMatch(url) )
return StandardUrl;
else if ( QRegExp(EmailAddressRegExp).exactMatch(url) )
return Email;
else
return Unknown;
}
void UrlFilter::HotSpot::activate(QObject* object)
{
QString url = capturedTexts().first();
QString url = texts.first();
// if the URL path does not include the protocol ( eg. "www.kde.org" ) then
// prepend http:// ( eg. "www.kde.org" --> "http://www.kde.org" )
if (!url.contains("://"))
const UrlType kind = urlType();
const QString& actionName = object ? object->objectName() : QString::null;
if ( actionName == "copy-action" )
{
url.prepend("http://");
qDebug() << "Copying url to clipboard:" << url;
QApplication::clipboard()->setText(url);
return;
}
if ( texts.count() > 0 )
if ( !object || actionName == "open-action" )
{
if ( kind == StandardUrl )
{
// if the URL path does not include the protocol ( eg. "www.kde.org" ) then
// prepend http:// ( eg. "www.kde.org" --> "http://www.kde.org" )
if (!url.contains("://"))
{
url.prepend("http://");
}
}
else if ( kind == Email )
{
url.prepend("mailto:");
}
new KRun(url,QApplication::activeWindow());
}
}
//regexp matches:
// full url:
// protocolname:// or www. followed by numbers, letters dots and dashes
const QString UrlFilter::FullUrlRegExp("([a-z]+://|www\\.)[a-zA-Z0-9\\-\\./]+");
// protocolname:// or www. followed by numbers, letters dots and dashes or the '@' character.
const QString UrlFilter::FullUrlRegExp("([a-z]+://|www\\.)[a-zA-Z0-9@\\-\\./]+");
// email address:
// [word chars, dots or dashes]@[word chars, dots or dashes].[word chars]
const QString UrlFilter::EmailAddressRegExp("(\\w|\\.|-)+@(\\w|\\.|-)+\\.\\w+");
......@@ -406,15 +439,41 @@ UrlFilter::HotSpot::~HotSpot()
}
void FilterObject::activated()
{
_filter->activate();
_filter->activate(sender());
}
QList<QAction*> UrlFilter::HotSpot::actions()
{
QList<QAction*> list;
QAction* openAction = new QAction(i18n("Open Link"),_urlObject);
const UrlType kind = urlType();
QAction* openAction = new QAction(_urlObject);
QAction* copyAction = new QAction(_urlObject);;
Q_ASSERT( kind == StandardUrl || kind == Email );
if ( kind == StandardUrl )
{
openAction->setText(i18n("Open Link"));
copyAction->setText(i18n("Copy Link Address"));
}
else if ( kind == Email )
{
openAction->setText(i18n("Send Email To..."));
copyAction->setText(i18n("Copy Email Address"));
}
// object names are set here so that the hotspot performs the
// correct action when activated() is called with the triggered
// action passed as a parameter.
openAction->setObjectName("open-action");
copyAction->setObjectName("copy-action");
QObject::connect( openAction , SIGNAL(triggered()) , _urlObject , SLOT(activated()) );
QObject::connect( copyAction , SIGNAL(triggered()) , _urlObject , SLOT(activated()) );
list << openAction;
list << copyAction;
return list;
}
......
......@@ -98,9 +98,19 @@ public:
* the hotspot graphically. eg. Link hotspots are typically underlined when the user mouses over them
*/
Type type() const;
/** Causes the primary action associated with a hotspot to be triggered. */
virtual void activate() = 0;
/** Returns a list of actions associated with the hotspot which can be used in a menu or toolbar */
/**
* Causes the an action associated with a hotspot to be triggered.
*
* @param object The object which caused the hotspot to be triggered. This is
* typically null ( in which case the default action should be performed ) or
* one of the objects from the actions() list. In which case the associated
* action should be performed.
*/
virtual void activate(QObject* object = 0) = 0;
/**
* Returns a list of actions associated with the hotspot which can be used in a
* menu or toolbar
*/
virtual QList<QAction*> actions();
/**
......@@ -180,7 +190,7 @@ public:
{
public:
HotSpot(int startLine, int startColumn, int endLine , int endColumn);
virtual void activate();
virtual void activate(QObject* object = 0);
/** Sets the captured texts associated with this hotspot */
void setCapturedTexts(const QStringList& texts);
......@@ -235,10 +245,18 @@ public:
* Open a web browser at the current URL. The url itself can be determined using
* the capturedTexts() method.
*/
virtual void activate();
virtual void activate(QObject* object = 0);
virtual QString tooltip() const;
private:
enum UrlType
{
StandardUrl,
Email,
Unknown
};
UrlType urlType() const;
FilterObject* _urlObject;
};
......@@ -248,6 +266,7 @@ protected:
virtual RegExpFilter::HotSpot* newHotSpot(int,int,int,int);
private:
static const QString FullUrlRegExp;
static const QString EmailAddressRegExp;
};
......
......@@ -1753,6 +1753,8 @@ void TerminalDisplay::mousePressEvent(QMouseEvent* ev)
characterPosition(ev->pos(),charLine,charColumn);
QPoint pos = QPoint(charColumn,charLine);
Filter::HotSpot* spot = _filterChain->hotSpotAt(charLine,charColumn);
//kDebug() << " mouse pressed at column = " << pos.x() << " , line = " << pos.y() << endl;
if ( ev->button() == Qt::LeftButton)
......@@ -1807,15 +1809,33 @@ void TerminalDisplay::mousePressEvent(QMouseEvent* ev)
}
else if ( ev->button() == Qt::RightButton )
{
if (_mouseMarks || (ev->modifiers() & Qt::ShiftModifier)) {
_configureRequestPoint = QPoint( ev->x(), ev->y() );
emit configureRequest( this, ev->modifiers()&(Qt::ShiftModifier|Qt::ControlModifier), ev->x(), ev->y() );
if (_mouseMarks || (ev->modifiers() & Qt::ShiftModifier))
{
if ( spot )
{
showHotSpotMenu(spot , mapToGlobal(QPoint(ev->x() , ev->y())) );
}
else
{
_configureRequestPoint = QPoint( ev->x(), ev->y() );
emit configureRequest( this,
ev->modifiers() & (Qt::ShiftModifier|Qt::ControlModifier),
ev->x(),
ev->y()
);
}
}
else
emit mouseSignal( 2, charColumn +1, charLine +1 +_scrollBar->value() -_scrollBar->maximum() , 0);
}
}
void TerminalDisplay::showHotSpotMenu(Filter::HotSpot* spot , const QPoint& position)
{
QMenu* menu = new QMenu(this);
menu->addActions( spot->actions() );
menu->popup(position);
}
void TerminalDisplay::mouseMoveEvent(QMouseEvent* ev)
{
int charLine = 0;
......
......@@ -538,6 +538,9 @@ private:
// current size in columns and lines
void showResizeNotification();
// shows the popup menu associated with a hotspot
void showHotSpotMenu(Filter::HotSpot* spot , const QPoint& position);
// scrolls the image by a number of lines. 'lines' may be positive ( to scroll the image down )
// or negative ( to scroll the image up )
void scrollImage(int lines);
......
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