File indexing completed on 2021-12-21 13:28:00
0001 /** 0002 * Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org> 0003 * 0004 * This program is free software; you can redistribute it and/or modify it under 0005 * the terms of the GNU General Public License as published by the Free Software 0006 * Foundation; either version 2 of the License, or (at your option) any later 0007 * version. 0008 * 0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details. 0012 * 0013 * You should have received a copy of the GNU General Public License along with 0014 * this program. If not, see <http://www.gnu.org/licenses/>. 0015 */ 0016 0017 #include "statuslabel.h" 0018 0019 #include <KIconLoader> 0020 #include <KSqueezedTextLabel> 0021 #include <KLocalizedString> 0022 #include <KFormat> 0023 0024 #include <QAction> 0025 #include <QMouseEvent> 0026 #include <QLabel> 0027 #include <QIcon> 0028 #include <QToolButton> 0029 #include <QPushButton> 0030 #include <QStatusBar> 0031 0032 #include "actioncollection.h" 0033 #include "filehandle.h" 0034 #include "iconsupport.h" 0035 #include "juk_debug.h" 0036 #include "juktag.h" 0037 #include "playermanager.h" 0038 #include "playlistinterface.h" 0039 0040 using namespace ActionCollection; 0041 0042 //////////////////////////////////////////////////////////////////////////////// 0043 // static helpers 0044 //////////////////////////////////////////////////////////////////////////////// 0045 0046 static QString formatTime(qint64 milliseconds) 0047 { 0048 static const KFormat fmt; 0049 return fmt.formatDuration(milliseconds); 0050 } 0051 0052 //////////////////////////////////////////////////////////////////////////////// 0053 // public methods 0054 //////////////////////////////////////////////////////////////////////////////// 0055 0056 StatusLabel::StatusLabel(const PlaylistInterface ¤tPlaylist, QStatusBar *parent) : 0057 QWidget(parent) 0058 { 0059 using namespace IconSupport; // ""_icon 0060 0061 m_playlistLabel = new KSqueezedTextLabel(this); 0062 m_playlistLabel->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred, QSizePolicy::Label)); 0063 m_playlistLabel->setTextFormat(Qt::PlainText); 0064 m_playlistLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); 0065 parent->addWidget(m_playlistLabel, 1); 0066 0067 m_trackLabel = new QLabel(this); 0068 m_trackLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter); 0069 m_trackLabel->setTextFormat(Qt::PlainText); 0070 parent->addPermanentWidget(m_trackLabel); 0071 0072 m_itemTimeLabel = new QToolButton(this); 0073 QFontMetrics fontMetrics(font()); 0074 m_itemTimeLabel->setMinimumWidth(fontMetrics.boundingRect("000:00 / 000:00").width()); 0075 m_itemTimeLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); 0076 m_itemTimeLabel->setAutoRaise(true); 0077 connect(m_itemTimeLabel, &QAbstractButton::clicked, this, [this]() { 0078 m_showTimeRemaining = !m_showTimeRemaining; 0079 updateTime(); 0080 }); 0081 parent->addPermanentWidget(m_itemTimeLabel); 0082 0083 QPushButton *jumpButton = new QPushButton(this); 0084 jumpButton->setIcon("go-jump"_icon); 0085 jumpButton->setFlat(true); 0086 0087 jumpButton->setToolTip(i18n("Jump to the currently playing item")); 0088 connect(jumpButton, &QPushButton::clicked, action("showPlaying"), &QAction::trigger); 0089 0090 parent->addPermanentWidget(jumpButton); 0091 0092 slotCurrentPlaylistHasChanged(currentPlaylist); 0093 } 0094 0095 void StatusLabel::slotPlayingItemHasChanged(const FileHandle &file) 0096 { 0097 const Tag *tag = file.tag(); 0098 const QString mid = (tag->artist().isEmpty() || tag->title().isEmpty()) 0099 ? QString() 0100 : QStringLiteral(" - "); 0101 0102 setItemTotalTime(tag->seconds()); 0103 setItemCurrentTime(0); 0104 0105 m_trackLabel->setText(tag->artist() + mid + tag->title()); 0106 updateTime(); 0107 } 0108 0109 void StatusLabel::slotCurrentPlaylistHasChanged(const PlaylistInterface ¤tPlaylist) 0110 { 0111 if(!currentPlaylist.playing()) { 0112 return; 0113 } 0114 0115 m_playlistLabel->setText(currentPlaylist.name()); 0116 m_trackLabel->setText( 0117 i18np("1 item", "%1 items", currentPlaylist.count()) + 0118 QStringLiteral(" - ") + 0119 formatTime(qint64(1000) * currentPlaylist.time()) 0120 ); 0121 updateTime(); 0122 } 0123 0124 //////////////////////////////////////////////////////////////////////////////// 0125 // protected methods 0126 //////////////////////////////////////////////////////////////////////////////// 0127 0128 void StatusLabel::mouseReleaseEvent(QMouseEvent *ev) 0129 { 0130 if(ev->button() != Qt::LeftButton) { 0131 return; 0132 } 0133 0134 m_showTimeRemaining = !m_showTimeRemaining; 0135 updateTime(); 0136 0137 ev->accept(); 0138 } 0139 0140 //////////////////////////////////////////////////////////////////////////////// 0141 // private methods 0142 //////////////////////////////////////////////////////////////////////////////// 0143 0144 void StatusLabel::updateTime() 0145 { 0146 const qint64 milliseconds = m_showTimeRemaining 0147 ? m_itemTotalTime - m_itemCurrentTime 0148 : m_itemCurrentTime; 0149 const QString timeString = formatTime(milliseconds) + QStringLiteral(" / ") + 0150 formatTime(m_itemTotalTime); 0151 0152 m_itemTimeLabel->setText(timeString); 0153 } 0154 0155 // vim: set et sw=4 tw=0 sta: