Commit 85daa39b authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Calculate line number area width for each file item instead of global width

parent 79697896
......@@ -122,8 +122,6 @@ void MatchModel::clear()
m_matchFileIndexHash.clear();
m_matchUnsavedFileIndexHash.clear();
m_lastMatchUrl.clear();
m_maxColNumFound = 0;
m_maxLineNumFound = 0;
endResetModel();
}
......@@ -182,10 +180,6 @@ void MatchModel::addMatches(const QUrl &fileUrl, const QVector<KateSearchMatch>
beginInsertRows(createIndex(fileIndex, 0, FileItemId), matchIndex, matchIndex + searchMatches.size() - 1);
m_matchFiles[fileIndex].matches += searchMatches;
endInsertRows();
const auto &lastMatch = searchMatches.last();
m_maxLineNumFound = std::max(lastMatch.range.start().line(), m_maxLineNumFound);
m_maxColNumFound = std::max(lastMatch.range.start().column(), m_maxColNumFound);
}
void MatchModel::setMatchColors(const QString &foreground, const QString &background, const QString &replaceBackground)
......@@ -904,6 +898,12 @@ QVariant MatchModel::data(const QModelIndex &index, int role) const
return m_matchFiles[fileRow].fileUrl;
case PlainTextRole:
return fileToPlainText(m_matchFiles[fileRow]);
case LastMatchedRangeInFile:
if (m_matchFiles[fileRow].matches.isEmpty()) {
qWarning() << "Unexpected empty matches for file!";
return {};
}
return QVariant::fromValue(m_matchFiles[fileRow].matches.constLast().range);
}
} else if (matchRow < m_matchFiles[fileRow].matches.size()) {
// Match
......@@ -939,6 +939,9 @@ QVariant MatchModel::data(const QModelIndex &index, int role) const
return matchToPlainText(match);
case MatchItem:
return QVariant::fromValue(match);
case LastMatchedRangeInFile:
qWarning() << "Requested last matched line from a match item instead of file item1";
return {};
}
} else {
qDebug() << "bad index";
......
......@@ -59,7 +59,8 @@ public:
ReplacedRole,
ReplaceTextRole,
PlainTextRole,
MatchItem
MatchItem,
LastMatchedRangeInFile,
};
Q_ENUM(MatchDataRoles)
......@@ -227,12 +228,6 @@ private:
QRegularExpression m_regExp;
QString m_replaceText;
bool m_cancelReplace = true;
// display related for current search
int m_maxLineNumFound = 0;
int m_maxColNumFound = 0;
friend class Results;
};
// tests
......
......@@ -133,11 +133,3 @@ bool Results::replaceSingleMatch(KTextEditor::Document *doc, const QModelIndex &
const auto sourceIndex = model()->mapToSource(matchIndex);
return matchModel.replaceSingleMatch(doc, sourceIndex, regExp, replaceString);
}
void Results::updateMaxLineNumTouched()
{
auto *delegate = static_cast<SPHtmlDelegate *>(treeView->itemDelegate());
int maxLineFound = matchModel.m_maxLineNumFound;
int maxColFound = matchModel.m_maxColNumFound;
delegate->setMaxLineCol(maxLineFound, maxColFound);
}
......@@ -37,9 +37,6 @@ public:
KTextEditor::Range matchRange(const QModelIndex &matchIndex) const;
bool replaceSingleMatch(KTextEditor::Document *doc, const QModelIndex &matchIndex, const QRegularExpression &regExp, const QString &replaceString);
// Tells the delegate about max line num found in any file. Used while painting
void updateMaxLineNumTouched();
Q_SIGNALS:
void colorsChanged();
};
......
......@@ -44,20 +44,22 @@ SPHtmlDelegate::SPHtmlDelegate(QObject *parent)
m_font = Utils::editorFont();
}
void SPHtmlDelegate::setMaxLineCol(int line, int col)
static int lineNumAreaWidth(const QModelIndex &index, const QFontMetrics &fm)
{
const QString s = QStringLiteral("%1:%2").arg(line).arg(col);
m_lineNumAreaWidth = QFontMetrics(m_font).horizontalAdvance(s);
const auto lastRangeForFile = index.parent().data(MatchModel::LastMatchedRangeInFile).value<KTextEditor::Range>();
const QString lineCol = QStringLiteral("%1:%2").arg(lastRangeForFile.start().line()).arg(lastRangeForFile.start().column());
return fm.horizontalAdvance(lineCol);
}
void SPHtmlDelegate::paintMatchItem(QPainter *p, const QStyleOptionViewItem &opt, const KateSearchMatch &match) const
void SPHtmlDelegate::paintMatchItem(QPainter *p, const QStyleOptionViewItem &opt, const QModelIndex &index) const
{
const KateSearchMatch match = index.data(MatchModel::MatchItem).value<KateSearchMatch>();
const int line = match.range.start().line() + 1;
const int col = match.range.start().column() + 1;
const QString lineCol = QStringLiteral("%1:%2").arg(line).arg(col);
QStyle *style = opt.widget->style() ? opt.widget->style() : qApp->style();
const auto fm = QFontMetrics(m_font);
const QFontMetrics fm(m_font);
static constexpr int hMargins = 2;
......@@ -75,7 +77,7 @@ void SPHtmlDelegate::paintMatchItem(QPainter *p, const QStyleOptionViewItem &opt
const bool selected = opt.state & QStyle::State_Selected;
const QBrush iconBorderRectColor = selected ? m_curLineHighlightColor : m_iconBorderColor;
const int lineColWidth = m_lineNumAreaWidth + (hMargins * 2);
const int lineColWidth = lineNumAreaWidth(index, fm) + (hMargins * 2);
if (rtl) {
iconBorderRect.setX(textRect.width() - lineColWidth);
}
......@@ -144,6 +146,11 @@ static bool isMatchItem(const QModelIndex &index)
void SPHtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (!index.isValid()) {
QStyledItemDelegate::paint(painter, option, index);
return;
}
QStyleOptionViewItem options = option;
initStyleOption(&options, index);
......@@ -152,8 +159,7 @@ void SPHtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option
options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
if (isMatchItem(index)) {
const auto item = index.data(MatchModel::MatchItem).value<KateSearchMatch>();
paintMatchItem(painter, options, item);
paintMatchItem(painter, options, index);
} else {
QTextDocument doc;
doc.setDefaultFont(m_font);
......
......@@ -10,8 +10,6 @@
#include <QFont>
#include <QStyledItemDelegate>
class KateSearchMatch;
class SPHtmlDelegate : public QStyledItemDelegate
{
Q_OBJECT
......@@ -22,13 +20,10 @@ public:
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setMaxLineCol(int line, int col);
private:
void paintMatchItem(QPainter *, const QStyleOptionViewItem &, const KateSearchMatch &) const;
void paintMatchItem(QPainter *, const QStyleOptionViewItem &, const QModelIndex &index) const;
QFont m_font;
int m_lineNumAreaWidth = 0;
QBrush m_curLineHighlightColor;
QBrush m_iconBorderColor;
QBrush m_borderColor;
......
......@@ -1275,9 +1275,6 @@ void KatePluginSearchView::searchDone()
m_ui.nextButton->setDisabled(m_curResults->matches < 1);
m_ui.filterBtn->setDisabled(m_curResults->matches <= 1);
// let the delegate know about max line num for painting line num area
m_curResults->updateMaxLineNumTouched();
// Set search to done. This sorts the model and collapses all items in the view
m_curResults->matchModel.setSearchState(MatchModel::SearchDone);
......@@ -1323,9 +1320,6 @@ void KatePluginSearchView::searchWhileTypingDone()
m_curResults->treeView->setColumnWidth(0, m_curResults->treeView->width() - 30);
}
// let the delegate know about max line num for painting line num area
m_curResults->updateMaxLineNumTouched();
// Set search to done. This sorts the model and collapses all items in the view
m_curResults->matchModel.setSearchState(MatchModel::SearchDone);
......
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