コード例 #1
0
int KST::vectorToFile(KstVectorPtr v, QFile *f) {
int rc = 0;
#define BSIZE 128
char buf[BSIZE];
int _size = v->length();
double *_v = v->value();
register int modval;
KProgressDialog *kpd = new KProgressDialog(0L, "vector save", i18n("Saving Vector"), i18n("Saving vector %1...").arg(v->tagName()));

  kpd->setAllowCancel(false);

  kpd->progressBar()->setTotalSteps(_size);
  kpd->show();

  modval = QMAX(_size/100, 100);

  for (int i = 0; i < _size; i++) {
    int l = snprintf(buf, BSIZE, "%d %g\n", i, _v[i]);
    f->writeBlock(buf, l);
    if (i % 100 == 0) {
      kpd->progressBar()->setProgress(i);
      kapp->processEvents();
    }
  }
  kpd->progressBar()->setProgress(_size);

delete kpd;

#undef BSIZE
return rc;
}
コード例 #2
0
ファイル: backup.cpp プロジェクト: KrissN/basket
void BackupDialog::restore()
{
    // Get last backup folder:
    KConfig *config = KGlobal::config().data();
    KConfigGroup configGroup(config, "Backups");
    QString folder = configGroup.readEntry("lastFolder", QDir::homePath()) + "/";

    // Ask a file name to the user:
    QString filter = "*.tar.gz|" + i18n("Tar Archives Compressed by Gzip") + "\n*|" + i18n("All Files");
    QString path = KFileDialog::getOpenFileName(folder, filter, this, i18n("Open Basket Archive"));
    if (path.isEmpty()) // User has canceled
        return;

    // Before replacing the basket data folder with the backup content, we safely backup the current baskets to the home folder.
    // So if the backup is corrupted or something goes wrong while restoring (power cut...) the user will be able to restore the old working data:
    QString safetyPath = Backup::newSafetyFolder();
    FormatImporter copier;
    copier.moveFolder(Global::savesFolder(), safetyPath);

    // Add the README file for user to cancel a bad restoration:
    QString readmePath = safetyPath + i18n("README.txt");
    QFile file(readmePath);
    if (file.open(QIODevice::WriteOnly)) {
        QTextStream stream(&file);
        stream << i18n("This is a safety copy of your baskets like they were before you started to restore the backup %1.", KUrl(path).fileName()) + "\n\n"
        << i18n("If the restoration was a success and you restored what you wanted to restore, you can remove this folder.") + "\n\n"
        << i18n("If something went wrong during the restoration process, you can re-use this folder to store your baskets and nothing will be lost.") + "\n\n"
        << i18n("Choose \"Basket\" -> \"Backup & Restore...\" -> \"Use Another Existing Folder...\" and select that folder.") + "\n";
        file.close();
    }

    QString message =
        "<p><nobr>" + i18n("Restoring <b>%1</b>. Please wait...", KUrl(path).fileName()) + "</nobr></p><p>" +
        i18n("If something goes wrong during the restoration process, read the file <b>%1</b>.", readmePath);

    KProgressDialog *dialog = new KProgressDialog(0, i18n("Restore Baskets"), message);
    dialog->setModal(/*modal=*/true);
    dialog->setAllowCancel(false);
    dialog->setAutoClose(true);
    dialog->show();
    QProgressBar *progress = dialog->progressBar();
    progress->setRange(0, 0/*Busy/Undefined*/);
    progress->setValue(0);
    progress->setTextVisible(false);

    // Uncompress:
    RestoreThread thread(path, Global::savesFolder());
    thread.start();
    while (thread.isRunning()) {
        progress->setValue(progress->value() + 1); // Or else, the animation is not played!
        kapp->processEvents();
        usleep(300); // Not too long because if the restore process is finished, we wait for nothing
    }

    dialog->hide(); // The restore is finished, do not continue to show it while telling the user the application is going to be restarted
    delete dialog; // If we only hidden it, it reappeared just after having restored a small backup... Very strange.
    dialog = 0;    // This was annoying since it is modal and the "BasKet Note Pads is going to be restarted" message was not reachable.
    //kapp->processEvents();

    // Check for errors:
    if (!thread.success()) {
        // Restore the old baskets:
        QDir dir;
        dir.remove(readmePath);
        copier.moveFolder(safetyPath, Global::savesFolder());
        // Tell the user:
        KMessageBox::error(0, i18n("This archive is either not a backup of baskets or is corrupted. It cannot be imported. Your old baskets have been preserved instead."), i18n("Restore Error"));
        return;
    }

    // Note: The safety backup is not removed now because the code can has been wrong, somehow, or the user perhapse restored an older backup by error...
    //       The restore process will not be called very often (it is possible it will only be called once or twice arround the world during the next years).
    //       So it is rare enough to force the user to remove the safety folder, but keep him in control and let him safely recover from restoration errors.

    Backup::setFolderAndRestart(Global::savesFolder()/*No change*/, i18n("Your backup has been successfuly restored to <b>%1</b>. %2 is going to be restarted to take this change into account."));
}
コード例 #3
0
QByteArray KstViewObjectImageDrag::encodedData(const char *mimeType) const {
  if (!_mimeTypes.contains(QString::fromLatin1(mimeType))) {
    return QByteArray();
  }

  QRect geom(0, 0, 0, 0);
  for (KstViewObjectList::ConstIterator i = _objects.begin(); i != _objects.end(); ++i) {
    geom = geom.unite((*i)->geometry());
  }

  QPixmap pm;
  pm.resize(geom.size());
  pm.fill();

  int prog = 0;
  bool cancelled = false;
  KstPainter p(KstPainter::P_EXPORT);
  p.begin(&pm);
  p.setClipping(true);
  KProgressDialog *dlg = new KProgressDialog(0, 0, QString::null, i18n("Generating and storing images of objects..."), true);
  dlg->setAllowCancel(true);
  dlg->progressBar()->setTotalSteps(_objects.count());
  dlg->progressBar()->setValue(prog);
  dlg->show();

  for (KstViewObjectList::Iterator i = _objects.begin(); i != _objects.end(); ++i) {
    p.setClipRect((*i)->geometry());
    p.setViewport((*i)->geometry());
    (*i)->paint(p, QRegion());
    if (dlg->wasCancelled()) {
      cancelled = true;
      break;
    }
    dlg->progressBar()->setValue(++prog);
    kapp->eventLoop()->processEvents(QEventLoop::ExcludeSocketNotifiers);
  }
  p.end();

  delete dlg;

  if (cancelled) {
    return QByteArray();
  }

  QByteArray rc;
#if QT_VERSION < 0x030200
  KTempFile tf;
  pm.save(tf.name(), KImageIO::typeForMime(mimeType).latin1());
  tf.close();
  QFile f(tf.name());
  if (f.open(IO_ReadOnly)) {
    rc = f.readAll();
    f.close();
  }
  QFile::remove(tf.name());
#else
  QDataStream ds(rc, IO_WriteOnly);
  pm.save(ds.device(), KImageIO::typeForMime(mimeType).latin1());
#endif

  return rc;
}