24 #include "kwidgetjobtracker_p.h"
29 #include <QProgressBar>
30 #include <QVBoxLayout>
31 #include <QGridLayout>
47 void KWidgetJobTracker::Private::_k_showProgressWidget()
49 if (progressWidgetsToBeShown.isEmpty()) {
53 KJob *job = progressWidgetsToBeShown.dequeue();
57 QWidget *widget = q->widget(job);
76 return d->progressWidget.value(job, 0);
81 Private::ProgressWidget *vi =
new Private::ProgressWidget(job,
this, d->parent);
82 vi->jobRegistered =
true;
83 vi->setAttribute(Qt::WA_DeleteOnClose);
84 d->progressWidget.insert(job, vi);
85 d->progressWidgetsToBeShown.enqueue(job);
89 QTimer::singleShot(500,
this, SLOT(_k_showProgressWidget()));
96 d->progressWidgetsToBeShown.removeAll(job);
97 KWidgetJobTracker::Private::ProgressWidget *pWidget = d->progressWidget.value(job, 0);
102 pWidget->jobRegistered =
false;
108 KWidgetJobTracker::Private::ProgressWidget *pWidget = d->progressWidget.value(job, 0);
113 return pWidget->keepOpenCheck->isChecked();
118 KWidgetJobTracker::Private::ProgressWidget *pWidget = d->progressWidget.value(job, 0);
123 pWidget->infoMessage(plain, rich);
130 KWidgetJobTracker::Private::ProgressWidget *pWidget = d->progressWidget.value(job, 0);
135 pWidget->description(title, field1, field2);
140 KWidgetJobTracker::Private::ProgressWidget *pWidget = d->progressWidget.value(job, 0);
145 pWidget->totalAmount(unit, amount);
150 KWidgetJobTracker::Private::ProgressWidget *pWidget = d->progressWidget.value(job, 0);
155 pWidget->processedAmount(unit, amount);
160 KWidgetJobTracker::Private::ProgressWidget *pWidget = d->progressWidget.value(job, 0);
165 pWidget->percent(percent);
170 KWidgetJobTracker::Private::ProgressWidget *pWidget = d->progressWidget.value(job, 0);
175 pWidget->speed(value);
180 KWidgetJobTracker::Private::ProgressWidget *pWidget = d->progressWidget.value(job, 0);
185 pWidget->slotClean();
190 KWidgetJobTracker::Private::ProgressWidget *pWidget = d->progressWidget.value(job, 0);
195 pWidget->suspended();
200 KWidgetJobTracker::Private::ProgressWidget *pWidget = d->progressWidget.value(job, 0);
208 void KWidgetJobTracker::Private::ProgressWidget::ref()
213 void KWidgetJobTracker::Private::ProgressWidget::deref()
220 if (!keepOpenCheck->isChecked()) {
228 void KWidgetJobTracker::Private::ProgressWidget::closeNow()
241 if (tracker->d->progressWidget[job] ==
this) {
242 tracker->d->progressWidget.remove(job);
243 tracker->d->progressWidgetsToBeShown.removeAll(job);
247 bool KWidgetJobTracker::Private::ProgressWidget::eventFilter(
QObject *watched, QEvent *event)
251 if ((watched == sourceEdit || watched == destEdit) && event->type() == QEvent::ContextMenu) {
253 watched->event(event);
258 return QWidget::eventFilter(watched, event);
261 void KWidgetJobTracker::Private::ProgressWidget::infoMessage(
const QString &plain,
const QString &)
263 speedLabel->setText(plain);
264 speedLabel->setAlignment(speedLabel->alignment() & ~Qt::TextWordWrap);
267 void KWidgetJobTracker::Private::ProgressWidget::description(
const QString &title,
271 setWindowTitle(title);
274 sourceInvite->setText(
i18nc(
"%1 is the label, we add a ':' to it",
"%1:", field1.first));
275 sourceEdit->setText(field1.second);
277 if (field2.first.isEmpty()) {
278 setDestVisible(
false);
280 setDestVisible(
true);
281 checkDestination(
KUrl(field2.second));
282 destInvite->setText(
i18nc(
"%1 is the label, we add a ':' to it",
"%1:", field2.first));
283 destEdit->setText(field2.second);
287 void KWidgetJobTracker::Private::ProgressWidget::totalAmount(KJob::Unit unit, qulonglong amount)
292 totalSizeKnown =
true;
294 if (totalSize == amount)
297 if (startTime.isNull())
302 if (totalFiles == amount)
308 case KJob::Directories:
309 if (totalDirs == amount)
317 void KWidgetJobTracker::Private::ProgressWidget::processedAmount(KJob::Unit unit, qulonglong amount)
324 if (processedSize == amount)
326 processedSize = amount;
328 if (totalSizeKnown) {
329 tmp =
i18np(
"%2 of %3 complete",
"%2 of %3 complete",
336 sizeLabel->setText(tmp);
338 progressBar->setValue(amount);
341 case KJob::Directories:
342 if (processedDirs == amount)
344 processedDirs = amount;
346 tmp =
i18np(
"%2 / %1 folder",
"%2 / %1 folders", totalDirs, processedDirs);
348 tmp +=
i18np(
"%2 / %1 file",
"%2 / %1 files", totalFiles, processedFiles);
349 progressLabel->setText(tmp);
353 if (processedFiles == amount)
355 processedFiles = amount;
358 tmp =
i18np(
"%2 / %1 folder",
"%2 / %1 folders", totalDirs, processedDirs);
361 tmp +=
i18np(
"%2 / %1 file",
"%2 / %1 files", totalFiles, processedFiles);
362 progressLabel->setText(tmp);
366 void KWidgetJobTracker::Private::ProgressWidget::percent(
unsigned long percent)
370 if (totalSizeKnown) {
371 title +=
i18n(
"%1% of %2", percent,
373 }
else if (totalFiles) {
374 title +=
i18np(
"%2% of 1 file",
"%2% of %1 files", totalFiles, percent);
376 title +=
i18n(
"%1%", percent);
381 progressBar->setMaximum(100);
382 progressBar->setValue(percent);
383 setWindowTitle(title);
386 void KWidgetJobTracker::Private::ProgressWidget::speed(
unsigned long value)
389 speedLabel->setText(
i18n(
"Stalled"));
392 if (totalSizeKnown) {
393 const int remaining = 1000*(totalSize - processedSize)/value;
394 speedLabel->setText(
i18np(
"%2/s (%3 remaining)",
"%2/s (%3 remaining)", remaining, speedStr,
397 speedLabel->setText(
i18nc(
"speed in bytes per second",
"%1/s", speedStr));
402 void KWidgetJobTracker::Private::ProgressWidget::slotClean()
406 openFile->setEnabled(
true);
407 if (!totalSizeKnown || totalSize < processedSize)
408 totalSize = processedSize;
409 processedAmount(KJob::Bytes, totalSize);
410 keepOpenCheck->setEnabled(
false);
411 pauseButton->setEnabled(
false);
412 if (!startTime.isNull()) {
413 int s = startTime.elapsed();
416 speedLabel->setText(
i18n(
"%1/s (done)",
421 void KWidgetJobTracker::Private::ProgressWidget::suspended()
423 pauseButton->setText(
i18n(
"&Resume"));
424 suspendedProperty =
true;
427 void KWidgetJobTracker::Private::ProgressWidget::resumed()
429 pauseButton->setText(
i18n(
"&Pause"));
430 suspendedProperty =
false;
433 void KWidgetJobTracker::Private::ProgressWidget::closeEvent(QCloseEvent *event)
435 if (jobRegistered && tracker->stopOnClose(job)) {
436 tracker->slotStop(job);
439 QWidget::closeEvent(event);
442 void KWidgetJobTracker::Private::ProgressWidget::init()
449 QVBoxLayout *topLayout =
new QVBoxLayout(
this);
451 QGridLayout *grid =
new QGridLayout();
452 topLayout->addLayout(grid);
455 sourceInvite =
new QLabel(
i18nc(
"The source url of a job",
"Source:"),
this);
456 grid->addWidget(sourceInvite, 0, 0);
459 sourceEdit->setTextInteractionFlags(Qt::TextSelectableByMouse);
460 sourceEdit->installEventFilter(
this);
461 grid->addWidget(sourceEdit, 0, 2);
463 destInvite =
new QLabel(
i18nc(
"The destination url of a job",
"Destination:"),
this);
464 grid->addWidget(destInvite, 1, 0);
467 destEdit->setTextInteractionFlags(Qt::TextSelectableByMouse);
468 destEdit->installEventFilter(
this);
469 grid->addWidget(destEdit, 1, 2);
471 QHBoxLayout *progressHBox =
new QHBoxLayout();
472 topLayout->addLayout(progressHBox);
474 progressBar =
new QProgressBar(
this);
475 progressBar->setMaximum(0);
476 progressHBox->addWidget(progressBar);
478 suspendedProperty =
false;
481 QHBoxLayout *hBox =
new QHBoxLayout();
482 topLayout->addLayout(hBox);
485 arrowButton->setMaximumSize(
QSize(32,25));
486 arrowButton->setIcon(
KIcon(
"arrow-down"));
487 arrowButton->setToolTip(
i18n(
"Click this to expand the dialog, to show details"));
488 arrowState = Qt::DownArrow;
489 connect(arrowButton, SIGNAL(clicked()),
this, SLOT(_k_arrowToggled()));
490 hBox->addWidget(arrowButton);
494 topLayout->addWidget(separator1);
496 sizeLabel =
new QLabel(
this);
497 hBox->addWidget(sizeLabel, 0, Qt::AlignLeft);
499 resumeLabel =
new QLabel(
this);
500 hBox->addWidget(resumeLabel);
503 connect(pauseButton, SIGNAL(clicked()),
this, SLOT(_k_pauseResumeClicked()));
504 hBox->addWidget(pauseButton);
506 hBox =
new QHBoxLayout();
507 topLayout->addLayout(hBox);
509 speedLabel =
new QLabel(
this);
510 hBox->addWidget(speedLabel, 1);
513 hBox =
new QHBoxLayout();
514 topLayout->addLayout(hBox);
516 progressLabel =
new QLabel(
this);
517 progressLabel->setAlignment(Qt::AlignLeft);
518 hBox->addWidget(progressLabel);
519 progressLabel->hide();
521 keepOpenCheck =
new QCheckBox(
i18n(
"&Keep this window open after transfer is complete"),
this);
522 connect(keepOpenCheck, SIGNAL(toggled(
bool)),
this, SLOT(_k_keepOpenToggled(
bool)));
523 topLayout->addWidget(keepOpenCheck);
524 keepOpenCheck->hide();
526 hBox =
new QHBoxLayout();
527 topLayout->addLayout(hBox);
530 connect(openFile, SIGNAL(clicked()),
this, SLOT(_k_openFile()));
531 hBox->addWidget(openFile);
532 openFile->setEnabled(
false);
536 connect(openLocation, SIGNAL(clicked()),
this, SLOT(_k_openLocation()));
537 hBox->addWidget(openLocation);
538 openLocation->hide();
543 connect(cancelClose, SIGNAL(clicked()),
this, SLOT(_k_stop()));
544 hBox->addWidget(cancelClose);
547 setMaximumHeight(sizeHint().height());
549 setWindowTitle(
i18n(
"Progress Dialog"));
552 void KWidgetJobTracker::Private::ProgressWidget::showTotals()
557 if (processedFiles == 0 && processedDirs == 0)
562 tmps =
i18np(
"%1 folder",
"%1 folders", totalDirs) +
" ";
563 tmps +=
i18np(
"%1 file",
"%1 files", totalFiles);
564 progressLabel->setText( tmps );
568 void KWidgetJobTracker::Private::ProgressWidget::setDestVisible(
bool visible)
581 destInvite->setText(
QString() );
582 destEdit->setText(
QString() );
584 setMaximumHeight(sizeHint().height());
587 void KWidgetJobTracker::Private::ProgressWidget::checkDestination(
const KUrl &dest)
594 for (QStringList::ConstIterator it = tmpDirs.begin() ; ok && it != tmpDirs.end() ; ++it)
595 if (path.contains(*it))
601 openLocation->show();
602 keepOpenCheck->show();
603 setMaximumHeight(sizeHint().height());
608 void KWidgetJobTracker::Private::ProgressWidget::_k_keepOpenToggled(
bool keepOpen)
617 void KWidgetJobTracker::Private::ProgressWidget::_k_openFile()
619 QProcess::startDetached(
"kde-open",
QStringList() << location.prettyUrl());
622 void KWidgetJobTracker::Private::ProgressWidget::_k_openLocation()
624 KUrl dirLocation(location);
625 dirLocation.setFileName(
QString());
626 QProcess::startDetached(
"kde-open",
QStringList() << dirLocation.prettyUrl());
629 void KWidgetJobTracker::Private::ProgressWidget::_k_pauseResumeClicked()
631 if (jobRegistered && !suspendedProperty) {
632 tracker->slotSuspend(job);
633 }
else if (jobRegistered) {
634 tracker->slotResume(job);
638 void KWidgetJobTracker::Private::ProgressWidget::_k_stop()
641 tracker->slotStop(job);
646 void KWidgetJobTracker::Private::ProgressWidget::_k_arrowToggled()
648 if (arrowState == Qt::DownArrow) {
650 progressLabel->show();
652 arrowButton->setIcon(
KIcon(
"arrow-up"));
653 arrowButton->setToolTip(
i18n(
"Click this to collapse the dialog, to hide details"));
654 arrowState = Qt::UpArrow;
657 progressLabel->hide();
659 arrowButton->setIcon(
KIcon(
"arrow-down"));
660 arrowButton->setToolTip(
i18n(
"Click this to expand the dialog, to show details"));
661 arrowState = Qt::DownArrow;
663 setMaximumHeight(sizeHint().height());
666 #include "kwidgetjobtracker.moc"
667 #include "kwidgetjobtracker_p.moc"
KGuiItem cancel()
Returns the 'Cancel' gui item.
QString i18n(const char *text)
static KIconLoader * global()
Returns the global icon loader initialized with the global KComponentData.
QString i18np(const char *sing, const char *plur, const A1 &a1)
QString toLocalFile(AdjustPathOption trailing=LeaveTrailingSlash) const
QString i18nc(const char *ctxt, const char *text)
Standard horizontal or vertical separator.
static int spacingHint()
Returns the number of pixels that should be used between widgets inside a dialog according to the KDE...
A wrapper around QIcon that provides KDE icon features.
QString formatByteSize(double size) const
A replacement for QLabel that squeezes its text.
static void setIcons(WId win, const QPixmap &icon, const QPixmap &miniIcon)
Sets an icon and a miniIcon on window win.
QStringList resourceDirs(const char *type) const
KGuiItem close()
Returns the 'Close' gui item.
KGuiItem ok()
Returns the 'Ok' gui item.
KAction * close(const QObject *recvr, const char *slot, QObject *parent)
Close the current document.