1 | /* This file is part of the KDE libraries |
2 | Copyright (C) 1999 Waldo Bastian (bastian@kde.org) |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; version 2 |
7 | of the License. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to |
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "k3activelabel.h" |
21 | |
22 | #include <Q3SimpleRichText> |
23 | #include <QFocusEvent> |
24 | |
25 | #include <ktoolinvocation.h> |
26 | |
27 | class K3ActiveLabelPrivate |
28 | { |
29 | public: |
30 | K3ActiveLabelPrivate(K3ActiveLabel *qq); |
31 | |
32 | void updatePalette(); |
33 | |
34 | K3ActiveLabel *q; |
35 | }; |
36 | |
37 | K3ActiveLabelPrivate::K3ActiveLabelPrivate(K3ActiveLabel *qq) |
38 | : q(qq) |
39 | { |
40 | q->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
41 | q->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
42 | q->setFrameStyle(QFrame::NoFrame); |
43 | q->setFocusPolicy(Qt::TabFocus); |
44 | updatePalette(); |
45 | } |
46 | |
47 | void K3ActiveLabelPrivate::updatePalette() |
48 | { |
49 | QPalette p = q->palette(); |
50 | p.setBrush(QPalette::Base, p.brush(QPalette::Normal, QPalette::Background)); |
51 | p.setColor(QPalette::Text, p.color(QPalette::Normal, QPalette::Foreground)); |
52 | q->setPalette(p); |
53 | } |
54 | |
55 | K3ActiveLabel::K3ActiveLabel(QWidget * parent) |
56 | : KTextBrowser(parent),d(new K3ActiveLabelPrivate(this)) |
57 | { |
58 | } |
59 | |
60 | K3ActiveLabel::K3ActiveLabel(const QString &text, QWidget * parent) |
61 | : KTextBrowser(parent),d(new K3ActiveLabelPrivate(this)) |
62 | { |
63 | setHtml(text); |
64 | } |
65 | |
66 | K3ActiveLabel::~K3ActiveLabel() |
67 | { |
68 | delete d; |
69 | } |
70 | |
71 | void K3ActiveLabel::focusInEvent( QFocusEvent* fe ) |
72 | { |
73 | KTextBrowser::focusInEvent(fe); |
74 | if(fe->reason() == Qt::TabFocusReason || fe->reason() == Qt::BacktabFocusReason) |
75 | selectAll(); |
76 | } |
77 | |
78 | void K3ActiveLabel::focusOutEvent( QFocusEvent* fe ) |
79 | { |
80 | KTextBrowser::focusOutEvent(fe); |
81 | if(fe->reason() == Qt::TabFocusReason || fe->reason() == Qt::BacktabFocusReason) |
82 | selectAll(); //TODO reimplement: deselect text |
83 | } |
84 | |
85 | void K3ActiveLabel::keyPressEvent( QKeyEvent *e ) |
86 | { |
87 | switch ( e->key() ) |
88 | { |
89 | case Qt::Key_Down: |
90 | case Qt::Key_Up: |
91 | case Qt::Key_Left: |
92 | case Qt::Key_Right: |
93 | // jump over QTextEdit's key navigation breakage. |
94 | // we're not interested in keyboard navigation within the text |
95 | QWidget::keyPressEvent( e ); |
96 | break; |
97 | default: |
98 | KTextBrowser::keyPressEvent( e ); |
99 | } |
100 | } |
101 | |
102 | bool K3ActiveLabel::event(QEvent *e) |
103 | { |
104 | // call the base implementation first so it updates |
105 | // our palette |
106 | const bool result = KTextBrowser::event(e); |
107 | if (e->type() == QEvent::ApplicationPaletteChange) { |
108 | d->updatePalette(); |
109 | } |
110 | return result; |
111 | } |
112 | |
113 | QSize K3ActiveLabel::minimumSizeHint() const |
114 | { |
115 | QSize ms = minimumSize(); |
116 | if ((ms.width() > 0) && (ms.height() > 0)) |
117 | return ms; |
118 | |
119 | int w = 400; |
120 | if (ms.width() > 0) |
121 | w = ms.width(); |
122 | |
123 | QString txt = toHtml(); |
124 | Q3SimpleRichText rt(txt, font()); |
125 | rt.setWidth(w - 2*frameWidth() - 10); |
126 | w = 10 + rt.widthUsed() + 2*frameWidth(); |
127 | if (w < ms.width()) |
128 | w = ms.width(); |
129 | int h = rt.height() + 2*frameWidth(); |
130 | if ( h < ms.height()) |
131 | h = ms.height(); |
132 | |
133 | return QSize(w, h); |
134 | } |
135 | |
136 | QSize K3ActiveLabel::sizeHint() const |
137 | { |
138 | return minimumSizeHint(); |
139 | } |
140 | |
141 | #include "k3activelabel.moc" |
142 | |