diff --git a/CMakeLists.txt b/CMakeLists.txt index 57cb91a..3947fe8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,6 +101,8 @@ add_qtc_plugin(QodeAssist ConfigurationManager.hpp ConfigurationManager.cpp CodeHandler.hpp CodeHandler.cpp UpdateStatusWidget.hpp UpdateStatusWidget.cpp + widgets/CompletionProgressHandler.hpp widgets/CompletionProgressHandler.cpp + widgets/ProgressWidget.hpp widgets/ProgressWidget.cpp ) get_target_property(QtCreatorCorePath QtCreator::Core LOCATION) diff --git a/QodeAssistClient.cpp b/QodeAssistClient.cpp index c3446c0..c5389b8 100644 --- a/QodeAssistClient.cpp +++ b/QodeAssistClient.cpp @@ -151,6 +151,9 @@ void QodeAssistClient::requestCompletions(TextEditor::TextEditorWidget *editor) {TextDocumentIdentifier(hostPathToServerUri(filePath)), documentVersion(filePath), Position(cursor.mainCursor())}}; + if (Settings::codeCompletionSettings().showProgressWidget()) { + m_progressHandler.showProgress(editor); + } request.setResponseCallback([this, editor = QPointer(editor)]( const GetCompletionRequest::Response &response) { QTC_ASSERT(editor, return); @@ -237,6 +240,7 @@ void QodeAssistClient::handleCompletions( Text::Position pos{toTextPos(c.position())}; return TextSuggestion::Data{range, pos, c.text()}; }); + m_progressHandler.hideProgress(); if (completions.isEmpty()) return; editor->insertSuggestion(std::make_unique(suggestions, editor->document())); @@ -248,6 +252,7 @@ void QodeAssistClient::cancelRunningRequest(TextEditor::TextEditorWidget *editor const auto it = m_runningRequests.constFind(editor); if (it == m_runningRequests.constEnd()) return; + m_progressHandler.hideProgress(); cancelRequest(it->id()); m_runningRequests.erase(it); } diff --git a/QodeAssistClient.hpp b/QodeAssistClient.hpp index 0e8260a..effdd80 100644 --- a/QodeAssistClient.hpp +++ b/QodeAssistClient.hpp @@ -26,6 +26,7 @@ #include "LLMClientInterface.hpp" #include "LSPCompletion.hpp" +#include "widgets/CompletionProgressHandler.hpp" #include #include #include @@ -60,6 +61,7 @@ private: QElapsedTimer m_typingTimer; int m_recentCharCount; + CompletionProgressHandler m_progressHandler; }; } // namespace QodeAssist diff --git a/settings/CodeCompletionSettings.cpp b/settings/CodeCompletionSettings.cpp index bff5055..1afc9b8 100644 --- a/settings/CodeCompletionSettings.cpp +++ b/settings/CodeCompletionSettings.cpp @@ -202,6 +202,10 @@ CodeCompletionSettings::CodeCompletionSettings() "(space-separated), file extensions (space-separated)")); customLanguages.setDefaultValue({{"cmake,#,cmake,CMakeLists.txt"}, {"qmake,#,qmake,pro pri"}}); + showProgressWidget.setSettingsKey(Constants::CC_SHOW_PROGRESS_WIDGET); + showProgressWidget.setLabelText(Tr::tr("Show progress indicator during code completion")); + showProgressWidget.setDefaultValue(true); + useProjectChangesCache.setSettingsKey(Constants::CC_USE_PROJECT_CHANGES_CACHE); useProjectChangesCache.setDefaultValue(true); useProjectChangesCache.setLabelText(Tr::tr("Max Changes Cache Size:")); @@ -281,7 +285,8 @@ CodeCompletionSettings::CodeCompletionSettings() Row{autoCompletionCharThreshold, autoCompletionTypingInterval, startSuggestionTimer, - Stretch{1}}}}, + Stretch{1}}, + showProgressWidget}}, Space{8}, Group{ title(Tr::tr("General Parameters")), diff --git a/settings/CodeCompletionSettings.hpp b/settings/CodeCompletionSettings.hpp index d3211c6..7aaa1dd 100644 --- a/settings/CodeCompletionSettings.hpp +++ b/settings/CodeCompletionSettings.hpp @@ -44,6 +44,8 @@ public: Utils::StringListAspect customLanguages{this}; + Utils::BoolAspect showProgressWidget{this}; + // General Parameters Settings Utils::DoubleAspect temperature{this}; Utils::IntegerAspect maxTokens{this}; diff --git a/settings/SettingsConstants.hpp b/settings/SettingsConstants.hpp index 880dcf6..226c20c 100644 --- a/settings/SettingsConstants.hpp +++ b/settings/SettingsConstants.hpp @@ -55,6 +55,7 @@ const char CC_PRESET1_URL_HISTORY[] = "QodeAssist.ccPreset1UrlHistory"; // settings const char ENABLE_QODE_ASSIST[] = "QodeAssist.enableQodeAssist"; const char CC_AUTO_COMPLETION[] = "QodeAssist.ccAutoCompletion"; +const char CC_SHOW_PROGRESS_WIDGET[] = "QodeAssist.ccShowProgressWidget"; const char ENABLE_LOGGING[] = "QodeAssist.enableLogging"; const char ENABLE_CHECK_UPDATE[] = "QodeAssist.enableCheckUpdate"; const char ENABLE_CHAT[] = "QodeAssist.enableChat"; diff --git a/widgets/CompletionProgressHandler.cpp b/widgets/CompletionProgressHandler.cpp new file mode 100644 index 0000000..1fd3046 --- /dev/null +++ b/widgets/CompletionProgressHandler.cpp @@ -0,0 +1,86 @@ +/* + * 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 "CompletionProgressHandler.hpp" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "ProgressWidget.hpp" + +namespace QodeAssist { + +void CompletionProgressHandler::showProgress(TextEditor::TextEditorWidget *widget) +{ + m_widget = widget; + m_isActive = true; + + if (m_widget) { + const QRect cursorRect = m_widget->cursorRect(m_widget->textCursor()); + m_iconPosition = m_widget->viewport()->mapToGlobal(cursorRect.topLeft()) + - Utils::ToolTip::offsetFromPosition(); + + identifyMatch(m_widget, m_widget->textCursor().position(), [this](auto priority) { + if (priority != Priority_None) + operateTooltip(m_widget, m_iconPosition); + }); + } +} + +void CompletionProgressHandler::hideProgress() +{ + m_isActive = false; + Utils::ToolTip::hide(); +} + +void CompletionProgressHandler::identifyMatch( + TextEditor::TextEditorWidget *editorWidget, int pos, ReportPriority report) +{ + if (!m_isActive || !editorWidget) { + report(Priority_None); + return; + } + + report(Priority_Tooltip); +} + +void CompletionProgressHandler::operateTooltip( + TextEditor::TextEditorWidget *editorWidget, const QPoint &point) +{ + if (!m_isActive || !editorWidget) + return; + + auto progressWidget = new ProgressWidget(editorWidget); + + QPoint showPoint = point; + showPoint.ry() -= progressWidget->height(); + + Utils::ToolTip::show(showPoint, progressWidget, editorWidget); +} + +} // namespace QodeAssist diff --git a/widgets/CompletionProgressHandler.hpp b/widgets/CompletionProgressHandler.hpp new file mode 100644 index 0000000..2d012e4 --- /dev/null +++ b/widgets/CompletionProgressHandler.hpp @@ -0,0 +1,44 @@ +/* + * 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 + +namespace QodeAssist { + +class CompletionProgressHandler : public TextEditor::BaseHoverHandler +{ +public: + void showProgress(TextEditor::TextEditorWidget *widget); + void hideProgress(); + +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_iconPosition; + bool m_isActive = false; +}; + +} // namespace QodeAssist diff --git a/widgets/ProgressWidget.cpp b/widgets/ProgressWidget.cpp new file mode 100644 index 0000000..2e4b207 --- /dev/null +++ b/widgets/ProgressWidget.cpp @@ -0,0 +1,114 @@ +/* + * 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 "ProgressWidget.hpp" + +namespace QodeAssist { + +ProgressWidget::ProgressWidget(QWidget *parent) + : QWidget(parent) +{ + m_dotPosition = 0; + m_timer.setInterval(300); + connect(&m_timer, &QTimer::timeout, this, [this]() { + m_dotPosition = (m_dotPosition + 1) % 4; + update(); + }); + m_timer.start(); + + 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); +} + +ProgressWidget::~ProgressWidget() +{ + m_timer.stop(); +} + +void ProgressWidget::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + + painter.fillRect(rect(), m_backgroundColor); + + if (!m_logoPixmap.isNull()) { + QRect logoRect( + (width() - m_logoPixmap.width()) / 2, 5, m_logoPixmap.width(), m_logoPixmap.height()); + painter.drawPixmap(logoRect, m_logoPixmap); + } + + int dotSpacing = 6; + int dotSize = 4; + int totalDotWidth = 3 * dotSize + 2 * dotSpacing; + int startX = (width() - totalDotWidth) / 2; + int dotY = height() - 8; + + for (int i = 0; i < 3; ++i) { + QColor dotColor = m_textColor; + + if (m_dotPosition == 0) { + dotColor.setAlpha(128); + } else { + if (i == m_dotPosition - 1) { + dotColor.setAlpha(255); + } else { + dotColor.setAlpha(80); + } + } + + painter.setPen(Qt::NoPen); + painter.setBrush(dotColor); + + int x = startX + i * (dotSize + dotSpacing); + painter.drawEllipse(x, dotY, dotSize, dotSize); + } +} + +} // namespace QodeAssist diff --git a/widgets/ProgressWidget.hpp b/widgets/ProgressWidget.hpp new file mode 100644 index 0000000..27f0b13 --- /dev/null +++ b/widgets/ProgressWidget.hpp @@ -0,0 +1,48 @@ +/* + * 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 + +#include +#include + +namespace QodeAssist { + +class ProgressWidget : public QWidget +{ +public: + ProgressWidget(QWidget *parent = nullptr); + ~ProgressWidget(); + +protected: + void paintEvent(QPaintEvent *) override; + +private: + QTimer m_timer; + int m_dotPosition; + QColor m_textColor; + QColor m_backgroundColor; + QPixmap m_logoPixmap; +}; + +} // namespace QodeAssist