summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCatalin Badea <catalin.badea392@gmail.com>2012-04-13 19:49:39 (GMT)
committerKevin Smith <git@kismith.co.uk>2012-04-14 15:44:35 (GMT)
commitd4517116fc71d1b2d5967004757630293613e928 (patch)
treecfb8d66bb781548c5b60d037ef8372f2e82b921b
parent2dcdee8e9f657c7f5c5d5c507373fd328beaa568 (diff)
downloadswift-contrib-d4517116fc71d1b2d5967004757630293613e928.zip
swift-contrib-d4517116fc71d1b2d5967004757630293613e928.tar.bz2
Improve tab completion
Remember last tab completion suggestion and use it for cycling through all completion candidates. Resolves: #1070 License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
-rw-r--r--Swift/QtUI/QtChatWindow.cpp32
-rw-r--r--Swift/QtUI/QtChatWindow.h4
2 files changed, 28 insertions, 8 deletions
diff --git a/Swift/QtUI/QtChatWindow.cpp b/Swift/QtUI/QtChatWindow.cpp
index cf520ee..5223ddb 100644
--- a/Swift/QtUI/QtChatWindow.cpp
+++ b/Swift/QtUI/QtChatWindow.cpp
@@ -143,22 +143,24 @@ QtChatWindow::QtChatWindow(const QString &contact, QtChatTheme* theme, UIEventSt
input_->setAcceptRichText(false);
inputBarLayout->addWidget(input_);
correctingLabel_ = new QLabel(tr("Correcting"), this);
inputBarLayout->addWidget(correctingLabel_);
correctingLabel_->hide();
layout->addLayout(inputBarLayout);
inputClearing_ = false;
contactIsTyping_ = false;
+ tabCompletion_ = false;
connect(input_, SIGNAL(unhandledKeyPressEvent(QKeyEvent*)), this, SLOT(handleKeyPressEvent(QKeyEvent*)));
connect(input_, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
connect(input_, SIGNAL(textChanged()), this, SLOT(handleInputChanged()));
+ connect(input_, SIGNAL(cursorPositionChanged()), this, SLOT(handleCursorPositionChanged()));
setFocusProxy(input_);
logRosterSplitter_->setFocusProxy(input_);
midBar->setFocusProxy(input_);
messageLog_->setFocusProxy(input_);
connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(qAppFocusChanged(QWidget*, QWidget*)));
connect(messageLog_, SIGNAL(gotFocus()), input_, SLOT(setFocus()));
resize(400,300);
connect(messageLog_, SIGNAL(fontResized(int)), this, SIGNAL(fontResized(int)));
@@ -282,38 +284,45 @@ void QtChatWindow::handleChangeSplitterState(QByteArray state) {
void QtChatWindow::handleSplitterMoved(int, int) {
emit splitterMoved();
}
void QtChatWindow::tabComplete() {
if (!completer_) {
return;
}
-// QTextDocument* document = input_->document();
- QTextCursor cursor = input_->textCursor();
- cursor.select(QTextCursor::WordUnderCursor);
+
+ QTextCursor cursor;
+ if (tabCompleteCursor_.hasSelection()) {
+ cursor = tabCompleteCursor_;
+ } else {
+ cursor = input_->textCursor();
+ cursor.select(QTextCursor::WordUnderCursor);
+ }
QString root = cursor.selectedText();
- bool firstWord = cursor.selectionStart() == 0;
if (root.isEmpty()) {
return;
}
QString suggestion = P2QSTRING(completer_->completeWord(Q2PSTRING(root)));
if (root == suggestion) {
return;
}
+ tabCompletion_ = true;
cursor.beginEditBlock();
cursor.removeSelectedText();
+ int oldPosition = cursor.position();
+
cursor.insertText(suggestion);
- if (firstWord) {
- // cursor.insertText(":");
- }
- //cursor.insertText(" ");
+ tabCompleteCursor_ = cursor;
+ tabCompleteCursor_.setPosition(oldPosition, QTextCursor::KeepAnchor);
+
cursor.endEditBlock();
+ tabCompletion_ = false;
}
void QtChatWindow::setRosterModel(Roster* roster) {
treeWidget_->setRosterModel(roster);
}
void QtChatWindow::setAvailableSecurityLabels(const std::vector<SecurityLabelsCatalog::Item>& labels) {
availableLabels_ = labels;
labelsWidget_->clear();
@@ -693,18 +702,25 @@ void QtChatWindow::handleInputChanged() {
return;
}
if (input_->toPlainText().isEmpty()) {
onUserCancelsTyping();
} else {
onUserTyping();
}
}
+void QtChatWindow::handleCursorPositionChanged() {
+ if (tabCompletion_) {
+ return;
+ }
+ tabCompleteCursor_.clearSelection();
+}
+
void QtChatWindow::show() {
if (parentWidget() == NULL) {
QWidget::show();
}
emit windowOpening();
}
void QtChatWindow::activate() {
if (isWindow()) {
diff --git a/Swift/QtUI/QtChatWindow.h b/Swift/QtUI/QtChatWindow.h
index 18eb092..9db1884 100644
--- a/Swift/QtUI/QtChatWindow.h
+++ b/Swift/QtUI/QtChatWindow.h
@@ -10,18 +10,19 @@
#include <Swift/QtUI/QtMUCConfigurationWindow.h>
#include <Swift/QtUI/QtAffiliationEditor.h>
#include <QtTabbable.h>
#include <SwifTools/LastLineTracker.h>
#include <map>
#include <QPointer>
+#include <QTextCursor>
class QTextEdit;
class QLineEdit;
class QComboBox;
class QLabel;
class QSplitter;
class QPushButton;
namespace Swift {
@@ -112,18 +113,19 @@ namespace Swift {
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
protected:
void showEvent(QShowEvent* event);
private slots:
void returnPressed();
void handleInputChanged();
+ void handleCursorPositionChanged();
void handleKeyPressEvent(QKeyEvent* event);
void handleSplitterMoved(int pos, int index);
void handleAlertButtonClicked();
void handleActionButtonClicked();
void handleHTMLButtonClicked(QString id, QString arg1, QString arg2, QString arg3);
void handleAffiliationEditorAccepted();
private:
@@ -144,36 +146,38 @@ namespace Swift {
std::string addMessage(const std::string &message, const std::string &senderName, bool senderIsSelf, boost::shared_ptr<SecurityLabel> label, const std::string& avatarPath, const QString& style, const boost::posix_time::ptime& time);
void handleOccupantSelectionChanged(RosterItem* item);
bool appendToPreviousCheck(PreviousMessageKind messageKind, const std::string& senderName, bool senderIsSelf) const;
int unreadCount_;
bool contactIsTyping_;
LastLineTracker lastLineTracker_;
QString contact_;
QString lastSentMessage_;
+ QTextCursor tabCompleteCursor_;
QtChatView* messageLog_;
QtChatTheme* theme_;
QtTextEdit* input_;
QComboBox* labelsWidget_;
QtOccupantListWidget* treeWidget_;
QLabel* correctingLabel_;
QLabel* alertLabel_;
QWidget* alertWidget_;
QPushButton* alertButton_;
TabComplete* completer_;
QLineEdit* subject_;
QPushButton* actionButton_;
std::vector<SecurityLabelsCatalog::Item> availableLabels_;
bool isCorrection_;
bool previousMessageWasSelf_;
PreviousMessageKind previousMessageKind_;
QString previousSenderName_;
bool inputClearing_;
+ bool tabCompletion_;
UIEventStream* eventStream_;
bool inputEnabled_;
QSplitter *logRosterSplitter_;
Tristate correctionEnabled_;
QString alertStyleSheet_;
std::map<QString, QString> descriptions;
QtChatWindowJSBridge* jsBridge;
QPointer<QtMUCConfigurationWindow> mucConfigurationWindow_;
QPointer<QtAffiliationEditor> affiliationEditor_;