diff options
Diffstat (limited to 'Swift/QtUI/Roster')
-rw-r--r-- | Swift/QtUI/Roster/DelegateCommons.cpp | 88 | ||||
-rw-r--r-- | Swift/QtUI/Roster/DelegateCommons.h | 11 | ||||
-rw-r--r-- | Swift/QtUI/Roster/QtOccupantListWidget.cpp | 60 | ||||
-rw-r--r-- | Swift/QtUI/Roster/QtOccupantListWidget.h | 30 | ||||
-rw-r--r-- | Swift/QtUI/Roster/QtRosterWidget.cpp | 100 | ||||
-rw-r--r-- | Swift/QtUI/Roster/QtRosterWidget.h | 25 | ||||
-rw-r--r-- | Swift/QtUI/Roster/QtTreeWidget.cpp | 97 | ||||
-rw-r--r-- | Swift/QtUI/Roster/QtTreeWidget.h | 29 | ||||
-rw-r--r-- | Swift/QtUI/Roster/QtTreeWidgetItem.cpp | 242 | ||||
-rw-r--r-- | Swift/QtUI/Roster/QtTreeWidgetItem.h | 86 | ||||
-rw-r--r-- | Swift/QtUI/Roster/RosterDelegate.cpp | 81 | ||||
-rw-r--r-- | Swift/QtUI/Roster/RosterDelegate.h | 4 |
12 files changed, 371 insertions, 482 deletions
diff --git a/Swift/QtUI/Roster/DelegateCommons.cpp b/Swift/QtUI/Roster/DelegateCommons.cpp index 164b80f..290794d 100644 --- a/Swift/QtUI/Roster/DelegateCommons.cpp +++ b/Swift/QtUI/Roster/DelegateCommons.cpp @@ -6,18 +6,102 @@ #include "DelegateCommons.h" +#include <QtScaledAvatarCache.h> +#include <QFileInfo> + namespace Swift { -void DelegateCommons::drawElidedText(QPainter* painter, const QRect& region, const QString& text) { +void DelegateCommons::drawElidedText(QPainter* painter, const QRect& region, const QString& text, int flags) { QString adjustedText(painter->fontMetrics().elidedText(text, Qt::ElideRight, region.width(), Qt::TextShowMnemonic)); - painter->drawText(region, Qt::AlignTop, adjustedText); + painter->drawText(region, flags, adjustedText); } +void DelegateCommons::paintContact(QPainter* painter, const QStyleOptionViewItem& option, const QColor& nameColor, const QString& avatarPath, const QIcon& presenceIcon, const QString& name, const QString& statusText, int unreadCount) const { + painter->save(); + QRect fullRegion(option.rect); + if ( option.state & QStyle::State_Selected ) { + painter->fillRect(fullRegion, option.palette.highlight()); + painter->setPen(option.palette.highlightedText().color()); + } else { + painter->setPen(QPen(nameColor)); + } + + QRect presenceIconRegion(QPoint(farLeftMargin, fullRegion.top()), QSize(presenceIconWidth, fullRegion.height() - verticalMargin)); + + int calculatedAvatarSize = presenceIconRegion.height(); + //This overlaps the presenceIcon, so must be painted first + QRect avatarRegion(QPoint(presenceIconRegion.right() - presenceIconWidth / 2, presenceIconRegion.top()), QSize(calculatedAvatarSize, calculatedAvatarSize)); + + QPixmap avatarPixmap; + if (!avatarPath.isEmpty()) { + QString scaledAvatarPath = QtScaledAvatarCache(avatarRegion.height()).getScaledAvatarPath(avatarPath); + if (QFileInfo(scaledAvatarPath).exists()) { + avatarPixmap.load(scaledAvatarPath); + } + } + if (avatarPixmap.isNull()) { + avatarPixmap = QPixmap(":/icons/avatar.png").scaled(avatarRegion.height(), avatarRegion.width(), Qt::KeepAspectRatio, Qt::SmoothTransformation); + } + + painter->drawPixmap(avatarRegion.topLeft() + QPoint(((avatarRegion.width() - avatarPixmap.width()) / 2), (avatarRegion.height() - avatarPixmap.height()) / 2), avatarPixmap); + + //Paint the presence icon over the top of the avatar + presenceIcon.paint(painter, presenceIconRegion, Qt::AlignBottom | Qt::AlignHCenter); + + QFontMetrics nameMetrics(nameFont); + painter->setFont(nameFont); + int extraFontWidth = nameMetrics.width("H"); + int leftOffset = avatarRegion.right() + horizontalMargin * 2 + extraFontWidth / 2; + QRect textRegion(fullRegion.adjusted(leftOffset, 0, 0/*-leftOffset*/, 0)); + + int nameHeight = nameMetrics.height() + verticalMargin; + QRect nameRegion(textRegion.adjusted(0, verticalMargin, 0, 0)); + + DelegateCommons::drawElidedText(painter, nameRegion, name); + + + painter->setFont(detailFont); + painter->setPen(QPen(QColor(160,160,160))); + + QRect statusTextRegion(textRegion.adjusted(0, nameHeight, 0, 0)); + DelegateCommons::drawElidedText(painter, statusTextRegion, statusText); + + if (unreadCount > 0) { + QRect unreadRect(fullRegion.right() - unreadCountSize - horizontalMargin, fullRegion.top() + (fullRegion.height() - unreadCountSize) / 2, unreadCountSize, unreadCountSize); + QPen pen(QColor("black")); + pen.setWidth(1); + painter->setRenderHint(QPainter::Antialiasing, true); + painter->setPen(pen); + painter->setBrush(QBrush(QColor("red"), Qt::SolidPattern)); + //painter->setBackgroundMode(Qt::OpaqueMode); + painter->drawEllipse(unreadRect); + painter->setBackgroundMode(Qt::TransparentMode); + painter->setPen(QColor("white")); + drawElidedText(painter, unreadRect, QString("%1").arg(unreadCount), Qt::AlignCenter); + } + + painter->restore(); +} + +QSize DelegateCommons::contactSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const { + int heightByAvatar = avatarSize + verticalMargin * 2; + QFontMetrics nameMetrics(nameFont); + QFontMetrics statusMetrics(detailFont); + int sizeByText = 2 * verticalMargin + nameMetrics.height() + statusMetrics.height(); + //Doesn't work, yay! FIXME: why? + //QSize size = (option.state & QStyle::State_Selected) ? QSize(150, 80) : QSize(150, avatarSize_ + margin_ * 2); + //qDebug() << "Returning size" << size; + return QSize(150, sizeByText > heightByAvatar ? sizeByText : heightByAvatar); +} const int DelegateCommons::horizontalMargin = 2; const int DelegateCommons::verticalMargin = 2; const int DelegateCommons::farLeftMargin = 2; +const int DelegateCommons::avatarSize = 20; +const int DelegateCommons::presenceIconHeight = 16; +const int DelegateCommons::presenceIconWidth = 16; +const int DelegateCommons::unreadCountSize = 16; diff --git a/Swift/QtUI/Roster/DelegateCommons.h b/Swift/QtUI/Roster/DelegateCommons.h index 9213ef5..e5e4ff9 100644 --- a/Swift/QtUI/Roster/DelegateCommons.h +++ b/Swift/QtUI/Roster/DelegateCommons.h @@ -11,6 +11,8 @@ #include <QPainter> #include <QRect> #include <QString> +#include <QIcon> +#include <QStyleOptionViewItem> namespace Swift { class DelegateCommons { @@ -21,7 +23,10 @@ namespace Swift { detailFont.setPointSize(nameFont.pointSize() - detailFontSizeDrop); } - static void drawElidedText(QPainter* painter, const QRect& region, const QString& text); + static void drawElidedText(QPainter* painter, const QRect& region, const QString& text, int flags = Qt::AlignTop); + + QSize contactSizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const; + void paintContact(QPainter* painter, const QStyleOptionViewItem& option, const QColor& nameColor, const QString& avatarPath, const QIcon& presenceIcon, const QString& name, const QString& statusText, int unreadCount) const; int detailFontSizeDrop; QFont nameFont; @@ -29,5 +34,9 @@ namespace Swift { static const int horizontalMargin; static const int verticalMargin; static const int farLeftMargin; + static const int avatarSize; + static const int presenceIconHeight; + static const int presenceIconWidth; + static const int unreadCountSize; }; } diff --git a/Swift/QtUI/Roster/QtOccupantListWidget.cpp b/Swift/QtUI/Roster/QtOccupantListWidget.cpp new file mode 100644 index 0000000..cbda0f1 --- /dev/null +++ b/Swift/QtUI/Roster/QtOccupantListWidget.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2011 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + + +#include "Roster/QtOccupantListWidget.h" + +#include <QContextMenuEvent> +#include <QMenu> +#include <QAction> +#include <QInputDialog> + +#include "Swift/Controllers/Roster/ContactRosterItem.h" +#include "Swift/Controllers/Roster/GroupRosterItem.h" +#include "Swift/Controllers/UIEvents/UIEventStream.h" +#include "QtSwiftUtil.h" + +namespace Swift { + +QtOccupantListWidget::QtOccupantListWidget(UIEventStream* eventStream, QWidget* parent) : QtTreeWidget(eventStream, parent) { + +} + +QtOccupantListWidget::~QtOccupantListWidget() { + +} + +void QtOccupantListWidget::setAvailableOccupantActions(const std::vector<ChatWindow::OccupantAction>& actions) { + availableOccupantActions_ = actions; +} + +void QtOccupantListWidget::contextMenuEvent(QContextMenuEvent* event) { + QModelIndex index = indexAt(event->pos()); + if (!index.isValid()) { + return; + } + RosterItem* item = static_cast<RosterItem*>(index.internalPointer()); + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); + if (contact) { + QMenu contextMenu; + std::map<QAction*, ChatWindow::OccupantAction> actions; + foreach (ChatWindow::OccupantAction availableAction, availableOccupantActions_) { + QString text = "Error: missing string"; + switch (availableAction) { + case ChatWindow::Kick: text = tr("Kick user"); break; + } + QAction* action = contextMenu.addAction(text); + actions[action] = availableAction; + } + QAction* result = contextMenu.exec(event->globalPos()); + if (result) { + onOccupantActionSelected(actions[result], contact); + } + } +} + +} + diff --git a/Swift/QtUI/Roster/QtOccupantListWidget.h b/Swift/QtUI/Roster/QtOccupantListWidget.h new file mode 100644 index 0000000..ef29c00 --- /dev/null +++ b/Swift/QtUI/Roster/QtOccupantListWidget.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2011 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include "Swift/QtUI/Roster/QtTreeWidget.h" + +#include "Swiften/Base/boost_bsignals.h" +#include "Swift/Controllers/UIInterfaces/ChatWindow.h" + +namespace Swift { + +class QtOccupantListWidget : public QtTreeWidget { + Q_OBJECT + public: + QtOccupantListWidget(UIEventStream* eventStream, QWidget* parent = 0); + virtual ~QtOccupantListWidget(); + void setAvailableOccupantActions(const std::vector<ChatWindow::OccupantAction>& actions); + boost::signal<void (ChatWindow::OccupantAction, ContactRosterItem*)> onOccupantActionSelected; + protected: + void contextMenuEvent(QContextMenuEvent* event); + private: + std::vector<ChatWindow::OccupantAction> availableOccupantActions_; +}; + +} + diff --git a/Swift/QtUI/Roster/QtRosterWidget.cpp b/Swift/QtUI/Roster/QtRosterWidget.cpp new file mode 100644 index 0000000..4c96695 --- /dev/null +++ b/Swift/QtUI/Roster/QtRosterWidget.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2011 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include "Roster/QtRosterWidget.h" + +#include <QContextMenuEvent> +#include <QMenu> +#include <QInputDialog> +#include <QFileDialog> + +#include "Swift/Controllers/UIEvents/RequestContactEditorUIEvent.h" +#include "Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h" +#include "Swift/Controllers/UIEvents/RenameGroupUIEvent.h" +#include "Swift/Controllers/UIEvents/SendFileUIEvent.h" +#include "QtContactEditWindow.h" +#include "Swift/Controllers/Roster/ContactRosterItem.h" +#include "Swift/Controllers/Roster/GroupRosterItem.h" +#include "Swift/Controllers/UIEvents/UIEventStream.h" +#include "QtSwiftUtil.h" + +namespace Swift { + +QtRosterWidget::QtRosterWidget(UIEventStream* eventStream, QWidget* parent) : QtTreeWidget(eventStream, parent) { + +} + +QtRosterWidget::~QtRosterWidget() { + +} + +void QtRosterWidget::handleEditUserActionTriggered(bool /*checked*/) { + QModelIndexList selectedIndexList = getSelectedIndexes(); + if (selectedIndexList.empty()) { + return; + } + QModelIndex index = selectedIndexList[0]; + if (!index.isValid()) { + return; + } + RosterItem* item = static_cast<RosterItem*>(index.internalPointer()); + if (ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item)) { + eventStream_->send(boost::make_shared<RequestContactEditorUIEvent>(contact->getJID())); + } +} + +void QtRosterWidget::contextMenuEvent(QContextMenuEvent* event) { + QModelIndex index = indexAt(event->pos()); + if (!index.isValid()) { + return; + } + RosterItem* item = static_cast<RosterItem*>(index.internalPointer()); + QMenu contextMenu; + if (ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item)) { + QAction* editContact = contextMenu.addAction(tr("Edit")); + QAction* removeContact = contextMenu.addAction(tr("Remove")); +#ifdef SWIFT_EXPERIMENTAL_FT + QAction* sendFile = NULL; + if (contact->supportsFeature(ContactRosterItem::FileTransferFeature)) { + sendFile = contextMenu.addAction(tr("Send File")); + } +#endif + QAction* result = contextMenu.exec(event->globalPos()); + if (result == editContact) { + eventStream_->send(boost::make_shared<RequestContactEditorUIEvent>(contact->getJID())); + } + else if (result == removeContact) { + if (QtContactEditWindow::confirmContactDeletion(contact->getJID())) { + eventStream_->send(boost::make_shared<RemoveRosterItemUIEvent>(contact->getJID())); + } + } +#ifdef SWIFT_EXPERIMENTAL_FT + else if (sendFile && result == sendFile) { + QString fileName = QFileDialog::getOpenFileName(this, tr("Send File"), "", tr("All Files (*);;")); + if (!fileName.isEmpty()) { + eventStream_->send(boost::make_shared<SendFileUIEvent>(contact->getJID(), Q2PSTRING(fileName))); + } + } +#endif + } + else if (GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item)) { + QAction* renameGroupAction = contextMenu.addAction(tr("Rename")); + QAction* result = contextMenu.exec(event->globalPos()); + if (result == renameGroupAction) { + renameGroup(group); + } + } +} + +void QtRosterWidget::renameGroup(GroupRosterItem* group) { + bool ok; + QString newName = QInputDialog::getText(NULL, tr("Rename group"), tr("Enter a new name for group '%1':").arg(P2QSTRING(group->getDisplayName())), QLineEdit::Normal, P2QSTRING(group->getDisplayName()), &ok); + if (ok) { + eventStream_->send(boost::make_shared<RenameGroupUIEvent>(group->getDisplayName(), Q2PSTRING(newName))); + } +} + +} diff --git a/Swift/QtUI/Roster/QtRosterWidget.h b/Swift/QtUI/Roster/QtRosterWidget.h new file mode 100644 index 0000000..f870237 --- /dev/null +++ b/Swift/QtUI/Roster/QtRosterWidget.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2011 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include "Swift/QtUI/Roster/QtTreeWidget.h" + +namespace Swift { +class QtRosterWidget : public QtTreeWidget { + Q_OBJECT + public: + QtRosterWidget(UIEventStream* eventStream, QWidget* parent = 0); + virtual ~QtRosterWidget(); + public slots: + void handleEditUserActionTriggered(bool checked); + protected: + void contextMenuEvent(QContextMenuEvent* event); + private: + void renameGroup(GroupRosterItem* group); +}; + +} diff --git a/Swift/QtUI/Roster/QtTreeWidget.cpp b/Swift/QtUI/Roster/QtTreeWidget.cpp index 1296872..690515d 100644 --- a/Swift/QtUI/Roster/QtTreeWidget.cpp +++ b/Swift/QtUI/Roster/QtTreeWidget.cpp @@ -6,25 +6,21 @@ #include "Roster/QtTreeWidget.h" -#include <QMenu> -#include <QContextMenuEvent> -#include <QInputDialog> #include <boost/smart_ptr/make_shared.hpp> +#include <QUrl> + #include "Swiften/Base/Platform.h" #include "Swift/Controllers/Roster/ContactRosterItem.h" #include "Swift/Controllers/Roster/GroupRosterItem.h" #include "Swift/Controllers/UIEvents/UIEventStream.h" #include "Swift/Controllers/UIEvents/RequestChatUIEvent.h" -#include "Swift/Controllers/UIEvents/RequestContactEditorUIEvent.h" -#include "Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h" -#include "Swift/Controllers/UIEvents/RenameGroupUIEvent.h" +#include "Swift/Controllers/UIEvents/SendFileUIEvent.h" #include "QtSwiftUtil.h" -#include "QtContactEditWindow.h" namespace Swift { -QtTreeWidget::QtTreeWidget(UIEventStream* eventStream, QWidget* parent) : QTreeView(parent), editable_(false) { +QtTreeWidget::QtTreeWidget(UIEventStream* eventStream, QWidget* parent) : QTreeView(parent) { eventStream_ = eventStream; model_ = new RosterModel(this); setModel(model_); @@ -38,6 +34,9 @@ QtTreeWidget::QtTreeWidget(UIEventStream* eventStream, QWidget* parent) : QTreeV expandAll(); setAnimated(true); setIndentation(0); +#ifdef SWIFT_EXPERIMENTAL_FT + setAcceptDrops(true); +#endif 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))); @@ -89,21 +88,20 @@ QModelIndexList QtTreeWidget::getSelectedIndexes() const { } void QtTreeWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous) { - bool valid = false; + RosterItem* item = NULL; QModelIndexList selectedIndexList = getSelectedIndexes(); - if (!editable_ || selectedIndexList.empty() || !selectedIndexList[0].isValid()) { + if (selectedIndexList.empty() || !selectedIndexList[0].isValid()) { /* I didn't quite understand why using current didn't seem to work here.*/ } else if (current.isValid()) { - RosterItem* item = static_cast<RosterItem*>(current.internalPointer()); - if (dynamic_cast<ContactRosterItem*>(item)) { - valid = true; - } + item = static_cast<RosterItem*>(current.internalPointer()); + item = dynamic_cast<ContactRosterItem*>(item); } - onSomethingSelectedChanged(valid); + onSomethingSelectedChanged(item); QTreeView::currentChanged(current, previous); } + void QtTreeWidget::handleItemActivated(const QModelIndex& index) { RosterItem* item = static_cast<RosterItem*>(index.internalPointer()); ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item); @@ -112,62 +110,39 @@ void QtTreeWidget::handleItemActivated(const QModelIndex& index) { } } -void QtTreeWidget::handleEditUserActionTriggered(bool /*checked*/) { - if (!editable_) { - return; - } - QModelIndexList selectedIndexList = getSelectedIndexes(); - if (selectedIndexList.empty()) { - return; - } - QModelIndex index = selectedIndexList[0]; - if (!index.isValid()) { - return; - } - RosterItem* item = static_cast<RosterItem*>(index.internalPointer()); - if (ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item)) { - eventStream_->send(boost::make_shared<RequestContactEditorUIEvent>(contact->getJID())); +void QtTreeWidget::dragEnterEvent(QDragEnterEvent *event) { + if (event->mimeData()->hasUrls() && event->mimeData()->urls().size() == 1) { + event->acceptProposedAction(); } } -void QtTreeWidget::contextMenuEvent(QContextMenuEvent* event) { - if (!editable_) { - return; - } +void QtTreeWidget::dropEvent(QDropEvent *event) { QModelIndex index = indexAt(event->pos()); - if (!index.isValid()) { - return; - } - RosterItem* item = static_cast<RosterItem*>(index.internalPointer()); - QMenu contextMenu; - if (ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item)) { - QAction* editContact = contextMenu.addAction(tr("Edit")); - QAction* removeContact = contextMenu.addAction(tr("Remove")); - QAction* result = contextMenu.exec(event->globalPos()); - if (result == editContact) { - eventStream_->send(boost::make_shared<RequestContactEditorUIEvent>(contact->getJID())); - } - else if (result == removeContact) { - if (QtContactEditWindow::confirmContactDeletion(contact->getJID())) { - eventStream_->send(boost::make_shared<RemoveRosterItemUIEvent>(contact->getJID())); + 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(boost::make_shared<SendFileUIEvent>(contact->getJID(), Q2PSTRING(filename))); + } } } } - else if (GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item)) { - QAction* renameGroupAction = contextMenu.addAction(tr("Rename")); - QAction* result = contextMenu.exec(event->globalPos()); - if (result == renameGroupAction) { - renameGroup(group); - } - } } -void QtTreeWidget::renameGroup(GroupRosterItem* group) { - bool ok; - QString newName = QInputDialog::getText(NULL, tr("Rename group"), tr("Enter a new name for group '%1':").arg(P2QSTRING(group->getDisplayName())), QLineEdit::Normal, P2QSTRING(group->getDisplayName()), &ok); - if (ok) { - eventStream_->send(boost::make_shared<RenameGroupUIEvent>(group->getDisplayName(), Q2PSTRING(newName))); +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; + } + } } + event->ignore(); } void QtTreeWidget::handleExpanded(const QModelIndex& index) { diff --git a/Swift/QtUI/Roster/QtTreeWidget.h b/Swift/QtUI/Roster/QtTreeWidget.h index 4ecba83..271fbd5 100644 --- a/Swift/QtUI/Roster/QtTreeWidget.h +++ b/Swift/QtUI/Roster/QtTreeWidget.h @@ -8,6 +8,9 @@ #include <QTreeView> #include <QModelIndex> +#include <QDragEnterEvent> +#include <QDropEvent> +#include <QDragMoveEvent> #include "Swift/QtUI/Roster/RosterModel.h" #include "Swift/QtUI/Roster/RosterDelegate.h" @@ -23,13 +26,7 @@ class QtTreeWidget : public QTreeView{ QtTreeWidgetItem* getRoot(); void setRosterModel(Roster* roster); Roster* getRoster() {return roster_;} - void setEditable(bool b) { editable_ = b; } - - signals: - void onSomethingSelectedChanged(bool); - - public slots: - void handleEditUserActionTriggered(bool checked); + boost::signal<void (RosterItem*)> onSomethingSelectedChanged; private slots: void handleItemActivated(const QModelIndex&); @@ -38,22 +35,24 @@ class QtTreeWidget : public QTreeView{ void handleCollapsed(const QModelIndex&); void handleClicked(const QModelIndex&); protected: - void contextMenuEvent(QContextMenuEvent* event); - protected slots: - virtual void currentChanged(const QModelIndex& current, const QModelIndex& previous); + void dragEnterEvent(QDragEnterEvent* event); + void dropEvent(QDropEvent* event); + void dragMoveEvent(QDragMoveEvent* event); + protected: + QModelIndexList getSelectedIndexes() const; private: - void renameGroup(GroupRosterItem* group); void drawBranches(QPainter*, const QRect&, const QModelIndex&) const; - QModelIndexList getSelectedIndexes() const; - + protected slots: + virtual void currentChanged(const QModelIndex& current, const QModelIndex& previous); + protected: + UIEventStream* eventStream_; + private: RosterModel* model_; Roster* roster_; RosterDelegate* delegate_; QtTreeWidgetItem* treeRoot_; - UIEventStream* eventStream_; - bool editable_; }; } diff --git a/Swift/QtUI/Roster/QtTreeWidgetItem.cpp b/Swift/QtUI/Roster/QtTreeWidgetItem.cpp deleted file mode 100644 index fcd8691..0000000 --- a/Swift/QtUI/Roster/QtTreeWidgetItem.cpp +++ /dev/null @@ -1,242 +0,0 @@ -/* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include "Swift/QtUI/Roster/QtTreeWidgetItem.h" -#include "Swift/QtUI/Roster/QtTreeWidget.h" - -#include <qdebug.h> -#include <QtAlgorithms> -#include <algorithm> - -namespace Swift { - -QtTreeWidgetItem::QtTreeWidgetItem(QtTreeWidgetItem* parentItem) : QObject(), textColor_(0,0,0), backgroundColor_(255,255,255) { - parent_ = parentItem; - shown_ = true; - expanded_ = true; -} - - -void QtTreeWidgetItem::setText(const std::string& text) { - displayName_ = P2QSTRING(text); - displayNameLower_ = displayName_.toLower(); - emit changed(this); -} - -void QtTreeWidgetItem::setStatusText(const std::string& text) { - statusText_ = P2QSTRING(text); - emit changed(this); -} - -void QtTreeWidgetItem::setAvatarPath(const std::string& path) { - avatar_ = QIcon(P2QSTRING(path)); - emit changed(this); -} - -void QtTreeWidgetItem::setStatusShow(StatusShow::Type show) { - statusShowType_ = show; - int color = 0; - switch (show) { - case StatusShow::Online: color = 0x000000; mergedShowType_ = StatusShow::Online; break; - case StatusShow::Away: color = 0x336699; mergedShowType_ = StatusShow::Away; break; - case StatusShow::XA: color = 0x336699; mergedShowType_ = StatusShow::Away; break; - case StatusShow::FFC: color = 0x000000; mergedShowType_ = StatusShow::Online; break; - case StatusShow::DND: color = 0x990000; mergedShowType_ = show; break; - case StatusShow::None: color = 0x7F7F7F; mergedShowType_ = show; break; - } - setTextColor(color); - emit changed(this); -} - -void QtTreeWidgetItem::setTextColor(unsigned long color) { - textColor_ = QColor( - ((color & 0xFF0000)>>16), - ((color & 0xFF00)>>8), - (color & 0xFF)); -} - -void QtTreeWidgetItem::setBackgroundColor(unsigned long color) { - backgroundColor_ = QColor( - ((color & 0xFF0000)>>16), - ((color & 0xFF00)>>8), - (color & 0xFF)); -} - -void QtTreeWidgetItem::setExpanded(bool expanded) { - expanded_ = expanded; - emit changed(this); -} - -void QtTreeWidgetItem::hide() { - shown_ = false; - emit changed(this); -} - -void QtTreeWidgetItem::show() { - shown_ = true; - emit changed(this); -} - -bool QtTreeWidgetItem::isShown() { - return isContact() ? shown_ : shownChildren_.size() > 0; -} - -QWidget* QtTreeWidgetItem::getCollapsedRosterWidget() { - QWidget* widget = new QWidget(); - return widget; -} - -QWidget* QtTreeWidgetItem::getExpandedRosterWidget() { - QWidget* widget = new QWidget(); - return widget; -} - -QtTreeWidgetItem::~QtTreeWidgetItem() { - //It's possible (due to the way the roster deletes items in unknown order when it is deleted) - // That the children will be deleted before the groups, or that the groups are deleted - // before the children. If the children are deleted first, they will let the parent know that - // They've been deleted. If the parent is deleted first, it must tell the children not to - // tell it when they're deleted. Everything will be deleted in the end, because all the - // widgets are owned by the Roster in Swiften. - if (parent_) { - parent_->removeChild(this); - } - - for (int i = 0; i < children_.size(); i++) { - children_[i]->parentItemHasBeenDeleted(); - } -} - -void QtTreeWidgetItem::parentItemHasBeenDeleted() { - parent_ = NULL; -} - -QtTreeWidgetItem* QtTreeWidgetItem::getParentItem() { - return parent_; -} - -void QtTreeWidgetItem::addChild(QtTreeWidgetItem* child) { - children_.append(child); - connect(child, SIGNAL(changed(QtTreeWidgetItem*)), this, SLOT(handleChanged(QtTreeWidgetItem*))); - handleChanged(child); -} - -void QtTreeWidgetItem::removeChild(QtTreeWidgetItem* child) { - children_.removeAll(child); - handleChanged(this); -} - -void bubbleSort(QList<QtTreeWidgetItem*>& list) { - bool done = false; - for (int i = 0; i < list.size() - 1 && !done; i++) { - done = true; - for (int j = i + 1; j < list.size(); j++) { - if (*(list[j]) < *(list[j - 1])) { - done = false; - QtTreeWidgetItem* lower = list[j]; - list[j] = list[j - 1]; - list[j - 1] = lower; - } - } - } -} - -void QtTreeWidgetItem::handleChanged(QtTreeWidgetItem* child) { - /*We don't use the much faster qStableSort because it causes changed(child) and all sorts of nasty recursion*/ - //qStableSort(children_.begin(), children_.end(), itemLessThan); - //bubbleSort(children_); - std::stable_sort(children_.begin(), children_.end(), itemLessThan); - shownChildren_.clear(); - for (int i = 0; i < children_.size(); i++) { - if (children_[i]->isShown()) { - shownChildren_.append(children_[i]); - } - } - emit changed(child); -} - -int QtTreeWidgetItem::rowCount() { - //qDebug() << "Returning size of " << children_.size() << " for item " << displayName_; - return shownChildren_.size(); -} - -int QtTreeWidgetItem::rowOf(QtTreeWidgetItem* item) { - return shownChildren_.indexOf(item); -} - -int QtTreeWidgetItem::row() { - return parent_ ? parent_->rowOf(this) : 0; -} - -QtTreeWidgetItem* QtTreeWidgetItem::getItem(int row) { - //qDebug() << "Returning row " << row << " from item " << displayName_; - Q_ASSERT(row >= 0); - Q_ASSERT(row < rowCount()); - return shownChildren_[row]; -} - - -QVariant QtTreeWidgetItem::data(int role) { - if (!isContact()) { - setTextColor(0xFFFFFF); - setBackgroundColor(0x969696); - } - switch (role) { - case Qt::DisplayRole: return displayName_; - case Qt::TextColorRole: return textColor_; - case Qt::BackgroundColorRole: return backgroundColor_; - case Qt::ToolTipRole: return isContact() ? toolTipString() : QVariant(); - case StatusTextRole: return statusText_; - case AvatarRole: return avatar_; - case PresenceIconRole: return getPresenceIcon(); - default: return QVariant(); - } -} - -QString QtTreeWidgetItem::toolTipString() { - return displayName_ + "\n " + statusText_; -} - -QIcon QtTreeWidgetItem::getPresenceIcon() { - QString iconString; - switch (statusShowType_) { - case StatusShow::Online: iconString = "online";break; - case StatusShow::Away: iconString = "away";break; - case StatusShow::XA: iconString = "away";break; - case StatusShow::FFC: iconString = "online";break; - case StatusShow::DND: iconString = "dnd";break; - case StatusShow::None: iconString = "offline";break; - } - return QIcon(":/icons/" + iconString + ".png"); -} - -bool QtTreeWidgetItem::isContact() const { - return children_.size() == 0; -} - -bool QtTreeWidgetItem::isExpanded() { - return expanded_; -} - -bool QtTreeWidgetItem::operator<(const QtTreeWidgetItem& item) const { - if (isContact()) { - if (!item.isContact()) { - return false; - } - return getStatusShowMerged() == item.getStatusShowMerged() ? getLowerName() < item.getLowerName() : getStatusShowMerged() < item.getStatusShowMerged(); - } else { - if (item.isContact()) { - return true; - } - return getLowerName() < item.getLowerName(); - } -} - -bool itemLessThan(QtTreeWidgetItem* left, QtTreeWidgetItem* right) { - return *left < *right; -} - -} diff --git a/Swift/QtUI/Roster/QtTreeWidgetItem.h b/Swift/QtUI/Roster/QtTreeWidgetItem.h deleted file mode 100644 index 6855989..0000000 --- a/Swift/QtUI/Roster/QtTreeWidgetItem.h +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include <QColor> -#include <QVariant> -#include <string> - -#include "Swiften/Roster/TreeWidgetFactory.h" -#include "Swiften/Roster/TreeWidget.h" -#include "Swiften/Roster/TreeWidgetItem.h" -#include "Swift/QtUI/QtSwiftUtil.h" - -namespace Swift { - enum RosterRoles { - StatusTextRole = Qt::UserRole, - AvatarRole = Qt::UserRole + 1, - PresenceIconRole = Qt::UserRole + 2, - StatusShowTypeRole = Qt::UserRole + 3 - }; - -class QtTreeWidget; -class QtTreeWidgetItem : public QObject, public TreeWidgetItem { - Q_OBJECT - public: - ~QtTreeWidgetItem(); - void addChild(QtTreeWidgetItem* child); - void removeChild(QtTreeWidgetItem* child); - QtTreeWidgetItem* getParentItem(); - int rowCount(); - int rowOf(QtTreeWidgetItem* item); - int row(); - QtTreeWidgetItem* getItem(int row); - QVariant data(int role); - QIcon getPresenceIcon(); - QtTreeWidgetItem(QtTreeWidgetItem* parentItem); - void setText(const std::string& text); - void setAvatarPath(const std::string& path); - void setStatusText(const std::string& text); - void setStatusShow(StatusShow::Type show); - void setTextColor(unsigned long color); - void setBackgroundColor(unsigned long color); - void setExpanded(bool b); - void parentItemHasBeenDeleted(); - void hide(); - void show(); - bool isShown(); - bool isContact() const; - bool isExpanded(); - const QString& getName() const {return displayName_;}; - const QString& getLowerName() const {return displayNameLower_;}; - StatusShow::Type getStatusShow() const {return statusShowType_;}; - StatusShow::Type getStatusShowMerged() const {return mergedShowType_;}; - - QWidget* getCollapsedRosterWidget(); - QWidget* getExpandedRosterWidget(); - bool operator<(const QtTreeWidgetItem& item) const; - - signals: - void changed(QtTreeWidgetItem*); - private slots: - void handleChanged(QtTreeWidgetItem* item); - private: - QString toolTipString(); - QList<QtTreeWidgetItem*> children_; - QList<QtTreeWidgetItem*> shownChildren_; - QtTreeWidgetItem* parent_; - QString displayName_; - QString displayNameLower_; - QString statusText_; - QColor textColor_; - QColor backgroundColor_; - QVariant avatar_; - bool shown_; - bool expanded_; - StatusShow::Type statusShowType_; - StatusShow::Type mergedShowType_; -}; - -bool itemLessThan(QtTreeWidgetItem* left, QtTreeWidgetItem* right); - -} diff --git a/Swift/QtUI/Roster/RosterDelegate.cpp b/Swift/QtUI/Roster/RosterDelegate.cpp index aaa6236..e40907a 100644 --- a/Swift/QtUI/Roster/RosterDelegate.cpp +++ b/Swift/QtUI/Roster/RosterDelegate.cpp @@ -12,7 +12,6 @@ #include <QBrush> #include <QFontMetrics> #include <QPainterPath> -#include <QFileInfo> #include <QPolygon> #include <qdebug.h> #include <QBitmap> @@ -22,7 +21,6 @@ #include "QtTreeWidget.h" #include "RosterModel.h" -#include "QtScaledAvatarCache.h" namespace Swift { @@ -43,15 +41,8 @@ QSize RosterDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelI return contactSizeHint(option, index); } -QSize RosterDelegate::contactSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const { - int heightByAvatar = avatarSize_ + common_.verticalMargin * 2; - QFontMetrics nameMetrics(common_.nameFont); - QFontMetrics statusMetrics(common_.detailFont); - int sizeByText = 2 * common_.verticalMargin + nameMetrics.height() + statusMetrics.height(); - //Doesn't work, yay! FIXME: why? - //QSize size = (option.state & QStyle::State_Selected) ? QSize(150, 80) : QSize(150, avatarSize_ + margin_ * 2); - //qDebug() << "Returning size" << size; - return QSize(150, sizeByText > heightByAvatar ? sizeByText : heightByAvatar); +QSize RosterDelegate::contactSizeHint(const QStyleOptionViewItem& option, const QModelIndex& index ) const { + return common_.contactSizeHint(option, index); } void RosterDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { @@ -70,70 +61,18 @@ void RosterDelegate::paintGroup(QPainter* painter, const QStyleOptionViewItem& o } void RosterDelegate::paintContact(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { - //qDebug() << "painting" << index.data(Qt::DisplayRole).toString(); - painter->save(); - //QStyledItemDelegate::paint(painter, option, index); - //initStyleOption(option, index); - QRect fullRegion(option.rect); - if ( option.state & QStyle::State_Selected ) { - painter->fillRect(fullRegion, option.palette.highlight()); - painter->setPen(option.palette.highlightedText().color()); - } else { - QColor nameColor = index.data(Qt::TextColorRole).value<QColor>(); - painter->setPen(QPen(nameColor)); - } - - QRect presenceIconRegion(QPoint(common_.farLeftMargin, fullRegion.top()), QSize(presenceIconWidth_, fullRegion.height() - common_.verticalMargin)); - - int calculatedAvatarSize = presenceIconRegion.height(); - //This overlaps the presenceIcon, so must be painted first - QRect avatarRegion(QPoint(presenceIconRegion.right() - presenceIconWidth_ / 2, presenceIconRegion.top()), QSize(calculatedAvatarSize, calculatedAvatarSize)); - - QPixmap avatarPixmap; + QColor nameColor = index.data(Qt::TextColorRole).value<QColor>(); + QString avatarPath; if (index.data(AvatarRole).isValid() && !index.data(AvatarRole).value<QString>().isNull()) { - QString avatarPath = index.data(AvatarRole).value<QString>(); - QString scaledAvatarPath = QtScaledAvatarCache(avatarRegion.height()).getScaledAvatarPath(avatarPath); - if (QFileInfo(scaledAvatarPath).exists()) { - avatarPixmap.load(scaledAvatarPath); - } + avatarPath = index.data(AvatarRole).value<QString>(); } - if (avatarPixmap.isNull()) { - avatarPixmap = QPixmap(":/icons/avatar.png").scaled(avatarRegion.height(), avatarRegion.width(), Qt::KeepAspectRatio, Qt::SmoothTransformation); - } - - painter->drawPixmap(avatarRegion.topLeft() + QPoint(((avatarRegion.width() - avatarPixmap.width()) / 2), (avatarRegion.height() - avatarPixmap.height()) / 2), avatarPixmap); - - //Paint the presence icon over the top of the avatar QIcon presenceIcon = index.data(PresenceIconRole).isValid() && !index.data(PresenceIconRole).value<QIcon>().isNull() - ? index.data(PresenceIconRole).value<QIcon>() - : QIcon(":/icons/offline.png"); - presenceIcon.paint(painter, presenceIconRegion, Qt::AlignBottom | Qt::AlignHCenter); - - QFontMetrics nameMetrics(common_.nameFont); - painter->setFont(common_.nameFont); - int extraFontWidth = nameMetrics.width("H"); - int leftOffset = avatarRegion.right() + common_.horizontalMargin * 2 + extraFontWidth / 2; - QRect textRegion(fullRegion.adjusted(leftOffset, 0, 0/*-leftOffset*/, 0)); - - int nameHeight = nameMetrics.height() + common_.verticalMargin; - QRect nameRegion(textRegion.adjusted(0, common_.verticalMargin, 0, 0)); - - DelegateCommons::drawElidedText(painter, nameRegion, index.data(Qt::DisplayRole).toString()); - - - painter->setFont(common_.detailFont); - painter->setPen(QPen(QColor(160,160,160))); - - QRect statusTextRegion(textRegion.adjusted(0, nameHeight, 0, 0)); - DelegateCommons::drawElidedText(painter, statusTextRegion, index.data(StatusTextRole).toString()); - - painter->restore(); + ? index.data(PresenceIconRole).value<QIcon>() + : QIcon(":/icons/offline.png"); + QString name = index.data(Qt::DisplayRole).toString(); + QString statusText = index.data(StatusTextRole).toString(); + common_.paintContact(painter, option, nameColor, avatarPath, presenceIcon, name, statusText, 0); } - -const int RosterDelegate::avatarSize_ = 20; -const int RosterDelegate::presenceIconHeight_ = 16; -const int RosterDelegate::presenceIconWidth_ = 16; - } diff --git a/Swift/QtUI/Roster/RosterDelegate.h b/Swift/QtUI/Roster/RosterDelegate.h index e6a16f2..253c11a 100644 --- a/Swift/QtUI/Roster/RosterDelegate.h +++ b/Swift/QtUI/Roster/RosterDelegate.h @@ -28,9 +28,5 @@ namespace Swift { DelegateCommons common_; GroupItemDelegate* groupDelegate_; QtTreeWidget* tree_; - static const int avatarSize_; - static const int presenceIconHeight_; - static const int presenceIconWidth_; - }; } |