2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
2014-10-01 13:21:18 +02:00
|
|
|
** conditions see http://www.qt.io/licensing. For further information
|
|
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "searchresulttreeitemdelegate.h"
|
|
|
|
|
#include "searchresulttreeitemroles.h"
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QApplication>
|
2011-01-24 12:29:48 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QModelIndex>
|
|
|
|
|
#include <QDebug>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
using namespace Core::Internal;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
SearchResultTreeItemDelegate::SearchResultTreeItemDelegate(QObject *parent)
|
2008-12-02 15:08:31 +01:00
|
|
|
: QItemDelegate(parent)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SearchResultTreeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
|
{
|
2010-07-29 16:33:18 +02:00
|
|
|
static const int iconSize = 16;
|
|
|
|
|
|
2010-07-19 14:46:53 +02:00
|
|
|
painter->save();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-07-19 14:46:53 +02:00
|
|
|
QStyleOptionViewItemV3 opt = setOptions(index, option);
|
|
|
|
|
painter->setFont(opt.font);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-07-19 14:46:53 +02:00
|
|
|
QItemDelegate::drawBackground(painter, opt, index);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-07-29 16:33:18 +02:00
|
|
|
// ---- do the layout
|
|
|
|
|
QRect checkRect;
|
|
|
|
|
QRect pixmapRect;
|
|
|
|
|
QRect textRect;
|
|
|
|
|
|
|
|
|
|
// check mark
|
|
|
|
|
bool checkable = (index.model()->flags(index) & Qt::ItemIsUserCheckable);
|
|
|
|
|
Qt::CheckState checkState = Qt::Unchecked;
|
|
|
|
|
if (checkable) {
|
|
|
|
|
QVariant checkStateData = index.data(Qt::CheckStateRole);
|
|
|
|
|
checkState = static_cast<Qt::CheckState>(checkStateData.toInt());
|
2012-01-13 10:51:13 +01:00
|
|
|
checkRect = doCheck(opt, opt.rect, checkStateData);
|
2010-07-29 16:33:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// icon
|
|
|
|
|
QIcon icon = index.model()->data(index, ItemDataRoles::ResultIconRole).value<QIcon>();
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (!icon.isNull())
|
2010-07-29 16:33:18 +02:00
|
|
|
pixmapRect = QRect(0, 0, iconSize, iconSize);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-07-29 16:33:18 +02:00
|
|
|
// text
|
|
|
|
|
textRect = opt.rect.adjusted(0, 0, checkRect.width() + pixmapRect.width(), 0);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-07-29 16:33:18 +02:00
|
|
|
// do layout
|
|
|
|
|
doLayout(opt, &checkRect, &pixmapRect, &textRect, false);
|
|
|
|
|
|
|
|
|
|
// ---- draw the items
|
|
|
|
|
// icon
|
|
|
|
|
if (!icon.isNull())
|
|
|
|
|
QItemDelegate::drawDecoration(painter, opt, pixmapRect, icon.pixmap(iconSize));
|
|
|
|
|
|
|
|
|
|
// line numbers
|
|
|
|
|
int lineNumberAreaWidth = drawLineNumber(painter, opt, textRect, index);
|
|
|
|
|
textRect.adjust(lineNumberAreaWidth, 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
// text and focus/selection
|
2012-10-18 08:02:25 +02:00
|
|
|
drawText(painter, opt, textRect, index);
|
2010-07-19 14:46:53 +02:00
|
|
|
QItemDelegate::drawFocus(painter, opt, opt.rect);
|
2009-09-29 18:16:13 +02:00
|
|
|
|
2010-07-29 16:33:18 +02:00
|
|
|
// check mark
|
|
|
|
|
if (checkable)
|
2010-07-19 14:46:53 +02:00
|
|
|
QItemDelegate::drawCheck(painter, opt, checkRect, checkState);
|
|
|
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-09 12:23:55 +01:00
|
|
|
// returns the width of the line number area
|
2008-12-02 12:01:29 +01:00
|
|
|
int SearchResultTreeItemDelegate::drawLineNumber(QPainter *painter, const QStyleOptionViewItemV3 &option,
|
2010-07-19 14:46:53 +02:00
|
|
|
const QRect &rect,
|
2009-04-08 14:32:31 +02:00
|
|
|
const QModelIndex &index) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
static const int lineNumberAreaHorizontalPadding = 4;
|
|
|
|
|
int lineNumber = index.model()->data(index, ItemDataRoles::ResultLineNumberRole).toInt();
|
2010-07-19 14:46:53 +02:00
|
|
|
if (lineNumber < 1)
|
|
|
|
|
return 0;
|
|
|
|
|
const bool isSelected = option.state & QStyle::State_Selected;
|
2012-02-09 12:23:55 +01:00
|
|
|
QString lineText = QString::number(lineNumber);
|
|
|
|
|
int minimumLineNumberDigits = qMax((int)m_minimumLineNumberDigits, lineText.count());
|
2010-06-25 20:17:00 +02:00
|
|
|
int fontWidth = painter->fontMetrics().width(QString(minimumLineNumberDigits, QLatin1Char('0')));
|
2008-12-02 12:01:29 +01:00
|
|
|
int lineNumberAreaWidth = lineNumberAreaHorizontalPadding + fontWidth + lineNumberAreaHorizontalPadding;
|
2010-07-19 14:46:53 +02:00
|
|
|
QRect lineNumberAreaRect(rect);
|
2008-12-02 12:01:29 +01:00
|
|
|
lineNumberAreaRect.setWidth(lineNumberAreaWidth);
|
|
|
|
|
|
|
|
|
|
QPalette::ColorGroup cg = QPalette::Normal;
|
|
|
|
|
if (!(option.state & QStyle::State_Active))
|
|
|
|
|
cg = QPalette::Inactive;
|
|
|
|
|
else if (!(option.state & QStyle::State_Enabled))
|
|
|
|
|
cg = QPalette::Disabled;
|
|
|
|
|
|
2009-04-08 14:32:31 +02:00
|
|
|
painter->fillRect(lineNumberAreaRect, QBrush(isSelected ?
|
2009-09-01 14:31:17 +02:00
|
|
|
option.palette.brush(cg, QPalette::Highlight) :
|
|
|
|
|
option.palette.color(cg, QPalette::Base).darker(111)));
|
2010-05-27 13:49:36 +02:00
|
|
|
|
|
|
|
|
QStyleOptionViewItemV3 opt = option;
|
|
|
|
|
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
|
|
|
|
|
opt.palette.setColor(cg, QPalette::Text, Qt::darkGray);
|
|
|
|
|
|
|
|
|
|
const QStyle *style = QApplication::style();
|
|
|
|
|
const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, 0) + 1;
|
|
|
|
|
|
|
|
|
|
const QRect rowRect = lineNumberAreaRect.adjusted(-textMargin, 0, textMargin-lineNumberAreaHorizontalPadding, 0);
|
2012-02-09 12:23:55 +01:00
|
|
|
QItemDelegate::drawDisplay(painter, opt, rowRect, lineText);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
return lineNumberAreaWidth;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-18 08:02:25 +02:00
|
|
|
void SearchResultTreeItemDelegate::drawText(QPainter *painter,
|
2013-11-15 10:06:51 +01:00
|
|
|
const QStyleOptionViewItem &option,
|
2012-10-18 08:02:25 +02:00
|
|
|
const QRect &rect,
|
|
|
|
|
const QModelIndex &index) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2012-10-18 08:02:25 +02:00
|
|
|
QString text = index.model()->data(index, Qt::DisplayRole).toString();
|
|
|
|
|
// show number of subresults in displayString
|
|
|
|
|
if (index.model()->hasChildren(index)) {
|
|
|
|
|
text += QLatin1String(" (")
|
|
|
|
|
+ QString::number(index.model()->rowCount(index))
|
|
|
|
|
+ QLatin1Char(')');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int searchTermStart = index.model()->data(index, ItemDataRoles::SearchTermStartRole).toInt();
|
2008-12-02 12:01:29 +01:00
|
|
|
int searchTermLength = index.model()->data(index, ItemDataRoles::SearchTermLengthRole).toInt();
|
2012-10-18 08:02:25 +02:00
|
|
|
if (searchTermStart < 0 || searchTermStart >= text.length() || searchTermLength < 1) {
|
2013-11-15 10:06:51 +01:00
|
|
|
QItemDelegate::drawDisplay(painter, option, rect, text);
|
2010-07-19 14:46:53 +02:00
|
|
|
return;
|
2012-10-18 08:02:25 +02:00
|
|
|
}
|
2011-10-27 14:25:18 +02:00
|
|
|
// clip searchTermLength to end of line
|
|
|
|
|
searchTermLength = qMin(searchTermLength, text.length() - searchTermStart);
|
2010-07-19 14:46:53 +02:00
|
|
|
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
|
|
|
|
|
int searchTermStartPixels = painter->fontMetrics().width(text.left(searchTermStart));
|
2008-12-02 12:01:29 +01:00
|
|
|
int searchTermLengthPixels = painter->fontMetrics().width(text.mid(searchTermStart, searchTermLength));
|
2012-10-18 08:02:25 +02:00
|
|
|
|
2013-11-15 10:06:51 +01:00
|
|
|
// rects
|
2012-10-18 08:02:25 +02:00
|
|
|
QRect beforeHighlightRect(rect);
|
|
|
|
|
beforeHighlightRect.setRight(beforeHighlightRect.left() + searchTermStartPixels);
|
2013-11-15 10:06:51 +01:00
|
|
|
|
|
|
|
|
QRect resultHighlightRect(rect);
|
|
|
|
|
resultHighlightRect.setLeft(beforeHighlightRect.right());
|
|
|
|
|
resultHighlightRect.setRight(resultHighlightRect.left() + searchTermLengthPixels);
|
|
|
|
|
|
|
|
|
|
QRect afterHighlightRect(rect);
|
|
|
|
|
afterHighlightRect.setLeft(resultHighlightRect.right());
|
|
|
|
|
|
|
|
|
|
// paint all highlight backgrounds
|
|
|
|
|
// qitemdelegate has problems with painting background when highlighted
|
|
|
|
|
// (highlighted background at wrong position because text is offset with textMargin)
|
|
|
|
|
// so we duplicate a lot here, see qitemdelegate for reference
|
|
|
|
|
bool isSelected = option.state & QStyle::State_Selected;
|
|
|
|
|
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
|
|
|
|
|
? QPalette::Normal : QPalette::Disabled;
|
|
|
|
|
if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
|
|
|
|
|
cg = QPalette::Inactive;
|
|
|
|
|
QStyleOptionViewItem baseOption = option;
|
|
|
|
|
baseOption.state &= ~QStyle::State_Selected;
|
|
|
|
|
if (isSelected) {
|
|
|
|
|
painter->fillRect(beforeHighlightRect.adjusted(textMargin, 0, textMargin, 0),
|
|
|
|
|
option.palette.brush(cg, QPalette::Highlight));
|
|
|
|
|
painter->fillRect(afterHighlightRect.adjusted(textMargin, 0, textMargin, 0),
|
|
|
|
|
option.palette.brush(cg, QPalette::Highlight));
|
|
|
|
|
}
|
|
|
|
|
const QColor highlightBackground =
|
|
|
|
|
index.model()->data(index, ItemDataRoles::ResultHighlightBackgroundColor).value<QColor>();
|
|
|
|
|
painter->fillRect(resultHighlightRect.adjusted(textMargin, 0, textMargin - 1, 0), QBrush(highlightBackground));
|
|
|
|
|
|
|
|
|
|
// Text before the highlighting
|
|
|
|
|
QStyleOptionViewItem noHighlightOpt = baseOption;
|
2012-10-18 08:02:25 +02:00
|
|
|
noHighlightOpt.rect = beforeHighlightRect;
|
|
|
|
|
noHighlightOpt.textElideMode = Qt::ElideNone;
|
2013-11-15 10:06:51 +01:00
|
|
|
if (isSelected)
|
|
|
|
|
noHighlightOpt.palette.setColor(QPalette::Text, noHighlightOpt.palette.color(cg, QPalette::HighlightedText));
|
2012-10-18 08:02:25 +02:00
|
|
|
QItemDelegate::drawDisplay(painter, noHighlightOpt,
|
|
|
|
|
beforeHighlightRect, text.mid(0, searchTermStart));
|
|
|
|
|
|
|
|
|
|
// Highlight text
|
|
|
|
|
QStyleOptionViewItem highlightOpt = noHighlightOpt;
|
|
|
|
|
const QColor highlightForeground =
|
|
|
|
|
index.model()->data(index, ItemDataRoles::ResultHighlightForegroundColor).value<QColor>();
|
|
|
|
|
highlightOpt.palette.setColor(QPalette::Text, highlightForeground);
|
|
|
|
|
QItemDelegate::drawDisplay(painter, highlightOpt, resultHighlightRect,
|
|
|
|
|
text.mid(searchTermStart, searchTermLength));
|
|
|
|
|
|
|
|
|
|
// Text after the Highlight
|
|
|
|
|
noHighlightOpt.rect = afterHighlightRect;
|
|
|
|
|
QItemDelegate::drawDisplay(painter, noHighlightOpt, afterHighlightRect,
|
|
|
|
|
text.mid(searchTermStart + searchTermLength));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|