📄 kurllabel.cpp
字号:
/** * KURLLabel class implementation * kurllable.cpp * * Copyright (C) 1998 Kurt Granroth <granroth@kde.org> */#include "kurllabel.h"#include <qtooltip.h>#include <kcursor.h>KURLLabel::KURLLabel(QWidget *parent, const char* name, WFlags f) : QLabel(parent, name, f), m_textAlign(Bottom), m_url(0), m_tipText(0), m_float(false), m_tips(false), m_glow(true), m_underline(true), m_inRegion(false), m_haveCursor(true), m_transparent(false){ /* set the defaults */ m_hc.setNamedColor("blue"); m_sc.setNamedColor("red"); m_bc = backgroundColor(); setUseCursor(true); setMouseTracking(true); m_resetPalette();}KURLLabel::~KURLLabel(){}const char* KURLLabel::url() const{ return m_url;}const char* KURLLabel::text() const{ return (const char*)m_text;}const QPixmap* KURLLabel::pixmap() const{ return &m_pixmap;}void KURLLabel::setURL(const char* url){ /* save the input */ m_url = url; /* show the tool tip if we are allowed to */ if (m_tips) { if (m_tipText) QToolTip::add(this, m_tipText); else QToolTip::add(this, m_url); }}void KURLLabel::setTextAlignment(TextAlignment align){ m_textAlign = align; if (autoResize()) adjustSize(); else repaint(true);}void KURLLabel::setUseCursor(bool use_cursor, const QCursor* cursor){ /* set the global var */ m_haveCursor = use_cursor; /* if this is false, then use the default cursor */ if (use_cursor == false) { m_customCursor = QCursor(); return; } /* set the cursor to the user defined one if supplied */ if (cursor) { m_customCursor = *cursor; } else { /* otherwise, use the "hand" cursor */ m_customCursor = KCursor::handCursor(); }}void KURLLabel::setTipText(const char* tip){ /* make sure there is something coming in */ if (!tip) return; /* assign the input */ m_tipText = tip; /* reset the tooltips if we can */ if (m_tips) QToolTip::add(this, m_tipText);}void KURLLabel::setUseTips(bool tips){ /* save the input */ m_tips = tips; /* setup the tool tip if we are ready */ if (m_tips) { /* can we use a user tip? */ if (m_tipText) QToolTip::add(this, m_tipText); else if (m_url) QToolTip::add(this, m_url); }}void KURLLabel::setFloat(bool do_float){ /* save the input */ m_float = do_float;}void KURLLabel::setFont(const QFont& font){ /* set the underlining */ QFont new_font = font; new_font.setUnderline(m_underline); /* use the base setFont to do all the real work */ QLabel::setFont(new_font);}void KURLLabel::setText(const char* text){ /** * we set the underlining now and in setUnderline * and in setFont to cover all the bases. * this allows the user to invoke these functions * in either order. */ QFont tmp_font = font(); tmp_font.setUnderline(m_underline); setFont(tmp_font); /* set the palette to normal (at first)*/ setPalette(m_nsp); /* save a copy of the text for our uses */ m_text = text; /* use the native setText for some processing */ QLabel::setText(text); /* if we don't have a decent URL, set it equal to our text */ if (m_url == 0) m_url = text; /* show the tool tip if we are allowed to */ if (m_tips) { if (m_tipText) QToolTip::add(this, m_tipText); else QToolTip::add(this, m_url); }}void KURLLabel::setAltPixmap(const QPixmap& pixmap){ /* set the "alt" pixmap */ m_altPixmap = pixmap;}void KURLLabel::setPixmap(const QPixmap& pixmap){ /* save a copy of the pixmap for use later */ m_pixmap = pixmap; /* let the base setPixmap do all the work */ QLabel::setPixmap(pixmap);}void KURLLabel::setMovie(const QMovie& movie){ /* save a copy of the movie */ m_movie = movie; /* let the base function do all the work */ QLabel::setMovie(movie);}void KURLLabel::setGlow(bool glow){ /* save the input */ m_glow = glow;}void KURLLabel::setUnderline(bool underline){ /* save the input */ m_underline = underline; /* turn on or off the underlining */ QFont tmp_font = font(); tmp_font.setUnderline(m_underline); setFont(tmp_font);}void KURLLabel::setHighlightedColor(const QColor& high){ /* set the new color */ m_hc = high; /* reset the palette to display the new color */ m_resetPalette(); setPalette(m_nsp);}void KURLLabel::setHighlightedColor(const char* high){ /* set the new color */ m_hc.setNamedColor(high); /* reset the palette to display the new color */ m_resetPalette(); setPalette(m_nsp);}void KURLLabel::setSelectedColor(const QColor& selected){ /* set the new color */ m_sc = selected; /* reset the palette to display the new color */ m_resetPalette();}void KURLLabel::setSelectedColor(const char* selected){ /* set the new color */ m_sc.setNamedColor(selected); /* reset the palette to display the new color */ m_resetPalette();}void KURLLabel::setBackgroundColor(const QColor& back){ /* set the new color */ m_bc = back; /* reset the palette to display the new color */ m_resetPalette(); setPalette(m_nsp);}void KURLLabel::setBackgroundColor(const char* back){ /* set the new color */ m_bc.setNamedColor(back); /* reset the palette to display the new color */ m_resetPalette(); setPalette(m_nsp);}QSize KURLLabel::sizeHint() const{ int x_min, x_max, y_min, y_max; /* get the bounding rectangles for text and pixmap */ QRect text_rect = m_textRect(); QRect pixmap_rect = m_pixmapRect(); /* get the outer x coordinates */ x_min = QMIN(text_rect.topLeft().x(), pixmap_rect.topLeft().x()); x_max = QMAX(text_rect.topRight().x(), pixmap_rect.topRight().x()); /* get the outer y coordinates */ y_min = QMIN(text_rect.topLeft().y(), pixmap_rect.topLeft().y()); y_max = QMAX(text_rect.bottomLeft().y(), pixmap_rect.bottomLeft().y()); return QSize((x_max - x_min)+1, (y_max - y_min)+1);}void KURLLabel::mouseMoveEvent(QMouseEvent *event){ /* get the boundries of the text and/or pixmap */ QRect text_rect = m_textRect(); QRect pixmap_text = m_pixmapRect(); int tx_min = text_rect.topLeft().x(); int ty_min = text_rect.topLeft().y(); int tx_max = text_rect.bottomRight().x(); int ty_max = text_rect.bottomRight().y(); int px_min = pixmap_text.topLeft().x(); int py_min = pixmap_text.topLeft().y(); int px_max = pixmap_text.bottomRight().x(); int py_max = pixmap_text.bottomRight().y(); /* process this event only if we are within the text boundry */ if (((event->x() > tx_min) && (event->x() < tx_max) && (event->y() > ty_min) && (event->y() < ty_max)) || ((event->x() > px_min) && (event->x() < px_max) && (event->y() > py_min) && (event->y() < py_max))) { /** * if we were not within the region before, then this is * a enter event */ if (m_inRegion == false) { m_inRegion = true; m_enterEvent(); } } /* if we were in the region before, then this is a leave event */ else if (m_inRegion == true) { m_inRegion = false; m_leaveEvent(); }}void KURLLabel::m_enterEvent(){ /* emit this signal with our URL*/ emit(enteredURL(m_url)); emit(enteredURL()); /* check if we have an "alt" pixmap */ if (!m_altPixmap.isNull() && (m_float || m_glow)) { /* display it instead of the regular pixmap */ m_origPixmap = m_pixmap; m_pixmap = m_altPixmap; } /* if we are using a custom cursor, use it */ if (m_haveCursor); setCursor(m_customCursor); /* check if we are in float mode */ if (m_float) { /* turn on underlining */ QFont tmp_font = font(); tmp_font.setUnderline(true); QLabel::setFont(tmp_font); /* and "glow" this */ setPalette(m_sp); } else /* if we are in "glow" mode, turn on the selected palette */ if (m_glow) setPalette(m_sp);}void KURLLabel::leaveEvent(QEvent*){ /* let m_leaveEvent handle this if we are in the region */ if (m_inRegion) { m_inRegion = false; m_leaveEvent(); }}void KURLLabel::m_leaveEvent(){ /* emit this signal with our URL*/ emit(leftURL(m_url)); emit(leftURL()); /* check if we have an "alt" pixmap */ if (!m_altPixmap.isNull() && (m_float || m_glow)) { /* change back to the original */ m_pixmap = m_origPixmap; } /* if we are using a custom cursor, set it back */ if (m_haveCursor) setCursor(QCursor()); /* check if we are in float mode */ if (m_float) { /* switch underlining back to original state */ QFont tmp_font = font(); tmp_font.setUnderline(m_underline); QLabel::setFont(tmp_font); /* set palette back to normal*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -