1 /* This file is part of the KDE project
2 Copyright (C) 2001 Simon Hausmann <hausmann@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the Artistic License.
11 #include <qapplication.h>
15 #include <q3textedit.h>
18 #include <QMouseEvent>
20 #include <QFocusEvent>
22 #include <Q3PopupMenu>
24 #include <kapplication.h>
29 #include <kstringhandler.h>
30 #include <kfiledialog.h>
38 // FIXME: name is no longer a parameter of KActiveLabel's constructor.
39 // Check if we needed it for anything.
40 KSircTopic::KSircTopic( QWidget
*parent
, const char *name
)
41 : KActiveLabel( parent
)
46 // setBackgroundColor( colorGroup().light() );
47 setFrameStyle( Q3Frame::Panel
| Q3Frame::Sunken
);
48 setSizePolicy( QSizePolicy( QSizePolicy::Expanding
, QSizePolicy::Fixed
) );
49 setTextFormat( RichText
);
50 setWordWrapMode( QTextOption::NoWrap
);
52 // setAlignment( alignment() | WordBreak );
55 void KSircTopic::setText( const QString
&_text
)
57 m_text
= _text
; /* save a raw copy for us */
60 QString
richText( "<font color=\"%1\">" );
61 richText
= richText
.arg( ksopts
->textColor
.name() );
63 text
.replace('&', "&");
64 text
.replace('<', "<");
65 text
.replace('>', ">");
67 text
.replace('~', "~~");
69 // ### a bit of a hack: turn '<nick> message' into
70 // <span><nick><span> message' . span itself isn't supported but it
71 // enforces the creation of separate item objects and hence separate
72 // drawing of '<nick>' and 'message' , which is needed for BiDi users,
73 // according to UV Kochavi <uv1st@yahoo.com> , to prevent output like
74 // '<nick message<' , which is supposedly a bug in Qt's reordering. The
75 // same is done for [nick] and >nick< to catch queries.
76 QRegExp
bidiRe( "^(<\\S+>)(.+)$" );
77 text
.replace( bidiRe
, QString::fromLatin1( "<span>\\1</span>\\2" ) );
78 QRegExp
bidiRe2( "^(\\[\\S+\\])(.+)$" );
79 text
.replace( bidiRe2
, QString::fromLatin1( "<span>\\1</span>\\2" ) );
80 QRegExp
bidiRe3( "^(>\\S+<)(.+)$" );
81 text
.replace( bidiRe3
, QString::fromLatin1( "<span>\\1</span>\\2" ) );
84 richText
+= parser
.parse( text
);
85 richText
+= "</font>";
87 richText
= KStringHandler::tagURLs( richText
);
88 KActiveLabel::setText(richText
);
94 void KSircTopic::mouseDoubleClickEvent( QMouseEvent
* )
99 void KSircTopic::mouseReleaseEvent( QMouseEvent
*e
)
109 m_editor
= new KSircTopicEditor( this );
111 connect( m_editor
, SIGNAL( returnPressed() ),
112 this, SLOT( setNewTopic() ) );
113 connect( m_editor
, SIGNAL( resized() ),
114 this, SLOT( slotEditResized() ) );
115 connect( m_editor
, SIGNAL( destroyed() ),
116 this, SLOT( doResize() ) );
119 * If you don't do this order
120 * the size really breaks and you get
123 m_editor
->setGeometry( geometry() );
124 m_editor
->setFocus();
127 m_editor
->setText( m_text
);
128 QToolTip::remove(this);
131 KActiveLabel::mouseReleaseEvent(e
);
134 void KSircTopic::setNewTopic()
136 QString topic
= m_editor
->text().trimmed();
139 * don't change the channel display
140 * test since if it is set we'll get it
141 * from the server. If we can't set the topic
142 * don't make it look like it was set
144 QTimer::singleShot( 0, m_editor
, SLOT( close() ) );
145 disconnect( m_editor
, SIGNAL( resized() ),
146 this, SLOT( slotEditResized() ) );
148 emit
topicChange( topic
);
151 void KSircTopic::slotEditResized( )
153 setFixedHeight( m_editor
->height() );
157 void KSircTopic::doResize()
161 QFontMetrics
metrics( currentFont() );
163 h
= metrics
.lineSpacing()+8;
168 QToolTip::remove(this);
169 QStringList sep
= QStringList::split(" ", m_text
);
172 QStringList::Iterator it
= sep
.begin();
173 for(; it
!= sep
.end(); ++it
) {
175 len
+= (*it
).length();
182 this->setToolTip( brok
);
186 void KSircTopic::fontChange(QFont
&f
)
188 KActiveLabel::fontChange(f
);
192 KSircTopicEditor::KSircTopicEditor( QWidget
*parent
, const char *name
)
193 : Q3TextEdit( parent
, name
)
195 setAttribute( Qt::WA_DeleteOnClose
);
196 setFocusPolicy( Qt::ClickFocus
);
197 connect( this, SIGNAL( textChanged () ), this, SLOT( slotMaybeResize() ) );
200 void KSircTopicEditor::keyPressEvent( QKeyEvent
*ev
)
202 if ( ev
->key() == Key_Escape
)
205 QTimer::singleShot( 0, this, SLOT( close() ) );
208 else if ( ev
->key() == Key_Return
)
211 emit
returnPressed();
214 Q3TextEdit::keyPressEvent( ev
);
217 void KSircTopicEditor::focusOutEvent( QFocusEvent
* fe
)
219 // we don't want to quit editing when someone brings up QLE's popup
221 if ( fe
->reason() == QFocusEvent::Popup
)
223 QWidget
*focusw
= qApp
->focusWidget();
224 if ( focusw
&& m_popup
&& focusw
== m_popup
)
228 QTimer::singleShot( 0, this, SLOT( close() ) );
231 Q3PopupMenu
*KSircTopicEditor::createPopupMenu( const QPoint
&pos
)
233 Q3PopupMenu
*popup
= Q3TextEdit::createPopupMenu( pos
);
238 void KSircTopicEditor::slotMaybeResize()
240 if(text().contains("\n")){
242 s
.replace('\n', " ");
244 setCursorPosition(0, s
.length());
247 QFontMetrics
metrics( currentFont() );
249 int h
= metrics
.lineSpacing() * lines()+8;