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>
51 #include <kmimetype.h>
55 class ToolTip :
public TQToolTip
58 ToolTip( TQWidget *parent, KateFileList *lv )
59 : TQToolTip( parent ),
63 virtual ~ToolTip() {};
65 void maybeTip(
const TQPoint &pos )
67 TQListViewItem *i = m_listView->itemAt( pos );
70 KateFileListItem *item = ((KateFileListItem*)i);
73 tip( m_listView->itemRect( i ), m_listView->tooltip( item, 0 ) );
78 KateFileList *m_listView;
84 KateFileList::KateFileList (KateMainWindow *main,
85 KateViewManager *_viewManager,
86 TQWidget * parent,
const char * name )
87 : TDEListView (parent, name)
88 , m_sort( KateFileList::sortByID )
91 m_tooltip =
new ToolTip( viewport(),
this );
94 m_viewShade = TQColor( 51, 204, 255 );
95 m_editShade = TQColor( 255, 102, 153 );
96 m_enableBgShading =
false;
98 setFocusPolicy ( TQWidget::NoFocus );
100 viewManager = _viewManager;
103 addColumn(
"Document Name");
105 setSelectionMode( TQListView::Single );
106 setSortType(KateFileList::sortByID);
107 setShowToolTips(
false );
111 connect(
this,TQ_SIGNAL(moved()),
this,TQ_SLOT(updateFileListLocations()));
113 for (uint i = 0; i < KateDocManager::self()->documents(); i++)
115 slotDocumentCreated (KateDocManager::self()->document(i));
116 slotModChanged (KateDocManager::self()->document(i));
119 connect(KateDocManager::self(),TQ_SIGNAL(documentCreated(Kate::Document *)),
120 this,TQ_SLOT(slotDocumentCreated(Kate::Document *)));
121 connect(KateDocManager::self(),TQ_SIGNAL(documentDeleted(uint)),
122 this,TQ_SLOT(slotDocumentDeleted(uint)));
126 connect(
this,TQ_SIGNAL(selectionChanged(TQListViewItem *)),
127 this,TQ_SLOT(slotActivateView(TQListViewItem *)));
128 connect(viewManager,TQ_SIGNAL(viewChanged()),
this,TQ_SLOT(slotViewChanged()));
129 connect(
this,TQ_SIGNAL(contextMenuRequested( TQListViewItem *,
const TQPoint &,
int )),
130 this,TQ_SLOT(slotMenu ( TQListViewItem *,
const TQPoint &,
int )));
133 KateFileList::~KateFileList ()
138 void KateFileList::setupActions ()
140 windowNext = KStdAction::back(
this, TQ_SLOT(slotPrevDocument()), m_main->actionCollection());
141 windowPrev = KStdAction::forward(
this, TQ_SLOT(slotNextDocument()), m_main->actionCollection());
142 sortAction =
new TDESelectAction( i18n(
"Sort &By"), 0,
143 m_main->actionCollection(),
"filelist_sortby" );
144 listMoveFileUp =
new TDEAction( i18n(
"Move File Up"), 0, m_main->actionCollection(),
"filelist_move_up" );
146 listMoveFileDown =
new TDEAction( i18n(
"Move File Down"), 0, m_main->actionCollection(),
"filelist_move_down" );
148 connect( listMoveFileUp, TQ_SIGNAL(activated()),
this, TQ_SLOT(moveFileUp()) );
149 connect( listMoveFileDown, TQ_SIGNAL(activated()),
this, TQ_SLOT(moveFileDown()) );
151 l << i18n(
"Opening Order") << i18n(
"Document Name") << i18n(
"URL") << i18n(
"Manual Placement");
152 sortAction->setItems( l );
153 connect( sortAction, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(setSortType(
int)) );
156 void KateFileList::updateActions ()
158 windowNext->setEnabled(KateDocManager::self()->documents() > 1);
159 windowPrev->setEnabled(KateDocManager::self()->documents() > 1);
162 void KateFileList::keyPressEvent(TQKeyEvent *e) {
163 if ( ( e->key() == Key_Return ) || ( e->key() == Key_Enter ) )
166 slotActivateView( currentItem() );
170 TDEListView::keyPressEvent(e);
178 void KateFileList::contentsMousePressEvent( TQMouseEvent *e )
180 if (e->button() != TQMouseEvent::LeftButton)
return;
181 m_lastMouseDownPos = e->pos();
183 if ( ! itemAt( contentsToViewport( e->pos() ) ) )
186 TDEListView::contentsMousePressEvent( e );
189 void KateFileList::resizeEvent( TQResizeEvent *e )
191 TDEListView::resizeEvent( e );
198 int w = viewport()->width();
199 if ( columnWidth( 0 ) < w )
200 setColumnWidth( 0, w );
203 void KateFileList::slotNextDocument()
205 if ( ! currentItem() || childCount() == 0 )
210 if ( currentItem()->nextSibling() )
211 viewManager->activateView( ((KateFileListItem*)currentItem()->nextSibling())->documentNumber() );
213 viewManager->activateView( ((KateFileListItem *)firstChild())->documentNumber() );
216 void KateFileList::slotPrevDocument()
218 if ( ! currentItem() || childCount() == 0 )
223 if ( currentItem()->itemAbove() )
224 viewManager->activateView( ((KateFileListItem*)currentItem()->itemAbove())->documentNumber() );
226 viewManager->activateView( ((KateFileListItem *)lastItem())->documentNumber() );
229 void KateFileList::slotDocumentCreated (Kate::Document *doc)
231 new KateFileListItem(
this, doc );
232 connect(doc,TQ_SIGNAL(modStateChanged(Kate::Document *)),
this,TQ_SLOT(slotModChanged(Kate::Document *)));
233 connect(doc,TQ_SIGNAL(nameChanged(Kate::Document *)),
this,TQ_SLOT(slotNameChanged(Kate::Document *)));
234 connect(doc,TQ_SIGNAL(modifiedOnDisc(Kate::Document *,
bool,
unsigned char)),
this,TQ_SLOT(slotModifiedOnDisc(Kate::Document *,
bool,
unsigned char)));
237 updateFileListLocations();
241 void KateFileList::slotDocumentDeleted (uint documentNumber)
243 TQListViewItem * item = firstChild();
245 if ( ((KateFileListItem *)item)->documentNumber() == documentNumber )
254 item = item->nextSibling();
257 updateFileListLocations();
261 void KateFileList::slotActivateView( TQListViewItem *item )
263 if ( ! item || item->rtti() != RTTI_KateFileListItem )
266 KateFileListItem *i = ((KateFileListItem*)item);
267 const KateDocumentInfo *info = KateDocManager::self()->documentInfo(i->document());
269 if (info && info->modifiedOnDisc) {
272 TQMouseEvent e(TQEvent::MouseButtonRelease, m_lastMouseDownPos, TQt::LeftButton, 0);
273 contentsMouseReleaseEvent(&e);
276 viewManager->activateView( i->documentNumber() );
279 void KateFileList::slotModChanged (Kate::Document *doc)
283 TQListViewItem * item = firstChild();
286 if ( ((KateFileListItem *)item)->documentNumber() == doc->documentNumber() )
289 item = item->nextSibling();
292 if ( ((KateFileListItem *)item)->document()->isModified() )
294 m_editHistory.removeRef( (KateFileListItem *)item );
295 m_editHistory.prepend( (KateFileListItem *)item );
297 for ( uint i=0; i < m_editHistory.count(); i++ )
299 m_editHistory.at( i )->setEditHistPos( i+1 );
300 repaintItem( m_editHistory.at( i ) );
307 void KateFileList::slotModifiedOnDisc (Kate::Document *doc,
bool,
unsigned char)
309 slotModChanged( doc );
312 void KateFileList::slotNameChanged (Kate::Document *doc)
318 TQListViewItem * item = firstChild();
320 if ( ((KateFileListItem*)item)->document() == doc )
322 item->setText( 0, doc->docName() );
326 item = item->nextSibling();
331 void KateFileList::slotViewChanged ()
333 if (!viewManager->activeView())
return;
335 Kate::View *view = viewManager->activeView();
336 uint dn = view->getDoc()->documentNumber();
338 TQListViewItem * i = firstChild();
340 if ( ((KateFileListItem *)i)->documentNumber() == dn )
344 i = i->nextSibling();
350 KateFileListItem *item = (KateFileListItem*)i;
351 setCurrentItem( item );
364 m_viewHistory.removeRef( item );
365 m_viewHistory.prepend( item );
367 for ( uint i=0; i < m_viewHistory.count(); i++ )
369 m_viewHistory.at( i )->setViewHistPos( i+1 );
370 repaintItem( m_viewHistory.at( i ) );
373 updateFileListLocations();
376 void KateFileList::updateFileListLocations()
378 TQListViewItem* item = firstChild();
381 Kate::Document* itemDocument = ((KateFileListItem *)item)->document();
382 if (m_sort == KateFileList::sortManual) {
383 if (KateDocManager::self()->findDocument(itemDocument) >= 0) {
384 itemDocument->setDocumentListPosition(i);
388 if (KateDocManager::self()->findDocument(itemDocument) >= 0) {
389 itemDocument->setDocumentListPosition(-1);
392 item = item->itemBelow();
397 void KateFileList::slotMenu ( TQListViewItem *item,
const TQPoint &p,
int )
402 m_clickedMenuItem = item;
403 if (m_clickedMenuItem->itemAbove()) {
404 listMoveFileUp->setEnabled(
true);
407 listMoveFileUp->setEnabled(
false);
409 if (m_clickedMenuItem->itemBelow()) {
410 listMoveFileDown->setEnabled(
true);
413 listMoveFileDown->setEnabled(
false);
416 TQPopupMenu *menu = (TQPopupMenu*) ((viewManager->mainWindow())->factory()->container(
"filelist_popup", viewManager->mainWindow()));
423 TQString KateFileList::tooltip( TQListViewItem *item,
int )
425 KateFileListItem *i = ((KateFileListItem*)item);
426 if ( ! i )
return TQString::null;
429 const KateDocumentInfo *info = KateDocManager::self()->documentInfo(i->document());
431 if (info && info->modifiedOnDisc)
433 if (info->modifiedOnDiscReason == 1)
434 str += i18n(
"<b>This file was changed (modified) on disk by another program.</b><br />");
435 else if (info->modifiedOnDiscReason == 2)
436 str += i18n(
"<b>This file was changed (created) on disk by another program.</b><br />");
437 else if (info->modifiedOnDiscReason == 3)
438 str += i18n(
"<b>This file was changed (deleted) on disk by another program.</b><br />");
441 str += i->document()->url().pathOrURL();
446 void KateFileList::setSortType (
int s)
449 if (m_sort == KateFileList::sortManual) {
450 setSorting( -1,
true );
451 setDragEnabled(
true);
452 setAcceptDrops(
true);
455 setSorting( 0,
true );
456 setDragEnabled(
false);
457 setAcceptDrops(
false);
462 void KateFileList::moveFileUp()
464 if (m_clickedMenuItem) {
465 sortAction->setCurrentItem(KateFileList::sortManual);
466 setSortType(KateFileList::sortManual);
467 TQListViewItem* nitemabove = m_clickedMenuItem->itemAbove();
469 nitemabove = nitemabove->itemAbove();
471 m_clickedMenuItem->moveItem(nitemabove);
475 nitemabove = m_clickedMenuItem->itemAbove();
476 nitemabove->moveItem(m_clickedMenuItem);
480 updateFileListLocations();
483 void KateFileList::moveFileDown()
485 if (m_clickedMenuItem) {
486 sortAction->setCurrentItem(KateFileList::sortManual);
487 setSortType(KateFileList::sortManual);
488 TQListViewItem* nitemabove = m_clickedMenuItem->itemBelow();
490 m_clickedMenuItem->moveItem(nitemabove);
493 updateFileListLocations();
496 void KateFileList::updateSort ()
499 updateFileListLocations();
502 void KateFileList::readConfig( TDEConfig *config,
const TQString &group )
504 TQString oldgroup = config->group();
505 config->setGroup( group );
507 setSortType( config->readNumEntry(
"Sort Type", sortByID ) );
508 m_viewShade = config->readColorEntry(
"View Shade", &m_viewShade );
509 m_editShade = config->readColorEntry(
"Edit Shade", &m_editShade );
510 m_enableBgShading = config->readBoolEntry(
"Shading Enabled", &m_enableBgShading );
512 sortAction->setCurrentItem( sortType() );
514 config->setGroup( oldgroup );
517 void KateFileList::writeConfig( TDEConfig *config,
const TQString &group )
519 TQString oldgroup = config->group();
520 config->setGroup( group );
522 config->writeEntry(
"Sort Type", m_sort );
523 config->writeEntry(
"View Shade", m_viewShade );
524 config->writeEntry(
"Edit Shade", m_editShade );
525 config->writeEntry(
"Shading Enabled", m_enableBgShading );
527 config->setGroup( oldgroup );
530 void KateFileList::takeItem( TQListViewItem *item )
532 if ( item->rtti() == RTTI_KateFileListItem )
534 m_editHistory.removeRef( (KateFileListItem*)item );
535 m_viewHistory.removeRef( (KateFileListItem*)item );
537 TQListView::takeItem( item );
542 KateFileListItem::KateFileListItem( TQListView* lv,
543 Kate::Document *_doc )
544 : TQListViewItem( lv, _doc->docName() ),
548 m_docNumber( _doc->documentNumber() )
551 TQListViewItem* lastitem = lv->lastItem();
557 KateFileListItem::~KateFileListItem()
562 const TQPixmap *KateFileListItem::pixmap (
int column )
const
565 static TQMap<TQString, TQPixmap> mimeIcons;
566 static TQPixmap modPm = SmallIcon(
"modified");
567 static TQPixmap discPm = SmallIcon(
"modonhd");
568 static TQPixmap modmodPm = SmallIcon(
"modmod");
570 const KateDocumentInfo *info = KateDocManager::self()->documentInfo(doc);
571 KMimeType::Ptr mime = KMimeType::findByURL(doc->url());
573 if (!mimeIcons.contains(mime->name()))
575 mimeIcons.insert(mime->name(), mime->pixmap(TDEIcon::Small));
578 if (info && info->modifiedOnDisc)
579 return doc->isModified() ? &modmodPm : &discPm;
581 return doc->isModified() ? &modPm : &mimeIcons[mime->name()];
587 void KateFileListItem::paintCell( TQPainter *painter,
const TQColorGroup & cg,
int column,
int width,
int align )
589 KateFileList *fl = (KateFileList*)listView();
594 TQColorGroup cgNew = cg;
597 if ( fl->shadingEnabled() && m_viewhistpos > 1 )
599 TQColor b( cg.base() );
601 TQColor shade = fl->viewShade();
602 TQColor eshade = fl->editShade();
603 int hc = fl->histCount();
606 if ( fl->shadingEnabled() && m_edithistpos > 0 )
608 int ec = fl->editHistCount();
609 int v = hc-m_viewhistpos;
610 int e = ec-m_edithistpos+1;
612 int n = TQMAX(v + e, 1);
614 ((shade.red()*v) + (eshade.red()*e))/n,
615 ((shade.green()*v) + (eshade.green()*e))/n,
616 ((shade.blue()*v) + (eshade.blue()*e))/n
621 float t = (0.5/hc)*(hc-m_viewhistpos+1);
623 (
int)((b.red()*(1-t)) + (shade.red()*t)),
624 (int)((b.green()*(1-t)) + (shade.green()*t)),
625 (int)((b.blue()*(1-t)) + (shade.blue()*t))
628 cgNew.setColor(TQColorGroup::Base, b);
631 TQListViewItem::paintCell( painter, cgNew, column, width, align );
634 TQListViewItem::paintCell( painter, cg, column, width, align );
637 int KateFileListItem::compare ( TQListViewItem * i,
int col,
bool ascending )
const
639 if ( i->rtti() == RTTI_KateFileListItem )
641 switch( ((KateFileList*)listView())->sortType() )
643 case KateFileList::sortByID:
646 int d = (int)doc->documentNumber() - ((KateFileListItem*)i)->documentNumber();
647 return ascending ? d : -d;
650 case KateFileList::sortByURL:
651 return doc->url().prettyURL().compare( ((KateFileListItem*)i)->document()->url().prettyURL() );
654 return TQListViewItem::compare( i, col, ascending );
662 KFLConfigPage::KFLConfigPage( TQWidget* parent,
const char *name, KateFileList *fl )
663 :
Kate::ConfigPage( parent, name ),
667 TQVBoxLayout *lo1 =
new TQVBoxLayout(
this );
668 int spacing = KDialog::spacingHint();
669 lo1->setSpacing( spacing );
671 TQGroupBox *gb =
new TQGroupBox( 1, TQt::Horizontal, i18n(
"Background Shading"),
this );
672 lo1->addWidget( gb );
674 TQWidget *g =
new TQWidget( gb );
675 TQGridLayout *lo =
new TQGridLayout( g, 2, 2 );
676 lo->setSpacing( KDialog::spacingHint() );
677 cbEnableShading =
new TQCheckBox( i18n(
"&Enable background shading"), g );
678 lo->addMultiCellWidget( cbEnableShading, 1, 1, 0, 1 );
680 kcbViewShade =
new KColorButton( g );
681 lViewShade =
new TQLabel( kcbViewShade, i18n(
"&Viewed documents' shade:"), g );
682 lo->addWidget( lViewShade, 2, 0 );
683 lo->addWidget( kcbViewShade, 2, 1 );
685 kcbEditShade =
new KColorButton( g );
686 lEditShade =
new TQLabel( kcbEditShade, i18n(
"&Modified documents' shade:"), g );
687 lo->addWidget( lEditShade, 3, 0 );
688 lo->addWidget( kcbEditShade, 3, 1 );
691 TQHBox *hbSorting =
new TQHBox(
this );
692 lo1->addWidget( hbSorting );
693 lSort =
new TQLabel( i18n(
"&Sort by:"), hbSorting );
694 cmbSort =
new TQComboBox( hbSorting );
695 lSort->setBuddy( cmbSort );
697 l << i18n(
"Opening Order") << i18n(
"Document Name") << i18n(
"URL");
698 cmbSort->insertStringList( l );
700 lo1->insertStretch( -1, 10 );
702 TQWhatsThis::add( cbEnableShading, i18n(
703 "When background shading is enabled, documents that have been viewed "
704 "or edited within the current session will have a shaded background. "
705 "The most recent documents have the strongest shade.") );
706 TQWhatsThis::add( kcbViewShade, i18n(
707 "Set the color for shading viewed documents.") );
708 TQWhatsThis::add( kcbEditShade, i18n(
709 "Set the color for modified documents. This color is blended into "
710 "the color for viewed files. The most recently edited documents get "
711 "most of this color.") );
713 TQWhatsThis::add( cmbSort, i18n(
714 "Set the sorting method for the documents.") );
719 connect( cbEnableShading, TQ_SIGNAL(toggled(
bool)),
this, TQ_SLOT(slotMyChanged()) );
720 connect( cbEnableShading, TQ_SIGNAL(toggled(
bool)),
this, TQ_SLOT(slotEnableChanged()) );
721 connect( kcbViewShade, TQ_SIGNAL(changed(
const TQColor&)),
this, TQ_SLOT(slotMyChanged()) );
722 connect( kcbEditShade, TQ_SIGNAL(changed(
const TQColor&)),
this, TQ_SLOT(slotMyChanged()) );
723 connect( cmbSort, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(slotMyChanged()) );
726 void KFLConfigPage::apply()
733 m_filelist->m_viewShade = kcbViewShade->color();
734 m_filelist->m_editShade = kcbEditShade->color();
735 m_filelist->m_enableBgShading = cbEnableShading->isChecked();
736 m_filelist->setSortType( cmbSort->currentItem() );
738 m_filelist->triggerUpdate();
741 void KFLConfigPage::reload()
744 TDEConfig *config = kapp->config();
745 config->setGroup(
"Filelist" );
746 cbEnableShading->setChecked( config->readBoolEntry(
"Shading Enabled", &m_filelist->m_enableBgShading ) );
747 kcbViewShade->setColor( config->readColorEntry(
"View Shade", &m_filelist->m_viewShade ) );
748 kcbEditShade->setColor( config->readColorEntry(
"Edit Shade", &m_filelist->m_editShade ) );
749 cmbSort->setCurrentItem( m_filelist->sortType() );
753 void KFLConfigPage::slotEnableChanged()
755 kcbViewShade->setEnabled( cbEnableShading->isChecked() );
756 kcbEditShade->setEnabled( cbEnableShading->isChecked() );
757 lViewShade->setEnabled( cbEnableShading->isChecked() );
758 lEditShade->setEnabled( cbEnableShading->isChecked() );
761 void KFLConfigPage::slotMyChanged()
Namespace collecting as much of the internal Kate classes as we can manage.