diff options
author | Kevin Smith <git@kismith.co.uk> | 2010-12-31 21:55:47 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2010-12-31 21:55:47 (GMT) |
commit | 437adabd3798dae0f1eeea0b4f8c2d4212a083e6 (patch) | |
tree | 25cd5d6e449a67d881bb9031416ffee37013201a | |
parent | 3fdf13823a391c7db83e05685e78db19e4110e59 (diff) | |
download | swift-contrib-437adabd3798dae0f1eeea0b4f8c2d4212a083e6.zip swift-contrib-437adabd3798dae0f1eeea0b4f8c2d4212a083e6.tar.bz2 |
Slightly sensible renderer for user search results.
Resolves: #719
-rw-r--r-- | Swift/QtUI/UserSearch/UserSearchDelegate.cpp | 106 | ||||
-rw-r--r-- | Swift/QtUI/UserSearch/UserSearchDelegate.h | 8 | ||||
-rw-r--r-- | Swift/QtUI/UserSearch/UserSearchModel.cpp | 31 | ||||
-rw-r--r-- | Swift/QtUI/UserSearch/UserSearchModel.h | 9 |
4 files changed, 91 insertions, 63 deletions
diff --git a/Swift/QtUI/UserSearch/UserSearchDelegate.cpp b/Swift/QtUI/UserSearch/UserSearchDelegate.cpp index ff3e766..812c1c3 100644 --- a/Swift/QtUI/UserSearch/UserSearchDelegate.cpp +++ b/Swift/QtUI/UserSearch/UserSearchDelegate.cpp @@ -6,11 +6,14 @@ #include <QPen> #include <QPainter> +#include <QStyleOptionViewItem> +#include <QModelIndex> +#include <QFontMetrics> #include "Swift/QtUI/UserSearch/UserSearchDelegate.h" //#include "Swift/QtUI/Roster/GroupItemDelegate.h" -//#include "Swift/QtUI/MUCSearch/MUCSearchItem.h" -//#include "Swift/QtUI/MUCSearch/MUCSearchRoomItem.h" +#include <Swift/Controllers/Chat/UserSearchController.h> +#include <Swift/QtUI/UserSearch/UserSearchModel.h> //#include "Swift/QtUI/MUCSearch/MUCSearchServiceItem.h" namespace Swift { @@ -23,64 +26,45 @@ UserSearchDelegate::~UserSearchDelegate() { } -// QSize MUCSearchDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index ) const { -// // MUCSearchItem* item = static_cast<MUCSearchItem*>(index.internalPointer()); -// // if (item && dynamic_cast<MUCSearchMUCItem*>(item)) { -// // return mucSizeHint(option, index); -// // } else if (item && dynamic_cast<MUCSearchGroupItem*>(item)) { -// // return groupDelegate_->sizeHint(option, index); -// // } -// return QStyledItemDelegate::sizeHint(option, index); -// } - -// QSize MUCSearchDelegate::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 MUCSearchDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { -// MUCSearchItem* item = static_cast<MUCSearchItem*>(index.internalPointer()); -// if (item && dynamic_cast<MUCSearchMUCItem*>(item)) { -// paintMUC(painter, option, dynamic_cast<MUCSearchMUCItem*>(item)); -// } else if (item && dynamic_cast<MUCSearchGroupItem*>(item)) { -// MUCSearchGroupItem* group = dynamic_cast<MUCSearchGroupItem*>(item); -// groupDelegate_->paint(painter, option, group->data(Qt::DisplayRole).toString(), group->rowCount(), option.state & QStyle::State_Open); -// } else { -// QStyledItemDelegate::paint(painter, option, index); -// } -// } - -// void MUCSearchDelegate::paintMUC(QPainter* painter, const QStyleOptionViewItem& option, MUCSearchMUCItem* 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(); -// } +QSize UserSearchDelegate::sizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const { + //UserSearchItem* item = static_cast<UserSearchItem*>(index.internalPointer()); + QFontMetrics nameMetrics(common_.nameFont); + QFontMetrics statusMetrics(common_.detailFont); + int sizeByText = 2 * common_.verticalMargin + nameMetrics.height() + statusMetrics.height(); + return QSize(150, sizeByText); +} + +void UserSearchDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { + UserSearchResult* item = static_cast<UserSearchResult*>(index.internalPointer()); + 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 = UserSearchModel::data(item, 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, UserSearchModel::data(item, 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, UserSearchModel::data(item, UserSearchModel::DetailTextRole).toString()); + + painter->restore(); +} } diff --git a/Swift/QtUI/UserSearch/UserSearchDelegate.h b/Swift/QtUI/UserSearch/UserSearchDelegate.h index d046d62..69848a8 100644 --- a/Swift/QtUI/UserSearch/UserSearchDelegate.h +++ b/Swift/QtUI/UserSearch/UserSearchDelegate.h @@ -7,14 +7,20 @@ #pragma once #include <QStyledItemDelegate> +#include <QPainter> +#include <QStyleOptionViewItem> -#include "Swift/QtUI/Roster/DelegateCommons.h" +#include <Swift/QtUI/Roster/DelegateCommons.h> namespace Swift { class UserSearchDelegate : public QStyledItemDelegate { public: UserSearchDelegate(); ~UserSearchDelegate(); + void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; + QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index ) const; + private: + DelegateCommons common_; }; } diff --git a/Swift/QtUI/UserSearch/UserSearchModel.cpp b/Swift/QtUI/UserSearch/UserSearchModel.cpp index f6fd4c0..1187c29 100644 --- a/Swift/QtUI/UserSearch/UserSearchModel.cpp +++ b/Swift/QtUI/UserSearch/UserSearchModel.cpp @@ -33,12 +33,41 @@ int UserSearchModel::columnCount(const QModelIndex& /*parent*/) const { QVariant UserSearchModel::data(const QModelIndex& index, int role) const { if (!index.isValid()) return QVariant(); UserSearchResult* result = static_cast<UserSearchResult*>(index.internalPointer()); + return data(result, role); +} + +QVariant UserSearchModel::data(UserSearchResult* item, int role) { switch (role) { - case Qt::DisplayRole: return QVariant(P2QSTRING(result->getJID().toString())); + case Qt::DisplayRole: return QVariant(nameLine(item)); + case DetailTextRole: return QVariant(detailLine(item)); default: return QVariant(); } } +QString UserSearchModel::nameLine(UserSearchResult* item) { + QString result; + const std::map<String, String> fields = item->getFields(); + std::map<String, String>::const_iterator first = fields.find("first"); + if (first != fields.end()) { + result += P2QSTRING((*first).second); + } + std::map<String, String>::const_iterator last = fields.find("last"); + if (last != fields.end()) { + if (!result.isEmpty()) { + result += " "; + } + result += P2QSTRING((*last).second); + } + if (result.isEmpty()) { + result = P2QSTRING(item->getJID().toString()); + } + return result; +} + +QString UserSearchModel::detailLine(UserSearchResult* item) { + return P2QSTRING(item->getJID().toString()); +} + QModelIndex UserSearchModel::index(int row, int column, const QModelIndex & parent) const { if (!hasIndex(row, column, parent)) { return QModelIndex(); diff --git a/Swift/QtUI/UserSearch/UserSearchModel.h b/Swift/QtUI/UserSearch/UserSearchModel.h index d766d9a..b547033 100644 --- a/Swift/QtUI/UserSearch/UserSearchModel.h +++ b/Swift/QtUI/UserSearch/UserSearchModel.h @@ -17,15 +17,24 @@ namespace Swift { class UserSearchModel : public QAbstractItemModel { Q_OBJECT public: + enum UserItemRoles { + DetailTextRole = Qt::UserRole/*, + AvatarRole = Qt::UserRole + 1, + PresenceIconRole = Qt::UserRole + 2, + StatusShowTypeRole = Qt::UserRole + 3*/ + }; UserSearchModel(); void clear(); void setResults(const std::vector<UserSearchResult>& results); int columnCount(const QModelIndex& parent = QModelIndex()) const; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; + static QVariant data(UserSearchResult* item, int role); QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const; QModelIndex parent(const QModelIndex& index) const; int rowCount(const QModelIndex& parent = QModelIndex()) const; private: + static QString nameLine(UserSearchResult* item); + static QString detailLine(UserSearchResult* item); std::vector<UserSearchResult> results_; }; |