aboutsummaryrefslogtreecommitdiffstats
path: root/src/threadlistview.cpp
blob: b5176bfa6cedd1237babc2971c8c17636a3095a1 (plain)
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
/*
 * qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs
 * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "threadlistview.h"

#include "carddelegate.h"
#include "threadlistmodel.h"

#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QScrollBar>
#include <QWheelEvent>

void ThreadListView::mousePressEvent(QMouseEvent *event)
{
    const QModelIndex index = indexAt(event->pos());

    // The message count IS the expander. Anything outside its rect selects the
    // card and opens it, which is what the rest of the card is for.
    if (event->button() == Qt::LeftButton && index.isValid()
        && index.data(ThreadListModel::MessageCountRole).toInt() > 0) {

        QStyleOptionViewItem option;
        initViewItemOption(&option);
        option.rect = visualRect(index);
        // State_Open decides which way the glyph points, and the rect is the
        // same either way, but pass it so the layout sees the true state.
        if (isExpanded(index))
            option.state |= QStyle::State_Open;

        if (CardDelegate::expanderRectFor(option, index)
                .contains(event->pos())) {
            setExpanded(index, !isExpanded(index));
            // Swallowed, so expanding does not also load the thread into the
            // message pane: it is a request to see the thread's shape, not to
            // read it.
            event->accept();
            return;
        }
    }

    QTreeView::mousePressEvent(event);
}

void ThreadListView::wheelEvent(QWheelEvent *event)
{
    const int notches = event->angleDelta().y() / QWheelEvent::DefaultDeltasPerStep;
    if (!event->pixelDelta().isNull() || event->modifiers() != Qt::NoModifier
        || notches == 0) {
        // ponytail: a high-resolution wheel sending less than one notch per
        // event falls through to the plain per-pixel scroll, unanimated.
        QTreeView::wheelEvent(event);
        return;
    }

    QScrollBar *bar = verticalScrollBar();
    if (!m_glide) {
        m_glide = new QPropertyAnimation(bar, "value", this);
        m_glide->setDuration(160);
        m_glide->setEasingCurve(QEasingCurve::OutCubic);
    }

    const bool gliding = m_glide->state() == QAbstractAnimation::Running;
    const int from = gliding ? m_glide->endValue().toInt() : bar->value();
    const int to = qBound(bar->minimum(), from - notches * bar->singleStep(),
                          bar->maximum());

    m_glide->stop();
    m_glide->setStartValue(bar->value());
    m_glide->setEndValue(to);
    m_glide->start();
    event->accept();
}