• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdefile
 

tdeio/tdefile

  • tdeio
  • tdefile
kurlrequester.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 1999,2000,2001 Carsten Pfeiffer <pfeiffer@kde.org>
3
4 library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2, as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19
20#include <sys/stat.h>
21#include <unistd.h>
22
23#include <tqstring.h>
24#include <tqtooltip.h>
25#include <tqapplication.h>
26
27#include <tdeaccel.h>
28#include <kcombobox.h>
29#include <kdebug.h>
30#include <kdialog.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>
38#include <kurldrag.h>
39#include <kprotocolinfo.h>
40
41#include "kurlrequester.h"
42
43
44class KURLDragPushButton : public KPushButton
45{
46public:
47 KURLDragPushButton( TQWidget *parent, const char *name=0 )
48 : KPushButton( parent, name ) {
49 setDragEnabled( true );
50 }
51 ~KURLDragPushButton() {}
52
53 void setURL( const KURL& url ) {
54 m_urls.clear();
55 m_urls.append( url );
56 }
57
58 /* not needed so far
59 void setURLs( const KURL::List& urls ) {
60 m_urls = urls;
61 }
62 const KURL::List& urls() const { return m_urls; }
63 */
64
65protected:
66 virtual TQDragObject *dragObject() {
67 if ( m_urls.isEmpty() )
68 return 0L;
69
70 TQDragObject *drag = new KURLDrag( m_urls, this, "url drag" );
71 return drag;
72 }
73
74private:
75 KURL::List m_urls;
76
77};
78
79
80/*
81*************************************************************************
82*/
83
84class KURLRequester::KURLRequesterPrivate
85{
86public:
87 KURLRequesterPrivate() {
88 edit = 0L;
89 combo = 0L;
90 fileDialogMode = KFile::File | KFile::ExistingOnly | KFile::LocalOnly;
91 }
92
93 void setText( const TQString& text ) {
94 if ( combo )
95 {
96 if (combo->editable())
97 {
98 combo->setEditText( text );
99 }
100 else
101 {
102 combo->insertItem( text );
103 combo->setCurrentItem( combo->count()-1 );
104 }
105 }
106 else
107 {
108 edit->setText( text );
109 }
110 }
111
112 void connectSignals( TQObject *receiver ) {
113 TQObject *sender;
114 if ( combo )
115 sender = combo;
116 else
117 sender = edit;
118
119 connect( sender, TQ_SIGNAL( textChanged( const TQString& )),
120 receiver, TQ_SIGNAL( textChanged( const TQString& )));
121 connect( sender, TQ_SIGNAL( returnPressed() ),
122 receiver, TQ_SIGNAL( returnPressed() ));
123 connect( sender, TQ_SIGNAL( returnPressed( const TQString& ) ),
124 receiver, TQ_SIGNAL( returnPressed( const TQString& ) ));
125 }
126
127 void setCompletionObject( TDECompletion *comp ) {
128 if ( combo )
129 combo->setCompletionObject( comp );
130 else
131 edit->setCompletionObject( comp );
132 }
133
137 TQString url() {
138 TQString txt = combo ? combo->currentText() : edit->text();
139 KURLCompletion *comp;
140 if ( combo )
141 comp = dynamic_cast<KURLCompletion*>(combo->completionObject());
142 else
143 comp = dynamic_cast<KURLCompletion*>(edit->completionObject());
144
145 if ( comp )
146 return comp->replacedPath( txt );
147 else
148 return txt;
149 }
150
151 KLineEdit *edit;
152 KComboBox *combo;
153 int fileDialogMode;
154 TQString fileDialogFilter;
155};
156
157
158
159KURLRequester::KURLRequester( TQWidget *editWidget, TQWidget *parent,
160 const char *name )
161 : TQHBox( parent, name )
162{
163 d = new KURLRequesterPrivate;
164
165 // must have this as parent
166 editWidget->reparent( this, 0, TQPoint(0,0) );
167 d->edit = dynamic_cast<KLineEdit*>( editWidget );
168 d->combo = dynamic_cast<KComboBox*>( editWidget );
169
170 init();
171}
172
173
174KURLRequester::KURLRequester( TQWidget *parent, const char *name )
175 : TQHBox( parent, name )
176{
177 d = new KURLRequesterPrivate;
178 init();
179}
180
181
182KURLRequester::KURLRequester( const TQString& url, TQWidget *parent,
183 const char *name )
184 : TQHBox( parent, name )
185{
186 d = new KURLRequesterPrivate;
187 init();
188 setKURL( KURL::fromPathOrURL( url ) );
189}
190
191
192KURLRequester::~KURLRequester()
193{
194 delete myCompletion;
195 delete myFileDialog;
196 delete d;
197}
198
199
200void KURLRequester::init()
201{
202 myFileDialog = 0L;
203 myShowLocalProt = false;
204
205 if ( !d->combo && !d->edit )
206 d->edit = new KLineEdit( this, "line edit" );
207
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"));
214
215 connect( myButton, TQ_SIGNAL( pressed() ), TQ_SLOT( slotUpdateURL() ));
216
217 setSpacing( KDialog::spacingHint() );
218
219 TQWidget *widget = d->combo ? (TQWidget*) d->combo : (TQWidget*) d->edit;
220 widget->installEventFilter( this );
221 setFocusProxy( widget );
222
223 d->connectSignals( this );
224 connect( myButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotOpenDialog() ));
225
226 myCompletion = new KURLCompletion();
227 d->setCompletionObject( myCompletion );
228
229 TDEAccel *accel = new TDEAccel( this );
230 accel->insert( TDEStdAccel::Open, this, TQ_SLOT( slotOpenDialog() ));
231 accel->readSettings();
232}
233
234
235void KURLRequester::setURL( const TQString& url )
236{
237 if ( myShowLocalProt )
238 {
239 d->setText( url );
240 }
241 else
242 {
243 // ### This code is broken (e.g. for paths with '#')
244 if ( url.startsWith("file://") )
245 d->setText( url.mid( 7 ) );
246 else if ( url.startsWith("file:") )
247 d->setText( url.mid( 5 ) );
248 else
249 d->setText( url );
250 }
251}
252
253void KURLRequester::setKURL( const KURL& url )
254{
255 if ( myShowLocalProt )
256 d->setText( url.url() );
257 else
258 d->setText( url.pathOrURL() );
259}
260
261void KURLRequester::setCaption( const TQString& caption )
262{
263 TQWidget::setCaption( caption );
264 if (myFileDialog)
265 myFileDialog->setCaption( caption );
266}
267
268TQString KURLRequester::url() const
269{
270 return d->url();
271}
272
273void KURLRequester::slotOpenDialog()
274{
275 KURL newurl;
276 if ( (d->fileDialogMode & KFile::Directory) && !(d->fileDialogMode & KFile::File) ||
277 /* catch possible fileDialog()->setMode( KFile::Directory ) changes */
278 (myFileDialog && ( (myFileDialog->mode() & KFile::Directory) &&
279 (myFileDialog->mode() & (KFile::File | KFile::Files)) == 0 ) ) )
280 {
281 newurl = KDirSelectDialog::selectDirectory(url(), d->fileDialogMode & KFile::LocalOnly);
282 if ( !newurl.isValid() )
283 {
284 return;
285 }
286 }
287 else
288 {
289 emit openFileDialog( this );
290
291 KFileDialog *dlg = fileDialog();
292 if ( !d->url().isEmpty() ) {
293 KURL u( url() );
294 // If we won't be able to list it (e.g. http), then don't try :)
295 if ( KProtocolInfo::supportsListing( u ) )
296 dlg->setSelection( u.url() );
297 }
298
299 if ( dlg->exec() != TQDialog::Accepted )
300 {
301 return;
302 }
303
304 newurl = dlg->selectedURL();
305 }
306
307 setKURL( newurl );
308 emit urlSelected( d->url() );
309}
310
311void KURLRequester::setMode(uint mode)
312{
313 Q_ASSERT( (mode & KFile::Files) == 0 );
314 d->fileDialogMode = mode;
315 if ( (mode & KFile::Directory) && !(mode & KFile::File) )
316 myCompletion->setMode( KURLCompletion::DirCompletion );
317
318 if (myFileDialog)
319 myFileDialog->setMode( d->fileDialogMode );
320}
321
322unsigned int KURLRequester::mode() const
323{
324 return d->fileDialogMode;
325}
326
327void KURLRequester::setFilter(const TQString &filter)
328{
329 d->fileDialogFilter = filter;
330 if (myFileDialog)
331 myFileDialog->setFilter( d->fileDialogFilter );
332}
333
334TQString KURLRequester::filter( ) const
335{
336 return d->fileDialogFilter;
337}
338
339
340KFileDialog * KURLRequester::fileDialog() const
341{
342 if ( !myFileDialog ) {
343 TQWidget *p = parentWidget();
344 myFileDialog = new KFileDialog( TQString::null, d->fileDialogFilter, p,
345 "file dialog", true );
346
347 myFileDialog->setMode( d->fileDialogMode );
348 myFileDialog->setCaption( caption() );
349 }
350
351 return myFileDialog;
352}
353
354
355void KURLRequester::setShowLocalProtocol( bool b )
356{
357 if ( myShowLocalProt == b )
358 return;
359
360 myShowLocalProt = b;
361 setKURL( url() );
362}
363
364void KURLRequester::clear()
365{
366 d->setText( TQString::null );
367}
368
369KLineEdit * KURLRequester::lineEdit() const
370{
371 return d->edit;
372}
373
374KComboBox * KURLRequester::comboBox() const
375{
376 return d->combo;
377}
378
379void KURLRequester::slotUpdateURL()
380{
381 // bin compat, myButton is declared as QPushButton
382 KURL u;
383 u = KURL( KURL( TQDir::currentDirPath() + '/' ), url() );
384 (static_cast<KURLDragPushButton *>( myButton ))->setURL( u );
385}
386
387bool KURLRequester::eventFilter( TQObject *obj, TQEvent *ev )
388{
389 if ( ( d->edit == obj ) || ( d->combo == obj ) )
390 {
391 if (( ev->type() == TQEvent::FocusIn ) || ( ev->type() == TQEvent::FocusOut ))
392 // Forward focusin/focusout events to the urlrequester; needed by file form element in tdehtml
393 TQApplication::sendEvent( this, ev );
394 }
395 return TQWidget::eventFilter( obj, ev );
396}
397
398KPushButton * KURLRequester::button() const
399{
400 return myButton;
401}
402
403KEditListBox::CustomEditor KURLRequester::customEditor()
404{
405 setSizePolicy(TQSizePolicy( TQSizePolicy::Preferred,
406 TQSizePolicy::Fixed));
407
408 KLineEdit *edit = d->edit;
409 if ( !edit && d->combo )
410 edit = dynamic_cast<KLineEdit*>( d->combo->lineEdit() );
411
412#ifndef NDEBUG
413 if ( !edit )
414 kdWarning() << "KURLRequester's lineedit is not a KLineEdit!??\n";
415#endif
416
417 KEditListBox::CustomEditor editor( this, edit );
418 return editor;
419}
420
421void KURLRequester::virtual_hook( int, void* )
422{ /*BASE::virtual_hook( id, data );*/ }
423
424KURLComboRequester::KURLComboRequester( TQWidget *parent,
425 const char *name )
426 : KURLRequester( new KComboBox(false), parent, name)
427{
428}
429
430#include "kurlrequester.moc"
KDirSelectDialog::selectDirectory
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.
Definition: kdirselectdialog.cpp:461
KFileDialog
Provides a user (and developer) friendly way to select files and directories.
Definition: tdefiledialog.h:77
KFileDialog::mode
KFile::Mode mode() const
Returns the mode of the filedialog.
Definition: tdefiledialog.cpp:1694
KFileDialog::selectedURL
KURL selectedURL() const
Definition: tdefiledialog.cpp:1447
KFileDialog::setFilter
void setFilter(const TQString &filter)
Sets the filter to be used to filter.
Definition: tdefiledialog.cpp:204
KFileDialog::setMode
void setMode(KFile::Mode m)
Convenient overload of the other setMode(unsigned int) method.
Definition: tdefiledialog.cpp:1676
KFileDialog::setSelection
void setSelection(const TQString &name)
Sets the file name to preselect to name.
Definition: tdefiledialog.cpp:1220
KURLComboRequester::KURLComboRequester
KURLComboRequester(TQWidget *parent=0, const char *name=0)
Constructs a KURLRequester widget with a combobox.
Definition: kurlrequester.cpp:424
KURLRequester
This class is a widget showing a lineedit and a button, which invokes a filedialog.
Definition: kurlrequester.h:57
KURLRequester::setKURL
void setKURL(const KURL &url)
Sets the url in the lineedit to url.
Definition: kurlrequester.cpp:253
KURLRequester::setFilter
void setFilter(const TQString &filter)
Sets the filter for the file dialog.
Definition: kurlrequester.cpp:327
KURLRequester::setURL
void setURL(const TQString &url)
Sets the url in the lineedit to url.
Definition: kurlrequester.cpp:235
KURLRequester::fileDialog
virtual KFileDialog * fileDialog() const
Definition: kurlrequester.cpp:340
KURLRequester::clear
void clear()
Clears the lineedit/combobox.
Definition: kurlrequester.cpp:364
KURLRequester::textChanged
void textChanged(const TQString &)
Emitted when the text in the lineedit changes.
KURLRequester::urlSelected
void urlSelected(const TQString &)
Emitted when the user changed the URL via the file dialog.
KURLRequester::comboBox
KComboBox * comboBox() const
Definition: kurlrequester.cpp:374
KURLRequester::setShowLocalProtocol
void setShowLocalProtocol(bool b)
Enables/disables showing file:/ in the lineedit, when a local file has been selected in the filedialo...
Definition: kurlrequester.cpp:355
KURLRequester::setMode
void setMode(uint m)
Sets the mode of the file dialog.
Definition: kurlrequester.cpp:311
KURLRequester::setCaption
virtual void setCaption(const TQString &caption)
Sets the caption of the file dialog.
Definition: kurlrequester.cpp:261
KURLRequester::KURLRequester
KURLRequester(TQWidget *parent=0, const char *name=0)
Constructs a KURLRequester widget.
Definition: kurlrequester.cpp:174
KURLRequester::customEditor
KEditListBox::CustomEditor customEditor()
Definition: kurlrequester.cpp:403
KURLRequester::mode
uint mode() const
Returns the current mode.
Definition: kurlrequester.cpp:322
KURLRequester::returnPressed
void returnPressed()
Emitted when return or enter was pressed in the lineedit.
KURLRequester::openFileDialog
void openFileDialog(KURLRequester *)
Emitted before the filedialog is going to open.
KURLRequester::url
TQString url() const
Definition: kurlrequester.cpp:268
KURLRequester::slotOpenDialog
void slotOpenDialog()
Called when the button is pressed to open the filedialog.
Definition: kurlrequester.cpp:273
KURLRequester::~KURLRequester
~KURLRequester()
Destructs the KURLRequester.
Definition: kurlrequester.cpp:192
KURLRequester::button
KPushButton * button() const
Definition: kurlrequester.cpp:398
KURLRequester::lineEdit
KLineEdit * lineEdit() const
Definition: kurlrequester.cpp:369
KURLRequester::filter
TQString filter() const
Returns the current filter for the file dialog.
Definition: kurlrequester.cpp:334

tdeio/tdefile

Skip menu "tdeio/tdefile"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeio/tdefile

Skip menu "tdeio/tdefile"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdefile by doxygen 1.9.4
This website is maintained by Timothy Pearson.