- enhancement: Make colors in code suggestion popup consistent with the editor.

- enhancement: Make colors in header suggestion popup consistent with the editor.
This commit is contained in:
Roy Qu 2024-03-31 20:43:37 +08:00
parent e960d820f3
commit 8fc621829e
6 changed files with 136 additions and 28 deletions

View File

@ -108,8 +108,8 @@ Red Panda C++ Version 2.27
- enhancement: By default, use monospaced font to display register values in the CPU Info dialog.
- fix: Negative values in register like AH/AL are wrongs displayed as 32/64-bit number.
- Change: Change background color for highlighted buttons in the default theme.
- enhancement: Make matched contents more obvious in the code suggestion popup.
- enhancement: Make matched contents more obvious in the header suggestion popup.
- enhancement: Make colors in code suggestion popup consistent with the editor.
- enhancement: Make colors in header suggestion popup consistent with the editor.
Red Panda C++ Version 2.26
- enhancement: Code suggestion for embedded std::vectors.

View File

@ -3605,11 +3605,9 @@ void Editor::showCompletion(const QString& preWord,bool autoComplete, CodeComple
mCompletionPopup->move(popupPos);
// fCompletionBox.CodeInsList := dmMain.CodeInserts.ItemList;
// fCompletionBox.SymbolUsage := dmMain.SymbolUsage;
// fCompletionBox.ShowCount := devCodeCompletion.MaxCount;
//Set Font size;
mCompletionPopup->setFont(font());
mCompletionPopup->setLineHeightFactor(pSettings->editor().lineSpacing());
// Redirect key presses to completion box if applicable
//todo:
mCompletionPopup->setKeypressedCallback([this](QKeyEvent *event)->bool{
@ -3690,6 +3688,7 @@ void Editor::showHeaderCompletion(bool autoComplete, bool forceShow)
pSettings->codeCompletion().height());
//Set Font size;
mHeaderCompletionPopup->setFont(font());
mHeaderCompletionPopup->setLineHeightFactor(pSettings->editor().lineSpacing());
// Redirect key presses to completion box if applicable
mHeaderCompletionPopup->setKeypressedCallback([this](QKeyEvent* event)->bool{

View File

@ -22,6 +22,7 @@
#include "../symbolusagemanager.h"
#include "../colorscheme.h"
#include "../iconsmanager.h"
#include "../settings.h"
#include <QKeyEvent>
#include <QVBoxLayout>
@ -151,12 +152,18 @@ bool CodeCompletionPopup::search(const QString &memberPhrase, bool autoHideOnSin
setCursor(oldCursor);
if (!mCompletionStatementList.isEmpty()) {
PColorSchemeItem item = mColors->value(StatementKind::skUnknown,PColorSchemeItem());
QString schemaName = pSettings->editor().colorScheme();
PColorSchemeItem item = pColorManager->getItem(schemaName, COLOR_SCHEME_ACTIVE_LINE);
if (item)
mDelegate->setCurrentSelectionColor(item->background());
else
mDelegate->setCurrentSelectionColor(palette().highlight().color());
item = pColorManager->getItem(schemaName, COLOR_SCHEME_TEXT);
if (item)
mDelegate->setNormalColor(item->foreground());
else
mDelegate->setNormalColor(palette().color(QPalette::Text));
item = mColors->value(StatementKind::skKeyword,PColorSchemeItem());
item = pColorManager->getItem(schemaName, SYNS_AttrReserveWord_Type);
if (item)
mDelegate->setMatchedColor(item->foreground());
else
@ -1113,6 +1120,11 @@ void CodeCompletionPopup::setHideSymbolsStartWithTwoUnderline(bool newHideSymbol
mHideSymbolsStartWithTwoUnderline = newHideSymbolsStartWithTwoUnderline;
}
void CodeCompletionPopup::setLineHeightFactor(float factor)
{
mDelegate->setLineHeightFactor(factor);
}
bool CodeCompletionPopup::hideSymbolsStartWithTwoUnderline() const
{
return mHideSymbolsStartWithTwoUnderline;
@ -1329,25 +1341,13 @@ void CodeCompletionListItemDelegate::paint(QPainter *painter, const QStyleOption
QFont normalFont{font()};
QFont matchedFont{font()};
normalFont.setBold(false);
normalFont.setUnderline(false);
matchedFont.setBold(true);
matchedFont.setUnderline(true);
painter->save();
painter->setFont(normalFont);
QColor normalColor = mNormalColor;
QColor matchedColor = mMatchedColor;
if (option.state & QStyle::State_Selected) {
painter->fillRect(option.rect, option.palette.highlight());
normalColor = option.palette.color(QPalette::HighlightedText);
float h = mMatchedColor.hslHueF();
float s = mMatchedColor.hslSaturationF();
float l = normalColor.lightnessF();
if (l>0.85) {
l = 0.85;
} else if (l<0.15) {
l = 0.15;
}
matchedColor = QColor::fromHslF(h,s,l);
painter->fillRect(option.rect, mCurrentSelectionColor);
}
QPixmap icon = mModel->statementIcon(index);
int x=option.rect.left();
@ -1360,7 +1360,8 @@ void CodeCompletionListItemDelegate::paint(QPainter *painter, const QStyleOption
}
QString text = statement->command;
int pos=0;
int y=option.rect.bottom()-painter->fontMetrics().descent();
int padding = (option.rect.height()-painter->fontMetrics().height())/2;
int y=option.rect.bottom()-painter->fontMetrics().descent()-padding;
foreach (const PStatementMathPosition& matchPosition, statement->matchPositions) {
if (pos<matchPosition->start) {
QString t = text.mid(pos,matchPosition->start-pos);
@ -1419,9 +1420,37 @@ void CodeCompletionListItemDelegate::setFont(const QFont &newFont)
mFont = newFont;
}
QSize CodeCompletionListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSize size = QStyledItemDelegate::sizeHint(option, index);
size.setHeight(size.height()*mLineHeightFactor);
return size;
}
float CodeCompletionListItemDelegate::lineHeightFactor() const
{
return mLineHeightFactor;
}
void CodeCompletionListItemDelegate::setLineHeightFactor(float newLineHeightFactor)
{
mLineHeightFactor = newLineHeightFactor;
}
QColor CodeCompletionListItemDelegate::currentSelectionColor() const
{
return mCurrentSelectionColor;
}
void CodeCompletionListItemDelegate::setCurrentSelectionColor(const QColor &newCurrentSelectionColor)
{
mCurrentSelectionColor = newCurrentSelectionColor;
}
CodeCompletionListItemDelegate::CodeCompletionListItemDelegate(CodeCompletionListModel *model, QWidget *parent) : QStyledItemDelegate(parent),
mModel(model)
{
mNormalColor = qApp->palette().color(QPalette::Text);
mMatchedColor = qApp->palette().color(QPalette::BrightText);
mLineHeightFactor = 1.0;
}

View File

@ -56,6 +56,7 @@ public:
// QAbstractItemDelegate interface
public:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
const QColor &normalColor() const;
void setNormalColor(const QColor &newNormalColor);
@ -66,11 +67,19 @@ public:
const QFont &font() const;
void setFont(const QFont &newFont);
float lineHeightFactor() const;
void setLineHeightFactor(float newLineHeightFactor);
QColor currentSelectionColor() const;
void setCurrentSelectionColor(const QColor &newCurrentSelectionColor);
private:
CodeCompletionListModel *mModel;
QColor mNormalColor;
QColor mMatchedColor;
QColor mCurrentSelectionColor;
QFont mFont;
float mLineHeightFactor;
};
class CodeCompletionPopup : public QWidget
@ -119,6 +128,7 @@ public:
void setHideSymbolsStartWithUnderline(bool newHideSymbolsStartWithUnderline);
bool hideSymbolsStartWithTwoUnderline() const;
void setHideSymbolsStartWithTwoUnderline(bool newHideSymbolsStartWithTwoUnderline);
void setLineHeightFactor(float factor);
const PStatement &currentScope() const;
void setCurrentScope(const PStatement &newCurrentStatement);

View File

@ -23,6 +23,9 @@
#include "qpainter.h"
#include "systemconsts.h"
#include "../utils.h"
#include "../settings.h"
#include "../colorscheme.h"
#include <qsynedit/constants.h>
HeaderCompletionPopup::HeaderCompletionPopup(QWidget* parent):QWidget(parent)
{
@ -89,6 +92,17 @@ bool HeaderCompletionPopup::search(const QString &phrase, bool autoHideOnSingleR
setCursor(oldCursor);
if (!mCompletionList.isEmpty()) {
QString schemaName = pSettings->editor().colorScheme();
PColorSchemeItem item = pColorManager->getItem(schemaName, COLOR_SCHEME_ACTIVE_LINE);
if (item)
mDelegate->setCurrentSelectionBackColor(item->background());
else
mDelegate->setCurrentSelectionBackColor(palette().highlight().color());
item = pColorManager->getItem(schemaName, SYNS_AttrReserveWord_Type);
if (item)
mDelegate->setMatchedColor(item->foreground());
else
mDelegate->setMatchedColor(palette().color(QPalette::HighlightedText));
mListView->setCurrentIndex(mModel->index(0,0));
if (mCompletionList.count() == 1) {
// if only one suggestion and auto hide , don't show the frame
@ -143,6 +157,11 @@ QString HeaderCompletionPopup::selectedFilename(bool updateUsageCount)
return "";
}
void HeaderCompletionPopup::setLineHeightFactor(float newLineHeightFactor)
{
mDelegate->setLineHeightFactor(newLineHeightFactor);
}
static bool sortByUsage(const PHeaderCompletionListItem& item1,const PHeaderCompletionListItem& item2){
if (item1->usageCount != item2->usageCount)
return item1->usageCount > item2->usageCount;
@ -390,7 +409,8 @@ void HeaderCompletionListModel::setMatched(int newMatched)
HeaderCompletionListItemDelegate::HeaderCompletionListItemDelegate(HeaderCompletionListModel *model, QWidget *parent):
QStyledItemDelegate{parent},
mModel{model}
mModel{model},
mLineHeightFactor{1.0}
{
}
@ -410,28 +430,34 @@ void HeaderCompletionListItemDelegate::paint(QPainter *painter, const QStyleOpti
QFont normalFont{font()};
QFont matchedFont{font()};
normalFont.setBold(false);
normalFont.setUnderline(false);
matchedFont.setBold(true);
matchedFont.setUnderline(true);
painter->setFont(normalFont);
QColor normalColor = mModel->data(index, Qt::ForegroundRole).value<QColor>();
if (option.state & QStyle::State_Selected) {
painter->fillRect(option.rect, option.palette.highlight());
normalColor = option.palette.color(QPalette::HighlightedText);
painter->fillRect(option.rect, mCurrentSelectionBackColor);
}
painter->setPen(normalColor);
int y=option.rect.bottom()-painter->fontMetrics().descent();
int padding = (option.rect.height()-painter->fontMetrics().height())/2;
int y=option.rect.bottom()-painter->fontMetrics().descent()-padding;
int x=0;
QString t = text.left(mModel->matched());
painter->setPen(mMatchedColor);
painter->setFont(matchedFont);
painter->drawText(x,y,t);
x+=painter->fontMetrics().horizontalAdvance(t);
t = text.mid(mModel->matched());
painter->setPen(normalColor);
painter->setFont(normalFont);
painter->drawText(x,y,t);
painter->restore();
}
QSize HeaderCompletionListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSize size = QStyledItemDelegate::sizeHint(option, index);
size.setHeight(size.height()*mLineHeightFactor);
return size;
}
const QFont &HeaderCompletionListItemDelegate::font() const
{
return mFont;
@ -441,3 +467,33 @@ void HeaderCompletionListItemDelegate::setFont(const QFont &newFont)
{
mFont=newFont;
}
float HeaderCompletionListItemDelegate::lineHeightFactor() const
{
return mLineHeightFactor;
}
void HeaderCompletionListItemDelegate::setLineHeightFactor(float newLineHeightFactor)
{
mLineHeightFactor = newLineHeightFactor;
}
QColor HeaderCompletionListItemDelegate::matchedColor() const
{
return mMatchedColor;
}
void HeaderCompletionListItemDelegate::setMatchedColor(const QColor &newMatchedColor)
{
mMatchedColor = newMatchedColor;
}
QColor HeaderCompletionListItemDelegate::currentSelectionBackColor() const
{
return mCurrentSelectionBackColor;
}
void HeaderCompletionListItemDelegate::setCurrentSelectionBackColor(const QColor &newCurrentSelectionBackColor)
{
mCurrentSelectionBackColor = newCurrentSelectionBackColor;
}

View File

@ -74,13 +74,26 @@ public:
// QAbstractItemDelegate interface
public:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
const QFont &font() const;
void setFont(const QFont &newFont);
float lineHeightFactor() const;
void setLineHeightFactor(float newLineHeightFactor);
QColor matchedColor() const;
void setMatchedColor(const QColor &newMatchedColor);
QColor currentSelectionBackColor() const;
void setCurrentSelectionBackColor(const QColor &newCurrentSelectionBackColor);
private:
HeaderCompletionListModel *mModel;
QColor mCurrentSelectionBackColor;
QColor mMatchedColor;
QFont mFont;
float mLineHeightFactor;
};
class HeaderCompletionPopup : public QWidget
@ -97,6 +110,7 @@ public:
const QColor& systemColor,
const QColor& folderColor);
QString selectedFilename(bool updateUsageCount);
void setLineHeightFactor(float newLineHeightFactor);
private:
void filterList(const QString& member);