From 418578743a82897c66d3fd13783c11803b73e18e Mon Sep 17 00:00:00 2001 From: Petr Mironychev <9195189+Palm1r@users.noreply.github.com> Date: Wed, 9 Apr 2025 18:27:25 +0200 Subject: [PATCH] feat: Prepare widget for chat --- CMakeLists.txt | 2 + QodeAssistClient.cpp | 7 ++ QodeAssistClient.hpp | 2 + widgets/EditorChatButton.cpp | 140 ++++++++++++++++++++++++++++ widgets/EditorChatButton.hpp | 53 +++++++++++ widgets/EditorChatButtonHandler.cpp | 89 ++++++++++++++++++ widgets/EditorChatButtonHandler.hpp | 52 +++++++++++ 7 files changed, 345 insertions(+) create mode 100644 widgets/EditorChatButton.cpp create mode 100644 widgets/EditorChatButton.hpp create mode 100644 widgets/EditorChatButtonHandler.cpp create mode 100644 widgets/EditorChatButtonHandler.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3947fe8..8dddaad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,6 +103,8 @@ add_qtc_plugin(QodeAssist UpdateStatusWidget.hpp UpdateStatusWidget.cpp widgets/CompletionProgressHandler.hpp widgets/CompletionProgressHandler.cpp widgets/ProgressWidget.hpp widgets/ProgressWidget.cpp + widgets/EditorChatButton.hpp widgets/EditorChatButton.cpp + widgets/EditorChatButtonHandler.hpp widgets/EditorChatButtonHandler.cpp ) get_target_property(QtCreatorCorePath QtCreator::Core LOCATION) diff --git a/QodeAssistClient.cpp b/QodeAssistClient.cpp index 74c81c8..6d5f3ed 100644 --- a/QodeAssistClient.cpp +++ b/QodeAssistClient.cpp @@ -128,6 +128,13 @@ void QodeAssistClient::openDocument(TextEditor::TextDocument *document) scheduleRequest(widget); } }); + + // auto editors = BaseTextEditor::textEditorsForDocument(document); + // connect( + // editors.first()->editorWidget(), + // &TextEditorWidget::selectionChanged, + // this, + // [this, editors]() { m_chatButtonHandler.showButton(editors.first()->editorWidget()); }); } bool QodeAssistClient::canOpenProject(ProjectExplorer::Project *project) diff --git a/QodeAssistClient.hpp b/QodeAssistClient.hpp index 9f0bd6e..0bf8a39 100644 --- a/QodeAssistClient.hpp +++ b/QodeAssistClient.hpp @@ -27,6 +27,7 @@ #include "LLMClientInterface.hpp" #include "LSPCompletion.hpp" #include "widgets/CompletionProgressHandler.hpp" +#include "widgets/EditorChatButtonHandler.hpp" #include #include #include @@ -62,6 +63,7 @@ private: QElapsedTimer m_typingTimer; int m_recentCharCount; CompletionProgressHandler m_progressHandler; + EditorChatButtonHandler m_chatButtonHandler; }; } // namespace QodeAssist diff --git a/widgets/EditorChatButton.cpp b/widgets/EditorChatButton.cpp new file mode 100644 index 0000000..9603a49 --- /dev/null +++ b/widgets/EditorChatButton.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2025 Petr Mironychev + * + * This file is part of QodeAssist. + * + * QodeAssist is free 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. + * + * QodeAssist 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 + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QodeAssist. If not, see . + */ + +#include "EditorChatButton.hpp" + +#include +#include +#include + +namespace QodeAssist { + +EditorChatButton::EditorChatButton(QWidget *parent) + : QWidget(parent) +{ + m_textColor = Utils::creatorTheme()->color(Utils::Theme::TextColorNormal); + m_backgroundColor = Utils::creatorTheme()->color(Utils::Theme::BackgroundColorNormal); + + m_logoPixmap = QPixmap(":/resources/images/qoderassist-icon.png"); + + if (!m_logoPixmap.isNull()) { + QImage image = m_logoPixmap.toImage(); + image = image.convertToFormat(QImage::Format_ARGB32); + + for (int y = 0; y < image.height(); ++y) { + for (int x = 0; x < image.width(); ++x) { + QColor pixelColor = QColor::fromRgba(image.pixel(x, y)); + + int brightness = (pixelColor.red() + pixelColor.green() + pixelColor.blue()) / 3; + + if (brightness > 200) { + pixelColor.setAlpha(0); + image.setPixelColor(x, y, pixelColor); + } else if (pixelColor.alpha() > 0) { + int alpha = pixelColor.alpha(); + pixelColor = m_textColor; + pixelColor.setAlpha(alpha); + image.setPixelColor(x, y, pixelColor); + } + } + } + + m_logoPixmap = QPixmap::fromImage(image); + m_logoPixmap = m_logoPixmap.scaled(24, 24, Qt::KeepAspectRatio, Qt::SmoothTransformation); + } + + setFixedSize(40, 40); + setCursor(Qt::PointingHandCursor); + setToolTip(tr("Open QodeAssist Chat")); +} + +EditorChatButton::~EditorChatButton() = default; + +void EditorChatButton::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + + QColor bgColor = m_backgroundColor; + if (m_isPressed) { + bgColor = bgColor.darker(120); + } else if (m_isHovered) { + bgColor = bgColor.lighter(110); + } + painter.fillRect(rect(), bgColor); + + QRect buttonRect = rect().adjusted(4, 4, -4, -4); + painter.setPen(Qt::NoPen); + QColor buttonBgColor + = m_isPressed ? Utils::creatorTheme()->color(Utils::Theme::BackgroundColorHover).darker(110) + : Utils::creatorTheme()->color(Utils::Theme::BackgroundColorHover); + + if (m_isHovered) { + buttonBgColor = buttonBgColor.lighter(110); + } + painter.setBrush(buttonBgColor); + painter.drawEllipse(buttonRect); + + if (!m_logoPixmap.isNull()) { + QRect logoRect( + (width() - m_logoPixmap.width()) / 2, + (height() - m_logoPixmap.height()) / 2, + m_logoPixmap.width(), + m_logoPixmap.height()); + painter.drawPixmap(logoRect, m_logoPixmap); + } +} + +void EditorChatButton::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + m_isPressed = true; + update(); + } + QWidget::mousePressEvent(event); +} + +void EditorChatButton::mouseReleaseEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton && m_isPressed) { + m_isPressed = false; + update(); + if (rect().contains(event->pos())) { + emit clicked(); + } + } + QWidget::mouseReleaseEvent(event); +} + +void EditorChatButton::enterEvent(QEnterEvent *event) +{ + m_isHovered = true; + update(); + QWidget::enterEvent(event); +} + +void EditorChatButton::leaveEvent(QEvent *event) +{ + m_isHovered = false; + m_isPressed = false; + update(); + QWidget::leaveEvent(event); +} + +} // namespace QodeAssist diff --git a/widgets/EditorChatButton.hpp b/widgets/EditorChatButton.hpp new file mode 100644 index 0000000..2bacc3c --- /dev/null +++ b/widgets/EditorChatButton.hpp @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2025 Petr Mironychev + * + * This file is part of QodeAssist. + * + * QodeAssist is free 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. + * + * QodeAssist 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 + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QodeAssist. If not, see . + */ + +#pragma once + +#include +#include +#include + +namespace QodeAssist { + +class EditorChatButton : public QWidget +{ + Q_OBJECT +public: + explicit EditorChatButton(QWidget *parent = nullptr); + ~EditorChatButton() override; + +signals: + void clicked(); + +protected: + void paintEvent(QPaintEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; + void enterEvent(QEnterEvent *event) override; + void leaveEvent(QEvent *event) override; + +private: + QPixmap m_logoPixmap; + QColor m_textColor; + QColor m_backgroundColor; + bool m_isPressed = false; + bool m_isHovered = false; +}; + +} // namespace QodeAssist diff --git a/widgets/EditorChatButtonHandler.cpp b/widgets/EditorChatButtonHandler.cpp new file mode 100644 index 0000000..9be8dfd --- /dev/null +++ b/widgets/EditorChatButtonHandler.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2025 Petr Mironychev + * + * This file is part of QodeAssist. + * + * QodeAssist is free 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. + * + * QodeAssist 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 + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QodeAssist. If not, see . + */ + +#include "EditorChatButtonHandler.hpp" +#include "EditorChatButton.hpp" + +#include +#include +#include + +namespace QodeAssist { + +EditorChatButtonHandler::~EditorChatButtonHandler() +{ + delete m_chatButton; +} + +void EditorChatButtonHandler::showButton(TextEditor::TextEditorWidget *widget) +{ + if (!widget) + return; + + m_widget = widget; + + identifyMatch(widget, widget->textCursor().position(), [this](auto priority) { + if (priority != Priority_None && m_widget) { + const QTextCursor cursor = m_widget->textCursor(); + const QRect selectionRect = m_widget->cursorRect(cursor); + m_cursorPosition = m_widget->viewport()->mapToGlobal(selectionRect.topLeft()) + - Utils::ToolTip::offsetFromPosition(); + operateTooltip(m_widget, m_cursorPosition); + } + }); +} + +void EditorChatButtonHandler::hideButton() +{ + Utils::ToolTip::hide(); +} + +void EditorChatButtonHandler::identifyMatch( + TextEditor::TextEditorWidget *editorWidget, int pos, ReportPriority report) +{ + if (!editorWidget) { + report(Priority_None); + return; + } + + report(Priority_Tooltip); +} + +void EditorChatButtonHandler::operateTooltip( + TextEditor::TextEditorWidget *editorWidget, const QPoint &point) +{ + if (!editorWidget) + return; + + if (!Utils::ToolTip::isVisible()) { + m_chatButton = new EditorChatButton(editorWidget); + m_buttonHeight = m_chatButton->height(); + + QPoint showPoint = point; + showPoint.ry() -= m_buttonHeight; + + Utils::ToolTip::show(showPoint, m_chatButton, editorWidget); + } else { + QPoint showPoint = point; + showPoint.ry() -= m_buttonHeight; + Utils::ToolTip::move(showPoint); + } +} + +} // namespace QodeAssist diff --git a/widgets/EditorChatButtonHandler.hpp b/widgets/EditorChatButtonHandler.hpp new file mode 100644 index 0000000..c2adb35 --- /dev/null +++ b/widgets/EditorChatButtonHandler.hpp @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2025 Petr Mironychev + * + * This file is part of QodeAssist. + * + * QodeAssist is free 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. + * + * QodeAssist 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 + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QodeAssist. If not, see . + */ + +#pragma once + +#include "widgets/EditorChatButton.hpp" +#include +#include + +namespace QodeAssist { + +class EditorChatButtonHandler : public TextEditor::BaseHoverHandler +{ +public: + explicit EditorChatButtonHandler() = default; + ~EditorChatButtonHandler() override; + + void showButton(TextEditor::TextEditorWidget *widget); + void hideButton(); + +signals: + void chatButtonClicked(TextEditor::TextEditorWidget *widget); + +protected: + void identifyMatch( + TextEditor::TextEditorWidget *editorWidget, int pos, ReportPriority report) override; + void operateTooltip(TextEditor::TextEditorWidget *editorWidget, const QPoint &point) override; + +private: + QPointer m_widget; + QPoint m_cursorPosition; + EditorChatButton *m_chatButton = nullptr; + int m_buttonHeight = 0; +}; + +} // namespace QodeAssist