fix: Adapt widget to content size

This commit is contained in:
Petr Mironychev
2025-11-28 13:51:46 +01:00
parent f6d647d5c8
commit 595895840a
2 changed files with 71 additions and 41 deletions

View File

@ -178,26 +178,26 @@ QuickRefactorSettings::QuickRefactorSettings()
widgetMinWidth.setSettingsKey(Constants::QR_WIDGET_MIN_WIDTH); widgetMinWidth.setSettingsKey(Constants::QR_WIDGET_MIN_WIDTH);
widgetMinWidth.setLabelText(Tr::tr("Widget Minimum Width:")); widgetMinWidth.setLabelText(Tr::tr("Widget Minimum Width:"));
widgetMinWidth.setToolTip(Tr::tr("Minimum width for the refactor widget (in pixels)")); widgetMinWidth.setToolTip(Tr::tr("Minimum width for the refactor widget (in pixels)"));
widgetMinWidth.setRange(400, 2000); widgetMinWidth.setRange(200, 999999);
widgetMinWidth.setDefaultValue(600); widgetMinWidth.setDefaultValue(400);
widgetMaxWidth.setSettingsKey(Constants::QR_WIDGET_MAX_WIDTH); widgetMaxWidth.setSettingsKey(Constants::QR_WIDGET_MAX_WIDTH);
widgetMaxWidth.setLabelText(Tr::tr("Widget Maximum Width:")); widgetMaxWidth.setLabelText(Tr::tr("Widget Maximum Width:"));
widgetMaxWidth.setToolTip(Tr::tr("Maximum width for the refactor widget (in pixels)")); widgetMaxWidth.setToolTip(Tr::tr("Maximum width for the refactor widget (in pixels)"));
widgetMaxWidth.setRange(600, 3000); widgetMaxWidth.setRange(400, 999999);
widgetMaxWidth.setDefaultValue(1400); widgetMaxWidth.setDefaultValue(9999);
widgetMinHeight.setSettingsKey(Constants::QR_WIDGET_MIN_HEIGHT); widgetMinHeight.setSettingsKey(Constants::QR_WIDGET_MIN_HEIGHT);
widgetMinHeight.setLabelText(Tr::tr("Widget Minimum Height:")); widgetMinHeight.setLabelText(Tr::tr("Widget Minimum Height:"));
widgetMinHeight.setToolTip(Tr::tr("Minimum height for the refactor widget (in pixels)")); widgetMinHeight.setToolTip(Tr::tr("Minimum height for the refactor widget (in pixels)"));
widgetMinHeight.setRange(80, 800); widgetMinHeight.setRange(80, 999999);
widgetMinHeight.setDefaultValue(120); widgetMinHeight.setDefaultValue(100);
widgetMaxHeight.setSettingsKey(Constants::QR_WIDGET_MAX_HEIGHT); widgetMaxHeight.setSettingsKey(Constants::QR_WIDGET_MAX_HEIGHT);
widgetMaxHeight.setLabelText(Tr::tr("Widget Maximum Height:")); widgetMaxHeight.setLabelText(Tr::tr("Widget Maximum Height:"));
widgetMaxHeight.setToolTip(Tr::tr("Maximum height for the refactor widget (in pixels)")); widgetMaxHeight.setToolTip(Tr::tr("Maximum height for the refactor widget (in pixels)"));
widgetMaxHeight.setRange(200, 1200); widgetMaxHeight.setRange(200, 999999);
widgetMaxHeight.setDefaultValue(500); widgetMaxHeight.setDefaultValue(9999);
systemPrompt.setSettingsKey(Constants::QR_SYSTEM_PROMPT); systemPrompt.setSettingsKey(Constants::QR_SYSTEM_PROMPT);
systemPrompt.setLabelText(Tr::tr("System Prompt:")); systemPrompt.setLabelText(Tr::tr("System Prompt:"));

View File

@ -26,11 +26,13 @@
#include <QCloseEvent> #include <QCloseEvent>
#include <QEnterEvent> #include <QEnterEvent>
#include <QEvent> #include <QEvent>
#include <QGuiApplication>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QKeyEvent> #include <QKeyEvent>
#include <QLabel> #include <QLabel>
#include <QPainter> #include <QPainter>
#include <QRegion> #include <QRegion>
#include <QScreen>
#include <QScrollBar> #include <QScrollBar>
#include <QSharedPointer> #include <QSharedPointer>
#include <QSplitter> #include <QSplitter>
@ -149,7 +151,7 @@ void RefactorWidget::setupUi()
m_statsLabel = new QLabel(this); m_statsLabel = new QLabel(this);
m_statsLabel->setAlignment(Qt::AlignLeft); m_statsLabel->setAlignment(Qt::AlignLeft);
mainLayout->addWidget(m_statsLabel); mainLayout->addWidget(m_statsLabel, 0);
m_leftDocument = QSharedPointer<TextEditor::TextDocument>::create(); m_leftDocument = QSharedPointer<TextEditor::TextDocument>::create();
m_rightDocument = QSharedPointer<TextEditor::TextDocument>::create(); m_rightDocument = QSharedPointer<TextEditor::TextDocument>::create();
@ -168,7 +170,6 @@ void RefactorWidget::setupUi()
m_leftEditor->setFrameStyle(QFrame::StyledPanel); m_leftEditor->setFrameStyle(QFrame::StyledPanel);
m_leftEditor->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_leftEditor->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_leftEditor->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); m_leftEditor->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_leftEditor->setMinimumHeight(100);
m_leftEditor->setMinimumWidth(150); m_leftEditor->setMinimumWidth(150);
m_leftEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_leftEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_leftEditor->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard); m_leftEditor->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
@ -179,7 +180,6 @@ void RefactorWidget::setupUi()
m_rightEditor->setFrameStyle(QFrame::StyledPanel); m_rightEditor->setFrameStyle(QFrame::StyledPanel);
m_rightEditor->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); m_rightEditor->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_rightEditor->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); m_rightEditor->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_rightEditor->setMinimumHeight(100);
m_rightEditor->setMinimumWidth(150); m_rightEditor->setMinimumWidth(150);
m_rightEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_rightEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
@ -190,8 +190,8 @@ void RefactorWidget::setupUi()
leftLayout->setContentsMargins(0, 0, 0, 0); leftLayout->setContentsMargins(0, 0, 0, 0);
auto *originalLabel = new QLabel(tr("◄ Original"), m_leftContainer); auto *originalLabel = new QLabel(tr("◄ Original"), m_leftContainer);
leftLayout->addWidget(originalLabel); leftLayout->addWidget(originalLabel, 0);
leftLayout->addWidget(m_leftEditor); leftLayout->addWidget(m_leftEditor, 1);
auto *rightContainer = new QWidget(); auto *rightContainer = new QWidget();
rightContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); rightContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
@ -200,8 +200,8 @@ void RefactorWidget::setupUi()
rightLayout->setContentsMargins(0, 0, 0, 0); rightLayout->setContentsMargins(0, 0, 0, 0);
auto *refactoredLabel = new QLabel(tr("Refactored ►"), rightContainer); auto *refactoredLabel = new QLabel(tr("Refactored ►"), rightContainer);
rightLayout->addWidget(refactoredLabel); rightLayout->addWidget(refactoredLabel, 0);
rightLayout->addWidget(m_rightEditor); rightLayout->addWidget(m_rightEditor, 1);
m_splitter->addWidget(m_leftContainer); m_splitter->addWidget(m_leftContainer);
m_splitter->addWidget(rightContainer); m_splitter->addWidget(rightContainer);
@ -209,7 +209,7 @@ void RefactorWidget::setupUi()
m_splitter->setStretchFactor(0, 1); m_splitter->setStretchFactor(0, 1);
m_splitter->setStretchFactor(1, 1); m_splitter->setStretchFactor(1, 1);
mainLayout->addWidget(m_splitter); mainLayout->addWidget(m_splitter, 1);
connect(m_leftEditor->verticalScrollBar(), &QScrollBar::valueChanged, connect(m_leftEditor->verticalScrollBar(), &QScrollBar::valueChanged,
this, &RefactorWidget::syncLeftScroll); this, &RefactorWidget::syncLeftScroll);
@ -245,7 +245,7 @@ void RefactorWidget::setupUi()
buttonLayout->addWidget(m_applyButton); buttonLayout->addWidget(m_applyButton);
buttonLayout->addWidget(m_declineButton); buttonLayout->addWidget(m_declineButton);
mainLayout->addLayout(buttonLayout); mainLayout->addLayout(buttonLayout, 0);
connect(m_applyButton, &QPushButton::clicked, this, &RefactorWidget::applyRefactoring); connect(m_applyButton, &QPushButton::clicked, this, &RefactorWidget::applyRefactoring);
connect(m_declineButton, &QPushButton::clicked, this, &RefactorWidget::declineRefactoring); connect(m_declineButton, &QPushButton::clicked, this, &RefactorWidget::declineRefactoring);
@ -630,39 +630,69 @@ void RefactorWidget::addLineMarkers()
void RefactorWidget::updateSizeToContent() void RefactorWidget::updateSizeToContent()
{ {
QFontMetrics fm(m_rightEditor->font()); QFontMetrics fm(m_rightEditor->font());
int charWidth = fm.horizontalAdvance('m');
int lineHeight = fm.height(); int lineHeight = fm.height();
int lineCount = m_rightDocument->document()->blockCount();
int leftMaxWidth = 0;
int leftLineCount = 0;
QTextBlock leftBlock = m_leftDocument->document()->begin();
while (leftBlock.isValid()) {
int lineWidth = fm.horizontalAdvance(leftBlock.text());
leftMaxWidth = qMax(leftMaxWidth, lineWidth);
leftLineCount++;
leftBlock = leftBlock.next();
}
int rightMaxWidth = 0;
int rightLineCount = 0;
QTextBlock rightBlock = m_rightDocument->document()->begin();
while (rightBlock.isValid()) {
int lineWidth = fm.horizontalAdvance(rightBlock.text());
rightMaxWidth = qMax(rightMaxWidth, lineWidth);
rightLineCount++;
rightBlock = rightBlock.next();
}
int maxLineWidth = qMax(leftMaxWidth, rightMaxWidth);
int maxLineCount = qMax(leftLineCount, rightLineCount);
const int editorPadding = 60;
const int scrollBarWidth = 20;
const int statsLabelHeight = 30;
const int buttonLayoutHeight = 35;
const int layoutMargins = 20;
const int labelHeight = 20;
int singleEditorWidth = maxLineWidth + editorPadding + scrollBarWidth;
int contentHeight = maxLineCount * lineHeight + statsLabelHeight + buttonLayoutHeight + layoutMargins + labelHeight;
bool horizontal = m_splitter->orientation() == Qt::Horizontal; bool horizontal = m_splitter->orientation() == Qt::Horizontal;
const int minWidth = Settings::quickRefactorSettings().widgetMinWidth(); int contentWidth;
const int maxWidth = qMin(Settings::quickRefactorSettings().widgetMaxWidth(), m_editorWidth - 40);
const int minHeight = Settings::quickRefactorSettings().widgetMinHeight();
const int maxHeight = Settings::quickRefactorSettings().widgetMaxHeight();
if (horizontal) { if (horizontal) {
int totalWidth = qBound(minWidth, charWidth * 60 * 2 + 90, maxWidth); contentWidth = singleEditorWidth * 2 + m_splitter->handleWidth() + 20;
setFixedWidth(totalWidth);
int editorHeight = qBound(minHeight, lineCount * lineHeight, maxHeight);
m_leftEditor->setMinimumHeight(editorHeight);
m_leftEditor->setMaximumHeight(editorHeight);
m_rightEditor->setMinimumHeight(editorHeight);
m_rightEditor->setMaximumHeight(editorHeight);
} else { } else {
int editorWidth = qBound(minWidth, charWidth * 85 + 80, maxWidth); contentWidth = singleEditorWidth + 20;
setFixedWidth(editorWidth);
int editorHeight = qBound(minHeight, lineCount * lineHeight, maxHeight);
m_leftEditor->setMinimumHeight(editorHeight);
m_leftEditor->setMaximumHeight(editorHeight);
m_rightEditor->setMinimumHeight(editorHeight);
m_rightEditor->setMaximumHeight(editorHeight);
} }
QScreen *screen = window()->screen();
if (!screen) {
screen = QGuiApplication::primaryScreen();
}
int screenWidth = screen ? screen->availableGeometry().width() : 1920;
int screenHeight = screen ? screen->availableGeometry().height() : 1080;
const int minWidth = Settings::quickRefactorSettings().widgetMinWidth();
const int maxWidth = qMin(Settings::quickRefactorSettings().widgetMaxWidth(),
qMin(m_editorWidth - 40, screenWidth - 100));
const int minHeight = Settings::quickRefactorSettings().widgetMinHeight();
const int maxHeight = qMin(Settings::quickRefactorSettings().widgetMaxHeight(), screenHeight - 100);
int targetWidth = qBound(minWidth, contentWidth, maxWidth);
int targetHeight = qBound(minHeight, contentHeight, maxHeight);
setFixedSize(targetWidth, targetHeight);
updateGeometry(); updateGeometry();
adjustSize();
} }
void RefactorWidget::applyEditorSettings() void RefactorWidget::applyEditorSettings()