Пример #1
0
// Build list widget
void KCardWidget::insertCardIcons()
{
    // Clear GUI
    d->ui.list->clear();

    // Rebuild list
    QSize itemSize;
    foreach(const QString &name, CardDeckInfo::deckNames())
    {
        KCardThemeInfo v = CardDeckInfo::deckInfo(name);
        // Show only SVG files?
        if (v.svgfile.isEmpty()) continue;

        const int iconSize = 48;
        QPixmap resizedCard = v.preview.scaled(QSize(iconSize, iconSize), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        QPixmap previewPixmap(iconSize, iconSize);
        previewPixmap.fill(Qt::transparent);
        QPainter p(&previewPixmap);
        p.drawPixmap((iconSize-resizedCard.width())/2, (iconSize-resizedCard.height())/2, resizedCard);
        p.end();

        QListWidgetItem *item = new QListWidgetItem(v.name, d->ui.list);
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        item->setToolTip(v.name);
        item->setData(Qt::DecorationRole, previewPixmap);
        item->setData(Qt::UserRole, v.noi18Name);
        itemSize = itemSize.expandedTo(previewPixmap.size());
    }
Пример #2
0
void Archive::save(Basket *basket, bool withSubBaskets, const QString &destination)
{
	QDir dir;
	KProgressDialog dialog(0, i18n("Save as Basket Archive"), i18n("Saving as basket archive. Please wait..."), /*Not modal, for password dialogs!*/false);
	dialog.showCancelButton(false);
	dialog.setAutoClose(true);
	dialog.show();
	QProgressBar *progress = dialog.progressBar();
    
    progress->setRange(0,/*Preparation:*/1 + /*Finishing:*/1 + /*Basket:*/1 + /*SubBaskets:*/(withSubBaskets ? Global::bnpView->basketCount(Global::bnpView->listViewItemForBasket(basket)) : 0));
	progress->setValue(0);

	// Create the temporary folder:
	QString tempFolder = Global::savesFolder() + "temp-archive/";
	dir.mkdir(tempFolder);

	// Create the temporary archive file:
	QString tempDestination = tempFolder + "temp-archive.tar.gz";
	KTar tar(tempDestination, "application/x-gzip");
	tar.open(QIODevice::WriteOnly);
	tar.writeDir("baskets", "", "");

    progress->setValue(progress->value()+1);        // Preparation finished
    
	kDebug() << "Preparation finished out of " << progress->maximum();

	// Copy the baskets data into the archive:
	QStringList backgrounds;
	Archive::saveBasketToArchive(basket, withSubBaskets, &tar, backgrounds, tempFolder, progress);

	// Create a Small baskets.xml Document:
	QDomDocument document("basketTree");
	QDomElement root = document.createElement("basketTree");
	document.appendChild(root);
	Global::bnpView->saveSubHierarchy(Global::bnpView->listViewItemForBasket(basket), document, root, withSubBaskets);
	Basket::safelySaveToFile(tempFolder + "baskets.xml", "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + document.toString());
	tar.addLocalFile(tempFolder + "baskets.xml", "baskets/baskets.xml");
	dir.remove(tempFolder + "baskets.xml");

	// Save a Small tags.xml Document:
	QList<Tag*> tags;
	listUsedTags(basket, withSubBaskets, tags);
	Tag::saveTagsTo(tags, tempFolder + "tags.xml");
	tar.addLocalFile(tempFolder + "tags.xml", "tags.xml");
	dir.remove(tempFolder + "tags.xml");

	// Save Tag Emblems (in case they are loaded on a computer that do not have those icons):
	QString tempIconFile = tempFolder + "icon.png";
	for (Tag::List::iterator it = tags.begin(); it != tags.end(); ++it) {
		State::List states = (*it)->states();
		for (State::List::iterator it2 = states.begin(); it2 != states.end(); ++it2) {
			State *state = (*it2);
			QPixmap icon = KIconLoader::global()->loadIcon(
                state->emblem(), KIconLoader::Small, 16, KIconLoader::DefaultState,
                QStringList(), 0L, true
                );
			if (!icon.isNull()) {
				icon.save(tempIconFile, "PNG");
				QString iconFileName = state->emblem().replace('/', '_');
				tar.addLocalFile(tempIconFile, "tag-emblems/" + iconFileName);
			}
		}
	}
	dir.remove(tempIconFile);

	// Finish Tar.Gz Exportation:
	tar.close();

	// Computing the File Preview:
	Basket *previewBasket = basket; // FIXME: Use the first non-empty basket!
	QPixmap previewPixmap(previewBasket->visibleWidth(), previewBasket->visibleHeight());
	QPainter painter(&previewPixmap);
	// Save old state, and make the look clean ("smile, you are filmed!"):
	NoteSelection *selection = previewBasket->selectedNotes();
	previewBasket->unselectAll();
	Note *focusedNote = previewBasket->focusedNote();
	previewBasket->setFocusedNote(0);
	previewBasket->doHoverEffects(0, Note::None);
	// Take the screenshot:
	previewBasket->drawContents(&painter, 0, 0, previewPixmap.width(), previewPixmap.height());
	// Go back to the old look:
	previewBasket->selectSelection(selection);
	previewBasket->setFocusedNote(focusedNote);
	previewBasket->doHoverEffects();
	// End and save our splandid painting:
	painter.end();
	QImage previewImage = previewPixmap.toImage();
	const int PREVIEW_SIZE = 256;
	previewImage = previewImage.scaled(PREVIEW_SIZE, PREVIEW_SIZE, Qt::KeepAspectRatio);
	previewImage.save(tempFolder + "preview.png", "PNG");

	// Finaly Save to the Real Destination file:
	QFile file(destination);
	if (file.open(QIODevice::WriteOnly)) {
		ulong previewSize = QFile(tempFolder + "preview.png").size();
		ulong archiveSize = QFile(tempDestination).size();
		QTextStream stream(&file);
		stream.setCodec("ISO-8859-1");
		stream << "BasKetNP:archive\n"
		       << "version:0.6.1\n"
//		       << "read-compatible:0.6.1\n"
//		       << "write-compatible:0.6.1\n"
		       << "preview*:" << previewSize << "\n";

		stream.flush();
		// Copy the Preview File:
		const unsigned long BUFFER_SIZE = 1024;
		char *buffer = new char[BUFFER_SIZE];
		long sizeRead;
		QFile previewFile(tempFolder + "preview.png");
		if (previewFile.open(QIODevice::ReadOnly)) {
			while ((sizeRead = previewFile.read(buffer, BUFFER_SIZE)) > 0)
				file.write(buffer, sizeRead);
		}
		stream << "archive*:" << archiveSize << "\n";
		stream.flush();

		// Copy the Archive File:
		QFile archiveFile(tempDestination);
		if (archiveFile.open(QIODevice::ReadOnly)) {
			while ((sizeRead = archiveFile.read(buffer, BUFFER_SIZE)) > 0)
				file.write(buffer, sizeRead);
		}
		// Clean Up:
		delete buffer;
		buffer = 0;
		file.close();
	}

    progress->setValue(progress->value()+1); // Finishing finished
	kDebug() << "Finishing finished";

	// Clean Up Everything:
	dir.remove(tempFolder + "preview.png");
	dir.remove(tempDestination);
	dir.rmdir(tempFolder);
}