diff --git a/statuslabel.cpp b/statuslabel.cpp index ac9f7eca..25571396 100644 --- a/statuslabel.cpp +++ b/statuslabel.cpp @@ -1,219 +1,192 @@ /** * Copyright (C) 2002-2004 Scott Wheeler * * 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. If not, see . */ #include "statuslabel.h" -#include -#include +#include +#include #include +#include #include #include #include #include #include #include #include #include "filehandle.h" #include "playlistinterface.h" #include "actioncollection.h" #include "tag.h" #include "juk_debug.h" using namespace ActionCollection; +//////////////////////////////////////////////////////////////////////////////// +// static helpers +//////////////////////////////////////////////////////////////////////////////// + +static QString formatTime(int seconds) +{ + static const KFormat fmt; + return fmt.formatDuration(seconds * 1000); +} + //////////////////////////////////////////////////////////////////////////////// // public methods //////////////////////////////////////////////////////////////////////////////// StatusLabel::StatusLabel(PlaylistInterface *playlist, QWidget *parent) : QWidget(parent), - PlaylistObserver(playlist), - m_showTimeRemaining(false) + PlaylistObserver(playlist) { auto hboxLayout = new QHBoxLayout(this); QFrame *trackAndPlaylist = new QFrame(this); hboxLayout->addWidget(trackAndPlaylist); trackAndPlaylist->setFrameStyle(QFrame::Box | QFrame::Sunken); trackAndPlaylist->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); // Make sure that we have enough of a margin to suffice for the borders, // hence the "lineWidth() * 2" QHBoxLayout *trackAndPlaylistLayout = new QHBoxLayout(trackAndPlaylist); trackAndPlaylistLayout->setMargin(trackAndPlaylist->lineWidth() * 2); trackAndPlaylistLayout->setSpacing(5); trackAndPlaylistLayout->setObjectName(QLatin1String("trackAndPlaylistLayout")); trackAndPlaylistLayout->addSpacing(5); m_playlistLabel = new KSqueezedTextLabel(trackAndPlaylist); trackAndPlaylistLayout->addWidget(m_playlistLabel); m_playlistLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_playlistLabel->setTextFormat(Qt::PlainText); m_playlistLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); m_trackLabel = new KSqueezedTextLabel(trackAndPlaylist); trackAndPlaylistLayout->addWidget(m_trackLabel); m_trackLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter); m_trackLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_trackLabel->setTextFormat(Qt::PlainText); trackAndPlaylistLayout->addSpacing(5); m_itemTimeLabel = new QLabel(this); hboxLayout->addWidget(m_itemTimeLabel); QFontMetrics fontMetrics(font()); m_itemTimeLabel->setAlignment(Qt::AlignCenter); m_itemTimeLabel->setMinimumWidth(fontMetrics.boundingRect("000:00 / 000:00").width()); m_itemTimeLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); m_itemTimeLabel->setFrameStyle(QFrame::Box | QFrame::Sunken); m_itemTimeLabel->installEventFilter(this); setItemTotalTime(0); setItemCurrentTime(0); auto jumpBox = new QFrame(this); hboxLayout->addWidget(jumpBox); jumpBox->setFrameStyle(QFrame::Box | QFrame::Sunken); jumpBox->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum); auto jumpBoxHLayout = new QHBoxLayout(jumpBox); QPushButton *jumpButton = new QPushButton(jumpBox); jumpBoxHLayout->addWidget(jumpButton); jumpButton->setIcon(SmallIcon("go-up")); jumpButton->setFlat(true); jumpButton->setToolTip(i18n("Jump to the currently playing item")); - connect(jumpButton, SIGNAL(clicked()), action("showPlaying"), SLOT(trigger())); + connect(jumpButton, &QPushButton::clicked, action("showPlaying"), &QAction::trigger); installEventFilter(this); playlistItemDataHasChanged(); } void StatusLabel::playingItemHasChanged() { if(!playlist()->playing()) { return; } - FileHandle file = playlist()->currentFile(); + const FileHandle file = playlist()->currentFile(); + const Tag *tag = file.tag(); + const QString mid = (tag->artist().isEmpty() || tag->title().isEmpty()) + ? QString() + : QStringLiteral(" - "); - QString mid = file.tag()->artist().isEmpty() || file.tag()->title().isEmpty() - ? QString::null : QString(" - "); //krazy:exclude=nullstrassign for old broken gcc - - QString text = file.tag()->artist() + mid + file.tag()->title(); - - m_trackLabel->setText(text); + m_trackLabel->setText(tag->artist() + mid + tag->title()); m_playlistLabel->setText(playlist()->name().simplified()); } void StatusLabel::playlistItemDataHasChanged() { playingItemHasChanged(); - if(!playlist()->playing()) { + const auto plist = playlist(); + + if(!plist->playing()) { return; } setItemTotalTime(0); setItemCurrentTime(0); - int time = playlist()->time(); - - int days = time / (60 * 60 * 24); - int hours = time / (60 * 60) % 24; - int minutes = time / 60 % 60; - int seconds = time % 60; - - QString timeString; - - if(days > 0) { - timeString = i18np("1 day", "%1 days", days); - timeString.append(" "); - } - - if(days > 0 || hours > 0) - timeString.append(QString().sprintf("%1d:%02d:%02d", hours, minutes, seconds)); - else - timeString.append(QString().sprintf("%1d:%02d", minutes, seconds)); - - m_playlistLabel->setText(playlist()->name()); - m_trackLabel->setText(i18np("1 item", "%1 items", playlist()->count()) + " - " + timeString); + m_playlistLabel->setText(plist->name()); + m_trackLabel->setText( + i18np("1 item", "%1 items", plist->count()) + + QStringLiteral(" - ") + + formatTime(plist->time()) + ); } //////////////////////////////////////////////////////////////////////////////// // private methods //////////////////////////////////////////////////////////////////////////////// void StatusLabel::updateTime() { - int minutes; - int seconds; + const int seconds = m_showTimeRemaining + ? m_itemTotalTime - m_itemCurrentTime + : m_itemCurrentTime; + const QString timeString = formatTime(seconds) + QStringLiteral(" / ") + + formatTime(m_itemTotalTime); - if(m_showTimeRemaining) { - minutes = int((m_itemTotalTime - m_itemCurrentTime) / 60); - seconds = (m_itemTotalTime - m_itemCurrentTime) % 60; - } - else { - minutes = int(m_itemCurrentTime / 60); - seconds = m_itemCurrentTime % 60; - } - - int totalMinutes = int(m_itemTotalTime / 60); - int totalSeconds = m_itemTotalTime % 60; - - QString timeString = formatTime(minutes, seconds) + " / " + - formatTime(totalMinutes, totalSeconds); m_itemTimeLabel->setText(timeString); } bool StatusLabel::eventFilter(QObject *o, QEvent *e) { if(!o || !e) return false; QMouseEvent *mouseEvent = static_cast(e); if(e->type() == QEvent::MouseButtonRelease && mouseEvent->button() == Qt::LeftButton) { if(o == m_itemTimeLabel) { m_showTimeRemaining = !m_showTimeRemaining; updateTime(); } else action("showPlaying")->trigger(); return true; } return false; } -// TODO: Look at QLocale or KCoreAddons::format for showing time durations -QString StatusLabel::formatTime(int minutes, int seconds) // static -{ - QString m = QString::number(minutes); - if(m.length() == 1) - m = '0' + m; - QString s = QString::number(seconds); - if(s.length() == 1) - s = '0' + s; - return m + ':' + s; -} - // vim: set et sw=4 tw=0 sta: diff --git a/statuslabel.h b/statuslabel.h index cd80eee3..0771a894 100644 --- a/statuslabel.h +++ b/statuslabel.h @@ -1,65 +1,60 @@ /** * Copyright (C) 2002-2004 Scott Wheeler * * 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. If not, see . */ #ifndef JUK_STATUSLABEL_H #define JUK_STATUSLABEL_H #include "playlistinterface.h" #include class QEvent; class QLabel; class KSqueezedTextLabel; class StatusLabel : public QWidget, public PlaylistObserver { Q_OBJECT public: explicit StatusLabel(PlaylistInterface *playlist, QWidget *parent = nullptr); virtual void playingItemHasChanged() Q_DECL_FINAL; public slots: /** * This just sets internal variables that are used by setItemCurrentTime(). * Please call that method to display the time. */ void setItemTotalTime(int time) { m_itemTotalTime = time; } void setItemCurrentTime(int time) { m_itemCurrentTime = time; updateTime(); } virtual void playlistItemDataHasChanged() Q_DECL_FINAL; -signals: - void jumpButtonClicked(); - private: void updateTime(); virtual bool eventFilter(QObject *o, QEvent *e); - static QString formatTime(int minutes, int seconds); - - int m_itemTotalTime; - int m_itemCurrentTime; - bool m_showTimeRemaining; + KSqueezedTextLabel *m_playlistLabel = nullptr; + KSqueezedTextLabel *m_trackLabel = nullptr; + QLabel *m_itemTimeLabel = nullptr; - KSqueezedTextLabel *m_playlistLabel; - KSqueezedTextLabel *m_trackLabel; - QLabel *m_itemTimeLabel; + int m_itemTotalTime = 0; + int m_itemCurrentTime = 0; + bool m_showTimeRemaining = false; }; #endif // vim: set et sw=4 tw=0 sta: