mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-11-16 15:02:53 -05:00
refactor: Full rework quick refactor (#257)
This commit is contained in:
100
widgets/CompletionErrorHandler.cpp
Normal file
100
widgets/CompletionErrorHandler.cpp
Normal file
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "CompletionErrorHandler.hpp"
|
||||
|
||||
#include <texteditor/texteditor.h>
|
||||
#include <utils/tooltip/tooltip.h>
|
||||
|
||||
#include "ErrorWidget.hpp"
|
||||
|
||||
namespace QodeAssist {
|
||||
|
||||
void CompletionErrorHandler::showError(
|
||||
TextEditor::TextEditorWidget *widget,
|
||||
const QString &errorMessage,
|
||||
int autoHideMs)
|
||||
{
|
||||
m_widget = widget;
|
||||
m_errorMessage = errorMessage;
|
||||
|
||||
if (m_widget) {
|
||||
const QRect cursorRect = m_widget->cursorRect(m_widget->textCursor());
|
||||
m_errorPosition = m_widget->viewport()->mapToGlobal(cursorRect.topLeft())
|
||||
- Utils::ToolTip::offsetFromPosition();
|
||||
|
||||
identifyMatch(m_widget, m_widget->textCursor().position(), [this, autoHideMs](auto priority) {
|
||||
if (priority != Priority_None) {
|
||||
if (m_errorWidget) {
|
||||
m_errorWidget->deleteLater();
|
||||
}
|
||||
|
||||
m_errorWidget = new ErrorWidget(m_errorMessage, m_widget);
|
||||
|
||||
const QRect cursorRect = m_widget->cursorRect(m_widget->textCursor());
|
||||
QPoint globalPos = m_widget->viewport()->mapToGlobal(cursorRect.topLeft());
|
||||
QPoint localPos = m_widget->mapFromGlobal(globalPos);
|
||||
localPos.ry() -= m_errorWidget->height() + 5;
|
||||
|
||||
if (localPos.y() < 0) {
|
||||
localPos.ry() = cursorRect.bottom() + 5;
|
||||
}
|
||||
|
||||
m_errorWidget->move(localPos);
|
||||
m_errorWidget->show();
|
||||
m_errorWidget->raise();
|
||||
|
||||
QObject::connect(m_errorWidget, &ErrorWidget::dismissed, m_errorWidget, [this]() {
|
||||
hideError();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void CompletionErrorHandler::hideError()
|
||||
{
|
||||
if (m_errorWidget) {
|
||||
m_errorWidget->deleteLater();
|
||||
m_errorWidget = nullptr;
|
||||
}
|
||||
Utils::ToolTip::hideImmediately();
|
||||
m_errorMessage.clear();
|
||||
}
|
||||
|
||||
void CompletionErrorHandler::identifyMatch(
|
||||
TextEditor::TextEditorWidget *editorWidget, int pos, ReportPriority report)
|
||||
{
|
||||
if (!editorWidget || m_errorMessage.isEmpty()) {
|
||||
report(Priority_None);
|
||||
return;
|
||||
}
|
||||
|
||||
report(Priority_Tooltip);
|
||||
}
|
||||
|
||||
void CompletionErrorHandler::operateTooltip(
|
||||
TextEditor::TextEditorWidget *editorWidget, const QPoint &point)
|
||||
{
|
||||
Q_UNUSED(editorWidget)
|
||||
Q_UNUSED(point)
|
||||
}
|
||||
|
||||
} // namespace QodeAssist
|
||||
|
||||
54
widgets/CompletionErrorHandler.hpp
Normal file
54
widgets/CompletionErrorHandler.hpp
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <texteditor/basehoverhandler.h>
|
||||
#include <QPointer>
|
||||
|
||||
namespace QodeAssist {
|
||||
|
||||
class ErrorWidget;
|
||||
|
||||
class CompletionErrorHandler : public TextEditor::BaseHoverHandler
|
||||
{
|
||||
public:
|
||||
void showError(
|
||||
TextEditor::TextEditorWidget *widget,
|
||||
const QString &errorMessage,
|
||||
int autoHideMs = 5000);
|
||||
|
||||
void hideError();
|
||||
|
||||
bool isErrorVisible() const { return !m_errorWidget.isNull(); }
|
||||
|
||||
protected:
|
||||
void identifyMatch(
|
||||
TextEditor::TextEditorWidget *editorWidget, int pos, ReportPriority report) override;
|
||||
void operateTooltip(TextEditor::TextEditorWidget *editorWidget, const QPoint &point) override;
|
||||
|
||||
private:
|
||||
QPointer<TextEditor::TextEditorWidget> m_widget;
|
||||
QPointer<ErrorWidget> m_errorWidget;
|
||||
QString m_errorMessage;
|
||||
QPoint m_errorPosition;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist
|
||||
|
||||
@ -53,6 +53,10 @@ void CompletionProgressHandler::showProgress(TextEditor::TextEditorWidget *widge
|
||||
|
||||
void CompletionProgressHandler::hideProgress()
|
||||
{
|
||||
if (m_progressWidget) {
|
||||
m_progressWidget->deleteLater();
|
||||
m_progressWidget = nullptr;
|
||||
}
|
||||
Utils::ToolTip::hideImmediately();
|
||||
}
|
||||
|
||||
@ -73,12 +77,24 @@ void CompletionProgressHandler::operateTooltip(
|
||||
if (!editorWidget)
|
||||
return;
|
||||
|
||||
auto progressWidget = new ProgressWidget(editorWidget);
|
||||
if (m_progressWidget) {
|
||||
delete m_progressWidget;
|
||||
}
|
||||
|
||||
QPoint showPoint = point;
|
||||
showPoint.ry() -= progressWidget->height();
|
||||
|
||||
Utils::ToolTip::show(showPoint, progressWidget, editorWidget);
|
||||
m_progressWidget = new ProgressWidget(editorWidget);
|
||||
|
||||
const QRect cursorRect = editorWidget->cursorRect(editorWidget->textCursor());
|
||||
QPoint globalPos = editorWidget->viewport()->mapToGlobal(cursorRect.topLeft());
|
||||
QPoint localPos = editorWidget->mapFromGlobal(globalPos);
|
||||
localPos.ry() -= m_progressWidget->height() + 5;
|
||||
|
||||
if (localPos.y() < 0) {
|
||||
localPos.ry() = cursorRect.bottom() + 5;
|
||||
}
|
||||
|
||||
m_progressWidget->move(localPos);
|
||||
m_progressWidget->show();
|
||||
m_progressWidget->raise();
|
||||
}
|
||||
|
||||
} // namespace QodeAssist
|
||||
|
||||
@ -24,6 +24,8 @@
|
||||
|
||||
namespace QodeAssist {
|
||||
|
||||
class ProgressWidget;
|
||||
|
||||
class CompletionProgressHandler : public TextEditor::BaseHoverHandler
|
||||
{
|
||||
public:
|
||||
@ -37,6 +39,7 @@ protected:
|
||||
|
||||
private:
|
||||
QPointer<TextEditor::TextEditorWidget> m_widget;
|
||||
QPointer<ProgressWidget> m_progressWidget;
|
||||
QPoint m_iconPosition;
|
||||
};
|
||||
|
||||
|
||||
213
widgets/ErrorWidget.cpp
Normal file
213
widgets/ErrorWidget.cpp
Normal file
@ -0,0 +1,213 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ErrorWidget.hpp"
|
||||
|
||||
#include <QFontMetrics>
|
||||
#include <QMouseEvent>
|
||||
#include <QPainterPath>
|
||||
|
||||
namespace QodeAssist {
|
||||
|
||||
ErrorWidget::ErrorWidget(const QString &errorMessage, QWidget *parent, int autoHideMs)
|
||||
: QWidget(parent)
|
||||
, m_errorMessage(errorMessage)
|
||||
, m_autoHideTimer(nullptr)
|
||||
, m_isHovered(false)
|
||||
{
|
||||
setupColors();
|
||||
setupIcon();
|
||||
|
||||
QFont errorFont = font();
|
||||
errorFont.setPointSize(qMax(8, errorFont.pointSize() - 2));
|
||||
setFont(errorFont);
|
||||
|
||||
setFixedSize(calculateSize());
|
||||
setAttribute(Qt::WA_TranslucentBackground);
|
||||
setMouseTracking(true);
|
||||
|
||||
if (autoHideMs > 0) {
|
||||
m_autoHideTimer = new QTimer(this);
|
||||
m_autoHideTimer->setSingleShot(true);
|
||||
connect(m_autoHideTimer, &QTimer::timeout, this, [this]() {
|
||||
if (!m_isHovered) {
|
||||
emit dismissed();
|
||||
deleteLater();
|
||||
}
|
||||
});
|
||||
m_autoHideTimer->start(autoHideMs);
|
||||
}
|
||||
}
|
||||
|
||||
ErrorWidget::~ErrorWidget()
|
||||
{
|
||||
if (m_autoHideTimer) {
|
||||
m_autoHideTimer->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void ErrorWidget::setErrorMessage(const QString &message)
|
||||
{
|
||||
m_errorMessage = message;
|
||||
QFont smallFont = font();
|
||||
smallFont.setPointSize(qMax(8, smallFont.pointSize() - 2));
|
||||
setFont(smallFont);
|
||||
setFixedSize(calculateSize());
|
||||
update();
|
||||
}
|
||||
|
||||
void ErrorWidget::setupColors()
|
||||
{
|
||||
m_textColor = Utils::creatorTheme()->color(Utils::Theme::TextColorNormal);
|
||||
m_backgroundColor = Utils::creatorTheme()->color(Utils::Theme::BackgroundColorNormal);
|
||||
m_errorColor = Utils::creatorTheme()->color(Utils::Theme::TextColorError);
|
||||
}
|
||||
|
||||
void ErrorWidget::setupIcon()
|
||||
{
|
||||
// Create a smaller error icon (exclamation mark in a circle)
|
||||
QPixmap pixmap(18, 18);
|
||||
pixmap.fill(Qt::transparent);
|
||||
|
||||
QPainter painter(&pixmap);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
// Draw circle
|
||||
painter.setPen(QPen(m_errorColor, 1.5));
|
||||
painter.setBrush(Qt::NoBrush);
|
||||
painter.drawEllipse(1, 1, 16, 16);
|
||||
|
||||
// Draw exclamation mark
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.setBrush(m_errorColor);
|
||||
|
||||
// Vertical bar of exclamation
|
||||
painter.drawRect(8, 4, 2, 8);
|
||||
|
||||
// Dot of exclamation
|
||||
painter.drawRect(8, 13, 2, 2);
|
||||
|
||||
m_errorIcon = pixmap;
|
||||
}
|
||||
|
||||
QSize ErrorWidget::calculateSize() const
|
||||
{
|
||||
QFontMetrics fm(font());
|
||||
|
||||
// Maximum width for the text area
|
||||
const int maxTextWidth = 350;
|
||||
const int iconWidth = 18;
|
||||
const int padding = 8;
|
||||
const int margin = 12;
|
||||
|
||||
// Calculate text area with word wrapping
|
||||
QRect textRect = fm.boundingRect(
|
||||
0, 0, maxTextWidth, 1000,
|
||||
Qt::AlignLeft | Qt::TextWordWrap,
|
||||
m_errorMessage
|
||||
);
|
||||
|
||||
// Total width: margin + icon + padding + text + margin
|
||||
int totalWidth = margin + iconWidth + padding + textRect.width() + margin;
|
||||
|
||||
// Total height: larger of icon or text, plus vertical margins
|
||||
int contentHeight = qMax(iconWidth, textRect.height());
|
||||
int totalHeight = contentHeight + margin * 2;
|
||||
|
||||
return QSize(totalWidth, totalHeight);
|
||||
}
|
||||
|
||||
void ErrorWidget::paintEvent(QPaintEvent *)
|
||||
{
|
||||
QPainter painter(this);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
painter.setRenderHint(QPainter::TextAntialiasing);
|
||||
|
||||
// Draw background with border
|
||||
QColor bgColor = m_backgroundColor;
|
||||
if (m_isHovered) {
|
||||
bgColor = bgColor.lighter(110);
|
||||
}
|
||||
|
||||
QPainterPath path;
|
||||
path.addRoundedRect(rect().adjusted(1, 1, -1, -1), 4, 4);
|
||||
|
||||
painter.fillPath(path, bgColor);
|
||||
painter.setPen(QPen(m_errorColor.lighter(150), 1));
|
||||
painter.drawPath(path);
|
||||
|
||||
const int iconSize = 18;
|
||||
const int padding = 8;
|
||||
const int margin = 12;
|
||||
|
||||
// Draw error icon
|
||||
if (!m_errorIcon.isNull()) {
|
||||
QRect iconRect(margin, margin, iconSize, iconSize);
|
||||
painter.drawPixmap(iconRect, m_errorIcon);
|
||||
}
|
||||
|
||||
// Draw error message with word wrap
|
||||
painter.setPen(m_textColor);
|
||||
QRect textRect = rect().adjusted(
|
||||
margin + iconSize + padding, // left
|
||||
margin, // top
|
||||
-margin, // right
|
||||
-margin // bottom
|
||||
);
|
||||
|
||||
painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextWordWrap, m_errorMessage);
|
||||
}
|
||||
|
||||
void ErrorWidget::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton) {
|
||||
emit dismissed();
|
||||
deleteLater();
|
||||
}
|
||||
QWidget::mousePressEvent(event);
|
||||
}
|
||||
|
||||
void ErrorWidget::enterEvent(QEnterEvent *event)
|
||||
{
|
||||
m_isHovered = true;
|
||||
update();
|
||||
|
||||
// Stop auto-hide timer while hovering
|
||||
if (m_autoHideTimer && m_autoHideTimer->isActive()) {
|
||||
m_autoHideTimer->stop();
|
||||
}
|
||||
|
||||
QWidget::enterEvent(event);
|
||||
}
|
||||
|
||||
void ErrorWidget::leaveEvent(QEvent *event)
|
||||
{
|
||||
m_isHovered = false;
|
||||
update();
|
||||
|
||||
// Restart auto-hide timer when leaving
|
||||
if (m_autoHideTimer) {
|
||||
m_autoHideTimer->start(2000); // Give 2 more seconds after leaving
|
||||
}
|
||||
|
||||
QWidget::leaveEvent(event);
|
||||
}
|
||||
|
||||
} // namespace QodeAssist
|
||||
|
||||
65
widgets/ErrorWidget.hpp
Normal file
65
widgets/ErrorWidget.hpp
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QPainter>
|
||||
#include <QTimer>
|
||||
#include <QWidget>
|
||||
|
||||
#include <utils/theme/theme.h>
|
||||
|
||||
namespace QodeAssist {
|
||||
|
||||
class ErrorWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ErrorWidget(const QString &errorMessage, QWidget *parent = nullptr, int autoHideMs = 5000);
|
||||
~ErrorWidget();
|
||||
|
||||
void setErrorMessage(const QString &message);
|
||||
|
||||
QString errorMessage() const { return m_errorMessage; }
|
||||
|
||||
signals:
|
||||
void dismissed();
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
void enterEvent(QEnterEvent *event) override;
|
||||
void leaveEvent(QEvent *event) override;
|
||||
|
||||
private:
|
||||
QString m_errorMessage;
|
||||
QTimer *m_autoHideTimer;
|
||||
QColor m_textColor;
|
||||
QColor m_backgroundColor;
|
||||
QColor m_errorColor;
|
||||
QPixmap m_errorIcon;
|
||||
bool m_isHovered;
|
||||
|
||||
void setupColors();
|
||||
void setupIcon();
|
||||
QSize calculateSize() const;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist
|
||||
|
||||
Reference in New Issue
Block a user