source: terepaima/terepaima-0.4.16/sources/searchitemdelegate.cpp

desarrollostretch
Last change on this file was 1f4adec, checked in by aosorio <aosorio@…>, 8 years ago

Agregado proyecto base, esto luego del dh_make -f

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2
3Copyright 2015 Adam Reichold
4
5This file is part of qpdfview.
6
7qpdfview is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 2 of the License, or
10(at your option) any later version.
11
12qpdfview is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.
19
20*/
21
22#include "searchitemdelegate.h"
23
24#include <QApplication>
25#include <qmath.h>
26#include <QPainter>
27#include <QTextLayout>
28
29#include "searchmodel.h"
30
31namespace qpdfview
32{
33
34SearchItemDelegate::SearchItemDelegate(QObject* parent) : QStyledItemDelegate(parent)
35{
36}
37
38void SearchItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
39{
40    QStyledItemDelegate::paint(painter, option, index);
41
42    const int progress = index.data(SearchModel::ProgressRole).toInt();
43
44    if(progress != 0)
45    {
46        paintProgress(painter, option, progress);
47        return;
48    }
49
50    const QString matchedText = index.data(SearchModel::MatchedTextRole).toString();
51    const QString surroundingText = index.data(SearchModel::SurroundingTextRole).toString();
52
53    if(!matchedText.isEmpty() && !surroundingText.isEmpty())
54    {
55        paintText(painter, option, matchedText, surroundingText);
56        return;
57    }
58}
59
60void SearchItemDelegate::paintProgress(QPainter* painter, const QStyleOptionViewItem& option,
61                                       int progress) const
62{
63    QRect highlightedRect = option.rect;
64    highlightedRect.setWidth(progress * highlightedRect.width() / 100);
65
66    painter->save();
67
68    painter->setCompositionMode(QPainter::CompositionMode_Multiply);
69    painter->fillRect(highlightedRect, option.palette.highlight());
70
71    painter->restore();
72}
73
74void SearchItemDelegate::paintText(QPainter* painter, const QStyleOptionViewItem& option,
75                                   const QString& matchedText, const QString& surroundingText) const
76{
77    const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
78    const QRect textRect = option.rect.adjusted(textMargin, 0, -textMargin, 0);
79    const QString elidedText = option.fontMetrics.elidedText(surroundingText, option.textElideMode, textRect.width());
80
81    QTextOption textOption;
82    textOption.setWrapMode(QTextOption::NoWrap);
83    textOption.setTextDirection(elidedText.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight);
84    textOption.setAlignment(QStyle::visualAlignment(textOption.textDirection(), option.displayAlignment));
85
86    QTextLayout textLayout;
87    textLayout.setTextOption(textOption);
88    textLayout.setText(elidedText);
89    textLayout.setFont(option.font);
90
91    QFont font = textLayout.font();
92    font.setWeight(QFont::Light);
93    textLayout.setFont(font);
94
95
96    QList< QTextLayout::FormatRange > additionalFormats;
97
98    for(int index = 0; (index = elidedText.indexOf(matchedText, index)) != -1; index += matchedText.length())
99    {
100        QTextLayout::FormatRange formatRange;
101        formatRange.start = index;
102        formatRange.length = matchedText.length();
103        formatRange.format.setFontWeight(QFont::Bold);
104
105        additionalFormats.append(formatRange);
106    }
107
108    textLayout.setAdditionalFormats(additionalFormats);
109
110
111    textLayout.beginLayout();
112
113    QTextLine textLine = textLayout.createLine();
114
115    if(!textLine.isValid())
116    {
117        return;
118    }
119
120    textLine.setLineWidth(textRect.width());
121
122    textLayout.endLayout();
123
124
125    const QSize layoutSize(textRect.width(), qFloor(textLine.height()));
126    const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment, layoutSize, textRect);
127
128    painter->save();
129
130    painter->setClipping(true);
131    painter->setClipRect(layoutRect);
132
133    textLine.draw(painter, layoutRect.topLeft());
134
135    painter->restore();
136}
137
138} // qpdfview
Note: See TracBrowser for help on using the repository browser.