refresh 0856514572d371eb96e7cc28b455df3eae46cab0
[tagua/yd.git] / src / histlineedit.cpp
blobab18359d8c725ff6689c82027b86e51bb3ee7e6f
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include <QKeyEvent>
12 #include "histlineedit.h"
14 HistLineEdit::HistLineEdit(QWidget* parent)
15 : QLineEdit(parent) {
16 current = 0;
18 connect(this, SIGNAL(returnPressed()), this, SLOT(updateHistory()));
21 void HistLineEdit::keyPressEvent(QKeyEvent* e) {
22 if (echoMode() == Password) {
23 e->ignore();
24 QLineEdit::keyPressEvent(e);
25 return;
28 if (e->key() == Qt::Key_Up) {
29 if (current > 0)
30 setHistoryText(current-1);
32 else if (e->key() == Qt::Key_Down) {
33 if (current < history.size())
34 setHistoryText(current+1);
36 else if (e->key() == Qt::Key_PageUp)
37 pageUp();
38 else if (e->key() == Qt::Key_PageDown)
39 pageDown();
40 else e->ignore();
42 QLineEdit::keyPressEvent(e);
45 void HistLineEdit::setHistoryText(uint index) {
46 if (current == history.size())
47 buffer = text();
49 if (index == history.size())
50 setText(buffer);
51 else
52 setText(history[index]);
54 current = index;
57 void HistLineEdit::updateHistory() {
58 if (echoMode() != Password) {
60 history.push_back(text());
61 buffer = "";
62 current = history.size();
65 receivedInput(text());
66 setText("");