From 4d809330ea573d0ff1bec31eb8385c940a5ea19f Mon Sep 17 00:00:00 2001 From: "craig.p.drummond" Date: Tue, 4 Feb 2014 19:14:11 +0000 Subject: [PATCH] Create a base class for TableViews --- CMakeLists.txt | 3 +- gui/playlistspage.cpp | 100 +---------------------------- gui/playlistspage.h | 15 +---- gui/settings.cpp | 25 +++----- gui/settings.h | 6 +- models/playlistsmodel.cpp | 19 +++++- models/playqueuemodel.cpp | 21 +++++- widgets/groupedview.h | 2 +- widgets/itemview.cpp | 2 - widgets/itemview.h | 12 +--- widgets/playqueueview.cpp | 111 +------------------------------- widgets/playqueueview.h | 20 ++---- widgets/tableview.cpp | 131 ++++++++++++++++++++++++++++++++++++++ widgets/tableview.h | 54 ++++++++++++++++ 14 files changed, 244 insertions(+), 277 deletions(-) create mode 100644 widgets/tableview.cpp create mode 100644 widgets/tableview.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 86d5ac2d7..927b22e28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -260,7 +260,7 @@ set(CANTATA_SRCS gui/application.cpp gui/main.cpp gui/initialsettingswizard.cpp widgets/coverwidget.cpp widgets/volumeslider.cpp widgets/icons.cpp widgets/genrecombo.cpp widgets/menubutton.cpp widgets/toolbutton.cpp widgets/wizardpage.cpp widgets/statuslabel.cpp widgets/searchwidget.cpp widgets/messageoverlay.cpp widgets/basicitemdelegate.cpp widgets/sizegrip.cpp widgets/sizewidget.cpp widgets/servicestatuslabel.cpp widgets/spacerwidget.cpp - widgets/songdialog.cpp widgets/stretchheaderview.cpp + widgets/songdialog.cpp widgets/stretchheaderview.cpp widgets/tableview.cpp network/networkaccessmanager.cpp network/networkproxyfactory.cpp http/httpserver.cpp devices/deviceoptions.cpp @@ -279,6 +279,7 @@ set(CANTATA_MOC_HDRS widgets/treeview.h widgets/listview.h widgets/itemview.h widgets/autohidingsplitter.h widgets/timeslider.h widgets/actionlabel.h widgets/playqueueview.h widgets/groupedview.h widgets/actionitemdelegate.h widgets/coverwidget.h widgets/volumeslider.h widgets/genrecombo.h widgets/searchwidget.h widgets/messageoverlay.h widgets/servicestatuslabel.h widgets/stretchheaderview.h + widgets/tableview.h network/networkaccessmanager.h context/togglelist.h context/ultimatelyrics.h context/ultimatelyricsprovider.h context/lyricsdialog.h context/contextwidget.h context/artistview.h context/albumview.h context/songview.h context/view.h context/contextengine.h diff --git a/gui/playlistspage.cpp b/gui/playlistspage.cpp index 1f97e8418..27f7a66f2 100644 --- a/gui/playlistspage.cpp +++ b/gui/playlistspage.cpp @@ -31,112 +31,16 @@ #include "stdactions.h" #include "actioncollection.h" #include "mpdconnection.h" -#include "settings.h" -#include "stretchheaderview.h" #include #ifndef ENABLE_KDE_SUPPORT #include #endif PlaylistTableView::PlaylistTableView(QWidget *p) - : TableView(p) - , menu(0) + : TableView(QLatin1String("playlist"), p) { - setContextMenuPolicy(Qt::CustomContextMenu); - setAcceptDrops(true); - setDragDropOverwriteMode(false); - setDragDropMode(QAbstractItemView::DragDrop); - setSelectionMode(QAbstractItemView::ExtendedSelection); - setDropIndicatorShown(true); - setUniformRowHeights(true); setUseSimpleDelegate(); setIndentation(fontMetrics().width(QLatin1String("XX"))); - StretchHeaderView *hdr=new StretchHeaderView(Qt::Horizontal, this); - setHeader(hdr); - connect(hdr, SIGNAL(StretchEnabledChanged(bool)), SLOT(stretchToggled(bool))); -} - -void PlaylistTableView::initHeader() -{ - if (!model()) { - return; - } - - StretchHeaderView *hdr=qobject_cast(header()); - if (!menu) { - hdr->SetStretchEnabled(true); - stretchToggled(true); - hdr->setContextMenuPolicy(Qt::CustomContextMenu); - hdr->SetColumnWidth(PlaylistsModel::COL_TITLE, 0.4); - hdr->SetColumnWidth(PlaylistsModel::COL_ARTIST, 0.15); - hdr->SetColumnWidth(PlaylistsModel::COL_ALBUM, 0.15); - hdr->SetColumnWidth(PlaylistsModel::COL_LENGTH, 0.125); - hdr->SetColumnWidth(PlaylistsModel::COL_YEAR, 0.05); - hdr->SetColumnWidth(PlaylistsModel::COL_GENRE, 0.125); - #if QT_VERSION >= 0x050000 - hdr->setSectionsMovable(true); - #else - hdr->setMovable(true); - #endif - connect(hdr, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu())); - } - - //Restore state - QByteArray state=Settings::self()->playlistHeaderState(); - if (state.isEmpty()) { - hdr->HideSection(PlaylistsModel::COL_YEAR); - hdr->HideSection(PlaylistsModel::COL_GENRE); - } else { - hdr->RestoreState(state); - } - - if (!menu) { - menu = new QMenu(this); - QAction *stretch=new QAction(i18n("Stretch Columns To Fit Window"), this); - stretch->setCheckable(true); - stretch->setChecked(hdr->is_stretch_enabled()); - connect(stretch, SIGNAL(toggled(bool)), hdr, SLOT(SetStretchEnabled(bool))); - menu->addAction(stretch); - menu->addSeparator(); - QList hideAble=QList() << PlaylistsModel::COL_YEAR << PlaylistsModel::COL_GENRE; - foreach (int col, hideAble) { - QAction *act=new QAction(PlaylistsModel::headerText(col), menu); - act->setCheckable(true); - act->setChecked(!hdr->isSectionHidden(col)); - menu->addAction(act); - act->setData(col); - connect(act, SIGNAL(toggled(bool)), this, SLOT(toggleHeaderItem(bool))); - } - } -} - -void PlaylistTableView::saveHeader() -{ - if (menu && model()) { - Settings::self()->savePlaylistHeaderState(qobject_cast(header())->SaveState()); - } -} - -void PlaylistTableView::showMenu() -{ - menu->exec(QCursor::pos()); -} - -void PlaylistTableView::toggleHeaderItem(bool visible) -{ - QAction *act=qobject_cast(sender()); - - if (act) { - int index=act->data().toInt(); - if (-1!=index) { - qobject_cast(header())->SetSectionHidden(index, !visible); - } - } -} - -void PlaylistTableView::stretchToggled(bool e) -{ - setHorizontalScrollBarPolicy(e ? Qt::ScrollBarAlwaysOff : Qt::ScrollBarAsNeeded); } PlaylistsPage::PlaylistsPage(QWidget *p) @@ -201,7 +105,7 @@ PlaylistsPage::~PlaylistsPage() void PlaylistsPage::saveConfig() { - PlaylistTableView *tv=qobject_cast(view->view()); + TableView *tv=qobject_cast(view->view()); if (tv) { tv->saveHeader(); } diff --git a/gui/playlistspage.h b/gui/playlistspage.h index 561aa7d1d..353030638 100644 --- a/gui/playlistspage.h +++ b/gui/playlistspage.h @@ -26,29 +26,16 @@ #include "ui_playlistspage.h" #include "playlistsproxymodel.h" -#include "itemview.h" +#include "tableview.h" #include "config.h" class Action; class PlaylistTableView : public TableView { - Q_OBJECT - public: PlaylistTableView(QWidget *p); virtual ~PlaylistTableView() { } - - void initHeader(); - void saveHeader(); - -private Q_SLOTS: - void showMenu(); - void toggleHeaderItem(bool visible); - void stretchToggled(bool e); - -private: - QMenu *menu; }; class PlaylistsPage : public QWidget, public Ui::PlaylistsPage diff --git a/gui/settings.cpp b/gui/settings.cpp index caea7e299..2e75b3d4c 100644 --- a/gui/settings.cpp +++ b/gui/settings.cpp @@ -334,20 +334,12 @@ bool Settings::showFullScreen() return GET_BOOL("showFullScreen", false); } -QByteArray Settings::playQueueHeaderState() +QByteArray Settings::headerState(const QString &key) { if (version() allConnections(); bool showPlaylist(); bool showFullScreen(); - QByteArray playQueueHeaderState(); - QByteArray playlistHeaderState(); + QByteArray headerState(const QString &key); QByteArray splitterState(); bool splitterAutoHide(); QSize mainWindowSize(); @@ -226,8 +225,7 @@ public: void saveShowFullScreen(bool v); void saveStopOnExit(bool v); void saveStopDynamizerOnExit(bool v); - void savePlayQueueHeaderState(const QByteArray &v); - void savePlaylistHeaderState(const QByteArray &v); + void saveHeaderState(const QString &key, const QByteArray &v); void saveSplitterState(const QByteArray &v); void saveSplitterAutoHide(bool v); void saveMainWindowSize(const QSize &v); diff --git a/models/playlistsmodel.cpp b/models/playlistsmodel.cpp index 0889fc74e..07f28198a 100644 --- a/models/playlistsmodel.cpp +++ b/models/playlistsmodel.cpp @@ -35,6 +35,7 @@ #include "itemview.h" #include "qtplural.h" #include "groupedview.h" +#include "tableview.h" #include "localize.h" #include "utils.h" #ifdef ENABLE_KDE_SUPPORT @@ -200,9 +201,10 @@ QModelIndex PlaylistsModel::index(int row, int col, const QModelIndex &parent) c QVariant PlaylistsModel::headerData(int section, Qt::Orientation orientation, int role) const { if (Qt::Horizontal==orientation) { - if (Qt::DisplayRole==role) { + switch (role) { + case Qt::DisplayRole: return headerText(section); - } else if (Qt::TextAlignmentRole==role) { + case Qt::TextAlignmentRole: switch (section) { case COL_TITLE: case COL_ARTIST: @@ -214,6 +216,19 @@ QVariant PlaylistsModel::headerData(int section, Qt::Orientation orientation, in case COL_YEAR: return int(Qt::AlignVCenter|Qt::AlignRight); } + case TableView::Role_Hideable: + return COL_YEAR==section || COL_GENRE==section ? true : false; + case TableView::Role_Width: + switch (section) { + case COL_TITLE: return 0.4; + case COL_ARTIST: return 0.15; + case COL_ALBUM: return 0.15; + case COL_YEAR: return 0.05; + case COL_GENRE: return 0.125; + case COL_LENGTH: return 0.125; + } + default: + break; } } diff --git a/models/playqueuemodel.cpp b/models/playqueuemodel.cpp index bc87584b3..afde6c895 100644 --- a/models/playqueuemodel.cpp +++ b/models/playqueuemodel.cpp @@ -184,9 +184,10 @@ QModelIndex PlayQueueModel::parent(const QModelIndex &idx) const QVariant PlayQueueModel::headerData(int section, Qt::Orientation orientation, int role) const { if (Qt::Horizontal==orientation) { - if (Qt::DisplayRole==role) { + switch (role) { + case Qt::DisplayRole: return headerText(section); - } else if (Qt::TextAlignmentRole==role) { + case Qt::TextAlignmentRole: switch (section) { case COL_TITLE: case COL_ARTIST: @@ -201,6 +202,22 @@ QVariant PlayQueueModel::headerData(int section, Qt::Orientation orientation, in case COL_PRIO: return int(Qt::AlignVCenter|Qt::AlignRight); } + case TableView::Role_Hideable: + return COL_YEAR==section || COL_DISC==section || COL_GENRE==section || COL_PRIO==section? true : false; + case TableView::Role_Width: + switch (section) { + case COL_TRACK: return 0.075; + case COL_DISC: return 0.03; + case COL_TITLE: return 0.3; + case COL_ARTIST: return 0.27; + case COL_ALBUM: return 0.27; + case COL_LENGTH: return 0.05; + case COL_YEAR: return 0.05; + case COL_GENRE: return 0.1; + case COL_PRIO: return 0.015; + } + default: + break; } } diff --git a/widgets/groupedview.h b/widgets/groupedview.h index fd9fb15f3..d3ee7dd86 100644 --- a/widgets/groupedview.h +++ b/widgets/groupedview.h @@ -44,7 +44,7 @@ public: }; enum Roles { - Role_Key = Qt::UserRole+512, + Role_Key = Qt::UserRole+200, Role_Id, Role_Song, Role_AlbumDuration, diff --git a/widgets/itemview.cpp b/widgets/itemview.cpp index 1d7e0426b..c5a8a6af5 100644 --- a/widgets/itemview.cpp +++ b/widgets/itemview.cpp @@ -636,9 +636,7 @@ void ItemView::allowTableView(TableView *v) tableView->setParent(stackedWidget); stackedWidget->addWidget(tableView); tableView->setProperty(constPageProp, stackedWidget->count()-1); -// tableView->setItemDelegate(new TableDelegate(v)); // Some styles, eg Cleanlooks/Plastique require that we explicitly set mouse tracking on the treeview. - tableView->setAttribute(Qt::WA_MouseTracking, true); // tableView->installEventFilter(new ViewEventHandler(0, tableView)); connect(tableView, SIGNAL(itemsSelected(bool)), this, SIGNAL(itemsSelected(bool))); connect(tableView, SIGNAL(itemActivated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &))); diff --git a/widgets/itemview.h b/widgets/itemview.h index 1860954c3..60a4b108c 100644 --- a/widgets/itemview.h +++ b/widgets/itemview.h @@ -38,6 +38,7 @@ class GroupedView; class ActionItemDelegate; class MessageOverlay; class Icon; +class TableView; class ViewEventHandler : public QObject { @@ -69,15 +70,6 @@ private: QAction *act; }; -class TableView : public TreeView -{ -public: - TableView(QWidget *p) : TreeView(p, false) { } - virtual ~TableView() { } - virtual void initHeader() =0; - virtual void saveHeader() =0; -}; - class ItemView : public QWidget, public Ui::ItemView { Q_OBJECT @@ -101,7 +93,7 @@ public: enum Role { - Role_ImageSize = Qt::UserRole+256, + Role_ImageSize = Qt::UserRole+100, Role_MainText, Role_SubText, Role_TitleText, diff --git a/widgets/playqueueview.cpp b/widgets/playqueueview.cpp index 35e739f79..2d5a5502c 100644 --- a/widgets/playqueueview.cpp +++ b/widgets/playqueueview.cpp @@ -31,11 +31,7 @@ #include "localize.h" #include "spinner.h" #include "messageoverlay.h" -#include "stretchheaderview.h" #include "basicitemdelegate.h" -#include -#include -#include #include #include #include @@ -69,29 +65,14 @@ public: }; PlayQueueTreeView::PlayQueueTreeView(PlayQueueView *parent) - : TreeView(parent, true) + : TableView(QLatin1String("playQueue"), parent, true) , view(parent) - , menu(0) { - setContextMenuPolicy(Qt::CustomContextMenu); - setAcceptDrops(true); - setDragDropOverwriteMode(false); - setDragDropMode(QAbstractItemView::DragDrop); - setSelectionMode(QAbstractItemView::ExtendedSelection); setIndentation(0); setItemsExpandable(false); setExpandsOnDoubleClick(false); - setDropIndicatorShown(true); setRootIsDecorated(false); - setUniformRowHeights(true); setItemDelegate(new PlayQueueTreeViewItemDelegate(this)); - StretchHeaderView *hdr=new StretchHeaderView(Qt::Horizontal, this); - setHeader(hdr); - connect(hdr, SIGNAL(StretchEnabledChanged(bool)), SLOT(stretchToggled(bool))); -} - -PlayQueueTreeView::~PlayQueueTreeView() -{ } void PlayQueueTreeView::paintEvent(QPaintEvent *e) @@ -116,96 +97,6 @@ void PlayQueueGroupedView::paintEvent(QPaintEvent *e) GroupedView::paintEvent(e); } -void PlayQueueTreeView::initHeader() -{ - if (!model()) { - return; - } - - StretchHeaderView *hdr=qobject_cast(header()); - if (!menu) { - hdr->SetStretchEnabled(true); - stretchToggled(true); - hdr->setContextMenuPolicy(Qt::CustomContextMenu); - hdr->SetColumnWidth(PlayQueueModel::COL_TRACK, 0.075); - hdr->SetColumnWidth(PlayQueueModel::COL_DISC, 0.03); - hdr->SetColumnWidth(PlayQueueModel::COL_TITLE, 0.3); - hdr->SetColumnWidth(PlayQueueModel::COL_ARTIST, 0.27); - hdr->SetColumnWidth(PlayQueueModel::COL_ALBUM, 0.27); - hdr->SetColumnWidth(PlayQueueModel::COL_LENGTH, 0.05); - hdr->SetColumnWidth(PlayQueueModel::COL_YEAR, 0.05); - hdr->SetColumnWidth(PlayQueueModel::COL_GENRE, 0.1); - hdr->SetColumnWidth(PlayQueueModel::COL_PRIO, 0.015); - #if QT_VERSION >= 0x050000 - hdr->setSectionsMovable(true); - #else - hdr->setMovable(true); - #endif - connect(hdr, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu())); - } - - //Restore state - QByteArray state=Settings::self()->playQueueHeaderState(); - if (state.isEmpty()) { - hdr->HideSection(PlayQueueModel::COL_YEAR); - hdr->HideSection(PlayQueueModel::COL_DISC); - hdr->HideSection(PlayQueueModel::COL_GENRE); - hdr->HideSection(PlayQueueModel::COL_PRIO); - } else { - hdr->RestoreState(state); - } - - if (!menu) { - menu = new QMenu(this); - QAction *stretch=new QAction(i18n("Stretch Columns To Fit Window"), this); - stretch->setCheckable(true); - stretch->setChecked(hdr->is_stretch_enabled()); - connect(stretch, SIGNAL(toggled(bool)), hdr, SLOT(SetStretchEnabled(bool))); - menu->addAction(stretch); - menu->addSeparator(); - QList hideAble=QList() << PlayQueueModel::COL_TRACK << PlayQueueModel::COL_ALBUM << PlayQueueModel::COL_LENGTH - << PlayQueueModel::COL_DISC << PlayQueueModel::COL_YEAR << PlayQueueModel::COL_GENRE - << PlayQueueModel::COL_PRIO; - foreach (int col, hideAble) { - QAction *act=new QAction(PlayQueueModel::headerText(col), menu); - act->setCheckable(true); - act->setChecked(!hdr->isSectionHidden(col)); - menu->addAction(act); - act->setData(col); - connect(act, SIGNAL(toggled(bool)), this, SLOT(toggleHeaderItem(bool))); - } - } -} - -void PlayQueueTreeView::saveHeader() -{ - if (menu && model()) { - Settings::self()->savePlayQueueHeaderState(qobject_cast(header())->SaveState()); - } -} - -void PlayQueueTreeView::showMenu() -{ - menu->exec(QCursor::pos()); -} - -void PlayQueueTreeView::toggleHeaderItem(bool visible) -{ - QAction *act=qobject_cast(sender()); - - if (act) { - int index=act->data().toInt(); - if (-1!=index) { - qobject_cast(header())->SetSectionHidden(index, !visible); - } - } -} - -void PlayQueueTreeView::stretchToggled(bool e) -{ - setHorizontalScrollBarPolicy(e ? Qt::ScrollBarAlwaysOff : Qt::ScrollBarAsNeeded); -} - PlayQueueView::PlayQueueView(QWidget *parent) : QStackedWidget(parent) , spinner(0) diff --git a/widgets/playqueueview.h b/widgets/playqueueview.h index eefee4351..a77bf7cc2 100644 --- a/widgets/playqueueview.h +++ b/widgets/playqueueview.h @@ -30,7 +30,7 @@ #include #include #include -#include "treeview.h" +#include "tableview.h" #include "groupedview.h" #include "song.h" @@ -43,26 +43,14 @@ class Spinner; class PlayQueueView; class MessageOverlay; -class PlayQueueTreeView : public TreeView +class PlayQueueTreeView : public TableView { - Q_OBJECT - public: PlayQueueTreeView(PlayQueueView *p); - virtual ~PlayQueueTreeView(); - - void initHeader(); - void saveHeader(); - -private Q_SLOTS: - void showMenu(); - void toggleHeaderItem(bool visible); + virtual ~PlayQueueTreeView() { } void paintEvent(QPaintEvent *e); - void stretchToggled(bool e); - private: PlayQueueView *view; - QMenu *menu; }; class PlayQueueGroupedView : public GroupedView @@ -88,7 +76,7 @@ public: }; enum Role { - Role_Decoration = Qt::UserRole+768 + Role_Decoration = Qt::UserRole+300 }; PlayQueueView(QWidget *parent=0); diff --git a/widgets/tableview.cpp b/widgets/tableview.cpp new file mode 100644 index 000000000..17e25ff19 --- /dev/null +++ b/widgets/tableview.cpp @@ -0,0 +1,131 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "tableview.h" +#include "stretchheaderview.h" +#include "settings.h" +#include "localize.h" +#include +#include + +TableView::TableView(const QString &cfgName, QWidget *parent, bool menuAlwaysAllowed) + : TreeView(parent, menuAlwaysAllowed) + , menu(0) + , configName(cfgName) +{ + setContextMenuPolicy(Qt::CustomContextMenu); + setAcceptDrops(true); + setDragDropOverwriteMode(false); + setDragDropMode(QAbstractItemView::DragDrop); + setSelectionMode(QAbstractItemView::ExtendedSelection); + setDropIndicatorShown(true); + setUniformRowHeights(true); + setAttribute(Qt::WA_MouseTracking, true); + StretchHeaderView *hdr=new StretchHeaderView(Qt::Horizontal, this); + setHeader(hdr); + connect(hdr, SIGNAL(StretchEnabledChanged(bool)), SLOT(stretchToggled(bool))); +} + +void TableView::initHeader() +{ + if (!model()) { + return; + } + + StretchHeaderView *hdr=qobject_cast(header()); + QList hideable; + if (!menu) { + hdr->SetStretchEnabled(true); + stretchToggled(true); + hdr->setContextMenuPolicy(Qt::CustomContextMenu); + for (int i=0; icolumnCount(); ++i) { + hdr->SetColumnWidth(i, model()->headerData(i, Qt::Horizontal, Role_Width).toDouble()); + if (model()->headerData(i, Qt::Horizontal, Role_Hideable).toBool()) { + hideable.append(i); + } + } + #if QT_VERSION >= 0x050000 + hdr->setSectionsMovable(true); + #else + hdr->setMovable(true); + #endif + connect(hdr, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu())); + } + + //Restore state + QByteArray state=Settings::self()->headerState(configName); + if (state.isEmpty()) { + foreach (int i, hideable) { + hdr->HideSection(i); + } + } else { + hdr->RestoreState(state); + } + + if (!menu) { + menu = new QMenu(this); + QAction *stretch=new QAction(i18n("Stretch Columns To Fit Window"), this); + stretch->setCheckable(true); + stretch->setChecked(hdr->is_stretch_enabled()); + connect(stretch, SIGNAL(toggled(bool)), hdr, SLOT(SetStretchEnabled(bool))); + menu->addAction(stretch); + menu->addSeparator(); + foreach (int col, hideable) { + QAction *act=new QAction(model()->headerData(col, Qt::Horizontal, Qt::DisplayRole).toString(), menu); + act->setCheckable(true); + act->setChecked(!hdr->isSectionHidden(col)); + menu->addAction(act); + act->setData(col); + connect(act, SIGNAL(toggled(bool)), this, SLOT(toggleHeaderItem(bool))); + } + } +} + +void TableView::saveHeader() +{ + if (menu && model()) { + Settings::self()->saveHeaderState(configName, qobject_cast(header())->SaveState()); + } +} + +void TableView::showMenu() +{ + menu->exec(QCursor::pos()); +} + +void TableView::toggleHeaderItem(bool visible) +{ + QAction *act=qobject_cast(sender()); + + if (act) { + int index=act->data().toInt(); + if (-1!=index) { + qobject_cast(header())->SetSectionHidden(index, !visible); + } + } +} + +void TableView::stretchToggled(bool e) +{ + setHorizontalScrollBarPolicy(e ? Qt::ScrollBarAlwaysOff : Qt::ScrollBarAsNeeded); +} diff --git a/widgets/tableview.h b/widgets/tableview.h new file mode 100644 index 000000000..9c338c080 --- /dev/null +++ b/widgets/tableview.h @@ -0,0 +1,54 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef TABLEVIEW_H +#define TABLEVIEW_H + +#include "treeview.h" + +class TableView : public TreeView +{ + Q_OBJECT + +public: + enum Roles { + Role_Width = Qt::UserRole+400, + Role_Hideable + }; + + TableView(const QString &cfgName, QWidget *parent=0, bool menuAlwaysAllowed=false); + virtual ~TableView() { } + void initHeader(); + void saveHeader(); + +private Q_SLOTS: + void showMenu(); + void toggleHeaderItem(bool visible); + void stretchToggled(bool e); + +protected: + QMenu *menu; + QString configName; +}; + +#endif