summaryrefslogtreecommitdiffstats
path: root/juk/statuslabel.cpp
blob: 951604cdcaa5251ac59aeda0767da3fa04ae1f9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/***************************************************************************
    begin                : Fri Oct 18 2002
    copyright            : (C) 2002 - 2004 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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kaction.h>
#include <kpushbutton.h>
#include <kiconloader.h>
#include <ksqueezedtextlabel.h> 
#include <klocale.h>
#include <kdebug.h>

#include <tqtooltip.h>
#include <tqlayout.h>

#include "statuslabel.h"
#include "filehandle.h"
#include "playlistinterface.h"
#include "actioncollection.h"
#include "tag.h"

using namespace ActionCollection;

////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

StatusLabel::StatusLabel(PlaylistInterface *playlist, TQWidget *parent, const char *name) :
    TQHBox(parent, name),
    PlaylistObserver(playlist),
    m_showTimeRemaining(false)
{
    TQFrame *trackAndPlaylist = new TQFrame(this);
    trackAndPlaylist->setFrameStyle(Box | Sunken);
    trackAndPlaylist->tqsetSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);

    // Make sure that we have enough of a margin to suffice for the borders,
    // hence the "lineWidth() * 2"
    TQHBoxLayout *trackAndPlaylistLayout = new TQHBoxLayout(trackAndPlaylist,
                                                          trackAndPlaylist->lineWidth() * 2,
                                                          5, "trackAndPlaylistLayout");
    trackAndPlaylistLayout->addSpacing(5);

    m_playlistLabel = new KSqueezedTextLabel(trackAndPlaylist, "playlistLabel");
    trackAndPlaylistLayout->addWidget(m_playlistLabel);
    m_playlistLabel->tqsetSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);
    m_playlistLabel->setTextFormat(PlainText);
    m_playlistLabel->tqsetAlignment(AlignLeft | AlignVCenter);

    m_trackLabel = new KSqueezedTextLabel(trackAndPlaylist, "trackLabel");
    trackAndPlaylistLayout->addWidget(m_trackLabel);
    m_trackLabel->tqsetAlignment(AlignRight | AlignVCenter);
    m_trackLabel->tqsetSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);
    m_trackLabel->setTextFormat(PlainText);

    trackAndPlaylistLayout->addSpacing(5);

    m_itemTimeLabel = new TQLabel(this);
    TQFontMetrics fontMetrics(font());
    m_itemTimeLabel->tqsetAlignment(AlignCenter);
    m_itemTimeLabel->setMinimumWidth(fontMetrics.boundingRect("000:00 / 000:00").width());
    m_itemTimeLabel->tqsetSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Expanding);
    m_itemTimeLabel->setFrameStyle(Box | Sunken);
    m_itemTimeLabel->installEventFilter(this);

    setItemTotalTime(0);
    setItemCurrentTime(0);

    TQHBox *jumpBox = new TQHBox(this);
    jumpBox->setFrameStyle(Box | Sunken);
    jumpBox->tqsetSizePolicy(TQSizePolicy::Maximum, TQSizePolicy::Minimum);

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

    TQToolTip::add(jumpButton, i18n("Jump to the currently playing item"));
    connect(jumpButton, TQT_SIGNAL(clicked()), action("showPlaying"), TQT_SLOT(activate()));

    installEventFilter(this);

    updateData();
}

StatusLabel::~StatusLabel()
{

}

void StatusLabel::updateCurrent()
{
    if(playlist()->playing()) {
        FileHandle file = playlist()->currentFile();

        TQString mid =  file.tag()->artist().isEmpty() || file.tag()->title().isEmpty()
            ? TQString() : TQString(" - ");

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

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

void StatusLabel::updateData()
{
    updateCurrent();

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

        int time = playlist()->time();

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

        TQString timeString;

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

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

        m_playlistLabel->setText(playlist()->name());
        m_trackLabel->setText(i18n("1 item", "%n items", playlist()->count()) + " - " + timeString);
    }
}

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

void StatusLabel::updateTime()
{
    int minutes;
    int seconds;

    if(m_showTimeRemaining) {
        minutes = int((m_itemTotalTime - m_itemCurrentTime) / 60);
        seconds = (m_itemTotalTime - m_itemCurrentTime) % 60;
    }
    else {
        minutes = int(m_itemCurrentTime / 60);
        seconds = m_itemCurrentTime % 60;
    }

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

    TQString timeString = formatTime(minutes, seconds) +  " / " +
        formatTime(totalMinutes, totalSeconds);
    m_itemTimeLabel->setText(timeString);
}

bool StatusLabel::eventFilter(TQObject *o, TQEvent *e)
{
    if(!o || !e)
        return false;

    TQMouseEvent *mouseEvent = TQT_TQMOUSEEVENT(e);
    if(e->type() == TQEvent::MouseButtonRelease &&
       mouseEvent->button() == Qt::LeftButton)
    {
        if(TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(m_itemTimeLabel)) {
            m_showTimeRemaining = !m_showTimeRemaining;
            updateTime();
        }
        else
            action("showPlaying")->activate();

        return true;
    }
    return false;
}

TQString StatusLabel::formatTime(int minutes, int seconds) // static
{
    TQString m = TQString::number(minutes);
    if(m.length() == 1)
        m = "0" + m;
    TQString s = TQString::number(seconds);
    if(s.length() == 1)
        s = "0" + s;
    return m + ":" + s;
}

#include "statuslabel.moc"