moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kig / kig / kig_part.cpp
blob08b78d6d9f36f3711224dc6de394484ad3ade0eb
1 /**
2 This file is part of Kig, a KDE program for Interactive Geometry...
3 Copyright (C) 2002 Dominique Devriese <devriese@kde.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 USA
19 **/
21 #include "kig_part.h"
22 #include "kig_part.moc"
24 #include "aboutdata.h"
25 #include "kig_commands.h"
26 #include "kig_document.h"
27 #include "kig_view.h"
29 #include "../filters/exporter.h"
30 #include "../filters/filter.h"
31 #include "../misc/builtin_stuff.h"
32 #include "../misc/calcpaths.h"
33 #include "../misc/coordinate_system.h"
34 #include "../misc/guiaction.h"
35 #include "../misc/kigpainter.h"
36 #include "../misc/lists.h"
37 #include "../misc/object_constructor.h"
38 #include "../misc/screeninfo.h"
39 #include "../modes/normal.h"
40 #include "../objects/object_drawer.h"
41 #include "../objects/point_imp.h"
43 #include <algorithm>
44 #include <functional>
46 #include <kaction.h>
47 #include <kapplication.h>
48 #include <kdebug.h>
49 #include <kfiledialog.h>
50 #include <kglobal.h>
51 #include <kiconloader.h>
52 #include <kinstance.h>
53 #include <klocale.h>
54 #include <kmainwindow.h>
55 #include <kmessagebox.h>
56 #include <kmimetype.h>
57 #include <kprinter.h>
58 #include <kstandarddirs.h>
59 #include <kstdaction.h>
60 #include <ktoolbar.h>
61 #include <kparts/genericfactory.h>
62 #include <kdeprint/kprintdialogpage.h>
64 #include <qcheckbox.h>
65 #include <qfile.h>
66 #include <qlayout.h>
67 #include <qpaintdevicemetrics.h>
68 #include <qsizepolicy.h>
69 #include <qtimer.h>
70 #if QT_VERSION >= 0x030100
71 #include <qeventloop.h>
72 #endif
74 using namespace std;
76 static const QString typesFile = "macros.kigt";
78 // export this library...
79 typedef KParts::GenericFactory<KigPart> KigPartFactory;
80 K_EXPORT_COMPONENT_FACTORY ( libkigpart, KigPartFactory )
82 KAboutData* KigPart::createAboutData()
84 return kigAboutData( "kig", I18N_NOOP( "KigPart" ) );
87 class SetCoordinateSystemAction
88 : public KSelectAction
90 KigPart& md;
91 public:
92 SetCoordinateSystemAction( KigPart& d, KActionCollection* parent );
93 void slotActivated( int index );
96 SetCoordinateSystemAction::SetCoordinateSystemAction(
97 KigPart& d, KActionCollection* parent )
98 : KSelectAction( i18n( "&Set Coordinate System" ), 0, parent, "settings_set_coordinate_system" ),
99 md( d )
101 setItems( CoordinateSystemFactory::names() );
102 setCurrentItem( md.document().coordinateSystem().id() );
105 void SetCoordinateSystemAction::slotActivated( int index )
107 CoordinateSystem* sys = CoordinateSystemFactory::build( index );
108 assert( sys );
109 md.history()->addCommand( KigCommand::changeCoordSystemCommand( md, sys ) );
110 setCurrentItem( index );
113 class KigPrintDialogPage
114 : public KPrintDialogPage
116 public:
117 KigPrintDialogPage( QWidget* parent = 0, const char* name = 0 );
118 ~KigPrintDialogPage();
120 void getOptions( QMap<QString,QString>& opts, bool );
121 void setOptions( const QMap<QString,QString>& opts );
122 bool isValid( QString& );
124 private:
125 QCheckBox *showgrid;
126 QCheckBox *showaxes;
129 KigPrintDialogPage::KigPrintDialogPage( QWidget* parent, const char* name )
130 : KPrintDialogPage( parent, name )
132 setTitle( i18n( "Kig Options" ) );
134 QVBoxLayout* vl = new QVBoxLayout( this, 0 , 11 );
136 showgrid = new QCheckBox( i18n( "Show grid" ), this );
137 vl->addWidget( showgrid );
139 showaxes = new QCheckBox( i18n( "Show axes" ), this );
140 vl->addWidget( showaxes );
142 vl->addItem( new QSpacerItem( 10, 10, QSizePolicy::Fixed, QSizePolicy::Expanding ) );
145 KigPrintDialogPage::~KigPrintDialogPage()
149 void KigPrintDialogPage::getOptions( QMap< QString, QString >& opts, bool )
151 opts[ "kde-kig-showgrid" ] = QString::number( showgrid->isChecked() );
152 opts[ "kde-kig-showaxes" ] = QString::number( showaxes->isChecked() );
155 void KigPrintDialogPage::setOptions( const QMap< QString, QString >& opts )
157 QString tmp = opts[ "kde-kig-showgrid" ];
158 bool bt = ( tmp != "0" );
159 showgrid->setChecked( bt );
160 tmp = opts[ "kde-kig-showaxes" ];
161 bt = ( tmp != "0" );
162 showaxes->setChecked( bt );
165 bool KigPrintDialogPage::isValid( QString& )
167 return true;
170 KigPart::KigPart( QWidget *parentWidget, const char *,
171 QObject *parent, const char *name,
172 const QStringList& )
173 : KParts::ReadWritePart( parent, name ),
174 mMode( 0 ), mdocument( new KigDocument() )
176 // we need an instance
177 setInstance( KigPartFactory::instance() );
179 mMode = new NormalMode( *this );
181 // we need a widget, to actually show the document
182 m_widget = new KigView(this, false, parentWidget, "kig_view");
183 // notify the part that this is our internal widget
184 setWidget( m_widget );
186 // create our actions...
187 setupActions();
189 // set our XML-UI resource file
190 setXMLFile("kigpartui.rc");
192 // our types...
193 setupTypes();
195 // construct our command history
196 mhistory = new KCommandHistory(actionCollection());
197 mhistory->documentSaved();
198 connect( mhistory, SIGNAL( documentRestored() ), this, SLOT( setUnmodified() ) );
200 // we are read-write by default
201 setReadWrite(true);
203 setModified (false);
205 GUIActionList::instance()->regDoc( this );
208 void KigPart::setupActions()
210 // save actions..
211 (void) KStdAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
212 (void) KStdAction::save(this, SLOT(fileSave()), actionCollection());
214 // print actions
215 (void) KStdAction::print( this, SLOT( filePrint() ), actionCollection() );
216 (void) KStdAction::printPreview( this, SLOT( filePrintPreview() ), actionCollection() );
218 // selection actions
219 aSelectAll = KStdAction::selectAll(
220 this, SLOT( slotSelectAll() ), actionCollection() );
221 aDeselectAll = KStdAction::deselect(
222 this, SLOT( slotDeselectAll() ), actionCollection() );
223 aInvertSelection = new KAction(
224 i18n( "Invert Selection" ), "", 0, this,
225 SLOT( slotInvertSelection() ), actionCollection(),
226 "edit_invert_selection" );
228 // we need icons...
229 KIconLoader* l = instance()->iconLoader();
230 QPixmap tmp;
232 aDeleteObjects = new KAction(
233 i18n("&Delete Objects"), "editdelete", Key_Delete, this,
234 SLOT(deleteObjects()), actionCollection(), "delete_objects");
235 aDeleteObjects->setToolTip(i18n("Delete the selected objects"));
237 aCancelConstruction = new KAction(
238 i18n("Cancel Construction"), "stop", Key_Escape, this,
239 SLOT(cancelConstruction()), actionCollection(), "cancel_construction");
240 aCancelConstruction->setToolTip(
241 i18n("Cancel the construction of the object being constructed"));
242 aCancelConstruction->setEnabled(false);
244 aShowHidden = new KAction(
245 i18n("Unhide &All"), 0, this, SLOT( showHidden() ),
246 actionCollection(), "edit_unhide_all");
247 aShowHidden->setToolTip(i18n("Show all hidden objects"));
248 aShowHidden->setEnabled( true );
250 aNewMacro = new KAction(
251 i18n("&New Macro..."), "gear", 0, this, SLOT(newMacro()),
252 actionCollection(), "macro_action");
253 aNewMacro->setToolTip(i18n("Define a new macro"));
255 aConfigureTypes = new KAction(
256 i18n("Manage &Types..."), 0, this, SLOT(editTypes()),
257 actionCollection(), "types_edit");
258 aConfigureTypes->setToolTip(i18n("Manage macro types."));
260 KigExportManager::instance()->addMenuAction( this, m_widget->realWidget(),
261 actionCollection() );
263 KAction* a = KStdAction::zoomIn( m_widget, SLOT( slotZoomIn() ),
264 actionCollection() );
265 a->setToolTip( i18n( "Zoom in on the document" ) );
266 a->setWhatsThis( i18n( "Zoom in on the document" ) );
268 a = KStdAction::zoomOut( m_widget, SLOT( slotZoomOut() ),
269 actionCollection() );
270 a->setToolTip( i18n( "Zoom out of the document" ) );
271 a->setWhatsThis( i18n( "Zoom out of the document" ) );
273 a = KStdAction::fitToPage( m_widget, SLOT( slotRecenterScreen() ),
274 actionCollection() );
275 // grr.. why isn't there an icon for this..
276 a->setIconSet( QIconSet( l->loadIcon( "view_fit_to_page", KIcon::User ) ) );
277 a->setToolTip( i18n( "Recenter the screen on the document" ) );
278 a->setWhatsThis( i18n( "Recenter the screen on the document" ) );
280 #ifdef KDE_IS_VERSION
281 #if KDE_IS_VERSION(3,1,90)
282 #define KIG_PART_CPP_STD_FULLSCREEN_ACTION
283 #endif
284 #endif
285 #ifdef KIG_PART_CPP_STD_FULLSCREEN_ACTION
286 a = KStdAction::fullScreen( m_widget, SLOT( toggleFullScreen() ), actionCollection(), (QWidget*)(widget()->parent()),"fullscreen" );
287 #else
288 tmp = l->loadIcon( "window_fullscreen", KIcon::Toolbar );
289 a = new KAction(
290 i18n( "Full Screen" ), tmp, CTRL+SHIFT+Key_F,
291 m_widget, SLOT( toggleFullScreen() ),
292 actionCollection(), "fullscreen" );
293 #endif
294 a->setToolTip( i18n( "View this document full-screen." ) );
295 a->setWhatsThis( i18n( "View this document full-screen." ) );
297 // TODO: an icon for this..
298 a = new KAction(
299 i18n( "&Select Shown Area" ), "viewmagfit", 0, m_widget, SLOT( zoomRect() ),
300 actionCollection(), "view_select_shown_rect" );
301 a->setToolTip( i18n( "Select the area that you want to be shown in the window." ) );
302 a->setWhatsThis( i18n( "Select the area that you want to be shown in the window." ) );
304 a = new KAction(
305 i18n( "S&elect Zoom Area" ), "viewmag", 0, m_widget, SLOT( zoomArea() ),
306 actionCollection(), "view_zoom_area" );
307 // a->setToolTip( i18n( "Select the area that you want to be shown in the window." ) );
308 // a->setWhatsThis( i18n( "Select the area that you want to be shown in the window." ) );
310 aToggleGrid = new KToggleAction(
311 i18n( "Show &Grid" ), 0, this, SLOT( toggleGrid() ),
312 actionCollection(), "settings_show_grid" );
313 aToggleGrid->setToolTip( i18n( "Show or hide the grid." ) );
314 aToggleGrid->setChecked( true );
316 aToggleAxes = new KToggleAction(
317 i18n( "Show &Axes" ), 0, this, SLOT( toggleAxes() ),
318 actionCollection(), "settings_show_axes" );
319 aToggleAxes->setToolTip( i18n( "Show or hide the axes." ) );
320 aToggleAxes->setChecked( true );
322 aToggleNightVision = new KToggleAction(
323 i18n( "Wear Infrared Glasses" ), 0, this, SLOT( toggleNightVision() ),
324 actionCollection(), "settings_toggle_nightvision" );
325 aToggleNightVision->setToolTip( i18n( "Enable/Disable hidden objects visibility." ) );
326 aToggleNightVision->setChecked( false );
328 // select coordinate system KActionMenu..
329 aCoordSystem = new SetCoordinateSystemAction( *this, actionCollection() );
332 void KigPart::setupTypes()
334 setupBuiltinStuff();
335 setupBuiltinMacros();
336 setupMacroTypes();
337 GUIActionList& l = *GUIActionList::instance();
338 typedef GUIActionList::avectype::const_iterator iter;
339 for ( iter i = l.actions().begin(); i != l.actions().end(); ++i )
341 KigGUIAction* ret = new KigGUIAction( *i, *this, actionCollection() );
342 aActions.push_back( ret );
343 ret->plug( this );
347 KigPart::~KigPart()
349 GUIActionList::instance()->unregDoc( this );
351 // save our types...
352 saveTypes();
354 // objects get deleted automatically, when mobjsref gets
355 // destructed..
357 delete_all( aActions.begin(), aActions.end() );
358 aActions.clear();
360 // cleanup
361 delete mMode;
362 delete mhistory;
364 delete mdocument;
367 bool KigPart::openFile()
369 QFileInfo fileinfo( m_file );
370 if ( ! fileinfo.exists() )
372 KMessageBox::sorry( widget(),
373 i18n( "The file \"%1\" you tried to open does not exist. "
374 "Please verify that you entered the correct path." ).arg( m_file ),
375 i18n( "File Not Found" ) );
376 return false;
379 // m_file is always local, so we can use findByPath instead of
380 // findByURL...
381 KMimeType::Ptr mimeType = KMimeType::findByPath ( m_file );
382 kdDebug() << k_funcinfo << "mimetype: " << mimeType->name() << endl;
383 KigFilter* filter = KigFilters::instance()->find( mimeType->name() );
384 if ( !filter )
386 // we don't support this mime type...
387 KMessageBox::sorry
389 widget(),
390 i18n( "You tried to open a document of type \"%1\"; unfortunately, "
391 "Kig does not support this format. If you think the format in "
392 "question would be worth implementing support for, you can "
393 "always ask us nicely on mailto:kde-edu-devel@kde.org "
394 "or do the work yourself and send me a patch."
395 ).arg(mimeType->name()),
396 i18n( "Format not Supported" )
398 return false;
401 KigDocument* newdoc = filter->load (m_file);
402 if ( !newdoc ) return false;
403 delete mdocument;
404 mdocument = newdoc;
405 coordSystemChanged( mdocument->coordinateSystem().id() );
406 aToggleGrid->setChecked( mdocument->grid() );
407 aToggleAxes->setChecked( mdocument->axes() );
408 aToggleNightVision->setChecked( mdocument->getNightVision() );
410 setModified(false);
411 mhistory->clear();
413 std::vector<ObjectCalcer*> tmp = calcPath( getAllParents( getAllCalcers( document().objects() ) ) );
414 for ( std::vector<ObjectCalcer*>::iterator i = tmp.begin(); i != tmp.end(); ++i )
415 ( *i )->calc( document() );
416 emit recenterScreen();
418 redrawScreen();
420 return true;
423 bool KigPart::saveFile()
425 if ( m_file.isEmpty() || m_bTemp ) return internalSaveAs();
426 // mimetype:
427 KMimeType::Ptr mimeType = KMimeType::findByPath ( m_file );
428 if ( mimeType->name() != "application/x-kig" )
430 // we don't support this mime type...
431 if( KMessageBox::warningYesNo( widget(),
432 i18n( "Kig does not support saving to any other file format than "
433 "its own. Save to Kig's format instead?" ),
434 i18n( "Format not Supported" ) ) == KMessageBox::No )
435 return false;
436 internalSaveAs();
439 if ( KigFilters::instance()->save( document(), m_file ) )
441 setModified ( false );
442 mhistory->documentSaved();
443 return true;
445 return false;
448 void KigPart::addObject(ObjectHolder* o)
450 mhistory->addCommand( KigCommand::addCommand( *this, o ) );
453 void KigPart::addObjects( const std::vector<ObjectHolder*>& os )
455 mhistory->addCommand( KigCommand::addCommand( *this, os ) );
458 void KigPart::_addObject( ObjectHolder* o )
460 document().addObject( o );
461 setModified(true);
464 void KigPart::delObject( ObjectHolder* o )
466 // we delete all children and their children etc. too...
467 std::vector<ObjectHolder*> os;
468 os.push_back( o );
469 delObjects( os );
472 void KigPart::_delObjects( const std::vector<ObjectHolder*>& o )
474 document().delObjects( o );
475 setModified( true );
478 void KigPart::_delObject(ObjectHolder* o)
480 document().delObject( o );
481 setModified(true);
484 void KigPart::setMode( KigMode* m )
486 mMode = m;
487 m->enableActions();
488 redrawScreen();
491 void KigPart::_addObjects( const std::vector<ObjectHolder*>& os )
493 document().addObjects( os );
494 setModified( true );
497 void KigPart::deleteObjects()
499 mode()->deleteObjects();
502 void KigPart::cancelConstruction()
504 mode()->cancelConstruction();
507 void KigPart::showHidden()
509 mode()->showHidden();
512 void KigPart::newMacro()
514 mode()->newMacro();
517 void KigPart::editTypes()
519 mode()->editTypes();
522 void KigPart::setUnmodified()
524 setModified( false );
527 KCommandHistory* KigPart::history()
529 return mhistory;
532 void KigPart::delObjects( const std::vector<ObjectHolder*>& os )
534 if ( os.size() < 1 ) return;
535 std::set<ObjectHolder*> delobjs;
537 std::set<ObjectCalcer*> delcalcers = getAllChildren( getAllCalcers( os ) );
538 std::map<ObjectCalcer*, ObjectHolder*> holdermap;
540 std::set<ObjectHolder*> curobjs = document().objectsSet();
542 for ( std::set<ObjectHolder*>::iterator i = curobjs.begin();
543 i != curobjs.end(); ++i )
544 holdermap[( *i )->calcer()] = *i;
546 for ( std::set<ObjectCalcer*>::iterator i = delcalcers.begin();
547 i != delcalcers.end(); ++i )
549 std::map<ObjectCalcer*, ObjectHolder*>::iterator j = holdermap.find( *i );
550 if ( j != holdermap.end() )
551 delobjs.insert( j->second );
554 assert( delobjs.size() >= os.size() );
556 std::vector<ObjectHolder*> delobjsvect( delobjs.begin(), delobjs.end() );
557 mhistory->addCommand( KigCommand::removeCommand( *this, delobjsvect ) );
560 void KigPart::enableConstructActions( bool enabled )
562 for_each( aActions.begin(), aActions.end(),
563 bind2nd( mem_fun( &KAction::setEnabled ),
564 enabled ) );
567 void KigPart::unplugActionLists()
569 unplugActionList( "user_conic_types" );
570 unplugActionList( "user_segment_types" );
571 unplugActionList( "user_point_types" );
572 unplugActionList( "user_circle_types" );
573 unplugActionList( "user_line_types" );
574 unplugActionList( "user_other_types" );
575 unplugActionList( "user_types" );
578 void KigPart::plugActionLists()
580 plugActionList( "user_conic_types", aMNewConic );
581 plugActionList( "user_segment_types", aMNewSegment );
582 plugActionList( "user_point_types", aMNewPoint );
583 plugActionList( "user_circle_types", aMNewCircle );
584 plugActionList( "user_line_types", aMNewLine );
585 plugActionList( "user_other_types", aMNewOther );
586 plugActionList( "user_types", aMNewAll );
589 void KigPart::emitStatusBarText( const QString& text )
591 emit setStatusBarText( text );
594 void KigPart::fileSaveAs()
596 internalSaveAs();
599 void KigPart::fileSave()
601 save();
604 bool KigPart::internalSaveAs()
606 // this slot is connected to the KStdAction::saveAs action...
607 QString formats = i18n( "*.kig|Kig Documents (*.kig)\n"
608 "*.kigz|Compressed Kig Documents (*.kigz)" );
610 // formats += "\n";
611 // formats += KImageIO::pattern( KImageIO::Writing );
613 QString file_name = KFileDialog::getSaveFileName(":document", formats );
614 if (file_name.isEmpty()) return false;
615 else if ( QFileInfo( file_name ).exists() )
617 int ret = KMessageBox::warningYesNo( m_widget,
618 i18n( "The file \"%1\" already exists. Do you wish to overwrite it?" )
619 .arg( file_name ), i18n( "Overwrite File?" ) );
620 if ( ret != KMessageBox::Yes )
622 return false;
625 saveAs(KURL::fromPathOrURL( file_name ));
626 return true;
629 void KigPart::runMode( KigMode* m )
631 KigMode* prev = mMode;
633 setMode( m );
635 #if QT_VERSION >= 0x030100
636 (void) kapp->eventLoop()->enterLoop();
637 #else
638 (void) kapp->enter_loop();
639 #endif
641 setMode( prev );
642 redrawScreen();
645 void KigPart::doneMode( KigMode* d )
647 assert( d == mMode );
648 // pretend to use this var..
649 (void)d;
650 #if QT_VERSION >= 0x030100
651 kapp->eventLoop()->exitLoop();
652 #else
653 kapp->exit_loop();
654 #endif
657 void KigPart::actionRemoved( GUIAction* a, GUIUpdateToken& t )
659 KigGUIAction* rem = 0;
660 for ( std::vector<KigGUIAction*>::iterator i = aActions.begin(); i != aActions.end(); ++i )
662 if ( (*i)->guiAction() == a )
664 rem = *i;
665 aActions.erase( i );
666 break;
669 assert( rem );
670 aMNewSegment.remove( rem );
671 aMNewConic.remove( rem );
672 aMNewPoint.remove( rem );
673 aMNewCircle.remove( rem );
674 aMNewLine.remove( rem );
675 aMNewOther.remove( rem );
676 aMNewAll.remove( rem );
677 t.push_back( rem );
680 void KigPart::actionAdded( GUIAction* a, GUIUpdateToken& )
682 KigGUIAction* ret = new KigGUIAction( a, *this, actionCollection() );
683 aActions.push_back( ret );
684 ret->plug( this );
687 void KigPart::endGUIActionUpdate( GUIUpdateToken& t )
689 unplugActionLists();
690 plugActionLists();
691 delete_all( t.begin(), t.end() );
692 t.clear();
695 KigPart::GUIUpdateToken KigPart::startGUIActionUpdate()
697 return GUIUpdateToken();
700 void KigPart::setupMacroTypes()
702 static bool alreadysetup = false;
703 if ( ! alreadysetup )
705 alreadysetup = true;
707 // the user's saved macro types:
708 QStringList dataFiles =
709 KGlobal::dirs()->findAllResources("appdata", "kig-types/*.kigt",
710 true, false );
711 std::vector<Macro*> macros;
712 for ( QStringList::iterator file = dataFiles.begin();
713 file != dataFiles.end(); ++file )
715 std::vector<Macro*> nmacros;
716 bool ok = MacroList::instance()->load( *file, nmacros, *this );
717 if ( ! ok ) continue;
718 copy( nmacros.begin(), nmacros.end(), back_inserter( macros ) );
720 MacroList::instance()->add( macros );
722 // hack: we need to plug the action lists _after_ the gui is
723 // built.. i can't find a better solution than this...
724 QTimer::singleShot( 0, this, SLOT( plugActionLists() ) );
727 void KigPart::setupBuiltinMacros()
729 static bool alreadysetup = false;
730 if ( ! alreadysetup )
732 alreadysetup = true;
733 // builtin macro types ( we try to make the user think these are
734 // normal types )..
735 QStringList builtinfiles =
736 KGlobal::dirs()->findAllResources( "appdata", "builtin-macros/*.kigt", true, false );
737 for ( QStringList::iterator file = builtinfiles.begin();
738 file != builtinfiles.end(); ++file )
740 std::vector<Macro*> macros;
741 bool ok = MacroList::instance()->load( *file, macros, *this );
742 if ( ! ok ) continue;
743 for ( uint i = 0; i < macros.size(); ++i )
745 ObjectConstructorList* ctors = ObjectConstructorList::instance();
746 GUIActionList* actions = GUIActionList::instance();
747 Macro* macro = macros[i];
748 macro->ctor->setBuiltin( true );
749 ctors->add( macro->ctor );
750 actions->add( macro->action );
751 macro->ctor = 0;
752 macro->action = 0;
753 delete macro;
759 void KigPart::addWidget( KigWidget* v )
761 mwidgets.push_back( v );
764 void KigPart::delWidget( KigWidget* v )
766 mwidgets.erase( std::remove( mwidgets.begin(), mwidgets.end(), v ), mwidgets.end() );
769 void KigPart::filePrintPreview()
771 KPrinter printer;
772 printer.setPreviewOnly( true );
773 doPrint( printer );
776 void KigPart::filePrint()
778 KPrinter printer;
779 KigPrintDialogPage* kp = new KigPrintDialogPage();
780 printer.addDialogPage( kp );
781 printer.setFullPage( true );
782 printer.setOption( "kde-kig-showgrid", QString::number( document().grid() ) );
783 printer.setOption( "kde-kig-showaxes", QString::number( document().axes() ) );
784 printer.setPageSelection( KPrinter::ApplicationSide );
785 if ( printer.setup( m_widget, i18n("Print Geometry") ) )
787 doPrint( printer );
791 void KigPart::doPrint( KPrinter& printer )
793 QPaintDeviceMetrics metrics( &printer );
794 Rect rect = document().suggestedRect();
795 QRect qrect( 0, 0, metrics.width(), metrics.height() );
796 if ( rect.width() * qrect.height() > rect.height() * qrect.width() )
798 // qrect is too high..
799 int nh = static_cast<int>( qrect.width() * rect.height() / rect.width() );
800 int rest = qrect.height() - nh;
801 qrect.setTop( qrect.top() - rest / 2 );
802 qrect.setTop( rest / 2 );
804 else
806 // qrect is too wide..
807 int nw = static_cast<int>( qrect.height() * rect.width() / rect.height() );
808 int rest = qrect.width() - nw;
809 qrect.setLeft( rest / 2 );
810 qrect.setRight( qrect.right() - rest / 2 );
812 ScreenInfo si( rect, qrect );
813 KigPainter painter( si, &printer, document() );
814 painter.setWholeWinOverlay();
815 bool sg = true;
816 bool sa = true;
817 if ( !printer.previewOnly() )
819 sg = ( printer.option( "kde-kig-showgrid" ) != "0" );
820 sa = ( printer.option( "kde-kig-showaxes" ) != "0" );
822 else
824 sg = document().grid();
825 sg = document().axes();
827 painter.drawGrid( document().coordinateSystem(), sg, sa );
828 painter.drawObjects( document().objects(), false );
831 void KigPart::slotSelectAll()
833 mMode->selectAll();
836 void KigPart::slotDeselectAll()
838 mMode->deselectAll();
841 void KigPart::slotInvertSelection()
843 mMode->invertSelection();
846 void KigPart::hideObjects( const std::vector<ObjectHolder*>& inos )
848 std::vector<ObjectHolder*> os;
849 for (std::vector<ObjectHolder*>::const_iterator i = inos.begin(); i != inos.end(); ++i )
851 if ( (*i)->shown() )
852 os.push_back( *i );
854 KigCommand* kc = 0;
855 if ( os.size() == 0 ) return;
856 else if ( os.size() == 1 )
857 kc = new KigCommand( *this, os[0]->imp()->type()->hideAStatement() );
858 else kc = new KigCommand( *this, i18n( "Hide %n Object", "Hide %n Objects", os.size() ) );
859 for ( std::vector<ObjectHolder*>::iterator i = os.begin();
860 i != os.end(); ++i )
861 kc->addTask( new ChangeObjectDrawerTask( *i, ( *i )->drawer()->getCopyShown( false ) ) );
862 mhistory->addCommand( kc );
865 void KigPart::showObjects( const std::vector<ObjectHolder*>& inos )
867 std::vector<ObjectHolder*> os;
868 for (std::vector<ObjectHolder*>::const_iterator i = inos.begin(); i != inos.end(); ++i )
870 if ( !(*i)->shown() )
871 os.push_back( *i );
873 KigCommand* kc = 0;
874 if ( os.size() == 0 ) return;
875 else if ( os.size() == 1 )
876 kc = new KigCommand( *this, os[0]->imp()->type()->showAStatement() );
877 else kc = new KigCommand( *this, i18n( "Show %n Object", "Show %n Objects", os.size() ) );
878 for ( std::vector<ObjectHolder*>::iterator i = os.begin();
879 i != os.end(); ++i )
880 kc->addTask( new ChangeObjectDrawerTask( *i, ( *i )->drawer()->getCopyShown( true ) ) );
881 mhistory->addCommand( kc );
884 void KigPart::redrawScreen( KigWidget* w )
886 mode()->redrawScreen( w );
889 void KigPart::redrawScreen()
891 for ( std::vector<KigWidget*>::iterator i = mwidgets.begin();
892 i != mwidgets.end(); ++i )
894 mode()->redrawScreen( *i );
898 const KigDocument& KigPart::document() const
900 return *mdocument;
903 KigDocument& KigPart::document()
905 return *mdocument;
908 extern "C" int convertToNative( const KURL& url, const QCString& outfile )
910 kdDebug() << "converting " << url.prettyURL() << " to " << outfile << endl;
912 if ( ! url.isLocalFile() )
914 // TODO
915 kdError() << "--convert-to-native only supports local files for now." << endl;
916 return -1;
919 QString file = url.path();
921 QFileInfo fileinfo( file );
922 if ( ! fileinfo.exists() )
924 kdError() << "The file \"" << file << "\" does not exist" << endl;
925 return -1;
928 KMimeType::Ptr mimeType = KMimeType::findByPath ( file );
929 kdDebug() << k_funcinfo << "mimetype: " << mimeType->name() << endl;
930 KigFilter* filter = KigFilters::instance()->find( mimeType->name() );
931 if ( !filter )
933 kdError() << "The file \"" << file << "\" is of a filetype not currently supported by Kig." << endl;
934 return -1;
937 KigDocument* doc = filter->load (file);
938 if ( !doc )
940 kdError() << "Parse error in file \"" << file << "\"." << endl;
941 return -1;
944 std::vector<ObjectCalcer*> tmp = calcPath( getAllParents( getAllCalcers( doc->objects() ) ) );
945 for ( std::vector<ObjectCalcer*>::iterator i = tmp.begin(); i != tmp.end(); ++i )
946 ( *i )->calc( *doc );
947 for ( std::vector<ObjectCalcer*>::iterator i = tmp.begin(); i != tmp.end(); ++i )
948 ( *i )->calc( *doc );
950 QString out = ( outfile == "-" ) ? QString::null : outfile;
951 bool success = KigFilters::instance()->save( *doc, out );
952 if ( !success )
954 kdError() << "something went wrong while saving" << endl;
955 return -1;
958 delete doc;
960 return 0;
963 void KigPart::toggleGrid()
965 bool toshow = !mdocument->grid();
966 aToggleGrid->setChecked( toshow );
967 mdocument->setGrid( toshow );
969 redrawScreen();
972 void KigPart::toggleAxes()
974 bool toshow = !mdocument->axes();
975 aToggleAxes->setChecked( toshow );
976 mdocument->setAxes( toshow );
978 redrawScreen();
981 void KigPart::toggleNightVision()
983 bool nv = !mdocument->getNightVision();
984 aToggleNightVision->setChecked( nv );
985 mdocument->setNightVision( nv );
987 redrawScreen();
990 void KigPart::coordSystemChanged( int id )
992 aCoordSystem->setCurrentItem( id );
995 void KigPart::saveTypes()
997 QString typesDir = KGlobal::dirs()->saveLocation( "appdata", "kig-types" );
998 if ( typesDir[ typesDir.length() - 1 ] != '/' )
999 typesDir += '/';
1000 QString typesFileWithPath = typesDir + typesFile;
1002 // removing existant types file
1003 if ( QFile::exists( typesFileWithPath ) )
1004 QFile::remove( typesFileWithPath );
1006 MacroList* macrolist = MacroList::instance();
1007 macrolist->save( macrolist->macros(), typesFileWithPath );
1010 void KigPart::loadTypes()
1012 QString typesDir = KGlobal::dirs()->saveLocation( "appdata", "kig-types" );
1013 if ( typesDir[ typesDir.length() - 1 ] != '/' )
1014 typesDir += '/';
1015 QString typesFileWithPath = typesDir + typesFile;
1017 if ( QFile::exists( typesFileWithPath ) )
1019 std::vector<Macro*> macros;
1020 MacroList::instance()->load( typesFileWithPath, macros, *this );
1021 MacroList::instance()->add( macros );
1025 void KigPart::deleteTypes()
1027 unplugActionLists();
1028 typedef MacroList::vectype vec;
1029 MacroList* macrolist = MacroList::instance();
1030 const vec& macros = macrolist->macros();
1031 for ( vec::const_reverse_iterator i = macros.rbegin(); i != macros.rend(); ++i )
1033 macrolist->remove( *i );
1035 plugActionLists();