Commit 93e9904d authored by Jan Paul Batrina's avatar Jan Paul Batrina
Browse files

[ColorPicker] Use QVector instead of QList (+other minor changes)

ColorIndex is now ColorRange and is initialized to {-1, -1} by default.
Updated some comments and code formatting.
parent 8f1e8ffd
......@@ -89,10 +89,8 @@ QIcon KateColorPickerConfigPage::icon() const
void KateColorPickerConfigPage::apply()
{
if (!m_colorConfigChanged) {
// apply() gets called when the "Apply" or "OK" button is pressed
// this means that if a user presses "Apply" THEN "OK", the config is updated twice
// since the reconstruction of the regex (and the regeneration of color note positions) is expensive,
// we only update on the first call to apply() before changes are made again
// apply() gets called when the "Apply" or "OK" button is pressed. This means that if a user presses "Apply" THEN "OK", the config is updated twice
// Since the the regeneration of color note positions is expensive, we only update on the first call to apply() before changes are made again
return;
}
......
......@@ -102,7 +102,7 @@ ColorPickerInlineNoteProvider::~ColorPickerInlineNoteProvider()
void ColorPickerInlineNoteProvider::updateColorMatchingCriteria()
{
KConfigGroup config(KSharedConfig::openConfig(), "ColorPicker");
m_matchHexLengths = config.readEntry("HexLengths", QList<int>{12, 9, 8, 6, 3});
m_matchHexLengths = config.readEntry("HexLengths", QList<int>{12, 9, 8, 6, 3}).toVector();
m_putPreviewAfterColor = config.readEntry("PreviewAfterColor", true);
m_matchNamedColors = config.readEntry("NamedColors", false);
}
......@@ -126,7 +126,7 @@ void ColorPickerInlineNoteProvider::updateNotes(int startLine, int endLine) {
}
}
ColorIndex ColorPickerInlineNoteProvider::findNamedColor(const QStringView lineText, int start) const
ColorRange ColorPickerInlineNoteProvider::findNamedColor(const QStringView lineText, int start) const
{
// only return when a valid named color is found, a # is found, or we have reached the end of the line
while (start < lineText.length()) {
......@@ -168,7 +168,7 @@ ColorIndex ColorPickerInlineNoteProvider::findNamedColor(const QStringView lineT
return {-1, start};
}
ColorIndex ColorPickerInlineNoteProvider::findHexColor(const QStringView lineText, int start) const
ColorRange ColorPickerInlineNoteProvider::findHexColor(const QStringView lineText, int start) const
{
if (m_matchHexLengths.size() == 0) {
return {-1, -1};
......@@ -207,7 +207,7 @@ QVector<int> ColorPickerInlineNoteProvider::inlineNotes(int line) const
const QString lineText = m_doc->line(line);
for (int lineIndex = 0; lineIndex < lineText.length(); ++lineIndex) {
ColorIndex color{-1, -1};
ColorRange color;
bool isNamedColor = true;
if (m_matchNamedColors) {
color = findNamedColor(lineText, lineIndex);
......@@ -267,7 +267,7 @@ void ColorPickerInlineNoteProvider::paintInlineNote(const KTextEditor::InlineNot
auto colorEnd = note.position().column();
const QVector<int> &colorNoteIndices = m_colorNoteIndices[line].colorNoteIndices;
// Since the colorNoteIndices are inserted in left-to-right (increasing) order in inlineNotes, we can use binary search to find the index (or color note number) for the line
// Since the colorNoteIndices are inserted in left-to-right (increasing) order in inlineNotes(), we can use binary search to find the index (or color note number) for the line
const int colorNoteNumber = std::lower_bound(colorNoteIndices.cbegin(), colorNoteIndices.cend(), colorEnd) - colorNoteIndices.cbegin();
auto colorStart = m_colorNoteIndices[line].otherColorIndices[colorNoteNumber];
......@@ -277,10 +277,11 @@ void ColorPickerInlineNoteProvider::paintInlineNote(const KTextEditor::InlineNot
}
const auto color = QColor(m_doc->text({line, colorStart, line, colorEnd}));
auto penColor = color;
penColor.setAlpha(255);
// ensure that the border color is always visible
painter.setPen((penColor.value() < 128 ? penColor.lighter(150) : penColor.darker(150)));
QColor penColor = color;
penColor.setAlpha(255);
painter.setPen(penColor.value() < 128 ? penColor.lighter(150) : penColor.darker(150));
painter.setBrush(color);
painter.setRenderHint(QPainter::Antialiasing, false);
const QFontMetricsF fm(note.font());
......@@ -305,14 +306,13 @@ void ColorPickerInlineNoteProvider::inlineNoteActivated(const KTextEditor::Inlin
}
const auto oldColor = QColor(m_doc->text({line, colorStart, line, colorEnd}));
QColorDialog::ColorDialogOptions dialogOptions = QColorDialog::ShowAlphaChannel;
QString title = i18n("Select Color (Hex output)");
if (!m_doc->isReadWrite()) {
dialogOptions |= QColorDialog::NoButtons;
title = i18n("View Color [Read only]");
}
const auto newColor = QColorDialog::getColor(oldColor, const_cast<KTextEditor::View *>(note.view()), title, dialogOptions);
const QColor newColor = QColorDialog::getColor(oldColor, const_cast<KTextEditor::View*>(note.view()), title, dialogOptions);
if (!newColor.isValid()) {
return;
}
......
......@@ -19,7 +19,8 @@
#include <QVariant>
#include <QVector>
struct ColorIndex {
struct ColorRange {
ColorRange(const int s = -1, const int e = -1) : start(s), end(e) {}
int start;
int end;
};
......@@ -35,8 +36,8 @@ public:
// if startLine == -1, update all notes. endLine is optional
void updateNotes(int startLine=-1, int endLine=-1);
ColorIndex findNamedColor(const QStringView lineText, int start) const;
ColorIndex findHexColor(const QStringView lineText, int start) const;
ColorRange findNamedColor(const QStringView lineText, int start) const;
ColorRange findHexColor(const QStringView lineText, int start) const;
QVector<int> inlineNotes(int line) const override;
QSize inlineNoteSize(const KTextEditor::InlineNote &note) const override;
......@@ -50,17 +51,17 @@ private:
int m_previousNumLines = -1;
struct ColorIndices {
// When m_putPreviewAfterColor is true, otherColorIndices holds the starting color indices while colorNoteIndices holds the end color indices
// When m_putPreviewAfterColor is true, otherColorIndices holds the starting color indices while colorNoteIndices holds the end color indices (and vice versa)
// colorNoteIndices[i] corresponds to otherColorIndices[i]
QVector<int> colorNoteIndices;
QVector<int> otherColorIndices;
};
// mutable is used here since InlineNoteProvider::inlineNotes is const only, and we update the notes lazily (only when inlineNotes is called)
// mutable is used here since InlineNoteProvider::inlineNotes() is const only, and we update the notes lazily (only when inlineNotes() is called)
mutable QHash<int, ColorIndices> m_colorNoteIndices;
static QVector<QString> s_namedColors;
QList<int> m_matchHexLengths;
QVector<int> m_matchHexLengths;
bool m_putPreviewAfterColor;
bool m_matchNamedColors;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment