KWrappedListViewItem

Rob Kaper cap at capsi.com
Sat Jun 5 00:32:20 BST 2004


Here's quick hack for a "more or less drop-in" KListViewItem replacement
which utilizes KWordWrap.

Known issues:

- bounding rectangle width calculation isn't 100% perfect

- not yet prepared for BC

- works on the last column only

- resizing only works partially: resizing the listview works fine, manually
  adjusting the width of the column headers doesn't

- constructor doesn't allow as many columns as KListViewItem (in fact, just
  two at the moment)

- creating a new fontmetrics every single time is ridiculous. Perhaps a
  KWrappedListView can cache this and connect to a proper KDE signal in case
  of a font change etc?

- QObject is expensive, but I haven't found another way to deal with
  resizes.

Perhaps there's a better way of doing this, but I've looked around and could
not find any examples. I've seen plenty of requests for a class like this
one though, so perhaps this hack can be a starting point for a
KWrappedListViewItem in KDE 4.0.

Example screenshot:
http://unixcode.org/img/atlantik/atlantik-20040530-kwrappedlistviewitem.png

Any thoughts?

Rob
-- 
Rob Kaper     | "Whoever wants to know the heart and mind of America had
cap at capsi.com | better learn baseball" -- Jacques Barzun
-------------- next part --------------
// Copyright (c) 2004 Rob Kaper <rob at unixcode.org>. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.

#include <qheader.h>
#include <qstring.h>

#include <kglobalsettings.h>
#include <klistview.h>
#include <kwordwrap.h>

#include "kwrappedlistviewitem.h"

KWrappedListViewItem::KWrappedListViewItem( QListView *parent, QString text, QString t2 )
: QObject(), KListViewItem( parent )
{
	init( parent, text, t2 );
}

KWrappedListViewItem::KWrappedListViewItem( QListView *parent, QListViewItem *after, QString text, QString t2 )
: QObject(), KListViewItem( parent, after )
{
	init( parent, text, t2 );
}

void KWrappedListViewItem::setup()
{
	widthChanged();
}

/*
int KWrappedListViewItem::width( const QFontMetrics&, const QListView*, int ) const
{
	return m_wrap->boundingRect().width();
}
*/

void KWrappedListViewItem::wrapColumn( int c )
{
	if ( c != m_wrapColumn )
		return;

	QListView *lv = listView();
	if ( !lv )
		return;

	QFont font = QFont( KGlobalSettings::generalFont().family(), KGlobalSettings::generalFont().pointSize(), QFont::Normal );
	QFontMetrics fm = QFontMetrics( font );

	int wrapWidth = lv->width();
	for ( int i = 0 ; i < m_wrapColumn ; i++ )
		wrapWidth -= ( width(fm, lv, i) + lv->itemMargin() );

	if ( pixmap(c) )
		wrapWidth -= ( pixmap( c )->width() + lv->itemMargin() );

	QScrollBar *scrollBar = lv->verticalScrollBar();
	if ( !scrollBar->isHidden() )
		wrapWidth -= scrollBar->width();

	QRect rect = QRect( 0, 0, wrapWidth - 20, -1 );

	KWordWrap *wrap = KWordWrap::formatText( fm, rect, 0, m_origText );
	setText( c, wrap->wrappedString() );

	int lc = text(c).contains( QChar( '\n' ) ) + 1;
	setHeight( wrap->boundingRect().height() + lc*lv->itemMargin() );

	widthChanged( c );

	delete wrap;
}

void KWrappedListViewItem::init( QListView *parent, QString text, QString t2 )
{
	m_wrapColumn = 0;
	setMultiLinesEnabled( true );
	parent->setResizeMode( QListView::LastColumn );

	m_origText = text;

	if ( !t2.isNull() )
	{
		setText( 0, text );
		m_origText = t2;
		m_wrapColumn = 1;
	}
	else
		m_origText = text;

	wrapColumn( m_wrapColumn );
		
	connect( parent->header(), SIGNAL(sizeChange(int, int, int)), this, SLOT(wrapColumn(int)));
}

#include "kwrappedlistviewitem.moc"
-------------- next part --------------
// Copyright (c) 2004 Rob Kaper <rob at unixcode.org>. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
         
#ifndef KWRAPPEDLISTVIEWITEM_H
#define KWRAPPEDLISTVIEWITEM_H

#include <qobject.h>
#include <qstring.h>

#include <klistview.h>

class KWordWrap;

class KWrappedListViewItem : public QObject, public KListViewItem
{
Q_OBJECT

public:
	KWrappedListViewItem( QListView *parent, QString text, QString=QString::null );
	KWrappedListViewItem( QListView *parent, QListViewItem *after, QString text, QString=QString::null );
	void setup();
//	int width(const QFontMetrics& fm, const QListView* lv, int c) const;

private slots:
	void wrapColumn( int c );

private:
	void init( QListView *parent, QString text, QString=QString::null );
	QString m_origText;
	int m_wrapColumn;
};

#endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20040605/2d56d2c8/attachment.sig>


More information about the kde-core-devel mailing list