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.

coorddialog.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /***************************************************************************
  2. coorddialog.cpp - description
  3. -------------------
  4. begin : Die Feb 4 2003
  5. copyright : (C) 2003 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. #include "coorddialog.h"
  17. // TQt includes
  18. #include <tqcheckbox.h>
  19. #include <tqfontmetrics.h>
  20. #include <tqlabel.h>
  21. #include <tqlayout.h>
  22. #include <tqvalidator.h>
  23. // KDE includes
  24. #include <tdeapplication.h>
  25. #include <tdelocale.h>
  26. DSLineEdit::DSLineEdit( TQWidget* parent, const char* name )
  27. : KLineEdit( parent, name )
  28. {
  29. }
  30. void DSLineEdit::keyPressEvent( TQKeyEvent* e )
  31. {
  32. KLineEdit::keyPressEvent( e );
  33. emit changed();
  34. }
  35. void DSLineEdit::mousePressEvent( TQMouseEvent* e )
  36. {
  37. KLineEdit::mousePressEvent( e );
  38. emit changed();
  39. }
  40. bool CoordDialog::m_inversion = false;
  41. CoordDialog::CoordDialog( const TQString & file, TQWidget *_parent, const char *name )
  42. : KDialogBase( KDialogBase::Plain, "KRename",
  43. KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok, _parent, name, true, true ), m_file( file )
  44. {
  45. TQFrame* parent = plainPage();
  46. TQVBoxLayout* layout = new TQVBoxLayout( parent );
  47. filename = new DSLineEdit( parent );
  48. filename->setText( file );
  49. filename->setValidator( new TQRegExpValidator( TQRegExp( file ), TQT_TQOBJECT(this) ) );
  50. preview = new TQLabel( parent );
  51. checkInvert = new TQCheckBox( i18n("&Invert selection"), plainPage() );
  52. checkInvert->setChecked( m_inversion );
  53. layout->addWidget( new TQLabel( i18n("Please select the text you want to insert:"), plainPage() ) );
  54. layout->addWidget( filename );
  55. layout->addWidget( checkInvert );
  56. layout->addWidget( preview );
  57. updateCommand();
  58. connect( filename, TQT_SIGNAL( selectionChanged() ), this, TQT_SLOT( updateCommand() ) );
  59. connect( checkInvert, TQT_SIGNAL( clicked() ), this, TQT_SLOT( updateCommand() ) );
  60. connect( filename, TQT_SIGNAL( textChanged( const TQString & ) ), this, TQT_SLOT( resetText() ) );
  61. connect( filename, TQT_SIGNAL( changed() ), this, TQT_SLOT( updateCommand() ) );
  62. show();
  63. TQFontMetrics fm( filename->font() );
  64. int w = fm.width( file );
  65. if( w > width() )
  66. resize(
  67. ( w < TDEApplication::desktop()->width() - 40 ? w + 40 : TDEApplication::desktop()->width() ), height() );
  68. }
  69. CoordDialog::~CoordDialog()
  70. {
  71. }
  72. void CoordDialog::updateCommand()
  73. {
  74. int start = 0;
  75. int end = 0;
  76. m_command = "";
  77. (void)filename->getSelection( &start, &end );
  78. if( !filename->text().isEmpty() ) {
  79. if( checkInvert->isChecked() && filename->hasSelectedText() ) {
  80. // inverted
  81. if( end ) {
  82. start++;
  83. end++;
  84. if( start > 1 )
  85. m_command = TQString("[$1;%1]").arg(start-1);
  86. if( end <= (signed int)filename->text().length() )
  87. m_command.append( TQString("[$%1-[length]]").arg(end) );
  88. }
  89. } else if( checkInvert->isChecked() && !filename->hasSelectedText() ) {
  90. int p = filename->cursorPosition();
  91. m_command = TQString("[$1;%1][$%2-[length]]").arg(p).arg(p+1);
  92. } else if( !checkInvert->isChecked() && filename->hasSelectedText() ){
  93. if( end ) {
  94. start++;
  95. end++;
  96. if( end <= (signed int)filename->text().length() )
  97. m_command = TQString("[$%1;%2]").arg(start).arg(end-start);
  98. else
  99. m_command = TQString("[$%1-[length]]").arg(start);
  100. }
  101. } else if( !checkInvert->isChecked() && !filename->hasSelectedText() ) {
  102. int p = filename->cursorPosition();
  103. m_command = TQString("[$%1-[length]]").arg( p );
  104. }
  105. }
  106. preview->setText( i18n("Preview: ") + m_command );
  107. }
  108. void CoordDialog::resetText()
  109. {
  110. filename->setText( m_file );
  111. updateCommand();
  112. }
  113. TQString CoordDialog::coords()
  114. {
  115. m_inversion = checkInvert->isChecked();
  116. return m_command;
  117. }
  118. #include "coorddialog.moc"