diff options
author | 2008-06-13 20:36:50 -0400 | |
---|---|---|
committer | 2008-06-13 20:38:02 -0400 | |
commit | 73677d9ee7075f0a86214a15d3799ae5625d2545 (patch) | |
tree | 283c53ee9c1c902dc3fb06c86a25cfbd504f14ad | |
parent | Re-cased source files. (diff) | |
download | zmusicplayer-73677d9ee7075f0a86214a15d3799ae5625d2545.tar.xz zmusicplayer-73677d9ee7075f0a86214a15d3799ae5625d2545.zip |
Subclassed qtreeview.
-rw-r--r-- | AutoSizingList.cpp | 111 | ||||
-rw-r--r-- | AutoSizingList.h | 33 | ||||
-rw-r--r-- | CollectionFilter.cpp | 1 | ||||
-rw-r--r-- | CollectionFilter.h | 3 | ||||
-rw-r--r-- | CollectionModel.cpp | 7 | ||||
-rw-r--r-- | PlayerWindow.cpp | 88 | ||||
-rw-r--r-- | PlayerWindow.h | 7 | ||||
-rw-r--r-- | ZMusicPlayer.pro | 19 |
8 files changed, 178 insertions, 91 deletions
diff --git a/AutoSizingList.cpp b/AutoSizingList.cpp new file mode 100644 index 0000000..954ccf1 --- /dev/null +++ b/AutoSizingList.cpp @@ -0,0 +1,111 @@ +/* + * Copyright 2008 Jason A. Donenfeld <Jason@zx2c4.com> + */ + +#include "AutoSizingList.h" +#include "CollectionModel.h" +#include "CollectionFilter.h" + +#include <QHeaderView> +#include <QDebug> + +AutoSizingList::AutoSizingList(QWidget *parent) + : QTreeView(parent) + , m_childWidget(0) + , m_suggestedRatio1(0) + , m_suggestedRatio2(0) + , m_suggestedRatio3(0) +{ + setUniformRowHeights(true); + setDragEnabled(true); + setDragDropMode(QAbstractItemView::DragOnly); + setRootIsDecorated(false); + setAllColumnsShowFocus(true); + setSelectionBehavior(QAbstractItemView::SelectRows); + setSelectionMode(QAbstractItemView::ExtendedSelection); + header()->setResizeMode(QHeaderView::Fixed); +} +void AutoSizingList::rowChange() +{ + setUpdatesEnabled(false); + header()->setStretchLastSection(false); + QAbstractItemModel *abstractModel = model(); + CollectionModel *model = 0; + CollectionFilter *filter = qobject_cast<CollectionFilter*>(abstractModel); + if(filter) + model = qobject_cast<CollectionModel*>(filter->sourceModel()); + if(model) + model->setFakeBoldState(true); + double suggestedWidth0 = qMin(sizeHintForColumn(0), 70); + setColumnWidth(0, suggestedWidth0); + double suggestedWidth1 = sizeHintForColumn(1); + double suggestedWidth2 = sizeHintForColumn(2); + double suggestedWidth3 = sizeHintForColumn(3); + if(model) + model->setFakeBoldState(false); + header()->setStretchLastSection(true); + + double suggestedWidthTotal = suggestedWidth0 + suggestedWidth1 + suggestedWidth2 + suggestedWidth3; + m_suggestedRatio1 = (double)suggestedWidth1 / (double)suggestedWidthTotal; + m_suggestedRatio2 = (double)suggestedWidth2 / (double)suggestedWidthTotal; + m_suggestedRatio3 = (double)suggestedWidth3 / (double)suggestedWidthTotal; + bool b; + double diff; + do { + b = false; + if(m_suggestedRatio1 > .42) { + diff = m_suggestedRatio1 - .42; + m_suggestedRatio1 = .42; + m_suggestedRatio2 += diff / 2; + m_suggestedRatio3 += diff / 2; + b = true; + } + if(m_suggestedRatio2 > .42) { + diff = m_suggestedRatio2 - .42; + m_suggestedRatio2 = .42; + m_suggestedRatio1 += diff / 2; + m_suggestedRatio3 += diff / 2; + b = true; + } + if(m_suggestedRatio3 > .42) { + diff = m_suggestedRatio3 - .42; + m_suggestedRatio3 = .42; + m_suggestedRatio2 += diff / 2; + m_suggestedRatio1 += diff / 2; + b = true; + } + } while(b); + fixColumnSizes(); +} +void AutoSizingList::fixColumnSizes() +{ + setUpdatesEnabled(false); + if(m_suggestedRatio1 + m_suggestedRatio2 + m_suggestedRatio3) { + int width = viewport()->width() - 40; //HACK: Why not 0? + setColumnWidth(1, m_suggestedRatio1 * width); + setColumnWidth(2, m_suggestedRatio2 * width); + setColumnWidth(3, m_suggestedRatio3 * width); + } + setUpdatesEnabled(true); +} +void AutoSizingList::resizeEvent(QResizeEvent *event) +{ + QWidget::resizeEvent(event); + fixColumnSizes(); + if(m_childWidget) { + QRect rect = viewport()->geometry(); + rect.setHeight(rect.height() - (int)(m_childWidget->sizeHint().height() * .5)); + m_childWidget->setGeometry(rect); + } +} +void AutoSizingList::setChildWidget(QWidget *child) +{ + m_childWidget = child; + child->setParent(this); +} +void AutoSizingList::setModel(CollectionFilter *model) +{ + connect(model, SIGNAL(filterChanged()), this, SLOT(rowChange())); + QTreeView::setModel(model); + rowChange(); +} diff --git a/AutoSizingList.h b/AutoSizingList.h new file mode 100644 index 0000000..7da05a7 --- /dev/null +++ b/AutoSizingList.h @@ -0,0 +1,33 @@ +/* + * Copyright 2008 Jason A. Donenfeld <Jason@zx2c4.com> + */ + +#ifndef AUTOSIZINGLIST_H +#define AUTOSIZINGLIST_H + + +#include <QTreeView> + +class CollectionFilter; + +class AutoSizingList : public QTreeView +{ + Q_OBJECT +public: + AutoSizingList(QWidget *parent = 0); + void resizeEvent(QResizeEvent *event); + void setChildWidget(QWidget *child); + void setModel(CollectionFilter *model); + +private: + double m_suggestedRatio1; + double m_suggestedRatio2; + double m_suggestedRatio3; + QWidget *m_childWidget; + void fixColumnSizes(); + +private slots: + void rowChange(); +}; + +#endif //AUTOSIZINGLIST_H diff --git a/CollectionFilter.cpp b/CollectionFilter.cpp index 5d29b48..0487707 100644 --- a/CollectionFilter.cpp +++ b/CollectionFilter.cpp @@ -34,6 +34,7 @@ void CollectionFilter::setFilter(QString filter) invalidateFilter(); if(currentSelected.isValid()) m_selected = mapFromSource(currentSelected).row(); + emit filterChanged(); } } QModelIndex CollectionFilter::peekNext() const diff --git a/CollectionFilter.h b/CollectionFilter.h index 5ac6088..dba647a 100644 --- a/CollectionFilter.h +++ b/CollectionFilter.h @@ -28,6 +28,9 @@ private: QString m_filter; CollectionModel *m_parent; int m_selected; + +signals: + void filterChanged(); }; #endif //COLLECTIONFILTER_H diff --git a/CollectionModel.cpp b/CollectionModel.cpp index 12689b3..2dc7f7c 100644 --- a/CollectionModel.cpp +++ b/CollectionModel.cpp @@ -13,7 +13,12 @@ #include <QMimeData> #include <QUrl> -CollectionModel::CollectionModel(QList<Song*> songs, QObject* parent) : QAbstractItemModel(parent), m_songs(songs), m_position(-1), m_fakeBoldState(false) {} +CollectionModel::CollectionModel(QList<Song*> songs, QObject* parent) + : QAbstractItemModel(parent) + , m_songs(songs) + , m_position(-1) + , m_fakeBoldState(false) +{} QVariant CollectionModel::data(const QModelIndex &index, int role) const { diff --git a/PlayerWindow.cpp b/PlayerWindow.cpp index f244baf..be53ea9 100644 --- a/PlayerWindow.cpp +++ b/PlayerWindow.cpp @@ -8,8 +8,8 @@ #include "CollectionFilter.h" #include "SearchLineEdit.h" #include "Credentials.h" +#include "AutoSizingList.h" -#include <QTreeView> #include <QDebug> #include <QProgressBar> #include <QStatusBar> @@ -18,14 +18,12 @@ #include <QLabel> #include <QVBoxLayout> #include <QSortFilterProxyModel> -#include <QHeaderView> //BEGIN CONSTRUCT OBJECTS Credentials *PlayerWindow::s_credentials = 0; PlayerWindow::PlayerWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags) , m_model(0) - , m_treeView(0) , m_filter(0) { setupUi(); @@ -33,17 +31,8 @@ PlayerWindow::PlayerWindow(QWidget *parent, Qt::WindowFlags flags) } void PlayerWindow::setupUi() { - m_treeView = new QTreeView(this); - m_treeView->setUniformRowHeights(true); - m_treeView->setDragEnabled(true); - m_treeView->setDragDropMode(QAbstractItemView::DragOnly); - m_treeView->setRootIsDecorated(false); - m_treeView->setAllColumnsShowFocus(true); - m_treeView->setSelectionBehavior(QAbstractItemView::SelectRows); - m_treeView->setSelectionMode(QAbstractItemView::ExtendedSelection); - m_treeView->header()->setResizeMode(QHeaderView::Fixed); + m_treeView = new AutoSizingList(this); setCentralWidget(m_treeView); - m_playAction = new QAction(style()->standardIcon(QStyle::SP_MediaPlay), tr("Play"), this); m_pauseAction = new QAction(style()->standardIcon(QStyle::SP_MediaPause), tr("Pause"), this); m_stopAction = new QAction(style()->standardIcon(QStyle::SP_MediaStop), tr("Stop"), this); @@ -99,7 +88,7 @@ void PlayerWindow::setupUi() statusBar()->addWidget(m_downloadProgress, 1); statusBar()->hide(); connect(credentials(), SIGNAL(receivedList(const QList<Song*>)), this, SLOT(setupTable(const QList<Song*>))); - credentials()->setParent(m_treeView); + m_treeView->setChildWidget(credentials()); credentials()->setDownloadProgressBar(m_downloadProgress); credentials()->setStatusBar(statusBar()); credentials()->show(); @@ -128,7 +117,6 @@ void PlayerWindow::setupTable(const QList<Song*> songList) m_searchEdit->setDisabled(false); connect(m_treeView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(songDoubleClicked(const QModelIndex&))); updateControls(); - rowChange(); } Credentials* PlayerWindow::credentials() { @@ -248,72 +236,7 @@ void PlayerWindow::songFinished() } //END CHANGE SONG -//BEGIN COLUMN SIZE AND FILTERING -void PlayerWindow::rowChange() -{ - m_treeView->setUpdatesEnabled(false); - m_treeView->header()->setStretchLastSection(false); - m_model->setFakeBoldState(true); - m_treeView->resizeColumnToContents(0); - m_treeView->resizeColumnToContents(1); - m_treeView->resizeColumnToContents(2); - m_treeView->resizeColumnToContents(3); - m_model->setFakeBoldState(false); - double suggestedWidth1 = m_treeView->columnWidth(1); - double suggestedWidth2 = m_treeView->columnWidth(2); - double suggestedWidth3 = m_treeView->columnWidth(3); - m_treeView->header()->setStretchLastSection(true); - double suggestedWidthTotal = m_treeView->columnWidth(0) + suggestedWidth1 + suggestedWidth2 + suggestedWidth3; - m_suggestedRatio1 = (double)suggestedWidth1 / (double)suggestedWidthTotal; - m_suggestedRatio2 = (double)suggestedWidth2 / (double)suggestedWidthTotal; - m_suggestedRatio3 = (double)suggestedWidth3 / (double)suggestedWidthTotal; - - bool b; - double diff; - do { - b = false; - if(m_suggestedRatio1 > .42) { - diff = m_suggestedRatio1 - .42; - m_suggestedRatio1 = .42; - m_suggestedRatio2 += diff / 2; - m_suggestedRatio3 += diff / 2; - b = true; - } - if(m_suggestedRatio2 > .42) { - diff = m_suggestedRatio2 - .42; - m_suggestedRatio2 = .42; - m_suggestedRatio1 += diff / 2; - m_suggestedRatio3 += diff / 2; - b = true; - } - if(m_suggestedRatio3 > .42) { - diff = m_suggestedRatio3 - .42; - m_suggestedRatio3 = .42; - m_suggestedRatio2 += diff / 2; - m_suggestedRatio1 += diff / 2; - b = true; - } - } while(b); - - fixColumnSizes(); -} -void PlayerWindow::fixColumnSizes() -{ - m_treeView->setUpdatesEnabled(false); - int width = m_treeView->width() - 40; //HACK: Why not 0? - m_treeView->setColumnWidth(1, m_suggestedRatio1 * width); - m_treeView->setColumnWidth(2, m_suggestedRatio2 * width); - m_treeView->setColumnWidth(3, m_suggestedRatio3 * width); - m_treeView->setUpdatesEnabled(true); -} -void PlayerWindow::resizeEvent(QResizeEvent *event) -{ - fixColumnSizes(); - QRect rect = m_treeView->geometry(); - rect.setHeight(rect.height() - (int)(credentials()->sizeHint().height() * 1.5)); - credentials()->setGeometry(rect); - QWidget::resizeEvent(event); -} +//BEGIN FILTERING void PlayerWindow::searchChanged(const QString&) { m_searchTimer.stop(); @@ -324,7 +247,6 @@ void PlayerWindow::updateSearch() if(m_filter) { m_filter->setFilter(m_searchEdit->lineEdit()->text().trimmed()); updateControls(); - rowChange(); } } -//END COLUMN SIZE AND FILTERING +//END FILTERING diff --git a/PlayerWindow.h b/PlayerWindow.h index 676cac9..11f14c1 100644 --- a/PlayerWindow.h +++ b/PlayerWindow.h @@ -13,7 +13,7 @@ class Song; class QBuffer; class QProgressBar; -class QTreeView; +class AutoSizingList; class QAction; class SearchLineEdit; class Credentials; @@ -46,7 +46,7 @@ private: QProgressBar *m_downloadProgress; CollectionModel *m_model; CollectionFilter *m_filter; - QTreeView *m_treeView; + AutoSizingList *m_treeView; QAction *m_playAction; QAction *m_pauseAction; QAction *m_stopAction; @@ -68,9 +68,6 @@ private: void setupPhonon(); void updateControls(); void playCurrentTrack(); - void fixColumnSizes(); - void rowChange(); - void resizeEvent(QResizeEvent *event); }; #endif //PLAYERWINDOW_H diff --git a/ZMusicPlayer.pro b/ZMusicPlayer.pro index 88c30f2..5d1336f 100644 --- a/ZMusicPlayer.pro +++ b/ZMusicPlayer.pro @@ -2,5 +2,20 @@ TEMPLATE = app QT += network xml phonon CONFIG += qt -HEADERS += Song.h CollectionModel.h PlayerWindow.h SearchLineEdit.h Credentials.h CollectionFilter.h -SOURCES += Main.cpp CollectionModel.cpp PlayerWindow.cpp SearchLineEdit.cpp Credentials.cpp CollectionFilter.cpp +HEADERS += \ + Song.h \ + CollectionModel.h \ + PlayerWindow.h \ + SearchLineEdit.h \ + Credentials.h \ + CollectionFilter.h \ + AutoSizingList.h + +SOURCES += \ + Main.cpp \ + CollectionModel.cpp \ + PlayerWindow.cpp \ + SearchLineEdit.cpp \ + Credentials.cpp \ + CollectionFilter.cpp \ + AutoSizingList.cpp |