summaryrefslogtreecommitdiffstats
path: root/krename/kmyhistorycombo.cpp
blob: 32724fb17d029a33ee3c93e55356a226b6d85030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/***************************************************************************
                          kmyhistorycombo.cpp  -  description
                             -------------------
    begin                : Tue Oct 16 2001
    copyright            : (C) 2001 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

// QT includes
#include <tqlistbox.h>

// Own includes
#include "kmyhistorycombo.h"

#include <tdeapplication.h>
#include <tdeconfig.h>
#include <tdelocale.h>

#include <tqpopupmenu.h>
#include <tqtimer.h>

#define TIMER_DELAY 500

#define KRENAME_FILENAME       5000
#define KRENAME_FILENAME_LOWER 5001
#define KRENAME_FILENAME_UPPER 5002
#define KRENAME_NUMBER         5003
#define KRENAME_DATE           5004

KMyHistoryCombo::KMyHistoryCombo( bool customPopup, TQWidget* parent, const char* name)
    : KHistoryCombo(parent, name)
{
    TQStringList history;
    TQStringList completion;
    TDEConfig* config = kapp->config();
    config->setGroup( name );
    this->setDuplicatesEnabled( false );
    
    history = config->readListEntry("History");
    completion = config->readListEntry("CompletionItems");
    m_timer = new TQTimer();
    
    setHistoryItems( history );

    completionObject()->setItems( ( completion.isEmpty() ? history : completion ) );
    setCompletionMode( (TDEGlobalSettings::Completion)config->readNumEntry( "CompletionMode",
                        TDEGlobalSettings::completionMode() ) );    
                        
    connect( this, TQT_SIGNAL( textChanged( const TQString & ) ), this, TQT_SLOT( textChangedGovernor() ) );
    connect( m_timer, TQT_SIGNAL( timeout() ), this, TQT_SIGNAL( delayedTextChanged() ) );

    if( customPopup )
        connect( this, TQT_SIGNAL( aboutToShowContextMenu( TQPopupMenu* ) ), this, TQT_SLOT( slotCustomContextMenu( TQPopupMenu* ) ) );
}

KMyHistoryCombo::~KMyHistoryCombo()
{
    delete m_timer;
}

void KMyHistoryCombo::saveSettings()
{
    addToHistory( text() );

    TDEConfig* config = kapp->config();
    
    config->setGroup( name() ? name() : "KMyHistoryCombo" );
    config->writeEntry( "History", historyItems() );
    config->writeEntry( "CompletionItems", completionObject()->items() );
    config->writeEntry( "CompletionMode", completionMode() );

    config->sync();
}

TQString KMyHistoryCombo::text( int index ) const 
{
    return this->listBox()->text( index );
}

void KMyHistoryCombo::setText( const TQString & text )
{
    this->lineEdit()->setText( text );
}

void KMyHistoryCombo::add( const TQString & text )
{
    int i;
    for ( i = 0; i < this->count(); i++ )
        if( this->text( i ) == text ) {
            TQString tmp = this->text( i );
            this->listBox()->removeItem( i );
            this->insertItem( tmp, 0 );
            return;
        }

    if( this->count() == this->maxCount() )
        this->listBox()->removeItem( this->maxCount() );

    this->insertItem( text, 0 );
}

bool KMyHistoryCombo::isEmpty() const
{
    return this->lineEdit()->text().isEmpty();
}

void KMyHistoryCombo::textChangedGovernor()
{
    m_timer->stop();
    m_timer->start( TIMER_DELAY, true );
}

void KMyHistoryCombo::slotCustomContextMenu( TQPopupMenu* p )
{
    TQPopupMenu* krename = new TQPopupMenu( p );
    krename->insertItem( i18n("&Filename"), KRENAME_FILENAME );
    krename->insertItem( i18n("Filename to &lowercase"), KRENAME_FILENAME_LOWER );
    krename->insertItem( i18n("Filename to &uppercase"), KRENAME_FILENAME_UPPER );
    krename->insertItem( i18n("&Number"), KRENAME_NUMBER );
    krename->insertItem( i18n("&Date"), KRENAME_DATE );

    connect( krename, TQT_SIGNAL( activated( int ) ), this, TQT_SLOT( slotInsertKRenameCommand( int ) ) );

    p->insertSeparator( 0 );
    p->insertItem( i18n("Insert &KRename token"), krename, 0, 0 );
}

void KMyHistoryCombo::slotInsertKRenameCommand( int id )
{
    TQString t;

    // TODO:
    // also use constants for KRename tokens!

    switch( id )
    {
        case KRENAME_FILENAME:
            t = "$";
            break;
        case KRENAME_FILENAME_LOWER:
            t = "%";
            break;
        case KRENAME_FILENAME_UPPER:
            t = "&";
            break;
        case KRENAME_NUMBER:
            t = "#";
            break;
        case KRENAME_DATE:
            t = "[date]";
            break;
        default:
            break;
    }

    if( !t.isEmpty() )
        this->lineEdit()->insert( t );
}