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 = qInstallMsgHandler( 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 qInstallMsgHandler( 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, TQ_SIGNAL( viewChanged(KFileView *) ),
136 this, TQ_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, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( btnFilterClick() ) );
164 connect( filter, TQ_SIGNAL( activated(
const TQString&) ),
165 TQ_SLOT( slotFilterChange(
const TQString&) ) );
166 connect( filter, TQ_SIGNAL( returnPressed(
const TQString&) ),
167 filter, TQ_SLOT( addToHistory(
const TQString&) ) );
170 acSyncDir =
new TDEAction( i18n(
"Current Document Folder"),
"curfiledir", 0,
171 this, TQ_SLOT( setActiveDocumentDir() ), mActionCollection,
"sync_dir" );
172 toolbar->setIconText( TDEToolBar::IconOnly );
173 toolbar->setIconSize( 16 );
174 toolbar->setEnableContextMenu(
false );
176 connect( cmbPath, TQ_SIGNAL( urlActivated(
const KURL& )),
177 this, TQ_SLOT( cmbPathActivated(
const KURL& ) ));
178 connect( cmbPath, TQ_SIGNAL( returnPressed(
const TQString& )),
179 this, TQ_SLOT( cmbPathReturnPressed(
const TQString& ) ));
180 connect(dir, TQ_SIGNAL(urlEntered(
const KURL&)),
181 this, TQ_SLOT(dirUrlEntered(
const KURL&)) );
183 connect(dir, TQ_SIGNAL(finishedLoading()),
184 this, TQ_SLOT(dirFinishedLoading()) );
187 connect ( viewmanager, TQ_SIGNAL( viewChanged() ),
188 this, TQ_SLOT( kateViewChanged() ) );
191 connect( bookmarkHandler, TQ_SIGNAL( openURL(
const TQString& )),
192 this, TQ_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 );
416 void KateFileSelector::setActiveDocumentDir()
419 KURL u = mainwin->activeDocumentUrl();
426 void KateFileSelector::kateViewChanged()
428 if ( autoSyncEvents & DocumentChanged )
433 setActiveDocumentDir();
434 waitingUrl = TQString::null;
438 KURL u = mainwin->activeDocumentUrl();
440 waitingUrl = u.directory();
446 acSyncDir->setEnabled( ! mainwin->activeDocumentUrl().directory().isEmpty() );
449 void KateFileSelector::selectorViewChanged( KFileView * newView )
451 newView->setSelectionMode(KFile::Extended);
458 void KateFileSelector::focusInEvent( TQFocusEvent * )
463 void KateFileSelector::showEvent( TQShowEvent * )
466 if ( autoSyncEvents & GotVisible ) {
468 setActiveDocumentDir();
469 waitingUrl = TQString::null;
472 else if ( ! waitingUrl.isEmpty() ) {
473 setDir( waitingUrl );
474 waitingUrl = TQString::null;
478 bool KateFileSelector::eventFilter( TQObject* o, TQEvent *e )
487 TQListBox *lb = cmbPath->listBox();
488 if ( o == lb && e->type() == TQEvent::Show ) {
489 int add = lb->height() < lb->contentsHeight() ? lb->verticalScrollBar()->width() : 0;
490 int w = TQMIN( mainwin->width(), lb->contentsWidth() + add );
491 lb->resize( w, lb->height() );
497 return TQWidget::eventFilter( o, e );
507 class ActionLBItem :
public TQListBoxPixmap {
509 ActionLBItem( TQListBox *lb=0,
510 const TQPixmap &pm = TQPixmap(),
511 const TQString &text=TQString::null,
512 const TQString &str=TQString::null ) :
513 TQListBoxPixmap( lb, pm, text ),
515 TQString idstring() {
return _str; };
525 KFSConfigPage::KFSConfigPage( TQWidget *parent,
const char *name, KateFileSelector *kfs )
526 :
Kate::ConfigPage( parent, name ),
530 TQVBoxLayout *lo =
new TQVBoxLayout(
this );
531 int spacing = KDialog::spacingHint();
532 lo->setSpacing( spacing );
535 TQGroupBox *gbToolbar =
new TQGroupBox( 1, TQt::Vertical, i18n(
"Toolbar"),
this );
536 acSel =
new TDEActionSelector( gbToolbar );
537 acSel->setAvailableLabel( i18n(
"A&vailable actions:") );
538 acSel->setSelectedLabel( i18n(
"S&elected actions:") );
539 lo->addWidget( gbToolbar );
540 connect( acSel, TQ_SIGNAL( added( TQListBoxItem * ) ),
this, TQ_SLOT( slotMyChanged() ) );
541 connect( acSel, TQ_SIGNAL( removed( TQListBoxItem * ) ),
this, TQ_SLOT( slotMyChanged() ) );
542 connect( acSel, TQ_SIGNAL( movedUp( TQListBoxItem * ) ),
this, TQ_SLOT( slotMyChanged() ) );
543 connect( acSel, TQ_SIGNAL( movedDown( TQListBoxItem * ) ),
this, TQ_SLOT( slotMyChanged() ) );
546 TQGroupBox *gbSync =
new TQGroupBox( 1, TQt::Horizontal, i18n(
"Auto Synchronization"),
this );
547 cbSyncActive =
new TQCheckBox( i18n(
"When a docu&ment becomes active"), gbSync );
548 cbSyncShow =
new TQCheckBox( i18n(
"When the file selector becomes visible"), gbSync );
549 lo->addWidget( gbSync );
550 connect( cbSyncActive, TQ_SIGNAL( toggled(
bool ) ),
this, TQ_SLOT( slotMyChanged() ) );
551 connect( cbSyncShow, TQ_SIGNAL( toggled(
bool ) ),
this, TQ_SLOT( slotMyChanged() ) );
554 TQHBox *hbPathHist =
new TQHBox (
this );
555 TQLabel *lbPathHist =
new TQLabel( i18n(
"Remember &locations:"), hbPathHist );
556 sbPathHistLength =
new TQSpinBox( hbPathHist );
557 lbPathHist->setBuddy( sbPathHistLength );
558 lo->addWidget( hbPathHist );
559 connect( sbPathHistLength, TQ_SIGNAL( valueChanged (
int ) ),
this, TQ_SLOT( slotMyChanged() ) );
561 TQHBox *hbFilterHist =
new TQHBox (
this );
562 TQLabel *lbFilterHist =
new TQLabel( i18n(
"Remember &filters:"), hbFilterHist );
563 sbFilterHistLength =
new TQSpinBox( hbFilterHist );
564 lbFilterHist->setBuddy( sbFilterHistLength );
565 lo->addWidget( hbFilterHist );
566 connect( sbFilterHistLength, TQ_SIGNAL( valueChanged (
int ) ),
this, TQ_SLOT( slotMyChanged() ) );
569 TQGroupBox *gbSession =
new TQGroupBox( 1, TQt::Horizontal, i18n(
"Session"),
this );
570 cbSesLocation =
new TQCheckBox( i18n(
"Restore loca&tion"), gbSession );
571 cbSesFilter =
new TQCheckBox( i18n(
"Restore last f&ilter"), gbSession );
572 lo->addWidget( gbSession );
573 connect( cbSesLocation, TQ_SIGNAL( toggled(
bool ) ),
this, TQ_SLOT( slotMyChanged() ) );
574 connect( cbSesFilter, TQ_SIGNAL( toggled(
bool ) ),
this, TQ_SLOT( slotMyChanged() ) );
591 "<p>Decides how many locations to keep in the history of the location "
593 TQWhatsThis::add( lbPathHist, lhwt );
594 TQWhatsThis::add( sbPathHistLength, lhwt );
596 "<p>Decides how many filters to keep in the history of the filter "
598 TQWhatsThis::add( lbFilterHist, fhwt );
599 TQWhatsThis::add( sbFilterHistLength, fhwt );
600 TQString synwt( i18n(
601 "<p>These options allow you to have the File Selector automatically "
602 "change location to the folder of the active document on certain "
604 "<p>Auto synchronization is <em>lazy</em>, meaning it will not take "
605 "effect until the file selector is visible."
606 "<p>None of these are enabled by default, but you can always sync the "
607 "location by pressing the sync button in the toolbar.") );
608 TQWhatsThis::add( gbSync, synwt );
609 TQWhatsThis::add( cbSesLocation, i18n(
610 "<p>If this option is enabled (default), the location will be restored "
611 "when you start Kate.<p><strong>Note</strong> that if the session is "
612 "handled by the TDE session manager, the location is always restored.") );
613 TQWhatsThis::add( cbSesFilter, i18n(
614 "<p>If this option is enabled (default), the current filter will be "
615 "restored when you start Kate.<p><strong>Note</strong> that if the "
616 "session is handled by the TDE session manager, the filter is always "
618 "<p><strong>Note</strong> that some of the autosync settings may "
619 "override the restored location if on.") );
625 void KFSConfigPage::apply()
632 TDEConfig *config = kapp->config();
633 config->setGroup(
"fileselector" );
636 TQListBoxItem *item = acSel->selectedListBox()->firstItem();
640 aItem = (ActionLBItem*)item;
643 l << aItem->idstring();
647 config->writeEntry(
"toolbar actions", l );
648 fileSelector->setupToolbar( config );
651 if ( cbSyncActive->isChecked() )
652 s |= KateFileSelector::DocumentChanged;
653 if ( cbSyncShow->isChecked() )
654 s |= KateFileSelector::GotVisible;
655 fileSelector->autoSyncEvents = s;
658 fileSelector->cmbPath->setMaxItems( sbPathHistLength->value() );
659 fileSelector->filter->setMaxCount( sbFilterHistLength->value() );
662 config->writeEntry(
"restore location", cbSesLocation->isChecked() );
663 config->writeEntry(
"restore last filter", cbSesFilter->isChecked() );
666 void KFSConfigPage::reload()
672 void KFSConfigPage::init()
674 TDEConfig *config = kapp->config();
675 config->setGroup(
"fileselector" );
677 TQStringList l = config->readListEntry(
"toolbar actions",
',' );
679 l <<
"up" <<
"back" <<
"forward" <<
"home" <<
680 "short view" <<
"detailed view" <<
681 "bookmarks" <<
"sync_dir";
684 TQStringList allActions;
685 allActions <<
"up" <<
"back" <<
"forward" <<
"home" <<
686 "reload" <<
"mkdir" <<
"delete" <<
687 "short view" <<
"detailed view"
689 "bookmarks" <<
"sync_dir";
690 TQRegExp re(
"&(?=[^&])");
693 for ( TQStringList::Iterator it=allActions.begin(); it != allActions.end(); ++it ) {
694 lb = l.contains( *it ) ? acSel->selectedListBox() : acSel->availableListBox();
695 if ( *it ==
"bookmarks" || *it ==
"sync_dir" )
696 ac = fileSelector->actionCollection()->action( (*it).latin1() );
698 ac = fileSelector->dirOperator()->actionCollection()->action( (*it).latin1() );
700 new ActionLBItem( lb, SmallIcon( ac->icon() ), ac->text().replace( re,
"" ), *it );
704 int s = fileSelector->autoSyncEvents;
705 cbSyncActive->setChecked( s & KateFileSelector::DocumentChanged );
706 cbSyncShow->setChecked( s & KateFileSelector::GotVisible );
708 sbPathHistLength->setValue( fileSelector->cmbPath->maxItems() );
709 sbFilterHistLength->setValue( fileSelector->filter->maxCount() );
711 cbSesLocation->setChecked( config->readBoolEntry(
"restore location",
true ) );
712 cbSesFilter->setChecked( config->readBoolEntry(
"restore last filter",
true ) );
715 void KFSConfigPage::slotMyChanged()
Namespace collecting as much of the internal Kate classes as we can manage.