24 #include <tqtooltip.h>
25 #include <tqapplication.h>
28 #include <kcombobox.h>
31 #include <kdirselectdialog.h>
32 #include <tdefiledialog.h>
33 #include <tdeglobal.h>
34 #include <kiconloader.h>
35 #include <klineedit.h>
36 #include <tdelocale.h>
37 #include <kurlcompletion.h>
39 #include <kprotocolinfo.h>
41 #include "kurlrequester.h"
44 class KURLDragPushButton :
public KPushButton
47 KURLDragPushButton( TQWidget *parent,
const char *name=0 )
48 : KPushButton( parent, name ) {
49 setDragEnabled(
true );
51 ~KURLDragPushButton() {}
53 void setURL(
const KURL& url ) {
66 virtual TQDragObject *dragObject() {
67 if ( m_urls.isEmpty() )
70 TQDragObject *drag =
new KURLDrag( m_urls,
this,
"url drag" );
84 class KURLRequester::KURLRequesterPrivate
87 KURLRequesterPrivate() {
90 fileDialogMode = KFile::File | KFile::ExistingOnly | KFile::LocalOnly;
93 void setText(
const TQString& text ) {
96 if (combo->editable())
98 combo->setEditText( text );
102 combo->insertItem( text );
103 combo->setCurrentItem( combo->count()-1 );
108 edit->setText( text );
112 void connectSignals( TQObject *receiver ) {
119 connect( sender, TQ_SIGNAL(
textChanged(
const TQString& )),
120 receiver, TQ_SIGNAL(
textChanged(
const TQString& )));
123 connect( sender, TQ_SIGNAL(
returnPressed(
const TQString& ) ),
127 void setCompletionObject( TDECompletion *comp ) {
129 combo->setCompletionObject( comp );
131 edit->setCompletionObject( comp );
138 TQString txt = combo ? combo->currentText() : edit->text();
139 KURLCompletion *comp;
141 comp =
dynamic_cast<KURLCompletion*
>(combo->completionObject());
143 comp =
dynamic_cast<KURLCompletion*
>(edit->completionObject());
146 return comp->replacedPath( txt );
154 TQString fileDialogFilter;
161 : TQHBox( parent, name )
163 d =
new KURLRequesterPrivate;
166 editWidget->reparent(
this, 0, TQPoint(0,0) );
167 d->edit =
dynamic_cast<KLineEdit*
>( editWidget );
168 d->combo =
dynamic_cast<KComboBox*
>( editWidget );
175 : TQHBox( parent, name )
177 d =
new KURLRequesterPrivate;
184 : TQHBox( parent, name )
186 d =
new KURLRequesterPrivate;
200 void KURLRequester::init()
203 myShowLocalProt =
false;
205 if ( !d->combo && !d->edit )
206 d->edit =
new KLineEdit(
this,
"line edit" );
208 myButton =
new KURLDragPushButton(
this,
"tdefile button");
209 TQIconSet iconSet = SmallIconSet(TQString::fromLatin1(
"document-open"));
210 TQPixmap pixMap = iconSet.pixmap( TQIconSet::Small, TQIconSet::Normal );
211 myButton->setIconSet( iconSet );
212 myButton->setFixedSize( pixMap.width()+8, pixMap.height()+8 );
213 TQToolTip::add(myButton, i18n(
"Open file dialog"));
215 connect( myButton, TQ_SIGNAL( pressed() ), TQ_SLOT( slotUpdateURL() ));
217 setSpacing( KDialog::spacingHint() );
219 TQWidget *widget = d->combo ? (TQWidget*) d->combo : (TQWidget*) d->edit;
220 widget->installEventFilter(
this );
221 setFocusProxy( widget );
223 d->connectSignals(
this );
224 connect( myButton, TQ_SIGNAL( clicked() ),
this, TQ_SLOT(
slotOpenDialog() ));
226 myCompletion =
new KURLCompletion();
227 d->setCompletionObject( myCompletion );
229 TDEAccel *accel =
new TDEAccel(
this );
230 accel->insert( TDEStdAccel::Open,
this, TQ_SLOT(
slotOpenDialog() ));
231 accel->readSettings();
237 if ( myShowLocalProt )
244 if (
url.startsWith(
"file://") )
245 d->setText(
url.mid( 7 ) );
246 else if (
url.startsWith(
"file:") )
247 d->setText(
url.mid( 5 ) );
255 if ( myShowLocalProt )
256 d->setText(
url.url() );
258 d->setText(
url.pathOrURL() );
263 TQWidget::setCaption( caption );
265 myFileDialog->setCaption( caption );
276 if ( (d->fileDialogMode & KFile::Directory) && !(d->fileDialogMode & KFile::File) ||
278 (myFileDialog && ( (myFileDialog->
mode() & KFile::Directory) &&
279 (myFileDialog->
mode() & (KFile::File | KFile::Files)) == 0 ) ) )
282 if ( !newurl.isValid() )
292 if ( !d->url().isEmpty() ) {
295 if ( KProtocolInfo::supportsListing( u ) )
299 if ( dlg->exec() != TQDialog::Accepted )
313 Q_ASSERT( (
mode & KFile::Files) == 0 );
314 d->fileDialogMode =
mode;
315 if ( (
mode & KFile::Directory) && !(
mode & KFile::File) )
316 myCompletion->setMode( KURLCompletion::DirCompletion );
319 myFileDialog->
setMode( d->fileDialogMode );
324 return d->fileDialogMode;
329 d->fileDialogFilter =
filter;
331 myFileDialog->
setFilter( d->fileDialogFilter );
336 return d->fileDialogFilter;
342 if ( !myFileDialog ) {
343 TQWidget *p = parentWidget();
344 myFileDialog =
new KFileDialog( TQString::null, d->fileDialogFilter, p,
345 "file dialog",
true );
347 myFileDialog->
setMode( d->fileDialogMode );
348 myFileDialog->setCaption( caption() );
357 if ( myShowLocalProt == b )
366 d->setText( TQString::null );
379 void KURLRequester::slotUpdateURL()
383 u = KURL( KURL( TQDir::currentDirPath() +
'/' ),
url() );
384 (
static_cast<KURLDragPushButton *
>( myButton ))->setURL( u );
387 bool KURLRequester::eventFilter( TQObject *obj, TQEvent *ev )
389 if ( ( d->edit == obj ) || ( d->combo == obj ) )
391 if (( ev->type() == TQEvent::FocusIn ) || ( ev->type() == TQEvent::FocusOut ))
393 TQApplication::sendEvent(
this, ev );
395 return TQWidget::eventFilter( obj, ev );
405 setSizePolicy(TQSizePolicy( TQSizePolicy::Preferred,
406 TQSizePolicy::Fixed));
408 KLineEdit *edit = d->edit;
409 if ( !edit && d->combo )
410 edit =
dynamic_cast<KLineEdit*
>( d->combo->lineEdit() );
414 kdWarning() <<
"KURLRequester's lineedit is not a KLineEdit!??\n";
417 KEditListBox::CustomEditor editor(
this, edit );
421 void KURLRequester::virtual_hook(
int,
void* )
430 #include "kurlrequester.moc"
static KURL selectDirectory(const TQString &startDir=TQString::null, bool localOnly=false, TQWidget *parent=0L, const TQString &caption=TQString::null)
Creates a KDirSelectDialog, and returns the result.
Provides a user (and developer) friendly way to select files and directories.
KFile::Mode mode() const
Returns the mode of the filedialog.
void setFilter(const TQString &filter)
Sets the filter to be used to filter.
void setMode(KFile::Mode m)
Convenient overload of the other setMode(unsigned int) method.
void setSelection(const TQString &name)
Sets the file name to preselect to name.
KURLComboRequester(TQWidget *parent=0, const char *name=0)
Constructs a KURLRequester widget with a combobox.
This class is a widget showing a lineedit and a button, which invokes a filedialog.
void setKURL(const KURL &url)
Sets the url in the lineedit to url.
void setFilter(const TQString &filter)
Sets the filter for the file dialog.
void setURL(const TQString &url)
Sets the url in the lineedit to url.
virtual KFileDialog * fileDialog() const
void clear()
Clears the lineedit/combobox.
void textChanged(const TQString &)
Emitted when the text in the lineedit changes.
void urlSelected(const TQString &)
Emitted when the user changed the URL via the file dialog.
KComboBox * comboBox() const
void setShowLocalProtocol(bool b)
Enables/disables showing file:/ in the lineedit, when a local file has been selected in the filedialo...
void setMode(uint m)
Sets the mode of the file dialog.
virtual void setCaption(const TQString &caption)
Sets the caption of the file dialog.
KURLRequester(TQWidget *parent=0, const char *name=0)
Constructs a KURLRequester widget.
KEditListBox::CustomEditor customEditor()
uint mode() const
Returns the current mode.
void returnPressed()
Emitted when return or enter was pressed in the lineedit.
void openFileDialog(KURLRequester *)
Emitted before the filedialog is going to open.
void slotOpenDialog()
Called when the button is pressed to open the filedialog.
~KURLRequester()
Destructs the KURLRequester.
KPushButton * button() const
KLineEdit * lineEdit() const
TQString filter() const
Returns the current filter for the file dialog.