2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\r
8 * Software distributed under the License is distributed
\r
9 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
\r
10 * express or implied. See the GPL for the specific language
\r
11 * governing rights and limitations.
\r
13 * You should have received a copy of the GPL along with this
\r
14 * program. If not, go to http://www.gnu.org/licenses/gpl.html
\r
15 * or write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
20 package cx.fbn.nevernote.gui;
\r
22 import java.util.ArrayList;
\r
23 import java.util.List;
\r
25 import com.evernote.edam.type.Tag;
\r
26 import com.trolltech.qt.core.QEvent;
\r
27 import com.trolltech.qt.core.QModelIndex;
\r
28 import com.trolltech.qt.gui.QLineEdit;
\r
30 import cx.fbn.nevernote.Global;
\r
32 public class TagLineEdit extends QLineEdit {
\r
33 private String text;
\r
34 private final boolean changed;
\r
35 public Signal2<List<String>, String> text_changed = new Signal2<List<String>, String>();
\r
36 public TagLineCompleter tagCompleter;
\r
37 public Signal0 focusLost = new Signal0();
\r
38 public String currentCompleterSelection;
\r
40 public TagLineEdit(List<Tag> allTags) {
\r
41 textChanged.connect(this, "textChanged(String)");
\r
42 tagCompleter = new TagLineCompleter(this);
\r
43 text_changed.connect(tagCompleter, "update(List, String)");
\r
44 tagCompleter.activated.connect(this, "completeText(String)");
\r
45 tagCompleter.highlightedIndex.connect(this, "completerSelected(QModelIndex)");
\r
47 currentCompleterSelection = null;
\r
50 public boolean hasChanged() {
\r
54 @SuppressWarnings("unused")
\r
55 private void textChanged(String t) {
\r
57 int cursor_pos = cursorPosition();
\r
58 String temp_text = text.substring(0,cursor_pos);
\r
59 int delimeter = temp_text.trim().lastIndexOf(Global.tagDelimeter);
\r
62 prefix = temp_text.substring(delimeter+1).trim();
\r
64 prefix = temp_text.trim();
\r
66 List<String> newTags = new ArrayList<String>();
\r
67 List<String> currentTags = new ArrayList<String>();
\r
68 String list[] = text.split(Global.tagDelimeter);
\r
69 for (String element : list) {
\r
70 currentTags.add(element.trim());
\r
73 text_changed.emit(currentTags, prefix.trim());
\r
78 // This method is used to store the current selection from the completer. It is
\r
79 // saved until later in case focus is lost while one is selected, but Enter is never pressed.
\r
80 @SuppressWarnings("unused")
\r
81 private void completerSelected(QModelIndex model) {
\r
82 currentCompleterSelection = null;
\r
85 tagCompleter.setCurrentRow(model.row());
\r
86 currentCompleterSelection = tagCompleter.currentCompletion();
\r
87 tagCompleter.setCurrentRow(model.row());
\r
90 public void completeText(String text){
\r
91 int cursor_pos = cursorPosition();
\r
92 String before_text = text().substring(0,cursor_pos);
\r
93 String after_text = text().substring(cursor_pos);
\r
94 int prefix_len = before_text.lastIndexOf(Global.tagDelimeter);
\r
95 if (prefix_len == -1) {
\r
96 prefix_len = cursor_pos;
\r
99 before_text = before_text.substring(0,cursor_pos-1);
\r
101 int nextTagPos = after_text.indexOf(Global.tagDelimeter);
\r
102 if (nextTagPos == -1) {
\r
106 setText(before_text +text +Global.tagDelimeter +" " +after_text);
\r
107 setCursorPosition(cursor_pos - prefix_len + text().length() +2);
\r
110 public void setTagList(List<Tag> t) {
\r
111 tagCompleter.resetList();
\r
112 tagCompleter.setTagList(t);
\r
116 public boolean event(QEvent e) {
\r
117 if (e.type().equals(QEvent.Type.FocusOut)) {
\r
120 return super.event(e);
\r