moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kig / modes / typesdialog.cpp
blob367ab96bc1d5d150c9cc17dae1a4c32a093144b0
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 "typesdialog.h"
22 #include "typesdialog.moc"
24 #include "edittype.h"
25 #include "../kig/kig_part.h"
26 #include "../misc/guiaction.h"
27 #include "../misc/object_constructor.h"
29 #include <kapplication.h>
30 #include <kdebug.h>
31 #include <kfiledialog.h>
32 #include <kiconloader.h>
33 #include <klocale.h>
34 #include <kmessagebox.h>
35 #include <kpushbutton.h>
36 #include <kstdguiitem.h>
38 #include <qfile.h>
39 #include <qpixmap.h>
40 #include <qstringlist.h>
41 #include <qtextstream.h>
43 #include <algorithm>
44 #include <vector>
46 class MacroListElement
47 : public QListViewItem
49 Macro* macro;
50 public:
51 MacroListElement( KListView* lv, Macro* m );
52 Macro* getMacro() const { return macro; };
55 MacroListElement::MacroListElement( KListView* lv, Macro* m )
56 : QListViewItem( lv, QString::null, m->action->descriptiveName(), m->action->description() ),
57 macro( m )
61 TypesDialog::TypesDialog( QWidget* parent, KigPart& part )
62 : TypesDialogBase( parent, "types_dialog", true ), mpart( part )
64 // improving GUI look'n'feel...
65 buttonHelp->setGuiItem( KStdGuiItem::help() );
66 buttonOk->setGuiItem( KStdGuiItem::ok() );
67 buttonCancel->setGuiItem( KStdGuiItem::cancel() );
68 il = part.instance()->iconLoader();
69 buttonEdit->setIconSet( QIconSet( il->loadIcon( "edit", KIcon::Small ) ) );
70 buttonRemove->setIconSet( QIconSet( il->loadIcon( "editdelete", KIcon::Small ) ) );
71 buttonExport->setIconSet( QIconSet( il->loadIcon( "fileexport", KIcon::Small ) ) );
72 buttonImport->setIconSet( QIconSet( il->loadIcon( "fileimport", KIcon::Small ) ) );
74 typeList->setColumnWidth( 0, 22 );
75 typeList->setColumnWidth( 1, 140 );
76 typeList->setColumnWidth( 2, 240 );
78 // loading macros...
79 loadAllMacros();
81 popup = new QPopupMenu( this );
82 popup->insertItem( SmallIcon( "edit" ), i18n( "&Edit..." ), this, SLOT( editType() ) );
83 popup->insertItem( SmallIcon( "editdelete" ), i18n( "&Delete" ), this, SLOT( deleteType() ) );
84 popup->insertSeparator();
85 popup->insertItem( SmallIcon( "fileexport" ), i18n( "E&xport..." ), this, SLOT( exportType() ) );
87 // saving types
88 part.saveTypes();
91 QListViewItem* TypesDialog::newListItem( Macro* m )
93 MacroListElement* e = new MacroListElement( typeList, m );
94 QCString ifn = m->action->iconFileName();
95 if ( !ifn.isNull() )
97 QPixmap p = il->loadIcon( ifn, KIcon::Small );
98 e->setPixmap( 0, p );
100 return e;
103 TypesDialog::~TypesDialog()
107 void TypesDialog::helpSlot()
109 kapp->invokeHelp( QString::fromLatin1( "working-with-types" ),
110 QString::fromLatin1( "kig" ) );
113 void TypesDialog::okSlot()
115 mpart.saveTypes();
116 mpart.deleteTypes();
117 mpart.loadTypes();
118 accept();
121 void TypesDialog::deleteType()
123 std::vector<QListViewItem*> items;
124 std::vector<Macro*> selectedTypes;
125 QListViewItemIterator it( typeList );
126 while ( it.current() ) {
127 if ( ( it.current() )->isSelected() )
129 items.push_back( it.current() );
130 selectedTypes.push_back( static_cast<MacroListElement*>( it.current() )->getMacro() );
132 ++it;
134 if (selectedTypes.empty()) return;
135 QStringList types;
136 for ( std::vector<Macro*>::iterator j = selectedTypes.begin();
137 j != selectedTypes.end(); ++j )
138 types << ( *j )->action->descriptiveName();
139 if ( KMessageBox::warningContinueCancelList( this,
140 i18n( "Are you sure you want to delete this type?",
141 "Are you sure you want to delete these %n types?", selectedTypes.size() ),
142 types, i18n("Are You Sure?"), KStdGuiItem::cont(),
143 "deleteTypeWarning") == KMessageBox::Cancel )
144 return;
145 for ( std::vector<QListViewItem*>::iterator i = items.begin(); i != items.end(); ++i)
147 int appel = typeList->itemIndex(*i);
148 assert (appel != -1);
149 delete *i;
151 for ( std::vector<Macro*>::iterator j = selectedTypes.begin();
152 j != selectedTypes.end(); ++j)
153 MacroList::instance()->remove( *j );
156 void TypesDialog::exportType()
158 std::vector<Macro*> types;
159 QListViewItemIterator it( typeList );
160 while ( it.current() ) {
161 if ( ( it.current() )->isSelected() )
163 types.push_back( static_cast<MacroListElement*>( it.current() )->getMacro() );
165 ++it;
167 if (types.empty()) return;
168 QString file_name = KFileDialog::getSaveFileName(":macro", i18n("*.kigt|Kig Types Files\n*|All Files"), this, i18n( "Export Types" ) );
169 if ( file_name.isNull() )
170 return;
171 QFile fi( file_name );
172 if ( fi.exists() )
173 if ( KMessageBox::questionYesNo( this, i18n( "The file \"%1\" already exists. "
174 "Do you wish to overwrite it?" ).arg( fi.name() ),
175 i18n( "Overwrite File?" ) ) == KMessageBox::No )
176 return;
177 MacroList::instance()->save( types, file_name );
180 void TypesDialog::importTypes()
182 QStringList file_names =
183 KFileDialog::getOpenFileNames(":importTypes", i18n("*.kigt|Kig Types Files\n*|All Files"), this, i18n( "Import Types" ));
185 std::vector<Macro*> macros;
187 for ( QStringList::Iterator i = file_names.begin();
188 i != file_names.end(); ++i)
190 std::vector<Macro*> nmacros;
191 bool ok = MacroList::instance()->load( *i, nmacros, mpart );
192 if ( ! ok )
193 continue;
194 std::copy( nmacros.begin(), nmacros.end(), std::back_inserter( macros ) );
196 MacroList::instance()->add( macros );
198 for ( uint i = 0; i < macros.size(); ++i )
199 typeList->insertItem( newListItem( macros[i] ) );
202 QString TypesDialog::fetchIconFromListItem( QListViewItem* i )
204 QListViewItemIterator it( typeList );
205 Macro* ai = static_cast<MacroListElement*>( i )->getMacro();
206 while ( it.current() ) {
207 if ( ( it.current() )->isSelected() )
209 Macro* ait = static_cast<MacroListElement*>( it.current() )->getMacro();
210 if ( ai == ait )
212 return ai->ctor->iconFileName( true );
215 ++it;
217 return "gear";
220 void TypesDialog::editType()
222 std::vector<QListViewItem*> items;
223 QListViewItemIterator it( typeList );
224 while ( it.current() ) {
225 if ( ( it.current() )->isSelected() )
226 items.push_back( it.current() );
227 ++it;
229 if ( items.size() == 0 )
230 return;
231 if ( items.size() > 1 )
233 KMessageBox::sorry( this,
234 i18n( "There is more than one type selected. You can "
235 "only edit one type at a time. Please select "
236 "only the type you want to edit and try again." ),
237 i18n( "More Than One Type Selected" ) );
238 return;
240 QListViewItem* i = items[0];
241 EditType* d = new EditType( this, i->text( 1 ), i->text( 2 ), fetchIconFromListItem( i ) );
242 if ( d->exec() )
244 QString newname = d->name();
245 QString newdesc = d->description();
246 QString newicon = d->icon();
248 Macro* oldmacro = static_cast<MacroListElement*>( i )->getMacro();
249 // mpart.unplugActionLists();
250 oldmacro->ctor->setName( newname );
251 oldmacro->ctor->setDescription( newdesc );
252 QCString ncicon( newicon.utf8() );
253 oldmacro->ctor->setIcon( ncicon );
254 // mpart.plugActionLists();
256 typeList->clear();
258 loadAllMacros();
260 delete d;
263 void TypesDialog::contextMenuRequested( QListViewItem*, const QPoint& p, int )
265 popup->exec( p );
268 void TypesDialog::loadAllMacros()
270 const vec& macros = MacroList::instance()->macros();
271 for ( vec::const_reverse_iterator i = macros.rbegin(); i != macros.rend(); ++i )
273 typeList->insertItem( newListItem( *i ) );
277 void TypesDialog::cancelSlot()
279 mpart.deleteTypes();
280 mpart.loadTypes();
281 reject();