mirror of
https://github.com/clementine-player/Clementine
synced 2025-01-27 09:41:32 +01:00
Add an option to make columns in the playlist stretch to fit the window width like in Amarok 1. This option is enabled by default. Fixes issue #23
This commit is contained in:
parent
d30a9c379c
commit
88a48a6bdf
@ -160,6 +160,7 @@ set(SOURCES
|
||||
widgets/sliderwidget.cpp
|
||||
widgets/spinbox.cpp
|
||||
widgets/stickyslider.cpp
|
||||
widgets/stretchheaderview.cpp
|
||||
widgets/trackslider.cpp
|
||||
widgets/tracksliderslider.cpp
|
||||
widgets/lineedit.cpp
|
||||
@ -282,6 +283,7 @@ set(HEADERS
|
||||
widgets/sliderwidget.h
|
||||
widgets/spinbox.h
|
||||
widgets/stickyslider.h
|
||||
widgets/stretchheaderview.h
|
||||
widgets/trackslider.h
|
||||
widgets/lineedit.h
|
||||
)
|
||||
|
@ -22,14 +22,19 @@
|
||||
#include <QSignalMapper>
|
||||
|
||||
PlaylistHeader::PlaylistHeader(Qt::Orientation orientation, QWidget* parent)
|
||||
: QHeaderView(orientation, parent),
|
||||
: StretchHeaderView(orientation, parent),
|
||||
menu_(new QMenu(this)),
|
||||
show_mapper_(new QSignalMapper(this))
|
||||
{
|
||||
hide_action_ = menu_->addAction(tr("Hide..."), this, SLOT(HideCurrent()));
|
||||
stretch_action_ = menu_->addAction(tr("Stretch columns to fit window"), this, SLOT(ToggleStretchEnabled()));
|
||||
menu_->addSeparator();
|
||||
|
||||
stretch_action_->setCheckable(true);
|
||||
stretch_action_->setChecked(is_stretch_enabled());
|
||||
|
||||
connect(show_mapper_, SIGNAL(mapped(int)), SLOT(ToggleVisible(int)));
|
||||
connect(this, SIGNAL(StretchEnabledChanged(bool)), stretch_action_, SLOT(setChecked(bool)));
|
||||
}
|
||||
|
||||
void PlaylistHeader::contextMenuEvent(QContextMenuEvent* e) {
|
||||
@ -69,15 +74,10 @@ void PlaylistHeader::HideCurrent() {
|
||||
if (menu_section_ == -1)
|
||||
return;
|
||||
|
||||
setSectionHidden(menu_section_, true);
|
||||
SetSectionHidden(menu_section_, true);
|
||||
}
|
||||
|
||||
void PlaylistHeader::ToggleVisible(int section) {
|
||||
setSectionHidden(section, !isSectionHidden(section));
|
||||
SetSectionHidden(section, !isSectionHidden(section));
|
||||
emit SectionVisibilityChanged(section, !isSectionHidden(section));
|
||||
|
||||
static const int kMinSectionSize = 80;
|
||||
|
||||
if (!isSectionHidden(section) && sectionSize(section) < kMinSectionSize)
|
||||
resizeSection(section, kMinSectionSize);
|
||||
}
|
||||
|
@ -17,12 +17,12 @@
|
||||
#ifndef PLAYLISTHEADER_H
|
||||
#define PLAYLISTHEADER_H
|
||||
|
||||
#include <QHeaderView>
|
||||
#include "widgets/stretchheaderview.h"
|
||||
|
||||
class QMenu;
|
||||
class QSignalMapper;
|
||||
|
||||
class PlaylistHeader : public QHeaderView {
|
||||
class PlaylistHeader : public StretchHeaderView {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
@ -45,6 +45,7 @@ class PlaylistHeader : public QHeaderView {
|
||||
int menu_section_;
|
||||
QMenu* menu_;
|
||||
QAction* hide_action_;
|
||||
QAction* stretch_action_;
|
||||
QList<QAction*> show_actions_;
|
||||
|
||||
QSignalMapper* show_mapper_;
|
||||
|
@ -41,6 +41,7 @@ const int PlaylistView::kDropIndicatorGradientWidth = 5;
|
||||
PlaylistView::PlaylistView(QWidget *parent)
|
||||
: QTreeView(parent),
|
||||
playlist_(NULL),
|
||||
header_(new PlaylistHeader(Qt::Horizontal, this)),
|
||||
glow_enabled_(true),
|
||||
currently_glowing_(false),
|
||||
glow_intensity_step_(0),
|
||||
@ -53,16 +54,17 @@ PlaylistView::PlaylistView(QWidget *parent)
|
||||
cached_current_row_row_(-1),
|
||||
drop_indicator_row_(-1)
|
||||
{
|
||||
PlaylistHeader* header = new PlaylistHeader(Qt::Horizontal, this);
|
||||
setHeader(header);
|
||||
header->setMovable(true);
|
||||
setHeader(header_);
|
||||
header_->setMovable(true);
|
||||
|
||||
connect(header, SIGNAL(sectionResized(int,int,int)), SLOT(SaveGeometry()));
|
||||
connect(header, SIGNAL(sectionMoved(int,int,int)), SLOT(SaveGeometry()));
|
||||
connect(header, SIGNAL(SectionVisibilityChanged(int,bool)), SLOT(SaveGeometry()));
|
||||
connect(header, SIGNAL(sectionResized(int,int,int)), SLOT(InvalidateCachedCurrentPixmap()));
|
||||
connect(header, SIGNAL(sectionMoved(int,int,int)), SLOT(InvalidateCachedCurrentPixmap()));
|
||||
connect(header, SIGNAL(SectionVisibilityChanged(int,bool)), SLOT(InvalidateCachedCurrentPixmap()));
|
||||
connect(header_, SIGNAL(sectionResized(int,int,int)), SLOT(SaveGeometry()));
|
||||
connect(header_, SIGNAL(sectionMoved(int,int,int)), SLOT(SaveGeometry()));
|
||||
connect(header_, SIGNAL(SectionVisibilityChanged(int,bool)), SLOT(SaveGeometry()));
|
||||
connect(header_, SIGNAL(sectionResized(int,int,int)), SLOT(InvalidateCachedCurrentPixmap()));
|
||||
connect(header_, SIGNAL(sectionMoved(int,int,int)), SLOT(InvalidateCachedCurrentPixmap()));
|
||||
connect(header_, SIGNAL(SectionVisibilityChanged(int,bool)), SLOT(InvalidateCachedCurrentPixmap()));
|
||||
connect(header_, SIGNAL(StretchEnabledChanged(bool)), SLOT(SaveSettings()));
|
||||
connect(header_, SIGNAL(StretchEnabledChanged(bool)), SLOT(StretchChanged(bool)));
|
||||
|
||||
inhibit_autoscroll_timer_->setInterval(kAutoscrollGraceTimeout * 1000);
|
||||
inhibit_autoscroll_timer_->setSingleShot(true);
|
||||
@ -123,34 +125,28 @@ void PlaylistView::LoadGeometry() {
|
||||
QSettings settings;
|
||||
settings.beginGroup(kSettingsGroup);
|
||||
|
||||
if (!header()->restoreState(settings.value("state").toByteArray())) {
|
||||
header()->hideSection(Playlist::Column_Disc);
|
||||
header()->hideSection(Playlist::Column_Year);
|
||||
header()->hideSection(Playlist::Column_Genre);
|
||||
header()->hideSection(Playlist::Column_BPM);
|
||||
header()->hideSection(Playlist::Column_Bitrate);
|
||||
header()->hideSection(Playlist::Column_Samplerate);
|
||||
header()->hideSection(Playlist::Column_Filename);
|
||||
header()->hideSection(Playlist::Column_BaseFilename);
|
||||
header()->hideSection(Playlist::Column_Filesize);
|
||||
header()->hideSection(Playlist::Column_Filetype);
|
||||
header()->hideSection(Playlist::Column_DateCreated);
|
||||
header()->hideSection(Playlist::Column_DateModified);
|
||||
header()->hideSection(Playlist::Column_AlbumArtist);
|
||||
header()->hideSection(Playlist::Column_Composer);
|
||||
}
|
||||
|
||||
// Work around weirdness in QHeaderView
|
||||
for (int i=0 ; i<header()->count() ; ++i) {
|
||||
if (header()->sectionSize(i) == 0)
|
||||
header()->hideSection(i);
|
||||
if (!header_->restoreState(settings.value("state").toByteArray())) {
|
||||
header_->HideSection(Playlist::Column_Disc);
|
||||
header_->HideSection(Playlist::Column_Year);
|
||||
header_->HideSection(Playlist::Column_Genre);
|
||||
header_->HideSection(Playlist::Column_BPM);
|
||||
header_->HideSection(Playlist::Column_Bitrate);
|
||||
header_->HideSection(Playlist::Column_Samplerate);
|
||||
header_->HideSection(Playlist::Column_Filename);
|
||||
header_->HideSection(Playlist::Column_BaseFilename);
|
||||
header_->HideSection(Playlist::Column_Filesize);
|
||||
header_->HideSection(Playlist::Column_Filetype);
|
||||
header_->HideSection(Playlist::Column_DateCreated);
|
||||
header_->HideSection(Playlist::Column_DateModified);
|
||||
header_->HideSection(Playlist::Column_AlbumArtist);
|
||||
header_->HideSection(Playlist::Column_Composer);
|
||||
}
|
||||
}
|
||||
|
||||
void PlaylistView::SaveGeometry() {
|
||||
QSettings settings;
|
||||
settings.beginGroup(kSettingsGroup);
|
||||
settings.setValue("state", header()->saveState());
|
||||
settings.setValue("state", header_->saveState());
|
||||
}
|
||||
|
||||
void PlaylistView::ReloadBarPixmaps() {
|
||||
@ -563,9 +559,21 @@ void PlaylistView::ReloadSettings() {
|
||||
QSettings s;
|
||||
s.beginGroup(kSettingsGroup);
|
||||
glow_enabled_ = s.value("glow_effect", true).toBool();
|
||||
header_->SetStretchEnabled(s.value("stretch", true).toBool());
|
||||
|
||||
if (currently_glowing_ && glow_enabled_ && isVisible())
|
||||
StartGlowing();
|
||||
if (!glow_enabled_)
|
||||
StopGlowing();
|
||||
}
|
||||
|
||||
void PlaylistView::SaveSettings() {
|
||||
QSettings s;
|
||||
s.beginGroup(kSettingsGroup);
|
||||
s.setValue("glow_effect", glow_enabled_);
|
||||
s.setValue("stretch", header_->is_stretch_enabled());
|
||||
}
|
||||
|
||||
void PlaylistView::StretchChanged(bool stretch) {
|
||||
setHorizontalScrollBarPolicy(stretch ? Qt::ScrollBarAlwaysOff : Qt::ScrollBarAsNeeded);
|
||||
}
|
||||
|
@ -22,8 +22,9 @@
|
||||
#include <QTreeView>
|
||||
#include <QBasicTimer>
|
||||
|
||||
class RadioLoadingIndicator;
|
||||
class LibraryBackend;
|
||||
class PlaylistHeader;
|
||||
class RadioLoadingIndicator;
|
||||
|
||||
class PlaylistView : public QTreeView {
|
||||
Q_OBJECT
|
||||
@ -77,6 +78,9 @@ class PlaylistView : public QTreeView {
|
||||
void InvalidateCachedCurrentPixmap();
|
||||
void PlaylistDestroyed();
|
||||
|
||||
void SaveSettings();
|
||||
void StretchChanged(bool stretch);
|
||||
|
||||
private:
|
||||
void ReloadBarPixmaps();
|
||||
QList<QPixmap> LoadBarPixmap(const QString& filename);
|
||||
@ -94,6 +98,7 @@ class PlaylistView : public QTreeView {
|
||||
QModelIndex PrevEditableIndex(const QModelIndex& current);
|
||||
|
||||
Playlist* playlist_;
|
||||
PlaylistHeader* header_;
|
||||
|
||||
bool glow_enabled_;
|
||||
bool currently_glowing_;
|
||||
|
@ -1587,6 +1587,9 @@ msgstr "المجرى"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1587,6 +1587,9 @@ msgstr ""
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1608,6 +1608,9 @@ msgstr "Flux de dades"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1591,6 +1591,9 @@ msgstr "Proud"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1594,6 +1594,9 @@ msgstr "Stream"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1608,6 +1608,9 @@ msgstr "Stream"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Streamingmitgliedschaft"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "%1 erfolgreich geschrieben"
|
||||
|
@ -1609,6 +1609,9 @@ msgstr "Stream"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Συνδρομή ροής"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "Επιτυχία εγγραφής του %1"
|
||||
|
@ -1592,6 +1592,9 @@ msgstr "Stream"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "Successfully written %1"
|
||||
|
@ -1589,6 +1589,9 @@ msgstr "Stream"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1612,6 +1612,9 @@ msgstr "Transmisión"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Membrecía de streaming"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "%1 se ha escrito con éxito"
|
||||
|
@ -1589,6 +1589,9 @@ msgstr "Virta"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1601,6 +1601,9 @@ msgstr "Flux"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1589,6 +1589,9 @@ msgstr "Fluxo"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1604,6 +1604,9 @@ msgstr "Adatfolyam"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Adatfolyam tagság"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "%1 sikeresen írva"
|
||||
|
@ -1616,6 +1616,9 @@ msgstr "Flusso"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Trasmissione"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "%1 scritto correttamente"
|
||||
|
@ -1589,6 +1589,9 @@ msgstr "Ағындық"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1587,6 +1587,9 @@ msgstr ""
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1590,6 +1590,9 @@ msgstr "Strøm"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1606,6 +1606,9 @@ msgstr "Radiostream"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Streaming lidmaatschap"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "%1 met succes weggeschreven"
|
||||
|
@ -1587,6 +1587,9 @@ msgstr "Flux"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1598,6 +1598,9 @@ msgstr "Strumień"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1604,6 +1604,9 @@ msgstr "Emissão"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Emissão"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "Escrito com sucesso %1"
|
||||
|
@ -1599,6 +1599,9 @@ msgstr "Transmissão online"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Membro de multimídia"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "%1 gravado com sucesso"
|
||||
|
@ -1588,6 +1588,9 @@ msgstr "Flux"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1601,6 +1601,9 @@ msgstr "Поток"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Streaming подписка"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "Успешно записано %1"
|
||||
|
@ -1602,6 +1602,9 @@ msgstr "Stream"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Streamovacie členstvo"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "Úspešne zapísané %1"
|
||||
|
@ -1604,6 +1604,9 @@ msgstr "Tok"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "Uspešno zapisan %1"
|
||||
|
@ -1592,6 +1592,9 @@ msgstr "Ток"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1596,6 +1596,9 @@ msgstr "Ström"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Strömmningsmedlemskap"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "Skrev %1 med lyckat resultat"
|
||||
|
@ -1593,6 +1593,9 @@ msgstr "Akış"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Yayın akış üyeliği"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1577,6 +1577,9 @@ msgstr ""
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1602,6 +1602,9 @@ msgstr "Потік"
|
||||
msgid "Streaming membership"
|
||||
msgstr "Потокове членство"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr "Успішно записано %1"
|
||||
|
@ -1587,6 +1587,9 @@ msgstr "流媒体"
|
||||
msgid "Streaming membership"
|
||||
msgstr ""
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
@ -1592,6 +1592,9 @@ msgstr "串流"
|
||||
msgid "Streaming membership"
|
||||
msgstr "流媒體成員"
|
||||
|
||||
msgid "Stretch columns to fit window"
|
||||
msgstr ""
|
||||
|
||||
#, qt-format
|
||||
msgid "Successfully written %1"
|
||||
msgstr ""
|
||||
|
204
src/widgets/stretchheaderview.cpp
Normal file
204
src/widgets/stretchheaderview.cpp
Normal file
@ -0,0 +1,204 @@
|
||||
/* This file is part of Clementine.
|
||||
|
||||
Clementine 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "stretchheaderview.h"
|
||||
|
||||
#include <QDataStream>
|
||||
#include <QtDebug>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
const float StretchHeaderView::kMinimumColumnWidth = 0.01;
|
||||
|
||||
StretchHeaderView::StretchHeaderView(Qt::Orientation orientation, QWidget* parent)
|
||||
: QHeaderView(orientation, parent),
|
||||
stretch_enabled_(false),
|
||||
in_mouse_move_event_(false)
|
||||
{
|
||||
connect(this, SIGNAL(sectionResized(int,int,int)), SLOT(SectionResized(int,int,int)));
|
||||
}
|
||||
|
||||
void StretchHeaderView::setModel(QAbstractItemModel* model) {
|
||||
QHeaderView::setModel(model);
|
||||
|
||||
if (stretch_enabled_) {
|
||||
column_widths_.clear();
|
||||
int count = model->columnCount();
|
||||
for (int i=0 ; i<count ; ++i) {
|
||||
column_widths_ << 1.0 / count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StretchHeaderView::NormaliseWidths() {
|
||||
if (!stretch_enabled_)
|
||||
return;
|
||||
|
||||
float sum = 0.0;
|
||||
foreach (float width, column_widths_)
|
||||
sum += width;
|
||||
|
||||
if (sum != 0.0 && !qFuzzyCompare(sum, 1.0f)) {
|
||||
const float mult = 1.0 / sum;
|
||||
for (int i=0 ; i<column_widths_.count() ; ++i) {
|
||||
column_widths_[i] *= mult;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StretchHeaderView::UpdateWidths(const QList<int>& sections) {
|
||||
if (!stretch_enabled_)
|
||||
return;
|
||||
|
||||
int total_pixels = 0;
|
||||
for (int i=0 ; i<column_widths_.count() ; ++i) {
|
||||
const float w = column_widths_[i];
|
||||
const int pixels = w * width();
|
||||
total_pixels += pixels;
|
||||
|
||||
if (!sections.isEmpty() && !sections.contains(i))
|
||||
continue;
|
||||
|
||||
if (pixels == 0 && !isSectionHidden(i))
|
||||
hideSection(i);
|
||||
else if (pixels != 0 && isSectionHidden(i))
|
||||
showSection(i);
|
||||
|
||||
if (pixels != 0)
|
||||
resizeSection(i, pixels);
|
||||
}
|
||||
|
||||
// Add the remaining pixels to some arbitrary column just to make sure it
|
||||
// adds up to the total width.
|
||||
for (int i=0 ; i<column_widths_.count() ; ++i) {
|
||||
if (!sections.isEmpty() && !sections.contains(i))
|
||||
continue;
|
||||
|
||||
const float w = column_widths_[i];
|
||||
const int pixels = w * width();
|
||||
if (pixels != 0) {
|
||||
resizeSection(i, pixels + (width() - total_pixels));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StretchHeaderView::HideSection(int logical) {
|
||||
if (!stretch_enabled_) {
|
||||
hideSection(logical);
|
||||
return;
|
||||
}
|
||||
|
||||
column_widths_[logical] = 0.0;
|
||||
NormaliseWidths();
|
||||
UpdateWidths();
|
||||
}
|
||||
|
||||
void StretchHeaderView::ShowSection(int logical) {
|
||||
if (!stretch_enabled_) {
|
||||
showSection(logical);
|
||||
return;
|
||||
}
|
||||
|
||||
// How many sections are visible already?
|
||||
int visible_count = 0;
|
||||
for (int i=0 ; i<count() ; ++i) {
|
||||
if (!isSectionHidden(i))
|
||||
visible_count ++;
|
||||
}
|
||||
|
||||
column_widths_[logical] =
|
||||
visible_count == 0 ? 1.0 : 1.0 / visible_count;
|
||||
NormaliseWidths();
|
||||
UpdateWidths();
|
||||
}
|
||||
|
||||
void StretchHeaderView::SetSectionHidden(int logical, bool hidden) {
|
||||
hidden ? HideSection(logical) : ShowSection(logical);
|
||||
}
|
||||
|
||||
void StretchHeaderView::resizeEvent(QResizeEvent* event) {
|
||||
QHeaderView::resizeEvent(event);
|
||||
|
||||
if (!stretch_enabled_)
|
||||
return;
|
||||
|
||||
UpdateWidths();
|
||||
}
|
||||
|
||||
void StretchHeaderView::mouseMoveEvent(QMouseEvent* e) {
|
||||
in_mouse_move_event_ = true;
|
||||
QHeaderView::mouseMoveEvent(e);
|
||||
in_mouse_move_event_ = false;
|
||||
}
|
||||
|
||||
void StretchHeaderView::SectionResized(int logical, int, int new_size) {
|
||||
if (!stretch_enabled_)
|
||||
return;
|
||||
|
||||
if (in_mouse_move_event_) {
|
||||
// Update this section's proportional width
|
||||
column_widths_[logical] = float(new_size) / width();
|
||||
|
||||
// Find the visible sections to the right of the section that's being resized
|
||||
int visual = visualIndex(logical);
|
||||
QList<int> logical_sections_to_resize;
|
||||
for (int i=0 ; i<count() ; ++i) {
|
||||
if (!isSectionHidden(i) && visualIndex(i) > visual)
|
||||
logical_sections_to_resize << i;
|
||||
}
|
||||
|
||||
// Resize just those columns
|
||||
if (!logical_sections_to_resize.isEmpty()) {
|
||||
float sum = 0.0;
|
||||
foreach (float width, column_widths_)
|
||||
sum += width;
|
||||
|
||||
if (sum != 0.0 && !qFuzzyCompare(sum, 1.0f)) {
|
||||
const float d = (1.0 - sum) / logical_sections_to_resize.count();
|
||||
foreach (int i, logical_sections_to_resize) {
|
||||
column_widths_[i] = qMax(kMinimumColumnWidth, column_widths_[i] + d);
|
||||
}
|
||||
}
|
||||
|
||||
in_mouse_move_event_ = false;
|
||||
UpdateWidths(logical_sections_to_resize);
|
||||
in_mouse_move_event_ = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StretchHeaderView::ToggleStretchEnabled() {
|
||||
SetStretchEnabled(!is_stretch_enabled());
|
||||
}
|
||||
|
||||
void StretchHeaderView::SetStretchEnabled(bool enabled) {
|
||||
stretch_enabled_ = enabled;
|
||||
|
||||
if (enabled) {
|
||||
// Initialise the list of widths from the current state of the widget
|
||||
column_widths_.clear();
|
||||
for (int i=0 ; i<count() ; ++i) {
|
||||
column_widths_ << float(sectionSize(i)) / width();
|
||||
}
|
||||
|
||||
// Stretch the columns to fill the widget
|
||||
NormaliseWidths();
|
||||
UpdateWidths();
|
||||
}
|
||||
|
||||
emit StretchEnabledChanged(enabled);
|
||||
}
|
63
src/widgets/stretchheaderview.h
Normal file
63
src/widgets/stretchheaderview.h
Normal file
@ -0,0 +1,63 @@
|
||||
/* This file is part of Clementine.
|
||||
|
||||
Clementine 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef STRETCHHEADERVIEW_H
|
||||
#define STRETCHHEADERVIEW_H
|
||||
|
||||
#include <QHeaderView>
|
||||
|
||||
class StretchHeaderView : public QHeaderView {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
StretchHeaderView(Qt::Orientation orientation, QWidget* parent = 0);
|
||||
|
||||
static const float kMinimumColumnWidth;
|
||||
|
||||
void setModel(QAbstractItemModel* model);
|
||||
|
||||
void HideSection(int logical);
|
||||
void ShowSection(int logical);
|
||||
void SetSectionHidden(int logical, bool hidden);
|
||||
|
||||
bool is_stretch_enabled() const { return stretch_enabled_; }
|
||||
|
||||
public slots:
|
||||
void ToggleStretchEnabled();
|
||||
void SetStretchEnabled(bool enabled);
|
||||
|
||||
signals:
|
||||
void StretchEnabledChanged(bool enabled);
|
||||
|
||||
protected:
|
||||
void mouseMoveEvent(QMouseEvent* e);
|
||||
void resizeEvent(QResizeEvent* event);
|
||||
|
||||
private:
|
||||
void NormaliseWidths();
|
||||
void UpdateWidths(const QList<int>& sections = QList<int>());
|
||||
|
||||
private slots:
|
||||
void SectionResized(int logical, int old_size, int new_size);
|
||||
|
||||
private:
|
||||
bool stretch_enabled_;
|
||||
QList<float> column_widths_;
|
||||
|
||||
bool in_mouse_move_event_;
|
||||
};
|
||||
|
||||
#endif // STRETCHHEADERVIEW_H
|
Loading…
x
Reference in New Issue
Block a user