18#include <kpushbutton.h>
19#include <tdelistview.h>
23#include <tdemessagebox.h>
26#include <tdepopupmenu.h>
28#include <tqinputdialog.h>
30#include <tqcheckbox.h>
31#include <tqwhatsthis.h>
32#include <tqdragobject.h>
36#include <kiconloader.h>
37#include <tdeshortcut.h>
39#include <kkeybutton.h>
41#include "snippetdlg.h"
42#include "snippetitem.h"
43#include "snippetwidget.h"
47SnippetWidget::SnippetWidget(KMEdit* editor, TDEActionCollection* actionCollection, TQWidget* parent)
48 : TDEListView(parent, "snippet widget"), TQToolTip( viewport() ),
49 mEditor( editor ), mActionCollection( actionCollection )
52 _list.setAutoDelete( true);
61 setDropVisualizer( false);
62 setRootIsDecorated( true);
65 connect( this, TQ_SIGNAL( contextMenuRequested ( TQListViewItem *, const TQPoint & , int ) ),
66 this, TQ_SLOT( showPopupMenu(TQListViewItem *, const TQPoint & , int ) ) );
68 connect( this, TQ_SIGNAL( doubleClicked (TQListViewItem *) ),
69 this, TQ_SLOT( slotEdit( TQListViewItem *) ) );
70 connect( this, TQ_SIGNAL( returnPressed (TQListViewItem *) ),
71 this, TQ_SLOT( slotExecuted( TQListViewItem *) ) );
73 connect( this, TQ_SIGNAL( dropped(TQDropEvent *, TQListViewItem *) ),
74 this, TQ_SLOT( slotDropped(TQDropEvent *, TQListViewItem *) ) );
76 connect( editor, TQ_SIGNAL( insertSnippet() ), this, TQ_SLOT( slotExecute() ));
80 TQTimer::singleShot(0, this, TQ_SLOT(initConfig()));
83SnippetWidget::~SnippetWidget()
91 while (_list.count() > 0) {
92 for (item=_list.first(); item; item=_list.next()) {
93 if (item->childCount() == 0)
107 SnippetDlg dlg( mActionCollection, this, "SnippetDlg");
113 if ( !group && selectedItem() )
114 group = dynamic_cast<SnippetGroup* >(selectedItem()->parent());
118 if ( _list.isEmpty() ) {
119 group = new SnippetGroup( this, i18n( "General"), SnippetGroup::getMaxId() );
120 _list.append( group );
128 for ( SnippetItem *it=_list.first(); it; it=_list.next()) {
130 dlg.cbGroup->insertItem(it->getName());
133 dlg.cbGroup->setCurrentText(group->getName());
135 if (dlg.exec() == TQDialog::Accepted) {
136 group = dynamic_cast<SnippetGroup* >(SnippetItem::findItemByName(dlg.cbGroup->currentText(), _list));
137 _list.append( makeItem( group, dlg.snippetName->text(), dlg.snippetText->text(), dlg.keyButton->shortcut() ) );
145SnippetItem* SnippetWidget::makeItem( SnippetItem* parent, const TQString& name, const TQString& text, const TDEShortcut& shortcut )
148 const TQString actionName = i18n( "Snippet %1").arg(name);
149 const TQString normalizedName = TQString(actionName).replace( " ", "_");
150 if ( !mActionCollection->action(normalizedName.utf8().data() ) ) {
151 TDEAction * action = new TDEAction( actionName, shortcut, item,
152 TQ_SLOT( slotExecute() ), mActionCollection,
153 normalizedName.utf8() );
154 item->setAction(action);
155 connect( item, TQ_SIGNAL( execute( TQListViewItem* ) ),
156 this, TQ_SLOT( slotExecuted( TQListViewItem* ) ) );
168 SnippetDlg dlg( mActionCollection, this, "SnippetDlg");
169 dlg.setShowShortcut( false );
170 dlg.snippetText->setEnabled( false);
171 dlg.snippetText->setText(i18n( "GROUP"));
172 dlg.setCaption(i18n( "Add Group"));
173 dlg.cbGroup->insertItem(i18n( "All"));
174 dlg.cbGroup->setCurrentText(i18n( "All"));
176 if (dlg.exec() == TQDialog::Accepted) {
177 _list.append( new SnippetGroup( this, dlg.snippetName->text(), SnippetGroup::getMaxId() ) );
189 TQListViewItem * item = currentItem();
196 if (group->childCount() > 0 &&
197 KMessageBox::warningContinueCancel( this, i18n( "Do you really want to remove this group and all its snippets?"),TQString(),KStdGuiItem::del())
198 == KMessageBox::Cancel)
203 if (it->getParent() == group->getId()) {
207 _list.remove(doomed);
227 item = currentItem();
232 if (!pSnippet || pGroup)
236 SnippetDlg dlg( mActionCollection, this, "SnippetDlg");
237 dlg.snippetName->setText(pSnippet->getName());
238 dlg.snippetText->setText(pSnippet->getText());
239 dlg.keyButton->setShortcut( pSnippet->getAction()->shortcut(), false );
240 dlg.btnAdd->setText(i18n( "&Apply"));
242 dlg.setCaption(i18n( "Edit Snippet"));
244 for ( SnippetItem *it=_list.first(); it; it=_list.next()) {
246 dlg.cbGroup->insertItem(it->getName());
249 dlg.cbGroup->setCurrentText(SnippetItem::findGroupById(pSnippet->getParent(), _list)->getName());
251 if (dlg.exec() == TQDialog::Accepted) {
253 item->setText( 0, dlg.snippetName->text() );
254 pSnippet->setName( dlg.snippetName->text() );
255 pSnippet->setText( dlg.snippetText->text() );
256 pSnippet->getAction()->setShortcut( dlg.keyButton->shortcut());
259 if ( SnippetItem::findGroupById(pSnippet->getParent(), _list)->getName() != dlg.cbGroup->currentText() ) {
260 SnippetGroup * newGroup = dynamic_cast<SnippetGroup* >(SnippetItem::findItemByName(dlg.cbGroup->currentText(), _list));
261 pSnippet->parent()->takeItem(pSnippet);
262 newGroup->insertItem(pSnippet);
263 pSnippet->resetParent();
266 setSelected(item, true);
277 TQListViewItem * item = currentItem();
284 SnippetDlg dlg( mActionCollection, this, "SnippetDlg" );
285 dlg.setShowShortcut( false );
286 dlg.snippetName->setText(pGroup->getName());
287 dlg.snippetText->setText(pGroup->getText());
288 dlg.btnAdd->setText(i18n( "&Apply"));
289 dlg.snippetText->setEnabled( false);
290 dlg.setCaption(i18n( "Edit Group"));
291 dlg.cbGroup->insertItem(i18n( "All"));
293 if (dlg.exec() == TQDialog::Accepted) {
295 item->setText( 0, dlg.snippetName->text() );
296 pGroup->setName( dlg.snippetName->text() );
298 setSelected(item, true);
302void SnippetWidget::slotExecuted(TQListViewItem * item)
306 item = currentItem();
314 insertIntoActiveView( parseText(pSnippet->getText(), _SnippetConfig.getDelimiter()) );
322void SnippetWidget::insertIntoActiveView( const TQString &text )
324 mEditor->insert( text );
336 _cfg->deleteGroup( "SnippetPart");
338 _cfg->setGroup( "SnippetPart");
340 TQString strKeyName= "";
341 TQString strKeyText= "";
342 TQString strKeyId= "";
355 strKeyName=TQString( "snippetGroupName_%1").arg(iGroupCount);
356 strKeyId=TQString( "snippetGroupId_%1").arg(iGroupCount);
358 _cfg->writeEntry(strKeyName, group->getName());
359 _cfg->writeEntry(strKeyId, group->getId());
364 strKeyName=TQString( "snippetName_%1").arg(iSnipCount);
365 strKeyText=TQString( "snippetText_%1").arg(iSnipCount);
366 strKeyId=TQString( "snippetParent_%1").arg(iSnipCount);
368 _cfg->writeEntry(strKeyName, item->getName());
369 _cfg->writeEntry(strKeyText, item->getText());
370 _cfg->writeEntry(strKeyId, item->getParent());
372 TDEAction * action = item->getAction();
374 const TDEShortcut& sc = action->shortcut();
376 _cfg->writeEntry( TQString( "snippetShortcut_%1").arg(iSnipCount), sc.toString() );
384 _cfg->writeEntry( "snippetCount", iSnipCount);
385 _cfg->writeEntry( "snippetGroupCount", iGroupCount);
388 TQMap<TQString, TQString>::Iterator it;
389 for ( it = _mapSaved.begin(); it != _mapSaved.end(); ++it ) {
390 if (it.data().length()<=0) continue;
392 strKeyName=TQString( "snippetSavedName_%1").arg(iCount);
393 strKeyText=TQString( "snippetSavedVal_%1").arg(iCount);
395 _cfg->writeEntry(strKeyName, it.key());
396 _cfg->writeEntry(strKeyText, it.data());
400 _cfg->writeEntry( "snippetSavedCount", iCount-1);
403 _cfg->writeEntry( "snippetDelimiter", _SnippetConfig.getDelimiter() );
404 _cfg->writeEntry( "snippetVarInput", _SnippetConfig.getInputMethod() );
405 _cfg->writeEntry( "snippetToolTips", _SnippetConfig.useToolTips() );
406 _cfg->writeEntry( "snippetGroupAutoOpen", _SnippetConfig.getAutoOpenGroups() );
408 _cfg->writeEntry( "snippetSingleRect", _SnippetConfig.getSingleRect() );
409 _cfg->writeEntry( "snippetMultiRect", _SnippetConfig.getMultiRect() );
418void SnippetWidget::initConfig()
421 _cfg = new TDEConfig( "kmailsnippetrc", false, false);
423 _cfg->setGroup( "SnippetPart");
425 TQString strKeyName= "";
426 TQString strKeyText= "";
427 TQString strKeyId= "";
434 int iCount = _cfg->readNumEntry( "snippetGroupCount", -1);
436 for ( int i=0; i<iCount; i++) {
437 strKeyName=TQString( "snippetGroupName_%1").arg(i);
438 strKeyId=TQString( "snippetGroupId_%1").arg(i);
440 TQString strNameVal= "";
443 strNameVal = _cfg->readEntry(strKeyName, "");
444 iIdVal = _cfg->readNumEntry(strKeyId, -1);
447 if (strNameVal != "" && iIdVal != -1) {
461 iCount = _cfg->readNumEntry( "snippetCount", 0);
462 for ( int i=0; i<iCount; i++) {
463 strKeyName=TQString( "snippetName_%1").arg(i);
464 strKeyText=TQString( "snippetText_%1").arg(i);
465 strKeyId=TQString( "snippetParent_%1").arg(i);
467 TQString strNameVal= "";
468 TQString strTextVal= "";
471 strNameVal = _cfg->readEntry(strKeyName, "");
472 strTextVal = _cfg->readEntry(strKeyText, "");
473 iParentVal = _cfg->readNumEntry(strKeyId, -1);
476 if (strNameVal != "" && strTextVal != "" && iParentVal != -1) {
477 TDEShortcut shortcut( _cfg->readEntry( TQString( "snippetShortcut_%1").arg(i), TQString() ) );
478 item = makeItem( SnippetItem::findGroupById(iParentVal, _list), strNameVal, strTextVal, shortcut );
487 iCount = _cfg->readNumEntry( "snippetSavedCount", 0);
489 for ( int i=1; i<=iCount; i++) {
490 strKeyName=TQString( "snippetSavedName_%1").arg(i);
491 strKeyText=TQString( "snippetSavedVal_%1").arg(i);
493 TQString strNameVal= "";
494 TQString strTextVal= "";
496 strNameVal = _cfg->readEntry(strKeyName, "");
497 strTextVal = _cfg->readEntry(strKeyText, "");
499 if (strNameVal != "" && strTextVal != "") {
500 _mapSaved[strNameVal] = strTextVal;
505 _SnippetConfig.setDelimiter( _cfg->readEntry( "snippetDelimiter", "$") );
506 _SnippetConfig.setInputMethod( _cfg->readNumEntry( "snippetVarInput", 0) );
507 _SnippetConfig.setToolTips( _cfg->readBoolEntry( "snippetToolTips", true) );
508 _SnippetConfig.setAutoOpenGroups( _cfg->readNumEntry( "snippetGroupAutoOpen", 1) );
510 _SnippetConfig.setSingleRect( _cfg->readRectEntry( "snippetSingleRect", 0L) );
511 _SnippetConfig.setMultiRect( _cfg->readRectEntry( "snippetMultiRect", 0L) );
524 TQRect r = itemRect( item );
527 _SnippetConfig.useToolTips() )
529 tip( r, item->getText() );
543 popup.insertTitle( selectedItem->getName() );
545 popup.insertItem( i18n( "Edit &group..."), this, TQ_SLOT( slotEditGroup() ) );
547 popup.insertItem( SmallIconSet( "edit-paste"), i18n( "&Paste"), this, TQ_SLOT( slotExecuted() ) );
548 popup.insertItem( SmallIconSet( "edit"), i18n( "&Edit..."), this, TQ_SLOT( slotEdit() ) );
550 popup.insertItem( SmallIconSet( "edit-delete"), i18n( "&Remove"), this, TQ_SLOT( slotRemove() ) );
551 popup.insertSeparator();
553 popup.insertTitle(i18n( "Text Snippets"));
555 popup.insertItem( i18n( "&Add Snippet..."), this, TQ_SLOT( slotAdd() ) );
556 popup.insertItem( i18n( "Add G&roup..."), this, TQ_SLOT( slotAddGroup() ) );
567TQString SnippetWidget::parseText(TQString text, TQString del)
570 TQString strName = "";
571 TQString strNew = "";
575 TQMap<TQString, TQString> mapVar;
576 int iInMeth = _SnippetConfig.getInputMethod();
577 TQRect rSingle = _SnippetConfig.getSingleRect();
578 TQRect rMulti = _SnippetConfig.getMultiRect();
581 iFound = text.find(TQRegExp( "\\"+del+ "[A-Za-z-_0-9\\s]*\\"+del), iEnd+1);
583 iEnd = text.find(del, iFound+1)+1;
584 strName = text.mid(iFound, iEnd-iFound);
586 if ( strName != del+del ) {
588 if ( mapVar[strName].length() <= 0 ) {
589 strMsg=i18n( "Please enter the value for <b>%1</b>:").arg(strName);
590 strNew = showSingleVarDialog( strName, &_mapSaved, rSingle );
604 str.replace(strName, strNew);
607 mapVar[strName] = strNew;
609 } while (iFound != -1);
614 bh = rMulti.height();
616 if (showMultiVarDialog( &mapVar, &_mapSaved, w, bh, oh )) {
617 TQMap<TQString, TQString>::Iterator it;
618 for ( it = mapVar.begin(); it != mapVar.end(); ++it ) {
619 str.replace(it.key(), it.data());
626 rMulti.setHeight(bh);
629 _SnippetConfig.setMultiRect(rMulti);
632 _SnippetConfig.setSingleRect(rSingle);
644bool SnippetWidget::showMultiVarDialog(TQMap<TQString, TQString> * map, TQMap<TQString, TQString> * mapSave,
645 int & iWidth, int & iBasicHeight, int & iOneHeight)
648 if (map->count() == 0)
652 TQMap<TQString, TQString>::Iterator it = map->begin();
653 if ( map->count() == 1 && it.data()==_SnippetConfig.getDelimiter()+_SnippetConfig.getDelimiter() )
656 TQMap<TQString, KTextEdit *> mapVar2Te;
657 TQMap<TQString, TQCheckBox *> mapVar2Cb;
661 dlg.setCaption(i18n( "Enter Values for Variables"));
663 TQGridLayout * layout = new TQGridLayout( &dlg, 1, 1, 11, 6, "layout");
664 TQGridLayout * layoutTop = new TQGridLayout( 0, 1, 1, 0, 6, "layoutTop");
665 TQGridLayout * layoutVar = new TQGridLayout( 0, 1, 1, 0, 6, "layoutVar");
666 TQGridLayout * layoutBtn = new TQGridLayout( 0, 1, 1, 0, 6, "layoutBtn");
668 KTextEdit *te = NULL;
669 TQLabel * labTop = NULL;
670 TQCheckBox * cb = NULL;
672 labTop = new TQLabel( &dlg, "label" );
673 labTop->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, (TQSizePolicy::SizeType)0, 0, 0,
674 labTop->sizePolicy().hasHeightForWidth() ) );
675 labTop->setText(i18n( "Enter the replacement values for these variables:"));
676 layoutTop->addWidget(labTop, 0, 0);
677 layout->addMultiCellLayout( layoutTop, 0, 0, 0, 1 );
681 for ( it = map->begin(); it != map->end(); ++it ) {
682 if (it.key() == _SnippetConfig.getDelimiter() + _SnippetConfig.getDelimiter())
685 cb = new TQCheckBox( &dlg, "cbVar" );
686 cb->setChecked( false );
687 cb->setText(it.key());
688 layoutVar->addWidget( cb, i ,0, TQt::AlignTop );
690 te = new KTextEdit( &dlg, "teVar" );
691 layoutVar->addWidget( te, i, 1, TQt::AlignTop );
693 if ((*mapSave)[it.key()].length() > 0) {
694 cb->setChecked( true );
695 te->setText((*mapSave)[it.key()]);
698 mapVar2Te[it.key()] = te;
699 mapVar2Cb[it.key()] = cb;
701 TQToolTip::add( cb, i18n( "Enable this to save the value entered to the right as the default value for this variable") );
702 TQWhatsThis::add( cb, i18n( "If you enable this option, the value entered to the right will be saved. "
703 "If you use the same variable later, even in another snippet, the value entered to the right "
704 "will be the default value for that variable.") );
708 layout->addMultiCellLayout( layoutVar, 1, 1, 0, 1 );
710 KPushButton * btn1 = new KPushButton( KStdGuiItem::cancel(), &dlg, "pushButton1" );
711 btn1->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, (TQSizePolicy::SizeType)0, 0, 0,
712 btn1->sizePolicy().hasHeightForWidth() ) );
713 layoutBtn->addWidget( btn1, 0, 0 );
715 KPushButton * btn2 = new KPushButton( KStdGuiItem::apply(), &dlg, "pushButton2" );
716 btn2->setDefault( true );
717 btn2->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, (TQSizePolicy::SizeType)0, 0, 0,
718 btn2->sizePolicy().hasHeightForWidth() ) );
719 layoutBtn->addWidget( btn2, 0, 1 );
721 layout->addMultiCellLayout( layoutBtn, 2, 2, 0, 1 );
725 connect(btn1, TQ_SIGNAL(clicked()), &dlg, TQ_SLOT(reject()) );
726 connect(btn2, TQ_SIGNAL(clicked()), &dlg, TQ_SLOT(accept()) );
729 bool bReturn = false;
732 TQRect r = dlg.geometry();
733 r.setHeight(iBasicHeight + iOneHeight*mapVar2Te.count());
738 dlg.exec() == TQDialog::Accepted ) {
740 TQMap<TQString, KTextEdit *>::Iterator it2;
741 for ( it2 = mapVar2Te.begin(); it2 != mapVar2Te.end(); ++it2 ) {
742 if (it2.key() == _SnippetConfig.getDelimiter() + _SnippetConfig.getDelimiter())
744 (*map)[it2.key()] = it2.data()->text();
746 if (mapVar2Cb[it2.key()]->isChecked())
747 (*mapSave)[it2.key()] = it2.data()->text();
749 (*mapSave).erase(it2.key());
753 iBasicHeight = dlg.geometry().height() - layoutVar->geometry().height();
754 iOneHeight = layoutVar->geometry().height() / mapVar2Te.count();
755 iWidth = dlg.geometry().width();
759 TQMap<TQString, KTextEdit *>::Iterator it1;
760 for (it1 = mapVar2Te.begin(); it1 != mapVar2Te.end(); ++it1)
763 TQMap<TQString, TQCheckBox *>::Iterator it2;
764 for (it2 = mapVar2Cb.begin(); it2 != mapVar2Cb.end(); ++it2)
785TQString SnippetWidget::showSingleVarDialog(TQString var, TQMap<TQString, TQString> * mapSave, TQRect & dlgSize)
789 dlg.setCaption(i18n( "Enter Values for Variables"));
791 TQGridLayout * layout = new TQGridLayout( &dlg, 1, 1, 11, 6, "layout");
792 TQGridLayout * layoutTop = new TQGridLayout( 0, 1, 1, 0, 6, "layoutTop");
793 TQGridLayout * layoutVar = new TQGridLayout( 0, 1, 1, 0, 6, "layoutVar");
794 TQGridLayout * layoutBtn = new TQGridLayout( 0, 2, 1, 0, 6, "layoutBtn");
796 KTextEdit *te = NULL;
797 TQLabel * labTop = NULL;
798 TQCheckBox * cb = NULL;
800 labTop = new TQLabel( &dlg, "label" );
801 layoutTop->addWidget(labTop, 0, 0);
802 labTop->setText(i18n( "Enter the replacement values for %1:").arg( var ));
803 layout->addMultiCellLayout( layoutTop, 0, 0, 0, 1 );
806 cb = new TQCheckBox( &dlg, "cbVar" );
807 cb->setChecked( false );
808 cb->setText(i18n( "Make value &default" ));
810 te = new KTextEdit( &dlg, "teVar" );
811 layoutVar->addWidget( te, 0, 1, TQt::AlignTop);
812 layoutVar->addWidget( cb, 1, 1, TQt::AlignTop);
813 if ((*mapSave)[var].length() > 0) {
814 cb->setChecked( true );
815 te->setText((*mapSave)[var]);
818 TQToolTip::add( cb, i18n( "Enable this to save the value entered to the right as the default value for this variable") );
819 TQWhatsThis::add( cb, i18n( "If you enable this option, the value entered to the right will be saved. "
820 "If you use the same variable later, even in another snippet, the value entered to the right "
821 "will be the default value for that variable.") );
823 layout->addMultiCellLayout( layoutVar, 1, 1, 0, 1 );
825 KPushButton * btn1 = new KPushButton( KStdGuiItem::cancel(), &dlg, "pushButton1" );
826 layoutBtn->addWidget( btn1, 0, 0 );
828 KPushButton * btn2 = new KPushButton( KStdGuiItem::apply(), &dlg, "pushButton2" );
829 btn2->setDefault( true );
830 layoutBtn->addWidget( btn2, 0, 1 );
832 layout->addMultiCellLayout( layoutBtn, 2, 2, 0, 1 );
837 connect(btn1, TQ_SIGNAL(clicked()), &dlg, TQ_SLOT(reject()) );
838 connect(btn2, TQ_SIGNAL(clicked()), &dlg, TQ_SLOT(accept()) );
841 TQString strReturn = "";
842 if (dlgSize.isValid())
843 dlg.setGeometry(dlgSize);
844 if ( dlg.exec() == TQDialog::Accepted ) {
846 (*mapSave)[var] = te->text();
848 (*mapSave).erase(var);
850 strReturn = te->text();
852 dlgSize = dlg.geometry();
880 TQListViewItem * item = itemAt(event->pos());
883 TQString(event->format()).startsWith( "text/plain") &&
888 TQString(event->format()).startsWith( "x-kmailsnippet") &&
895 event->acceptAction( false);
908 TQListViewItem * item2 = itemAt(e->pos());
915 TQByteArray data = e->encodedData( "text/plain");
916 if ( e->provides( "text/plain") && data.size()>0 ) {
918 TQString encData(data.data());
922 SnippetDlg dlg( mActionCollection, this, "SnippetDlg" );
923 dlg.snippetName->clear();
924 dlg.snippetText->setText(encData);
927 for ( SnippetItem *it=_list.first(); it; it=_list.next()) {
929 dlg.cbGroup->insertItem(it->getName());
932 dlg.cbGroup->setCurrentText(group->getName());
934 if (dlg.exec() == TQDialog::Accepted) {
936 group = dynamic_cast<SnippetGroup* >(SnippetItem::findItemByName(dlg.cbGroup->currentText(), _list));
937 _list.append( makeItem(group, dlg.snippetName->text(), dlg.snippetText->text(), dlg.keyButton->shortcut() ) );
942void SnippetWidget::startDrag()
944 TQString text = dynamic_cast<SnippetItem* >( currentItem() )->getText();
945 TQTextDrag *drag = new TQTextDrag(text, this);
946 drag->setSubtype( "x-textsnippet");
950void SnippetWidget::slotExecute()
952 slotExecuted(currentItem());
956#include "snippetwidget.moc"
This class represents one group in the listview.
This class represents one CodeSnippet-Item in the listview.
|