Project

General

Profile

0001-InputLine-history-handling-improved.patch

admin, 10/07/2008 06:09 PM

View differences:

src/uisupport/inputline.cpp
39 39
  case Qt::Key_Up:
40 40
    event->accept();
41 41

  
42
    if(addToHistory(text())) {
43
      clear();
44
      break;
45
    }
42
    addToHistory(text(), true);
46 43
    
47 44
    if(idx > 0) {
48 45
      idx--;
49
      setText(history[idx]);
46
      showHistoryEntry();
50 47
    }
51 48

  
52 49
    break;
......
54 51
  case Qt::Key_Down:
55 52
    event->accept();
56 53

  
57
    if(addToHistory(text())) {
58
      clear();
59
      break;
60
    }
54
    addToHistory(text(), true);
61 55
    
62 56
    if(idx < history.count())
63 57
      idx++;
64 58

  
65 59
    if(idx < history.count())
66
      setText(history[idx]);
60
      showHistoryEntry();
67 61
    else
68
      clear();
62
      resetLine();              // equals clear() in this case
69 63

  
70 64
    break;
71 65
    
......
77 71
  }
78 72
}
79 73

  
80
bool InputLine::addToHistory(const QString &text) {
74
bool InputLine::addToHistory(const QString &text, bool temporary) {
81 75
  if(text.isEmpty())
82 76
    return false;
83 77

  
84 78
  Q_ASSERT(0 <= idx && idx <= history.count());
85 79
  
86 80
  if(history.isEmpty() || text != history[idx - (int)(idx == history.count())]) {
87
    // if we change an entry of the history the changed entry is appended to the list and we seek to the end
88
    // we could also easily change the entry in the history... per setting maybe?
89
    history << text;
90
    idx = history.count();
81
    // if an entry of the history is changed, we remember it and show it again at this
82
    // position until a line was actually sent
83
    // sent lines get appended to the history
84
    if(temporary) {
85
      tempHistory[idx] = text;
86
    } else {
87
      history << text;
88
      tempHistory.clear();
89
    }
91 90
    return true;
92 91
  } else {
93 92
    return false;
......
97 96
void InputLine::on_returnPressed() {
98 97
  addToHistory(text());
99 98
  emit sendText(text());
100
  clear();
99
  resetLine();
101 100
}
102 101

  
103 102
void InputLine::on_textChanged(QString newText) {
......
125 124
    emit returnPressed();
126 125
    insert(remainder);
127 126
  }
128
  
129 127
}
130 128

  
129
void InputLine::resetLine() {
130
  // every time the InputLine is cleared we also reset history index
131
  idx = history.count();
132
  clear();
133
}
134

  
135
void InputLine::showHistoryEntry() {
136
  // if the user changed the history, display the changed line
137
  tempHistory.contains(idx) ? setText(tempHistory[idx]) : setText(history[idx]);
138
}
src/uisupport/inputline.h
40 40
  void on_returnPressed();
41 41
  void on_textChanged(QString newText);
42 42

  
43
  bool addToHistory(const QString &text);
43
  bool addToHistory(const QString &text, bool temporary = false);
44 44

  
45 45
signals:
46 46
  void sendText(QString text);
47 47

  
48 48
private:
49 49
  QStringList history;
50
  QHash<int, QString> tempHistory;
50 51
  qint32 idx;
51 52
  TabCompleter *tabCompleter;
52 53

  
53 54
  int bindModifier;
54 55
  int jumpModifier;
56

  
57
  void resetLine();
58
  void showHistoryEntry();
55 59
};
56 60

  
57 61
#endif
58
-