From c704b4f65cf3c2b14d2779989840cba74f376657 Mon Sep 17 00:00:00 2001 From: malte_langkabel Date: Fri, 23 Oct 2015 13:25:15 +0200 Subject: [PATCH] ui: IDE communication integration * handled communication from VS to Coati and vice versa. * one bug still remains: Coati regards a tab as a single character while VS regards it as multiple characters. This causes offsets in communicated positions. --- src/app/qt/element/QtCodeArea.cpp | 37 ++++++++++++++++-- src/app/qt/element/QtCodeArea.h | 1 + .../network/QtIDECommunicationController.cpp | 5 ++- .../qt/network/QtIDECommunicationController.h | 4 +- src/app/qt/network/QtNetworkFactory.cpp | 4 +- src/app/qt/network/QtNetworkFactory.h | 2 +- src/app/qt/network/QtTcpWrapper.cpp | 19 +--------- src/app/qt/network/QtTcpWrapper.h | 3 +- src/lib/Application.cpp | 2 +- .../controller/FeatureController.cpp | 6 +-- .../component/controller/FeatureController.h | 6 +-- .../controller/IDECommunicationController.cpp | 38 ++++++++++++++++--- .../controller/IDECommunicationController.h | 8 +++- src/lib/component/controller/NetworkFactory.h | 3 +- .../helper/NetworkProtocolHelper.cpp | 26 ++++++------- 15 files changed, 104 insertions(+), 60 deletions(-) diff --git a/src/app/qt/element/QtCodeArea.cpp b/src/app/qt/element/QtCodeArea.cpp index 042f0845..d07be039 100644 --- a/src/app/qt/element/QtCodeArea.cpp +++ b/src/app/qt/element/QtCodeArea.cpp @@ -1,5 +1,6 @@ #include "qt/element/QtCodeArea.h" +#include #include #include #include @@ -11,6 +12,7 @@ #include "utility/messaging/type/MessageShowFile.h" #include "utility/messaging/type/MessageFocusIn.h" #include "utility/messaging/type/MessageFocusOut.h" +#include "utility/messaging/type/MessageMoveIDECursor.h" #include "utility/utility.h" #include "data/location/TokenLocation.h" @@ -249,12 +251,20 @@ void QtCodeArea::mouseReleaseEvent(QMouseEvent* event) if (event->button() == Qt::LeftButton) { m_panningValue = -1; - QTextCursor cursor = this->cursorForPosition(event->pos()); - std::vector locationIds = findLocationIdsForPosition(cursor.position()); - if (locationIds.size()) + if (Qt::KeyboardModifier::ControlModifier && QApplication::keyboardModifiers()) { - MessageActivateTokenLocations(locationIds).dispatch(); + std::pair lineColumn = toLineColumn(this->cursorForPosition(event->pos()).position()); + MessageMoveIDECursor(m_locationFile->getFilePath().str(), lineColumn.first, lineColumn.second).dispatch(); + } + else + { + QTextCursor cursor = this->cursorForPosition(event->pos()); + std::vector locationIds = findLocationIdsForPosition(cursor.position()); + if (locationIds.size()) + { + MessageActivateTokenLocations(locationIds).dispatch(); + } } } } @@ -566,6 +576,25 @@ int QtCodeArea::toTextEditPosition(int lineNumber, int columnNumber) const return position; } +std::pair QtCodeArea::toLineColumn(int textEditPosition) const +{ + int lineNumber = m_startLineNumber; + for (int i = 0; i < document()->lineCount(); i++) + { + int nextTextEditPosition = textEditPosition - document()->findBlockByLineNumber(i).length(); + if (nextTextEditPosition >= 0) + { + textEditPosition = nextTextEditPosition; + lineNumber++; + } + else + { + break; + } + } + return std::make_pair(lineNumber, textEditPosition); +} + int QtCodeArea::startTextEditPosition() const { return 0; diff --git a/src/app/qt/element/QtCodeArea.h b/src/app/qt/element/QtCodeArea.h index e4e9670f..ce847517 100644 --- a/src/app/qt/element/QtCodeArea.h +++ b/src/app/qt/element/QtCodeArea.h @@ -114,6 +114,7 @@ private: bool locationBelongsToSnippet(TokenLocation* location) const; int toTextEditPosition(int lineNumber, int columnNumber) const; + std::pair toLineColumn(int textEditPosition) const; int startTextEditPosition() const; int endTextEditPosition() const; diff --git a/src/app/qt/network/QtIDECommunicationController.cpp b/src/app/qt/network/QtIDECommunicationController.cpp index 978fca28..82d1be53 100644 --- a/src/app/qt/network/QtIDECommunicationController.cpp +++ b/src/app/qt/network/QtIDECommunicationController.cpp @@ -2,8 +2,9 @@ #include -QtIDECommunicationController::QtIDECommunicationController(QObject* parent) - : m_tcpWrapper(parent) +QtIDECommunicationController::QtIDECommunicationController(QObject* parent, StorageAccess* storageAccess) + : IDECommunicationController(storageAccess) + , m_tcpWrapper(parent) { m_tcpWrapper.setReadCallback(std::bind(&QtIDECommunicationController::handleIncomingMessage, this, std::placeholders::_1)); } diff --git a/src/app/qt/network/QtIDECommunicationController.h b/src/app/qt/network/QtIDECommunicationController.h index c45c33af..93a030a5 100644 --- a/src/app/qt/network/QtIDECommunicationController.h +++ b/src/app/qt/network/QtIDECommunicationController.h @@ -7,11 +7,13 @@ #include "component/controller/IDECommunicationController.h" +class StorageAccess; + class QtIDECommunicationController : public IDECommunicationController { public: - QtIDECommunicationController(QObject* parent); + QtIDECommunicationController(QObject* parent, StorageAccess* storageAccess); ~QtIDECommunicationController(); private: diff --git a/src/app/qt/network/QtNetworkFactory.cpp b/src/app/qt/network/QtNetworkFactory.cpp index 7d3eb4ae..91944892 100644 --- a/src/app/qt/network/QtNetworkFactory.cpp +++ b/src/app/qt/network/QtNetworkFactory.cpp @@ -10,7 +10,7 @@ QtNetworkFactory::~QtNetworkFactory() { } -std::shared_ptr QtNetworkFactory::createIDECommunicationController() const +std::shared_ptr QtNetworkFactory::createIDECommunicationController(StorageAccess* storageAccess) const { - return std::make_shared(nullptr); + return std::make_shared(nullptr, storageAccess); } \ No newline at end of file diff --git a/src/app/qt/network/QtNetworkFactory.h b/src/app/qt/network/QtNetworkFactory.h index 4361d73c..6c8b37cf 100644 --- a/src/app/qt/network/QtNetworkFactory.h +++ b/src/app/qt/network/QtNetworkFactory.h @@ -9,7 +9,7 @@ public: QtNetworkFactory(); virtual ~QtNetworkFactory(); - virtual std::shared_ptr createIDECommunicationController() const; + virtual std::shared_ptr createIDECommunicationController(StorageAccess* storageAccess) const; }; #endif // QT_NETWORK_FACTORY_H \ No newline at end of file diff --git a/src/app/qt/network/QtTcpWrapper.cpp b/src/app/qt/network/QtTcpWrapper.cpp index 474c4978..a173c1a1 100644 --- a/src/app/qt/network/QtTcpWrapper.cpp +++ b/src/app/qt/network/QtTcpWrapper.cpp @@ -2,23 +2,6 @@ #include "utility/logging/logging.h" -QtTcpWrapper::QtTcpWrapper(QObject* parent) - : QObject(parent) - , m_port(6667) - , m_ip("127.0.0.1") -{ - QHostAddress address(m_ip.c_str()); - - m_tcpServer = new QTcpServer(this); - - connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection())); - - if (!m_tcpServer->listen(QHostAddress::LocalHost, m_port)) - { - LOG_ERROR_STREAM(<< "TCP server failed to start. Unable to listen for IDE plugin messages."); - } -} - QtTcpWrapper::QtTcpWrapper(QObject* parent, const std::string& ip, const quint16 port) : QObject(parent) , m_port(port) @@ -32,7 +15,7 @@ QtTcpWrapper::QtTcpWrapper(QObject* parent, const std::string& ip, const quint16 if (!m_tcpServer->listen(QHostAddress::LocalHost, m_port)) { - LOG_ERROR_STREAM(<< "TCP server failed to start. Unable to listen for IDE plugin messages."); + LOG_ERROR_STREAM(<< "TCP server failed to start with error: \"" + m_tcpServer->errorString().toStdString() + "\". Unable to listen for IDE plugin messages."); } } diff --git a/src/app/qt/network/QtTcpWrapper.h b/src/app/qt/network/QtTcpWrapper.h index 87c4134f..55d70ce6 100644 --- a/src/app/qt/network/QtTcpWrapper.h +++ b/src/app/qt/network/QtTcpWrapper.h @@ -13,8 +13,7 @@ class QtTcpWrapper : public QObject Q_OBJECT public: - QtTcpWrapper(QObject* parent); - QtTcpWrapper(QObject* parent, const std::string& ip, const quint16 port); + QtTcpWrapper(QObject* parent, const std::string& ip = "127.0.0.1", const quint16 port = 6667); ~QtTcpWrapper(); void sendMessage(const std::string& message) const; diff --git a/src/lib/Application.cpp b/src/lib/Application.cpp index f62a88ce..7b0cd368 100644 --- a/src/lib/Application.cpp +++ b/src/lib/Application.cpp @@ -39,7 +39,7 @@ std::shared_ptr Application::create(ViewFactory* viewFactory, Netwo ptr->m_mainView->hideStartScreen(); } - ptr->m_ideCommunicationController = networkFactory->createIDECommunicationController(); + ptr->m_ideCommunicationController = networkFactory->createIDECommunicationController(ptr->m_storageCache.get()); return ptr; } diff --git a/src/lib/component/controller/FeatureController.cpp b/src/lib/component/controller/FeatureController.cpp index 9454d9fe..1f13f12b 100644 --- a/src/lib/component/controller/FeatureController.cpp +++ b/src/lib/component/controller/FeatureController.cpp @@ -1,11 +1,11 @@ #include "component/controller/FeatureController.h" -#include "utility/messaging/type/MessageActivateTokens.h" -#include "utility/messaging/type/MessageRefresh.h" - #include "data/access/StorageAccess.h" #include "settings/ApplicationSettings.h" +#include "utility/messaging/type/MessageActivateTokens.h" +#include "utility/messaging/type/MessageRefresh.h" + FeatureController::FeatureController(StorageAccess* storageAccess) : m_storageAccess(storageAccess) , m_activationTranslator(storageAccess) diff --git a/src/lib/component/controller/FeatureController.h b/src/lib/component/controller/FeatureController.h index 30ca8d27..40275254 100644 --- a/src/lib/component/controller/FeatureController.h +++ b/src/lib/component/controller/FeatureController.h @@ -3,6 +3,9 @@ #include +#include "component/controller/helper/ActivationTranslator.h" +#include "component/controller/Controller.h" + #include "utility/messaging/MessageListener.h" #include "utility/messaging/type/MessageActivateEdge.h" #include "utility/messaging/type/MessageActivateFile.h" @@ -12,9 +15,6 @@ #include "utility/messaging/type/MessageSwitchColorScheme.h" #include "utility/messaging/type/MessageZoom.h" -#include "component/controller/Controller.h" -#include "component/controller/helper/ActivationTranslator.h" - class StorageAccess; class FeatureController diff --git a/src/lib/component/controller/IDECommunicationController.cpp b/src/lib/component/controller/IDECommunicationController.cpp index cf98eba5..1c29b923 100644 --- a/src/lib/component/controller/IDECommunicationController.cpp +++ b/src/lib/component/controller/IDECommunicationController.cpp @@ -1,11 +1,15 @@ #include "IDECommunicationController.h" -#include "utility/messaging/type/MessageActivateTokens.h" +#include "component/controller/helper/NetworkProtocolHelper.h" +#include "data/access/StorageAccess.h" +#include "data/location/TokenLocationFile.h" +#include "data/location/TokenLocation.h" + +#include "utility/messaging/type/MessageActivateTokenLocations.h" #include "utility/logging/logging.h" -#include "component/controller/helper/NetworkProtocolHelper.h" - -IDECommunicationController::IDECommunicationController() +IDECommunicationController::IDECommunicationController(StorageAccess* storageAccess) + : m_storageAccess(storageAccess) { } @@ -21,7 +25,31 @@ void IDECommunicationController::handleIncomingMessage(const std::string& messag if (parsedMessage.valid) { - // TODO: set active token + const unsigned int cursorColumn = parsedMessage.column; + + std::shared_ptr tokenLocationFile = m_storageAccess->getTokenLocationsForLinesInFile( + parsedMessage.fileLocation, parsedMessage.row, parsedMessage.row + ); + + std::vector selectedLocationIds; + tokenLocationFile->forEachStartTokenLocation( + [&](TokenLocation* startLocation) + { + if (startLocation->getColumnNumber() < cursorColumn) + { + TokenLocation* endLocation = startLocation->getOtherTokenLocation(); + if (endLocation && endLocation->getColumnNumber() > cursorColumn) + { + selectedLocationIds.push_back(startLocation->getId()); + } + } + } + ); + + if (selectedLocationIds.size() > 0) + { + MessageActivateTokenLocations(selectedLocationIds).dispatch(); + } } } diff --git a/src/lib/component/controller/IDECommunicationController.h b/src/lib/component/controller/IDECommunicationController.h index 864d1eca..10fde95d 100644 --- a/src/lib/component/controller/IDECommunicationController.h +++ b/src/lib/component/controller/IDECommunicationController.h @@ -3,17 +3,19 @@ #include +#include "component/controller/Controller.h" + #include "utility/messaging/MessageListener.h" #include "utility/messaging/type/MessageMoveIDECursor.h" -#include "component/controller/Controller.h" +class StorageAccess; class IDECommunicationController : public Controller , public MessageListener { public: - IDECommunicationController(); + IDECommunicationController(StorageAccess* storageAccess); virtual ~IDECommunicationController(); void handleIncomingMessage(const std::string& message); @@ -21,6 +23,8 @@ public: private: virtual void handleMessage(MessageMoveIDECursor* message); virtual void sendMessage(const std::string& message) const = 0; + + StorageAccess* m_storageAccess; }; #endif // IDE_COMMUNICATION_CONTROLLER_H \ No newline at end of file diff --git a/src/lib/component/controller/NetworkFactory.h b/src/lib/component/controller/NetworkFactory.h index 9e8f0e0f..d2e6469a 100644 --- a/src/lib/component/controller/NetworkFactory.h +++ b/src/lib/component/controller/NetworkFactory.h @@ -4,6 +4,7 @@ #include class IDECommunicationController; +class StorageAccess; class NetworkFactory { @@ -11,7 +12,7 @@ public: NetworkFactory(); virtual ~NetworkFactory(); - virtual std::shared_ptr createIDECommunicationController() const = 0; + virtual std::shared_ptr createIDECommunicationController(StorageAccess* storageAccess) const = 0; }; #endif // NETWORK_FACTORY_H \ No newline at end of file diff --git a/src/lib/component/controller/helper/NetworkProtocolHelper.cpp b/src/lib/component/controller/helper/NetworkProtocolHelper.cpp index 2db6df4e..a583730b 100644 --- a/src/lib/component/controller/helper/NetworkProtocolHelper.cpp +++ b/src/lib/component/controller/helper/NetworkProtocolHelper.cpp @@ -12,29 +12,25 @@ NetworkProtocolHelper::NetworkMessage NetworkProtocolHelper::parseMessage(const { NetworkMessage networkMessage; - std::string tmpMessage = removeEndOfMessageToken(message); - - size_t pos = tmpMessage.find(m_setActiveTokenPrefix); + size_t pos = message.find(m_setActiveTokenPrefix); if(pos != std::string::npos) { - tmpMessage = ""; + std::string tmpMessage = ""; getSubstringAfterString(message, m_divider, tmpMessage); // just get rid of message type + tmpMessage = removeEndOfMessageToken(tmpMessage); std::string fileLocation = getSubstringAfterString(tmpMessage, m_divider, tmpMessage); std::string row = getSubstringAfterString(tmpMessage, m_divider, tmpMessage); - std::string column = tmpMessage; // getSubstringAfterString(tmpMessage, "", tmpMessage); // there should be nothing left in the message but the column number + std::string column = tmpMessage; // there should be nothing left in the message but the column number - if(fileLocation.length() <= 0 - || row.length() <= 0 - || column.length() <= 0 - || isDigits(row) == false - || isDigits(column) == false) - { - return networkMessage; - } - else - { + if( + fileLocation.length() > 0 + && row.length() > 0 + && column.length() > 0 + && isDigits(row) + && isDigits(column) + ){ networkMessage.fileLocation = fileLocation; networkMessage.row = std::stoi(row); networkMessage.column = std::stoi(column);