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

24
#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
StatusLabel::StatusLabel(QWidget *parent, const char *name) :
    QHBox(parent, name),
    mode(PlaylistInfo),
    m_showTimeRemaining(false)
40
{
41
    QFrame *trackAndPlaylist = new QFrame(this);
42
    trackAndPlaylist->setFrameStyle(Box | Sunken);
43
    trackAndPlaylist->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
44 45

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

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

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

64
    trackAndPlaylistLayout->addSpacing(5);
65

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

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

    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"));
86
    connect(jumpButton, SIGNAL(clicked()), this, SIGNAL(jumpButtonClicked()));
87 88

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

StatusLabel::~StatusLabel()
{

}

96
void StatusLabel::setPlaylist(PlaylistInterface *playlist)
97
{
98 99 100 101
    int days = playlist->time() / (60 * 60 * 24);
    int hours = playlist->time() / (60 * 60) % 24;
    int minutes = playlist->time() / 60 % 60;
    int seconds = playlist->time() % 60;
102 103 104 105 106 107 108 109 110 111 112 113 114

    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));

115 116
    m_playlistLength = i18n("1 item", "%n items", playlist->count()) + " - " + time;
    m_playlistName = playlist->name();
117

118 119 120 121
    if(mode == PlaylistInfo) {
	m_playlistLabel->setText(m_playlistName);
	m_trackLabel->setText(m_playlistLength);
    }
122 123
}

124
void StatusLabel::setPlayingItemInfo(const FileHandle &file, const PlaylistInterface *playlist)
125
{
126 127
    mode = PlayingItemInfo;

128 129 130 131
    QString text = file.tag()->artist() + " - " + file.tag()->title();

    m_trackLabel->setText(text);
    m_playlistLabel->setText(playlist->name().simplifyWhiteSpace());
132 133
}

134 135
void StatusLabel::clear()
{
136 137
    setItemTotalTime(0);
    setItemCurrentTime(0);
138

139
    mode = PlaylistInfo;
140

141 142
    m_playlistLabel->setText(m_playlistName);
    m_trackLabel->setText(m_playlistLength);
143 144 145 146 147 148
}

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

149
void StatusLabel::updateTime()
150
{
151 152 153
    int minutes;
    int seconds;

154 155 156
    if(m_showTimeRemaining) {
	minutes = int((m_itemTotalTime - m_itemCurrentTime) / 60);
	seconds = (m_itemTotalTime - m_itemCurrentTime) % 60;
157 158
    }
    else {
159 160
	minutes = int(m_itemCurrentTime / 60);
	seconds = m_itemCurrentTime % 60;
161 162
    }

163 164
    int totalMinutes = int(m_itemTotalTime / 60);
    int totalSeconds = m_itemTotalTime % 60;
165

Scott Wheeler's avatar
Scott Wheeler committed
166 167
    QString timeString = formatTime(minutes, seconds) +  " / " +
	formatTime(totalMinutes, totalSeconds);
168
    m_itemTimeLabel->setText(timeString);
169
}
170

171 172 173
bool StatusLabel::eventFilter(QObject *o, QEvent *e)
{
    if(!o || !e)
174
	return false;
175

176
    QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(e);
177 178 179 180
    if(mouseEvent &&
       mouseEvent->state() == LeftButton &&
       mouseEvent->button() == LeftButton)
    {
181

182 183
	if(o == m_itemTimeLabel) {
	    m_showTimeRemaining = !m_showTimeRemaining;
184 185 186
	    updateTime();
	}
	else
187
	    emit(jumpButtonClicked());
188

189
	return true;
190
    }
191
    return false;
192 193
}

194 195 196 197 198 199 200 201
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;
202
    return m + ":" + s;
203 204
}

205
#include "statuslabel.moc"