File: krinterbriefview.cpp

package info (click to toggle)
krusader 2%3A2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,448 kB
  • sloc: cpp: 56,112; ansic: 1,187; xml: 811; sh: 23; makefile: 3
file content (669 lines) | stat: -rw-r--r-- 19,214 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
    SPDX-FileCopyrightText: 2009 Csaba Karai <cskarai@freemail.hu>
    SPDX-FileCopyrightText: 2009-2022 Krusader Krew <https://krusader.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "krinterbriefview.h"

// QtCore
#include <QDebug>
#include <QDir>
#include <QHashIterator>
#include <QItemSelection>
#include <QItemSelectionRange>
// QtGui
#include <QFontMetrics>
#include <QPainter>
#include <QRegion>
// QtWidgets
#include <QApplication>
#include <QFileSystemModel>
#include <QMenu>
#include <QScrollBar>

#include <KConfig>
#include <KDirLister>
#include <KLocalizedString>

#include "../FileSystem/krpermhandler.h"
#include "../GUI/krstyleproxy.h"
#include "../compat.h"
#include "../defaults.h"
#include "../krcolorcache.h"
#include "krmousehandler.h"
#include "krviewfactory.h"
#include "krviewitem.h"
#include "krviewitemdelegate.h"
#include "listmodel.h"

#define MAX_BRIEF_COLS 5

KrInterBriefView::KrInterBriefView(QWidget *parent, KrViewInstance &instance, KConfig *cfg)
    : QAbstractItemView(parent)
    , KrInterView(instance, cfg, this)
    , _header(nullptr)
{
    setWidget(this);
    setModel(_model);

    setSelectionMode(QAbstractItemView::NoSelection);
    setSelectionModel(new DummySelectionModel(_model, this));

    KConfigGroup grpSvr(_config, "Look&Feel");
    _viewFont = grpSvr.readEntry("Filelist Font", _FilelistFont);

    auto *style = new KrStyleProxy();
    style->setParent(this);
    setStyle(style);
    viewport()->setStyle(style); // for custom tooltip delay
    setItemDelegate(new KrViewItemDelegate());
    setMouseTracking(true);
    setAcceptDrops(true);
    setDropIndicatorShown(true);

    connect(_mouseHandler, &KrMouseHandler::renameCurrentItem, this, &KrInterBriefView::renameCurrentItem);

    _model->setExtensionEnabled(false);
    _model->setAlternatingTable(true);
    connect(_model, &ListModel::layoutChanged, this, &KrInterBriefView::updateGeometries);
}

KrInterBriefView::~KrInterBriefView()
{
    delete _properties;
    _properties = nullptr;

    delete _operator;
    _operator = nullptr;
}

void KrInterBriefView::doRestoreSettings(KConfigGroup group)
{
    _properties->numberOfColumns = group.readEntry("Number Of Brief Columns", _NumberOfBriefColumns);
    if (_properties->numberOfColumns < 1)
        _properties->numberOfColumns = 1;
    else if (_properties->numberOfColumns > MAX_BRIEF_COLS)
        _properties->numberOfColumns = MAX_BRIEF_COLS;

    _numOfColumns = _properties->numberOfColumns;

    KrInterView::doRestoreSettings(group);

    updateGeometries();
}

void KrInterBriefView::saveSettings(KConfigGroup grp, KrViewProperties::PropertyType properties)
{
    KrInterView::saveSettings(grp, properties);
    if (properties & KrViewProperties::PropColumns)
        grp.writeEntry("Number Of Brief Columns", _numOfColumns);
}

int KrInterBriefView::itemsPerPage()
{
    int height = getItemHeight();
    if (height == 0)
        height++;
    int numRows = viewport()->height() / height;
    return numRows;
}
void KrInterBriefView::updateView()
{
}

void KrInterBriefView::setup()
{
    _header = new QHeaderView(Qt::Horizontal, this);
    _header->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    _header->setParent(this);
    _header->setModel(_model);
    _header->hideSection(KrViewProperties::Type);
    _header->hideSection(KrViewProperties::Permissions);
    _header->hideSection(KrViewProperties::KrPermissions);
    _header->hideSection(KrViewProperties::Owner);
    _header->hideSection(KrViewProperties::Group);
    _header->hideSection(KrViewProperties::Changed);
    _header->hideSection(KrViewProperties::Accessed);
    _header->setStretchLastSection(true);
    _header->setSectionResizeMode(QHeaderView::Fixed);
    _header->setSectionsClickable(true);
    _header->setSortIndicatorShown(true);
    connect(_header, &QHeaderView::sortIndicatorChanged, _model, QOverload<int, Qt::SortOrder>::of(&ListModel::sort));
    _header->installEventFilter(this);

    _numOfColumns = _properties->numberOfColumns;

    setSortMode(_properties->sortColumn, (_properties->sortOptions & KrViewProperties::Descending));
}

void KrInterBriefView::keyPressEvent(QKeyEvent *e)
{
    if (!e || !_model->ready())
        return; // subclass bug

    if (handleKeyEvent(e))
        return;

    QAbstractItemView::keyPressEvent(e);
}

bool KrInterBriefView::handleKeyEvent(QKeyEvent *e)
{
    if (((e->key() != Qt::Key_Left && e->key() != Qt::Key_Right) || (e->modifiers() == Qt::ControlModifier)) && (KrView::handleKeyEvent(e)))
        // did the view class handled the event?
        return true;

    switch (e->key()) {
    case Qt::Key_Right: {
        KrViewItem *i = getCurrentKrViewItem();
        KrViewItem *newCurrent = i;

        if (!i)
            break;

        int num = itemsPerPage() + 1;

        if (e->modifiers() & Qt::ShiftModifier)
            i->setSelected(!i->isSelected());

        while (i && num > 0) {
            if (e->modifiers() & Qt::ShiftModifier)
                i->setSelected(!i->isSelected());
            newCurrent = i;
            i = getNext(i);
            num--;
        }

        if (newCurrent) {
            setCurrentKrViewItem(newCurrent);
            makeCurrentVisible();
        }
        if (e->modifiers() & Qt::ShiftModifier)
            op()->emitSelectionChanged();
        return true;
    }
    case Qt::Key_Left: {
        KrViewItem *i = getCurrentKrViewItem();
        KrViewItem *newCurrent = i;

        if (!i)
            break;

        int num = itemsPerPage() + 1;

        if (e->modifiers() & Qt::ShiftModifier)
            i->setSelected(!i->isSelected());

        while (i && num > 0) {
            if (e->modifiers() & Qt::ShiftModifier)
                i->setSelected(!i->isSelected());
            newCurrent = i;
            i = getPrev(i);
            num--;
        }

        if (newCurrent) {
            setCurrentKrViewItem(newCurrent);
            makeCurrentVisible();
        }
        if (e->modifiers() & Qt::ShiftModifier)
            op()->emitSelectionChanged();
        return true;
    }
    }

    return false;
}

void KrInterBriefView::wheelEvent(QWheelEvent *ev)
{
    if (!_mouseHandler->wheelEvent(ev))
        QApplication::sendEvent(horizontalScrollBar(), ev);
}

bool KrInterBriefView::eventFilter(QObject *object, QEvent *event)
{
    if (object == _header) {
        if (event->type() == QEvent::ContextMenu) {
            auto *me = dynamic_cast<QContextMenuEvent *>(event);
            showContextMenu(me->globalPos());
            return true;
        }
    }
    return false;
}

void KrInterBriefView::showContextMenu(const QPoint &p)
{
    QMenu popup(this);
    popup.setTitle(i18n("Columns"));

    int COL_ID = 14700;

    for (int i = 1; i <= MAX_BRIEF_COLS; i++) {
        QAction *act = popup.addAction(QString("%1").arg(i));
        act->setData(QVariant(COL_ID + i));
        act->setCheckable(true);
        act->setChecked(properties()->numberOfColumns == i);
    }

    QAction *res = popup.exec(p);
    int result = -1;
    if (res && res->data().canConvert<int>())
        result = res->data().toInt();

    if (result > COL_ID && result <= COL_ID + MAX_BRIEF_COLS) {
        _properties->numberOfColumns = result - COL_ID;
        _numOfColumns = _properties->numberOfColumns;
        updateGeometries();
        op()->settingsChanged(KrViewProperties::PropColumns);
    }
}

QRect KrInterBriefView::visualRect(const QModelIndex &ndx) const
{
    int width = (viewport()->width()) / _numOfColumns;
    if ((viewport()->width()) % _numOfColumns)
        width++;
    int height = getItemHeight();
    int numRows = viewport()->height() / height;
    if (numRows == 0)
        numRows++;
    int x = width * (ndx.row() / numRows);
    int y = height * (ndx.row() % numRows);
    return mapToViewport(QRect(x, y, width, height));
}

void KrInterBriefView::scrollTo(const QModelIndex &ndx, QAbstractItemView::ScrollHint hint)
{
    const QRect rect = visualRect(ndx);
    if (hint == EnsureVisible && viewport()->rect().contains(rect)) {
        setDirtyRegion(rect);
        return;
    }

    const QRect area = viewport()->rect();

    const bool leftOf = rect.left() < area.left();
    const bool rightOf = rect.right() > area.right();

    int horizontalValue = horizontalScrollBar()->value();

    if (leftOf)
        horizontalValue -= area.left() - rect.left();
    else if (rightOf)
        horizontalValue += rect.right() - area.right();

    horizontalScrollBar()->setValue(horizontalValue);
}

QModelIndex KrInterBriefView::indexAt(const QPoint &p) const
{
    int x = p.x() + horizontalOffset();
    int y = p.y() + verticalOffset();

    int itemWidth = (viewport()->width()) / _numOfColumns;
    if ((viewport()->width()) % _numOfColumns)
        itemWidth++;
    int itemHeight = getItemHeight();

    int numRows = viewport()->height() / itemHeight;
    if (numRows == 0)
        numRows++;

    int row = y / itemHeight;
    int col = x / itemWidth;

    int numColsTotal = _model->rowCount() / numRows;
    if (_model->rowCount() % numRows)
        numColsTotal++;

    if (row < numRows && col < numColsTotal)
        return _model->index((col * numRows) + row, 0);

    return QModelIndex();
}

QModelIndex KrInterBriefView::moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers)
{
    if (_model->rowCount() == 0)
        return QModelIndex();

    QModelIndex current = currentIndex();
    if (!current.isValid())
        return _model->index(0, 0);

    switch (cursorAction) {
    case MoveLeft:
    case MovePageDown: {
        int newRow = current.row() - itemsPerPage();
        if (newRow < 0)
            newRow = 0;
        return _model->index(newRow, 0);
    }
    case MoveRight:
    case MovePageUp: {
        int newRow = current.row() + itemsPerPage();
        if (newRow >= _model->rowCount())
            newRow = _model->rowCount() - 1;
        return _model->index(newRow, 0);
    }
    case MovePrevious:
    case MoveUp: {
        int newRow = current.row() - 1;
        if (newRow < 0)
            newRow = 0;
        return _model->index(newRow, 0);
    }
    case MoveNext:
    case MoveDown: {
        int newRow = current.row() + 1;
        if (newRow >= _model->rowCount())
            newRow = _model->rowCount() - 1;
        return _model->index(newRow, 0);
    }
    case MoveHome:
        return _model->index(0, 0);
    case MoveEnd:
        return _model->index(_model->rowCount() - 1, 0);
    }

    return current;
}

int KrInterBriefView::horizontalOffset() const
{
    return horizontalScrollBar()->value();
}

int KrInterBriefView::verticalOffset() const
{
    return 0;
}

bool KrInterBriefView::isIndexHidden(const QModelIndex &ndx) const
{
    return ndx.column() != 0;
}

void KrInterBriefView::paintEvent(QPaintEvent *e)
{
    QStyleOptionViewItem option;
    initViewItemOption(&option);
    option.widget = this;
    option.decorationSize = QSize(_fileIconSize, _fileIconSize);
    option.decorationPosition = QStyleOptionViewItem::Left;
    QPainter painter(viewport());

    QModelIndex curr = currentIndex();

    QVector<QModelIndex> intersectVector;
    QRect area = e->rect();
    area.adjust(horizontalOffset(), verticalOffset(), horizontalOffset(), verticalOffset());
    intersectionSet(area, intersectVector);

    for (const QModelIndex &mndx : std::as_const(intersectVector)) {
        option.rect = visualRect(mndx);
        painter.save();

        itemDelegate()->paint(&painter, option, mndx);

        // (always) draw dashed line border around current item row
        const bool isCurrent = curr.isValid() && curr.row() == mndx.row();
        if (isCurrent && drawCurrent()) {
            QStyleOptionFocusRect o;
            o.QStyleOption::operator=(option);
            QPalette::ColorGroup cg = QPalette::Normal;
            o.backgroundColor = option.palette.color(cg, QPalette::Window);

            style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, &painter);
        }

        painter.restore();
    }
}

int KrInterBriefView::getItemHeight() const
{
    int textHeight = QFontMetrics(_viewFont).height();
    int height = textHeight;
    int iconSize = 0;
    if (properties()->displayIcons)
        iconSize = _fileIconSize;
    if (iconSize > textHeight)
        height = iconSize;
    if (height == 0)
        height++;
    return height;
}

void KrInterBriefView::updateGeometries()
{
    if (_header) {
        QSize hint = _header->sizeHint();
        setViewportMargins(0, hint.height(), 0, 0);
        QRect vg = viewport()->geometry();
        QRect geometryRect(vg.left(), vg.top() - hint.height(), vg.width(), hint.height());
        _header->setGeometry(geometryRect);

        int items = 0;
        for (int i = 0; i != _header->count(); i++)
            if (!_header->isSectionHidden(i))
                items++;
        if (items == 0)
            items++;

        int sectWidth = viewport()->width() / items;
        for (int i = 0; i != _header->count(); i++)
            if (!_header->isSectionHidden(i))
                _header->resizeSection(i, sectWidth);

        QMetaObject::invokeMethod(_header, "updateGeometries");
    }

    if (_model->rowCount() <= 0)
        horizontalScrollBar()->setRange(0, 0);
    else {
        int itemsPerColumn = viewport()->height() / getItemHeight();
        if (itemsPerColumn <= 0)
            itemsPerColumn = 1;
        int columnWidth = (viewport()->width()) / _numOfColumns;
        if ((viewport()->width()) % _numOfColumns)
            columnWidth++;
        int maxWidth = _model->rowCount() / itemsPerColumn;
        if (_model->rowCount() % itemsPerColumn)
            maxWidth++;
        maxWidth *= columnWidth;
        if (maxWidth > viewport()->width()) {
            horizontalScrollBar()->setSingleStep(columnWidth);
            horizontalScrollBar()->setPageStep(columnWidth * _numOfColumns);
            horizontalScrollBar()->setRange(0, maxWidth - viewport()->width());
        } else {
            horizontalScrollBar()->setRange(0, 0);
        }
    }

    QAbstractItemView::updateGeometries();
}

void KrInterBriefView::setSortMode(KrViewProperties::ColumnType sortColumn, bool descending)
{
    Qt::SortOrder sortDir = descending ? Qt::DescendingOrder : Qt::AscendingOrder;
    _header->setSortIndicator(sortColumn, sortDir);
}

int KrInterBriefView::elementWidth(const QModelIndex &index)
{
    QString text = index.data(Qt::DisplayRole).toString();

    int textWidth = QFontMetrics(_viewFont).horizontalAdvance(text);

    const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
    textWidth += 2 * textMargin;

    QVariant decor = index.data(Qt::DecorationRole);
    if (decor.isValid() && decor.typeId() == QMetaType::QPixmap) {
        QPixmap p = decor.value<QPixmap>();
        textWidth += p.width() + 2 * textMargin;
    }

    return textWidth;
}

void KrInterBriefView::intersectionSet(const QRect &rect, QVector<QModelIndex> &ndxList)
{
    int maxNdx = _model->rowCount();
    int width = (viewport()->width()) / _numOfColumns;
    if ((viewport()->width()) % _numOfColumns)
        width++;

    int height = getItemHeight();
    int items = viewport()->height() / height;
    if (items == 0)
        items++;

    int xmin = -1;
    int ymin = -1;
    int xmax = -1;
    int ymax = -1;

    xmin = rect.x() / width;
    ymin = rect.y() / height;
    xmax = (rect.x() + rect.width()) / width;
    if ((rect.x() + rect.width()) % width)
        xmax++;
    ymax = (rect.y() + rect.height()) / height;
    if ((rect.y() + rect.height()) % height)
        ymax++;

    for (int i = ymin; i < ymax; i++)
        for (int j = xmin; j < xmax; j++) {
            int ndx = j * items + i;
            if (ndx < maxNdx)
                ndxList.append(_model->index(ndx, 0));
        }
}

QRect KrInterBriefView::itemRect(const FileItem *item)
{
    return visualRect(_model->fileItemIndex(item));
}

void KrInterBriefView::copySettingsFrom(KrView *other)
{
    if (other->instance() == instance()) { // the other view is of the same type
        auto *v = dynamic_cast<KrInterBriefView *>(other);
        int column = v->_model->lastSortOrder();
        Qt::SortOrder sortDir = v->_model->lastSortDir();
        _header->setSortIndicator(column, sortDir);
        _model->sort(column, sortDir);
        setFileIconSize(v->fileIconSize());
    }
}

void KrInterBriefView::setFileIconSize(int size)
{
    KrView::setFileIconSize(size);
    setIconSize(QSize(fileIconSize(), fileIconSize()));
    updateGeometries();
}

void KrInterBriefView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
{
    if (_model->ready()) {
        KrViewItem *item = getKrViewItem(currentIndex());
        op()->emitCurrentChanged(item);
    }
    QAbstractItemView::currentChanged(current, previous);
}

void KrInterBriefView::renameCurrentItem()
{
    QModelIndex nameIndex = _model->index(currentIndex().row(), KrViewProperties::Name);

    // cycle through various text selections if we are in the editing mode already
    if (state() == QAbstractItemView::EditingState) {
        auto delegate = dynamic_cast<KrViewItemDelegate *>(itemDelegateForIndex(nameIndex));
        if (!delegate) {
            qWarning() << "KrInterView item delegate is not KrViewItemDelegate, selection is not updated";
            return;
        }

        delegate->cycleEditorSelection();
        return;
    }

    // create and show file name editor
    edit(nameIndex);
    updateEditorData();
    update(nameIndex);
}

bool KrInterBriefView::event(QEvent *e)
{
    _mouseHandler->otherEvent(e);
    return QAbstractItemView::event(e);
}

void KrInterBriefView::mousePressEvent(QMouseEvent *ev)
{
    if (!_mouseHandler->mousePressEvent(ev))
        QAbstractItemView::mousePressEvent(ev);
}

void KrInterBriefView::mouseReleaseEvent(QMouseEvent *ev)
{
    if (!_mouseHandler->mouseReleaseEvent(ev))
        QAbstractItemView::mouseReleaseEvent(ev);
}

void KrInterBriefView::mouseDoubleClickEvent(QMouseEvent *ev)
{
    if (!_mouseHandler->mouseDoubleClickEvent(ev))
        QAbstractItemView::mouseDoubleClickEvent(ev);
}

void KrInterBriefView::mouseMoveEvent(QMouseEvent *ev)
{
    if (!_mouseHandler->mouseMoveEvent(ev))
        QAbstractItemView::mouseMoveEvent(ev);
}

void KrInterBriefView::dragEnterEvent(QDragEnterEvent *ev)
{
    if (!_mouseHandler->dragEnterEvent(ev))
        QAbstractItemView::dragEnterEvent(ev);
}

void KrInterBriefView::dragMoveEvent(QDragMoveEvent *ev)
{
    QAbstractItemView::dragMoveEvent(ev);
    _mouseHandler->dragMoveEvent(ev);
}

void KrInterBriefView::dragLeaveEvent(QDragLeaveEvent *ev)
{
    if (!_mouseHandler->dragLeaveEvent(ev))
        QAbstractItemView::dragLeaveEvent(ev);
}

void KrInterBriefView::dropEvent(QDropEvent *ev)
{
    if (!_mouseHandler->dropEvent(ev))
        QAbstractItemView::dropEvent(ev);
}

QRect KrInterBriefView::mapToViewport(const QRect &rect) const
{
    if (!rect.isValid())
        return rect;

    QRect result = rect;

    int dx = -horizontalOffset();
    int dy = -verticalOffset();
    result.adjust(dx, dy, dx, dy);
    return result;
}