Commit be6dfde5 authored by Scott Wheeler's avatar Scott Wheeler

Move the lines caclulation code to a separate function so that it can be used

to calculate the item heights before they're painted.  Oh, and calculate the
item heights before they're painted so that scrolling isn't goofy.

svn path=/trunk/kdemultimedia/juk/; revision=253614
parent 9bd2e293
......@@ -450,7 +450,7 @@ void PlaylistSplitter::slotRenameFile()
void PlaylistSplitter::setupLayout()
{
setOpaqueResize(true);
setOpaqueResize(false);
m_playlistBox = new PlaylistBox(this, "playlistBox");
......
......@@ -36,6 +36,8 @@ ViewMode::ViewMode(PlaylistBox *b) : QObject(b),
{
connect(this, SIGNAL(signalCreateSearchList(const PlaylistSearch &, const QString &, const QString &)),
b, SIGNAL(signalCreateSearchList(const PlaylistSearch &, const QString &, const QString &)));
m_playlistBox->viewport()->installEventFilter(this);
}
ViewMode::~ViewMode()
......@@ -56,37 +58,16 @@ void ViewMode::paintCell(PlaylistBox::Item *i,
PlaylistBox::Item *item = static_cast<PlaylistBox::Item *>(i);
QFontMetrics fm = painter->fontMetrics();
QString line = item->text();
QStringList lines;
while(!line.isEmpty()) {
int textLength = line.length();
while(textLength > 0 &&
fm.width(line.mid(0, textLength).stripWhiteSpace()) + item->listView()->itemMargin() * 2 > width)
{
int i = line.findRev(QRegExp( "\\W"), textLength - 1);
if(i > 0)
textLength = i;
else
textLength--;
}
lines.append(line.mid(0, textLength).stripWhiteSpace());
line = line.mid(textLength);
}
int y = item->listView()->itemMargin();
const QPixmap *pm = item->pixmap(column);
int height = 3 * item->listView()->itemMargin() + pm->height() +
(fm.height() - fm.descent()) * lines.count();
if(item->isSelected()) {
painter->fillRect(0, 0, width, height, colorGroup.brush(QColorGroup::Highlight));
painter->fillRect(0, 0, width, item->height(), colorGroup.brush(QColorGroup::Highlight));
painter->setPen(colorGroup.highlightedText());
}
else
painter->eraseRect(0, 0, width, height);
painter->eraseRect(0, 0, width, item->height());
if (!pm->isNull()) {
int x = (width - pm->width()) / 2;
......@@ -94,13 +75,12 @@ void ViewMode::paintCell(PlaylistBox::Item *i,
painter->drawPixmap(x, y, *pm);
}
y += pm->height() + fm.height() - fm.descent();
for(QStringList::Iterator it = lines.begin(); it != lines.end(); ++it) {
for(QStringList::Iterator it = m_lines[i].begin(); it != m_lines[i].end(); ++it) {
int x = (width - fm.width(*it)) / 2;
x = QMAX(x, item->listView()->itemMargin());
painter->drawText(x, y, *it);
y += fm.height() - fm.descent();
}
item->setHeight(height);
}
PlaylistBox::Item *ViewMode::createSearchItem(PlaylistBox *box, SearchPlaylist *playlist,
......@@ -109,11 +89,21 @@ PlaylistBox::Item *ViewMode::createSearchItem(PlaylistBox *box, SearchPlaylist *
return new PlaylistBox::Item(box, "midi", playlist->name(), playlist);
}
bool ViewMode::eventFilter(QObject *watched, QEvent *e)
{
if(watched == m_playlistBox->viewport() && e->type() == QEvent::Resize)
updateHeights(static_cast<QResizeEvent *>(e)->size().width());
return QObject::eventFilter(watched, e);
}
void ViewMode::setShown(bool shown)
{
m_visible = shown;
if(shown)
if(shown) {
updateIcons(32);
updateHeights();
}
}
void ViewMode::updateIcons(int size)
......@@ -124,6 +114,52 @@ void ViewMode::updateIcons(int size)
}
}
void ViewMode::updateHeights(int width)
{
static int oldWidth = 0;
if(width == 0)
width = m_playlistBox->visibleWidth();
if(oldWidth == width)
return;
oldWidth = width;
const int baseHeight = 3 * m_playlistBox->itemMargin() + 32;
const QFontMetrics fm = m_playlistBox->fontMetrics();
for(QListViewItemIterator it(m_playlistBox); it.current(); ++it) {
PlaylistBox::Item *i = static_cast<PlaylistBox::Item *>(it.current());
m_lines[i] = lines(i, fm, width);
const int height = baseHeight + (fm.height() - fm.descent()) * m_lines[i].count();
i->setHeight(height);
i->invalidateHeight();
}
}
QStringList ViewMode::lines(const PlaylistBox::Item *item, const QFontMetrics &fm, int width) const
{
QString line = item->text();
QStringList l;
while(!line.isEmpty()) {
int textLength = line.length();
while(textLength > 0 &&
fm.width(line.mid(0, textLength).stripWhiteSpace()) + item->listView()->itemMargin() * 2 > width)
{
int i = line.findRev(QRegExp( "\\W"), textLength - 1);
if(i > 0)
textLength = i;
else
textLength--;
}
l.append(line.mid(0, textLength).stripWhiteSpace());
line = line.mid(textLength);
}
return l;
}
///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////
......
......@@ -39,7 +39,7 @@ public:
ViewMode(PlaylistBox *b);
virtual ~ViewMode();
virtual QString name() const { return i18n("Default"); }
virtual QString name() const { return i18n("Default"); }
virtual void setShown(bool shown);
virtual void paintCell(PlaylistBox::Item *item,
......@@ -50,20 +50,28 @@ public:
virtual PlaylistBox::Item *createSearchItem(PlaylistBox *box,
SearchPlaylist *playlist,
const QString &searchCategory);
virtual bool eventFilter(QObject *watched, QEvent *e);
protected:
PlaylistBox *playlistBox() const { return m_playlistBox; }
bool visible() const { return m_visible; }
bool visible() const { return m_visible; }
void updateIcons(int size);
protected:
virtual void updateHeights(int width = 0);
signals:
void signalCreateSearchList(const PlaylistSearch &search,
const QString &searchCategory,
const QString &name);
private:
QStringList lines(const PlaylistBox::Item *item, const QFontMetrics &fm, int width) const;
PlaylistBox *m_playlistBox;
bool m_visible;
QMap<PlaylistBox::Item *, QStringList> m_lines;
};
////////////////////////////////////////////////////////////////////////////////
......@@ -81,6 +89,11 @@ public:
QPainter *painter,
const QColorGroup &colorGroup,
int column, int width, int align);
protected:
/**
* Override the implementation from the base class. This isn't needed here.
*/
virtual void updateHeights(int = 0) {}
};
////////////////////////////////////////////////////////////////////////////////
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment