Commit 8c239b47 authored by Scott Wheeler's avatar Scott Wheeler

More changes related to the width. Don't barf if there are no items in the

list.  Update the width if the width of the contents change.  Update the
width when new items are inserted.

svn path=/trunk/kdemultimedia/juk/; revision=248356
parent c9683190
......@@ -216,6 +216,7 @@ Playlist::Playlist(QWidget *parent, const QString &name) :
m_selectedCount(0),
m_allowDuplicates(false),
m_polished(false),
m_disableColumnWidthUpdates(true),
m_lastSelected(0),
m_playlistName(name),
m_rmbMenu(0)
......@@ -228,6 +229,7 @@ Playlist::Playlist(const QFileInfo &playlistFile, QWidget *parent, const QString
m_selectedCount(0),
m_allowDuplicates(false),
m_polished(false),
m_disableColumnWidthUpdates(true),
m_lastSelected(0),
m_fileName(playlistFile.absFilePath()),
m_rmbMenu(0)
......@@ -556,6 +558,21 @@ void Playlist::slotReload()
loadFile(m_fileName, fileInfo);
}
void Playlist::slotWidthDirty(int column)
{
if(column < 0) {
m_widthDirty.clear();
for(int i = 0; i < columns(); i++) {
if(isColumnVisible(i))
m_widthDirty.append(i);
}
return;
}
if(m_widthDirty.find(column) == m_widthDirty.end())
m_widthDirty.append(column);
}
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////
......@@ -854,6 +871,8 @@ void Playlist::polish()
setAcceptDrops(true);
setDropVisualizer(true);
m_disableColumnWidthUpdates = false;
}
////////////////////////////////////////////////////////////////////////////////
......@@ -883,6 +902,8 @@ void Playlist::loadFile(const QString &fileName, const QFileInfo &fileInfo)
PlaylistItem *after = 0;
m_disableColumnWidthUpdates = true;
while(!stream.atEnd()) {
QString itemName = stream.readLine().stripWhiteSpace();
......@@ -904,6 +925,8 @@ void Playlist::loadFile(const QString &fileName, const QFileInfo &fileInfo)
file.close();
emit signalCountChanged(this);
m_disableColumnWidthUpdates = false;
}
void Playlist::setPlaying(PlaylistItem *item, bool p)
......@@ -936,25 +959,36 @@ int Playlist::leftMostVisibleColumn() const
void Playlist::updateColumnWidths()
{
if(m_disableColumnWidthUpdates)
return;
// Make sure that the column weights have been initialized before trying to
// update the columns.
if(m_columnWeights.isEmpty())
return;
QValueList<int> visibleColumns;
for(int i = 0; i < columns(); i++) {
if(isColumnVisible(i))
visibleColumns.append(i);
}
QValueListConstIterator<int> it;
if(count() == 0) {
for(it = visibleColumns.begin(); it != visibleColumns.end(); ++it)
setColumnWidth(*it, header()->fontMetrics().width(header()->label(*it) + 10));
return;
}
if(m_columnWeights.isEmpty())
return;
// First build a list of minimum widths based on the strings in the listview
// header. We won't let the width of the column go below this width.
QValueVector<int> minimumWidth(columns(), 0);
int minimumWidthTotal = 0;
QValueListConstIterator<int> it;
for(it = visibleColumns.begin(); it != visibleColumns.end(); ++it) {
minimumWidth[*it] = header()->fontMetrics().width(header()->label(*it) + 10);
minimumWidthTotal += minimumWidth[*it];
......@@ -1056,6 +1090,9 @@ void Playlist::updateColumnWidths()
void Playlist::calculateColumnWeights()
{
if(m_disableColumnWidthUpdates)
return;
PlaylistItemList l = items();
QValueListConstIterator<int> columnIt;
......@@ -1281,12 +1318,6 @@ void Playlist::slotToggleColumnVisible(int column)
SharedSettings::instance()->toggleColumnVisible(column - columnOffset());
}
void Playlist::slotWidthDirty(int column)
{
if(m_widthDirty.find(column) == m_widthDirty.end())
m_widthDirty.append(column);
}
////////////////////////////////////////////////////////////////////////////////
// helper functions
////////////////////////////////////////////////////////////////////////////////
......@@ -1316,6 +1347,7 @@ QDataStream &operator>>(QDataStream &s, Playlist &p)
PlaylistItem *after = 0;
p.setSorting(p.columns() + 1);
p.setColumnWidthUpdatesDisabled(true);
for(QStringList::Iterator it = files.begin(); it != files.end(); ++it ) {
QFileInfo info(*it);
......@@ -1323,6 +1355,7 @@ QDataStream &operator>>(QDataStream &s, Playlist &p)
}
p.emitCountChanged();
p.setColumnWidthUpdatesDisabled(false);
return s;
}
......
......@@ -270,6 +270,8 @@ public:
*/
virtual bool readOnly() const { return false; }
void setColumnWidthUpdatesDisabled(bool disabled) { m_disableColumnWidthUpdates = disabled; }
public slots:
/**
* Remove the currently selected items from the playlist and disk.
......@@ -327,6 +329,13 @@ public slots:
*/
virtual void slotReload();
/**
* Tells the listview that the next time that it paints that the weighted
* column widths must be recalculated. If this is called without a column
* all visible columns are marked as dirty.
*/
void slotWidthDirty(int column = -1);
protected:
/**
* Remove \a items from the playlist and disk. This will ignore items that
......@@ -473,6 +482,7 @@ private:
*/
void calculateColumnWeights();
private:
/**
* This class is used internally to store settings that are shared by all
* of the playlists, such as column order. It is implemented as a singleton.
......@@ -525,12 +535,6 @@ private slots:
*/
void slotCreateGroup() { emit signalCreatePlaylist(selectedItems()); }
/**
* Tells the listview that the next time that it paints that the weighted
* column widths must be recalculated.
*/
void slotWidthDirty(int column);
private:
StringHash m_members;
......@@ -546,6 +550,7 @@ private:
bool m_polished;
QValueList<int> m_widthDirty;
bool m_disableColumnWidthUpdates;
/**
* The average minimum widths of columns to be used in balancing calculations.
*/
......@@ -631,12 +636,16 @@ ItemType *Playlist::createItem(const QFileInfo &file, const QString &absFilePath
return i;
}
else
return 0;
return 0;
}
template <class CollectionItemType, class ItemType, class SiblingType>
void Playlist::createItems(const QValueList<SiblingType *> &siblings)
{
if(siblings.isEmpty())
return;
m_disableColumnWidthUpdates = true;
ItemType *previous = 0;
QValueListConstIterator<SiblingType *> it = siblings.begin();
......@@ -648,6 +657,8 @@ void Playlist::createItems(const QValueList<SiblingType *> &siblings)
}
emit signalCountChanged(this);
m_disableColumnWidthUpdates = false;
slotWidthDirty();
}
#endif
......@@ -143,6 +143,7 @@ void PlaylistItem::slotRefresh()
{
// This signal will be received by the "parent" CollectionListItem which will
// in turn call slotRefreshImpl() for all of its children, including this item.
emit(signalRefreshed());
}
......@@ -172,6 +173,7 @@ PlaylistItem::PlaylistItem(CollectionListItem *item, Playlist *parent, QListView
// This constructor should only be used by the CollectionList subclass.
PlaylistItem::PlaylistItem(CollectionList *parent) :
QObject(parent), KListViewItem(parent),
m_collectionItem(static_cast<CollectionListItem *>(this)), m_data(0), m_playing(false)
......@@ -199,6 +201,12 @@ void PlaylistItem::paintCell(QPainter *p, const QColorGroup &cg, int column, int
QListViewItem::paintCell(p, colorGroup, column, width, align);
}
void PlaylistItem::setText(int column, const QString &text)
{
KListViewItem::setText(column, text);
emit signalColumnWidthChanged(column);
}
int PlaylistItem::compare(QListViewItem *item, int column, bool ascending) const
{
// reimplemented from QListViewItem
......@@ -348,6 +356,12 @@ void PlaylistItem::setup(CollectionListItem *item, Playlist *parent)
}
setDragEnabled(true);
// We only want this connection to take effect for changes after item
// creation -- i.e. editing the item. We'll handle item creation separately
// as that avoids this signal firing a few thousand times.
connect(this, SIGNAL(signalColumnWidthChanged(int)), parent, SLOT(slotWidthDirty(int)));
}
////////////////////////////////////////////////////////////////////////////////
......
......@@ -137,6 +137,7 @@ protected:
void setData(Data *d) { m_data = d; }
virtual void paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int align);
virtual void setText(int column, const QString &text);
virtual int compare(QListViewItem *item, int column, bool ascending) const;
int compare(const PlaylistItem *firstItem, const PlaylistItem *secondItem, int column, bool ascending) const;
......@@ -151,6 +152,7 @@ protected slots:
signals:
void signalRefreshed();
void signalColumnWidthChanged(int column);
private:
void setup(CollectionListItem *item, Playlist *parent);
......
......@@ -362,6 +362,7 @@ void PlaylistSplitter::slotAddToPlaylist(const QString &file, Playlist *list, Pl
KApplication::setOverrideCursor(Qt::waitCursor);
addImpl(file, list, after);
list->slotWidthDirty();
list->emitCountChanged();
KApplication::restoreOverrideCursor();
......@@ -387,6 +388,7 @@ void PlaylistSplitter::slotAddToPlaylist(const QStringList &files, Playlist *lis
for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
after = addImpl(*it, list, after);
list->slotWidthDirty();
list->emitCountChanged();
KApplication::restoreOverrideCursor();
......
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