statuslabel.cpp 6.75 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 <klocale.h>
21 22 23
#include <kdebug.h>

#include <qevent.h>
24
#include <qstylesheet.h>
25 26 27
#include <qlabel.h>
#include <qtooltip.h>
#include <qfontmetrics.h>
28
#include <qlayout.h>
29

30 31 32 33
#include "statuslabel.h"
#include "playlistitem.h"
#include "playlist.h"
#include "playlistbox.h"
34 35 36 37 38
#include "playlistsplitter.h"

////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////
39

40
StatusLabel::StatusLabel(QWidget *parent, const char *name) : QHBox(parent, name), playingItem(0), showTimeRemaining(false)
41
{
42
    QFrame *trackAndPlaylist = new QFrame(this);
43
    trackAndPlaylist->setFrameStyle(Box | Sunken);
44 45 46 47 48 49
    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, trackAndPlaylist->lineWidth() * 2, 5, "trackAndPlaylistLayout");
    trackAndPlaylistLayout->addSpacing(5);
50 51

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

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

63 64
    trackAndPlaylistLayout->addSpacing(5);
    
65 66 67 68 69 70
    itemTimeLabel = new QLabel(this);
    QFontMetrics fontMetrics(font());
    itemTimeLabel->setAlignment(AlignCenter);
    itemTimeLabel->setMinimumWidth(fontMetrics.boundingRect("000:00 / 000:00").width());
    itemTimeLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
    itemTimeLabel->setFrameStyle(Box | Sunken);
71
    itemTimeLabel->installEventFilter(this);
72 73 74

    setItemTotalTime(0);
    setItemCurrentTime(0);
75 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"));
    connect(jumpButton, SIGNAL(clicked()), this, SLOT(jumpToPlayingItem()));
86 87

    installEventFilter(this);
88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

StatusLabel::~StatusLabel()
{

}

void StatusLabel::setPlayingItem(PlaylistItem *item)
{
    playingItem = item;

    if(item) {
	Playlist *p = static_cast<Playlist *>(item->listView());
	if(p && p->playlistBoxItem()) {
102

103 104 105
	    QString playlist = QStyleSheet::escape(p->playlistBoxItem()->text().simplifyWhiteSpace());
	    QString artist = QStyleSheet::escape(item->text(PlaylistItem::ArtistColumn).simplifyWhiteSpace());
    	    QString track = QStyleSheet::escape(item->text(PlaylistItem::TrackColumn).simplifyWhiteSpace());
106

107
	    playlistLabel->setText(playlist);
108 109 110 111 112 113 114
	    
	    QString label;
	    if(artist.isEmpty() || track.isEmpty())
		label = artist + track;
	    else
		label = artist + " - " + track;

115 116 117
	    trackLabel->setText(label);
	}
	else
118
	    clear();
119 120
    }
    else
121
	clear();
122 123
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137
void StatusLabel::setPlaylistName(const QString &t)
{
    playlistName = t;
    if(!playingItem)
	playlistLabel->setText(playlistName);
}

void StatusLabel::setPlaylistCount(int c)
{
    playlistCount = c;
    if(!playingItem)
	trackLabel->setText(QString::number(playlistCount) + " " + i18n("Item(s)"));
}

138 139
void StatusLabel::clear()
{
140
    playlistLabel->clear();
141
    trackLabel->clear();
142
    playingItem = 0;
143 144
    setItemTotalTime(0);
    setItemCurrentTime(0);
145 146 147
    
    setPlaylistName(playlistName);
    setPlaylistCount(playlistCount);
148 149
}

150
void StatusLabel::setItemTotalTime(int time)
151
{
152
    itemTotalTime = time;
153 154
}

155
void StatusLabel::setItemCurrentTime(int time)
156
{
157 158
    itemCurrentTime = time;
    updateTime();
159 160 161 162 163 164
}

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

165
void StatusLabel::updateTime()
166
{
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    int minutes;
    int seconds;

    if(showTimeRemaining) {
	minutes = int((itemTotalTime - itemCurrentTime) / 60);
	seconds = (itemTotalTime - itemCurrentTime) % 60;
    }
    else {
	minutes = int(itemCurrentTime / 60);
	seconds = itemCurrentTime % 60;
    }

    int totalMinutes = int(itemTotalTime / 60);
    int totalSeconds = itemTotalTime % 60;

    QString timeString = formatTime(minutes, seconds) +  " / " + formatTime(totalMinutes, totalSeconds);
    itemTimeLabel->setText(timeString);    
184
}
185

186 187 188 189 190 191 192 193 194 195 196
QString StatusLabel::formatTime(int minutes, int seconds)
{
    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);
}

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
bool StatusLabel::eventFilter(QObject *o, QEvent *e)
{
    if(!o || !e)
	return(false);
    
    QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(e);
    if(mouseEvent && mouseEvent->state() == LeftButton) {

	if(o == itemTimeLabel) {
	    showTimeRemaining = !showTimeRemaining;
	    updateTime();
	}
	else
	    jumpToPlayingItem();

	return(true);
    }
    return(false);
}

217 218 219 220 221 222 223 224
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

void StatusLabel::jumpToPlayingItem() const
{
    if(playingItem)
	PlaylistSplitter::setSelected(playingItem);
225 226 227
}

#include "statuslabel.moc"