summaryrefslogtreecommitdiffstats
path: root/kio/kio/statusbarprogress.cpp
blob: 777f96e58843d0e5f32fba9762b7a42d85d85184 (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
/* This file is part of the KDE libraries
   Copyright (C) 2000 Matej Koss <koss@miesto.sk>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <tqtooltip.h>
#include <tqlayout.h>
#include <tqwidgetstack.h>
#include <tqpushbutton.h>
#include <tqlabel.h>

#include <kapplication.h>
#include <klocale.h>
#include <kdebug.h>
#include <kprogress.h>

#include "jobclasses.h"
#include "statusbarprogress.h"

namespace KIO {

StatusbarProgress::StatusbarProgress( TQWidget* parent, bool button )
  : ProgressBase( parent ) {

  m_bShowButton = button;

  // only clean this dialog
  setOnlyClean(true);
  // TODO : is this really needed ?
  setStopOnClose(false);

  int w = fontMetrics().width( " 999.9 kB/s 00:00:01 " ) + 8;
  box = new TQHBoxLayout( this, 0, 0 );

  m_pButton = new TQPushButton( "X", this );
  box->addWidget( m_pButton  );
  stack = new TQWidgetStack( this );
  box->addWidget( stack );
  connect( m_pButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotStop() ) );

  m_pProgressBar = new KProgress( this );
  m_pProgressBar->setFrameStyle( TQFrame::Box | TQFrame::Raised );
  m_pProgressBar->setLineWidth( 1 );
  m_pProgressBar->setBackgroundMode( TQWidget::PaletteBackground );
  m_pProgressBar->installEventFilter( this );
  m_pProgressBar->setMinimumWidth( w );
  stack->addWidget( m_pProgressBar, 1 );

  m_pLabel = new TQLabel( "", this );
  m_pLabel->setAlignment( AlignHCenter | AlignVCenter );
  m_pLabel->installEventFilter( this );
  m_pLabel->setMinimumWidth( w );
  stack->addWidget( m_pLabel, 2 );
  setMinimumSize( sizeHint() );

  mode = None;
  setMode();
}


void StatusbarProgress::setJob( KIO::Job *job )
{
  ProgressBase::setJob( job );

  mode = Progress;
  setMode();
}


void StatusbarProgress::setMode() {
  switch ( mode ) {
  case None:
    if ( m_bShowButton ) {
      m_pButton->hide();
    }
    stack->hide();
    break;

  case Label:
    if ( m_bShowButton ) {
      m_pButton->show();
    }
    stack->show();
    stack->raiseWidget( m_pLabel );
    break;

  case Progress:
    if ( m_bShowButton ) {
      m_pButton->show();
    }
    stack->show();
    stack->raiseWidget( m_pProgressBar );
    break;
  }
}


void StatusbarProgress::slotClean() {
  // we don't want to delete this widget, only clean
  m_pProgressBar->setValue( 0 );
  m_pLabel->clear();

  mode = None;
  setMode();
}


void StatusbarProgress::slotTotalSize( KIO::Job*, KIO::filesize_t size ) {
  m_iTotalSize = size;  // size is measured in bytes
}

void StatusbarProgress::slotPercent( KIO::Job*, unsigned long percent ) {
  m_pProgressBar->setValue( percent );
}


void StatusbarProgress::slotSpeed( KIO::Job*, unsigned long speed ) {
  if ( speed == 0 ) { // spped is measured in bytes-per-second
    m_pLabel->setText( i18n( " Stalled ") );
  } else {
    m_pLabel->setText( i18n( " %1/s ").arg( KIO::convertSize( speed )) );
  }
}


bool StatusbarProgress::eventFilter( TQObject *, TQEvent *ev ) {
  if ( ! m_pJob ) { // don't react when there isn't any job doing IO
    return true;
  }

  if ( ev->type() == TQEvent::MouseButtonPress ) {
    TQMouseEvent *e = (TQMouseEvent*)ev;

    if ( e->button() == Qt::LeftButton ) {    // toggle view on left mouse button
      if ( mode == Label ) {
	mode = Progress;
      } else if ( mode == Progress ) {
	mode = Label;
      }
      setMode();
      return true;

    }
  }

  return false;
}

void StatusbarProgress::virtual_hook( int id, void* data )
{ ProgressBase::virtual_hook( id, data ); }

} /* namespace */
#include "statusbarprogress.moc"