22 #include "typesdialog.moc"
25 #include "ui_typeswidget.h"
26 #include "../kig/kig_part.h"
27 #include "../misc/guiaction.h"
28 #include "../misc/object_constructor.h"
30 #include <kfiledialog.h>
31 #include <kiconloader.h>
33 #include <kmessagebox.h>
34 #include <kpushbutton.h>
35 #include <ktoolinvocation.h>
37 #include <qbytearray.h>
42 #include <qstringlist.h>
51 while ( delta + col < str.
length() )
53 int pos = delta + col;
55 ret << str.
mid( delta, pos - delta );
58 ret << str.
mid( delta );
59 return ret.
join(
"<br>" );
68 virtual ~BaseListElement();
70 virtual bool isMacro()
const {
return false; }
71 virtual QString name()
const = 0;
72 virtual QString description()
const = 0;
73 virtual QString icon(
bool canNull =
false )
const = 0;
74 virtual QString type()
const = 0;
77 BaseListElement::BaseListElement()
81 BaseListElement::~BaseListElement()
85 class MacroListElement
86 :
public BaseListElement
90 MacroListElement(
Macro* m );
91 virtual ~MacroListElement();
93 Macro* getMacro()
const {
return mmacro; }
94 virtual bool isMacro()
const {
return true; }
96 virtual QString description()
const;
97 virtual QString icon(
bool canNull =
false )
const;
101 MacroListElement::MacroListElement(
Macro* m )
102 : BaseListElement(), mmacro( m )
106 MacroListElement::~MacroListElement()
113 QString MacroListElement::name()
const
115 return mmacro->action->descriptiveName();
118 QString MacroListElement::description()
const
120 return mmacro->action->description();
123 QString MacroListElement::icon(
bool canNull )
const
125 return mmacro->ctor->iconFileName( canNull );
128 QString MacroListElement::type()
const
130 return i18n(
"Macro" );
145 if ( macros.size() < 1 )
150 for ( std::vector<Macro*>::const_iterator it = macros.begin();
151 it != macros.end(); ++it )
153 melems.push_back(
new MacroListElement( *it ) );
168 for (
int i = elems.count(); i > 0; --i )
171 if ( !index.
isValid() || index.
row() < 0 || index.
row() >=
static_cast<int>( melems.size() )
175 BaseListElement* element = melems[ index.
row() ];
179 for ( std::vector<BaseListElement*>::iterator mit = melems.begin();
180 mit != melems.end() && !found; )
182 if ( *mit == element )
188 mit = melems.erase( mit );
202 for ( std::vector<BaseListElement*>::const_iterator it = melems.begin();
203 it != melems.end(); ++it )
210 if ( !index.
isValid() || index.
row() < 0 || index.
row() >=
static_cast<int>( melems.size() )
221 if ( !index.
isValid() || index.
row() < 0 || index.
row() >=
static_cast<int>( melems.size() ) )
224 return melems[ index.
row() ]->isMacro();
229 if ( !index.
isValid() || index.
row() < 0 || index.
row() >=
static_cast<int>( melems.size() ) )
232 BaseListElement* el = melems[ index.
row() ];
233 if ( !el->isMacro() )
236 return static_cast<MacroListElement*
>( el )->getMacro();
241 return parent.
isValid() ? 0 : 3;
249 if ( ( index.
row() < 0 ) || ( index.
row() >=
static_cast<int>( melems.size() ) ) )
254 case Qt::DecorationRole:
256 if ( index.
column() == 1 )
257 return KIcon( melems[ index.
row() ]->icon() );
262 case Qt::DisplayRole:
266 case 0:
return melems[ index.
row() ]->type();
break;
267 case 1:
return melems[ index.
row() ]->name();
break;
268 case 2:
return melems[ index.
row() ]->description();
break;
274 case Qt::ToolTipRole:
276 static QString macro_with_image(
277 "<qt><table cellspacing=\"5\"><tr><td><b>%1</b> (%4)</td>"
278 "<td rowspan=\"2\" align=\"right\"><img src=\"%3\"></td></tr>"
279 "<tr><td>%2</td></tr></table></qt>" );
281 "<qt><b>%1</b> (%3)<br>%2</qt>" );
283 if ( melems[ index.
row() ]->icon(
true ).isEmpty() )
284 return macro_no_image
285 .arg( melems[ index.
row() ]->name() )
286 .arg(
wrapAt( melems[ index.
row() ]->description() ) )
287 .
arg( melems[ index.
row() ]->type() );
289 return macro_with_image
290 .
arg( melems[ index.
row() ]->name() )
291 .arg(
wrapAt( melems[ index.
row() ]->description() ) )
292 .
arg( KIconLoader::global()->iconPath( melems[ index.
row() ]->icon(), - KIconLoader::SizeMedium ) )
293 .
arg( melems[ index.
row() ]->type() );
302 if ( orientation != Qt::Horizontal )
305 if ( role == Qt::TextAlignmentRole )
308 if ( role != Qt::DisplayRole )
313 case 0:
return i18n(
"Type" );
break;
314 case 1:
return i18n(
"Name" );
break;
315 case 2:
return i18n(
"Description" );
break;
323 if ( parent.
isValid() || row < 0 || row >=
static_cast<int>( melems.size() ) || column < 0 || column > 3 )
331 return parent.
isValid() ? 0 : melems.size();
339 setCaption( i18n(
"Manage Types" ) );
340 setButtons( Help | Ok | Cancel );
343 setMainWidget( base );
344 mtypeswidget =
new Ui_TypesWidget();
345 mtypeswidget->setupUi( base );
349 mmodel =
new TypesModel( mtypeswidget->typeList );
350 mtypeswidget->typeList->setModel( mmodel );
352 mtypeswidget->typeList->setContextMenuPolicy( Qt::CustomContextMenu );
355 mtypeswidget->buttonEdit->setIcon( KIcon(
"document-properties" ) );
356 mtypeswidget->buttonRemove->setIcon( KIcon(
"edit-delete" ) );
357 mtypeswidget->buttonExport->setIcon( KIcon(
"document-export" ) );
358 mtypeswidget->buttonImport->setIcon( KIcon(
"document-import" ) );
365 mtypeswidget->typeList->resizeColumnToContents( 0 );
367 popup =
new QMenu(
this );
368 popup->
addAction( KIcon(
"document-properties" ), i18n(
"&Edit..." ),
this, SLOT( editType() ) );
369 popup->
addAction( KIcon(
"edit-delete" ), i18n(
"&Delete" ),
this, SLOT( deleteType() ) );
371 popup->
addAction( KIcon(
"document-export" ), i18n(
"E&xport..." ),
this, SLOT( exportType() ) );
376 connect( mtypeswidget->buttonExport, SIGNAL( clicked() ),
this, SLOT( exportType() ) );
377 connect( mtypeswidget->buttonImport, SIGNAL( clicked() ),
this, SLOT( importTypes() ) );
378 connect( mtypeswidget->buttonRemove, SIGNAL( clicked() ),
this, SLOT( deleteType() ) );
379 connect( mtypeswidget->buttonEdit, SIGNAL( clicked() ),
this, SLOT( editType() ) );
380 connect( mtypeswidget->typeList, SIGNAL( customContextMenuRequested(
const QPoint& ) ),
this, SLOT( typeListContextMenu(
const QPoint& ) ) );
381 connect(
this, SIGNAL( helpClicked() ),
this, SLOT( slotHelp() ) );
382 connect(
this, SIGNAL( okClicked() ),
this, SLOT( slotOk() ) );
383 connect(
this, SIGNAL( cancelClicked() ),
this, SLOT( slotCancel() ) );
393 void TypesDialog::slotHelp()
395 KToolInvocation::invokeHelp(
"working-with-types",
"kig" );
398 void TypesDialog::slotOk()
406 void TypesDialog::deleteType()
408 std::vector<Macro*> selectedTypes;
409 QModelIndexList indexes = selectedRows();
410 for ( QModelIndexList::const_iterator it = indexes.constBegin(); it != indexes.constEnd(); ++it )
415 selectedTypes.push_back( macro );
419 if (selectedTypes.empty())
return;
421 for ( std::vector<Macro*>::iterator j = selectedTypes.begin();
422 j != selectedTypes.end(); ++j )
423 types << ( *j )->action->descriptiveName();
425 if ( KMessageBox::warningContinueCancelList(
this,
426 i18np(
"Are you sure you want to delete this type?",
427 "Are you sure you want to delete these %1 types?", selectedTypes.size() ),
428 types, i18n(
"Are You Sure?"), KStandardGuiItem::cont(), KStandardGuiItem::cancel(),
429 "deleteTypeWarning") == KMessageBox::Cancel )
431 bool updates = mtypeswidget->typeList->updatesEnabled();
432 mtypeswidget->typeList->setUpdatesEnabled(
false );
434 mtypeswidget->typeList->setUpdatesEnabled( updates );
435 for ( std::vector<Macro*>::iterator j = selectedTypes.begin();
436 j != selectedTypes.end(); ++j)
440 void TypesDialog::exportType()
442 std::vector<Macro*> types;
443 QModelIndexList indexes = selectedRows();
444 for ( QModelIndexList::const_iterator it = indexes.constBegin(); it != indexes.constEnd(); ++it )
448 types.push_back( macro );
450 if (types.empty())
return;
451 QString file_name = KFileDialog::getSaveFileName( KUrl(
"kfiledialog:///macro" ), i18n(
"*.kigt|Kig Types Files\n*|All Files"),
this, i18n(
"Export Types" ) );
454 QFile fi( file_name );
456 if ( KMessageBox::warningContinueCancel(
this, i18n(
"The file \"%1\" already exists. "
457 "Do you wish to overwrite it?", fi.fileName() ),
458 i18n(
"Overwrite File?" ), KStandardGuiItem::overwrite() ) == KMessageBox::Cancel )
463 void TypesDialog::importTypes()
466 KFileDialog::getOpenFileNames( KUrl(
"kfiledialog:///importTypes" ), i18n(
"*.kigt|Kig Types Files\n*|All Files"),
this, i18n(
"Import Types" ));
468 std::vector<Macro*> macros;
473 std::vector<Macro*> nmacros;
477 std::copy( nmacros.begin(), nmacros.end(), std::back_inserter( macros ) );
483 mtypeswidget->typeList->resizeColumnToContents( 0 );
486 void TypesDialog::editType()
488 QModelIndexList indexes = selectedRows();
489 if ( indexes.isEmpty() )
491 if ( indexes.count() > 1 )
493 KMessageBox::sorry(
this,
494 i18n(
"There is more than one type selected. You can "
495 "only edit one type at a time. Please select "
496 "only the type you want to edit and try again." ),
497 i18n(
"More Than One Type Selected" ) );
500 bool refresh =
false;
502 if ( mmodel->
isMacro( index ) )
507 if ( editdialog.exec() )
509 QString newname = editdialog.name();
510 QString newdesc = editdialog.description();
511 QString newicon = editdialog.icon();
528 void TypesDialog::slotCancel()
535 QModelIndexList TypesDialog::selectedRows()
const
537 QModelIndexList indexes = mtypeswidget->typeList->selectionModel()->selectedRows();
542 void TypesDialog::typeListContextMenu(
const QPoint& pos )
544 QModelIndexList indexes = mtypeswidget->typeList->selectionModel()->selectedRows();
545 if ( indexes.isEmpty() )
548 popup->
exec( mtypeswidget->typeList->viewport()->mapToGlobal( pos ) );
A model for representing the data.
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Simply dialog that allow the user the editing of a macro type...
QString join(const QString &separator) const
static QString wrapAt(const QString &str, int col=50)
This file is part of Kig, a KDE program for Interactive Geometry...
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
const QByteArray iconFileName(const bool canBeNull=false) const
void add(Macro *m)
Add a Macro m .
Macro * macroFromIndex(const QModelIndex &index) const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
TypesDialog(QWidget *parent, KigPart &)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
void removeElements(const QModelIndexList &elems)
void elementChanged(const QModelIndex &index)
void setMargin(int margin)
QModelIndex createIndex(int row, int column, void *ptr) const
void setDescription(const QString &desc)
void beginInsertRows(const QModelIndex &parent, int first, int last)
void setName(const QString &name)
QString mid(int position, int n) const
bool save(Macro *m, const QString &f)
Save macro m to file f .
virtual QString descriptiveName() const =0
void addMacros(const std::vector< Macro * > ¯os)
const QChar at(int position) const
this is just a simple data struct.
bool load(const QString &f, vectype &ret, const KigPart &)
load macro's from file f .
void setIcon(QByteArray &icon)
void remove(Macro *m)
Remove macro m .
const_iterator constEnd() const
const_iterator constBegin() const
bool isMacro(const QModelIndex &index) const
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
TypesModel(QObject *parent=0)
static MacroList * instance()
MacroList is a singleton.
virtual QString description() const =0
QByteArray toUtf8() const