statuslabel.cpp 4.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */
16

17 18
#include "statuslabel.h"

19 20
#include <KIconLoader>
#include <KSqueezedTextLabel>
Michael Pyne's avatar
Michael Pyne committed
21
#include <KLocalizedString>
22
#include <KFormat>
23

24
#include <QAction>
Laurent Montel's avatar
Laurent Montel committed
25 26
#include <QMouseEvent>
#include <QLabel>
27
#include <QIcon>
Laurent Montel's avatar
Laurent Montel committed
28
#include <QFrame>
Laurent Montel's avatar
Laurent Montel committed
29
#include <QEvent>
30
#include <QPushButton>
31
#include <QStatusBar>
32

33 34
#include "filehandle.h"
#include "playlistinterface.h"
35
#include "actioncollection.h"
36
#include "playermanager.h"
37
#include "tag.h"
38
#include "juk_debug.h"
39

40 41
using namespace ActionCollection;

42 43 44 45
////////////////////////////////////////////////////////////////////////////////
// static helpers
////////////////////////////////////////////////////////////////////////////////

46
static QString formatTime(qint64 milliseconds)
47 48
{
    static const KFormat fmt;
49
    return fmt.formatDuration(milliseconds);
50 51
}

52 53 54
////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////
55

56
StatusLabel::StatusLabel(const PlaylistInterface &currentPlaylist, QStatusBar *parent) :
57
    QWidget(parent)
58
{
59 60
    m_playlistLabel = new KSqueezedTextLabel(this);
    m_playlistLabel->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred, QSizePolicy::Label));
61 62
    m_playlistLabel->setTextFormat(Qt::PlainText);
    m_playlistLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
63
    parent->addWidget(m_playlistLabel, 1);
64

65
    m_trackLabel = new QLabel(this);
66 67
    m_trackLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    m_trackLabel->setTextFormat(Qt::PlainText);
68
    parent->addPermanentWidget(m_trackLabel);
69

70
    m_itemTimeLabel = new QLabel(this);
71
    QFontMetrics fontMetrics(font());
72
    m_itemTimeLabel->setAlignment(Qt::AlignCenter);
73 74
    m_itemTimeLabel->setMinimumWidth(fontMetrics.boundingRect("000:00 / 000:00").width());
    m_itemTimeLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
75
    m_itemTimeLabel->setFrameStyle(QFrame::Box | QFrame::Sunken);
76
    m_itemTimeLabel->installEventFilter(this);
77
    parent->addPermanentWidget(m_itemTimeLabel);
78 79 80

    setItemTotalTime(0);
    setItemCurrentTime(0);
81

82
    QPushButton *jumpButton = new QPushButton(this);
83
    jumpButton->setIcon(QIcon::fromTheme("go-jump"));
84 85
    jumpButton->setFlat(true);

86
    jumpButton->setToolTip(i18n("Jump to the currently playing item"));
87
    connect(jumpButton, &QPushButton::clicked, action("showPlaying"), &QAction::trigger);
88

89
    parent->addPermanentWidget(jumpButton);
90
    installEventFilter(this);
91

92
    slotCurrentPlaylistHasChanged(currentPlaylist);
93 94
}

95
void StatusLabel::slotPlayingItemHasChanged(const FileHandle &file)
96
{
97 98 99 100
    const Tag *tag = file.tag();
    const QString mid = (tag->artist().isEmpty() || tag->title().isEmpty())
        ? QString()
        : QStringLiteral(" - ");
101

102 103 104
    setItemTotalTime(tag->seconds());
    setItemCurrentTime(0);

105
    m_trackLabel->setText(tag->artist() + mid + tag->title());
106 107
}

108
void StatusLabel::slotCurrentPlaylistHasChanged(const PlaylistInterface &currentPlaylist)
109
{
110
    if(!currentPlaylist.playing()) {
111 112
        return;
    }
113

114
    m_playlistLabel->setText(currentPlaylist.name());
115
    m_trackLabel->setText(
116
            i18np("1 item", "%1 items", currentPlaylist.count()) +
117
            QStringLiteral(" - ") +
118
            formatTime(qint64(1000) * currentPlaylist.time())
119
            );
120 121
}

122 123 124 125
////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

126
void StatusLabel::updateTime()
127
{
128
    const qint64 milliseconds = m_showTimeRemaining
129 130
        ? m_itemTotalTime - m_itemCurrentTime
        : m_itemCurrentTime;
131
    const QString timeString = formatTime(milliseconds) + QStringLiteral(" / ") +
132
        formatTime(m_itemTotalTime);
133

134
    m_itemTimeLabel->setText(timeString);
135
}
136

137 138 139
bool StatusLabel::eventFilter(QObject *o, QEvent *e)
{
    if(!o || !e)
140
        return false;
141

142 143
    QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
    if(e->type() == QEvent::MouseButtonRelease &&
144
       mouseEvent->button() == Qt::LeftButton)
145
    {
146 147 148 149 150
        if(o == m_itemTimeLabel) {
            m_showTimeRemaining = !m_showTimeRemaining;
            updateTime();
        }
        else
David Faure's avatar
David Faure committed
151
            action("showPlaying")->trigger();
152

153
        return true;
154
    }
155
    return false;
156 157
}

158
// vim: set et sw=4 tw=0 sta: