From 5b85f7905549356f8f0e522016da3bd17bc801cc Mon Sep 17 00:00:00 2001 From: dubslow Date: Mon, 20 Oct 2014 05:01:22 -0500 Subject: [PATCH 1/4] tab completion for groupchats (fixes #486) --- qtox.pro | 6 ++++-- src/widget/form/genericchatform.h | 2 +- src/widget/form/groupchatform.cpp | 5 +++++ src/widget/form/groupchatform.h | 2 ++ src/widget/tool/chattextedit.cpp | 14 +++++++++----- src/widget/tool/chattextedit.h | 2 ++ 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/qtox.pro b/qtox.pro index fe826b158..88ca5339e 100644 --- a/qtox.pro +++ b/qtox.pro @@ -138,7 +138,8 @@ HEADERS += src/widget/form/addfriendform.h \ src/widget/maskablepixmapwidget.h \ src/videosource.h \ src/cameraworker.h \ - src/widget/videosurface.h + src/widget/videosurface.h \ + src/widget/form/tabcompleter.h SOURCES += \ src/widget/form/addfriendform.cpp \ @@ -186,4 +187,5 @@ SOURCES += \ src/widget/maskablepixmapwidget.cpp \ src/cameraworker.cpp \ src/widget/videosurface.cpp \ - src/netvideosource.cpp + src/netvideosource.cpp \ + src/widget/form/tabcompleter.cpp diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index 0baa4f315..3f05639bc 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -22,7 +22,7 @@ #include // Spacing in px inserted when the author of the last message changes -#define AUTHOR_CHANGE_SPACING 5 +#define AUTHOR_CHANGE_SPACING 5 // why the hell is this a thing? surely the different font is enough? class QLabel; class QVBoxLayout; diff --git a/src/widget/form/groupchatform.cpp b/src/widget/form/groupchatform.cpp index 0c5b4c9e1..11a32a71a 100644 --- a/src/widget/form/groupchatform.cpp +++ b/src/widget/form/groupchatform.cpp @@ -15,6 +15,7 @@ */ #include "groupchatform.h" +#include "tabcompleter.h" #include "src/group.h" #include "src/widget/groupwidget.h" #include "src/widget/tool/chattextedit.h" @@ -33,6 +34,8 @@ GroupChatForm::GroupChatForm(Group* chatGroup) namesList = new QLabel(); namesList->setObjectName("peersLabel"); + tabber = new TabCompleter(msgEdit, group); + fileButton->setEnabled(false); callButton->setVisible(false); videoButton->setVisible(false); @@ -59,6 +62,8 @@ GroupChatForm::GroupChatForm(Group* chatGroup) connect(sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered())); connect(msgEdit, SIGNAL(enterPressed()), this, SLOT(onSendTriggered())); + connect(msgEdit, &ChatTextEdit::tabPressed, tabber, &TabCompleter::complete); + connect(msgEdit, &ChatTextEdit::keyPressed, tabber, &TabCompleter::reset); setAcceptDrops(true); } diff --git a/src/widget/form/groupchatform.h b/src/widget/form/groupchatform.h index 6b3984b06..f715768e8 100644 --- a/src/widget/form/groupchatform.h +++ b/src/widget/form/groupchatform.h @@ -21,6 +21,7 @@ namespace Ui {class MainWindow;} class Group; +class TabCompleter; class GroupChatForm : public GenericChatForm { @@ -41,6 +42,7 @@ protected: private: Group* group; QLabel *nusersLabel, *namesList; + TabCompleter* tabber; }; #endif // GROUPCHATFORM_H diff --git a/src/widget/tool/chattextedit.cpp b/src/widget/tool/chattextedit.cpp index 9b51b8fba..d9ba2ee46 100644 --- a/src/widget/tool/chattextedit.cpp +++ b/src/widget/tool/chattextedit.cpp @@ -27,11 +27,15 @@ ChatTextEdit::ChatTextEdit(QWidget *parent) : void ChatTextEdit::keyPressEvent(QKeyEvent * event) { int key = event->key(); - if ((key == Qt::Key_Enter || key == Qt::Key_Return) - && !(event->modifiers() && Qt::ShiftModifier)) - { + if ((key == Qt::Key_Enter || key == Qt::Key_Return) && !(event->modifiers() && Qt::ShiftModifier)) emit enterPressed(); - return; + else if (key == Qt::Key_Tab) + emit tabPressed(); + else if (key == Qt::Key_Backspace) // because of the backspace() hack in tabber, we can't emit on these + QTextEdit::keyPressEvent(event); + else + { + emit keyPressed(); + QTextEdit::keyPressEvent(event); } - QTextEdit::keyPressEvent(event); } diff --git a/src/widget/tool/chattextedit.h b/src/widget/tool/chattextedit.h index f8a1cb73a..6bab07cda 100644 --- a/src/widget/tool/chattextedit.h +++ b/src/widget/tool/chattextedit.h @@ -28,6 +28,8 @@ public: signals: void enterPressed(); + void tabPressed(); + void keyPressed(); public slots: From 30e2afcc92886d37e003a5e6b76bf718cba52f88 Mon Sep 17 00:00:00 2001 From: dubslow Date: Mon, 20 Oct 2014 06:05:45 -0500 Subject: [PATCH 2/4] add alerts on user mention (could use some tweaking, but is plenty fine for now) --- qtox.pro | 2 + src/misc/style.cpp | 2 + src/misc/style.h | 1 + src/widget/form/genericchatform.cpp | 8 ++++ src/widget/form/genericchatform.h | 1 + src/widget/tool/chatactions/actionaction.cpp | 28 ++--------- src/widget/tool/chatactions/actionaction.h | 6 +-- src/widget/tool/chatactions/alertaction.cpp | 47 +++++++++++++++++++ src/widget/tool/chatactions/alertaction.h | 35 ++++++++++++++ src/widget/tool/chatactions/messageaction.cpp | 17 ++++--- src/widget/tool/chatactions/messageaction.h | 3 +- src/widget/widget.cpp | 11 +++-- ui/chatArea/innerStyle.css | 14 ++++++ 13 files changed, 138 insertions(+), 37 deletions(-) create mode 100644 src/widget/tool/chatactions/alertaction.cpp create mode 100644 src/widget/tool/chatactions/alertaction.h diff --git a/qtox.pro b/qtox.pro index 88ca5339e..0d0019c91 100644 --- a/qtox.pro +++ b/qtox.pro @@ -135,6 +135,7 @@ HEADERS += src/widget/form/addfriendform.h \ src/widget/tool/chatactions/filetransferaction.h \ src/widget/tool/chatactions/systemmessageaction.h \ src/widget/tool/chatactions/actionaction.h \ + src/widget/tool/chatactions/alertaction.h \ src/widget/maskablepixmapwidget.h \ src/videosource.h \ src/cameraworker.h \ @@ -184,6 +185,7 @@ SOURCES += \ src/widget/tool/chatactions/filetransferaction.cpp \ src/widget/tool/chatactions/systemmessageaction.cpp \ src/widget/tool/chatactions/actionaction.cpp \ + src/widget/tool/chatactions/alertaction.cpp \ src/widget/maskablepixmapwidget.cpp \ src/cameraworker.cpp \ src/widget/videosurface.cpp \ diff --git a/src/misc/style.cpp b/src/misc/style.cpp index 70100270f..e9dec32e3 100644 --- a/src/misc/style.cpp +++ b/src/misc/style.cpp @@ -70,6 +70,7 @@ QColor Style::getColor(Style::ColorPalette entry) QColor("#414141").lighter(120), QColor("#d1d1d1"), QColor("#ffffff"), + QColor("#ff7700"), }; return palette[entry]; @@ -108,6 +109,7 @@ QString Style::resolve(QString qss) {"@mediumGreyLight", getColor(MediumGreyLight).name()}, {"@lightGrey", getColor(LightGrey).name()}, {"@white", getColor(White).name()}, + {"@orange", getColor(Orange).name()}, // fonts {"@extraBig", qssifyFont(getFont(ExtraBig))}, diff --git a/src/misc/style.h b/src/misc/style.h index a07064e04..e818b2ddb 100644 --- a/src/misc/style.h +++ b/src/misc/style.h @@ -37,6 +37,7 @@ public: MediumGreyLight, LightGrey, White, + Orange, }; enum Font diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index bdb764777..bcac25589 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -25,6 +25,7 @@ #include "src/widget/tool/chatactions/messageaction.h" #include "src/widget/tool/chatactions/systemmessageaction.h" #include "src/widget/tool/chatactions/actionaction.h" +#include "src/widget/tool/chatactions/alertaction.h" #include "src/widget/chatareawidget.h" #include "src/widget/tool/chattextedit.h" #include "src/widget/maskablepixmapwidget.h" @@ -193,6 +194,13 @@ void GenericChatForm::addMessage(QString author, QString message, bool isAction, previousName = author; } +void GenericChatForm::addAlertMessage(QString author, QString message, QDateTime datetime) +{ + QString date = datetime.toString(Settings::getInstance().getTimestampFormat()); + chatWidget->insertMessage(new AlertAction(author, message, date)); + previousName = author; +} + void GenericChatForm::onEmoteButtonClicked() { // don't show the smiley selection widget if there are no smileys available diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index 3f05639bc..5782823a6 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -45,6 +45,7 @@ public: virtual void setName(const QString &newName); virtual void show(Ui::MainWindow &ui); void addMessage(QString author, QString message, bool isAction = false, QDateTime datetime=QDateTime::currentDateTime()); + void addAlertMessage(QString author, QString message, QDateTime datetime=QDateTime::currentDateTime()); void addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime=QDateTime::currentDateTime()); int getNumberOfMessages(); diff --git a/src/widget/tool/chatactions/actionaction.cpp b/src/widget/tool/chatactions/actionaction.cpp index 3facb978c..995b89e9c 100644 --- a/src/widget/tool/chatactions/actionaction.cpp +++ b/src/widget/tool/chatactions/actionaction.cpp @@ -15,12 +15,11 @@ */ #include "actionaction.h" -#include "src/misc/smileypack.h" -ActionAction::ActionAction(const QString &author, const QString &message, const QString &date, const bool& me) : - ChatAction(me, author, date), - message(message) +ActionAction::ActionAction(const QString &author, QString message, const QString &date, const bool& me) : + MessageAction(author, message, date, me) { + message = name + " " + message; } void ActionAction::setup(QTextCursor cursor, QTextEdit *) @@ -45,24 +44,5 @@ QString ActionAction::getName() QString ActionAction::getMessage() { - QString message_ = SmileyPack::getInstance().smileyfied(toHtmlChars(message)); - - // detect urls - QRegExp exp("(www\\.|http[s]?:\\/\\/|ftp:\\/\\/)\\S+"); - int offset = 0; - while ((offset = exp.indexIn(message_, offset)) != -1) - { - QString url = exp.cap(); - - // add scheme if not specified - if (exp.cap(1) == "www.") - url.prepend("http://"); - - QString htmledUrl = QString("%1").arg(url); - message_.replace(offset, exp.cap().length(), htmledUrl); - - offset += htmledUrl.length(); - } - - return QString("
%1 %2
").arg(name).arg(message_); + return MessageAction::getMessage("action"); } diff --git a/src/widget/tool/chatactions/actionaction.h b/src/widget/tool/chatactions/actionaction.h index 08553a96e..d438f0ecd 100644 --- a/src/widget/tool/chatactions/actionaction.h +++ b/src/widget/tool/chatactions/actionaction.h @@ -17,12 +17,12 @@ #ifndef ACTIONACTION_H #define ACTIONACTION_H -#include "chataction.h" +#include "messageaction.h" -class ActionAction : public ChatAction +class ActionAction : public MessageAction { public: - ActionAction(const QString &author, const QString &message, const QString& date, const bool&); + ActionAction(const QString &author, QString message, const QString& date, const bool&); virtual ~ActionAction(){;} virtual QString getMessage(); virtual QString getName(); diff --git a/src/widget/tool/chatactions/alertaction.cpp b/src/widget/tool/chatactions/alertaction.cpp new file mode 100644 index 000000000..b62c1896e --- /dev/null +++ b/src/widget/tool/chatactions/alertaction.cpp @@ -0,0 +1,47 @@ +/* + Copyright (C) 2014 by Project Tox + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +#include "alertaction.h" + +AlertAction::AlertAction(const QString &author, const QString &message, const QString &date) : + MessageAction(author, message, date, false) +{ +} + +void AlertAction::setup(QTextCursor cursor, QTextEdit *) +{ + // When this function is called, we're supposed to only update ourselve when needed + // Nobody should ask us to do anything with our content, we're on our own + // Except we never udpate on our own, so we can safely free our resources + + (void) cursor; + message.clear(); + message.squeeze(); + name.clear(); + name.squeeze(); + date.clear(); + date.squeeze(); +} +/* +QString AlertAction::getName() +{ + return QString("
%2
").arg("alert_name").arg(toHtmlChars(name)); +} +*/ +QString AlertAction::getMessage() +{ + return MessageAction::getMessage("alert"); +} diff --git a/src/widget/tool/chatactions/alertaction.h b/src/widget/tool/chatactions/alertaction.h new file mode 100644 index 000000000..64e00e6bf --- /dev/null +++ b/src/widget/tool/chatactions/alertaction.h @@ -0,0 +1,35 @@ +/* + Copyright (C) 2014 by Project Tox + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +#ifndef ALERTACTION_H +#define ALERTACTION_H + +#include "messageaction.h" + +class AlertAction : public MessageAction +{ +public: + AlertAction(const QString &author, const QString &message, const QString& date); + virtual ~AlertAction(){;} + virtual QString getMessage(); + //virtual QString getName(); only do the message for now; preferably would do the whole row + virtual void setup(QTextCursor cursor, QTextEdit*) override; + +private: + QString message; +}; + +#endif // MESSAGEACTION_H diff --git a/src/widget/tool/chatactions/messageaction.cpp b/src/widget/tool/chatactions/messageaction.cpp index 4a8b6962b..8058aa56e 100644 --- a/src/widget/tool/chatactions/messageaction.cpp +++ b/src/widget/tool/chatactions/messageaction.cpp @@ -38,7 +38,7 @@ void MessageAction::setup(QTextCursor cursor, QTextEdit *) date.squeeze(); } -QString MessageAction::getMessage() +QString MessageAction::getMessage(QString div) { QString message_ = SmileyPack::getInstance().smileyfied(toHtmlChars(message)); @@ -65,14 +65,19 @@ QString MessageAction::getMessage() for (QString& s : messageLines) { if (QRegExp("^[ ]*>.*").exactMatch(s)) - message_ += ">" + s.right(s.length()-4) + "
"; + message_ += "" + s.right(s.length()-4) + "
"; else message_ += s + "
"; } message_ = message_.left(message_.length()-4); - if (isMe) - return QString("
" + message_ + "
"); - else - return QString("
" + message_ + "
"); + return QString(QString("
").arg(div) + message_ + "
"); +} + +QString MessageAction::getMessage() +{ + if (isMe) + return getMessage("message_me"); + else + return getMessage("message"); } diff --git a/src/widget/tool/chatactions/messageaction.h b/src/widget/tool/chatactions/messageaction.h index aa32801eb..c5ba63de3 100644 --- a/src/widget/tool/chatactions/messageaction.h +++ b/src/widget/tool/chatactions/messageaction.h @@ -25,9 +25,10 @@ public: MessageAction(const QString &author, const QString &message, const QString &date, const bool &me); virtual ~MessageAction(){;} virtual QString getMessage(); + virtual QString getMessage(QString div); virtual void setup(QTextCursor cursor, QTextEdit*) override; -private: +protected: QString message; }; diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index fdc3465d8..dd34374d5 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -750,14 +750,19 @@ void Widget::onGroupMessageReceived(int groupnumber, const QString& message, con if (!g) return; - g->chatForm->addMessage(author, message); + QString name = core->getUsername(); + bool targeted = (author != name) && message.contains(name, Qt::CaseInsensitive); + if (targeted) + g->chatForm->addAlertMessage(author, message); + else + g->chatForm->addMessage(author, message); if ((static_cast(g->widget) != activeChatroomWidget) || isMinimized() || !isActiveWindow()) { g->hasNewMessages = 1; - newMessageAlert(); // sound alert on any message, not just naming user - if (message.contains(core->getUsername(), Qt::CaseInsensitive)) + if (targeted) { + newMessageAlert(); g->userWasMentioned = 1; // useful for highlighting line or desktop notifications } g->widget->updateStatusLight(); diff --git a/ui/chatArea/innerStyle.css b/ui/chatArea/innerStyle.css index b5c537362..1b5e2e844 100644 --- a/ui/chatArea/innerStyle.css +++ b/ui/chatArea/innerStyle.css @@ -67,6 +67,20 @@ div.red { font: @small; } +div.alert { + margin-left: 0px; + margin-right: 0px; + color: @black; + background-color: @orange; + font: @big; +} + +div.alert_name { + color: @black; + background-color: @orange; + font: @bigBold; +} + div.button { margin-top: 0px; margin-bottom: 0px; From aa55dcae1efd754d98bb03044cbb5b015ebfeebc Mon Sep 17 00:00:00 2001 From: dubslow Date: Mon, 20 Oct 2014 06:34:17 -0500 Subject: [PATCH 3/4] fix bug, fix actions to be like irc, and a disabled feature for future --- src/widget/form/genericchatform.cpp | 2 +- src/widget/tool/chatactions/actionaction.cpp | 4 ++-- src/widget/widget.cpp | 10 ++++++++-- ui/chatArea/innerStyle.css | 12 ++++++------ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index bcac25589..d25484543 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -174,7 +174,7 @@ void GenericChatForm::addMessage(QString author, QString message, bool isAction, QString date = datetime.toString(Settings::getInstance().getTimestampFormat()); bool isMe = (author == Widget::getInstance()->getUsername()); - if (!isAction && message.startsWith("/me ")) + if (!isAction && message.startsWith("/me")) { // always render actions regardless of what core thinks isAction = true; message = message.right(message.length()-4); diff --git a/src/widget/tool/chatactions/actionaction.cpp b/src/widget/tool/chatactions/actionaction.cpp index 995b89e9c..c68f4449f 100644 --- a/src/widget/tool/chatactions/actionaction.cpp +++ b/src/widget/tool/chatactions/actionaction.cpp @@ -15,11 +15,11 @@ */ #include "actionaction.h" +#include ActionAction::ActionAction(const QString &author, QString message, const QString &date, const bool& me) : - MessageAction(author, message, date, me) + MessageAction(author, author+" "+message, date, me) { - message = name + " " + message; } void ActionAction::setup(QTextCursor cursor, QTextEdit *) diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index dd34374d5..6e69c9c73 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -778,17 +778,23 @@ void Widget::onGroupNamelistChanged(int groupnumber, int peernumber, uint8_t Cha g = createGroup(groupnumber); } + QString name = core->getGroupPeerName(groupnumber, peernumber); TOX_CHAT_CHANGE change = static_cast(Change); if (change == TOX_CHAT_CHANGE_PEER_ADD) { - QString name = core->getGroupPeerName(groupnumber, peernumber); if (name.isEmpty()) name = tr("", "Placeholder when we don't know someone's name in a group chat"); g->addPeer(peernumber,name); + //g->chatForm->addSystemInfoMessage(tr("%1 has joined the chat").arg(name), "green"); + // we can't display these messages until irungentoo fixes peernumbers + // https://github.com/irungentoo/toxcore/issues/1128 } else if (change == TOX_CHAT_CHANGE_PEER_DEL) + { g->removePeer(peernumber); - else if (change == TOX_CHAT_CHANGE_PEER_NAME) + //g->chatForm->addSystemInfoMessage(tr("%1 has left the chat").arg(name), "silver"); + } + else if (change == TOX_CHAT_CHANGE_PEER_NAME) // core overwrites old name before telling us it changed... g->updatePeer(peernumber,core->getGroupPeerName(groupnumber, peernumber)); } diff --git a/ui/chatArea/innerStyle.css b/ui/chatArea/innerStyle.css index 1b5e2e844..dc1f46d73 100644 --- a/ui/chatArea/innerStyle.css +++ b/ui/chatArea/innerStyle.css @@ -38,8 +38,8 @@ span.quote { } div.green { - margin-top: 12px; - margin-bottom: 12px; + margin-top: 6px; + margin-bottom: 6px; margin-left: 0px; margin-right: 0px; color: @white; @@ -48,8 +48,8 @@ div.green { } div.silver { - margin-top: 12px; - margin-bottom: 12px; + margin-top: 6px; + margin-bottom: 6px; margin-left: 0px; margin-right: 0px; color: @black; @@ -58,8 +58,8 @@ div.silver { } div.red { - margin-top: 12px; - margin-bottom: 12px; + margin-top: 6px; + margin-bottom: 6px; margin-left: 0px; margin-right: 0px; color: @white; From 995e86aad95f0ef44cb65a5e88cdf10070ce3115 Mon Sep 17 00:00:00 2001 From: dubslow Date: Mon, 20 Oct 2014 06:53:17 -0500 Subject: [PATCH 4/4] ADD FORGOTTEN FILES WHY DO THESE THINGS HAPPEN (and fix outbound actions) --- src/widget/form/chatform.cpp | 2 +- src/widget/form/tabcompleter.cpp | 122 +++++++++++++++++++++++++++++++ src/widget/form/tabcompleter.h | 61 ++++++++++++++++ 3 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 src/widget/form/tabcompleter.cpp create mode 100644 src/widget/form/tabcompleter.h diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index e95f7fe93..eefeb8868 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -87,7 +87,7 @@ void ChatForm::onSendTriggered() if (msg.isEmpty()) return; QString name = Widget::getInstance()->getUsername(); - if (msg.startsWith("/me ")) + if (msg.startsWith("/me")) { msg = msg.right(msg.length() - 4); addMessage(name, msg, true); diff --git a/src/widget/form/tabcompleter.cpp b/src/widget/form/tabcompleter.cpp new file mode 100644 index 000000000..fa4691e13 --- /dev/null +++ b/src/widget/form/tabcompleter.cpp @@ -0,0 +1,122 @@ +/* + Copyright (C) 2005-2014 by the Quassel Project and Project Tox + devel@quassel-irc.org and https://tox.im + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +/* This file was taken from the Quassel IRC client source (src/uisupport), and + was greatly simplified for use in qTox. */ + +#include "tabcompleter.h" +#include "src/core.h" +#include "src/group.h" +#include "src/widget/tool/chattextedit.h" +#include +#include + +const QString TabCompleter::nickSuffix = QString(": "); + +TabCompleter::TabCompleter(ChatTextEdit* msgEdit, Group* group) + : QObject(msgEdit), msgEdit(msgEdit), group(group), enabled(false) +{ +} + +/* from quassel/src/uisupport/multilineedit.h + // Compatibility methods with the rest of the classes which still expect this to be a QLineEdit + inline QString text() const { return toPlainText(); } + inline QString html() const { return toHtml(); } + inline int cursorPosition() const { return textCursor().position(); } + inline void insert(const QString &newText) { insertPlainText(newText); } + inline void backspace() { keyPressEvent(new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier)); } +*/ + +void TabCompleter::buildCompletionList() +{ + // ensure a safe state in case we return early. + completionMap.clear(); + nextCompletion = completionMap.begin(); + + // split the string on the given RE (not chars, nums or braces/brackets) and take the last section + QString tabAbbrev = msgEdit->toPlainText().left(msgEdit->textCursor().position()).section(QRegExp("[^\\w\\d-_\\[\\]{}|`^.\\\\]"), -1, -1); + // that section is then used as the completion regex + QRegExp regex(QString("^[-_\\[\\]{}|`^.\\\\]*").append(QRegExp::escape(tabAbbrev)), Qt::CaseInsensitive); + + for(auto name : group->peers.values()) + if (regex.indexIn(name) > -1) + completionMap[name.toLower()] = name; + + nextCompletion = completionMap.begin(); + lastCompletionLength = tabAbbrev.length(); +} + + +void TabCompleter::complete() +{ + if (!enabled) { + buildCompletionList(); + enabled = true; + } + + if (nextCompletion != completionMap.end()) { + // clear previous completion + for (int i = 0; i < lastCompletionLength; i++) { + msgEdit->keyPressEvent(new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier)); + } + + // insert completion + msgEdit->insertPlainText(*nextCompletion); + + // remember charcount to delete next time and advance to next completion + lastCompletionLength = nextCompletion->length(); + nextCompletion++; + + // we're completing the first word of the line + if (msgEdit->textCursor().position() == lastCompletionLength) { + msgEdit->insertPlainText(nickSuffix); + lastCompletionLength += nickSuffix.length(); + } + } + else { // we're at the end of the list -> start over again + if (!completionMap.isEmpty()) { + nextCompletion = completionMap.begin(); + complete(); + } + } +} + + +void TabCompleter::reset() +{ + enabled = false; +} + +// this determines the sort order +bool TabCompleter::SortableString::operator<(const SortableString &other) const +{ + QString name = Core::getInstance()->getUsername(); + if (this->contents == name) + return false; + else if (other.contents == name) + return true; + +/* QDateTime thisTime = thisUser->lastChannelActivity(_currentBufferId); + QDateTime thatTime = thatUser->lastChannelActivity(_currentBufferId); + + + if (thisTime.isValid() || thatTime.isValid()) + return thisTime > thatTime; +*/ // this could be a useful feature at some point + + return QString::localeAwareCompare(this->contents, other.contents) < 0; +} diff --git a/src/widget/form/tabcompleter.h b/src/widget/form/tabcompleter.h new file mode 100644 index 000000000..f48b1a797 --- /dev/null +++ b/src/widget/form/tabcompleter.h @@ -0,0 +1,61 @@ +/* + Copyright (C) 2005-2014 by the Quassel Project and Project Tox + devel@quassel-irc.org and https://tox.im + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +/* This file was taken from the Quassel IRC client source (src/uisupport), and + was greatly simplified for use in qTox. */ + +#ifndef TABCOMPLETER_H +#define TABCOMPLETER_H + +#include +#include +#include // I'm really confused why I need this + +class ChatTextEdit; +class Group; + +class TabCompleter : public QObject +{ + Q_OBJECT +public: + explicit TabCompleter(ChatTextEdit* msgEdit, Group* group); + +public slots: + void complete(); + void reset(); + +private: + struct SortableString { + inline SortableString(const QString &n) { contents = n; } + bool operator<(const SortableString &other) const; + QString contents; + }; + + ChatTextEdit* msgEdit; + Group* group; + bool enabled; + const static QString nickSuffix; + + QMap completionMap; + QMap::Iterator nextCompletion; + int lastCompletionLength; + + void buildCompletionList(); +}; + + +#endif