Right click on playlist column headers to hide and show them

This commit is contained in:
David Sansome 2009-12-25 16:12:07 +00:00
parent 911aed411b
commit 18b7cc2870
4 changed files with 103 additions and 2 deletions

62
src/playlistheader.cpp Normal file
View File

@ -0,0 +1,62 @@
#include "playlistheader.h"
#include <QtDebug>
#include <QContextMenuEvent>
#include <QMenu>
#include <QSignalMapper>
PlaylistHeader::PlaylistHeader(Qt::Orientation orientation, QWidget* parent)
: QHeaderView(orientation, parent),
menu_(new QMenu(this)),
show_menu_(new QMenu(this)),
show_mapper_(new QSignalMapper(this))
{
hide_action_ = menu_->addAction("Hide...", this, SLOT(HideCurrent()));
QAction* show_action = menu_->addAction("Show section");
show_action->setMenu(show_menu_);
connect(show_mapper_, SIGNAL(mapped(int)), SLOT(ToggleVisible(int)));
}
void PlaylistHeader::contextMenuEvent(QContextMenuEvent* e) {
menu_section_ = logicalIndexAt(e->pos());
if (menu_section_ == -1)
hide_action_->setVisible(false);
else {
hide_action_->setVisible(true);
QString title(model()->headerData(menu_section_, Qt::Horizontal).toString());
hide_action_->setText("Hide " + title);
}
show_menu_->clear();
for (int i=0 ; i<model()->columnCount() ; ++i) {
AddColumnAction(i);
}
menu_->popup(e->globalPos());
}
void PlaylistHeader::AddColumnAction(int index) {
QString title(model()->headerData(index, Qt::Horizontal).toString());
QAction* action = show_menu_->addAction(title, show_mapper_, SLOT(map()));
action->setCheckable(true);
action->setChecked(!isSectionHidden(index));
show_mapper_->setMapping(action, index);
}
void PlaylistHeader::HideCurrent() {
if (menu_section_ == -1)
return;
setSectionHidden(menu_section_, true);
}
void PlaylistHeader::ToggleVisible(int section) {
setSectionHidden(section, !isSectionHidden(section));
}

34
src/playlistheader.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef PLAYLISTHEADER_H
#define PLAYLISTHEADER_H
#include <QHeaderView>
class QMenu;
class QSignalMapper;
class PlaylistHeader : public QHeaderView {
Q_OBJECT
public:
PlaylistHeader(Qt::Orientation orientation, QWidget* parent = 0);
// QWidget
void contextMenuEvent(QContextMenuEvent* e);
private slots:
void HideCurrent();
void ToggleVisible(int section);
private:
void AddColumnAction(int index);
private:
int menu_section_;
QMenu* menu_;
QAction* hide_action_;
QMenu* show_menu_;
QSignalMapper* show_mapper_;
};
#endif // PLAYLISTHEADER_H

View File

@ -1,5 +1,6 @@
#include "playlistview.h"
#include "playlist.h"
#include "playlistheader.h"
#include <QPainter>
#include <QHeaderView>
@ -101,6 +102,8 @@ PlaylistView::PlaylistView(QWidget *parent)
{
setItemDelegate(new PlaylistDelegateBase(this));
setItemDelegateForColumn(Playlist::Column_Length, new LengthItemDelegate(this));
setHeader(new PlaylistHeader(Qt::Horizontal, this));
header()->setMovable(true);
connect(header(), SIGNAL(sectionResized(int,int,int)), SLOT(SaveGeometry()));

View File

@ -32,7 +32,8 @@ SOURCES += main.cpp \
systemtrayicon.cpp \
libraryquery.cpp \
fileview.cpp \
fileviewlist.cpp
fileviewlist.cpp \
playlistheader.cpp
HEADERS += mainwindow.h \
player.h \
library.h \
@ -61,7 +62,8 @@ HEADERS += mainwindow.h \
systemtrayicon.h \
libraryquery.h \
fileview.h \
fileviewlist.h
fileviewlist.h \
playlistheader.h
FORMS += mainwindow.ui \
libraryconfig.ui \
fileview.ui