22 #include "katefileselector.h"
23 #include "katefileselector.moc"
25 #include "katemainwindow.h"
26 #include "kateviewmanager.h"
27 #include "kbookmarkhandler.h"
29 #include "tdeactionselector.h"
32 #include <tqtoolbutton.h>
36 #include <tqstrlist.h>
37 #include <tqtooltip.h>
38 #include <tqwhatsthis.h>
39 #include <tqapplication.h>
40 #include <tqlistbox.h>
41 #include <tqscrollbar.h>
42 #include <tqspinbox.h>
43 #include <tqgroupbox.h>
44 #include <tqcheckbox.h>
46 #include <tqdockarea.h>
50 #include <tdeapplication.h>
51 #include <kiconloader.h>
52 #include <kurlcombobox.h>
53 #include <kurlcompletion.h>
54 #include <kprotocolinfo.h>
55 #include <tdediroperator.h>
56 #include <tdeconfig.h>
57 #include <tdelocale.h>
58 #include <kcombobox.h>
59 #include <tdeaction.h>
60 #include <tdemessagebox.h>
61 #include <tdetoolbarbutton.h>
62 #include <tqtoolbar.h>
63 #include <tdepopupmenu.h>
70 static void silenceQToolBar(TQtMsgType,
const char *){}
73 KateFileSelectorToolBar::KateFileSelectorToolBar(TQWidget *parent)
74 : TDEToolBar( parent,
"Kate FileSelector Toolbar", true )
79 KateFileSelectorToolBar::~KateFileSelectorToolBar(){}
81 void KateFileSelectorToolBar::setMovingEnabled(
bool)
83 TDEToolBar::setMovingEnabled(
false);
87 KateFileSelectorToolBarParent::KateFileSelectorToolBarParent(TQWidget *parent)
88 :TQFrame(parent),m_tb(0){}
89 KateFileSelectorToolBarParent::~KateFileSelectorToolBarParent(){}
90 void KateFileSelectorToolBarParent::setToolBar(KateFileSelectorToolBar *tb)
95 void KateFileSelectorToolBarParent::resizeEvent ( TQResizeEvent * )
99 setMinimumHeight(m_tb->sizeHint().height());
100 m_tb->resize(width(),height());
107 KateFileSelector::KateFileSelector( KateMainWindow *mainWindow,
108 KateViewManager *viewManager,
109 TQWidget * parent,
const char * name )
110 : TQVBox (parent, name),
112 viewmanager(viewManager)
114 mActionCollection =
new TDEActionCollection(
this );
116 TQtMsgHandler oldHandler = tqInstallMsgHandler( silenceQToolBar );
118 KateFileSelectorToolBarParent *tbp=
new KateFileSelectorToolBarParent(
this);
119 toolbar =
new KateFileSelectorToolBar(tbp);
120 tbp->setToolBar(toolbar);
121 toolbar->setMovingEnabled(
false);
122 toolbar->setFlat(
true);
123 tqInstallMsgHandler( oldHandler );
125 cmbPath =
new KURLComboBox( KURLComboBox::Directories,
true,
this,
"path combo" );
126 cmbPath->setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed ));
127 KURLCompletion* cmpl =
new KURLCompletion(KURLCompletion::DirCompletion);
128 cmbPath->setCompletionObject( cmpl );
129 cmbPath->setAutoDeleteCompletionObject(
true );
130 cmbPath->listBox()->installEventFilter(
this );
132 dir =
new KDirOperator(KURL(),
this,
"operator");
133 dir->setView(KFile::Detail);
134 dir->view()->setSelectionMode(KFile::Extended);
135 connect ( dir, TQT_SIGNAL( viewChanged(KFileView *) ),
136 this, TQT_SLOT( selectorViewChanged(KFileView *) ) );
137 setStretchFactor(dir, 2);
139 TDEActionCollection *coll = dir->actionCollection();
141 coll->action(
"delete" )->setShortcut( TDEShortcut( ALT + Key_Delete ) );
142 coll->action(
"reload" )->setShortcut( TDEShortcut( ALT + Key_F5 ) );
143 coll->action(
"back" )->setShortcut( TDEShortcut( ALT + SHIFT + Key_Left ) );
144 coll->action(
"forward" )->setShortcut( TDEShortcut( ALT + SHIFT + Key_Right ) );
146 coll->action(
"up" )->setShortcut( TDEShortcut( ALT + SHIFT + Key_Up ) );
147 coll->action(
"home" )->setShortcut( TDEShortcut( CTRL + ALT + Key_Home ) );
150 TDEActionMenu *acmBookmarks =
new TDEActionMenu( i18n(
"Bookmarks"),
"bookmark",
151 mActionCollection,
"bookmarks" );
152 acmBookmarks->setDelayed(
false );
153 bookmarkHandler =
new KBookmarkHandler(
this, acmBookmarks->popupMenu() );
154 TQHBox* filterBox =
new TQHBox(
this);
156 btnFilter =
new TQToolButton( filterBox );
157 btnFilter->setIconSet( SmallIconSet(
"filter" ) );
158 btnFilter->setToggleButton(
true );
159 filter =
new KHistoryCombo(
true, filterBox,
"filter");
160 filter->setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed ));
161 filterBox->setStretchFactor(filter, 2);
162 connect( btnFilter, TQT_SIGNAL( clicked() ),
this, TQT_SLOT( btnFilterClick() ) );
164 connect( filter, TQT_SIGNAL( activated(
const TQString&) ),
165 TQT_SLOT( slotFilterChange(
const TQString&) ) );
166 connect( filter, TQT_SIGNAL( returnPressed(
const TQString&) ),
167 filter, TQT_SLOT( addToHistory(
const TQString&) ) );
170 acSyncDir =
new TDEAction( i18n(
"Current Document Folder"),
"curfiledir", 0,
171 TQT_TQOBJECT(
this), TQT_SLOT( setActiveDocumentDir() ), mActionCollection,
"sync_dir" );
172 toolbar->setIconText( TDEToolBar::IconOnly );
173 toolbar->setIconSize( 16 );
174 toolbar->setEnableContextMenu(
false );
176 connect( cmbPath, TQT_SIGNAL( urlActivated(
const KURL& )),
177 this, TQT_SLOT( cmbPathActivated(
const KURL& ) ));
178 connect( cmbPath, TQT_SIGNAL( returnPressed(
const TQString& )),
179 this, TQT_SLOT( cmbPathReturnPressed(
const TQString& ) ));
180 connect(dir, TQT_SIGNAL(urlEntered(
const KURL&)),
181 this, TQT_SLOT(dirUrlEntered(
const KURL&)) );
183 connect(dir, TQT_SIGNAL(finishedLoading()),
184 this, TQT_SLOT(dirFinishedLoading()) );
187 connect ( viewmanager, TQT_SIGNAL( viewChanged() ),
188 this, TQT_SLOT( kateViewChanged() ) );
191 connect( bookmarkHandler, TQT_SIGNAL( openURL(
const TQString& )),
192 this, TQT_SLOT( setDir(
const TQString& ) ) );
194 waitingUrl = TQString::null;
197 TQWhatsThis::add( cmbPath,
198 i18n(
"<p>Here you can enter a path for a folder to display."
199 "<p>To go to a folder previously entered, press the arrow on "
200 "the right and choose one. <p>The entry has folder "
201 "completion. Right-click to choose how completion should behave.") );
202 TQWhatsThis::add( filter,
203 i18n(
"<p>Here you can enter a name filter to limit which files are displayed."
204 "<p>To clear the filter, toggle off the filter button to the left."
205 "<p>To reapply the last filter used, toggle on the filter button." ) );
206 TQWhatsThis::add( btnFilter,
207 i18n(
"<p>This button clears the name filter when toggled off, or "
208 "reapplies the last filter used when toggled on.") );
212 KateFileSelector::~KateFileSelector()
219 void KateFileSelector::readConfig(TDEConfig *config,
const TQString & name)
221 dir->setViewConfig( config, name +
":view" );
222 dir->readConfig(config, name +
":dir");
223 dir->setView( KFile::Default );
224 dir->view()->setSelectionMode(KFile::Extended);
225 config->setGroup( name );
228 setupToolbar( config );
230 cmbPath->setMaxItems( config->readNumEntry(
"pathcombo history len", 9 ) );
231 cmbPath->setURLs( config->readPathListEntry(
"dir history" ) );
233 if ( config->readBoolEntry(
"restore location",
true ) || kapp->isRestored() ) {
234 TQString loc( config->readPathEntry(
"location" ) );
235 if ( ! loc.isEmpty() ) {
244 filter->setMaxCount( config->readNumEntry(
"filter history len", 9 ) );
245 filter->setHistoryItems( config->readListEntry(
"filter history"), true );
246 lastFilter = config->readEntry(
"last filter" );
248 if ( config->readBoolEntry(
"restore last filter",
true ) || kapp->isRestored() )
249 flt = config->readEntry(
"current filter");
250 filter->lineEdit()->setText( flt );
251 slotFilterChange( flt );
253 autoSyncEvents = config->readNumEntry(
"AutoSyncEvents", 0 );
256 void KateFileSelector::initialDirChangeHack()
258 setDir( waitingDir );
261 void KateFileSelector::setupToolbar( TDEConfig *config )
264 TQStringList tbactions = config->readListEntry(
"toolbar actions",
',' );
265 if ( tbactions.isEmpty() ) {
267 tbactions <<
"up" <<
"back" <<
"forward" <<
"home" <<
268 "short view" <<
"detailed view" <<
269 "bookmarks" <<
"sync_dir";
272 for ( TQStringList::Iterator it=tbactions.begin(); it != tbactions.end(); ++it ) {
273 if ( *it ==
"bookmarks" || *it ==
"sync_dir" )
274 ac = mActionCollection->action( (*it).latin1() );
276 ac = dir->actionCollection()->action( (*it).latin1() );
282 void KateFileSelector::writeConfig(TDEConfig *config,
const TQString & name)
284 dir->writeConfig(config,name +
":dir");
286 config->setGroup( name );
287 config->writeEntry(
"pathcombo history len", cmbPath->maxItems() );
289 for (
int i = 0; i < cmbPath->count(); i++) {
290 l.append( cmbPath->text( i ) );
292 config->writePathEntry(
"dir history", l );
293 config->writePathEntry(
"location", cmbPath->currentText() );
295 config->writeEntry(
"filter history len", filter->maxCount() );
296 config->writeEntry(
"filter history", filter->historyItems() );
297 config->writeEntry(
"current filter", filter->currentText() );
298 config->writeEntry(
"last filter", lastFilter );
299 config->writeEntry(
"AutoSyncEvents", autoSyncEvents );
302 void KateFileSelector::setView(KFile::FileView view)
305 dir->view()->setSelectionMode(KFile::Extended);
312 void KateFileSelector::slotFilterChange(
const TQString & nf )
314 TQString f = nf.stripWhiteSpace();
315 bool empty = f.isEmpty() || f ==
"*";
316 TQToolTip::remove( btnFilter );
319 filter->lineEdit()->setText( TQString::null );
320 TQToolTip::add( btnFilter,
321 TQString( i18n(
"Apply last filter (\"%1\")") ).arg( lastFilter ) );
324 dir->setNameFilter( f );
326 TQToolTip::add( btnFilter, i18n(
"Clear filter") );
328 btnFilter->setOn( !empty );
331 btnFilter->setEnabled( !( empty && lastFilter.isEmpty() ) );
335 bool kateFileSelectorIsReadable (
const KURL& url )
337 if ( !url.isLocalFile() )
340 TQDir dir (url.path());
341 return dir.exists ();
344 void KateFileSelector::setDir( KURL u )
349 newurl.setPath( TQDir::homeDirPath() );
353 TQString pathstr = newurl.path(+1);
354 newurl.setPath(pathstr);
356 if ( !kateFileSelectorIsReadable ( newurl ) )
357 newurl.cd(TQString::fromLatin1(
".."));
359 if ( !kateFileSelectorIsReadable (newurl) )
360 newurl.setPath( TQDir::homeDirPath() );
362 dir->setURL(newurl,
true);
369 void KateFileSelector::cmbPathActivated(
const KURL& u )
371 cmbPathReturnPressed( u.url() );
374 void KateFileSelector::cmbPathReturnPressed(
const TQString& u )
377 if ( typedURL.hasPass() )
378 typedURL.setPass( TQString::null );
380 TQStringList urls = cmbPath->urls();
381 urls.remove( typedURL.url() );
382 urls.prepend( typedURL.url() );
383 cmbPath->setURLs( urls, KURLComboBox::RemoveBottom );
385 dir->setURL( KURL(u),
true );
388 void KateFileSelector::dirUrlEntered(
const KURL& u )
390 cmbPath->setURL( u );
393 void KateFileSelector::dirFinishedLoading()
405 void KateFileSelector::btnFilterClick()
407 if ( !btnFilter->isOn() ) {
408 slotFilterChange( TQString::null );
411 filter->lineEdit()->setText( lastFilter );
412 slotFilterChange( lastFilter );
417 void KateFileSelector::setActiveDocumentDir()
420 KURL u = mainwin->activeDocumentUrl();
427 void KateFileSelector::kateViewChanged()
429 if ( autoSyncEvents & DocumentChanged )
434 setActiveDocumentDir();
435 waitingUrl = TQString::null;
439 KURL u = mainwin->activeDocumentUrl();
441 waitingUrl = u.directory();
447 acSyncDir->setEnabled( ! mainwin->activeDocumentUrl().directory().isEmpty() );
450 void KateFileSelector::selectorViewChanged( KFileView * newView )
452 newView->setSelectionMode(KFile::Extended);
459 void KateFileSelector::focusInEvent( TQFocusEvent * )
464 void KateFileSelector::showEvent( TQShowEvent * )
467 if ( autoSyncEvents & GotVisible ) {
469 setActiveDocumentDir();
470 waitingUrl = TQString::null;
473 else if ( ! waitingUrl.isEmpty() ) {
474 setDir( waitingUrl );
475 waitingUrl = TQString::null;
479 bool KateFileSelector::eventFilter( TQObject* o, TQEvent *e )
488 TQListBox *lb = cmbPath->listBox();
489 if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(lb) && e->type() == TQEvent::Show ) {
490 int add = lb->height() < lb->contentsHeight() ? lb->verticalScrollBar()->width() : 0;
491 int w = TQMIN( mainwin->width(), lb->contentsWidth() + add );
492 lb->resize( w, lb->height() );
498 return TQWidget::eventFilter( o, e );
508 class ActionLBItem :
public TQListBoxPixmap {
510 ActionLBItem( TQListBox *lb=0,
511 const TQPixmap &pm = TQPixmap(),
512 const TQString &text=TQString::null,
513 const TQString &str=TQString::null ) :
514 TQListBoxPixmap( lb, pm, text ),
516 TQString idstring() {
return _str; };
526 KFSConfigPage::KFSConfigPage( TQWidget *parent,
const char *name, KateFileSelector *kfs )
527 : Kate::ConfigPage( parent, name ),
531 TQVBoxLayout *lo =
new TQVBoxLayout(
this );
532 int spacing = KDialog::spacingHint();
533 lo->setSpacing( spacing );
536 TQGroupBox *gbToolbar =
new TQGroupBox( 1, Qt::Vertical, i18n(
"Toolbar"),
this );
537 acSel =
new TDEActionSelector( gbToolbar );
538 acSel->setAvailableLabel( i18n(
"A&vailable actions:") );
539 acSel->setSelectedLabel( i18n(
"S&elected actions:") );
540 lo->addWidget( gbToolbar );
541 connect( acSel, TQT_SIGNAL( added( TQListBoxItem * ) ),
this, TQT_SLOT( slotMyChanged() ) );
542 connect( acSel, TQT_SIGNAL( removed( TQListBoxItem * ) ),
this, TQT_SLOT( slotMyChanged() ) );
543 connect( acSel, TQT_SIGNAL( movedUp( TQListBoxItem * ) ),
this, TQT_SLOT( slotMyChanged() ) );
544 connect( acSel, TQT_SIGNAL( movedDown( TQListBoxItem * ) ),
this, TQT_SLOT( slotMyChanged() ) );
547 TQGroupBox *gbSync =
new TQGroupBox( 1, Qt::Horizontal, i18n(
"Auto Synchronization"),
this );
548 cbSyncActive =
new TQCheckBox( i18n(
"When a docu&ment becomes active"), gbSync );
549 cbSyncShow =
new TQCheckBox( i18n(
"When the file selector becomes visible"), gbSync );
550 lo->addWidget( gbSync );
551 connect( cbSyncActive, TQT_SIGNAL( toggled(
bool ) ),
this, TQT_SLOT( slotMyChanged() ) );
552 connect( cbSyncShow, TQT_SIGNAL( toggled(
bool ) ),
this, TQT_SLOT( slotMyChanged() ) );
555 TQHBox *hbPathHist =
new TQHBox (
this );
556 TQLabel *lbPathHist =
new TQLabel( i18n(
"Remember &locations:"), hbPathHist );
557 sbPathHistLength =
new TQSpinBox( hbPathHist );
558 lbPathHist->setBuddy( sbPathHistLength );
559 lo->addWidget( hbPathHist );
560 connect( sbPathHistLength, TQT_SIGNAL( valueChanged (
int ) ),
this, TQT_SLOT( slotMyChanged() ) );
562 TQHBox *hbFilterHist =
new TQHBox (
this );
563 TQLabel *lbFilterHist =
new TQLabel( i18n(
"Remember &filters:"), hbFilterHist );
564 sbFilterHistLength =
new TQSpinBox( hbFilterHist );
565 lbFilterHist->setBuddy( sbFilterHistLength );
566 lo->addWidget( hbFilterHist );
567 connect( sbFilterHistLength, TQT_SIGNAL( valueChanged (
int ) ),
this, TQT_SLOT( slotMyChanged() ) );
570 TQGroupBox *gbSession =
new TQGroupBox( 1, Qt::Horizontal, i18n(
"Session"),
this );
571 cbSesLocation =
new TQCheckBox( i18n(
"Restore loca&tion"), gbSession );
572 cbSesFilter =
new TQCheckBox( i18n(
"Restore last f&ilter"), gbSession );
573 lo->addWidget( gbSession );
574 connect( cbSesLocation, TQT_SIGNAL( toggled(
bool ) ),
this, TQT_SLOT( slotMyChanged() ) );
575 connect( cbSesFilter, TQT_SIGNAL( toggled(
bool ) ),
this, TQT_SLOT( slotMyChanged() ) );
592 "<p>Decides how many locations to keep in the history of the location "
594 TQWhatsThis::add( lbPathHist, lhwt );
595 TQWhatsThis::add( sbPathHistLength, lhwt );
597 "<p>Decides how many filters to keep in the history of the filter "
599 TQWhatsThis::add( lbFilterHist, fhwt );
600 TQWhatsThis::add( sbFilterHistLength, fhwt );
601 TQString synwt( i18n(
602 "<p>These options allow you to have the File Selector automatically "
603 "change location to the folder of the active document on certain "
605 "<p>Auto synchronization is <em>lazy</em>, meaning it will not take "
606 "effect until the file selector is visible."
607 "<p>None of these are enabled by default, but you can always sync the "
608 "location by pressing the sync button in the toolbar.") );
609 TQWhatsThis::add( gbSync, synwt );
610 TQWhatsThis::add( cbSesLocation, i18n(
611 "<p>If this option is enabled (default), the location will be restored "
612 "when you start Kate.<p><strong>Note</strong> that if the session is "
613 "handled by the TDE session manager, the location is always restored.") );
614 TQWhatsThis::add( cbSesFilter, i18n(
615 "<p>If this option is enabled (default), the current filter will be "
616 "restored when you start Kate.<p><strong>Note</strong> that if the "
617 "session is handled by the TDE session manager, the filter is always "
619 "<p><strong>Note</strong> that some of the autosync settings may "
620 "override the restored location if on.") );
626 void KFSConfigPage::apply()
633 TDEConfig *config = kapp->config();
634 config->setGroup(
"fileselector" );
637 TQListBoxItem *item = acSel->selectedListBox()->firstItem();
641 aItem = (ActionLBItem*)item;
644 l << aItem->idstring();
648 config->writeEntry(
"toolbar actions", l );
649 fileSelector->setupToolbar( config );
652 if ( cbSyncActive->isChecked() )
653 s |= KateFileSelector::DocumentChanged;
654 if ( cbSyncShow->isChecked() )
655 s |= KateFileSelector::GotVisible;
656 fileSelector->autoSyncEvents = s;
659 fileSelector->cmbPath->setMaxItems( sbPathHistLength->value() );
660 fileSelector->filter->setMaxCount( sbFilterHistLength->value() );
663 config->writeEntry(
"restore location", cbSesLocation->isChecked() );
664 config->writeEntry(
"restore last filter", cbSesFilter->isChecked() );
667 void KFSConfigPage::reload()
673 void KFSConfigPage::init()
675 TDEConfig *config = kapp->config();
676 config->setGroup(
"fileselector" );
678 TQStringList l = config->readListEntry(
"toolbar actions",
',' );
680 l <<
"up" <<
"back" <<
"forward" <<
"home" <<
681 "short view" <<
"detailed view" <<
682 "bookmarks" <<
"sync_dir";
685 TQStringList allActions;
686 allActions <<
"up" <<
"back" <<
"forward" <<
"home" <<
687 "reload" <<
"mkdir" <<
"delete" <<
688 "short view" <<
"detailed view"
690 "bookmarks" <<
"sync_dir";
691 TQRegExp re(
"&(?=[^&])");
694 for ( TQStringList::Iterator it=allActions.begin(); it != allActions.end(); ++it ) {
695 lb = l.contains( *it ) ? acSel->selectedListBox() : acSel->availableListBox();
696 if ( *it ==
"bookmarks" || *it ==
"sync_dir" )
697 ac = fileSelector->actionCollection()->action( (*it).latin1() );
699 ac = fileSelector->dirOperator()->actionCollection()->action( (*it).latin1() );
701 new ActionLBItem( lb, SmallIcon( ac->icon() ), ac->text().replace( re,
"" ), *it );
705 int s = fileSelector->autoSyncEvents;
706 cbSyncActive->setChecked( s & KateFileSelector::DocumentChanged );
707 cbSyncShow->setChecked( s & KateFileSelector::GotVisible );
709 sbPathHistLength->setValue( fileSelector->cmbPath->maxItems() );
710 sbFilterHistLength->setValue( fileSelector->filter->maxCount() );
712 cbSesLocation->setChecked( config->readBoolEntry(
"restore location",
true ) );
713 cbSesFilter->setChecked( config->readBoolEntry(
"restore last filter",
true ) );
716 void KFSConfigPage::slotMyChanged()