00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#include "config.h"
00021
00022
#include <qdragobject.h>
00023
#include <qtimer.h>
00024
#include <qheader.h>
00025
#include <qcursor.h>
00026
#include <qtooltip.h>
00027
#include <qstyle.h>
00028
#include <qpainter.h>
00029
00030
#include <kglobalsettings.h>
00031
#include <kconfig.h>
00032
#include <kcursor.h>
00033
#include <kapplication.h>
00034
00035
#if defined Q_WS_X11 && ! defined K_WS_QTONLY
00036
#include <kipc.h>
00037
#endif
00038
00039
#include <kdebug.h>
00040
00041
#include "klistview.h"
00042
#include "klistviewlineedit.h"
00043
00044
#if defined Q_WS_X11 && ! defined K_WS_QTONLY
00045
#include <X11/Xlib.h>
00046
#endif
00047
00048
class KListView::Tooltip :
public QToolTip
00049 {
00050
public:
00051 Tooltip (
KListView* parent,
QToolTipGroup* group = 0L);
00052
virtual ~Tooltip () {}
00053
00054
protected:
00058
virtual void maybeTip (
const QPoint&);
00059
00060
private:
00061
KListView* mParent;
00062 };
00063
00064 KListView::Tooltip::Tooltip (
KListView* parent,
QToolTipGroup* group)
00065 :
QToolTip (parent, group),
00066 mParent (parent)
00067 {
00068 }
00069
00070
void KListView::Tooltip::maybeTip (
const QPoint&)
00071 {
00072
00073 }
00074
00075
class KListView::KListViewPrivate
00076 {
00077
public:
00078 KListViewPrivate (
KListView* listview)
00079 : pCurrentItem (0L),
00080 dragDelay (
KGlobalSettings::dndEventDelay()),
00081 editor (new
KListViewLineEdit (listview)),
00082 cursorInExecuteArea(false),
00083 itemsMovable (true),
00084 selectedBySimpleMove(false),
00085 selectedUsingMouse(false),
00086 itemsRenameable (false),
00087 validDrag (false),
00088 dragEnabled (false),
00089 autoOpen (true),
00090 disableAutoSelection (false),
00091 dropVisualizer (true),
00092 dropHighlighter (false),
00093 createChildren (true),
00094 pressedOnSelected (false),
00095 wasShiftEvent (false),
00096 fullWidth (false),
00097 sortAscending(true),
00098 tabRename(true),
00099 sortColumn(0),
00100 selectionDirection(0),
00101 tooltipColumn (0),
00102 selectionMode (Single),
00103 contextMenuKey (
KGlobalSettings::contextMenuKey()),
00104 showContextMenusOnPress (
KGlobalSettings::showContextMenusOnPress()),
00105 mDropVisualizerWidth (4),
00106 paintAbove (0),
00107 paintCurrent (0),
00108 paintBelow (0),
00109 painting (false)
00110 {
00111 renameable.append(0);
00112 connect(editor, SIGNAL(done(
QListViewItem*,
int)), listview, SLOT(doneEditing(
QListViewItem*,
int)));
00113 }
00114
00115 ~KListViewPrivate ()
00116 {
00117
delete editor;
00118 }
00119
00120
QListViewItem* pCurrentItem;
00121
00122
QTimer autoSelect;
00123
int autoSelectDelay;
00124
00125
QTimer dragExpand;
00126
QListViewItem* dragOverItem;
00127
QPoint dragOverPoint;
00128
00129
QPoint startDragPos;
00130
int dragDelay;
00131
00132
KListViewLineEdit *editor;
00133
QValueList<int> renameable;
00134
00135
bool cursorInExecuteArea:1;
00136
bool bUseSingle:1;
00137
bool bChangeCursorOverItem:1;
00138
bool itemsMovable:1;
00139
bool selectedBySimpleMove : 1;
00140
bool selectedUsingMouse:1;
00141
bool itemsRenameable:1;
00142
bool validDrag:1;
00143
bool dragEnabled:1;
00144
bool autoOpen:1;
00145
bool disableAutoSelection:1;
00146
bool dropVisualizer:1;
00147
bool dropHighlighter:1;
00148
bool createChildren:1;
00149
bool pressedOnSelected:1;
00150
bool wasShiftEvent:1;
00151
bool fullWidth:1;
00152
bool sortAscending:1;
00153
bool tabRename:1;
00154
00155
int sortColumn;
00156
00157
00158
int selectionDirection;
00159
int tooltipColumn;
00160
00161 SelectionModeExt selectionMode;
00162
int contextMenuKey;
00163
bool showContextMenusOnPress;
00164
00165
QRect mOldDropVisualizer;
00166
int mDropVisualizerWidth;
00167
QRect mOldDropHighlighter;
00168
QListViewItem *afterItemDrop;
00169
QListViewItem *parentItemDrop;
00170
00171
QListViewItem *paintAbove;
00172
QListViewItem *paintCurrent;
00173
QListViewItem *paintBelow;
00174
bool painting;
00175
00176
QColor alternateBackground;
00177 };
00178
00179
00180 KListViewLineEdit::KListViewLineEdit(
KListView *parent)
00181 :
KLineEdit(parent->viewport()), item(0), col(0), p(parent)
00182 {
00183 setFrame(
false );
00184 hide();
00185 connect( parent, SIGNAL( selectionChanged() ), SLOT( slotSelectionChanged() ));
00186 }
00187
00188 KListViewLineEdit::~KListViewLineEdit()
00189 {
00190 }
00191
00192
QListViewItem *KListViewLineEdit::currentItem()
const
00193
{
00194
return item;
00195 }
00196
00197
void KListViewLineEdit::load(
QListViewItem *i,
int c)
00198 {
00199 item=i;
00200 col=c;
00201
00202
QRect rect(p->itemRect(i));
00203
setText(item->text(c));
00204
home(
true );
00205
00206
int fieldX = rect.x() - 1;
00207
int fieldW = p->columnWidth(col) + 2;
00208
00209
int pos = p->header()->mapToIndex(col);
00210
for (
int index = 0; index < pos; index++ )
00211 fieldX += p->columnWidth( p->header()->mapToSection( index ));
00212
00213
if ( col == 0 ) {
00214
int d = i->depth() + (p->rootIsDecorated() ? 1 : 0);
00215 d *= p->treeStepSize();
00216 fieldX += d;
00217 fieldW -= d;
00218 }
00219
00220
if ( i->pixmap( col ) ) {
00221
int d = i->pixmap( col )->width();
00222 fieldX += d;
00223 fieldW -= d;
00224 }
00225
00226 setGeometry(fieldX, rect.y() - 1, fieldW, rect.height() + 2);
00227 show();
00228 setFocus();
00229 }
00230
00231
00232
00233
00234
00235
static int nextCol (
KListView *pl,
QListViewItem *pi,
int start,
int dir)
00236 {
00237
if (pi)
00238 {
00239
00240
for (; ((dir == +1) ? (start < pl->columns()) : (start >= 0)); start += dir)
00241
if (pl->
isRenameable(start))
00242
return start;
00243 }
00244
00245
return -1;
00246 }
00247
00248
static QListViewItem *prevItem (
QListViewItem *pi)
00249 {
00250
QListViewItem *pa = pi->itemAbove();
00251
00252
00253
00254
00255
if (pa && pa->parent() == pi->parent())
00256
return pa;
00257
00258
return 0;
00259 }
00260
00261
static QListViewItem *lastQChild (
QListViewItem *pi)
00262 {
00263
if (pi)
00264 {
00265
00266
00267
00268
00269
for (
QListViewItem *pt = pi->nextSibling(); pt; pt = pt->nextSibling())
00270 pi = pt;
00271 }
00272
00273
return pi;
00274 }
00275
00276 void KListViewLineEdit::selectNextCell (
QListViewItem *pitem,
int column,
bool forward)
00277 {
00278
const int ncols = p->columns();
00279
const int dir = forward ? +1 : -1;
00280
const int restart = forward ? 0 : (ncols - 1);
00281
QListViewItem *top = (pitem && pitem->parent())
00282 ? pitem->parent()->firstChild()
00283 : p->firstChild();
00284
QListViewItem *pi = pitem;
00285
00286 terminate();
00287
00288
do
00289 {
00290
00291
00292
00293
00294
00295
00296
if ((column = nextCol(p, pi, column + dir, dir)) != -1 ||
00297 (column = nextCol(p, (pi = (forward ? pi->nextSibling() : prevItem(pi))), restart, dir)) != -1 ||
00298 (column = nextCol(p, (pi = (forward ? top : lastQChild(pitem))), restart, dir)) != -1)
00299 {
00300
if (pi)
00301 {
00302 p->setCurrentItem(pi);
00303 p->
rename(pi, column);
00304
00305
00306
00307
00308
00309
00310
if (!item)
00311
continue;
00312
00313
break;
00314 }
00315 }
00316 }
00317
while (pi && !item);
00318 }
00319
00320
#ifdef KeyPress
00321
#undef KeyPress
00322
#endif
00323
00324
bool KListViewLineEdit::event (
QEvent *pe)
00325 {
00326
if (pe->type() == QEvent::KeyPress)
00327 {
00328
QKeyEvent *k = (
QKeyEvent *) pe;
00329
00330
if ((k->key() == Qt::Key_Backtab || k->key() == Qt::Key_Tab) &&
00331 p->
tabOrderedRenaming() && p->
itemsRenameable() &&
00332 !(k->state() & ControlButton || k->state() & AltButton))
00333 {
00334 selectNextCell(item, col,
00335 (k->key() == Key_Tab && !(k->state() & ShiftButton)));
00336
return true;
00337 }
00338 }
00339
00340
return KLineEdit::event(pe);
00341 }
00342
00343 void KListViewLineEdit::keyPressEvent(
QKeyEvent *e)
00344 {
00345
if(e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter )
00346 terminate(
true);
00347
else if(e->key() == Qt::Key_Escape)
00348 terminate(
false);
00349
else if (e->key() == Qt::Key_Down || e->key() == Qt::Key_Up)
00350 {
00351 terminate(
true);
00352 KLineEdit::keyPressEvent(e);
00353 }
00354
else
00355 KLineEdit::keyPressEvent(e);
00356 }
00357
00358
void KListViewLineEdit::terminate()
00359 {
00360 terminate(
true);
00361 }
00362
00363
void KListViewLineEdit::terminate(
bool commit)
00364 {
00365
if ( item )
00366 {
00367
00368
if (commit)
00369 item->setText(col, text());
00370
int c=col;
00371
QListViewItem *i=item;
00372 col=0;
00373 item=0;
00374 hide();
00375
if (commit)
00376 emit done(i,c);
00377 }
00378 }
00379
00380
void KListViewLineEdit::focusOutEvent(
QFocusEvent *ev)
00381 {
00382
QFocusEvent * focusEv = static_cast<QFocusEvent*>(ev);
00383
00384
if (focusEv->reason() != QFocusEvent::Popup && focusEv->reason() != QFocusEvent::ActiveWindow)
00385 terminate(
true);
00386
else
00387 KLineEdit::focusOutEvent(ev);
00388 }
00389
00390
void KListViewLineEdit::paintEvent(
QPaintEvent *e )
00391 {
00392 KLineEdit::paintEvent( e );
00393
00394
if ( !frame() ) {
00395
QPainter p(
this );
00396 p.setClipRegion( e->region() );
00397 p.drawRect( rect() );
00398 }
00399 }
00400
00401
00402
00403
00404
void KListViewLineEdit::slotSelectionChanged()
00405 {
00406 item = 0;
00407 col = 0;
00408 hide();
00409 }
00410
00411
00412 KListView::KListView(
QWidget *parent,
const char *name )
00413 :
QListView( parent, name ),
00414 d (new KListViewPrivate (this))
00415 {
00416 setDragAutoScroll(
true);
00417
00418 connect(
this, SIGNAL( onViewport() ),
00419
this, SLOT(
slotOnViewport() ) );
00420 connect(
this, SIGNAL( onItem(
QListViewItem * ) ),
00421
this, SLOT(
slotOnItem(
QListViewItem * ) ) );
00422
00423 connect (
this, SIGNAL(contentsMoving(
int,
int)),
00424
this, SLOT(
cleanDropVisualizer()));
00425 connect (
this, SIGNAL(contentsMoving(
int,
int)),
00426
this, SLOT(
cleanItemHighlighter()));
00427
00428
slotSettingsChanged(KApplication::SETTINGS_MOUSE);
00429
if (kapp)
00430 {
00431 connect( kapp, SIGNAL( settingsChanged(
int) ), SLOT(
slotSettingsChanged(
int) ) );
00432
#if defined Q_WS_X11 && ! defined K_WS_QTONLY
00433
kapp->addKipcEventMask( KIPC::SettingsChanged );
00434
#endif
00435
}
00436
00437 connect(&d->autoSelect, SIGNAL( timeout() ),
00438
this, SLOT(
slotAutoSelect() ) );
00439 connect(&d->dragExpand, SIGNAL( timeout() ),
00440
this, SLOT( slotDragExpand() ) );
00441
00442
00443
if (d->showContextMenusOnPress)
00444 {
00445 connect (
this, SIGNAL (rightButtonPressed (
QListViewItem*,
const QPoint&,
int)),
00446
this, SLOT (
emitContextMenu (QListViewItem*,
const QPoint&,
int)));
00447 }
00448
else
00449 {
00450 connect (
this, SIGNAL (rightButtonClicked (
QListViewItem*,
const QPoint&,
int)),
00451
this, SLOT (
emitContextMenu (QListViewItem*,
const QPoint&,
int)));
00452 }
00453
00454 connect (
this, SIGNAL (
menuShortCutPressed (
KListView*,
QListViewItem*)),
00455
this, SLOT (
emitContextMenu (KListView*,
QListViewItem*)));
00456 d->alternateBackground =
KGlobalSettings::alternateBackgroundColor();
00457 }
00458
00459 KListView::~KListView()
00460 {
00461
delete d;
00462 }
00463
00464 bool KListView::isExecuteArea(
const QPoint& point )
00465 {
00466
QListViewItem* item = itemAt( point );
00467
if ( item ) {
00468
int offset = treeStepSize() * ( item->depth() + ( rootIsDecorated() ? 1 : 0) );
00469
if (point.x() > (item->width( fontMetrics() ,
this, 0 ) + offset ))
00470
return false;
00471
return isExecuteArea( point.x() );
00472 }
00473
return false;
00474 }
00475
00476 bool KListView::isExecuteArea(
int x )
00477 {
00478
if( allColumnsShowFocus() )
00479
return true;
00480
else {
00481
int offset = 0;
00482
int width = columnWidth( 0 );
00483
int pos = header()->mapToIndex( 0 );
00484
00485
for (
int index = 0; index < pos; index++ )
00486 offset += columnWidth( header()->mapToSection( index ) );
00487
00488 x += contentsX();
00489
return ( x > offset && x < ( offset + width ) );
00490 }
00491 }
00492
00493 void KListView::slotOnItem(
QListViewItem *item )
00494 {
00495
QPoint vp = viewport()->mapFromGlobal( QCursor::pos() );
00496
if ( item &&
isExecuteArea( vp.x() ) && (d->autoSelectDelay > -1) && d->bUseSingle ) {
00497 d->autoSelect.start( d->autoSelectDelay,
true );
00498 d->pCurrentItem = item;
00499 }
00500 }
00501
00502 void KListView::slotOnViewport()
00503 {
00504
if ( d->bChangeCursorOverItem )
00505 viewport()->unsetCursor();
00506
00507 d->autoSelect.stop();
00508 d->pCurrentItem = 0L;
00509 }
00510
00511 void KListView::slotSettingsChanged(
int category)
00512 {
00513
switch (category)
00514 {
00515
case KApplication::SETTINGS_MOUSE:
00516 d->dragDelay =
KGlobalSettings::dndEventDelay();
00517 d->bUseSingle =
KGlobalSettings::singleClick();
00518
00519 disconnect(
this, SIGNAL (mouseButtonClicked (
int,
QListViewItem*,
const QPoint &,
int)),
00520
this, SLOT (slotMouseButtonClicked (
int,
QListViewItem*,
const QPoint &,
int)));
00521
00522
if( d->bUseSingle )
00523 connect (
this, SIGNAL (mouseButtonClicked (
int,
QListViewItem*,
const QPoint &,
int)),
00524
this, SLOT (slotMouseButtonClicked(
int,
QListViewItem*,
const QPoint &,
int)));
00525
00526 d->bChangeCursorOverItem =
KGlobalSettings::changeCursorOverIcon();
00527
if ( !d->disableAutoSelection )
00528 d->autoSelectDelay =
KGlobalSettings::autoSelectDelay();
00529
00530
if( !d->bUseSingle || !d->bChangeCursorOverItem )
00531 viewport()->unsetCursor();
00532
00533
break;
00534
00535
case KApplication::SETTINGS_POPUPMENU:
00536 d->contextMenuKey =
KGlobalSettings::contextMenuKey ();
00537 d->showContextMenusOnPress =
KGlobalSettings::showContextMenusOnPress ();
00538
00539
if (d->showContextMenusOnPress)
00540 {
00541 disconnect (0L, 0L,
this, SLOT (
emitContextMenu (
QListViewItem*,
const QPoint&,
int)));
00542
00543 connect(
this, SIGNAL (rightButtonPressed (QListViewItem*,
const QPoint&,
int)),
00544
this, SLOT (
emitContextMenu (QListViewItem*,
const QPoint&,
int)));
00545 }
00546
else
00547 {
00548 disconnect (0L, 0L,
this, SLOT (
emitContextMenu (
QListViewItem*,
const QPoint&,
int)));
00549
00550 connect(
this, SIGNAL (rightButtonClicked (QListViewItem*,
const QPoint&,
int)),
00551
this, SLOT (
emitContextMenu (QListViewItem*,
const QPoint&,
int)));
00552 }
00553
break;
00554
00555
default:
00556
break;
00557 }
00558 }
00559
00560 void KListView::slotAutoSelect()
00561 {
00562
00563
if(
itemIndex( d->pCurrentItem ) == -1 )
00564
return;
00565
00566
if (!isActiveWindow())
00567 {
00568 d->autoSelect.stop();
00569
return;
00570 }
00571
00572
00573
if( !hasFocus() )
00574 setFocus();
00575
00576
#if defined Q_WS_X11 && ! defined K_WS_QTONLY
00577
00578 Window root;
00579 Window child;
00580
int root_x, root_y, win_x, win_y;
00581 uint keybstate;
00582 XQueryPointer( qt_xdisplay(), qt_xrootwin(), &root, &child,
00583 &root_x, &root_y, &win_x, &win_y, &keybstate );
00584
#endif
00585
00586
QListViewItem* previousItem = currentItem();
00587 setCurrentItem( d->pCurrentItem );
00588
00589
00590
#if defined Q_WS_X11 && ! defined K_WS_QTONLY
00591
00592
if( d->pCurrentItem ) {
00593
00594
if( (keybstate & ShiftMask) ) {
00595
bool block = signalsBlocked();
00596 blockSignals(
true );
00597
00598
00599
if( !(keybstate & ControlMask) )
00600 clearSelection();
00601
00602
bool select = !d->pCurrentItem->isSelected();
00603
bool update = viewport()->isUpdatesEnabled();
00604 viewport()->setUpdatesEnabled(
false );
00605
00606
bool down = previousItem->itemPos() < d->pCurrentItem->itemPos();
00607
QListViewItemIterator lit( down ? previousItem : d->pCurrentItem );
00608
for ( ; lit.current(); ++lit ) {
00609
if ( down && lit.current() == d->pCurrentItem ) {
00610 d->pCurrentItem->setSelected( select );
00611
break;
00612 }
00613
if ( !down && lit.current() == previousItem ) {
00614 previousItem->setSelected( select );
00615
break;
00616 }
00617 lit.current()->setSelected( select );
00618 }
00619
00620 blockSignals( block );
00621 viewport()->setUpdatesEnabled( update );
00622 triggerUpdate();
00623
00624 emit selectionChanged();
00625
00626
if( selectionMode() == QListView::Single )
00627 emit selectionChanged( d->pCurrentItem );
00628 }
00629
else if( (keybstate & ControlMask) )
00630 setSelected( d->pCurrentItem, !d->pCurrentItem->isSelected() );
00631
else {
00632
bool block = signalsBlocked();
00633 blockSignals(
true );
00634
00635
if( !d->pCurrentItem->isSelected() )
00636 clearSelection();
00637
00638 blockSignals( block );
00639
00640 setSelected( d->pCurrentItem,
true );
00641 }
00642 }
00643
else
00644
kdDebug() <<
"KListView::slotAutoSelect: Thatīs not supposed to happen!!!!" <<
endl;
00645
#endif
00646
}
00647
00648 void KListView::slotHeaderChanged()
00649 {
00650
if (d->fullWidth && columns())
00651 {
00652
int w = 0;
00653
for (
int i = 0; i < columns() - 1; ++i) w += columnWidth(i);
00654 setColumnWidth( columns() - 1, viewport()->width() - w - 1 );
00655 }
00656 }
00657
00658 void KListView::emitExecute(
QListViewItem *item,
const QPoint &pos,
int c )
00659 {
00660
if(
isExecuteArea( viewport()->mapFromGlobal(pos) ) ) {
00661
00662
00663
if ( !d->bUseSingle )
00664 {
00665 viewport()->unsetCursor();
00666 emit
executed( item );
00667 emit executed( item, pos, c );
00668 }
00669
else
00670 {
00671
00672
#if defined Q_WS_X11 && ! defined K_WS_QTONLY
00673
00674 Window root;
00675 Window child;
00676
int root_x, root_y, win_x, win_y;
00677 uint keybstate;
00678 XQueryPointer( qt_xdisplay(), qt_xrootwin(), &root, &child,
00679 &root_x, &root_y, &win_x, &win_y, &keybstate );
00680
00681 d->autoSelect.stop();
00682
00683
00684
if( !( ((keybstate & ShiftMask) || (keybstate & ControlMask)) ) ) {
00685 viewport()->unsetCursor();
00686 emit
executed( item );
00687 emit executed( item, pos, c );
00688 }
00689
#endif
00690
}
00691 }
00692 }
00693
00694 void KListView::focusInEvent(
QFocusEvent *fe )
00695 {
00696
00697 QListView::focusInEvent( fe );
00698
if ((d->selectedBySimpleMove)
00699 && (d->selectionMode == FileManager)
00700 && (fe->reason()!=QFocusEvent::Popup)
00701 && (fe->reason()!=QFocusEvent::ActiveWindow)
00702 && (currentItem()!=0))
00703 {
00704 currentItem()->setSelected(
true);
00705 currentItem()->repaint();
00706 emit selectionChanged();
00707 };
00708 }
00709
00710 void KListView::focusOutEvent(
QFocusEvent *fe )
00711 {
00712
cleanDropVisualizer();
00713
cleanItemHighlighter();
00714
00715 d->autoSelect.stop();
00716
00717
if ((d->selectedBySimpleMove)
00718 && (d->selectionMode == FileManager)
00719 && (fe->reason()!=QFocusEvent::Popup)
00720 && (fe->reason()!=QFocusEvent::ActiveWindow)
00721 && (currentItem()!=0)
00722 && (!d->editor->isVisible()))
00723 {
00724 currentItem()->setSelected(
false);
00725 currentItem()->repaint();
00726 emit selectionChanged();
00727 };
00728
00729 QListView::focusOutEvent( fe );
00730 }
00731
00732 void KListView::leaveEvent(
QEvent *e )
00733 {
00734 d->autoSelect.stop();
00735
00736 QListView::leaveEvent( e );
00737 }
00738
00739 bool KListView::event(
QEvent *e )
00740 {
00741
if (e->type() == QEvent::ApplicationPaletteChange)
00742 d->alternateBackground=
KGlobalSettings::alternateBackgroundColor();
00743
00744
return QListView::event(e);
00745 }
00746
00747 void KListView::contentsMousePressEvent(
QMouseEvent *e )
00748 {
00749
if( (
selectionModeExt() == Extended) && (e->state() & ShiftButton) && !(e->state() & ControlButton) )
00750 {
00751
bool block = signalsBlocked();
00752 blockSignals(
true );
00753
00754 clearSelection();
00755
00756 blockSignals( block );
00757 }
00758
else if ((
selectionModeExt()==FileManager) && (d->selectedBySimpleMove))
00759 {
00760 d->selectedBySimpleMove=
false;
00761 d->selectedUsingMouse=
true;
00762
if (currentItem()!=0)
00763 {
00764 currentItem()->setSelected(
false);
00765 currentItem()->repaint();
00766
00767 };
00768 };
00769
00770
QPoint p( contentsToViewport( e->pos() ) );
00771
QListViewItem *at = itemAt (p);
00772
00773
00774
bool rootDecoClicked = at
00775 && ( p.x() <= header()->cellPos( header()->mapToActual( 0 ) ) +
00776 treeStepSize() * ( at->depth() + ( rootIsDecorated() ? 1 : 0) ) + itemMargin() )
00777 && ( p.x() >= header()->cellPos( header()->mapToActual( 0 ) ) );
00778
00779
if (e->button() == LeftButton && !rootDecoClicked)
00780 {
00781
00782 d->startDragPos = e->pos();
00783
00784
if (at)
00785 {
00786 d->validDrag =
true;
00787 d->pressedOnSelected = at->isSelected();
00788 }
00789 }
00790
00791 QListView::contentsMousePressEvent( e );
00792 }
00793
00794 void KListView::contentsMouseMoveEvent(
QMouseEvent *e )
00795 {
00796
if (!
dragEnabled() || d->startDragPos.isNull() || !d->validDrag)
00797 QListView::contentsMouseMoveEvent (e);
00798
00799
QPoint vp = contentsToViewport(e->pos());
00800
QListViewItem *item = itemAt( vp );
00801
00802
00803
if ( item && d->bChangeCursorOverItem && d->bUseSingle )
00804 {
00805
00806
if( (item != d->pCurrentItem) ||
00807 (
isExecuteArea(vp) != d->cursorInExecuteArea) )
00808 {
00809 d->cursorInExecuteArea =
isExecuteArea(vp);
00810
00811
if( d->cursorInExecuteArea )
00812 viewport()->setCursor( KCursor::handCursor() );
00813
else
00814 viewport()->unsetCursor();
00815 }
00816 }
00817
00818
bool dragOn =
dragEnabled();
00819
QPoint newPos = e->pos();
00820
if (dragOn && d->validDrag &&
00821 (newPos.x() > d->startDragPos.x()+d->dragDelay ||
00822 newPos.x() < d->startDragPos.x()-d->dragDelay ||
00823 newPos.y() > d->startDragPos.y()+d->dragDelay ||
00824 newPos.y() < d->startDragPos.y()-d->dragDelay))
00825
00826 {
00827 QListView::contentsMouseReleaseEvent( 0 );
00828
startDrag();
00829 d->startDragPos =
QPoint();
00830 d->validDrag =
false;
00831 }
00832 }
00833
00834 void KListView::contentsMouseReleaseEvent(
QMouseEvent *e )
00835 {
00836
if (e->button() == LeftButton)
00837 {
00838
00839
if ( d->pressedOnSelected &&
itemsRenameable() )
00840 {
00841
QPoint p( contentsToViewport( e->pos() ) );
00842
QListViewItem *at = itemAt (p);
00843
if ( at )
00844 {
00845
00846
bool rootDecoClicked =
00847 ( p.x() <= header()->cellPos( header()->mapToActual( 0 ) ) +
00848 treeStepSize() * ( at->depth() + ( rootIsDecorated() ? 1 : 0) ) + itemMargin() )
00849 && ( p.x() >= header()->cellPos( header()->mapToActual( 0 ) ) );
00850
00851
if (!rootDecoClicked)
00852 {
00853
int col = header()->mapToLogical( header()->cellAt( p.x() ) );
00854
if ( d->renameable.contains(col) )
00855
rename(at, col);
00856 }
00857 }
00858 }
00859
00860 d->pressedOnSelected =
false;
00861 d->validDrag =
false;
00862 d->startDragPos =
QPoint();
00863 }
00864 QListView::contentsMouseReleaseEvent( e );
00865 }
00866
00867 void KListView::contentsMouseDoubleClickEvent (
QMouseEvent *e )
00868 {
00869
00870
00871
00872
00873
QPoint vp = contentsToViewport(e->pos());
00874
QListViewItem *item = itemAt( vp );
00875 emit QListView::doubleClicked( item );
00876
00877
int col = item ? header()->mapToLogical( header()->cellAt( vp.x() ) ) : -1;
00878
00879
if( item ) {
00880 emit
doubleClicked( item, e->globalPos(), col );
00881
00882
if( (e->button() == LeftButton) && !d->bUseSingle )
00883
emitExecute( item, e->globalPos(), col );
00884 }
00885 }
00886
00887
void KListView::slotMouseButtonClicked(
int btn,
QListViewItem *item,
const QPoint &pos,
int c )
00888 {
00889
if( (btn == LeftButton) && item )
00890 emitExecute(item, pos, c);
00891 }
00892
00893 void KListView::contentsDropEvent(
QDropEvent* e)
00894 {
00895
cleanDropVisualizer();
00896
cleanItemHighlighter();
00897 d->dragExpand.stop();
00898
00899
if (
acceptDrag (e))
00900 {
00901 e->acceptAction();
00902
QListViewItem *afterme;
00903
QListViewItem *parent;
00904
findDrop(e->pos(), parent, afterme);
00905
00906
if (e->source() == viewport() &&
itemsMovable())
00907
movableDropEvent(parent, afterme);
00908
else
00909 {
00910 emit
dropped(e, afterme);
00911 emit dropped(
this, e, afterme);
00912 emit dropped(e, parent, afterme);
00913 emit dropped(
this, e, parent, afterme);
00914 }
00915 }
00916 }
00917
00918 void KListView::movableDropEvent (
QListViewItem* parent,
QListViewItem* afterme)
00919 {
00920
QPtrList<QListViewItem> items, afterFirsts, afterNows;
00921
QListViewItem *current=currentItem();
00922
bool hasMoved=
false;
00923
for (
QListViewItem *i = firstChild(), *iNext=0; i != 0; i = iNext)
00924 {
00925 iNext=i->itemBelow();
00926
if (!i->isSelected())
00927
continue;
00928
00929
00930
00931
if (i==afterme)
00932
continue;
00933
00934 i->setSelected(
false);
00935
00936
QListViewItem *afterFirst = i->itemAbove();
00937
00938
if (!hasMoved)
00939 {
00940 emit
aboutToMove();
00941 hasMoved=
true;
00942 }
00943
00944
moveItem(i, parent, afterme);
00945
00946
00947
00948 emit
moved(i, afterFirst, afterme);
00949
00950 items.append (i);
00951 afterFirsts.append (afterFirst);
00952 afterNows.append (afterme);
00953
00954 afterme = i;
00955 }
00956 clearSelection();
00957
for (
QListViewItem *i=items.first(); i != 0; i=items.next() )
00958 i->setSelected(
true);
00959
if (current)
00960 setCurrentItem(current);
00961
00962 emit
moved(items,afterFirsts,afterNows);
00963
00964
if (firstChild())
00965 emit
moved();
00966 }
00967
00968 void KListView::contentsDragMoveEvent(
QDragMoveEvent *event)
00969 {
00970
if (
acceptDrag(event))
00971 {
00972 event->acceptAction();
00973
00974
00975
findDrop(event->pos(), d->parentItemDrop, d->afterItemDrop);
00976
QPoint vp = contentsToViewport( event->pos() );
00977
QListViewItem *item =
isExecuteArea( vp ) ? itemAt( vp ) : 0L;
00978
00979
if ( item != d->dragOverItem )
00980 {
00981 d->dragExpand.stop();
00982 d->dragOverItem = item;
00983 d->dragOverPoint = vp;
00984
if ( d->dragOverItem && d->dragOverItem->isExpandable() && !d->dragOverItem->isOpen() )
00985 d->dragExpand.start( QApplication::startDragTime(),
true );
00986 }
00987
if (
dropVisualizer())
00988 {
00989
QRect tmpRect =
drawDropVisualizer(0, d->parentItemDrop, d->afterItemDrop);
00990
if (tmpRect != d->mOldDropVisualizer)
00991 {
00992
cleanDropVisualizer();
00993 d->mOldDropVisualizer=tmpRect;
00994 viewport()->repaint(tmpRect);
00995 }
00996 }
00997
if (
dropHighlighter())
00998 {
00999
QRect tmpRect =
drawItemHighlighter(0, d->afterItemDrop);
01000
if (tmpRect != d->mOldDropHighlighter)
01001 {
01002
cleanItemHighlighter();
01003 d->mOldDropHighlighter=tmpRect;
01004 viewport()->repaint(tmpRect);
01005 }
01006 }
01007 }
01008
else
01009 event->ignore();
01010 }
01011
01012
void KListView::slotDragExpand()
01013 {
01014
if ( itemAt( d->dragOverPoint ) == d->dragOverItem )
01015 d->dragOverItem->setOpen(
true );
01016 }
01017
01018 void KListView::contentsDragLeaveEvent (
QDragLeaveEvent*)
01019 {
01020 d->dragExpand.stop();
01021
cleanDropVisualizer();
01022
cleanItemHighlighter();
01023 }
01024
01025 void KListView::cleanDropVisualizer()
01026 {
01027
if (d->mOldDropVisualizer.isValid())
01028 {
01029
QRect rect=d->mOldDropVisualizer;
01030 d->mOldDropVisualizer =
QRect();
01031 viewport()->repaint(rect,
true);
01032 }
01033 }
01034
01035 int KListView::depthToPixels(
int depth )
01036 {
01037
return treeStepSize() * ( depth + (rootIsDecorated() ? 1 : 0) ) + itemMargin();
01038 }
01039
01040 void KListView::findDrop(
const QPoint &pos,
QListViewItem *&parent,
QListViewItem *&after)
01041 {
01042
QPoint p (contentsToViewport(pos));
01043
01044
01045
QListViewItem *atpos = itemAt(p);
01046
01047
QListViewItem *above;
01048
if (!atpos)
01049 above =
lastItem();
01050
else
01051 {
01052
01053
if (p.y() - itemRect(atpos).topLeft().y() < (atpos->height()/2))
01054 above = atpos->itemAbove();
01055
else
01056 above = atpos;
01057 }
01058
01059
if (above)
01060 {
01061
01062
01063
if (above->firstChild() && above->isOpen())
01064 {
01065 parent = above;
01066 after = 0;
01067
return;
01068 }
01069
01070
01071
01072
if (above->isExpandable())
01073 {
01074
01075
if (p.x() >=
depthToPixels( above->depth() + 1 ) ||
01076 (above->isOpen() && above->childCount() > 0) )
01077 {
01078 parent = above;
01079 after = 0L;
01080
return;
01081 }
01082 }
01083
01084
01085
01086
QListViewItem * betterAbove = above->parent();
01087
QListViewItem * last = above;
01088
while ( betterAbove )
01089 {
01090
01091
01092
if ( last->nextSibling() == 0 )
01093 {
01094
if (p.x() <
depthToPixels ( betterAbove->depth() + 1 ))
01095 above = betterAbove;
01096
else
01097
break;
01098 last = betterAbove;
01099 betterAbove = betterAbove->parent();
01100 }
else
01101
break;
01102 }
01103 }
01104
01105 after = above;
01106 parent = after ? after->parent() : 0L ;
01107 }
01108
01109 QListViewItem*
KListView::lastChild ()
const
01110
{
01111
QListViewItem* lastchild = firstChild();
01112
01113
if (lastchild)
01114
for (; lastchild->nextSibling(); lastchild = lastchild->nextSibling());
01115
01116
return lastchild;
01117 }
01118
01119 QListViewItem *
KListView::lastItem()
const
01120
{
01121
QListViewItem* last =
lastChild();
01122
01123
for (
QListViewItemIterator it (last); it.current(); ++it)
01124 last = it.current();
01125
01126
return last;
01127 }
01128
01129 KLineEdit *
KListView::renameLineEdit()
const
01130
{
01131
return d->editor;
01132 }
01133
01134 void KListView::startDrag()
01135 {
01136
QDragObject *drag =
dragObject();
01137
01138
if (!drag)
01139
return;
01140
01141
if (drag->drag() && drag->target() != viewport())
01142 emit
moved();
01143 }
01144
01145 QDragObject *
KListView::dragObject()
01146 {
01147
if (!currentItem())
01148
return 0;
01149
01150
return new QStoredDrag(
"application/x-qlistviewitem", viewport());
01151 }
01152
01153 void KListView::setItemsMovable(
bool b)
01154 {
01155 d->itemsMovable=b;
01156 }
01157
01158
bool KListView::itemsMovable()
const
01159
{
01160
return d->itemsMovable;
01161 }
01162
01163 void KListView::setItemsRenameable(
bool b)
01164 {
01165 d->itemsRenameable=b;
01166 }
01167
01168
bool KListView::itemsRenameable()
const
01169
{
01170
return d->itemsRenameable;
01171 }
01172
01173
01174 void KListView::setDragEnabled(
bool b)
01175 {
01176 d->dragEnabled=b;
01177 }
01178
01179
bool KListView::dragEnabled()
const
01180
{
01181
return d->dragEnabled;
01182 }
01183
01184 void KListView::setAutoOpen(
bool b)
01185 {
01186 d->autoOpen=b;
01187 }
01188
01189
bool KListView::autoOpen()
const
01190
{
01191
return d->autoOpen;
01192 }
01193
01194
bool KListView::dropVisualizer()
const
01195
{
01196
return d->dropVisualizer;
01197 }
01198
01199 void KListView::setDropVisualizer(
bool b)
01200 {
01201 d->dropVisualizer=b;
01202 }
01203
01204 QPtrList<QListViewItem> KListView::selectedItems()
const
01205
{
01206
QPtrList<QListViewItem> list;
01207
01208
QListViewItemIterator it(const_cast<KListView *>(
this), QListViewItemIterator::Selected);
01209
01210
for(; it.current(); ++it)
01211 list.append(it.current());
01212
01213
return list;
01214 }
01215
01216
01217 void KListView::moveItem(
QListViewItem *item,
QListViewItem *parent,
QListViewItem *after)
01218 {
01219
01220
QListViewItem *i = parent;
01221
while(i)
01222 {
01223
if(i == item)
01224
return;
01225 i = i->parent();
01226 }
01227
01228
if (after)
01229 {
01230 item->moveItem(after);
01231
return;
01232 }
01233
01234
01235
01236
01237
01238
01239
01240
if (item->parent())
01241 item->parent()->takeItem(item);
01242
else
01243
takeItem(item);
01244
01245
if (parent)
01246 parent->insertItem(item);
01247
else
01248 insertItem(item);
01249 }
01250
01251 void KListView::contentsDragEnterEvent(
QDragEnterEvent *event)
01252 {
01253
if (
acceptDrag (event))
01254 event->accept();
01255 }
01256
01257 void KListView::setDropVisualizerWidth (
int w)
01258 {
01259 d->mDropVisualizerWidth = w > 0 ? w : 1;
01260 }
01261
01262 QRect KListView::drawDropVisualizer(
QPainter *p,
QListViewItem *parent,
01263
QListViewItem *after)
01264 {
01265
QRect insertmarker;
01266
01267
if (!after && !parent)
01268 insertmarker =
QRect (0, 0, viewport()->width(), d->mDropVisualizerWidth/2);
01269
else
01270 {
01271
int level = 0;
01272
if (after)
01273 {
01274
QListViewItem* it = 0L;
01275
if (after->isOpen())
01276 {
01277
01278 it = after->firstChild();
01279
if (it)
01280
while (it->nextSibling() || it->firstChild())
01281
if ( it->nextSibling() )
01282 it = it->nextSibling();
01283
else
01284 it = it->firstChild();
01285 }
01286
01287 insertmarker = itemRect (it ? it : after);
01288 level = after->depth();
01289 }
01290
else if (parent)
01291 {
01292 insertmarker = itemRect (parent);
01293 level = parent->depth() + 1;
01294 }
01295 insertmarker.setLeft( treeStepSize() * ( level + (rootIsDecorated() ? 1 : 0) ) + itemMargin() );
01296 insertmarker.setRight (viewport()->width());
01297 insertmarker.setTop (insertmarker.bottom() - d->mDropVisualizerWidth/2 + 1);
01298 insertmarker.setBottom (insertmarker.bottom() + d->mDropVisualizerWidth/2);
01299 }
01300
01301
01302
01303
if (p)
01304 p->fillRect(insertmarker, Dense4Pattern);
01305
01306
return insertmarker;
01307 }
01308
01309 QRect KListView::drawItemHighlighter(
QPainter *painter,
QListViewItem *item)
01310 {
01311
QRect r;
01312
01313
if (item)
01314 {
01315 r = itemRect(item);
01316 r.setLeft(r.left()+(item->depth()+1)*treeStepSize());
01317
if (painter)
01318 style().drawPrimitive(QStyle::PE_FocusRect, painter, r, colorGroup(),
01319 QStyle::Style_FocusAtBorder, colorGroup().highlight());
01320 }
01321
01322
return r;
01323 }
01324
01325 void KListView::cleanItemHighlighter ()
01326 {
01327
if (d->mOldDropHighlighter.isValid())
01328 {
01329
QRect rect=d->mOldDropHighlighter;
01330 d->mOldDropHighlighter =
QRect();
01331 viewport()->repaint(rect,
true);
01332 }
01333 }
01334
01335 void KListView::rename(
QListViewItem *item,
int c)
01336 {
01337
if (d->renameable.contains(c))
01338 {
01339 ensureItemVisible(item);
01340 d->editor->load(item,c);
01341 }
01342 }
01343
01344 bool KListView::isRenameable (
int col)
const
01345
{
01346
return d->renameable.contains(col);
01347 }
01348
01349 void KListView::setRenameable (
int col,
bool yesno)
01350 {
01351
if (col>=header()->count())
return;
01352
01353 d->renameable.remove(col);
01354
if (yesno && d->renameable.find(col)==d->renameable.end())
01355 d->renameable+=col;
01356
else if (!yesno && d->renameable.find(col)!=d->renameable.end())
01357 d->renameable.remove(col);
01358 }
01359
01360
void KListView::doneEditing(
QListViewItem *item,
int row)
01361 {
01362 emit itemRenamed(item, item->text(row), row);
01363 emit itemRenamed(item);
01364 }
01365
01366 bool KListView::acceptDrag(
QDropEvent* e)
const
01367
{
01368
return acceptDrops() &&
itemsMovable() && (e->source()==viewport());
01369 }
01370
01371 void KListView::setCreateChildren(
bool b)
01372 {
01373 d->createChildren=b;
01374 }
01375
01376 bool KListView::createChildren()
const
01377
{
01378
return d->createChildren;
01379 }
01380
01381
01382
int KListView::tooltipColumn()
const
01383
{
01384
return d->tooltipColumn;
01385 }
01386
01387 void KListView::setTooltipColumn(
int column)
01388 {
01389 d->tooltipColumn=column;
01390 }
01391
01392 void KListView::setDropHighlighter(
bool b)
01393 {
01394 d->dropHighlighter=b;
01395 }
01396
01397 bool KListView::dropHighlighter()
const
01398
{
01399
return d->dropHighlighter;
01400 }
01401
01402 bool KListView::showTooltip(
QListViewItem *item,
const QPoint &,
int column)
const
01403
{
01404
return ((
tooltip(item, column).length()>0) && (column==
tooltipColumn()));
01405 }
01406
01407 QString KListView::tooltip(
QListViewItem *item,
int column)
const
01408
{
01409
return item->text(column);
01410 }
01411
01412 void KListView::setTabOrderedRenaming(
bool b)
01413 {
01414 d->tabRename = b;
01415 }
01416
01417 bool KListView::tabOrderedRenaming()
const
01418
{
01419
return d->tabRename;
01420 }
01421
01422 void KListView::keyPressEvent (
QKeyEvent* e)
01423 {
01424
01425
if (e->key() == d->contextMenuKey)
01426 {
01427 emit
menuShortCutPressed (
this, currentItem());
01428
return;
01429 }
01430
01431
if (d->selectionMode != FileManager)
01432 QListView::keyPressEvent (e);
01433
else
01434
fileManagerKeyPressEvent (e);
01435 }
01436
01437 void KListView::activateAutomaticSelection()
01438 {
01439 d->selectedBySimpleMove=
true;
01440 d->selectedUsingMouse=
false;
01441
if (currentItem()!=0)
01442 {
01443 selectAll(
false);
01444 currentItem()->setSelected(
true);
01445 currentItem()->repaint();
01446 emit selectionChanged();
01447 };
01448 }
01449
01450 void KListView::deactivateAutomaticSelection()
01451 {
01452 d->selectedBySimpleMove=
false;
01453 }
01454
01455 bool KListView::automaticSelection()
const
01456
{
01457
return d->selectedBySimpleMove;
01458 }
01459
01460 void KListView::fileManagerKeyPressEvent (
QKeyEvent* e)
01461 {
01462
01463
int e_state=(e->state() & ~Keypad);
01464
01465
int oldSelectionDirection(d->selectionDirection);
01466
01467
if ((e->key()!=Key_Shift) && (e->key()!=Key_Control)
01468 && (e->key()!=Key_Meta) && (e->key()!=Key_Alt))
01469 {
01470
if ((e_state==ShiftButton) && (!d->wasShiftEvent) && (!d->selectedBySimpleMove))
01471 selectAll(
false);
01472 d->selectionDirection=0;
01473 d->wasShiftEvent = (e_state == ShiftButton);
01474 };
01475
01476
01477
01478
01479
QListViewItem* item = currentItem();
01480
if (item==0)
return;
01481
01482
QListViewItem* repaintItem1 = item;
01483
QListViewItem* repaintItem2 = 0L;
01484
QListViewItem* visItem = 0L;
01485
01486
QListViewItem* nextItem = 0L;
01487
int items = 0;
01488
01489
bool shiftOrCtrl((e_state==ControlButton) || (e_state==ShiftButton));
01490
int selectedItems(0);
01491
for (
QListViewItem *tmpItem=firstChild(); tmpItem!=0; tmpItem=tmpItem->nextSibling())
01492
if (tmpItem->isSelected()) selectedItems++;
01493
01494
if (((selectedItems==0) || ((selectedItems==1) && (d->selectedUsingMouse)))
01495 && (e_state==NoButton)
01496 && ((e->key()==Key_Down)
01497 || (e->key()==Key_Up)
01498 || (e->key()==Key_Next)
01499 || (e->key()==Key_Prior)
01500 || (e->key()==Key_Home)
01501 || (e->key()==Key_End)))
01502 {
01503 d->selectedBySimpleMove=
true;
01504 d->selectedUsingMouse=
false;
01505 }
01506
else if (selectedItems>1)
01507 d->selectedBySimpleMove=
false;
01508
01509
bool emitSelectionChanged(
false);
01510
01511
switch (e->key())
01512 {
01513
case Key_Escape:
01514 selectAll(
false);
01515 emitSelectionChanged=
true;
01516
break;
01517
01518
case Key_Space:
01519
01520
if (d->selectedBySimpleMove)
01521 d->selectedBySimpleMove=
false;
01522 item->setSelected(!item->isSelected());
01523 emitSelectionChanged=
true;
01524
break;
01525
01526
case Key_Insert:
01527
01528
if (d->selectedBySimpleMove)
01529 {
01530 d->selectedBySimpleMove=
false;
01531
if (!item->isSelected()) item->setSelected(
true);
01532 }
01533
else
01534 {
01535 item->setSelected(!item->isSelected());
01536 };
01537
01538 nextItem=item->itemBelow();
01539
01540
if (nextItem!=0)
01541 {
01542 repaintItem2=nextItem;
01543 visItem=nextItem;
01544 setCurrentItem(nextItem);
01545 };
01546 d->selectionDirection=1;
01547 emitSelectionChanged=
true;
01548
break;
01549
01550
case Key_Down:
01551 nextItem=item->itemBelow();
01552
01553
if (shiftOrCtrl)
01554 {
01555 d->selectionDirection=1;
01556
if (d->selectedBySimpleMove)
01557 d->selectedBySimpleMove=
false;
01558
else
01559 {
01560
if (oldSelectionDirection!=-1)
01561 {
01562 item->setSelected(!item->isSelected());
01563 emitSelectionChanged=
true;
01564 };
01565 };
01566 }
01567
else if ((d->selectedBySimpleMove) && (nextItem!=0))
01568 {
01569 item->setSelected(
false);
01570 emitSelectionChanged=
true;
01571 };
01572
01573
if (nextItem!=0)
01574 {
01575
if (d->selectedBySimpleMove)
01576 nextItem->setSelected(
true);
01577 repaintItem2=nextItem;
01578 visItem=nextItem;
01579 setCurrentItem(nextItem);
01580 };
01581
break;
01582
01583
case Key_Up:
01584 nextItem=item->itemAbove();
01585 d->selectionDirection=-1;
01586
01587
01588
01589
if (shiftOrCtrl)
01590 {
01591
if (d->selectedBySimpleMove)
01592 d->selectedBySimpleMove=
false;
01593
else
01594 {
01595
if (oldSelectionDirection!=1)
01596 {
01597 item->setSelected(!item->isSelected());
01598 emitSelectionChanged=
true;
01599 };
01600 }
01601 }
01602
else if ((d->selectedBySimpleMove) && (nextItem!=0))
01603 {
01604 item->setSelected(
false);
01605 emitSelectionChanged=
true;
01606 };
01607
01608
if (nextItem!=0)
01609 {
01610
if (d->selectedBySimpleMove)
01611 nextItem->setSelected(
true);
01612 repaintItem2=nextItem;
01613 visItem=nextItem;
01614 setCurrentItem(nextItem);
01615 };
01616
break;
01617
01618
case Key_End:
01619
01620 nextItem=item;
01621
if (d->selectedBySimpleMove)
01622 item->setSelected(
false);
01623
if (shiftOrCtrl)
01624 d->selectedBySimpleMove=
false;
01625
01626
while(nextItem!=0)
01627 {
01628
if (shiftOrCtrl)
01629 nextItem->setSelected(!nextItem->isSelected());
01630
if (nextItem->itemBelow()==0)
01631 {
01632
if (d->selectedBySimpleMove)
01633 nextItem->setSelected(
true);
01634 repaintItem2=nextItem;
01635 visItem=nextItem;
01636 setCurrentItem(nextItem);
01637 }
01638 nextItem=nextItem->itemBelow();
01639 }
01640 emitSelectionChanged=
true;
01641
break;
01642
01643
case Key_Home:
01644
01645 nextItem = firstChild();
01646 visItem = nextItem;
01647 repaintItem2 = visItem;
01648
if (d->selectedBySimpleMove)
01649 item->setSelected(
false);
01650
if (shiftOrCtrl)
01651 {
01652 d->selectedBySimpleMove=
false;
01653
01654
while ( nextItem != item )
01655 {
01656 nextItem->setSelected( !nextItem->isSelected() );
01657 nextItem = nextItem->itemBelow();
01658 }
01659 item->setSelected( !item->isSelected() );
01660 }
01661 setCurrentItem( firstChild() );
01662 emitSelectionChanged=
true;
01663
break;
01664
01665
case Key_Next:
01666 items=visibleHeight()/item->height();
01667 nextItem=item;
01668
if (d->selectedBySimpleMove)
01669 item->setSelected(
false);
01670
if (shiftOrCtrl)
01671 {
01672 d->selectedBySimpleMove=
false;
01673 d->selectionDirection=1;
01674 };
01675
01676
for (
int i=0; i<items; i++)
01677 {
01678
if (shiftOrCtrl)
01679 nextItem->setSelected(!nextItem->isSelected());
01680
01681
if ((i==items-1) || (nextItem->itemBelow()==0))
01682
01683 {
01684
if (shiftOrCtrl)
01685 nextItem->setSelected(!nextItem->isSelected());
01686
if (d->selectedBySimpleMove)
01687 nextItem->setSelected(
true);
01688 ensureItemVisible(nextItem);
01689 setCurrentItem(nextItem);
01690 update();
01691
if ((shiftOrCtrl) || (d->selectedBySimpleMove))
01692 {
01693 emit selectionChanged();
01694 }
01695
return;
01696 }
01697 nextItem=nextItem->itemBelow();
01698 }
01699
break;
01700
01701
case Key_Prior:
01702 items=visibleHeight()/item->height();
01703 nextItem=item;
01704
if (d->selectedBySimpleMove)
01705 item->setSelected(
false);
01706
if (shiftOrCtrl)
01707 {
01708 d->selectionDirection=-1;
01709 d->selectedBySimpleMove=
false;
01710 };
01711
01712
for (
int i=0; i<items; i++)
01713 {
01714
if ((nextItem!=item) &&(shiftOrCtrl))
01715 nextItem->setSelected(!nextItem->isSelected());
01716
01717
if ((i==items-1) || (nextItem->itemAbove()==0))
01718
01719 {
01720
if (d->selectedBySimpleMove)
01721 nextItem->setSelected(
true);
01722 ensureItemVisible(nextItem);
01723 setCurrentItem(nextItem);
01724 update();
01725
if ((shiftOrCtrl) || (d->selectedBySimpleMove))
01726 {
01727 emit selectionChanged();
01728 }
01729
return;
01730 }
01731 nextItem=nextItem->itemAbove();
01732 }
01733
break;
01734
01735
case Key_Minus:
01736
if ( item->isOpen() )
01737 setOpen( item,
false );
01738
break;
01739
case Key_Plus:
01740
if ( !item->isOpen() && (item->isExpandable() || item->childCount()) )
01741 setOpen( item,
true );
01742
break;
01743
default:
01744
bool realKey = ((e->key()!=Key_Shift) && (e->key()!=Key_Control)
01745 && (e->key()!=Key_Meta) && (e->key()!=Key_Alt));
01746
01747
bool selectCurrentItem = (d->selectedBySimpleMove) && (item->isSelected());
01748
if (realKey && selectCurrentItem)
01749 item->setSelected(
false);
01750
01751 QListView::SelectionMode oldSelectionMode = selectionMode();
01752 setSelectionMode (QListView::Multi);
01753 QListView::keyPressEvent (e);
01754 setSelectionMode (oldSelectionMode);
01755
if (realKey && selectCurrentItem)
01756 {
01757 currentItem()->setSelected(
true);
01758 emitSelectionChanged=
true;
01759 }
01760 repaintItem2=currentItem();
01761
if (realKey)
01762 visItem=currentItem();
01763
break;
01764 }
01765
01766
if (visItem)
01767 ensureItemVisible(visItem);
01768
01769
QRect ir;
01770
if (repaintItem1)
01771 ir = ir.unite( itemRect(repaintItem1) );
01772
if (repaintItem2)
01773 ir = ir.unite( itemRect(repaintItem2) );
01774
01775
if ( !ir.isEmpty() )
01776 {
01777
if ( ir.x() < 0 )
01778 ir.moveBy( -ir.x(), 0 );
01779 viewport()->repaint( ir,
false );
01780 }
01781
01782
01783
01784
01785 update();
01786
if (emitSelectionChanged)
01787 emit selectionChanged();
01788 }
01789
01790 void KListView::setSelectionModeExt (SelectionModeExt mode)
01791 {
01792 d->selectionMode = mode;
01793
01794
switch (mode)
01795 {
01796
case Single:
01797
case Multi:
01798
case Extended:
01799
case NoSelection:
01800 setSelectionMode (static_cast<QListView::SelectionMode>(static_cast<int>(mode)));
01801
break;
01802
01803
case FileManager:
01804 setSelectionMode (QListView::Extended);
01805
break;
01806
01807
default:
01808
kdWarning () <<
"Warning: illegal selection mode " << int(mode) <<
" set!" <<
endl;
01809
break;
01810 }
01811 }
01812
01813 KListView::SelectionModeExt KListView::selectionModeExt ()
const
01814
{
01815
return d->selectionMode;
01816 }
01817
01818 int KListView::itemIndex(
const QListViewItem *item )
const
01819
{
01820
if ( !item )
01821
return -1;
01822
01823
if ( item == firstChild() )
01824
return 0;
01825
else {
01826
QListViewItemIterator it(firstChild());
01827 uint j = 0;
01828
for (; it.current() && it.current() != item; ++it, ++j );
01829
01830
if( !it.current() )
01831
return -1;
01832
01833
return j;
01834 }
01835 }
01836
01837 QListViewItem*
KListView::itemAtIndex(
int index)
01838 {
01839
if (index<0)
01840
return 0;
01841
01842
int j(0);
01843
for (
QListViewItemIterator it=firstChild(); it.current(); it++)
01844 {
01845
if (j==index)
01846
return it.current();
01847 j++;
01848 };
01849
return 0;
01850 }
01851
01852
01853 void KListView::emitContextMenu (
KListView*,
QListViewItem* i)
01854 {
01855
QPoint p;
01856
01857
if (i)
01858 p = viewport()->mapToGlobal(itemRect(i).center());
01859
else
01860 p = mapToGlobal(rect().center());
01861
01862 emit
contextMenu (
this, i, p);
01863 }
01864
01865 void KListView::emitContextMenu (
QListViewItem* i,
const QPoint& p,
int)
01866 {
01867 emit
contextMenu (
this, i, p);
01868 }
01869
01870 void KListView::setAcceptDrops (
bool val)
01871 {
01872 QListView::setAcceptDrops (val);
01873 viewport()->setAcceptDrops (val);
01874 }
01875
01876
int KListView::dropVisualizerWidth ()
const
01877
{
01878
return d->mDropVisualizerWidth;
01879 }
01880
01881
01882 void KListView::viewportPaintEvent(
QPaintEvent *e)
01883 {
01884 d->paintAbove = 0;
01885 d->paintCurrent = 0;
01886 d->paintBelow = 0;
01887 d->painting =
true;
01888
01889 QListView::viewportPaintEvent(e);
01890
01891
if (d->mOldDropVisualizer.isValid() && e->rect().intersects(d->mOldDropVisualizer))
01892 {
01893
QPainter painter(viewport());
01894
01895
01896 painter.fillRect(d->mOldDropVisualizer, Dense4Pattern);
01897 }
01898
if (d->mOldDropHighlighter.isValid() && e->rect().intersects(d->mOldDropHighlighter))
01899 {
01900
QPainter painter(viewport());
01901
01902
01903 style().drawPrimitive(QStyle::PE_FocusRect, &painter, d->mOldDropHighlighter, colorGroup(),
01904 QStyle::Style_FocusAtBorder);
01905 }
01906 d->painting =
false;
01907 }
01908
01909 void KListView::setFullWidth()
01910 {
01911
setFullWidth(
true);
01912 }
01913
01914 void KListView::setFullWidth(
bool fullWidth)
01915 {
01916 d->fullWidth = fullWidth;
01917 header()->setStretchEnabled(fullWidth, columns()-1);
01918 }
01919
01920
bool KListView::fullWidth()
const
01921
{
01922
return d->fullWidth;
01923 }
01924
01925 int KListView::addColumn(
const QString& label,
int width)
01926 {
01927
int result = QListView::addColumn(label, width);
01928
if (d->fullWidth) {
01929 header()->setStretchEnabled(
false, columns()-2);
01930 header()->setStretchEnabled(
true, columns()-1);
01931 }
01932
return result;
01933 }
01934
01935 int KListView::addColumn(
const QIconSet& iconset,
const QString& label,
int width)
01936 {
01937
int result = QListView::addColumn(iconset, label, width);
01938
if (d->fullWidth) {
01939 header()->setStretchEnabled(
false, columns()-2);
01940 header()->setStretchEnabled(
true, columns()-1);
01941 }
01942
return result;
01943 }
01944
01945 void KListView::removeColumn(
int index)
01946 {
01947 QListView::removeColumn(index);
01948
if (d->fullWidth && index == columns()) header()->setStretchEnabled(
true, columns()-1);
01949 }
01950
01951 void KListView::viewportResizeEvent(
QResizeEvent* e)
01952 {
01953 QListView::viewportResizeEvent(e);
01954 }
01955
01956
const QColor &
KListView::alternateBackground()
const
01957
{
01958
return d->alternateBackground;
01959 }
01960
01961 void KListView::setAlternateBackground(
const QColor &c)
01962 {
01963 d->alternateBackground = c;
01964 repaint();
01965 }
01966
01967 void KListView::saveLayout(
KConfig *config,
const QString &group)
const
01968
{
01969
KConfigGroupSaver saver(config, group);
01970
QStringList widths, order;
01971
for (
int i = 0; i < columns(); ++i)
01972 {
01973 widths << QString::number(columnWidth(i));
01974 order << QString::number(header()->mapToIndex(i));
01975 }
01976 config->
writeEntry(
"ColumnWidths", widths);
01977 config->
writeEntry(
"ColumnOrder", order);
01978 config->
writeEntry(
"SortColumn", d->sortColumn);
01979 config->
writeEntry(
"SortAscending", d->sortAscending);
01980 }
01981
01982 void KListView::restoreLayout(
KConfig *config,
const QString &group)
01983 {
01984
KConfigGroupSaver saver(config, group);
01985
QStringList cols = config->
readListEntry(
"ColumnWidths");
01986
int i = 0;
01987
for (QStringList::ConstIterator it = cols.begin(); it != cols.end(); ++it)
01988 setColumnWidth(i++, (*it).toInt());
01989
01990 cols = config->
readListEntry(
"ColumnOrder");
01991 i = 0;
01992
for (QStringList::ConstIterator it = cols.begin(); it != cols.end(); ++it)
01993 header()->moveSection(i++, (*it).toInt());
01994
if (config->
hasKey(
"SortColumn"))
01995
setSorting(config->
readNumEntry(
"SortColumn"), config->
readBoolEntry(
"SortAscending",
true));
01996 }
01997
01998 void KListView::setSorting(
int column,
bool ascending)
01999 {
02000 d->sortColumn = column;
02001 d->sortAscending = ascending;
02002 QListView::setSorting(column, ascending);
02003
QListViewItem* item = firstChild();
02004
while ( item ) {
02005
KListViewItem *kItem = dynamic_cast<KListViewItem*>(item);
02006
if (kItem) kItem->
m_known =
false;
02007 item = item->itemBelow();
02008 }
02009 }
02010
02011 int KListView::columnSorted(
void)
const
02012
{
02013
return d->sortColumn;
02014 }
02015
02016 bool KListView::ascendingSort(
void)
const
02017
{
02018
return d->sortAscending;
02019 }
02020
02021 void KListView::takeItem(
QListViewItem *item)
02022 {
02023
if(item && item == d->editor->currentItem())
02024 d->editor->terminate();
02025
02026 QListView::takeItem(item);
02027 }
02028
02029 void KListView::disableAutoSelection()
02030 {
02031
if ( d->disableAutoSelection )
02032
return;
02033
02034 d->disableAutoSelection =
true;
02035 d->autoSelect.stop();
02036 d->autoSelectDelay = -1;
02037 }
02038
02039 void KListView::resetAutoSelection()
02040 {
02041
if ( !d->disableAutoSelection )
02042
return;
02043
02044 d->disableAutoSelection =
false;
02045 d->autoSelectDelay =
KGlobalSettings::autoSelectDelay();
02046 }
02047
02048
02049
02050 KListViewItem::KListViewItem(
QListView *parent)
02051 :
QListViewItem(parent)
02052 {
02053 init();
02054 }
02055
02056 KListViewItem::KListViewItem(
QListViewItem *parent)
02057 :
QListViewItem(parent)
02058 {
02059 init();
02060 }
02061
02062
KListViewItem::KListViewItem(
QListView *parent,
QListViewItem *after)
02063 :
QListViewItem(parent, after)
02064 {
02065 init();
02066 }
02067
02068
KListViewItem::KListViewItem(
QListViewItem *parent,
QListViewItem *after)
02069 :
QListViewItem(parent, after)
02070 {
02071 init();
02072 }
02073
02074
KListViewItem::KListViewItem(
QListView *parent,
02075
QString label1,
QString label2,
QString label3,
QString label4,
02076
QString label5,
QString label6,
QString label7,
QString label8)
02077 :
QListViewItem(parent, label1, label2, label3, label4, label5, label6, label7, label8)
02078 {
02079 init();
02080 }
02081
02082
KListViewItem::KListViewItem(
QListViewItem *parent,
02083
QString label1,
QString label2,
QString label3,
QString label4,
02084
QString label5,
QString label6,
QString label7,
QString label8)
02085 :
QListViewItem(parent, label1, label2, label3, label4, label5, label6, label7, label8)
02086 {
02087 init();
02088 }
02089
02090
KListViewItem::KListViewItem(
QListView *parent,
QListViewItem *after,
02091
QString label1,
QString label2,
QString label3,
QString label4,
02092
QString label5,
QString label6,
QString label7,
QString label8)
02093 :
QListViewItem(parent, after, label1, label2, label3, label4, label5, label6, label7, label8)
02094 {
02095 init();
02096 }
02097
02098
KListViewItem::KListViewItem(
QListViewItem *parent,
QListViewItem *after,
02099
QString label1,
QString label2,
QString label3,
QString label4,
02100
QString label5,
QString label6,
QString label7,
QString label8)
02101 :
QListViewItem(parent, after, label1, label2, label3, label4, label5, label6, label7, label8)
02102 {
02103 init();
02104 }
02105
02106 KListViewItem::~KListViewItem()
02107 {
02108 }
02109
02110
void KListViewItem::init()
02111 {
02112 m_odd = m_known =
false;
02113
KListView *lv = static_cast<KListView *>(listView());
02114 setDragEnabled( dragEnabled() || lv->
dragEnabled() );
02115 }
02116
02117 const QColor &
KListViewItem::backgroundColor()
02118 {
02119
if (
isAlternate())
02120
return static_cast< KListView* >(listView())->alternateBackground();
02121
return listView()->viewport()->colorGroup().base();
02122 }
02123
02124 bool KListViewItem::isAlternate()
02125 {
02126
KListView *lv = static_cast<KListView *>(listView());
02127
if (lv && lv->
alternateBackground().isValid())
02128 {
02129
KListViewItem *above;
02130
02131
02132
02133
02134
02135
02136
02137
02138
02139
02140
02141
02142
02143
02144
02145
02146
02147
02148
if (lv->
d->painting) {
02149
if (lv->
d->paintCurrent !=
this)
02150 {
02151 lv->
d->paintAbove = lv->
d->paintBelow ==
this ? lv->
d->paintCurrent : itemAbove();
02152 lv->
d->paintCurrent =
this;
02153 lv->
d->paintBelow = itemBelow();
02154 }
02155
02156 above = dynamic_cast<KListViewItem *>(lv->
d->paintAbove);
02157 }
02158
else
02159 {
02160 above = dynamic_cast<KListViewItem *>(itemAbove());
02161 }
02162
02163 m_known = above ? above->
m_known :
true;
02164
if (m_known)
02165 {
02166 m_odd = above ? !above->
m_odd :
false;
02167 }
02168
else
02169 {
02170
KListViewItem *item;
02171
bool previous =
true;
02172
if (parent())
02173 {
02174 item = dynamic_cast<KListViewItem *>(parent());
02175
if (item)
02176 previous = item->
m_odd;
02177 item = dynamic_cast<KListViewItem *>(parent()->firstChild());
02178 }
02179
else
02180 {
02181 item = dynamic_cast<KListViewItem *>(lv->firstChild());
02182 }
02183
02184
while(item)
02185 {
02186 item->
m_odd = previous = !previous;
02187 item->
m_known =
true;
02188 item = dynamic_cast<KListViewItem *>(item->nextSibling());
02189 }
02190 }
02191
return m_odd;
02192 }
02193
return false;
02194 }
02195
02196
void KListViewItem::paintCell(
QPainter *p,
const QColorGroup &cg,
int column,
int width,
int alignment)
02197 {
02198
QColorGroup _cg = cg;
02199
const QPixmap *pm = listView()->viewport()->backgroundPixmap();
02200
if (pm && !pm->isNull())
02201 {
02202 _cg.setBrush(QColorGroup::Base,
QBrush(
backgroundColor(), *pm));
02203
QPoint o = p->brushOrigin();
02204 p->setBrushOrigin( o.x()-listView()->contentsX(), o.y()-listView()->contentsY() );
02205 }
02206
else if (
isAlternate())
02207
if (listView()->viewport()->backgroundMode()==Qt::FixedColor)
02208 _cg.setColor(QColorGroup::Background, static_cast< KListView* >(listView())->alternateBackground());
02209
else
02210 _cg.setColor(QColorGroup::Base, static_cast< KListView* >(listView())->alternateBackground());
02211
02212 QListViewItem::paintCell(p, _cg, column, width, alignment);
02213 }
02214
02215
void KListView::virtual_hook(
int,
void* )
02216 { }
02217
02218
#include "klistview.moc"
02219
#include "klistviewlineedit.moc"
02220
02221