00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#include <sys/stat.h>
00021
#include <unistd.h>
00022
00023
#include <qstring.h>
00024
#include <qtooltip.h>
00025
00026
#include <kaccel.h>
00027
#include <kcombobox.h>
00028
#include <kdebug.h>
00029
#include <kdialog.h>
00030
#include <kdirselectdialog.h>
00031
#include <kfiledialog.h>
00032
#include <kglobal.h>
00033
#include <kiconloader.h>
00034
#include <klineedit.h>
00035
#include <klocale.h>
00036
#include <kurlcompletion.h>
00037
#include <kurldrag.h>
00038
#include <kprotocolinfo.h>
00039
00040
#include "kurlrequester.h"
00041
00042
00043
class KURLDragPushButton :
public KPushButton
00044 {
00045
public:
00046 KURLDragPushButton(
QWidget *parent,
const char *name=0 )
00047 :
KPushButton( parent,
name ) {
00048
setDragEnabled(
true );
00049 }
00050 ~KURLDragPushButton() {}
00051
00052
void setURL(
const KURL& url ) {
00053 m_urls.clear();
00054 m_urls.append( url );
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064
protected:
00065
virtual QDragObject *
dragObject() {
00066
if ( m_urls.isEmpty() )
00067
return 0L;
00068
00069
QDragObject *drag =
new KURLDrag( m_urls,
this,
"url drag" );
00070
return drag;
00071 }
00072
00073
private:
00074
KURL::List m_urls;
00075
00076 };
00077
00078
00079
00080
00081
00082
00083
class KURLRequester::KURLRequesterPrivate
00084 {
00085
public:
00086 KURLRequesterPrivate() {
00087 edit = 0L;
00088 combo = 0L;
00089 fileDialogMode = KFile::File | KFile::ExistingOnly | KFile::LocalOnly;
00090 }
00091
00092
void setText(
const QString& text ) {
00093
if ( combo )
00094 {
00095
if (combo->editable())
00096 {
00097 combo->setEditText( text );
00098 }
00099
else
00100 {
00101 combo->insertItem( text );
00102 combo->setCurrentItem( combo->count()-1 );
00103 }
00104 }
00105
else
00106 {
00107 edit->setText( text );
00108 }
00109 }
00110
00111
void connectSignals(
QObject *receiver ) {
00112
QObject *sender;
00113
if ( combo )
00114 sender = combo;
00115
else
00116 sender = edit;
00117
00118 connect( sender, SIGNAL(
textChanged(
const QString& )),
00119 receiver, SIGNAL(
textChanged(
const QString& )));
00120 connect( sender, SIGNAL(
returnPressed() ),
00121 receiver, SIGNAL(
returnPressed() ));
00122 connect( sender, SIGNAL(
returnPressed(
const QString& ) ),
00123 receiver, SIGNAL(
returnPressed(
const QString& ) ));
00124 }
00125
00126
void setCompletionObject(
KCompletion *comp ) {
00127
if ( combo )
00128 combo->setCompletionObject( comp );
00129
else
00130 edit->setCompletionObject( comp );
00131 }
00132
00136
QString url() {
00137
QString txt = combo ? combo->currentText() : edit->text();
00138
KURLCompletion *comp;
00139
if ( combo )
00140 comp = dynamic_cast<KURLCompletion*>(combo->completionObject());
00141
else
00142 comp = dynamic_cast<KURLCompletion*>(edit->completionObject());
00143
00144
if ( comp )
00145
return comp->
replacedPath( txt );
00146
else
00147
return txt;
00148 }
00149
00150
KLineEdit *edit;
00151
KComboBox *combo;
00152
int fileDialogMode;
00153
QString fileDialogFilter;
00154 };
00155
00156
00157
00158 KURLRequester::KURLRequester(
QWidget *editWidget,
QWidget *parent,
00159
const char *name )
00160 :
QHBox( parent, name )
00161 {
00162 d =
new KURLRequesterPrivate;
00163
00164
00165 editWidget->reparent(
this, 0,
QPoint(0,0) );
00166 d->edit = dynamic_cast<KLineEdit*>( editWidget );
00167 d->combo = dynamic_cast<KComboBox*>( editWidget );
00168
00169 init();
00170 }
00171
00172
00173 KURLRequester::KURLRequester(
QWidget *parent,
const char *name )
00174 :
QHBox( parent, name )
00175 {
00176 d =
new KURLRequesterPrivate;
00177 init();
00178 }
00179
00180
00181 KURLRequester::KURLRequester(
const QString& url,
QWidget *parent,
00182
const char *name )
00183 :
QHBox( parent, name )
00184 {
00185 d =
new KURLRequesterPrivate;
00186 init();
00187
setURL( url );
00188 }
00189
00190
00191 KURLRequester::~KURLRequester()
00192 {
00193
delete myCompletion;
00194
delete myFileDialog;
00195
delete d;
00196 }
00197
00198
00199
void KURLRequester::init()
00200 {
00201 myFileDialog = 0L;
00202 myShowLocalProt =
false;
00203
00204
if ( !d->combo && !d->edit )
00205 d->edit =
new KLineEdit(
this,
"line edit" );
00206
00207 myButton =
new KURLDragPushButton(
this,
"kfile button");
00208
QIconSet iconSet = SmallIconSet(QString::fromLatin1(
"fileopen"));
00209
QPixmap pixMap = iconSet.pixmap( QIconSet::Small, QIconSet::Normal );
00210 myButton->setIconSet( iconSet );
00211 myButton->setFixedSize( pixMap.width()+8, pixMap.height()+8 );
00212 QToolTip::add(myButton, i18n(
"Open file dialog"));
00213
00214 connect( myButton, SIGNAL( pressed() ), SLOT( slotUpdateURL() ));
00215
00216 setSpacing( KDialog::spacingHint() );
00217
00218
QWidget *widget = d->combo ? (
QWidget*) d->combo : (
QWidget*) d->edit;
00219 setFocusProxy( widget );
00220
00221 d->connectSignals(
this );
00222 connect( myButton, SIGNAL( clicked() ),
this, SLOT(
slotOpenDialog() ));
00223
00224 myCompletion =
new KURLCompletion();
00225 d->setCompletionObject( myCompletion );
00226
00227
KAccel *accel =
new KAccel(
this );
00228 accel->
insert( KStdAccel::Open,
this, SLOT(
slotOpenDialog() ));
00229 accel->
readSettings();
00230 }
00231
00232
00233 void KURLRequester::setURL(
const QString& url )
00234 {
00235
bool hasLocalPrefix = (url.startsWith(
"file:"));
00236
00237
if ( !myShowLocalProt && hasLocalPrefix )
00238 d->setText( url.mid( 5, url.length()-5 ));
00239
else
00240 d->setText( url );
00241 }
00242
00243 void KURLRequester::setCaption(
const QString& caption )
00244 {
00245 QWidget::setCaption( caption );
00246
if (myFileDialog)
00247 myFileDialog->
setCaption( caption );
00248 }
00249
00250
QString KURLRequester::url()
const
00251
{
00252
return d->url();
00253 }
00254
00255
00256 void KURLRequester::slotOpenDialog()
00257 {
00258
KURL newurl;
00259
if ( (d->fileDialogMode & KFile::Directory) && !(d->fileDialogMode & KFile::File) ||
00260
00261 (myFileDialog && ( (myFileDialog->
mode() & KFile::Directory) &&
00262 (myFileDialog->
mode() & (KFile::File | KFile::Files)) == 0 ) ) )
00263 {
00264 newurl =
KDirSelectDialog::selectDirectory(
url(), d->fileDialogMode & KFile::LocalOnly);
00265
if ( !newurl.
isValid() )
00266 {
00267
return;
00268 }
00269 }
00270
else
00271 {
00272 emit
openFileDialog(
this );
00273
00274
KFileDialog *dlg =
fileDialog();
00275
if ( !d->url().isEmpty() ) {
00276
KURL u(
url() );
00277
00278
if (
KProtocolInfo::supportsListing( u ) )
00279 dlg->
setSelection( u.
url() );
00280 }
00281
00282
if ( dlg->exec() != QDialog::Accepted )
00283 {
00284
return;
00285 }
00286
00287 newurl = dlg->
selectedURL();
00288 }
00289
00290
if ( newurl.
isLocalFile() )
00291 {
00292
setURL( newurl.
path() );
00293 }
00294
else
00295 {
00296
setURL( newurl.
prettyURL() );
00297 }
00298 emit
urlSelected( d->url() );
00299 }
00300
00301
void KURLRequester::setMode(
unsigned int mode)
00302 {
00303 Q_ASSERT( (mode & KFile::Files) == 0 );
00304 d->fileDialogMode = mode;
00305
if ( (mode & KFile::Directory) && !(mode & KFile::File) )
00306 myCompletion->
setMode( KURLCompletion::DirCompletion );
00307
00308
if (myFileDialog)
00309 myFileDialog->
setMode( d->fileDialogMode );
00310 }
00311
00312
unsigned int KURLRequester::mode()
const
00313
{
00314
return d->fileDialogMode;
00315 }
00316
00317 void KURLRequester::setFilter(
const QString &filter)
00318 {
00319 d->fileDialogFilter = filter;
00320
if (myFileDialog)
00321 myFileDialog->
setFilter( d->fileDialogFilter );
00322 }
00323
00324
QString KURLRequester::filter( )
const
00325
{
00326
return d->fileDialogFilter;
00327 }
00328
00329
00330 KFileDialog *
KURLRequester::fileDialog()
const
00331
{
00332
if ( !myFileDialog ) {
00333
QWidget *p = parentWidget();
00334 myFileDialog =
new KFileDialog( QString::null, QString::null, p,
00335
"file dialog",
true );
00336
00337 myFileDialog->
setMode( d->fileDialogMode );
00338 myFileDialog->
setFilter( d->fileDialogFilter );
00339 myFileDialog->
setCaption( caption() );
00340 }
00341
00342
return myFileDialog;
00343 }
00344
00345
00346 void KURLRequester::setShowLocalProtocol(
bool b )
00347 {
00348
if ( myShowLocalProt == b )
00349
return;
00350
00351 myShowLocalProt = b;
00352
setURL(
url() );
00353 }
00354
00355 void KURLRequester::clear()
00356 {
00357 d->setText( QString::null );
00358 }
00359
00360 KLineEdit *
KURLRequester::lineEdit()
const
00361
{
00362
return d->edit;
00363 }
00364
00365 KComboBox *
KURLRequester::comboBox()
const
00366
{
00367
return d->combo;
00368 }
00369
00370
void KURLRequester::slotUpdateURL()
00371 {
00372
00373
KURL u;
00374 u =
KURL(
KURL( QDir::currentDirPath() +
'/' ),
url() );
00375 (static_cast<KURLDragPushButton *>( myButton))->setURL( u );
00376 }
00377
00378 KPushButton *
KURLRequester::button()
const
00379
{
00380
return myButton;
00381 }
00382
00383 KEditListBox::CustomEditor KURLRequester::customEditor()
00384 {
00385 setSizePolicy(
QSizePolicy( QSizePolicy::Preferred,
00386 QSizePolicy::Fixed));
00387
00388
KLineEdit *edit = d->edit;
00389
if ( !edit && d->combo )
00390 edit = dynamic_cast<KLineEdit*>( d->combo->lineEdit() );
00391
00392
#ifndef NDEBUG
00393
if ( !edit )
00394
kdWarning() <<
"KURLRequester's lineedit is not a KLineEdit!??\n";
00395
#endif
00396
00397
KEditListBox::CustomEditor editor(
this, edit );
00398
return editor;
00399 }
00400
00401
void KURLRequester::virtual_hook(
int,
void* )
00402 { }
00403
00404 KURLComboRequester::KURLComboRequester(
QWidget *parent,
00405
const char *name )
00406 :
KURLRequester( new
KComboBox(false), parent,
name)
00407 {
00408 }
00409
00410
#include "kurlrequester.moc"