summaryrefslogtreecommitdiffstats
path: root/klinkstatus/src/ui/klshistorycombo.cpp
blob: 3a4c77872269aa7f586aefb7af9cbd52fd35eeba (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// C++ Implementation: klshistorycombo
//
// Description:
//
//
// Author: Paulo Moura Guedes <[email protected]>, (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "klshistorycombo.h"
#include "klsconfig.h"

#include <kapplication.h>
#include <tdeconfig.h>
#include <kcompletionbox.h>
#include <kdebug.h>
#include <tdestdaccel.h>
#include <kurldrag.h>
#include <kglobalsettings.h>


bool KLSHistoryCombo::items_saved_ = false;


KLSHistoryCombo::KLSHistoryCombo(TQWidget *parent, const char *name)
        : KHistoryCombo(parent, name)
{
    setMaxCount(KLSConfig::maxCountComboUrl());
    
    setDuplicatesEnabled(false);
    setAutoCompletion(false);

    connect(this, TQT_SIGNAL(activated(const TQString& )),
            this, TQT_SLOT(addToHistory(const TQString& )));
}

KLSHistoryCombo::~KLSHistoryCombo()
{}

void KLSHistoryCombo::init()
{
    loadItems();
}

void KLSHistoryCombo::saveItems()
{
    if(items_saved_)
        return;

    TQStringList items = historyItems();

    KLSConfig::setComboUrlHistory(items);
    KLSConfig::writeConfig();
    
    items_saved_ = true;
}

void KLSHistoryCombo::loadItems()
{
    clear();
    
    TQStringList items = KLSConfig::comboUrlHistory();

    bool block = signalsBlocked();
    blockSignals( true );

    setHistoryItems(items);
    blockSignals(block);

    completionObject()->setItems(items);

    setCompletionMode(TDEGlobalSettings::completionMode());
}

bool KLSHistoryCombo::eventFilter( TQObject *o, TQEvent *ev )
{
    // Handle Ctrl+Del/Backspace etc better than the TQt widget, which always
    // jumps to the next whitespace.
    TQLineEdit *edit = lineEdit();
    if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(edit) )
    {
        int type = ev->type();
        if ( type == TQEvent::KeyPress )
        {
            TQKeyEvent *e = TQT_TQKEYEVENT( ev );

            if ( e->key() == Key_Return || e->key() == Key_Enter )
            {
                //m_modifier = e->state();
                return false;
            }

            int delete_word_back = TDEStdAccel::deleteWordBack().keyCodeQt();
            int delete_word_forward = TDEStdAccel::deleteWordForward().keyCodeQt();

            if ( KKey( e ) == KKey(delete_word_back) ||
                    KKey( e ) == KKey(delete_word_forward) ||
                    ((e->state() & ControlButton) &&
                     (e->key() == Key_Left || e->key() == Key_Right) ) )
            {
                selectWord(e);
                e->accept();
                return true;
            }
        }

        else if ( type == TQEvent::MouseButtonDblClick )
        {
            edit->selectAll();
            return true;
        }
    }
    return KComboBox::eventFilter( o, ev );
}

/*
   Handle Ctrl+Cursor etc better than the TQt widget, which always
   jumps to the next whitespace. This code additionally jumps to
   the next [/#?:], which makes more sense for URLs. The list of 
   chars that will stop the cursor are '/', '.', '?', '#', ':'.
*/
void KLSHistoryCombo::selectWord(TQKeyEvent *e)
{
    TQLineEdit* edit = lineEdit();
    TQString text = edit->text();
    int pos = edit->cursorPosition();
    int pos_old = pos;
    int count = 0;

    // TODO: make these a parameter when in tdelibs/tdeui...
    TQValueList<TQChar> chars;
    chars << TQChar('/') << TQChar('.') << TQChar('?') << TQChar('#') << TQChar(':');
    bool allow_space_break = true;

    if( e->key() == Key_Left || e->key() == Key_Backspace )
    {
        do
        {
            pos--;
            count++;
            if( allow_space_break && text[pos].isSpace() && count > 1 )
                break;
        }
        while( pos >= 0 && (chars.findIndex(text[pos]) == -1 || count <= 1) );

        if( e->state() & ShiftButton )
        {
            edit->cursorForward(true, 1-count);
        }
        else if(  e->key() == Key_Backspace )
        {
            edit->cursorForward(false, 1-count);
            TQString text = edit->text();
            int pos_to_right = edit->text().length() - pos_old;
            TQString cut = text.left(edit->cursorPosition()) + text.right(pos_to_right);
            edit->setText(cut);
            edit->setCursorPosition(pos_old-count+1);
        }
        else
        {
            edit->cursorForward(false, 1-count);
        }
    }
    else if( e->key() == Key_Right || e->key() == Key_Delete )
    {
        do
        {
            pos++;
            count++;
            if( allow_space_break && text[pos].isSpace() )
                break;
        }
        while( pos < (int) text.length() && chars.findIndex(text[pos]) == -1 );

        if( e->state() & ShiftButton )
        {
            edit->cursorForward(true, count+1);
        }
        else if(  e->key() == Key_Delete )
        {
            edit->cursorForward(false, -count-1);
            TQString text = edit->text();
            int pos_to_right = text.length() - pos - 1;
            TQString cut = text.left(pos_old) +
                          (pos_to_right > 0 ? text.right(pos_to_right) : TQString() );
            edit->setText(cut);
            edit->setCursorPosition(pos_old);
        }
        else
        {
            edit->cursorForward(false, count+1);
        }
    }
}

#include "klshistorycombo.moc"