예제 #1
0
void ImageView::exportImage()
{
#ifndef QT_NO_SVG
    QGraphicsSvgItem *svgItem = qgraphicsitem_cast<QGraphicsSvgItem *>(m_imageItem);
    QTC_ASSERT(svgItem, return);

    const QFileInfo origFi = m_file->filePath().toFileInfo();
    ExportDialog exportDialog(this);
    exportDialog.setWindowTitle(tr("Export %1").arg(origFi.fileName()));
    exportDialog.setExportSize(svgSize());
    exportDialog.setExportFileName(suggestedExportFileName(origFi));

    while (exportDialog.exec() == QDialog::Accepted && !exportSvg(exportDialog.exportData())) {}
#endif // !QT_NO_SVG
}
예제 #2
0
void ItemDocument::exportToImage() {
	// scaralously copied from print.
	// this slot is called whenever the File->Export menu is selected,
	// the Export shortcut is pressed or the Export toolbar
	// button is clicked

	// widget for the kfiledialog
	// It is the bit that says "Crop circuit?"
	// Okay need to think of something way better to say here.
	// gotme here, KFileDialog makes itself parent so tries to destroy cropCheck when it is deleted.
	// therefore we use a pointer.
	QString cropMessage;

	cropMessage = i18n("Crop image");

	QCheckBox *cropCheck = new QCheckBox(cropMessage, KTechlab::self(), "cropCheck");
	cropCheck->setChecked(true); // yes by default?

	// we need an object so we can retrieve which image type was selected by the user
	// so setup the filedialog.
	QString f;
	f = QString("*.png|%1\n*.bmp|%2\n*.svg|%3").arg(i18n("PNG Image")).arg(i18n("BMP Image")).arg(i18n("SVG Image"));
	KFileDialog exportDialog(QString::null, f, KTechlab::self(), i18n("Export As Image"), true, cropCheck);

	exportDialog.setOperationMode(KFileDialog::Saving);
	// now actually show it

	if (exportDialog.exec() == QDialog::Rejected)
		return;

	KURL url = exportDialog.selectedURL();

	if (url.isEmpty()) return;

	if (QFile::exists(url.path())) {
		int query = KMessageBox::warningYesNo(KTechlab::self(), i18n("A file named \"%1\" already exists. " "Are you sure you want to overwrite it?").arg(url.fileName()), i18n("Overwrite File?"), i18n("Overwrite"), KStdGuiItem::cancel());

		if (query == KMessageBox::No) return;
	}

	// with Qt, you always "print" to a
	// QPainter.. whether the output medium is a pixmap, a screen,
	// or paper

	// needs to be something like QPicture to do SVG etc...

	QRect saveArea;
	QString type;
	QRect cropArea;
	QPaintDevice *outputImage;
	QString filter = exportDialog.currentFilter();
	filter = filter.lower(); // gently soften the appearance of the letters.

	// did have a switch here but seems you can't use that on strings
	if (filter == "*.png") 	type = "PNG";
	else if (filter == "*.bmp")	type = "BMP";
	else if (filter == "*.svg") {
		KMessageBox::information(NULL, i18n("SVG export is sub-functional"), i18n("Export As Image"));
		type = "SVG";
	}
	// I don't like forcing people to use the right extension (personally)
	// but it is the easiest way to decide image type.
	else {
		KMessageBox::sorry(NULL, i18n("Unknown extension, please select one from the filter list."), i18n("Export As Image"));
		return;
	}

	if (cropCheck->isChecked()) {
		cropArea = canvasBoundingRect();

		if (cropArea.isNull()) {
			KMessageBox::sorry(0l, i18n("There is nothing to crop"), i18n("Export As Image"));
			return;
		} else {
			cropArea &= canvas()->rect();
		}
	}

	saveArea = m_canvas->rect();

	if (type == "PNG" || type == "BMP")
		outputImage = new QPixmap(saveArea.size());
	else if (type == "SVG") {
		setSVGExport(true);
		outputImage = new QPicture();
		// svg can't be cropped using the qimage method.
		saveArea = cropArea;
	} else {
		kdWarning() << "Unknown type!" << endl;
		return;
	}

	QPainter p(outputImage);

	m_canvas->setBackgroundPixmap(QPixmap());
	m_canvas->drawArea(saveArea, &p);
	updateBackground();

	p.end();

	bool saveResult;

	// if cropping we need to convert to an image,
	// crop, then save.

	if (cropCheck->isChecked()) {
		if (type == "SVG")
			saveResult = dynamic_cast<QPicture*>(outputImage)->save(url.path(), type);
		else {
			QImage img = dynamic_cast<QPixmap*>(outputImage)->convertToImage();
			img = img.copy(cropArea);
			saveResult = img.save(url.path(), type);
		}
	} else {
		if (type == "SVG")
			saveResult = dynamic_cast<QPicture*>(outputImage)->save(url.path(), type);
		else	saveResult = dynamic_cast<QPixmap*>(outputImage)->save(url.path(), type);
	}

	//if(saveResult == true)	KMessageBox::information( this, i18n("Sucessfully exported to \"%1\"").arg( url.filename() ), i18n("Image Export") );
	//else KMessageBox::information( this, i18n("Export failed"), i18n("Image Export") );

	if (type == "SVG") setSVGExport(false);

	if (saveResult == false)
		KMessageBox::information(KTechlab::self(), i18n("Export failed"), i18n("Image Export"));

	delete outputImage;
}