29 #include <QtCore/QFileInfo>
30 #include <QtCore/QDir>
31 #include <QtGui/QBoxLayout>
32 #include <QtGui/QHeaderView>
33 #include <QtGui/QTreeView>
34 #include <QtGui/QLabel>
42 #include <kfiledialog.h>
48 #include <kicondialog.h>
51 #include <kurlrequester.h>
56 using namespace Kross;
65 class ActionCollectionEditor::Private
68 enum Type { ActionType, CollectionType };
76 return type == ActionType ? action->name() : collection->name();
79 return type == ActionType ? action->text() : collection->text();
82 return type == ActionType ? action->description() : collection->description();
85 return type == ActionType ? action->iconName() : collection->iconName();
87 bool isEnabled()
const {
88 return type == ActionType ? action->isEnabled() : collection->isEnabled();
96 KUrlRequester* fileedit;
99 explicit Private(
Action* a) : type(ActionType), action(a) { Q_ASSERT(a); }
100 explicit Private(
ActionCollection* c) : type(CollectionType), collection(c) { Q_ASSERT(c); }
106 :
QWidget(parent), d(new Private(action))
112 :
QWidget(parent), d(new Private(collection))
124 return d->type == Private::ActionType ? d->action : 0;
129 return d->type == Private::CollectionType ? d->collection : 0;
141 QVBoxLayout* mainlayout =
new QVBoxLayout();
142 setLayout(mainlayout);
145 mainlayout->addWidget(w);
146 QGridLayout* gridlayout =
new QGridLayout();
147 gridlayout->setMargin(0);
149 w->setLayout(gridlayout);
152 gridlayout->addWidget(namelabel, 0, 0);
154 namelabel->setBuddy(d->nameedit);
155 d->nameedit->setText( d->name() );
156 d->nameedit->setEnabled(
false);
157 gridlayout->addWidget(d->nameedit, 0, 1);
160 gridlayout->addWidget(textlabel, 1, 0);
162 textlabel->setBuddy(d->textedit);
163 d->textedit->setText( d->text() );
164 gridlayout->addWidget(d->textedit, 1, 1);
167 gridlayout->addWidget(commentlabel, 2, 0);
169 commentlabel->setBuddy(d->commentedit);
170 d->commentedit->setText( d->description() );
171 gridlayout->addWidget(d->commentedit, 2, 1);
174 gridlayout->addWidget(iconlabel, 3, 0);
176 QHBoxLayout* iconlayout =
new QHBoxLayout();
177 iconlayout->setMargin(0);
178 iconbox->setLayout(iconlayout);
180 iconlabel->setBuddy(d->iconedit);
181 d->iconedit->setText( d->iconName() );
182 iconlayout->addWidget(d->iconedit, 1);
183 KIconButton* iconbutton =
new KIconButton(iconbox);
184 iconbutton->setIcon( d->iconName() );
185 connect(iconbutton, SIGNAL(iconChanged(
QString)), d->iconedit, SLOT(setText(
QString)));
186 iconlayout->addWidget(iconbutton);
187 gridlayout->addWidget(iconbox, 3, 1);
193 if( d->type == Private::ActionType ) {
195 gridlayout->addWidget(interpreterlabel, 4, 0);
197 interpreterlabel->setBuddy(d->interpreteredit);
198 d->interpreteredit->setMaxVisibleItems(10);
199 d->interpreteredit->insertItems(0,
Manager::self().interpreters());
200 d->interpreteredit->setEditable(
true);
204 d->interpreteredit->setCurrentIndex(idx);
206 d->interpreteredit->setEditText( d->action->interpreter() );
207 gridlayout->addWidget(d->interpreteredit, 4, 1);
210 gridlayout->addWidget(filelabel, 5, 0);
211 d->fileedit =
new KUrlRequester(w);
212 filelabel->setBuddy(d->fileedit);
217 mimetypes.append( info->
mimeTypes().join(
" ").trimmed() );
221 d->fileedit->fileDialog()->setMimeFilter(mimetypes );
222 d->fileedit->setMode( KFile::File | KFile::ExistingOnly | KFile::LocalOnly );
223 d->fileedit->setUrl(
KUrl(d->action->file()) );
224 gridlayout->addWidget(d->fileedit, 5, 1);
227 d->interpreteredit = 0;
236 mainlayout->addStretch(1);
242 return ! d->nameedit->text().isEmpty();
248 case Private::ActionType: {
249 d->action->setText( d->textedit->text() );
250 d->action->setDescription( d->commentedit->text() );
251 d->action->setIconName( d->iconedit->text() );
252 d->action->setInterpreter( d->interpreteredit->currentText() );
253 d->action->setFile( d->fileedit->url().path() );
256 case Private::CollectionType: {
257 d->collection->setText( d->textedit->text() );
258 d->collection->setDescription( d->commentedit->text() );
259 d->collection->setIconName( d->iconedit->text() );
273 class ActionCollectionView::Private
279 explicit Private() : modified(false) {}
289 setSelectionMode(QAbstractItemView::SingleSelection);
290 setAlternatingRowColors(
true);
291 setRootIsDecorated(
true);
292 setSortingEnabled(
false);
293 setItemsExpandable(
true);
296 setDropIndicatorShown(
true);
297 setDragDropMode(QAbstractItemView::InternalMove);
302 runaction->setObjectName(
"run");
303 runaction->setToolTip(
i18n(
"Execute the selected script.") );
304 runaction->setEnabled(
false);
305 d->collection->addAction(
"run", runaction);
306 connect(runaction, SIGNAL(triggered()),
this, SLOT(
slotRun()));
309 stopaction->setObjectName(
"stop");
310 stopaction->setToolTip(
i18n(
"Stop execution of the selected script.") );
311 stopaction->setEnabled(
false);
312 d->collection->addAction(
"stop", stopaction);
313 connect(stopaction, SIGNAL(triggered()),
this, SLOT(
slotStop()));
316 editaction->setObjectName(
"edit");
317 editaction->setToolTip(
i18n(
"Edit selected script.") );
318 editaction->setEnabled(
false);
319 d->collection->addAction(
"edit", editaction);
320 connect(editaction, SIGNAL(triggered()),
this, SLOT(
slotEdit()));
323 addaction->setObjectName(
"add");
324 addaction->setToolTip(
i18n(
"Add a new script.") );
326 d->collection->addAction(
"add", addaction);
327 connect(addaction, SIGNAL(triggered()),
this, SLOT(
slotAdd()) );
330 removeaction->setObjectName(
"remove");
331 removeaction->setToolTip(
i18n(
"Remove selected script.") );
332 removeaction->setEnabled(
false);
333 d->collection->addAction(
"remove", removeaction);
334 connect(removeaction, SIGNAL(triggered()),
this, SLOT(
slotRemove()) );
347 QTreeView::setModel(m);
351 setSelectionModel(selectionmodel);
353 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
355 connect(m, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
366 d->modified = modified;
371 return d->collection;
376 return d->buttons.contains(actionname) ? d->buttons[actionname] : 0;
382 QItemSelection selection = selectionModel()->selection();
383 return proxymodel ? proxymodel->mapSelectionToSource(selection) : selection;
388 QAction* action = d->collection->action(actionname);
389 if( ! action )
return 0;
392 btn->
setText( action->text() );
393 btn->setToolTip( action->toolTip() );
395 btn->setEnabled( action->isEnabled() );
396 if( parentWidget && parentWidget->layout() )
397 parentWidget->layout()->addWidget(btn);
398 QObject::connect(btn, SIGNAL(clicked()), action, SLOT(trigger()));
399 d->buttons.insert( actionname, btn );
405 if( d->buttons.contains( actionname ) ) {
406 QAction* action = d->collection->action( actionname );
407 d->buttons[ actionname ]->setEnabled( action ? action->isEnabled() : false );
413 bool startenabled = selectionModel()->hasSelection();
414 bool stopenabled =
false;
415 bool hasselection = selectionModel()->selectedIndexes().count() > 0;
416 foreach(
const QModelIndex &index,
itemSelection().indexes()) {
418 if( startenabled && ! action )
419 startenabled =
false;
423 QAction* runaction = d->collection->action(
"run");
425 runaction->setEnabled(startenabled);
428 QAction* stopaction = d->collection->action(
"stop");
430 stopaction->setEnabled(stopenabled);
433 QAction* editaction = d->collection->action(
"edit");
435 editaction->setEnabled(hasselection);
438 QAction* removeaction = d->collection->action(
"remove");
440 removeaction->setEnabled(hasselection);
452 if( ! selectionModel() )
return;
453 QAction* stopaction = d->collection->action(
"stop");
455 foreach(
const QModelIndex &index,
itemSelection().indexes()) {
456 if( ! index.isValid() )
459 stopaction->setEnabled(
true);
473 if( ! selectionModel() )
return;
474 foreach(
const QModelIndex &index,
itemSelection().indexes()) {
475 if( ! index.isValid() )
489 if( ! selectionModel() )
return;
492 foreach(
const QModelIndex &index,
itemSelection().indexes()) {
493 if( ! index.isValid() )
continue;
502 if( (! action) && (! collection) )
return;
504 dialog->setCaption(
i18n(
"Edit") );
505 dialog->setButtons( KDialog::Ok | KDialog::Cancel );
507 dialog->setFaceType( KPageDialog::Plain );
511 dialog->addPage(editor,
i18nc(
"@title:group Script properties",
"General"));
513 dialog->resize(
QSize(580, 200).expandedTo( dialog->minimumSizeHint() ) );
514 int result = dialog->exec();
515 if( result == QDialog::Accepted ) {
518 dialog->delayedDestruct();
531 if( ! selectionModel() )
return;
534 if( ! index.isValid() )
continue;
537 QModelIndex parent = index;
538 while( parent.isValid() && ! collection ) {
539 parent = d->view->model()->parent(parent);
542 if( collection )
break;
549 ScriptManagerAddWizard wizard(
this, collection);
550 int result = wizard.exec();
557 if( ! selectionModel() )
return;