summaryrefslogtreecommitdiffstats
blob: 121ae2e3323872c2e2cc2a8f3713aa5e514ff3a6 (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
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
/*
 * Copyright (c) 2018 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/QtChatOverviewBundle.h>

#include <QHBoxLayout>
#include <QLabel>
#include <QPalette>
#include <QSortFilterProxyModel>
#include <QVBoxLayout>

#include <Swiften/Elements/StatusShow.h>

#include <Swift/QtUI/ChattablesModel.h>
#include <Swift/QtUI/QtChatOverviewDelegate.h>
#include <Swift/QtUI/QtClickableLabel.h>
#include <Swift/QtUI/QtExpandedListView.h>
#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {

BundleFilter::BundleFilter(QObject* parent) : QSortFilterProxyModel(parent) {
    sort(0, Qt::AscendingOrder);
    setDynamicSortFilter(true);
    setSortCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
}

void BundleFilter::addFilter(Filter filter) {
    filters_.emplace(filter);
    invalidateFilter();
}

bool BundleFilter::hasFilter(Filter filter) {
    return filters_.count(filter) > 0;
}

void BundleFilter::removeFilter(Filter filter) {
    filters_.erase(filter);
    invalidateFilter();
}

bool BundleFilter::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const {
    auto row = sourceModel()->index(sourceRow, 0, sourceParent);
    if (filters_.count(Filter::Unread)) {
        if (row.data(ChattablesModel::UnreadCountRole).toInt() == 0) {
            return false;
        }
    }
    if (filters_.count(Filter::People)) {
        if (row.data(ChattablesModel::TypeRole).toString() != "PERSON") {
            return false;
        }
    }
    if (filters_.count(Filter::Rooms)) {
        if (row.data(ChattablesModel::TypeRole).toString() != "ROOM") {
            return false;
        }
    }
    if (filters_.count(Filter::Online)) {
        if (static_cast<StatusShow::Type>(row.data(ChattablesModel::StatusRole).toInt()) == StatusShow::None) {
            return false;
        }
    }
    return true;
}

QtChatOverviewBundle::QtChatOverviewBundle(ChattablesModel* rootModel, QString name, bool hideWhenEmpty, QWidget* parent) : QWidget(parent), rootModel_(rootModel), hideWhenEmpty_(hideWhenEmpty) {
    proxyModel_ = new BundleFilter(this);
    if (name == "UNREAD") { // FIXME: Obviously needs a better approach
        proxyModel_->addFilter(BundleFilter::Filter::Unread);
    }
    if (name == "PEOPLE") {
        proxyModel_->addFilter(BundleFilter::Filter::People);
        proxyModel_->addFilter(BundleFilter::Filter::Online);
    }
    if (name == "ROOMS") {
        proxyModel_->addFilter(BundleFilter::Filter::Rooms);
        proxyModel_->addFilter(BundleFilter::Filter::Online);
    }
    proxyModel_->setSourceModel(rootModel);


    auto mainLayout = new QVBoxLayout();
    setLayout(mainLayout);

    auto headerLayout = new QHBoxLayout();
    mainLayout->addLayout(headerLayout);
    auto nameLabel = new QLabel(name, this);
    nameLabel->setStyleSheet("color: white;");
    headerLayout->addWidget(nameLabel);
    headerLayout->addStretch();
    if (!hideWhenEmpty_) {
        filterLabel_ = new QtClickableLabel(this);
        filterLabel_->setText(tr("Online"));
        filterLabel_->setStyleSheet("color: white;");
        headerLayout->addWidget(filterLabel_);
        connect(filterLabel_, SIGNAL(clicked()), this, SLOT(handleFilterClicked()));
    }
    listView_ = new QtExpandedListView(this);
    listView_->setModel(proxyModel_);
    listView_->setFrameStyle(QFrame::NoFrame);
    listView_->setItemDelegate(new QtChatOverviewDelegate(this));
    connect(listView_, SIGNAL(clicked(const QModelIndex&)), this, SLOT(handleItemClicked(const QModelIndex&)));
    mainLayout->addWidget(listView_);

    if (hideWhenEmpty_) {
        connect(proxyModel_, &QAbstractItemModel::modelReset, this, [&](){
            updateVisibility();
        });
        connect(proxyModel_, &QAbstractItemModel::rowsInserted, this, [&](){
            updateVisibility();
        });
        connect(proxyModel_, &QAbstractItemModel::rowsRemoved, this, [&](){
            updateVisibility();
        });
        updateVisibility();
    }
}

QtChatOverviewBundle::~QtChatOverviewBundle() {}

void QtChatOverviewBundle::handleFilterClicked() {
    if (proxyModel_->hasFilter(BundleFilter::Filter::Online)) {
        proxyModel_->removeFilter(BundleFilter::Filter::Online);
        filterLabel_->setText(tr("All"));
    }
    else {
        proxyModel_->addFilter(BundleFilter::Filter::Online);
        filterLabel_->setText(tr("Online"));
    }
}

void QtChatOverviewBundle::updateVisibility() {
    auto shouldBeVisible = (proxyModel_->rowCount(listView_->rootIndex()) > 0);
    setVisible(shouldBeVisible);
}


void QtChatOverviewBundle::handleItemClicked(const QModelIndex& index) {
    clicked(JID(Q2PSTRING(index.data(ChattablesModel::JIDRole).toString())));
}

} // namespace Swift