Initial commit
[keepassx] / src / lib / UrlLabel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005 by Tarek Saidi                                     *
3  *   tarek@linux                                                           *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; version 2 of the License.               *
8
9  *                                                                         *
10  *   This program 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         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21
22 #include "UrlLabel.h"
23
24 LinkLabel::LinkLabel(QWidget *parent,const QString& text, int x, int y,Qt::WFlags f) : QLabel(parent,f){
25         QFont font(parentWidget()->font());
26         font.setUnderline(true);
27         setFont(font);
28         QPalette palette;
29         palette.setColor(foregroundRole(),QColor(20,20,255));
30         setPalette(palette);
31         setCursor(Qt::PointingHandCursor);
32         setText(text);
33         setPos(x,y);
34 }
35
36 LinkLabel::~LinkLabel(){
37 }
38
39 QString LinkLabel::url(){
40         if(URL!=QString())
41                 return URL;
42         else if(text().contains("@"))
43                 return QString("mailto:")+text();
44         else
45                 return text();
46 }
47
48 void LinkLabel::mouseReleaseEvent(QMouseEvent* event){
49         if(event->button()==Qt::LeftButton){
50                 emit clicked();
51                 openBrowser(url());
52         }
53 }
54
55 void LinkLabel::setPos(int x,int y){
56         QFontMetrics fm(font());
57         setGeometry(x,y,fm.width(text()),fm.height());
58 }
59
60 void LinkLabel::setText(const QString& text){
61         QLabel::setText(text);
62         setPos(geometry().x(), geometry().y());
63 }