summaryrefslogtreecommitdiffstats
path: root/amarok/src/engine/nmm/HostListItem.cpp
blob: d73b94efbc94848ced2cbdf841020d10797022e2 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* NMM - Network-Integrated Multimedia Middleware
 *
 * Copyright (C) 2005-2006
 *                    NMM work group,
 *                    Computer Graphics Lab,
 *                    Saarland University, Germany
 *                    http://www.networkmultimedia.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include "HostListItem.h"

#include <tqbitmap.h>
#include <tqfont.h>
#include <tqheader.h>
#include <tqpainter.h>
#include <tqwhatsthis.h>

#include <tdeglobal.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <kpixmap.h>
#include <kpixmapeffect.h>
#include <kstandarddirs.h>

#include "debug.h"
#include "HostList.h"
#include "nmm_engine.h"

HostListItem::HostListItem( TQListView *parent, TQString hostname, bool audio, bool video, int volume, int status, bool read_only )
    : TDEListViewItem( parent ),
    m_audio( audio ),
    m_video( video ),
    m_volume( volume ),
    m_status( status ),
    m_read_only( read_only )
{
  setText( HostListItem::Hostname, hostname);

  setPixmap( HostListItem::Status, SmallIcon("info") );
  setText( HostListItem::Status, i18n("Unknown") );
  setPixmap( HostListItem::Playback, SmallIcon("info") );
  setText( HostListItem::Playback, i18n("Unknown") );

  if( 24 /*m_pixmapInset.height()*/ > height() )
    this->setHeight( 24 /*m_pixmapInset.height()*/ );
}

HostListItem::~HostListItem()
{
}

int HostListItem::volumeAtPosition( int x )
{
  if( x > 106 )
    return 100;
  else if ( x < 6 )
    return -100;
  else
    return (x - 56) * 2;
}


void HostListItem::updateColumn( int column ) const
{
  const TQRect r = listView()->itemRect( this );
  if( !r.isValid() )
    return;

  listView()->viewport()->update( listView()->header()->sectionPos( column ) - listView()->contentsX() + 1,
      r.y() + 1,
      listView()->header()->sectionSize( column ) - 2, height() - 2 );
}

void HostListItem::statusToolTip()
{
  TQWhatsThis::display( prettyStatus( m_status ) );
}

TQString HostListItem::prettyStatus( int error )
{
  TQString st;

  debug() << "### ERROR code : " << error << endl;

  st = "<html><body>";

  if(!error)
    st += i18n("So far no status available for this host entry.<br/>Probably this means the host has not been used yet for playback.");


  if( error & NmmEngine::ERROR_PLAYBACKNODE )
    // TODO distinguish between ALSAPlaybackNode and PlaybackNode
    st += i18n("An error appeared during audio playback initialization. Make sure the <b>PlaybackNode</b> is present on your system. If it is present, the command <b>serverregistry -s</b> in a console will list <b>PlaybackNode</b> as <b>available</b>.<br/>");

  if( error & NmmEngine::ERROR_DISPLAYNODE )
    st += i18n("An error appeared during video playback initialization. Make sure the <b>XDisplayNode</b> is present on your system. If it is present, the command <b>serverregistry -s</b> in a console will list <b>XDisplayNode</b> as <b>available</b>.<br/>");

  if( error )
    st += i18n("In general have a look at the <a href=\"http://www.networkmultimedia.org/Download/Binary/index.html#configure\">Configuration and tests</a> instructions.");

  st += "</body></html>";
  return st;
}

void HostListItem::paintCell(TQPainter * p, const TQColorGroup & cg, int column, int width, int align )
{
  TQColorGroup m_cg( cg );

  // TODO: reuse icons?
  if( column == HostListItem::Video )
  {
    if( m_video ) { // video ?
      if( m_read_only )
        setPixmap( HostListItem::Video, SmallIcon("nmm_option_on_readonly")  );
      else
        setPixmap( HostListItem::Video, SmallIcon("nmm_option_on")  );
    }
    else
      if( ! m_read_only)
        setPixmap( HostListItem::Video, SmallIcon("nmm_option_off") );
  }
  else if( column == HostListItem::Audio )
  {
    if( m_audio ) {// audio ?
      if( m_read_only )
        setPixmap( HostListItem::Audio, SmallIcon("nmm_option_on_readonly")  );
      else
        setPixmap( HostListItem::Audio, SmallIcon("nmm_option_on")  );
    }
    else
      if( ! m_read_only)
        setPixmap( HostListItem::Audio, SmallIcon("nmm_option_off") );
  }
  else if( column ==  HostListItem::Status )
  {
    TQFont font( p->font() );
    if( ! m_status  ) // Unknown
    {
      font.setBold( false );
      setText( HostListItem::Status , i18n("Unknown") );
    }
    else if( m_status == NmmEngine::STATUS_OK )
    {
      font.setBold( false );
      m_cg.setColor( TQColorGroup::Text, TQt::darkGreen );
      setText( HostListItem::Status , i18n("OK") );
    }
    else { // error
      font.setBold( true );
      m_cg.setColor( TQColorGroup::Text, TQt::red );
      setText( HostListItem::Status , i18n("Failed") );
    }
    p->setFont( font );
  }
  else if( column == HostListItem::Volume )
  {
    TQPixmap buf( width, height() );
    TQColor bg = listView()->viewport()->backgroundColor();
    buf.fill( bg );

    bitBlt( &buf, 0, 0, pixmapVolume( PixInset ) );

    // Draw gradient
    static int padding = 7;
    static int vol; // pixelposition
    if( this == ((HostList*)listView())->m_hoveredVolume )
    {
      vol = listView()->viewportToContents( listView()->viewport()->mapFromGlobal( TQCursor::pos() ) ).x();
      vol -= listView()->header()->sectionPos( HostListItem::Volume );
    }
    else
      vol = (m_volume / 2) + 56;

    //std::cerr << "rel vol = " << vol << std::endl;

    static int center = 56;
    if( vol > center ) {
      bitBlt( &buf, 0, 0, pixmapVolume( PixRight ), 0, 0, vol + 1 /* TODO: why + 1??? */ );
    }
    else if ( vol < center ) {
      bitBlt( &buf, vol, 0, pixmapVolume( PixLeft ), vol, 0, 56 );
    }
    else
    {}

    // Calculate actual volume string from pixelposition
    vol = volumeAtPosition( vol );
    TQString vol_text;
    if( vol > 0 )
      vol_text = "+";
    vol_text += TQString::number( vol );
    vol_text += '%';

    // Draw relative volume number
    TQPainter p_number(&buf);
    p_number.setPen( cg.buttonText() );
    TQFont font;
    font.setPixelSize( 9 );
    p_number.setFont( font );
    const TQRect rect( 40, 0, 34, 15 );
    p_number.drawText( rect, TQt::AlignRight | TQt::AlignVCenter, vol_text );
    p_number.end();
    //bitBlt( p_number.device(), 0, 0, &buf );

    p->drawPixmap( 0, 0, buf );
    return;
  }

  TDEListViewItem::paintCell(p, m_cg, column, width, align);
}

TQPixmap* HostListItem::pixmapVolume( int type )
{
  if( type == PixInset )
  {
    static TQPixmap m_pixmapInset( locate( "data", "amarok/images/nmm-volume-inset.png" ) );
    return &m_pixmapInset;
  }
  else if( type == PixRight )
  {
    static TQPixmap m_pixmapGradientRight = generateGradient( PixRight );
    return &m_pixmapGradientRight;
  }
  else if ( type == PixLeft )
  {
    static TQPixmap m_pixmapGradientLeft = generateGradient( PixLeft );
    return &m_pixmapGradientLeft;
  }

  return 0;
}

TQPixmap HostListItem::generateGradient( int type )
{
  TQPixmap temp;

  if( type == PixRight )
    temp = TQPixmap( locate( "data", "amarok/images/nmm-gradient-right.png" ) );
  else // PixLeft
    temp = TQPixmap( locate( "data", "amarok/images/nmm-gradient-left.png" ) );
  const TQBitmap mask( temp.createHeuristicMask() );

  KPixmap result = TQPixmap( 113, 24 );
  if( type == PixRight)
    KPixmapEffect::gradient( result, listView()->colorGroup().background(), listView()->colorGroup().highlight(), KPixmapEffect::HorizontalGradient );
  else
    KPixmapEffect::gradient( result, listView()->colorGroup().highlight(), listView()->colorGroup().background(), KPixmapEffect::HorizontalGradient );

  result.setMask( mask);
  return result;
}