22 #include "katefilelist.h"
23 #include "katefilelist.moc"
25 #include "katedocmanager.h"
26 #include "kateviewmanager.h"
27 #include "katemainwindow.h"
29 #include <tqapplication.h>
30 #include <tqpainter.h>
31 #include <tqpopupmenu.h>
34 #include <tqcheckbox.h>
37 #include <tqgroupbox.h>
39 #include <tqwhatsthis.h>
41 #include <kiconloader.h>
42 #include <tdeconfig.h>
43 #include <tdelocale.h>
44 #include <tdeglobalsettings.h>
45 #include <kpassivepopup.h>
47 #include <tdeapplication.h>
48 #include <kstringhandler.h>
49 #include <kcolorbutton.h>
54 class ToolTip :
public TQToolTip
57 ToolTip( TQWidget *parent, KateFileList *lv )
58 : TQToolTip( parent ),
62 virtual ~ToolTip() {};
64 void maybeTip(
const TQPoint &pos )
66 TQListViewItem *i = m_listView->itemAt( pos );
69 KateFileListItem *item = ((KateFileListItem*)i);
72 tip( m_listView->itemRect( i ), m_listView->tooltip( item, 0 ) );
77 KateFileList *m_listView;
83 KateFileList::KateFileList (KateMainWindow *main,
84 KateViewManager *_viewManager,
85 TQWidget * parent,
const char * name )
86 : TDEListView (parent, name)
87 , m_sort( KateFileList::sortByID )
90 m_tooltip =
new ToolTip( viewport(),
this );
93 m_viewShade = TQColor( 51, 204, 255 );
94 m_editShade = TQColor( 255, 102, 153 );
95 m_enableBgShading =
false;
97 setFocusPolicy ( TQ_NoFocus );
99 viewManager = _viewManager;
102 addColumn(
"Document Name");
104 setSelectionMode( TQListView::Single );
105 setSortType(KateFileList::sortByID);
106 setShowToolTips(
false );
110 connect(
this,TQT_SIGNAL(moved()),
this,TQT_SLOT(updateFileListLocations()));
112 for (uint i = 0; i < KateDocManager::self()->documents(); i++)
114 slotDocumentCreated (KateDocManager::self()->document(i));
115 slotModChanged (KateDocManager::self()->document(i));
118 connect(KateDocManager::self(),TQT_SIGNAL(documentCreated(Kate::Document *)),
119 this,TQT_SLOT(slotDocumentCreated(Kate::Document *)));
120 connect(KateDocManager::self(),TQT_SIGNAL(documentDeleted(uint)),
121 this,TQT_SLOT(slotDocumentDeleted(uint)));
125 connect(
this,TQT_SIGNAL(selectionChanged(TQListViewItem *)),
126 this,TQT_SLOT(slotActivateView(TQListViewItem *)));
127 connect(viewManager,TQT_SIGNAL(viewChanged()),
this,TQT_SLOT(slotViewChanged()));
128 connect(
this,TQT_SIGNAL(contextMenuRequested( TQListViewItem *,
const TQPoint &,
int )),
129 this,TQT_SLOT(slotMenu ( TQListViewItem *,
const TQPoint &,
int )));
132 KateFileList::~KateFileList ()
137 void KateFileList::setupActions ()
139 windowNext = KStdAction::back(TQT_TQOBJECT(
this), TQT_SLOT(slotPrevDocument()), m_main->actionCollection());
140 windowPrev = KStdAction::forward(TQT_TQOBJECT(
this), TQT_SLOT(slotNextDocument()), m_main->actionCollection());
141 sortAction =
new TDESelectAction( i18n(
"Sort &By"), 0,
142 m_main->actionCollection(),
"filelist_sortby" );
143 listMoveFileUp =
new TDEAction( i18n(
"Move File Up"), 0, m_main->actionCollection(),
"filelist_move_up" );
145 listMoveFileDown =
new TDEAction( i18n(
"Move File Down"), 0, m_main->actionCollection(),
"filelist_move_down" );
147 connect( listMoveFileUp, TQT_SIGNAL(activated()), TQT_TQOBJECT(
this), TQT_SLOT(moveFileUp()) );
148 connect( listMoveFileDown, TQT_SIGNAL(activated()), TQT_TQOBJECT(
this), TQT_SLOT(moveFileDown()) );
150 l << i18n(
"Opening Order") << i18n(
"Document Name") << i18n(
"URL") << i18n(
"Manual Placement");
151 sortAction->setItems( l );
152 connect( sortAction, TQT_SIGNAL(activated(
int)), TQT_TQOBJECT(
this), TQT_SLOT(setSortType(
int)) );
155 void KateFileList::updateActions ()
157 windowNext->setEnabled(KateDocManager::self()->documents() > 1);
158 windowPrev->setEnabled(KateDocManager::self()->documents() > 1);
161 void KateFileList::keyPressEvent(TQKeyEvent *e) {
162 if ( ( e->key() == Key_Return ) || ( e->key() == Key_Enter ) )
165 slotActivateView( currentItem() );
169 TDEListView::keyPressEvent(e);
177 void KateFileList::contentsMousePressEvent( TQMouseEvent *e )
179 m_lastMouseDownPos = e->pos();
181 if ( ! itemAt( contentsToViewport( e->pos() ) ) )
184 TDEListView::contentsMousePressEvent( e );
187 void KateFileList::resizeEvent( TQResizeEvent *e )
189 TDEListView::resizeEvent( e );
196 int w = viewport()->width();
197 if ( columnWidth( 0 ) < w )
198 setColumnWidth( 0, w );
201 void KateFileList::slotNextDocument()
203 if ( ! currentItem() || childCount() == 0 )
208 if ( currentItem()->nextSibling() )
209 viewManager->activateView( ((KateFileListItem*)currentItem()->nextSibling())->documentNumber() );
211 viewManager->activateView( ((KateFileListItem *)firstChild())->documentNumber() );
214 void KateFileList::slotPrevDocument()
216 if ( ! currentItem() || childCount() == 0 )
221 if ( currentItem()->itemAbove() )
222 viewManager->activateView( ((KateFileListItem*)currentItem()->itemAbove())->documentNumber() );
224 viewManager->activateView( ((KateFileListItem *)lastItem())->documentNumber() );
227 void KateFileList::slotDocumentCreated (Kate::Document *doc)
229 new KateFileListItem(
this, doc );
230 connect(doc,TQT_SIGNAL(modStateChanged(Kate::Document *)),
this,TQT_SLOT(slotModChanged(Kate::Document *)));
231 connect(doc,TQT_SIGNAL(nameChanged(Kate::Document *)),
this,TQT_SLOT(slotNameChanged(Kate::Document *)));
232 connect(doc,TQT_SIGNAL(modifiedOnDisc(Kate::Document *,
bool,
unsigned char)),
this,TQT_SLOT(slotModifiedOnDisc(Kate::Document *,
bool,
unsigned char)));
235 updateFileListLocations();
239 void KateFileList::slotDocumentDeleted (uint documentNumber)
241 TQListViewItem * item = firstChild();
243 if ( ((KateFileListItem *)item)->documentNumber() == documentNumber )
252 item = item->nextSibling();
255 updateFileListLocations();
259 void KateFileList::slotActivateView( TQListViewItem *item )
261 if ( ! item || item->rtti() != RTTI_KateFileListItem )
264 KateFileListItem *i = ((KateFileListItem*)item);
265 const KateDocumentInfo *info = KateDocManager::self()->documentInfo(i->document());
267 if (info && info->modifiedOnDisc) {
270 TQMouseEvent e(TQEvent::MouseButtonRelease, m_lastMouseDownPos, Qt::LeftButton, 0);
271 contentsMouseReleaseEvent(&e);
274 viewManager->activateView( i->documentNumber() );
277 void KateFileList::slotModChanged (Kate::Document *doc)
281 TQListViewItem * item = firstChild();
284 if ( ((KateFileListItem *)item)->documentNumber() == doc->documentNumber() )
287 item = item->nextSibling();
290 if ( ((KateFileListItem *)item)->document()->isModified() )
292 m_editHistory.removeRef( (KateFileListItem *)item );
293 m_editHistory.prepend( (KateFileListItem *)item );
295 for ( uint i=0; i < m_editHistory.count(); i++ )
297 m_editHistory.at( i )->setEditHistPos( i+1 );
298 repaintItem( m_editHistory.at( i ) );
305 void KateFileList::slotModifiedOnDisc (Kate::Document *doc,
bool,
unsigned char)
307 slotModChanged( doc );
310 void KateFileList::slotNameChanged (Kate::Document *doc)
316 TQListViewItem * item = firstChild();
318 if ( ((KateFileListItem*)item)->document() == doc )
320 item->setText( 0, doc->docName() );
324 item = item->nextSibling();
329 void KateFileList::slotViewChanged ()
331 if (!viewManager->activeView())
return;
333 Kate::View *view = viewManager->activeView();
334 uint dn = view->getDoc()->documentNumber();
336 TQListViewItem * i = firstChild();
338 if ( ((KateFileListItem *)i)->documentNumber() == dn )
342 i = i->nextSibling();
348 KateFileListItem *item = (KateFileListItem*)i;
349 setCurrentItem( item );
362 m_viewHistory.removeRef( item );
363 m_viewHistory.prepend( item );
365 for ( uint i=0; i < m_viewHistory.count(); i++ )
367 m_viewHistory.at( i )->setViewHistPos( i+1 );
368 repaintItem( m_viewHistory.at( i ) );
371 updateFileListLocations();
374 void KateFileList::updateFileListLocations()
376 TQListViewItem* item = firstChild();
379 Kate::Document* itemDocument = ((KateFileListItem *)item)->document();
380 if (m_sort == KateFileList::sortManual) {
381 if (KateDocManager::self()->findDocument(itemDocument) >= 0) {
382 itemDocument->setDocumentListPosition(i);
386 if (KateDocManager::self()->findDocument(itemDocument) >= 0) {
387 itemDocument->setDocumentListPosition(-1);
390 item = item->itemBelow();
395 void KateFileList::slotMenu ( TQListViewItem *item,
const TQPoint &p,
int )
400 m_clickedMenuItem = item;
401 if (m_clickedMenuItem->itemAbove()) {
402 listMoveFileUp->setEnabled(
true);
405 listMoveFileUp->setEnabled(
false);
407 if (m_clickedMenuItem->itemBelow()) {
408 listMoveFileDown->setEnabled(
true);
411 listMoveFileDown->setEnabled(
false);
414 TQPopupMenu *menu = (TQPopupMenu*) ((viewManager->mainWindow())->factory()->container(
"filelist_popup", viewManager->mainWindow()));
421 TQString KateFileList::tooltip( TQListViewItem *item,
int )
423 KateFileListItem *i = ((KateFileListItem*)item);
424 if ( ! i )
return TQString::null;
427 const KateDocumentInfo *info = KateDocManager::self()->documentInfo(i->document());
429 if (info && info->modifiedOnDisc)
431 if (info->modifiedOnDiscReason == 1)
432 str += i18n(
"<b>This file was changed (modified) on disk by another program.</b><br />");
433 else if (info->modifiedOnDiscReason == 2)
434 str += i18n(
"<b>This file was changed (created) on disk by another program.</b><br />");
435 else if (info->modifiedOnDiscReason == 3)
436 str += i18n(
"<b>This file was changed (deleted) on disk by another program.</b><br />");
439 str += i->document()->url().prettyURL();
444 void KateFileList::setSortType (
int s)
447 if (m_sort == KateFileList::sortManual) {
448 setSorting( -1,
true );
449 setDragEnabled(
true);
450 setAcceptDrops(
true);
453 setSorting( 0,
true );
454 setDragEnabled(
false);
455 setAcceptDrops(
false);
460 void KateFileList::moveFileUp()
462 if (m_clickedMenuItem) {
463 sortAction->setCurrentItem(KateFileList::sortManual);
464 setSortType(KateFileList::sortManual);
465 TQListViewItem* nitemabove = m_clickedMenuItem->itemAbove();
467 nitemabove = nitemabove->itemAbove();
469 m_clickedMenuItem->moveItem(nitemabove);
473 nitemabove = m_clickedMenuItem->itemAbove();
474 nitemabove->moveItem(m_clickedMenuItem);
478 updateFileListLocations();
481 void KateFileList::moveFileDown()
483 if (m_clickedMenuItem) {
484 sortAction->setCurrentItem(KateFileList::sortManual);
485 setSortType(KateFileList::sortManual);
486 TQListViewItem* nitemabove = m_clickedMenuItem->itemBelow();
488 m_clickedMenuItem->moveItem(nitemabove);
491 updateFileListLocations();
494 void KateFileList::updateSort ()
497 updateFileListLocations();
500 void KateFileList::readConfig( TDEConfig *config,
const TQString &group )
502 TQString oldgroup = config->group();
503 config->setGroup( group );
505 setSortType( config->readNumEntry(
"Sort Type", sortByID ) );
506 m_viewShade = config->readColorEntry(
"View Shade", &m_viewShade );
507 m_editShade = config->readColorEntry(
"Edit Shade", &m_editShade );
508 m_enableBgShading = config->readBoolEntry(
"Shading Enabled", &m_enableBgShading );
510 sortAction->setCurrentItem( sortType() );
512 config->setGroup( oldgroup );
515 void KateFileList::writeConfig( TDEConfig *config,
const TQString &group )
517 TQString oldgroup = config->group();
518 config->setGroup( group );
520 config->writeEntry(
"Sort Type", m_sort );
521 config->writeEntry(
"View Shade", m_viewShade );
522 config->writeEntry(
"Edit Shade", m_editShade );
523 config->writeEntry(
"Shading Enabled", m_enableBgShading );
525 config->setGroup( oldgroup );
528 void KateFileList::takeItem( TQListViewItem *item )
530 if ( item->rtti() == RTTI_KateFileListItem )
532 m_editHistory.removeRef( (KateFileListItem*)item );
533 m_viewHistory.removeRef( (KateFileListItem*)item );
535 TQListView::takeItem( item );
540 KateFileListItem::KateFileListItem( TQListView* lv,
541 Kate::Document *_doc )
542 : TQListViewItem( lv, _doc->docName() ),
546 m_docNumber( _doc->documentNumber() )
549 TQListViewItem* lastitem = lv->lastItem();
555 KateFileListItem::~KateFileListItem()
559 const TQPixmap *KateFileListItem::pixmap (
int column )
const
562 static TQPixmap noPm = SmallIcon (
"null");
563 static TQPixmap modPm = SmallIcon(
"modified");
564 static TQPixmap discPm = SmallIcon(
"modonhd");
565 static TQPixmap modmodPm = SmallIcon(
"modmod");
567 const KateDocumentInfo *info = KateDocManager::self()->documentInfo(doc);
569 if (info && info->modifiedOnDisc)
570 return doc->isModified() ? &modmodPm : &discPm;
572 return doc->isModified() ? &modPm : &noPm;
578 void KateFileListItem::paintCell( TQPainter *painter,
const TQColorGroup & cg,
int column,
int width,
int align )
580 KateFileList *fl = (KateFileList*)listView();
585 TQColorGroup cgNew = cg;
588 if ( fl->shadingEnabled() && m_viewhistpos > 1 )
590 TQColor b( cg.base() );
592 TQColor shade = fl->viewShade();
593 TQColor eshade = fl->editShade();
594 int hc = fl->histCount();
597 if ( fl->shadingEnabled() && m_edithistpos > 0 )
599 int ec = fl->editHistCount();
600 int v = hc-m_viewhistpos;
601 int e = ec-m_edithistpos+1;
603 int n = TQMAX(v + e, 1);
605 ((shade.red()*v) + (eshade.red()*e))/n,
606 ((shade.green()*v) + (eshade.green()*e))/n,
607 ((shade.blue()*v) + (eshade.blue()*e))/n
612 float t = (0.5/hc)*(hc-m_viewhistpos+1);
614 (
int)((b.red()*(1-t)) + (shade.red()*t)),
615 (int)((b.green()*(1-t)) + (shade.green()*t)),
616 (int)((b.blue()*(1-t)) + (shade.blue()*t))
619 cgNew.setColor(TQColorGroup::Base, b);
622 TQListViewItem::paintCell( painter, cgNew, column, width, align );
625 TQListViewItem::paintCell( painter, cg, column, width, align );
628 int KateFileListItem::compare ( TQListViewItem * i,
int col,
bool ascending )
const
630 if ( i->rtti() == RTTI_KateFileListItem )
632 switch( ((KateFileList*)listView())->sortType() )
634 case KateFileList::sortByID:
637 int d = (int)doc->documentNumber() - ((KateFileListItem*)i)->documentNumber();
638 return ascending ? d : -d;
641 case KateFileList::sortByURL:
642 return doc->url().prettyURL().compare( ((KateFileListItem*)i)->document()->url().prettyURL() );
645 return TQListViewItem::compare( i, col, ascending );
653 KFLConfigPage::KFLConfigPage( TQWidget* parent,
const char *name, KateFileList *fl )
654 : Kate::ConfigPage( parent, name ),
658 TQVBoxLayout *lo1 =
new TQVBoxLayout(
this );
659 int spacing = KDialog::spacingHint();
660 lo1->setSpacing( spacing );
662 TQGroupBox *gb =
new TQGroupBox( 1, Qt::Horizontal, i18n(
"Background Shading"),
this );
663 lo1->addWidget( gb );
665 TQWidget *g =
new TQWidget( gb );
666 TQGridLayout *lo =
new TQGridLayout( g, 2, 2 );
667 lo->setSpacing( KDialog::spacingHint() );
668 cbEnableShading =
new TQCheckBox( i18n(
"&Enable background shading"), g );
669 lo->addMultiCellWidget( cbEnableShading, 1, 1, 0, 1 );
671 kcbViewShade =
new KColorButton( g );
672 lViewShade =
new TQLabel( kcbViewShade, i18n(
"&Viewed documents' shade:"), g );
673 lo->addWidget( lViewShade, 2, 0 );
674 lo->addWidget( kcbViewShade, 2, 1 );
676 kcbEditShade =
new KColorButton( g );
677 lEditShade =
new TQLabel( kcbEditShade, i18n(
"&Modified documents' shade:"), g );
678 lo->addWidget( lEditShade, 3, 0 );
679 lo->addWidget( kcbEditShade, 3, 1 );
682 TQHBox *hbSorting =
new TQHBox(
this );
683 lo1->addWidget( hbSorting );
684 lSort =
new TQLabel( i18n(
"&Sort by:"), hbSorting );
685 cmbSort =
new TQComboBox( hbSorting );
686 lSort->setBuddy( cmbSort );
688 l << i18n(
"Opening Order") << i18n(
"Document Name") << i18n(
"URL");
689 cmbSort->insertStringList( l );
691 lo1->insertStretch( -1, 10 );
693 TQWhatsThis::add( cbEnableShading, i18n(
694 "When background shading is enabled, documents that have been viewed "
695 "or edited within the current session will have a shaded background. "
696 "The most recent documents have the strongest shade.") );
697 TQWhatsThis::add( kcbViewShade, i18n(
698 "Set the color for shading viewed documents.") );
699 TQWhatsThis::add( kcbEditShade, i18n(
700 "Set the color for modified documents. This color is blended into "
701 "the color for viewed files. The most recently edited documents get "
702 "most of this color.") );
704 TQWhatsThis::add( cmbSort, i18n(
705 "Set the sorting method for the documents.") );
710 connect( cbEnableShading, TQT_SIGNAL(toggled(
bool)),
this, TQT_SLOT(slotMyChanged()) );
711 connect( cbEnableShading, TQT_SIGNAL(toggled(
bool)),
this, TQT_SLOT(slotEnableChanged()) );
712 connect( kcbViewShade, TQT_SIGNAL(changed(
const TQColor&)),
this, TQT_SLOT(slotMyChanged()) );
713 connect( kcbEditShade, TQT_SIGNAL(changed(
const TQColor&)),
this, TQT_SLOT(slotMyChanged()) );
714 connect( cmbSort, TQT_SIGNAL(activated(
int)),
this, TQT_SLOT(slotMyChanged()) );
717 void KFLConfigPage::apply()
724 m_filelist->m_viewShade = kcbViewShade->color();
725 m_filelist->m_editShade = kcbEditShade->color();
726 m_filelist->m_enableBgShading = cbEnableShading->isChecked();
727 m_filelist->setSortType( cmbSort->currentItem() );
729 m_filelist->triggerUpdate();
732 void KFLConfigPage::reload()
735 TDEConfig *config = kapp->config();
736 config->setGroup(
"Filelist" );
737 cbEnableShading->setChecked( config->readBoolEntry(
"Shading Enabled", &m_filelist->m_enableBgShading ) );
738 kcbViewShade->setColor( config->readColorEntry(
"View Shade", &m_filelist->m_viewShade ) );
739 kcbEditShade->setColor( config->readColorEntry(
"Edit Shade", &m_filelist->m_editShade ) );
740 cmbSort->setCurrentItem( m_filelist->sortType() );
744 void KFLConfigPage::slotEnableChanged()
746 kcbViewShade->setEnabled( cbEnableShading->isChecked() );
747 kcbEditShade->setEnabled( cbEnableShading->isChecked() );
748 lViewShade->setEnabled( cbEnableShading->isChecked() );
749 lEditShade->setEnabled( cbEnableShading->isChecked() );
752 void KFLConfigPage::slotMyChanged()