diff options
author | Kevin Smith <git@kismith.co.uk> | 2010-04-16 11:25:42 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2010-04-16 11:25:42 (GMT) |
commit | 1e42aa0003876f5416f723d535ca27e7b2f6dc68 (patch) | |
tree | e4cdc3a0c7610f9661f208ddb24086d8090b4303 /Swift/QtUI/ChatList | |
parent | 76e1ad3b1290562ad0ebb4ed430eb1f2566645f0 (diff) | |
download | swift-1e42aa0003876f5416f723d535ca27e7b2f6dc68.zip swift-1e42aa0003876f5416f723d535ca27e7b2f6dc68.tar.bz2 |
Prettify the ChatList a bit.
Resolves: #322
Diffstat (limited to 'Swift/QtUI/ChatList')
-rw-r--r-- | Swift/QtUI/ChatList/ChatListDelegate.cpp | 79 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListDelegate.h | 14 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListMUCItem.cpp | 1 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListMUCItem.h | 6 |
4 files changed, 99 insertions, 1 deletions
diff --git a/Swift/QtUI/ChatList/ChatListDelegate.cpp b/Swift/QtUI/ChatList/ChatListDelegate.cpp index c8e958d..ac05b07 100644 --- a/Swift/QtUI/ChatList/ChatListDelegate.cpp +++ b/Swift/QtUI/ChatList/ChatListDelegate.cpp @@ -4,4 +4,83 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ +#include <QPen> +#include <QPainter> + #include "Swift/QtUI/ChatList/ChatListDelegate.h" +#include "Swift/QtUI/Roster/GroupItemDelegate.h" +#include "Swift/QtUI/ChatList/ChatListItem.h" +#include "Swift/QtUI/ChatList/ChatListMUCItem.h" +#include "Swift/QtUI/ChatList/ChatListGroupItem.h" + +namespace Swift { + +ChatListDelegate::ChatListDelegate() { + groupDelegate_ = new GroupItemDelegate(); +} + +ChatListDelegate::~ChatListDelegate() { + delete groupDelegate_; +} + +QSize ChatListDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index ) const { + ChatListItem* item = static_cast<ChatListItem*>(index.internalPointer()); + if (item && dynamic_cast<ChatListMUCItem*>(item)) { + return mucSizeHint(option, index); + } else if (item && dynamic_cast<ChatListGroupItem*>(item)) { + return groupDelegate_->sizeHint(option, index); + } + return QStyledItemDelegate::sizeHint(option, index); +} + +QSize ChatListDelegate::mucSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const { + QFontMetrics nameMetrics(common_.nameFont); + QFontMetrics statusMetrics(common_.detailFont); + int sizeByText = 2 * common_.verticalMargin + nameMetrics.height() + statusMetrics.height(); + return QSize(150, sizeByText); +} + +void ChatListDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { + ChatListItem* item = static_cast<ChatListItem*>(index.internalPointer()); + if (item && dynamic_cast<ChatListMUCItem*>(item)) { + paintMUC(painter, option, dynamic_cast<ChatListMUCItem*>(item)); + } else if (item && dynamic_cast<ChatListGroupItem*>(item)) { + ChatListGroupItem* group = dynamic_cast<ChatListGroupItem*>(item); + groupDelegate_->paint(painter, option, group->data(Qt::DisplayRole).toString(), group->rowCount(), option.state & QStyle::State_Open); + } else { + QStyledItemDelegate::paint(painter, option, index); + } +} + +void ChatListDelegate::paintMUC(QPainter* painter, const QStyleOptionViewItem& option, ChatListMUCItem* item) 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 { + QColor nameColor = item->data(Qt::TextColorRole).value<QColor>(); + painter->setPen(QPen(nameColor)); + } + + QFontMetrics nameMetrics(common_.nameFont); + painter->setFont(common_.nameFont); + int extraFontWidth = nameMetrics.width("H"); + int leftOffset = common_.horizontalMargin * 2 + extraFontWidth / 2; + QRect textRegion(fullRegion.adjusted(leftOffset, 0, 0, 0)); + + int nameHeight = nameMetrics.height() + common_.verticalMargin; + QRect nameRegion(textRegion.adjusted(0, common_.verticalMargin, 0, 0)); + + painter->drawText(nameRegion, Qt::AlignTop, item->data(Qt::DisplayRole).toString()); + + painter->setFont(common_.detailFont); + painter->setPen(QPen(QColor(160,160,160))); + + QRect detailRegion(textRegion.adjusted(0, nameHeight, 0, 0)); + painter->drawText(detailRegion, Qt::AlignTop, item->data(DetailTextRole).toString()); + + painter->restore(); +} + +} diff --git a/Swift/QtUI/ChatList/ChatListDelegate.h b/Swift/QtUI/ChatList/ChatListDelegate.h index ab7d43e..f6c6c40 100644 --- a/Swift/QtUI/ChatList/ChatListDelegate.h +++ b/Swift/QtUI/ChatList/ChatListDelegate.h @@ -8,10 +8,22 @@ #include <QStyledItemDelegate> -namespace Swift { +#include "Swift/QtUI/Roster/GroupItemDelegate.h" +namespace Swift { + class ChatListMUCItem; class ChatListDelegate : public QStyledItemDelegate { + public: + ChatListDelegate(); + ~ChatListDelegate(); + QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const; + void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; + private: + void paintMUC(QPainter* painter, const QStyleOptionViewItem& option, ChatListMUCItem* item) const; + QSize mucSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const; + DelegateCommons common_; + GroupItemDelegate* groupDelegate_; }; } diff --git a/Swift/QtUI/ChatList/ChatListMUCItem.cpp b/Swift/QtUI/ChatList/ChatListMUCItem.cpp index 91f64e1..ec643d1 100644 --- a/Swift/QtUI/ChatList/ChatListMUCItem.cpp +++ b/Swift/QtUI/ChatList/ChatListMUCItem.cpp @@ -20,6 +20,7 @@ boost::shared_ptr<MUCBookmark> ChatListMUCItem::getBookmark() { QVariant ChatListMUCItem::data(int role) { switch (role) { case Qt::DisplayRole: return P2QSTRING(bookmark_->getName()); + case DetailTextRole: return P2QSTRING(bookmark_->getRoom().toString()); /*case Qt::TextColorRole: return textColor_; case Qt::BackgroundColorRole: return backgroundColor_; case Qt::ToolTipRole: return isContact() ? toolTipString() : QVariant(); diff --git a/Swift/QtUI/ChatList/ChatListMUCItem.h b/Swift/QtUI/ChatList/ChatListMUCItem.h index 58abb55..4170c6f 100644 --- a/Swift/QtUI/ChatList/ChatListMUCItem.h +++ b/Swift/QtUI/ChatList/ChatListMUCItem.h @@ -15,6 +15,12 @@ #include "Swift/QtUI/ChatList/ChatListItem.h" namespace Swift { + enum MUCItemRoles { + DetailTextRole = Qt::UserRole/*, + AvatarRole = Qt::UserRole + 1, + PresenceIconRole = Qt::UserRole + 2, + StatusShowTypeRole = Qt::UserRole + 3*/ + }; class ChatListMUCItem : public ChatListItem { public: ChatListMUCItem(boost::shared_ptr<MUCBookmark> bookmark, ChatListGroupItem* parent); |