mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-11-16 15:02:53 -05:00
feat: Add cancel function for progress indicator
This commit is contained in:
@ -60,6 +60,11 @@ void CompletionProgressHandler::hideProgress()
|
||||
Utils::ToolTip::hideImmediately();
|
||||
}
|
||||
|
||||
void CompletionProgressHandler::setCancelCallback(std::function<void()> callback)
|
||||
{
|
||||
m_cancelCallback = callback;
|
||||
}
|
||||
|
||||
void CompletionProgressHandler::identifyMatch(
|
||||
TextEditor::TextEditorWidget *editorWidget, int pos, ReportPriority report)
|
||||
{
|
||||
@ -83,6 +88,11 @@ void CompletionProgressHandler::operateTooltip(
|
||||
|
||||
m_progressWidget = new ProgressWidget(editorWidget);
|
||||
|
||||
// Set cancel callback for the widget
|
||||
if (m_cancelCallback) {
|
||||
m_progressWidget->setCancelCallback(m_cancelCallback);
|
||||
}
|
||||
|
||||
const QRect cursorRect = editorWidget->cursorRect(editorWidget->textCursor());
|
||||
QPoint globalPos = editorWidget->viewport()->mapToGlobal(cursorRect.topLeft());
|
||||
QPoint localPos = editorWidget->mapFromGlobal(globalPos);
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
|
||||
#include <texteditor/basehoverhandler.h>
|
||||
#include <QPointer>
|
||||
#include <functional>
|
||||
|
||||
namespace QodeAssist {
|
||||
|
||||
@ -31,6 +32,8 @@ class CompletionProgressHandler : public TextEditor::BaseHoverHandler
|
||||
public:
|
||||
void showProgress(TextEditor::TextEditorWidget *widget);
|
||||
void hideProgress();
|
||||
void setCancelCallback(std::function<void()> callback);
|
||||
bool isProgressVisible() const { return !m_progressWidget.isNull(); }
|
||||
|
||||
protected:
|
||||
void identifyMatch(
|
||||
@ -41,6 +44,7 @@ private:
|
||||
QPointer<TextEditor::TextEditorWidget> m_widget;
|
||||
QPointer<ProgressWidget> m_progressWidget;
|
||||
QPoint m_iconPosition;
|
||||
std::function<void()> m_cancelCallback;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright (C) 2025 Petr Mironychev
|
||||
*
|
||||
* This file is part of QodeAssist.
|
||||
@ -19,10 +19,15 @@
|
||||
|
||||
#include "ProgressWidget.hpp"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QMouseEvent>
|
||||
#include <QStyle>
|
||||
|
||||
namespace QodeAssist {
|
||||
|
||||
ProgressWidget::ProgressWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, m_isHovered(false)
|
||||
{
|
||||
m_dotPosition = 0;
|
||||
m_timer.setInterval(300);
|
||||
@ -64,6 +69,10 @@ ProgressWidget::ProgressWidget(QWidget *parent)
|
||||
}
|
||||
|
||||
setFixedSize(40, 40);
|
||||
setMouseTracking(true);
|
||||
|
||||
QIcon closeIcon = QApplication::style()->standardIcon(QStyle::SP_DockWidgetCloseButton);
|
||||
m_closePixmap = closeIcon.pixmap(16, 16);
|
||||
}
|
||||
|
||||
ProgressWidget::~ProgressWidget()
|
||||
@ -71,6 +80,11 @@ ProgressWidget::~ProgressWidget()
|
||||
m_timer.stop();
|
||||
}
|
||||
|
||||
void ProgressWidget::setCancelCallback(std::function<void()> callback)
|
||||
{
|
||||
m_cancelCallback = callback;
|
||||
}
|
||||
|
||||
void ProgressWidget::paintEvent(QPaintEvent *)
|
||||
{
|
||||
QPainter painter(this);
|
||||
@ -78,10 +92,21 @@ void ProgressWidget::paintEvent(QPaintEvent *)
|
||||
|
||||
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);
|
||||
if (m_isHovered) {
|
||||
if (!m_closePixmap.isNull()) {
|
||||
int x = ((width() - (m_closePixmap.width() / 2)) / 2);
|
||||
int y = ((height() - (m_closePixmap.height() / 2)) / 2);
|
||||
painter.drawPixmap(x, y, m_closePixmap);
|
||||
}
|
||||
} else {
|
||||
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;
|
||||
@ -111,4 +136,31 @@ void ProgressWidget::paintEvent(QPaintEvent *)
|
||||
}
|
||||
}
|
||||
|
||||
void ProgressWidget::enterEvent(QEnterEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
m_isHovered = true;
|
||||
setCursor(Qt::PointingHandCursor);
|
||||
update();
|
||||
}
|
||||
|
||||
void ProgressWidget::leaveEvent(QEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
m_isHovered = false;
|
||||
setCursor(Qt::ArrowCursor);
|
||||
update();
|
||||
}
|
||||
|
||||
void ProgressWidget::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton && m_isHovered) {
|
||||
emit cancelRequested();
|
||||
if (m_cancelCallback) {
|
||||
m_cancelCallback();
|
||||
}
|
||||
}
|
||||
QWidget::mousePressEvent(event);
|
||||
}
|
||||
|
||||
} // namespace QodeAssist
|
||||
|
||||
@ -22,6 +22,7 @@
|
||||
#include <QPainter>
|
||||
#include <QTimer>
|
||||
#include <QWidget>
|
||||
#include <functional>
|
||||
|
||||
#include <utils/progressindicator.h>
|
||||
#include <utils/theme/theme.h>
|
||||
@ -30,12 +31,21 @@ namespace QodeAssist {
|
||||
|
||||
class ProgressWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ProgressWidget(QWidget *parent = nullptr);
|
||||
~ProgressWidget();
|
||||
|
||||
void setCancelCallback(std::function<void()> callback);
|
||||
|
||||
signals:
|
||||
void cancelRequested();
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *) override;
|
||||
void enterEvent(QEnterEvent *event) override;
|
||||
void leaveEvent(QEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
|
||||
private:
|
||||
QTimer m_timer;
|
||||
@ -43,6 +53,9 @@ private:
|
||||
QColor m_textColor;
|
||||
QColor m_backgroundColor;
|
||||
QPixmap m_logoPixmap;
|
||||
QPixmap m_closePixmap;
|
||||
bool m_isHovered;
|
||||
std::function<void()> m_cancelCallback;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist
|
||||
|
||||
Reference in New Issue
Block a user