Logo Search packages:      
Sourcecode: ktechlab version File versions  Download package

void ItemDocument::exportToImage (  )  [slot, inherited]

Brings up a file dialog requesting the location of the file to export to, and then exports an image of the canvas.

Definition at line 783 of file itemdocument.cpp.

References ItemDocument::canvas(), ItemDocument::canvasBoundingRect(), ItemDocument::setSVGExport(), Document::type(), ItemDocument::updateBackground(), and Document::url().

{
      // 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;
      if ( type() == Document::dt_flowcode )
            cropMessage = i18n("Crop image to program parts");
      
      else if ( type() == Document::dt_circuit )
            cropMessage = i18n("Crop image to circuit components");
      
      else 
            cropMessage = i18n("Crop image");
      
      QCheckBox *cropCheck = new QCheckBox( cropMessage, p_ktechlab, "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.
      KFileDialog exportDialog(QString::null, "*.png|PNG Image\n*.bmp|BMP Image\n*.svg|SVG Image" , p_ktechlab, 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( p_ktechlab, 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...
      // at the moment the pixmap is just as big as the canvas,
      // intend to make some kind of cropping thing so it just 
      // takes the bit with the circuit on.
      
      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( p_ktechlab, i18n("Export failed"), i18n("Image Export") );
      
      delete outputImage;
}


Generated by  Doxygen 1.6.0   Back to index