statuslabel.cpp 6.58 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
#include <kaction.h>
20
#include <kiconloader.h>
21
#include <ksqueezedtextlabel.h>
Scott Wheeler's avatar
Scott Wheeler committed
22
#include <klocale.h>
23 24
#include <kdebug.h>

Laurent Montel's avatar
Laurent Montel committed
25 26
#include <QMouseEvent>
#include <QLabel>
Laurent Montel's avatar
Laurent Montel committed
27
#include <QFrame>
Laurent Montel's avatar
Laurent Montel committed
28 29
#include <QHBoxLayout>
#include <QEvent>
30
#include <QPushButton>
31

32 33
#include "filehandle.h"
#include "playlistinterface.h"
34
#include "actioncollection.h"
35
#include "tag.h"
36

37 38
using namespace ActionCollection;

39 40 41
////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////
42

Laurent Montel's avatar
Laurent Montel committed
43 44
StatusLabel::StatusLabel(PlaylistInterface *playlist, QWidget *parent) :
    KHBox(parent),
45
    PlaylistObserver(playlist),
Scott Wheeler's avatar
Scott Wheeler committed
46
    m_showTimeRemaining(false)
47
{
Laurent Montel's avatar
Laurent Montel committed
48
    QFrame *trackAndPlaylist = new QFrame(this);
49
    trackAndPlaylist->setFrameStyle(Box | Sunken);
50
    trackAndPlaylist->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
51 52

    // Make sure that we have enough of a margin to suffice for the borders,
53
    // hence the "lineWidth() * 2"
Stephan Kulow's avatar
Stephan Kulow committed
54 55 56
    QHBoxLayout *trackAndPlaylistLayout = new QHBoxLayout( trackAndPlaylist );
    trackAndPlaylistLayout->setMargin( trackAndPlaylist->lineWidth() * 2 );
    trackAndPlaylistLayout->setSpacing( 5 );
Laurent Montel's avatar
Laurent Montel committed
57
    trackAndPlaylistLayout->setObjectName( QLatin1String( "trackAndPlaylistLayout" ));
58
    trackAndPlaylistLayout->addSpacing(5);
59

60
    m_playlistLabel = new KSqueezedTextLabel(trackAndPlaylist);
61 62
    trackAndPlaylistLayout->addWidget(m_playlistLabel);
    m_playlistLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
63 64
    m_playlistLabel->setTextFormat(Qt::PlainText);
    m_playlistLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
65

66
    m_trackLabel = new KSqueezedTextLabel(trackAndPlaylist);
67
    trackAndPlaylistLayout->addWidget(m_trackLabel);
68
    m_trackLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
69
    m_trackLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
70
    m_trackLabel->setTextFormat(Qt::PlainText);
71

72
    trackAndPlaylistLayout->addSpacing(5);
73

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

    setItemTotalTime(0);
    setItemCurrentTime(0);
84

Laurent Montel's avatar
Laurent Montel committed
85
    KHBox *jumpBox = new KHBox(this);
86 87 88 89
    jumpBox->setFrameStyle(Box | Sunken);
    jumpBox->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum);

    QPushButton *jumpButton = new QPushButton(jumpBox);
90
    jumpButton->setIcon(SmallIcon("go-up"));
91 92
    jumpButton->setFlat(true);

Laurent Montel's avatar
Laurent Montel committed
93
    jumpButton->setToolTip( i18n("Jump to the currently playing item"));
David Faure's avatar
David Faure committed
94
    connect(jumpButton, SIGNAL(clicked()), action("showPlaying"), SLOT(trigger()));
95 96

    installEventFilter(this);
97

98
    updateData();
99 100 101 102 103 104 105
}

StatusLabel::~StatusLabel()
{

}

106
void StatusLabel::updateCurrent()
107
{
108 109
    if(playlist()->playing()) {
        FileHandle file = playlist()->currentFile();
110 111

        QString mid =  file.tag()->artist().isEmpty() || file.tag()->title().isEmpty()
112
            ? QString::null : QString(" - ");	//krazy:exclude=nullstrassign for old broken gcc
113 114 115

        QString text = file.tag()->artist() + mid + file.tag()->title();

116
        m_trackLabel->setText(text);
Tim Beaulen's avatar
Tim Beaulen committed
117
        m_playlistLabel->setText(playlist()->name().simplified());
118
    }
119 120 121 122
}

void StatusLabel::updateData()
{
123
    updateCurrent();
124 125

    if(!playlist()->playing()) {
126 127 128
        setItemTotalTime(0);
        setItemCurrentTime(0);

129
        int time = playlist()->time();
130 131 132 133 134 135 136

        int days = time / (60 * 60 * 24);
        int hours = time / (60 * 60) % 24;
        int minutes = time / 60 % 60;
        int seconds = time % 60;

        QString timeString;
137 138

        if(days > 0) {
139
            timeString = i18np("1 day", "%1 days", days);
140
            timeString.append(" ");
141 142 143
        }

        if(days > 0 || hours > 0)
144
            timeString.append(QString().sprintf("%1d:%02d:%02d", hours, minutes, seconds));
145
        else
146
            timeString.append(QString().sprintf("%1d:%02d", minutes, seconds));
147 148

        m_playlistLabel->setText(playlist()->name());
149
        m_trackLabel->setText(i18np("1 item", "%1 items", playlist()->count()) + " - " + timeString);
150
    }
151 152
}

153 154 155 156
////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

157
void StatusLabel::updateTime()
158
{
159 160 161
    int minutes;
    int seconds;

162
    if(m_showTimeRemaining) {
163 164
        minutes = int((m_itemTotalTime - m_itemCurrentTime) / 60);
        seconds = (m_itemTotalTime - m_itemCurrentTime) % 60;
165 166
    }
    else {
167 168
        minutes = int(m_itemCurrentTime / 60);
        seconds = m_itemCurrentTime % 60;
169 170
    }

171 172
    int totalMinutes = int(m_itemTotalTime / 60);
    int totalSeconds = m_itemTotalTime % 60;
173

Scott Wheeler's avatar
Scott Wheeler committed
174
    QString timeString = formatTime(minutes, seconds) +  " / " +
175
        formatTime(totalMinutes, totalSeconds);
176
    m_itemTimeLabel->setText(timeString);
177
}
178

179 180 181
bool StatusLabel::eventFilter(QObject *o, QEvent *e)
{
    if(!o || !e)
182
        return false;
183

184 185
    QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
    if(e->type() == QEvent::MouseButtonRelease &&
186
       mouseEvent->button() == Qt::LeftButton)
187
    {
188 189 190 191 192
        if(o == m_itemTimeLabel) {
            m_showTimeRemaining = !m_showTimeRemaining;
            updateTime();
        }
        else
David Faure's avatar
David Faure committed
193
            action("showPlaying")->trigger();
194

195
        return true;
196
    }
197
    return false;
198 199
}

200 201 202 203
QString StatusLabel::formatTime(int minutes, int seconds) // static
{
    QString m = QString::number(minutes);
    if(m.length() == 1)
204
        m = '0' + m;
205 206
    QString s = QString::number(seconds);
    if(s.length() == 1)
207 208
        s = '0' + s;
    return m + ':' + s;
209 210
}

211
#include "statuslabel.moc"
212 213

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