From efce2498ca74e37171ce7f5212c249d520680b25 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sat, 12 Nov 2011 16:23:41 +0000 Subject: [PATCH] Indicate which column alignment action is currently set on each column in the menu. --- src/playlist/playlistheader.cpp | 45 +++++++++++++++++++------------ src/playlist/playlistheader.h | 11 +++++--- src/playlist/playlistview.cpp | 6 ++++- src/playlist/playlistview.h | 1 + src/translations/translations.pot | 14 +++++----- 5 files changed, 48 insertions(+), 29 deletions(-) diff --git a/src/playlist/playlistheader.cpp b/src/playlist/playlistheader.cpp index 99d362eaf..b25aa542c 100644 --- a/src/playlist/playlistheader.cpp +++ b/src/playlist/playlistheader.cpp @@ -23,8 +23,10 @@ #include #include -PlaylistHeader::PlaylistHeader(Qt::Orientation orientation, QWidget* parent) +PlaylistHeader::PlaylistHeader(Qt::Orientation orientation, PlaylistView* view, + QWidget* parent) : StretchHeaderView(orientation, parent), + view_(view), menu_(new QMenu(this)), show_mapper_(new QSignalMapper(this)) { @@ -32,9 +34,18 @@ PlaylistHeader::PlaylistHeader(Qt::Orientation orientation, QWidget* parent) stretch_action_ = menu_->addAction(tr("&Stretch columns to fit window"), this, SLOT(ToggleStretchEnabled())); menu_->addSeparator(); - QMenu* align_menu = new QMenu(tr("&Align text"), this); align_left_action_ = align_menu->addAction(tr("&Left"), this, SLOT(AlignCurrentLeft())); - align_center_action_ = align_menu->addAction(tr("&Center"), this, SLOT(AlignCurrentCenter())); - align_right_action_ = align_menu->addAction(tr("&Right"), this, SLOT(AlignCurrentRight())); + QMenu* align_menu = new QMenu(tr("&Align text"), this); + QActionGroup* align_group = new QActionGroup(this); + align_left_action_ = new QAction(tr("&Left"), align_group); + align_center_action_ = new QAction(tr("&Center"), align_group); + align_right_action_ = new QAction(tr("&Right"), align_group); + + align_left_action_->setCheckable(true); + align_center_action_->setCheckable(true); + align_right_action_->setCheckable(true); + align_menu->addActions(align_group->actions()); + + connect(align_group, SIGNAL(triggered(QAction*)), SLOT(SetColumnAlignment(QAction*))); menu_->addMenu(align_menu); menu_->addSeparator(); @@ -49,14 +60,19 @@ PlaylistHeader::PlaylistHeader(Qt::Orientation orientation, QWidget* parent) void PlaylistHeader::contextMenuEvent(QContextMenuEvent* e) { menu_section_ = logicalIndexAt(e->pos()); - if (menu_section_ == -1 || ( - menu_section_ == logicalIndex(0) && logicalIndex(1) == -1)) + if (menu_section_ == -1 || + (menu_section_ == logicalIndex(0) && logicalIndex(1) == -1)) hide_action_->setVisible(false); else { hide_action_->setVisible(true); QString title(model()->headerData(menu_section_, Qt::Horizontal).toString()); hide_action_->setText(tr("&Hide %1").arg(title)); + + Qt::Alignment alignment = view_->column_alignment(menu_section_); + if (alignment & Qt::AlignLeft) align_left_action_->setChecked(true); + else if (alignment & Qt::AlignHCenter) align_center_action_->setChecked(true); + else if (alignment & Qt::AlignRight) align_right_action_->setChecked(true); } qDeleteAll(show_actions_); @@ -86,19 +102,14 @@ void PlaylistHeader::HideCurrent() { SetSectionHidden(menu_section_, true); } -void PlaylistHeader::AlignCurrentLeft() { - static_cast(parent())->SetColumnAlignment( - menu_section_, Qt::AlignLeft | Qt::AlignVCenter); -} +void PlaylistHeader::SetColumnAlignment(QAction* action) { + Qt::Alignment alignment = Qt::AlignVCenter; -void PlaylistHeader::AlignCurrentCenter() { - static_cast(parent())->SetColumnAlignment( - menu_section_, Qt::AlignHCenter | Qt::AlignVCenter); -} + if (action == align_left_action_) alignment |= Qt::AlignLeft; + if (action == align_center_action_) alignment |= Qt::AlignHCenter; + if (action == align_right_action_) alignment |= Qt::AlignRight; -void PlaylistHeader::AlignCurrentRight() { - static_cast(parent())->SetColumnAlignment( - menu_section_, Qt::AlignRight | Qt::AlignVCenter); + view_->SetColumnAlignment(menu_section_, alignment); } void PlaylistHeader::ToggleVisible(int section) { diff --git a/src/playlist/playlistheader.h b/src/playlist/playlistheader.h index 3524e59c0..0280b1724 100644 --- a/src/playlist/playlistheader.h +++ b/src/playlist/playlistheader.h @@ -20,6 +20,8 @@ #include "widgets/stretchheaderview.h" +class PlaylistView; + class QMenu; class QSignalMapper; @@ -27,7 +29,8 @@ class PlaylistHeader : public StretchHeaderView { Q_OBJECT public: - PlaylistHeader(Qt::Orientation orientation, QWidget* parent = 0); + PlaylistHeader(Qt::Orientation orientation, PlaylistView* view, + QWidget* parent = 0); // QWidget void contextMenuEvent(QContextMenuEvent* e); @@ -39,15 +42,15 @@ class PlaylistHeader : public StretchHeaderView { private slots: void HideCurrent(); - void AlignCurrentLeft(); - void AlignCurrentCenter(); - void AlignCurrentRight(); void ToggleVisible(int section); + void SetColumnAlignment(QAction* action); private: void AddColumnAction(int index); private: + PlaylistView* view_; + int menu_section_; QMenu* menu_; QAction* hide_action_; diff --git a/src/playlist/playlistview.cpp b/src/playlist/playlistview.cpp index f9ebf52e7..d9d93d32e 100644 --- a/src/playlist/playlistview.cpp +++ b/src/playlist/playlistview.cpp @@ -85,7 +85,7 @@ PlaylistView::PlaylistView(QWidget *parent) : QTreeView(parent), style_(new PlaylistProxyStyle(style())), playlist_(NULL), - header_(new PlaylistHeader(Qt::Horizontal, this)), + header_(new PlaylistHeader(Qt::Horizontal, this, this)), setting_initial_header_layout_(false), upgrading_from_qheaderview_(false), read_only_settings_(true), @@ -985,3 +985,7 @@ void PlaylistView::SetColumnAlignment(int section, Qt::Alignment alignment) { emit ColumnAlignmentChanged(column_alignment_); SaveSettings(); } + +Qt::Alignment PlaylistView::column_alignment(int section) const { + return column_alignment_.value(section, Qt::AlignLeft | Qt::AlignVCenter); +} diff --git a/src/playlist/playlistview.h b/src/playlist/playlistview.h index 5f16b6faa..b65456ca1 100644 --- a/src/playlist/playlistview.h +++ b/src/playlist/playlistview.h @@ -75,6 +75,7 @@ class PlaylistView : public QTreeView { Playlist* playlist() const { return playlist_; } bool background_enabled() const { return background_enabled_; } + Qt::Alignment column_alignment(int section) const; // QTreeView void drawTree(QPainter* painter, const QRegion& region) const; diff --git a/src/translations/translations.pot b/src/translations/translations.pot index 02ccf50a2..94919fa92 100644 --- a/src/translations/translations.pot +++ b/src/translations/translations.pot @@ -125,11 +125,11 @@ msgstr "" msgid "%n remaining" msgstr "" -#: playlist/playlistheader.cpp:35 +#: playlist/playlistheader.cpp:37 msgid "&Align text" msgstr "" -#: playlist/playlistheader.cpp:36 +#: playlist/playlistheader.cpp:40 msgid "&Center" msgstr "" @@ -145,16 +145,16 @@ msgstr "" msgid "&Help" msgstr "" -#: playlist/playlistheader.cpp:59 +#: playlist/playlistheader.cpp:70 #, qt-format msgid "&Hide %1" msgstr "" -#: playlist/playlistheader.cpp:31 +#: playlist/playlistheader.cpp:33 msgid "&Hide..." msgstr "" -#: playlist/playlistheader.cpp:35 +#: playlist/playlistheader.cpp:39 msgid "&Left" msgstr "" @@ -178,7 +178,7 @@ msgstr "" msgid "&Repeat mode" msgstr "" -#: playlist/playlistheader.cpp:37 +#: playlist/playlistheader.cpp:41 msgid "&Right" msgstr "" @@ -186,7 +186,7 @@ msgstr "" msgid "&Shuffle mode" msgstr "" -#: playlist/playlistheader.cpp:32 +#: playlist/playlistheader.cpp:34 msgid "&Stretch columns to fit window" msgstr ""