Fix crash on logout
[kdenetwork.git] / ksirc / topic.cpp
blobf5623deae7acbaec3d7aa08771b92a267aad7513
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.
6 */
9 #include "topic.h"
11 #include <qapplication.h>
12 #include <qtimer.h>
13 #include <qregexp.h>
14 #include <qlayout.h>
15 #include <q3textedit.h>
16 #include <qtooltip.h>
17 //Added by qt3to4:
18 #include <QMouseEvent>
19 #include <Q3Frame>
20 #include <QFocusEvent>
21 #include <QKeyEvent>
22 #include <Q3PopupMenu>
24 #include <kapplication.h>
25 #include <kdebug.h>
26 #include <klocale.h>
27 #include <krun.h>
28 #include <kmenu.h>
29 #include <kstringhandler.h>
30 #include <kfiledialog.h>
31 #include <kio/job.h>
33 #include "ksopts.h"
34 #include "ksparser.h"
36 using namespace Qt;
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 )
43 m_editor = 0;
44 m_doEdit = false;
45 m_height = 0;
46 // setBackgroundColor( colorGroup().light() );
47 setFrameStyle( Q3Frame::Panel | Q3Frame::Sunken );
48 setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
49 setTextFormat( RichText );
50 setWordWrapMode( QTextOption::NoWrap);
51 doResize();
52 // setAlignment( alignment() | WordBreak );
55 void KSircTopic::setText( const QString &_text)
57 m_text = _text; /* save a raw copy for us */
58 QString text = _text;
60 QString richText( "<font color=\"%1\">" );
61 richText = richText.arg( ksopts->textColor.name() );
63 text.replace('&', "&amp;");
64 text.replace('<', "&lt;");
65 text.replace('>', "&gt;");
67 text.replace('~', "~~");
69 // ### a bit of a hack: turn '&lt;nick&gt; message' into
70 // <span>&lt;nick&gt;<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( "^(&lt;\\S+&gt;)(.+)$" );
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( "^(&gt;\\S+&lt;)(.+)$" );
81 text.replace( bidiRe3, QString::fromLatin1( "<span>\\1</span>\\2" ) );
83 KSParser parser;
84 richText += parser.parse( text );
85 richText += "</font>";
87 richText = KStringHandler::tagURLs( richText );
88 KActiveLabel::setText(richText);
90 doResize();
94 void KSircTopic::mouseDoubleClickEvent( QMouseEvent * )
96 m_doEdit = true;
99 void KSircTopic::mouseReleaseEvent( QMouseEvent *e )
101 if ( m_doEdit ) {
102 m_doEdit = false;
104 if ( m_editor )
105 return;
107 doResize();
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
121 * a huge widget
123 m_editor->setGeometry( geometry() );
124 m_editor->setFocus();
125 m_editor->show();
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() ) );
147 doResize();
148 emit topicChange( topic );
151 void KSircTopic::slotEditResized( )
153 setFixedHeight( m_editor->height() );
157 void KSircTopic::doResize()
160 int h;
161 QFontMetrics metrics( currentFont() );
163 h = metrics.lineSpacing()+8;
164 m_height = h;
165 setFixedHeight( h );
168 QToolTip::remove(this);
169 QStringList sep = QStringList::split(" ", m_text);
170 int len = 0;
171 QString brok;
172 QStringList::Iterator it = sep.begin();
173 for(; it != sep.end(); ++it) {
174 brok += *it + " ";
175 len += (*it).length();
176 if(len >= 50){
177 brok += "\n";
178 len = 0;
182 this->setToolTip( brok);
186 void KSircTopic::fontChange(QFont &f)
188 KActiveLabel::fontChange(f);
189 doResize();
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 )
204 ev->accept();
205 QTimer::singleShot( 0, this, SLOT( close() ) );
206 return;
208 else if ( ev->key() == Key_Return )
210 ev->accept();
211 emit returnPressed();
212 return;
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
220 // menu
221 if ( fe->reason() == QFocusEvent::Popup )
223 QWidget *focusw = qApp->focusWidget();
224 if ( focusw && m_popup && focusw == m_popup )
225 return;
228 QTimer::singleShot( 0, this, SLOT( close() ) );
231 Q3PopupMenu *KSircTopicEditor::createPopupMenu( const QPoint &pos )
233 Q3PopupMenu *popup = Q3TextEdit::createPopupMenu( pos );
234 m_popup = popup;
235 return popup;
238 void KSircTopicEditor::slotMaybeResize()
240 if(text().contains("\n")){
241 QString s = text();
242 s.replace('\n', " ");
243 setText(s);
244 setCursorPosition(0, s.length());
247 QFontMetrics metrics( currentFont() );
249 int h = metrics.lineSpacing() * lines()+8;
250 setFixedHeight( h );
251 emit resized();
254 #include "topic.moc"