File indexing completed on 2023-05-30 11:30:51

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 <KSqueezedTextLabel>
0020 #include <KLocalizedString>
0021 #include <KFormat>
0022 
0023 #include <QAction>
0024 #include <QLabel>
0025 #include <QMouseEvent>
0026 #include <QPushButton>
0027 #include <QStatusBar>
0028 #include <QToolButton>
0029 
0030 #include "actioncollection.h"
0031 #include "filehandle.h"
0032 #include "iconsupport.h"
0033 #include "juk_debug.h"
0034 #include "juktag.h"
0035 #include "playermanager.h"
0036 #include "playlistinterface.h"
0037 
0038 using namespace ActionCollection;
0039 
0040 ////////////////////////////////////////////////////////////////////////////////
0041 // static helpers
0042 ////////////////////////////////////////////////////////////////////////////////
0043 
0044 static QString formatTime(qint64 milliseconds)
0045 {
0046     static const KFormat fmt;
0047     return fmt.formatDuration(milliseconds);
0048 }
0049 
0050 ////////////////////////////////////////////////////////////////////////////////
0051 // public methods
0052 ////////////////////////////////////////////////////////////////////////////////
0053 
0054 StatusLabel::StatusLabel(const PlaylistInterface &currentPlaylist, QStatusBar *parent) :
0055     QWidget(parent)
0056 {
0057     using namespace IconSupport; // ""_icon
0058 
0059     m_playlistLabel = new KSqueezedTextLabel(this);
0060     m_playlistLabel->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred, QSizePolicy::Label));
0061     m_playlistLabel->setTextFormat(Qt::PlainText);
0062     m_playlistLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
0063     parent->addWidget(m_playlistLabel, 1);
0064 
0065     m_trackLabel = new QLabel(this);
0066     m_trackLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
0067     m_trackLabel->setTextFormat(Qt::PlainText);
0068     parent->addPermanentWidget(m_trackLabel);
0069 
0070     m_itemTimeLabel = new QToolButton(this);
0071     QFontMetrics fontMetrics(font());
0072     m_itemTimeLabel->setMinimumWidth(fontMetrics.boundingRect("000:00 / 000:00").width());
0073     m_itemTimeLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
0074     m_itemTimeLabel->setAutoRaise(true);
0075     connect(m_itemTimeLabel, &QAbstractButton::clicked, this, [this]() {
0076                 m_showTimeRemaining = !m_showTimeRemaining;
0077                 updateTime();
0078             });
0079     parent->addPermanentWidget(m_itemTimeLabel);
0080 
0081     QPushButton *jumpButton = new QPushButton(this);
0082     jumpButton->setIcon("go-jump"_icon);
0083     jumpButton->setFlat(true);
0084 
0085     jumpButton->setToolTip(i18n("Jump to the currently playing item"));
0086     connect(jumpButton, &QPushButton::clicked, action("showPlaying"), &QAction::trigger);
0087 
0088     parent->addPermanentWidget(jumpButton);
0089 
0090     slotCurrentPlaylistHasChanged(currentPlaylist);
0091 }
0092 
0093 void StatusLabel::slotPlayingItemHasChanged(const FileHandle &file)
0094 {
0095     const Tag *tag = file.tag();
0096     const QString mid = (tag->artist().isEmpty() || tag->title().isEmpty())
0097         ? QString()
0098         : QStringLiteral(" - ");
0099 
0100     setItemTotalTime(tag->seconds());
0101     setItemCurrentTime(0);
0102 
0103     m_trackLabel->setText(tag->artist() + mid + tag->title());
0104     updateTime();
0105 }
0106 
0107 void StatusLabel::slotCurrentPlaylistHasChanged(const PlaylistInterface &currentPlaylist)
0108 {
0109     if(!currentPlaylist.playing()) {
0110         return;
0111     }
0112 
0113     m_playlistLabel->setText(currentPlaylist.name());
0114     m_trackLabel->setText(
0115             i18np("1 item", "%1 items", currentPlaylist.count()) +
0116             QStringLiteral(" - ") +
0117             formatTime(qint64(1000) * currentPlaylist.time())
0118             );
0119     updateTime();
0120 }
0121 
0122 ////////////////////////////////////////////////////////////////////////////////
0123 // protected methods
0124 ////////////////////////////////////////////////////////////////////////////////
0125 
0126 void StatusLabel::mouseReleaseEvent(QMouseEvent *ev)
0127 {
0128     if(ev->button() != Qt::LeftButton) {
0129         return;
0130     }
0131 
0132     m_showTimeRemaining = !m_showTimeRemaining;
0133     updateTime();
0134 
0135     ev->accept();
0136 }
0137 
0138 ////////////////////////////////////////////////////////////////////////////////
0139 // private methods
0140 ////////////////////////////////////////////////////////////////////////////////
0141 
0142 void StatusLabel::updateTime()
0143 {
0144     const qint64 milliseconds = m_showTimeRemaining
0145         ? m_itemTotalTime - m_itemCurrentTime
0146         : m_itemCurrentTime;
0147     const QString timeString = formatTime(milliseconds) + QStringLiteral(" / ") +
0148         formatTime(m_itemTotalTime);
0149 
0150     m_itemTimeLabel->setText(timeString);
0151 }
0152 
0153 // vim: set et sw=4 tw=0 sta: