1 /*
2  * urllabel.cpp - clickable URL-label
3  * Copyright (C) 2003-2006  Michail Pishchagin
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20 
21 #include "urllabel.h"
22 
23 #include <QMenu>
24 #include <QContextMenuEvent>
25 
26 #include "urlobject.h"
27 
28 //! \if _hide_doc_
29 class URLLabel::Private
30 {
31 public:
32 	QString url;
33 	QString title;
34 };
35 //! \endif
36 
37 /**
38  * \class URLLabel
39  * \brief Clickable URL-label
40  */
41 
42 /**
43  * Default constructor.
44  */
URLLabel(QWidget * parent)45 URLLabel::URLLabel(QWidget *parent)
46 : QLabel(parent)
47 {
48 	d = new Private;
49 	setCursor( Qt::PointingHandCursor );
50 }
51 
~URLLabel()52 URLLabel::~URLLabel()
53 {
54 	delete d;
55 }
56 
57 /**
58  * Returns URL of this label.
59  */
url() const60 const QString &URLLabel::url() const
61 {
62 	return d->url;
63 }
64 
65 /**
66  * Sets the URL of this label to url
67  * \param url an URL string
68  */
setUrl(const QString & url)69 void URLLabel::setUrl(const QString &url)
70 {
71 	d->url = url;
72 	updateText();
73 }
74 
75 /**
76  * Returns title of this label.
77  */
title() const78 const QString &URLLabel::title() const
79 {
80 	return d->title;
81 }
82 
83 /**
84  * Sets the title of this lable to t
85  * \param t this string will be shown to user
86  */
setTitle(const QString & t)87 void URLLabel::setTitle(const QString &t)
88 {
89 	d->title = t;
90 	updateText();
91 }
92 
updateText()93 void URLLabel::updateText()
94 {
95 	setText( QString("<a href=\"%1\">%2</a>").arg(d->url).arg(d->title) );
96 
97 	if ( d->url != d->title )
98 		setToolTip(d->url);
99 	else
100 		setToolTip(QString());
101 }
102 
contextMenuEvent(QContextMenuEvent * e)103 void URLLabel::contextMenuEvent(QContextMenuEvent *e)
104 {
105 	QMenu *m = URLObject::getInstance()->createPopupMenu(d->url);
106 
107 	if ( m ) {
108 		m->exec( e->globalPos() );
109 		delete m;
110 	}
111 
112 	e->accept();
113 }
114 
mouseReleaseEvent(QMouseEvent * e)115 void URLLabel::mouseReleaseEvent(QMouseEvent *e)
116 {
117 	if (e->button() == Qt::LeftButton)
118 		URLObject::getInstance()->popupAction(url());
119 
120 	QLabel::mouseReleaseEvent(e);
121 }
122