statuslabel.cpp 6.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/***************************************************************************
                          statuslabel.cpp  -  description
                             -------------------
    begin                : Fri Oct 18 2002
    copyright            : (C) 2002 by Scott Wheeler
    email                : 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.                                   *
 *                                                                         *
 ***************************************************************************/

18 19
#include <kpushbutton.h>
#include <kiconloader.h>
Scott Wheeler's avatar
Scott Wheeler committed
20
#include <klocale.h>
21 22
#include <kdebug.h>

23 24
#include <qlabel.h>
#include <qtooltip.h>
25
#include <qlayout.h>
26

27
#include "statuslabel.h"
28 29 30
#include "filehandle.h"
#include "playlistinterface.h"
#include "tag.h"
31 32 33 34

////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////
35

Scott Wheeler's avatar
Scott Wheeler committed
36 37 38 39 40
StatusLabel::StatusLabel(QWidget *parent, const char *name) :
    QHBox(parent, name),
    mode(PlaylistInfo),
    m_playlistCount(0),
    m_showTimeRemaining(false)
41
{
42
    QFrame *trackAndPlaylist = new QFrame(this);
43
    trackAndPlaylist->setFrameStyle(Box | Sunken);
44
    trackAndPlaylist->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
45 46

    // Make sure that we have enough of a margin to suffice for the borders,
47
    // hence the "lineWidth() * 2"
Scott Wheeler's avatar
Scott Wheeler committed
48 49 50
    QHBoxLayout *trackAndPlaylistLayout = new QHBoxLayout(trackAndPlaylist,
							  trackAndPlaylist->lineWidth() * 2,
							  5, "trackAndPlaylistLayout");
51
    trackAndPlaylistLayout->addSpacing(5);
52

53 54 55 56 57
    m_playlistLabel = new QLabel(trackAndPlaylist, "playlistLabel");
    trackAndPlaylistLayout->addWidget(m_playlistLabel);
    m_playlistLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_playlistLabel->setTextFormat(PlainText);
    m_playlistLabel->setAlignment(AlignLeft | AlignVCenter);
58

59 60 61 62 63
    m_trackLabel = new QLabel(trackAndPlaylist, "trackLabel");
    trackAndPlaylistLayout->addWidget(m_trackLabel);
    m_trackLabel->setAlignment(AlignRight | AlignVCenter);
    m_trackLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_trackLabel->setTextFormat(PlainText);
64

65
    trackAndPlaylistLayout->addSpacing(5);
66

67
    m_itemTimeLabel = new QLabel(this);
68
    QFontMetrics fontMetrics(font());
69 70 71 72 73
    m_itemTimeLabel->setAlignment(AlignCenter);
    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);
74 75 76

    setItemTotalTime(0);
    setItemCurrentTime(0);
77 78 79 80 81 82 83 84 85 86

    QHBox *jumpBox = new QHBox(this);
    jumpBox->setFrameStyle(Box | Sunken);
    jumpBox->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum);

    QPushButton *jumpButton = new QPushButton(jumpBox);
    jumpButton->setPixmap(SmallIcon("up"));
    jumpButton->setFlat(true);

    QToolTip::add(jumpButton, i18n("Jump to the currently playing item"));
87
    connect(jumpButton, SIGNAL(clicked()), this, SIGNAL(jumpButtonClicked()));
88 89

    installEventFilter(this);
90 91 92 93 94 95 96
}

StatusLabel::~StatusLabel()
{

}

97
void StatusLabel::setPlaylistInfo(const QString &name, int count, int totalTime)
98
{
99
    m_playlistName = name;
100 101
    m_playlistCount = count;
    m_playlistTotalTime = totalTime;
102

103 104
    if(mode != PlaylistInfo)
	return;
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    m_playlistLabel->setText(m_playlistName);

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

    QString time;

    if(days > 0) {
	time = i18n("1 day", "%n days", days);
	time.append(" ");
    }

    if(days > 0 || hours > 0)
	time.append(QString().sprintf("%1d:%02d:%02d", hours, minutes, seconds));
    else
	time.append(QString().sprintf("%1d:%02d", minutes, seconds));

    m_trackLabel->setText(i18n("1 item", "%n items", count) + " - " + time);
126 127
}

128
void StatusLabel::setPlaylistTime(int totalTime)
129
{
130 131
    setPlaylistInfo(m_playlistName, m_playlistCount, totalTime);
}
132

133 134 135
void StatusLabel::setPlaylistCount(int c)
{
    setPlaylistInfo(m_playlistName, c, m_playlistTotalTime);
136 137
}

138
void StatusLabel::setPlayingItemInfo(const FileHandle &file, const PlaylistInterface *playlist)
139
{
140 141
    mode = PlayingItemInfo;

142 143 144 145
    QString text = file.tag()->artist() + " - " + file.tag()->title();

    m_trackLabel->setText(text);
    m_playlistLabel->setText(playlist->name().simplifyWhiteSpace());
146 147
}

148 149
void StatusLabel::clear()
{
150 151
    m_playlistLabel->clear();
    m_trackLabel->clear();
152 153
    setItemTotalTime(0);
    setItemCurrentTime(0);
154

155
    mode = PlaylistInfo;
156

157
    setPlaylistInfo(m_playlistName, m_playlistCount, m_playlistTotalTime);
158 159 160 161 162 163
}

////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

164
void StatusLabel::updateTime()
165
{
166 167 168
    int minutes;
    int seconds;

169 170 171
    if(m_showTimeRemaining) {
	minutes = int((m_itemTotalTime - m_itemCurrentTime) / 60);
	seconds = (m_itemTotalTime - m_itemCurrentTime) % 60;
172 173
    }
    else {
174 175
	minutes = int(m_itemCurrentTime / 60);
	seconds = m_itemCurrentTime % 60;
176 177
    }

178 179
    int totalMinutes = int(m_itemTotalTime / 60);
    int totalSeconds = m_itemTotalTime % 60;
180

Scott Wheeler's avatar
Scott Wheeler committed
181 182
    QString timeString = formatTime(minutes, seconds) +  " / " +
	formatTime(totalMinutes, totalSeconds);
183
    m_itemTimeLabel->setText(timeString);
184
}
185

186 187 188
bool StatusLabel::eventFilter(QObject *o, QEvent *e)
{
    if(!o || !e)
189
	return false;
190

191
    QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(e);
192 193 194 195
    if(mouseEvent &&
       mouseEvent->state() == LeftButton &&
       mouseEvent->button() == LeftButton)
    {
196

197 198
	if(o == m_itemTimeLabel) {
	    m_showTimeRemaining = !m_showTimeRemaining;
199 200 201
	    updateTime();
	}
	else
202
	    emit(jumpButtonClicked());
203

204
	return true;
205
    }
206
    return false;
207 208
}

209 210 211 212 213 214 215 216
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;
217
    return m + ":" + s;
218 219
}

220
#include "statuslabel.moc"