KRename – powerful batch renamer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

replacedialog.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /***************************************************************************
  2. replacedialog.cpp - description
  3. -------------------
  4. begin : Sat Aug 18 2001
  5. copyright : (C) 2001 by Dominik Seichter
  6. email : domseichter@web.de
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. // Own includes
  17. #include "replacedialog.h"
  18. // QT includes
  19. #include <tqcheckbox.h>
  20. #include <tqlabel.h>
  21. #include <tqlineedit.h>
  22. #include <tqlayout.h>
  23. #include <tqregexp.h>
  24. // KDE includes
  25. #include <tdeapplication.h>
  26. #include <tdeconfig.h>
  27. #include <tdelocale.h>
  28. #include <tdelistview.h>
  29. #include <tdemessagebox.h>
  30. #include <tdeparts/componentfactory.h>
  31. #include <kpushbutton.h>
  32. #include <kregexpeditorinterface.h>
  33. ReplaceDialog::ReplaceDialog( TQValueList<replacestrings> & r, TQWidget* parent )
  34. : KDialogBase( KDialogBase::Plain, i18n( "Find and Replace" ),
  35. KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok, parent, 0, true, true )
  36. {
  37. ReplaceDialogLayout = new TQGridLayout( plainPage(), 11, 6);
  38. list = new TDEListView( plainPage() );
  39. list->addColumn( i18n("Find") );
  40. list->addColumn( i18n("Replace With") );
  41. list->addColumn( i18n("Regular Expression") );
  42. list->addColumn( "regexp" ); // no i18n, because not user visible
  43. // list->setColumnWidthMode( 0, TQListView::Manual );
  44. // list->setColumnWidthMode( 1, TQListView::Manual );
  45. // list->setColumnWidthMode( 2, TQListView::Manual );
  46. list->setColumnWidthMode( 3, TQListView::Manual );
  47. list->setColumnWidth( 3, 0 );
  48. list->setSorting( -1 );
  49. list->setAllColumnsShowFocus( true );
  50. TextLabel1 = new TQLabel( plainPage() );
  51. TextLabel1->setText( i18n( "Find:" ) );
  52. TextLabel2 = new TQLabel( plainPage() );
  53. TextLabel2->setText( i18n( "Replace with:" ) );
  54. text1 = new TQLineEdit( plainPage() );
  55. text2 = new TQLineEdit( plainPage() );
  56. checkReg = new TQCheckBox( i18n("&Regular expression"), plainPage() );
  57. buttonRegEdit = new KPushButton( plainPage() );
  58. buttonRegEdit->setText( i18n( "&Edit..." ) );
  59. buttonRegEdit->setEnabled( false );
  60. buttonAdd = new KPushButton( plainPage() );
  61. buttonAdd->setText( i18n( "&Add" ) );
  62. buttonEdit = new KPushButton( plainPage() );
  63. buttonEdit->setText( i18n("&Edit") );
  64. buttonRemove = new KPushButton( plainPage() );
  65. buttonRemove->setText( i18n( "&Remove" ) );
  66. TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Expanding );
  67. ReplaceDialogLayout->addWidget( TextLabel1, 0, 0 );
  68. ReplaceDialogLayout->addWidget( text1, 0, 1 );
  69. ReplaceDialogLayout->addWidget( TextLabel2, 0, 3 );
  70. ReplaceDialogLayout->addWidget( text2, 0, 4 );
  71. ReplaceDialogLayout->addWidget( checkReg, 1, 0 );
  72. ReplaceDialogLayout->addWidget( buttonRegEdit, 1, 1 );
  73. ReplaceDialogLayout->addWidget( buttonAdd, 0, 6 );
  74. ReplaceDialogLayout->addWidget( buttonEdit, 1, 6 );
  75. ReplaceDialogLayout->addWidget( buttonRemove, 2, 6 );
  76. ReplaceDialogLayout->addItem( spacer, 0, 5 );
  77. ReplaceDialogLayout->addItem( spacer, 0, 2 );
  78. ReplaceDialogLayout->addMultiCellWidget( list, 3, 3, 0, 4 );
  79. text1->setFocus();
  80. connect( buttonAdd, TQT_SIGNAL( clicked() ), this, TQT_SLOT( add() ) );
  81. connect( buttonRemove, TQT_SIGNAL( clicked() ), this, TQT_SLOT( remove() ) );
  82. connect( list, TQT_SIGNAL( clicked( TQListViewItem* ) ), this, TQT_SLOT( enableControls() ) );
  83. connect( list, TQT_SIGNAL( doubleClicked( TQListViewItem* ) ), this, TQT_SLOT( slotEdit() ) );
  84. connect( text2, TQT_SIGNAL( returnPressed() ), this, TQT_SLOT( add() ) );
  85. connect( text1, TQT_SIGNAL( returnPressed() ), this, TQT_SLOT( moveFocus() ) );
  86. connect( buttonRegEdit, TQT_SIGNAL( clicked() ), this, TQT_SLOT( invokeRegEdit() ) );
  87. connect( checkReg, TQT_SIGNAL( clicked() ), this, TQT_SLOT( enableControls() ) );
  88. connect( buttonEdit, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotEdit() ) );
  89. for( unsigned int i = 0; i < r.count(); i++ ) {
  90. replacestrings rs = r[i];
  91. BatchRenamer::unEscape( rs.find );
  92. TDEListViewItem* item = new TDEListViewItem( list );
  93. item->setText( 0, encode( rs.find ) );
  94. item->setText( 1, encode( rs.replace ) );
  95. item->setText( 2, rs.reg ? i18n("yes") : i18n("no") );
  96. item->setText( 3, TQString::number( rs.reg ) );
  97. list->insertItem( item );
  98. }
  99. enableControls();
  100. }
  101. ReplaceDialog::~ReplaceDialog()
  102. { }
  103. void ReplaceDialog::add()
  104. {
  105. if( text1->text().isEmpty() ) {
  106. KMessageBox::sorry( this, i18n( "Add a text that should be replaced." ) );
  107. return;
  108. }
  109. TQListViewItem* it = list->firstChild();
  110. while( it ) {
  111. if( it->text( 0 ) == text1->text() ) {
  112. KMessageBox::sorry( this, i18n( "You can't replace the same text twice." ) );
  113. return;
  114. }
  115. it = it->nextSibling();
  116. }
  117. TDEListViewItem* item = new TDEListViewItem( list, i18n("Regular expression") );
  118. item->setText( 0, encode( text1->text() ) );
  119. item->setText( 1, encode( text2->text() ) );
  120. item->setText( 2, checkReg->isChecked() ? i18n("yes") : i18n("no") );
  121. item->setText( 3, TQString::number( checkReg->isChecked() ) );
  122. list->insertItem( item );
  123. reset();
  124. enableControls();
  125. }
  126. void ReplaceDialog::remove()
  127. {
  128. if( list->selectedItem() ) {
  129. TQListViewItem* item = list->selectedItem();
  130. list->takeItem( item );
  131. delete item;
  132. }
  133. enableControls();
  134. }
  135. TQValueList<replacestrings> ReplaceDialog::getList()
  136. {
  137. TQValueList<replacestrings> r;
  138. TQListViewItem* item = list->firstChild();
  139. while( item ) {
  140. replacestrings n;
  141. n.find = decode( item->text( 0 ) );
  142. n.replace = decode( item->text( 1 ) );
  143. n.reg = item->text( 3 ).toInt();
  144. BatchRenamer::doEscape( n.find );
  145. r.append( n );
  146. item = item->nextSibling();
  147. }
  148. return r;
  149. }
  150. void ReplaceDialog::moveFocus()
  151. {
  152. text2->setFocus();
  153. }
  154. void ReplaceDialog::invokeRegEdit()
  155. {
  156. TQDialog* regExpDialog = KParts::ComponentFactory::createInstanceFromQuery<TQDialog>( "KRegExpEditor/KRegExpEditor", TQString(), TQT_TQOBJECT(this) );
  157. KRegExpEditorInterface *iface = static_cast<KRegExpEditorInterface *>( regExpDialog->tqt_cast( "KRegExpEditorInterface" ) );
  158. if ( !iface )
  159. return;
  160. iface->setRegExp( text1->text() );
  161. bool ok = regExpDialog->exec();
  162. if ( ok )
  163. text1->setText( iface->regExp() );
  164. }
  165. TQString ReplaceDialog::encode( TQString s )
  166. {
  167. s.append("\"");
  168. s.prepend("\"");
  169. return s;
  170. }
  171. TQString ReplaceDialog::decode( TQString s )
  172. {
  173. if( s[0] == '"' )
  174. s.remove( 0, 1 );
  175. if( s[s.length()-1] == '"' )
  176. s.remove( s.length()-1, 1 );
  177. return s;
  178. }
  179. void ReplaceDialog::resizeEvent( TQResizeEvent* e )
  180. {
  181. TQDialog::resizeEvent( e );
  182. // list->setColumnWidth( 0, TextLabel1->width() + text1->width() );
  183. // list->setColumnWidth( 1, TextLabel2->width() + text2->width() );
  184. }
  185. void ReplaceDialog::reset()
  186. {
  187. text1->clear();
  188. text2->clear();
  189. checkReg->setChecked( false );
  190. text1->setFocus();
  191. }
  192. void ReplaceDialog::enableControls()
  193. {
  194. buttonRemove->setEnabled( list->selectedItem() );
  195. buttonRegEdit->setEnabled( checkReg->isChecked() && !TDETrader::self()->query("KRegExpEditor/KRegExpEditor").isEmpty() );
  196. buttonEdit->setEnabled( list->selectedItem() );
  197. }
  198. void ReplaceDialog::slotEdit()
  199. {
  200. text1->setText( decode( list->selectedItem()->text( 0 ) ) );
  201. text2->setText( decode( list->selectedItem()->text( 1 ) ) );
  202. checkReg->setChecked( list->selectedItem()->text( 3 ).toInt() );
  203. TQListViewItem* item = list->selectedItem();
  204. list->takeItem( item );
  205. delete item;
  206. text1->setFocus();
  207. enableControls();
  208. }
  209. #include "replacedialog.moc"