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>
44 #include <kglobalsettings.h>
45 #include <kpassivepopup.h>
47 #include <kapplication.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 : KListView (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 for (uint i = 0; i < KateDocManager::self()->documents(); i++)
112 slotDocumentCreated (KateDocManager::self()->document(i));
113 slotModChanged (KateDocManager::self()->document(i));
116 connect(KateDocManager::self(),TQT_SIGNAL(documentCreated(Kate::Document *)),
117 this,TQT_SLOT(slotDocumentCreated(Kate::Document *)));
118 connect(KateDocManager::self(),TQT_SIGNAL(documentDeleted(uint)),
119 this,TQT_SLOT(slotDocumentDeleted(uint)));
123 connect(
this,TQT_SIGNAL(selectionChanged(TQListViewItem *)),
124 this,TQT_SLOT(slotActivateView(TQListViewItem *)));
125 connect(viewManager,TQT_SIGNAL(viewChanged()),
this,TQT_SLOT(slotViewChanged()));
126 connect(
this,TQT_SIGNAL(contextMenuRequested( TQListViewItem *,
const TQPoint &,
int )),
127 this,TQT_SLOT(slotMenu ( TQListViewItem *,
const TQPoint &,
int )));
130 KateFileList::~KateFileList ()
135 void KateFileList::setupActions ()
137 windowNext = KStdAction::back(TQT_TQOBJECT(
this), TQT_SLOT(slotPrevDocument()), m_main->actionCollection());
138 windowPrev = KStdAction::forward(TQT_TQOBJECT(
this), TQT_SLOT(slotNextDocument()), m_main->actionCollection());
139 sortAction =
new KSelectAction( i18n(
"Sort &By"), 0,
140 m_main->actionCollection(),
"filelist_sortby" );
141 listMoveFileUp =
new KAction( i18n(
"Move File Up"), 0, m_main->actionCollection(),
"filelist_move_up" );
143 listMoveFileDown =
new KAction( i18n(
"Move File Down"), 0, m_main->actionCollection(),
"filelist_move_down" );
145 connect( listMoveFileUp, TQT_SIGNAL(activated()), TQT_TQOBJECT(
this), TQT_SLOT(moveFileUp()) );
146 connect( listMoveFileDown, TQT_SIGNAL(activated()), TQT_TQOBJECT(
this), TQT_SLOT(moveFileDown()) );
148 l << i18n(
"Opening Order") << i18n(
"Document Name") << i18n(
"URL") << i18n(
"Manual Placement");
149 sortAction->setItems( l );
150 connect( sortAction, TQT_SIGNAL(activated(
int)), TQT_TQOBJECT(
this), TQT_SLOT(setSortType(
int)) );
153 void KateFileList::updateActions ()
155 windowNext->setEnabled(KateDocManager::self()->documents() > 1);
156 windowPrev->setEnabled(KateDocManager::self()->documents() > 1);
159 void KateFileList::keyPressEvent(TQKeyEvent *e) {
160 if ( ( e->key() == Key_Return ) || ( e->key() == Key_Enter ) )
163 slotActivateView( currentItem() );
167 KListView::keyPressEvent(e);
175 void KateFileList::contentsMousePressEvent( TQMouseEvent *e )
177 if ( ! itemAt( contentsToViewport( e->pos() ) ) )
180 KListView::contentsMousePressEvent( e );
183 void KateFileList::resizeEvent( TQResizeEvent *e )
185 KListView::resizeEvent( e );
192 int w = viewport()->width();
193 if ( columnWidth( 0 ) < w )
194 setColumnWidth( 0, w );
197 void KateFileList::slotNextDocument()
199 if ( ! currentItem() || childCount() == 0 )
204 if ( currentItem()->nextSibling() )
205 viewManager->activateView( ((KateFileListItem*)currentItem()->nextSibling())->documentNumber() );
207 viewManager->activateView( ((KateFileListItem *)firstChild())->documentNumber() );
210 void KateFileList::slotPrevDocument()
212 if ( ! currentItem() || childCount() == 0 )
217 if ( currentItem()->itemAbove() )
218 viewManager->activateView( ((KateFileListItem*)currentItem()->itemAbove())->documentNumber() );
220 viewManager->activateView( ((KateFileListItem *)lastItem())->documentNumber() );
223 void KateFileList::slotDocumentCreated (Kate::Document *doc)
225 new KateFileListItem(
this, doc );
226 connect(doc,TQT_SIGNAL(modStateChanged(Kate::Document *)),
this,TQT_SLOT(slotModChanged(Kate::Document *)));
227 connect(doc,TQT_SIGNAL(nameChanged(Kate::Document *)),
this,TQT_SLOT(slotNameChanged(Kate::Document *)));
228 connect(doc,TQT_SIGNAL(modifiedOnDisc(Kate::Document *,
bool,
unsigned char)),
this,TQT_SLOT(slotModifiedOnDisc(Kate::Document *,
bool,
unsigned char)));
234 void KateFileList::slotDocumentDeleted (uint documentNumber)
236 TQListViewItem * item = firstChild();
238 if ( ((KateFileListItem *)item)->documentNumber() == documentNumber )
247 item = item->nextSibling();
253 void KateFileList::slotActivateView( TQListViewItem *item )
255 if ( ! item || item->rtti() != RTTI_KateFileListItem )
258 viewManager->activateView( ((KateFileListItem *)item)->documentNumber() );
261 void KateFileList::slotModChanged (Kate::Document *doc)
265 TQListViewItem * item = firstChild();
268 if ( ((KateFileListItem *)item)->documentNumber() == doc->documentNumber() )
271 item = item->nextSibling();
274 if ( ((KateFileListItem *)item)->document()->isModified() )
276 m_editHistory.removeRef( (KateFileListItem *)item );
277 m_editHistory.prepend( (KateFileListItem *)item );
279 for ( uint i=0; i < m_editHistory.count(); i++ )
281 m_editHistory.at( i )->setEditHistPos( i+1 );
282 repaintItem( m_editHistory.at( i ) );
289 void KateFileList::slotModifiedOnDisc (Kate::Document *doc,
bool,
unsigned char)
291 slotModChanged( doc );
294 void KateFileList::slotNameChanged (Kate::Document *doc)
300 TQListViewItem * item = firstChild();
302 if ( ((KateFileListItem*)item)->document() == doc )
304 item->setText( 0, doc->docName() );
308 item = item->nextSibling();
313 void KateFileList::slotViewChanged ()
315 if (!viewManager->activeView())
return;
317 Kate::View *view = viewManager->activeView();
318 uint dn = view->getDoc()->documentNumber();
320 TQListViewItem * i = firstChild();
322 if ( ((KateFileListItem *)i)->documentNumber() == dn )
326 i = i->nextSibling();
332 KateFileListItem *item = (KateFileListItem*)i;
333 setCurrentItem( item );
346 m_viewHistory.removeRef( item );
347 m_viewHistory.prepend( item );
349 for ( uint i=0; i < m_viewHistory.count(); i++ )
351 m_viewHistory.at( i )->setViewHistPos( i+1 );
352 repaintItem( m_viewHistory.at( i ) );
357 void KateFileList::slotMenu ( TQListViewItem *item,
const TQPoint &p,
int )
362 m_clickedMenuItem = item;
363 if (m_clickedMenuItem->itemAbove()) {
364 listMoveFileUp->setEnabled(
true);
367 listMoveFileUp->setEnabled(
false);
369 if (m_clickedMenuItem->itemBelow()) {
370 listMoveFileDown->setEnabled(
true);
373 listMoveFileDown->setEnabled(
false);
376 TQPopupMenu *menu = (TQPopupMenu*) ((viewManager->mainWindow())->factory()->container(
"filelist_popup", viewManager->mainWindow()));
383 TQString KateFileList::tooltip( TQListViewItem *item,
int )
385 KateFileListItem *i = ((KateFileListItem*)item);
386 if ( ! i )
return TQString::null;
389 const KateDocumentInfo *info = KateDocManager::self()->documentInfo(i->document());
391 if (info && info->modifiedOnDisc)
393 if (info->modifiedOnDiscReason == 1)
394 str += i18n(
"<b>This file was changed (modified) on disk by another program.</b><br />");
395 else if (info->modifiedOnDiscReason == 2)
396 str += i18n(
"<b>This file was changed (created) on disk by another program.</b><br />");
397 else if (info->modifiedOnDiscReason == 3)
398 str += i18n(
"<b>This file was changed (deleted) on disk by another program.</b><br />");
401 str += i->document()->url().prettyURL();
406 void KateFileList::setSortType (
int s)
409 if (m_sort == KateFileList::sortManual) {
410 setSorting( -1,
true );
411 setDragEnabled(
true);
412 setAcceptDrops(
true);
415 setSorting( 0,
true );
416 setDragEnabled(
false);
417 setAcceptDrops(
false);
422 void KateFileList::moveFileUp()
424 if (m_clickedMenuItem) {
425 sortAction->setCurrentItem(KateFileList::sortManual);
426 setSortType(KateFileList::sortManual);
427 TQListViewItem* nitemabove = m_clickedMenuItem->itemAbove();
429 nitemabove = nitemabove->itemAbove();
431 m_clickedMenuItem->moveItem(nitemabove);
435 nitemabove = m_clickedMenuItem->itemAbove();
436 nitemabove->moveItem(m_clickedMenuItem);
442 void KateFileList::moveFileDown()
444 if (m_clickedMenuItem) {
445 sortAction->setCurrentItem(KateFileList::sortManual);
446 setSortType(KateFileList::sortManual);
447 TQListViewItem* nitemabove = m_clickedMenuItem->itemBelow();
449 m_clickedMenuItem->moveItem(nitemabove);
454 void KateFileList::updateSort ()
459 void KateFileList::readConfig( KConfig *config,
const TQString &group )
461 TQString oldgroup = config->group();
462 config->setGroup( group );
464 setSortType( config->readNumEntry(
"Sort Type", sortByID ) );
465 m_viewShade = config->readColorEntry(
"View Shade", &m_viewShade );
466 m_editShade = config->readColorEntry(
"Edit Shade", &m_editShade );
467 m_enableBgShading = config->readBoolEntry(
"Shading Enabled", &m_enableBgShading );
469 sortAction->setCurrentItem( sortType() );
471 config->setGroup( oldgroup );
474 void KateFileList::writeConfig( KConfig *config,
const TQString &group )
476 TQString oldgroup = config->group();
477 config->setGroup( group );
479 config->writeEntry(
"Sort Type", m_sort );
480 config->writeEntry(
"View Shade", m_viewShade );
481 config->writeEntry(
"Edit Shade", m_editShade );
482 config->writeEntry(
"Shading Enabled", m_enableBgShading );
484 config->setGroup( oldgroup );
487 void KateFileList::takeItem( TQListViewItem *item )
489 if ( item->rtti() == RTTI_KateFileListItem )
491 m_editHistory.removeRef( (KateFileListItem*)item );
492 m_viewHistory.removeRef( (KateFileListItem*)item );
494 TQListView::takeItem( item );
499 KateFileListItem::KateFileListItem( TQListView* lv,
500 Kate::Document *_doc )
501 : TQListViewItem( lv, _doc->docName() ),
505 m_docNumber( _doc->documentNumber() )
508 TQListViewItem* lastitem = lv->lastItem();
514 KateFileListItem::~KateFileListItem()
518 const TQPixmap *KateFileListItem::pixmap (
int column )
const
521 static TQPixmap noPm = SmallIcon (
"null");
522 static TQPixmap modPm = SmallIcon(
"modified");
523 static TQPixmap discPm = SmallIcon(
"modonhd");
524 static TQPixmap modmodPm = SmallIcon(
"modmod");
526 const KateDocumentInfo *info = KateDocManager::self()->documentInfo(doc);
528 if (info && info->modifiedOnDisc)
529 return doc->isModified() ? &modmodPm : &discPm;
531 return doc->isModified() ? &modPm : &noPm;
537 void KateFileListItem::paintCell( TQPainter *painter,
const TQColorGroup & cg,
int column,
int width,
int align )
539 KateFileList *fl = (KateFileList*)listView();
544 TQColorGroup cgNew = cg;
547 if ( fl->shadingEnabled() && m_viewhistpos > 1 )
549 TQColor b( cg.base() );
551 TQColor shade = fl->viewShade();
552 TQColor eshade = fl->editShade();
553 int hc = fl->histCount();
556 if ( fl->shadingEnabled() && m_edithistpos > 0 )
558 int ec = fl->editHistCount();
559 int v = hc-m_viewhistpos;
560 int e = ec-m_edithistpos+1;
562 int n = QMAX(v + e, 1);
564 ((shade.red()*v) + (eshade.red()*e))/n,
565 ((shade.green()*v) + (eshade.green()*e))/n,
566 ((shade.blue()*v) + (eshade.blue()*e))/n
571 float t = (0.5/hc)*(hc-m_viewhistpos+1);
573 (
int)((b.red()*(1-t)) + (shade.red()*t)),
574 (int)((b.green()*(1-t)) + (shade.green()*t)),
575 (int)((b.blue()*(1-t)) + (shade.blue()*t))
578 cgNew.setColor(TQColorGroup::Base, b);
581 TQListViewItem::paintCell( painter, cgNew, column, width, align );
584 TQListViewItem::paintCell( painter, cg, column, width, align );
587 int KateFileListItem::compare ( TQListViewItem * i,
int col,
bool ascending )
const
589 if ( i->rtti() == RTTI_KateFileListItem )
591 switch( ((KateFileList*)listView())->sortType() )
593 case KateFileList::sortByID:
596 int d = (int)doc->documentNumber() - ((KateFileListItem*)i)->documentNumber();
597 return ascending ? d : -d;
600 case KateFileList::sortByURL:
601 return doc->url().prettyURL().compare( ((KateFileListItem*)i)->document()->url().prettyURL() );
604 return TQListViewItem::compare( i, col, ascending );
612 KFLConfigPage::KFLConfigPage( TQWidget* parent,
const char *name, KateFileList *fl )
613 : Kate::ConfigPage( parent, name ),
617 TQVBoxLayout *lo1 =
new TQVBoxLayout(
this );
618 int spacing = KDialog::spacingHint();
619 lo1->setSpacing( spacing );
621 TQGroupBox *gb =
new TQGroupBox( 1, Qt::Horizontal, i18n(
"Background Shading"),
this );
622 lo1->addWidget( gb );
624 TQWidget *g =
new TQWidget( gb );
625 TQGridLayout *lo =
new TQGridLayout( g, 2, 2 );
626 lo->setSpacing( KDialog::spacingHint() );
627 cbEnableShading =
new TQCheckBox( i18n(
"&Enable background shading"), g );
628 lo->addMultiCellWidget( cbEnableShading, 1, 1, 0, 1 );
630 kcbViewShade =
new KColorButton( g );
631 lViewShade =
new TQLabel( kcbViewShade, i18n(
"&Viewed documents' shade:"), g );
632 lo->addWidget( lViewShade, 2, 0 );
633 lo->addWidget( kcbViewShade, 2, 1 );
635 kcbEditShade =
new KColorButton( g );
636 lEditShade =
new TQLabel( kcbEditShade, i18n(
"&Modified documents' shade:"), g );
637 lo->addWidget( lEditShade, 3, 0 );
638 lo->addWidget( kcbEditShade, 3, 1 );
641 TQHBox *hbSorting =
new TQHBox(
this );
642 lo1->addWidget( hbSorting );
643 lSort =
new TQLabel( i18n(
"&Sort by:"), hbSorting );
644 cmbSort =
new TQComboBox( hbSorting );
645 lSort->setBuddy( cmbSort );
647 l << i18n(
"Opening Order") << i18n(
"Document Name") << i18n(
"URL");
648 cmbSort->insertStringList( l );
650 lo1->insertStretch( -1, 10 );
652 TQWhatsThis::add( cbEnableShading, i18n(
653 "When background shading is enabled, documents that have been viewed "
654 "or edited within the current session will have a shaded background. "
655 "The most recent documents have the strongest shade.") );
656 TQWhatsThis::add( kcbViewShade, i18n(
657 "Set the color for shading viewed documents.") );
658 TQWhatsThis::add( kcbEditShade, i18n(
659 "Set the color for modified documents. This color is blended into "
660 "the color for viewed files. The most recently edited documents get "
661 "most of this color.") );
663 TQWhatsThis::add( cmbSort, i18n(
664 "Set the sorting method for the documents.") );
669 connect( cbEnableShading, TQT_SIGNAL(toggled(
bool)),
this, TQT_SLOT(slotMyChanged()) );
670 connect( cbEnableShading, TQT_SIGNAL(toggled(
bool)),
this, TQT_SLOT(slotEnableChanged()) );
671 connect( kcbViewShade, TQT_SIGNAL(changed(
const TQColor&)),
this, TQT_SLOT(slotMyChanged()) );
672 connect( kcbEditShade, TQT_SIGNAL(changed(
const TQColor&)),
this, TQT_SLOT(slotMyChanged()) );
673 connect( cmbSort, TQT_SIGNAL(activated(
int)),
this, TQT_SLOT(slotMyChanged()) );
676 void KFLConfigPage::apply()
683 m_filelist->m_viewShade = kcbViewShade->color();
684 m_filelist->m_editShade = kcbEditShade->color();
685 m_filelist->m_enableBgShading = cbEnableShading->isChecked();
686 m_filelist->setSortType( cmbSort->currentItem() );
688 m_filelist->triggerUpdate();
691 void KFLConfigPage::reload()
694 KConfig *config = kapp->config();
695 config->setGroup(
"Filelist" );
696 cbEnableShading->setChecked( config->readBoolEntry(
"Shading Enabled", &m_filelist->m_enableBgShading ) );
697 kcbViewShade->setColor( config->readColorEntry(
"View Shade", &m_filelist->m_viewShade ) );
698 kcbEditShade->setColor( config->readColorEntry(
"Edit Shade", &m_filelist->m_editShade ) );
699 cmbSort->setCurrentItem( m_filelist->sortType() );
703 void KFLConfigPage::slotEnableChanged()
705 kcbViewShade->setEnabled( cbEnableShading->isChecked() );
706 kcbEditShade->setEnabled( cbEnableShading->isChecked() );
707 lViewShade->setEnabled( cbEnableShading->isChecked() );
708 lEditShade->setEnabled( cbEnableShading->isChecked() );
711 void KFLConfigPage::slotMyChanged()