IOWidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled
5  applications.
6 
7  TerraLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License,
10  or (at your option) any later version.
11 
12  TerraLib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with TerraLib. See COPYING. If not, write to
19  TerraLib Team at <terralib-team@terralib.org>.
20  */
21 
22 /*!
23  \file terralib/qt/widgets/ceditor/IOWidget.cpp
24 
25  \brief A widget that can be used to do input and out
26 */
27 
28 // TerraLib
29 #include "IOWidget.h"
30 
31 // QT
32 #include <QKeyEvent>
33 #include <QTextBlock>
34 #include <QTextCursor>
35 #include <QTimer>
36 
38  : QPlainTextEdit(parent), userPrompt(QString(""))
39 {
40  setLineWrapMode(WidgetWidth);
41  lastIndex = getIndex(this->textCursor());
42 }
43 
45 {
46  switch(e->key())
47  {
48  case Qt::Key_Return:
49  case Qt::Key_Enter:
50  handleEnter();
51  break;
52  case Qt::Key_Backspace:
53  handleLeft(e);
54  break;
55  case Qt::Key_Left:
56  handleLeft(e);
57  break;
58  case Qt::Key_Home:
59  handleHome();
60  break;
61  default:
62  if(isWritable(this->textCursor()))
63  QPlainTextEdit::keyPressEvent(e);
64  break;
65  }
66 }
67 
69 {
70  QString cmd = getCommand();
71 
72  QPlainTextEdit::moveCursor(QTextCursor::EndOfLine);
73 
74  if(cmd.length() > 0)
75  {
76  setFocus();
77  insertPlainText("\n");
78  lastIndex = getIndex(this->textCursor());
79  emit command(cmd);
80  }
81  else
82  {
83  appendPlainText(prompt());
84  ensureCursorVisible();
85  }
86 }
87 
89 {
90  insertPlainText(userPrompt);
91  ensureCursorVisible();
92  QTimer::singleShot(0, this, SLOT(setFocus()));
93  locked = false;
94 }
95 
97 {
98  insertPlainText(text);
99  ensureCursorVisible();
100  lastIndex = getIndex(this->textCursor());
101  locked = true;
102 }
103 
105 {
106  QTextCursor c = this->textCursor();
107  c.select(QTextCursor::LineUnderCursor);
108  c.removeSelectedText();
109 }
110 
112 {
113  QTextCursor c = this->textCursor();
114  c.select(QTextCursor::LineUnderCursor);
115 
116  QString text = c.selectedText();
117  text.remove(0, lastIndex + userPrompt.length());
118 
119  return text;
120 }
121 
123 {
124  if(getIndex(textCursor()) > userPrompt.length())
125  {
126  QPlainTextEdit::keyPressEvent(event);
127  }
128 }
129 
131 {
132  QTextCursor c = textCursor();
133  c.movePosition(QTextCursor::StartOfLine);
134  c.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor,
135  userPrompt.length());
136  setTextCursor(c);
137 }
138 
139 int te::qt::widgets::IOWidget::getIndex(const QTextCursor &cursor)
140 {
141  QTextBlock b;
142  int column = 1;
143  b = cursor.block();
144  column = cursor.position() - b.position();
145  return column;
146 }
147 
149 {
150  userPrompt = prompt;
151  clearLine();
152 }
153 
155 {
156  return userPrompt;
157 }
158 
159 bool te::qt::widgets::IOWidget::isWritable(const QTextCursor &cursor)
160 {
161  QTextCursor tempCursor = textCursor();
162  tempCursor.movePosition(QTextCursor::End);
163  return (getIndex(cursor) >= prompt().length()) &&
164  (cursor.blockNumber() + 1) == (tempCursor.blockNumber() + 1) &&
165  !locked;
166 }
void setPrompt(const QString &prompt)
setPrompt Sets a new prompt from a given string.
Definition: IOWidget.cpp:148
void append(QString text)
append Appends the given text to the text edit and locks the widget for input
Definition: IOWidget.cpp:96
QString userPrompt
userPrompt prompt to indicate that the text edit is waiting for input
Definition: IOWidget.h:110
void handleHome()
handleHome Handles Home key press.
Definition: IOWidget.cpp:130
void handleLeft(QKeyEvent *event)
handleLeft Handles Left arrow key press.
Definition: IOWidget.cpp:122
void handleEnter()
handleEnter Handles Enter and Return key press.
Definition: IOWidget.cpp:68
int b
Definition: TsRtree.cpp:32
void clearLine()
clearLine Clears the line under the cursor.
Definition: IOWidget.cpp:104
void addPrompt()
addPrompt Adds the prompt to the text edit and unlocks the widget for input
Definition: IOWidget.cpp:88
int lastIndex
lastIndex The index of the last character.
Definition: IOWidget.h:121
int getIndex(const QTextCursor &cursor)
getIndex Gets the index of a given cursor.
Definition: IOWidget.cpp:139
QString prompt() const
prompt Retrieves the current prompt.
Definition: IOWidget.cpp:154
IOWidget(QWidget *parent=0)
Definition: IOWidget.cpp:37
QString getCommand() const
getCommand Gets the input from the user.
Definition: IOWidget.cpp:111
bool isWritable(const QTextCursor &cursor)
isWritable Checks if a given cursor is at a writable position.
Definition: IOWidget.cpp:159
void command(QString command)
command Signal emitted when input is entered.
bool locked
locked False if the text edit can receive input, true otherwise.
Definition: IOWidget.h:116
void keyPressEvent(QKeyEvent *e)
Definition: IOWidget.cpp:44
A widget that can be used to do input and out.