From 9343fc6c77da8678df98ceb6330c61f43aa48737 Mon Sep 17 00:00:00 2001 From: Arno Date: Sun, 5 Mar 2017 05:51:22 +0100 Subject: Implement expand and collapse for DB view Sounds easy, right? It is, if you don't try to create a QIcon from a QChar. That took me a while... First, it's not a good idea to fill the QPixmap for the QIcon with transparency. That gives you a random background. Fill it with palette color instead. Then there's QFont's pixelSize(). I have absolutely no idea how it corresponds to the pixmap's size, but roughly double the width of the pixmap is a good guess... --- BeetPlayer.pro | 6 ++++-- helper.cpp | 26 ++++++++++++++++++++++++++ helper.h | 14 ++++++++++++++ playerwidget.cpp | 32 +++++++++++++++++++++++++++++--- playerwidget.h | 2 ++ 5 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 helper.cpp create mode 100644 helper.h diff --git a/BeetPlayer.pro b/BeetPlayer.pro index 4a208a8..a2aa13f 100644 --- a/BeetPlayer.pro +++ b/BeetPlayer.pro @@ -30,7 +30,8 @@ SOURCES += main.cpp\ globals.cpp \ playerwidget.cpp \ beetview.cpp \ - indexerdialog.cpp + indexerdialog.cpp \ + helper.cpp HEADERS += beetplayer.h \ configurationdialog.h \ @@ -38,7 +39,8 @@ HEADERS += beetplayer.h \ globals.h \ playerwidget.h \ beetview.h \ - indexerdialog.h + indexerdialog.h \ + helper.h LIBS += -ltag diff --git a/helper.cpp b/helper.cpp new file mode 100644 index 0000000..91e4d7f --- /dev/null +++ b/helper.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +#include "helper.h" + +namespace Helper { + QIcon iconFromQChar(const QChar &c, int pixelSize){ + QPixmap pm(QSize(64,64)); + QPainter p(&pm); + QFont f = p.font(); + f.setPixelSize(pixelSize); + p.setFont(f); + p.fillRect(pm.rect(), qApp->palette().color(QPalette::Window)); + p.setBrush(QBrush(Qt::black)); + p.drawText(pm.rect(), Qt::AlignCenter, c); + return QIcon(pm); + } + + QAction* createSeparator(QObject *parent){ + QAction *a = new QAction(parent); + a->setSeparator(true); + return a; + } +} diff --git a/helper.h b/helper.h new file mode 100644 index 0000000..9b4a5c9 --- /dev/null +++ b/helper.h @@ -0,0 +1,14 @@ +#ifndef HELPER_H +#define HELPER_H + +#include +#include + +class QAction; + +namespace Helper { + QIcon iconFromQChar(const QChar &c, int pixelSize); + QAction* createSeparator(QObject *parent); +} + +#endif // HELPER_H diff --git a/playerwidget.cpp b/playerwidget.cpp index 1bd8ac6..deddfe6 100644 --- a/playerwidget.cpp +++ b/playerwidget.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -25,6 +26,7 @@ #include "beetview.h" #include "indexerdialog.h" #include "globals.h" +#include "helper.h" PlayerWidget::PlayerWidget(QWidget *parent) : QWidget(parent){ setupGui(); @@ -203,6 +205,10 @@ void PlayerWidget::createActions(){ connect(addToPlayListA, SIGNAL(triggered()), this, SLOT(addToPlayList())); QAction *addToPlayListAndClearA = new QAction(QIcon(":/belly_right_and_clear.png"), tr("Clear and add"), this); connect(addToPlayListAndClearA, SIGNAL(triggered()), this, SLOT(addToPlayListAndClear())); + QAction *expandA = new QAction(Helper::iconFromQChar(QChar(0x2640), 90), tr("Expand"), this); + connect(expandA, SIGNAL(triggered()), this, SLOT(expand())); + QAction *collapseAllA = new QAction(Helper::iconFromQChar(QChar(0x2642), 120), tr("Collapse all"), this); + connect(collapseAllA, SIGNAL(triggered()), mView, SLOT(collapseAll())); QAction *removeFromPlayListA = new QAction(QIcon(":/belly_left.png"), tr("Remove from playlist"), this); connect(removeFromPlayListA, SIGNAL(triggered()), this, SLOT(removeFromPlayList())); QAction *clearPlayListA = new QAction(QIcon(":/delete.png"), tr("Clear Playlist"), this); @@ -219,9 +225,10 @@ void PlayerWidget::createActions(){ QAction *configA = Globals::instance()->action(Globals::ConfigAction); mView->addAction(addToPlayListA); mView->addAction(addToPlayListAndClearA); - QAction *mViewAS1 = new QAction(this); - mViewAS1->setSeparator(true); - mView->addAction(mViewAS1); + mView->addAction(Helper::createSeparator(this)); + mView->addAction(expandA); + mView->addAction(collapseAllA); + mView->addAction(Helper::createSeparator(this)); mView->addAction(randomPlayA); mPlayListView->addAction(removeFromPlayListA); mPlayListView->addAction(shufflePlayistA); @@ -720,3 +727,22 @@ void PlayerWidget::continuePlaying(QMediaPlayer::State state){ next(); } } + +void PlayerWidget::expand(){ + QModelIndexList sel = mView->selectionModel()->selectedRows(); + foreach(QModelIndex i, sel){ + mView->expand(i); + expandRecursive(i); + } +} + +void PlayerWidget::expandRecursive(const QModelIndex &idx){ + const QStandardItemModel *model = static_cast(idx.model()); + QStandardItem *item = model->itemFromIndex(idx); + for(int i = 0; i < item->rowCount(); ++i){ + QModelIndex cur = model->indexFromItem(item->child(i, 0)); + if(cur.isValid()){ + mView->expand(cur); + } + } +} diff --git a/playerwidget.h b/playerwidget.h index e2ac24d..5f9210c 100644 --- a/playerwidget.h +++ b/playerwidget.h @@ -47,6 +47,7 @@ class PlayerWidget : public QWidget { void setDuration(qint64 dur); void slide(int value); void continuePlaying(QMediaPlayer::State state); + void expand(); private: void setupGui(); @@ -59,6 +60,7 @@ class PlayerWidget : public QWidget { void addSong(const QModelIndex &idx); void play(const QString &fullPath); void advance(int numSongs); + void expandRecursive(const QModelIndex &idx); QLineEdit *mSearch; QMediaPlayer *mPlayer; BeetView *mView; -- cgit v1.2.3-70-g09d2