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

#include <Swift/QtUI/Roster/QtTreeWidget.h>

#include <memory>

#include <boost/bind.hpp>

#include <QLabel>
#include <QMimeData>
#include <QObject>
#include <QTimer>
#include <QToolTip>
#include <QUrl>

#include <Swiften/Base/Platform.h>

#include <Swift/Controllers/Roster/ContactRosterItem.h>
#include <Swift/Controllers/Roster/GroupRosterItem.h>
#include <Swift/Controllers/Settings/SettingsProvider.h>
#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h>
#include <Swift/Controllers/UIEvents/SendFileUIEvent.h>
#include <Swift/Controllers/UIEvents/UIEventStream.h>

#include <Swift/QtUI/QtSwiftUtil.h>
#include <Swift/QtUI/QtUISettingConstants.h>
#include <Swift/QtUI/Roster/RosterModel.h>

namespace Swift {

QtTreeWidget::QtTreeWidget(UIEventStream* eventStream, SettingsProvider* settings, MessageTarget messageTarget, QWidget* parent) : QTreeView(parent), tooltipShown_(false), messageTarget_(messageTarget) {
    eventStream_ = eventStream;
    settings_ = settings;
    model_ = new RosterModel(this, settings_->getSetting(QtUISettingConstants::USE_SCREENREADER));
    setModel(model_);
    delegate_ = new RosterDelegate(this, settings_->getSetting(QtUISettingConstants::COMPACT_ROSTER));
    setItemDelegate(delegate_);
    setHeaderHidden(true);
#ifdef SWIFT_PLATFORM_MACOSX
    setAlternatingRowColors(true);
#endif
    setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    expandAll();
    setAnimated(true);
    setIndentation(0);
#ifdef SWIFT_EXPERIMENTAL_FT
    setAcceptDrops(true);
#endif
    setDragEnabled(true);
    setRootIsDecorated(true);
    connect(this, SIGNAL(activated(const QModelIndex&)), this, SLOT(handleItemActivated(const QModelIndex&)));
    connect(model_, SIGNAL(itemExpanded(const QModelIndex&, bool)), this, SLOT(handleModelItemExpanded(const QModelIndex&, bool)));
    connect(this, SIGNAL(expanded(const QModelIndex&)), this, SLOT(handleExpanded(const QModelIndex&)));
    connect(this, SIGNAL(collapsed(const QModelIndex&)), this, SLOT(handleCollapsed(const QModelIndex&)));
    connect(this, SIGNAL(clicked(const QModelIndex&)), this, SLOT(handleClicked(const QModelIndex&)));

    settings_->onSettingChanged.connect(boost::bind(&QtTreeWidget::handleSettingChanged, this, _1));
}

QtTreeWidget::~QtTreeWidget() {
    settings_->onSettingChanged.disconnect(boost::bind(&QtTreeWidget::handleSettingChanged, this, _1));
    delete model_;
    delete delegate_;
}

void QtTreeWidget::handleSettingChanged(const std::string& setting) {
    if (setting == QtUISettingConstants::COMPACT_ROSTER.getKey()) {
        delegate_->setCompact(settings_->getSetting(QtUISettingConstants::COMPACT_ROSTER));
        repaint();
    }
}

void QtTreeWidget::handleRefreshTooltip() {
    if (tooltipShown_) {
        QPoint position = QCursor::pos();
        QModelIndex index = indexAt(mapFromGlobal(position));
        QToolTip::showText(position, model_->data(index, Qt::ToolTipRole).toString());
    }
}

void QtTreeWidget::setRosterModel(Roster* roster) {
    roster_ = roster;
    model_->setRoster(roster);
    expandAll();
}

void QtTreeWidget::refreshTooltip() {
    // Qt needs some time to emit the events we need to detect tooltip's visibility correctly; 20 ms should be enough
    QTimer::singleShot(20, this, SLOT(handleRefreshTooltip()));
}

QtTreeWidgetItem* QtTreeWidget::getRoot() {
    return treeRoot_;
}

void QtTreeWidget::handleClicked(const QModelIndex& index) {
    GroupRosterItem* item = dynamic_cast<GroupRosterItem*>(static_cast<RosterItem*>(index.internalPointer()));
    if (item) {
        setExpanded(index, !isExpanded(index));
    }
    currentChanged(index, QModelIndex());
}

QModelIndexList QtTreeWidget::getSelectedIndexes() const {
    // Not using selectedIndexes(), because this seems to cause a crash in Qt (4.7.0) in the
    // QModelIndexList destructor.
    // This is a workaround posted in http://www.qtcentre.org/threads/16933 (although this case
    // was resolved by linking against the debug libs, ours isn't, and we're not alone)
    QItemSelection ranges = selectionModel()->selection();
    QModelIndexList selectedIndexList;
    for (int i = 0; i < ranges.count(); ++i) {
        QModelIndex parent = ranges.at(i).parent();
        int right = ranges.at(i).model()->columnCount(parent) - 1;
        if (ranges.at(i).left() == 0 && ranges.at(i).right() == right) {
            for (int r = ranges.at(i).top(); r <= ranges.at(i).bottom(); ++r) {
                selectedIndexList.append(ranges.at(i).model()->index(r, 0, parent));
            }
        }
    }
    return selectedIndexList;
}

void QtTreeWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous) {
    RosterItem* item = nullptr;
    QModelIndexList selectedIndexList = getSelectedIndexes();
    if (selectedIndexList.empty() || !selectedIndexList[0].isValid()) {
        /* I didn't quite understand why using current didn't seem to work here.*/
    }
    else if (current.isValid()) {
        item = static_cast<RosterItem*>(current.internalPointer());
        item = dynamic_cast<ContactRosterItem*>(item);
    }
    onSomethingSelectedChanged(item);
    QTreeView::currentChanged(current, previous);
}

void QtTreeWidget::handleItemActivated(const QModelIndex& index) {
    JID target = jidFromIndex(index);
    if (target.isValid()) {
        eventStream_->send(std::make_shared<RequestChatUIEvent>(target));
    }
}

void QtTreeWidget::dragEnterEvent(QDragEnterEvent *event) {
    if (event->mimeData()->hasUrls() && event->mimeData()->urls().size() == 1) {
        event->acceptProposedAction();
    }
}

void QtTreeWidget::dropEvent(QDropEvent *event) {
    QModelIndex index = indexAt(event->pos());
    if (index.isValid()) {
        RosterItem* item = static_cast<RosterItem*>(index.internalPointer());
        if (ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item)) {
            if (contact->supportsFeature(ContactRosterItem::FileTransferFeature)) {
                QString filename = event->mimeData()->urls().at(0).toLocalFile();
                if (!filename.isEmpty()) {
                    eventStream_->send(std::make_shared<SendFileUIEvent>(contact->getJID(), Q2PSTRING(filename)));
                }
            }
        }
    }
}

void QtTreeWidget::dragMoveEvent(QDragMoveEvent* event) {
    QModelIndex index = indexAt(event->pos());
    if (index.isValid()) {
        RosterItem* item = static_cast<RosterItem*>(index.internalPointer());
        if (ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item)) {
            if (contact->supportsFeature(ContactRosterItem::FileTransferFeature)) {
                event->accept();
                return;
            }
        }
    }
    QTreeView::dragMoveEvent(event);
}

bool QtTreeWidget::event(QEvent* event) {
    QChildEvent* childEvent = nullptr;
    if ((childEvent = dynamic_cast<QChildEvent*>(event))) {
        if (childEvent->polished()) {
            if (dynamic_cast<QLabel*>(childEvent->child())) {
                tooltipShown_ = true;
            }
        }
        else if (childEvent->removed()) {
            if (childEvent->child()->objectName() == "qtooltip_label") {
                tooltipShown_ = false;
            }
        }
    }
    return QAbstractItemView::event(event);
}

void QtTreeWidget::handleExpanded(const QModelIndex& index) {
    GroupRosterItem* item = dynamic_cast<GroupRosterItem*>(static_cast<RosterItem*>(index.internalPointer()));
    if (item) {
        item->setExpanded(true);
    }
}

void QtTreeWidget::handleCollapsed(const QModelIndex& index) {
    GroupRosterItem* item = dynamic_cast<GroupRosterItem*>(static_cast<RosterItem*>(index.internalPointer()));
    if (item) {
        item->setExpanded(false);
    }
}

void QtTreeWidget::handleModelItemExpanded(const QModelIndex& index, bool shouldExpand) {
    if (!index.isValid()) {
        return;
    }
    bool alreadyRight = this->isExpanded(index) == shouldExpand;
    if (alreadyRight) {
        return;
    }
    setExpanded(index, shouldExpand);
}

void QtTreeWidget::drawBranches(QPainter*, const QRect&, const QModelIndex&) const {
}

void QtTreeWidget::show() {
    QWidget::show();
}

void QtTreeWidget::setMessageTarget(MessageTarget messageTarget) {
    messageTarget_ = messageTarget;
}

JID QtTreeWidget::jidFromIndex(const QModelIndex& index) const {
    JID target;
    if (messageTarget_ == MessageDisplayJID) {
        target = JID(Q2PSTRING(index.data(DisplayJIDRole).toString()));
        target = target.toBare();
    }
    if (!target.isValid()) {
        target = JID(Q2PSTRING(index.data(JIDRole).toString()));
    }
    return target;
}

JID QtTreeWidget::selectedJID() const {
    QModelIndexList list = selectedIndexes();
    if (list.size() != 1) {
        return JID();
    }
    return jidFromIndex(list[0]);
}

void QtTreeWidget::setOnline(bool isOnline) {
    isOnline_ = isOnline;
}

bool QtTreeWidget::isOnline() const {
    return isOnline_;
}

}