Improve subitle showing in Plasma::Delegate.

Automatically show subtitle for adjasent items with the same content
only when model doesn't provide explicit data for SubTitleMandatoryRole,
otherwise respect what model says and set visibility of subtitle accordingly

Review: http://reviewboard.kde.org/r/1357/

svn path=/trunk/KDE/kdelibs/; revision=1014442
This commit is contained in:
Dmitry Suzdalev 2009-08-22 20:47:15 +00:00
parent 67db5ce960
commit d21f690651
2 changed files with 36 additions and 22 deletions

View File

@ -239,18 +239,27 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
QRect subTitleRect = d->subTitleRect(option, index); QRect subTitleRect = d->subTitleRect(option, index);
subTitleRect.moveTopLeft(subTitleRect.topLeft()-option.rect.topLeft()); subTitleRect.moveTopLeft(subTitleRect.topLeft()-option.rect.topLeft());
bool uniqueTitle = !index.data(d->roles[SubTitleMandatoryRole]).value<bool>();// true; // If the model wants to have exact control for subtitles showing
if (uniqueTitle) { // it is expected to return a valid data for SubTitleMandatoryRole.
QModelIndex sib = index.sibling(index.row() + 1, index.column()); // If it doesn't return a valid data for this role
if (sib.isValid()) { // then by default we well be showing a subtitles for
uniqueTitle = sib.data(Qt::DisplayRole).value<QString>() != titleText; // adjasent items with the same content (see comments below too)
} bool uniqueTitle = true;
QVariant mandatoryRoleData = index.data(d->roles[SubTitleMandatoryRole]);
if (!mandatoryRoleData.isValid()) {
uniqueTitle = !mandatoryRoleData.value<bool>();// true;
if (uniqueTitle) { if (uniqueTitle) {
sib = index.sibling(index.row() + -1, index.column()); QModelIndex sib = index.sibling(index.row() + 1, index.column());
if (sib.isValid()) { if (sib.isValid()) {
uniqueTitle = sib.data(Qt::DisplayRole).value<QString>() != titleText; uniqueTitle = sib.data(Qt::DisplayRole).value<QString>() != titleText;
} }
if (uniqueTitle) {
sib = index.sibling(index.row() + -1, index.column());
if (sib.isValid()) {
uniqueTitle = sib.data(Qt::DisplayRole).value<QString>() != titleText;
}
}
} }
} }
@ -289,19 +298,20 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
} }
p.drawText(titleRect, Qt::AlignLeft|Qt::AlignVCenter, titleText); p.drawText(titleRect, Qt::AlignLeft|Qt::AlignVCenter, titleText);
if (hover || !uniqueTitle) { // draw sub-title, BUT only if:
// draw sub-title, BUT only if: // * SubTitleMandatoryRole is defined and model returns 'true'
// * it isn't a unique title, this allows two items to have the same title and be // * SubTitleMandatoryRole is not defined and the adjasent model indexes
// disambiguated by their subtitle // have the same contents of the Qt::DisplayRole
// * we are directed by the model that this item should never be treated as unique // * when model doesn't provide a valid data for SubTitleMandatory role
// e.g. the documents list in the recently used tab // we also show title on mouse hover
// * the mouse is hovered over the item, causing the additional information to be //
// displayed // the rationale for this is that subtitle text should in most cases not be
// // required to understand the item itself and that showing all the subtexts in a
// the rational for this is that subtitle text should in most cases not be // listing makes the information density very high, impacting both the speed at
// required to understand the item itself and that showing all the subtexts in a // which one can scan the list visually and the aesthetic qualities of the listing.
// listing makes the information density very high, impacting both the speed at bool drawSubTitle = mandatoryRoleData.isValid() ? mandatoryRoleData.value<bool>() : (hover || !uniqueTitle);
// which one can scan the list visually and the aesthetic qualities of the listing.
if (drawSubTitle) {
if (option.palette.color(QPalette::Base).alpha() > 0) { if (option.palette.color(QPalette::Base).alpha() > 0) {
p.setPen(QPen(KColorScheme(QPalette::Active).foreground(KColorScheme::InactiveText), 1)); p.setPen(QPen(KColorScheme(QPalette::Active).foreground(KColorScheme::InactiveText), 1));
} else { } else {
@ -341,7 +351,7 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
} }
} else { } else {
if (((titleRect.width() + decorationRect.width() + 10) > option.rect.width() || if (((titleRect.width() + decorationRect.width() + 10) > option.rect.width() ||
(subTitleRect.width() + decorationRect.width() + 15 )> option.rect.width()) && (subTitleRect.width() + decorationRect.width() + 15 )> option.rect.width()) &&
(titleRect.width() > 120 || subTitleRect.width() > 120)) { (titleRect.width() > 120 || subTitleRect.width() > 120)) {
buffer.fill(Qt::transparent); buffer.fill(Qt::transparent);

View File

@ -41,6 +41,10 @@ class DelegatePrivate;
* SubTitleRole: the text of the subtitle * SubTitleRole: the text of the subtitle
* SubTitleMandatoryRole: if the subtitle is to always be displayed * SubTitleMandatoryRole: if the subtitle is to always be displayed
* (as default the subtitle is displayed only on mouse over) * (as default the subtitle is displayed only on mouse over)
* NOTE: if model doesn't return a valid data for SubTitleMandatoryRole (i.e. if it returns QVaraint())
* then subtitles will be shown for adjasent items with the same content and not shown
* otherwise.
*
* ColumnTypeRole: if the column is a main column (with title and subtitle) * ColumnTypeRole: if the column is a main column (with title and subtitle)
* or a secondary action column (only a little icon that appears on mouse * or a secondary action column (only a little icon that appears on mouse
* over is displayed) * over is displayed)