exporter.cc 4.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C)  2003  Dominique Devriese <devriese@kde.org>

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
15
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Dirk Mueller's avatar
Dirk Mueller committed
16
// 02110-1301, USA.
17 18 19

#include "exporter.h"

Pino Toscano's avatar
Pino Toscano committed
20
#include "imageexporteroptions.h"
21
#include "latexexporter.h"
22
#include "asyexporter.h"
Pino Toscano's avatar
Pino Toscano committed
23
#include "svgexporter.h"
24
#include "xfigexporter.h"
25

26
#include "../kig/kig_document.h"
27
#include "../kig/kig_part.h"
28 29
#include "../kig/kig_view.h"
#include "../misc/common.h"
Pino Toscano's avatar
Pino Toscano committed
30 31
#include "../misc/kigfiledialog.h"
#include "../misc/kigpainter.h"
32

David Narváez's avatar
David Narváez committed
33
#include <QIcon>
34 35
#include <qfile.h>

36
#include <KIconEngine>
Pino Toscano's avatar
Pino Toscano committed
37
#include <kactionmenu.h>
Thomas Zander's avatar
Thomas Zander committed
38
#include <kactioncollection.h>
Pino Toscano's avatar
Pino Toscano committed
39
#include <kimageio.h>
40 41
#include <klocale.h>
#include <kmessagebox.h>
Pino Toscano's avatar
Pino Toscano committed
42
#include <kmimetype.h>
43

44
ExporterAction::ExporterAction( const KigPart* doc, KigWidget* w,
45
                                KActionCollection* parent, KigExporter* exp )
David Narváez's avatar
David Narváez committed
46
  : QAction( exp->menuEntryName(), parent),
47 48
    mexp( exp ), mdoc( doc ), mw( w )
{
49
  QString iconstr = exp->menuIcon();
50
  if ( !iconstr.isEmpty() )
51
    setIcon( QIcon( new KIconEngine( iconstr, const_cast<KigPart*>( doc )->iconLoader() ) ) );
52
  connect( this, SIGNAL( triggered() ), this, SLOT( slotActivated() ) );
Thomas Zander's avatar
Thomas Zander committed
53 54
  if(parent)
    parent->addAction("action", this );
Dirk Mueller's avatar
Dirk Mueller committed
55
}
56 57 58 59

void ExporterAction::slotActivated()
{
  mexp->run( *mdoc, *mw );
Dirk Mueller's avatar
Dirk Mueller committed
60
}
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

KigExporter::~KigExporter()
{
}

ImageExporter::~ImageExporter()
{
}

QString ImageExporter::exportToStatement() const
{
  return i18n( "&Export to image" );
}

QString ImageExporter::menuEntryName() const
{
  return i18n( "&Image..." );
}

80 81
QString ImageExporter::menuIcon() const
{
Pino Toscano's avatar
Pino Toscano committed
82
  return "image-x-generic";
83 84
}

85
void ImageExporter::run( const KigPart& doc, KigWidget& w )
86
{
Pino Toscano's avatar
Pino Toscano committed
87
  KigFileDialog* kfd = new KigFileDialog(
88
      QString(), KImageIO::pattern( KImageIO::Writing ),
Pino Toscano's avatar
Pino Toscano committed
89 90
      i18n( "Export as Image" ), &w );
  kfd->setOptionCaption( i18n( "Image Options" ) );
91
  ImageExporterOptions* opts = new ImageExporterOptions( 0L );
Pino Toscano's avatar
Pino Toscano committed
92
  kfd->setOptionsWidget( opts );
93 94 95
  opts->setImageSize( w.size() );
  opts->setGrid( doc.document().grid() );
  opts->setAxes( doc.document().axes() );
Pino Toscano's avatar
Pino Toscano committed
96 97 98 99
  if ( !kfd->exec() )
    return;

  QString filename = kfd->selectedFile();
100 101 102
  bool showgrid = opts->showGrid();
  bool showaxes = opts->showAxes();
  QSize imgsize = opts->imageSize();
Pino Toscano's avatar
Pino Toscano committed
103 104 105 106

  delete opts;
  delete kfd;

Pino Toscano's avatar
Pino Toscano committed
107
  KMimeType::Ptr mimeType = KMimeType::findByPath( filename );
David Narváez's avatar
David Narváez committed
108
  qDebug() << "mimetype: " << mimeType->name();
Pino Toscano's avatar
Pino Toscano committed
109 110 111 112 113 114
  if ( !KImageIO::isSupported( mimeType->name(), KImageIO::Writing ) )
  {
    KMessageBox::sorry( &w, i18n( "Sorry, this file format is not supported." ) );
    return;
  };

Pino Toscano's avatar
Pino Toscano committed
115
  QFile file( filename );
Pino Toscano's avatar
Pino Toscano committed
116
  if ( ! file.open( QIODevice::WriteOnly ) )
Pino Toscano's avatar
Pino Toscano committed
117 118
  {
    KMessageBox::sorry( &w,
119 120
                        i18n( "The file \"%1\" could not be opened. Please check if the file permissions are set correctly." ,
                          filename ) );
Pino Toscano's avatar
Pino Toscano committed
121 122 123
    return;
  };

124
  QPixmap img( imgsize );
Pino Toscano's avatar
Pino Toscano committed
125 126 127 128 129 130
  img.fill( Qt::white );
  KigPainter p( ScreenInfo( w.screenInfo().shownRect(), img.rect() ), &img, doc.document() );
  p.setWholeWinOverlay();
  p.drawGrid( doc.document().coordinateSystem(), showgrid, showaxes );
  // FIXME: show the selections ?
  p.drawObjects( doc.document().objects(), false );
Pino Toscano's avatar
Pino Toscano committed
131 132 133
  QStringList types = KImageIO::typeForMime( mimeType->name() );
  if ( types.isEmpty() ) return; // TODO error dialog?
  if ( !img.save( filename, types.at(0).toLatin1() ) )
Pino Toscano's avatar
Pino Toscano committed
134
  {
135
    KMessageBox::error( &w, i18n( "Sorry, something went wrong while saving to image \"%1\"", filename ) );
Pino Toscano's avatar
Pino Toscano committed
136 137
  }

138 139 140 141 142
}

KigExportManager::KigExportManager()
{
  mexporters.push_back( new ImageExporter );
Dominique Devriese's avatar
Dominique Devriese committed
143
  mexporters.push_back( new XFigExporter );
144
  mexporters.push_back( new LatexExporter );
145
  mexporters.push_back( new AsyExporter );
Pino Toscano's avatar
Pino Toscano committed
146
  mexporters.push_back( new SVGExporter );
147 148 149 150 151 152 153 154
}

KigExportManager::~KigExportManager()
{
  for ( uint i = 0; i < mexporters.size(); ++i )
    delete mexporters[i];
}

155
void KigExportManager::addMenuAction( const KigPart* doc, KigWidget* w,
156 157
                                      KActionCollection* coll )
{
Pino Toscano's avatar
Pino Toscano committed
158
  KActionMenu* m = new KActionMenu( i18n( "&Export To" ), w );
159
  m->setIcon( QIcon( new KIconEngine( "document-export", const_cast<KigPart*>( doc )->iconLoader() ) ) );
160
  for ( uint i = 0; i < mexporters.size(); ++i )
161
    m->addAction( new ExporterAction( doc, w, coll, mexporters[i] ) );
Thomas Zander's avatar
Thomas Zander committed
162 163
  if(coll)
    coll->addAction("file_export", m );
164 165 166 167 168 169 170
}

KigExportManager* KigExportManager::instance()
{
  static KigExportManager m;
  return &m;
}