Author: kmachalkova
Date: Wed Jun 17 14:31:37 2009
New Revision: 57610
URL: http://svn.opensuse.org/viewcvs/yast?rev=57610&view=rev
Log:
Draw icon items nicely when mouse is over them
(show GenericName as item description)
Modified:
branches/tmp/tgoettlicher/yast2cc_rewrite/src/main_window.cpp
branches/tmp/tgoettlicher/yast2cc_rewrite/src/moduleiconitem.cpp
branches/tmp/tgoettlicher/yast2cc_rewrite/src/moduleiconitem.h
Modified: branches/tmp/tgoettlicher/yast2cc_rewrite/src/main_window.cpp
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/tgoettlicher/yast2cc_rewrite/src/main_window.cpp?rev=57610&r1=57609&r2=57610&view=diff
==============================================================================
--- branches/tmp/tgoettlicher/yast2cc_rewrite/src/main_window.cpp (original)
+++ branches/tmp/tgoettlicher/yast2cc_rewrite/src/main_window.cpp Wed Jun 17 14:31:37 2009
@@ -81,7 +81,7 @@
// tv->setSpacing(KDialog::spacingHint());
d->modview->setCategoryDrawer( drawer );
d->modview->setViewMode( QListView::IconMode );
- d->modview->setItemDelegate( new ModuleIconItemDelegate( this ) );
+ d->modview->setItemDelegate( new ModuleIconItemDelegate( this , d->modview->style() ) );
d->modview->setMouseTracking( true );
d->modview->viewport()->setAttribute( Qt::WA_Hover );
Modified: branches/tmp/tgoettlicher/yast2cc_rewrite/src/moduleiconitem.cpp
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/tgoettlicher/yast2cc_rewrite/src/moduleiconitem.cpp?rev=57610&r1=57609&r2=57610&view=diff
==============================================================================
--- branches/tmp/tgoettlicher/yast2cc_rewrite/src/moduleiconitem.cpp (original)
+++ branches/tmp/tgoettlicher/yast2cc_rewrite/src/moduleiconitem.cpp Wed Jun 17 14:31:37 2009
@@ -30,11 +30,15 @@
#include
#include <iostream>
+#include <QDebug>
+
#define IMAGE_SIZE 32
#define ICON_WIDTH 100
-ModuleIconItemDelegate::ModuleIconItemDelegate(QObject *parent) : QItemDelegate(parent)
+ModuleIconItemDelegate::ModuleIconItemDelegate(QObject *parent, const QStyle *style) :
+ QItemDelegate(parent)
+ , lviewstyle( style )
{
}
@@ -45,6 +49,9 @@
QStyle *style;
bool selected = option.state & QStyle::State_Selected || option.state & QStyle::State_HasFocus;
+ bool mouseover = option.state & QStyle::State_MouseOver;
+ const QSize &decorationSize = option.decorationSize;
+
if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast(&option)) {
style = v3->widget->style();
if (!v3->widget->hasFocus()) selected = false;
@@ -52,34 +59,65 @@
style = QApplication::style();
}
- if (selected) {
+
+ QPen p = painter->pen();
+ if (selected || mouseover)
+ {
+ if (selected)
+ {
painter->fillPath(roundedRectangle(option.rect, 10), option.palette.brush(QPalette::Highlight));
painter->setPen(option.palette.color(QPalette::HighlightedText));
- }
-
- if (!selected && (option.state & QStyle::State_MouseOver)) {
+ }
+ else
+ {
QColor hover = option.palette.color(QPalette::Highlight);
hover.setAlpha(88);
painter->fillPath(roundedRectangle(option.rect, 10), hover);
+
+ QColor inv = option.palette.color( QPalette::WindowText );
+ inv.setAlpha(100);
+ painter->setPen( inv );
+ }
+
+ QString descr = option.fontMetrics.elidedText( index.data( KCategorizedSortFilterProxyModel::GenericNameRole ).toString(), Qt::ElideRight, option.rect.width() );
+ QRect tr = option.rect;
+ tr.setTop( tr.top() + decorationSize.height() + 2*style->pixelMetric( QStyle::PM_FocusFrameVMargin) );
+ painter->drawText( tr, Qt::AlignLeft | Qt::TextSingleLine, descr );
+
}
+ painter->setPen( p );
// if( index.data( Qt::UserRole ).toInt() == KIconLoader::DisabledState ) {
// painter->setPen( option.palette.color( QPalette::Disabled, QPalette::Text ) );
// }
- const QSize &decorationSize = option.decorationSize;
- QIcon::Mode iconMode = QIcon::Normal
+ QIcon::Mode iconMode = QIcon::Normal;
if (selected) iconMode = QIcon::Selected;
const QPixmap &pixmap = qvariant_cast<QIcon>(index.data(Qt::DecorationRole)).pixmap(option.decorationSize, iconMode);
- int iconX = option.rect.left() + (option.rect.width() - decorationSize.width()) / 2;
- painter->drawPixmap(iconX, option.rect.top() + style->pixelMetric(QStyle::PM_FocusFrameVMargin), decorationSize.width(), decorationSize.height(), pixmap);
+ int iconX = option.rect.left() + style->pixelMetric( QStyle::PM_FocusFrameHMargin );
+ int iconY = option.rect.top() + style->pixelMetric( QStyle::PM_FocusFrameVMargin );
+ painter->drawPixmap(iconX, iconY, decorationSize.width(), decorationSize.height(), pixmap);
QRect textRectangle = option.rect;
- textRectangle.setTop(textRectangle.top() + decorationSize.height() + style->pixelMetric(QStyle::PM_FocusFrameVMargin));
- painter->drawText(textRectangle, Qt::AlignHCenter | Qt::TextWordWrap, index.data(Qt::DisplayRole).toString());
+ textRectangle.setLeft (textRectangle.left() + decorationSize.width() + 2*style->pixelMetric(QStyle::PM_FocusFrameHMargin));
+ textRectangle.setHeight( decorationSize.height() + 2*style->pixelMetric(QStyle::PM_FocusFrameVMargin));
+ painter->drawText(textRectangle, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextWordWrap, index.data(Qt::DisplayRole).toString());
painter->restore();
}
+QSize ModuleIconItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
+{
+ QString text = index.data( Qt::DisplayRole).toString();
+ QVariant v = index.data(Qt::FontRole);
+ QFont f = qvariant_cast<QFont>(v);
+ QFontMetrics fm( f );
+
+ QSize s = QItemDelegate::sizeHint( option, index );
+ int w = lviewstyle->pixelMetric( QStyle::PM_FocusFrameHMargin ) + 2*lviewstyle->pixelMetric( QStyle::PM_ListViewIconSize ) +
+ + fm.boundingRect( text ).width();
+ return QSize( w, s.height());
+}
+
// Method taken from KFileItemDelegate. Check whether it has been moved to
// kdefx/kdrawutil.cpp as the comment says on Fredrik's code. If so, remove
// this code (duplication), and use the library one.
Modified: branches/tmp/tgoettlicher/yast2cc_rewrite/src/moduleiconitem.h
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/tgoettlicher/yast2cc_rewrite/src/moduleiconitem.h?rev=57610&r1=57609&r2=57610&view=diff
==============================================================================
--- branches/tmp/tgoettlicher/yast2cc_rewrite/src/moduleiconitem.h (original)
+++ branches/tmp/tgoettlicher/yast2cc_rewrite/src/moduleiconitem.h Wed Jun 17 14:31:37 2009
@@ -28,16 +28,21 @@
#include <QPainterPath>
#include <QRectF>
+#include "kcategorizedsortfilterproxymodel.h"
+
class KCModuleInfo;
class ModuleIconItemDelegate : public QItemDelegate
{
public:
- ModuleIconItemDelegate(QObject *parent);
+ ModuleIconItemDelegate(QObject *parent, const QStyle *style);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
+ virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
private:
+ const QStyle *lviewstyle;
+
// Method taken from KFileItemDelegate. Check whether it has been moved to
// kdefx/kdrawutil.cpp as the comment says on Fredrik's code. If so, remove
// this code (duplication), and use the library one.
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org