File indexing completed on 2024-05-05 16:38:59

0001 /* This file is part of the KDE project
0002    Copyright (C) 1998-2002 Carsten Pfeiffer <pfeiffer@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, version 2.
0007 
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011     General Public License for more details.
0012 
0013    You should have received a copy of the GNU General Public License
0014    along with this program; see the file COPYING.  If not, write to
0015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016    Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include "filefinder.h"
0020 
0021 #include <KCompletionBox>
0022 #include <KConfigGroup>
0023 #include <KSharedConfig>
0024 #include <KUrlCompletion>
0025 
0026 #include <QFocusEvent>
0027 #include <QKeyEvent>
0028 
0029 
0030 FileFinder::FileFinder( QWidget *parent )
0031     : KLineEdit( parent )
0032 {
0033     // make this widget just as large, as the font is + 8 Pixels
0034     int height = fontMetrics().height() + 8;
0035     setFixedSize( 150, height );
0036     setFrame( true );
0037 
0038     setHandleSignals( true ); // we want the completionbox signals
0039     completionBox()->setTabHandling( true );
0040 
0041     connect( completionBox(), SIGNAL( userCancelled(const QString&) ),
0042              SLOT( hide() ));
0043     connect( completionBox(), SIGNAL( activated( const QString& ) ),
0044              SLOT( slotAccept( const QString& )));
0045     connect( this, SIGNAL( returnPressed( const QString& )),
0046              SLOT( slotAccept( const QString& ) ));
0047 
0048     KUrlCompletion *comp = new KUrlCompletion();
0049     comp->setReplaceHome( true );
0050     comp->setReplaceEnv( true );
0051     setCompletionObject( comp, false );
0052     setAutoDeleteCompletionObject( true );
0053     setFocusPolicy( Qt::ClickFocus );
0054 
0055     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0056     KConfigGroup cs( config, "GeneralConfiguration" );
0057     setCompletionMode( (KCompletion::CompletionMode)
0058                cs.readEntry( "FileFinderCompletionMode",
0059                                      int(KCompletion().completionMode())));
0060 }
0061 
0062 FileFinder::~FileFinder()
0063 {
0064     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0065     KConfigGroup cs( config, "GeneralConfiguration" );
0066     cs.writeEntry( "FileFinderCompletionMode", int(completionMode()) );
0067 }
0068 
0069 void FileFinder::focusOutEvent( QFocusEvent *e )
0070 {
0071     if ( e->reason() != Qt::PopupFocusReason )
0072         hide();
0073 }
0074 
0075 void FileFinder::keyPressEvent( QKeyEvent *e )
0076 {
0077     int key = e->key();
0078     if ( key == Qt::Key_Escape ) {
0079         hide();
0080         e->accept();
0081     }
0082 
0083     else {
0084     KLineEdit::keyPressEvent( e );
0085     }
0086 }
0087 
0088 void FileFinder::hide()
0089 {
0090     KLineEdit::hide();
0091     parentWidget()->setFocus();
0092 }
0093 
0094 void FileFinder::slotAccept( const QString& dir )
0095 {
0096     hide();
0097     emit enterDir( dir );
0098 }