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.

kmyhistorycombo.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /***************************************************************************
  2. kmyhistorycombo.cpp - description
  3. -------------------
  4. begin : Tue Oct 16 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. // QT includes
  17. #include <tqlistbox.h>
  18. // Own includes
  19. #include "kmyhistorycombo.h"
  20. #include <tdeapplication.h>
  21. #include <tdeconfig.h>
  22. #include <tdelocale.h>
  23. #include <tqpopupmenu.h>
  24. #include <tqtimer.h>
  25. #define TIMER_DELAY 500
  26. #define KRENAME_FILENAME 5000
  27. #define KRENAME_FILENAME_LOWER 5001
  28. #define KRENAME_FILENAME_UPPER 5002
  29. #define KRENAME_NUMBER 5003
  30. #define KRENAME_DATE 5004
  31. KMyHistoryCombo::KMyHistoryCombo( bool customPopup, TQWidget* parent, const char* name)
  32. : KHistoryCombo(parent, name)
  33. {
  34. TQStringList history;
  35. TQStringList completion;
  36. TDEConfig* config = kapp->config();
  37. config->setGroup( name );
  38. this->setDuplicatesEnabled( false );
  39. history = config->readListEntry("History");
  40. completion = config->readListEntry("CompletionItems");
  41. m_timer = new TQTimer();
  42. setHistoryItems( history );
  43. completionObject()->setItems( ( completion.isEmpty() ? history : completion ) );
  44. setCompletionMode( (TDEGlobalSettings::Completion)config->readNumEntry( "CompletionMode",
  45. TDEGlobalSettings::completionMode() ) );
  46. connect( this, TQT_SIGNAL( textChanged( const TQString & ) ), this, TQT_SLOT( textChangedGovernor() ) );
  47. connect( m_timer, TQT_SIGNAL( timeout() ), this, TQT_SIGNAL( delayedTextChanged() ) );
  48. if( customPopup )
  49. connect( this, TQT_SIGNAL( aboutToShowContextMenu( TQPopupMenu* ) ), this, TQT_SLOT( slotCustomContextMenu( TQPopupMenu* ) ) );
  50. }
  51. KMyHistoryCombo::~KMyHistoryCombo()
  52. {
  53. delete m_timer;
  54. }
  55. void KMyHistoryCombo::saveSettings()
  56. {
  57. addToHistory( text() );
  58. TDEConfig* config = kapp->config();
  59. config->setGroup( name() ? name() : "KMyHistoryCombo" );
  60. config->writeEntry( "History", historyItems() );
  61. config->writeEntry( "CompletionItems", completionObject()->items() );
  62. config->writeEntry( "CompletionMode", completionMode() );
  63. config->sync();
  64. }
  65. TQString KMyHistoryCombo::text( int index ) const
  66. {
  67. return this->listBox()->text( index );
  68. }
  69. void KMyHistoryCombo::setText( const TQString & text )
  70. {
  71. this->lineEdit()->setText( text );
  72. }
  73. void KMyHistoryCombo::add( const TQString & text )
  74. {
  75. int i;
  76. for ( i = 0; i < this->count(); i++ )
  77. if( this->text( i ) == text ) {
  78. TQString tmp = this->text( i );
  79. this->listBox()->removeItem( i );
  80. this->insertItem( tmp, 0 );
  81. return;
  82. }
  83. if( this->count() == this->maxCount() )
  84. this->listBox()->removeItem( this->maxCount() );
  85. this->insertItem( text, 0 );
  86. }
  87. bool KMyHistoryCombo::isEmpty() const
  88. {
  89. return this->lineEdit()->text().isEmpty();
  90. }
  91. void KMyHistoryCombo::textChangedGovernor()
  92. {
  93. m_timer->stop();
  94. m_timer->start( TIMER_DELAY, true );
  95. }
  96. void KMyHistoryCombo::slotCustomContextMenu( TQPopupMenu* p )
  97. {
  98. TQPopupMenu* krename = new TQPopupMenu( p );
  99. krename->insertItem( i18n("&Filename"), KRENAME_FILENAME );
  100. krename->insertItem( i18n("Filename to &lowercase"), KRENAME_FILENAME_LOWER );
  101. krename->insertItem( i18n("Filename to &uppercase"), KRENAME_FILENAME_UPPER );
  102. krename->insertItem( i18n("&Number"), KRENAME_NUMBER );
  103. krename->insertItem( i18n("&Date"), KRENAME_DATE );
  104. connect( krename, TQT_SIGNAL( activated( int ) ), this, TQT_SLOT( slotInsertKRenameCommand( int ) ) );
  105. p->insertSeparator( 0 );
  106. p->insertItem( i18n("Insert &KRename token"), krename, 0, 0 );
  107. }
  108. void KMyHistoryCombo::slotInsertKRenameCommand( int id )
  109. {
  110. TQString t;
  111. // TODO:
  112. // also use constants for KRename tokens!
  113. switch( id )
  114. {
  115. case KRENAME_FILENAME:
  116. t = "$";
  117. break;
  118. case KRENAME_FILENAME_LOWER:
  119. t = "%";
  120. break;
  121. case KRENAME_FILENAME_UPPER:
  122. t = "&";
  123. break;
  124. case KRENAME_NUMBER:
  125. t = "#";
  126. break;
  127. case KRENAME_DATE:
  128. t = "[date]";
  129. break;
  130. default:
  131. break;
  132. }
  133. if( !t.isEmpty() )
  134. this->lineEdit()->insert( t );
  135. }
  136. #include "kmyhistorycombo.moc"