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
|
/*
* Copyright (c) 2010 Kevin Smith
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include "Roster/QtTreeWidget.h"
#include "Swiften/Base/Platform.h"
#include "Swiften/Roster/OpenChatRosterAction.h"
#include <qdebug.h>
#include <QMenu>
#include <QContextMenuEvent>
namespace Swift {
QtTreeWidget::QtTreeWidget(QWidget* parent) : QTreeView(parent) {
treeRoot_ = new QtTreeWidgetItem(NULL);
model_ = new RosterModel();
model_->setRoot(treeRoot_);
setModel(model_);
delegate_ = new RosterDelegate();
setItemDelegate(delegate_);
setHeaderHidden(true);
contextMenu_ = NULL;
#ifdef SWIFT_PLATFORM_MACOSX
setAlternatingRowColors(true);
#endif
setAnimated(true);
setIndentation(0);
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(model_, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(handleDataChanged(const QModelIndex&, const QModelIndex&)));
connect(this, SIGNAL(expanded(const QModelIndex&)), this, SLOT(handleExpanded(const QModelIndex&)));
connect(this, SIGNAL(collapsed(const QModelIndex&)), this, SLOT(handleCollapsed(const QModelIndex&)));
}
QtTreeWidget::~QtTreeWidget() {
delete model_;
delete delegate_;
}
void QtTreeWidget::setContextMenu(QtContextMenu* contextMenu) {
contextMenu_ = contextMenu;
}
QtTreeWidgetItem* QtTreeWidget::getRoot() {
return treeRoot_;
}
void QtTreeWidget::handleItemActivated(const QModelIndex& index) {
QtTreeWidgetItem* qtItem = static_cast<QtTreeWidgetItem*>(index.internalPointer());
if (qtItem) {
qtItem->performUserAction(boost::shared_ptr<UserRosterAction>(new OpenChatRosterAction()));
}
}
void QtTreeWidget::contextMenuEvent(QContextMenuEvent* event) {
if (!contextMenu_) {
return;
}
QModelIndex index = indexAt(event->pos());
QtTreeWidgetItem* qtItem = index.isValid() ? static_cast<QtTreeWidgetItem*>(index.internalPointer()) : NULL;
if (qtItem) {
contextMenu_->show(qtItem);
}
}
void QtTreeWidget::handleExpanded(const QModelIndex& index) {
QtTreeWidgetItem* qtItem = static_cast<QtTreeWidgetItem*>(index.internalPointer());
if (qtItem) {
qtItem->setExpanded(true);
}
}
void QtTreeWidget::handleCollapsed(const QModelIndex& index) {
QtTreeWidgetItem* qtItem = static_cast<QtTreeWidgetItem*>(index.internalPointer());
if (qtItem) {
qtItem->setExpanded(false);
}
}
void QtTreeWidget::handleModelItemExpanded(const QModelIndex& index, bool shouldExpand) {
if (this->isExpanded(index) == shouldExpand) {
return;
}
//setExpanded(index, shouldExpand);
if (shouldExpand) {
expand(index);
emit expanded(index);
} else {
collapse(index);
emit collapsed(index);
}
}
void QtTreeWidget::handleDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight) {
Q_UNUSED(bottomRight);
//in our model, this is only thrown with topLeft == bottomRight
if (!topLeft.isValid()) {
return;
}
QtTreeWidgetItem* qtItem = static_cast<QtTreeWidgetItem*>(topLeft.internalPointer());
if (qtItem) {
setExpanded(topLeft, qtItem->isExpanded());
//qDebug() << "Item changed, passing expanded state to view: " << qtItem->isExpanded() << " giving an expanded state of " << isExpanded(topLeft);
}
}
void QtTreeWidget::drawBranches(QPainter*, const QRect&, const QModelIndex&) const {
}
void QtTreeWidget::show() {
QWidget::show();
}
}
|