Merge lp:~adamreichold/qpdfview/extend-search-dock2 into lp:~srazi/qpdfview/extend-search-dock2

Proposed by Adam Reichold
Status: Merged
Merged at revision: 1707
Proposed branch: lp:~adamreichold/qpdfview/extend-search-dock2
Merge into: lp:~srazi/qpdfview/extend-search-dock2
Diff against target: 351 lines (+82/-137)
4 files modified
sources/mainwindow.cpp (+7/-13)
sources/miscellaneous.cpp (+60/-101)
sources/miscellaneous.h (+8/-14)
sources/searchmodel.cpp (+7/-9)
To merge this branch: bzr merge lp:~adamreichold/qpdfview/extend-search-dock2
Reviewer Review Type Date Requested Status
Razi Alavizadeh Approve
Review via email: mp+236249@code.launchpad.net

Description of the change

Fixes a compilation problem using GCC, reduces the amount of boiler plate code in the search delegate and removes this first column in the search model.

To post a comment you must log in.
1710. By Adam Reichold <email address hidden>

Add a missing const and improve method naming.

Revision history for this message
Razi Alavizadeh (srazi) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'sources/mainwindow.cpp'
2--- sources/mainwindow.cpp 2014-09-26 08:56:12 +0000
3+++ sources/mainwindow.cpp 2014-09-28 13:18:11 +0000
4@@ -431,7 +431,7 @@
5 m_bookmarksView->setModel(bookmarkModelForCurrentTab());
6 m_thumbnailsView->setScene(currentTab()->thumbnailsScene());
7
8- m_searchDelegate->setKeyword(currentTab()->searchModel()->lastPhrase());
9+ m_searchDelegate->setPhrase(currentTab()->searchModel()->lastPhrase());
10 m_searchView->setModel(currentTab()->searchModel());
11
12 on_currentTab_documentChanged();
13@@ -1100,7 +1100,7 @@
14
15 void MainWindow::on_cancelSearch_triggered()
16 {
17- m_searchDelegate->setKeyword(QString());
18+ m_searchDelegate->setPhrase(QString());
19
20 m_searchLineEdit->stopTimer();
21 m_searchLineEdit->setProgress(0);
22@@ -1697,7 +1697,7 @@
23 {
24 if(!text.isEmpty())
25 {
26- m_searchDelegate->setKeyword(text);
27+ m_searchDelegate->setPhrase(text);
28
29 if(allTabs)
30 {
31@@ -1997,27 +1997,21 @@
32
33 void MainWindow::on_searchView_sectionCountChanged()
34 {
35- if(m_searchView->horizontalHeader()->count() > 0 && currentTab())
36- {
37- const int textMargin = m_searchView->style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
38- m_searchView->setColumnWidth(0, 2 * textMargin + m_searchView->fontMetrics().width(QString::number(currentTab()->numberOfPages())));
39- }
40-
41- if(m_searchView->horizontalHeader()->count() > 1)
42+ if(m_searchView->horizontalHeader()->count() > 0)
43 {
44 #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
45
46- m_searchView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
47+ m_searchView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
48
49 #else
50
51- m_searchView->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
52+ m_searchView->horizontalHeader()->setResizeMode(0, QHeaderView::ResizeToContents);
53
54 #endif // QT_VERSION
55 }
56
57 m_searchView->horizontalHeader()->setMinimumSectionSize(0);
58- m_searchView->horizontalHeader()->setStretchLastSection(true);
59+ m_searchView->horizontalHeader()->setStretchLastSection(false);
60 m_searchView->horizontalHeader()->setVisible(false);
61
62 if(m_searchView->verticalHeader()->count() > 0)
63
64=== modified file 'sources/miscellaneous.cpp'
65--- sources/miscellaneous.cpp 2014-09-25 14:49:35 +0000
66+++ sources/miscellaneous.cpp 2014-09-28 13:18:11 +0000
67@@ -487,133 +487,92 @@
68 }
69
70
71-SearchDelegate::SearchDelegate(QWidget* parent) : QStyledItemDelegate(parent)
72+SearchDelegate::SearchDelegate(QObject* parent) : QStyledItemDelegate(parent)
73 {
74 }
75
76 void SearchDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
77 {
78- QStyledItemDelegate::paint(painter, option, index);
79-
80 const QString text = index.data().toString();
81
82- if (text.isEmpty())
83+ if(text.isEmpty())
84 {
85+ QStyledItemDelegate::paint(painter, option, index);
86+
87 return;
88 }
89
90 const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
91- QRect textRect = option.rect.adjusted(textMargin, 0, -textMargin, 0);
92- QString elidedText = option.fontMetrics.elidedText(text, option.textElideMode, textRect.width());
93-
94- m_textOption.setWrapMode(QTextOption::NoWrap);
95- m_textOption.setTextDirection(text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight);
96- m_textOption.setAlignment(QStyle::visualAlignment(m_textOption.textDirection(), option.displayAlignment));
97-
98- m_textLayout.setTextOption(m_textOption);
99- m_textLayout.setFont(option.font);
100- m_textLayout.setText(elidedText);
101-
102- QSizeF textLayoutSize = doTextLayout(textRect.width());
103-
104- if (m_textLayout.lineCount() <= 0) {
105+ const QRect textRect = option.rect.adjusted(textMargin, 0, -textMargin, 0);
106+ const QString elidedText = option.fontMetrics.elidedText(text, option.textElideMode, textRect.width());
107+
108+ QTextOption textOption;
109+ textOption.setWrapMode(QTextOption::NoWrap);
110+ textOption.setTextDirection(text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight);
111+ textOption.setAlignment(QStyle::visualAlignment(textOption.textDirection(), option.displayAlignment));
112+
113+ QTextLayout textLayout;
114+ textLayout.setTextOption(textOption);
115+ textLayout.setText(elidedText);
116+ textLayout.setFont(option.font);
117+
118+ emphasizePhrase(textLayout);
119+
120+ textLayout.beginLayout();
121+
122+ QTextLine textLine = textLayout.createLine();
123+
124+ if(!textLine.isValid())
125+ {
126 return;
127 }
128
129- QTextLine textLine = m_textLayout.lineAt(0);
130-
131- const QSize layoutSize(textRect.width(), int(textLayoutSize.height()));
132+ textLine.setLineWidth(textRect.width());
133+
134+ textLayout.endLayout();
135+
136+ const QSize layoutSize(textRect.width(), qFloor(textLine.height()));
137 const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment, layoutSize, textRect);
138- const QPointF &position = layoutRect.topLeft();
139
140 painter->save();
141+
142+ painter->setClipping(true);
143 painter->setClipRect(layoutRect);
144- textLine.draw(painter, position);
145+
146+ textLine.draw(painter, layoutRect.topLeft());
147+
148 painter->restore();
149 }
150
151-void SearchDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const
152-{
153- QStyledItemDelegate::initStyleOption(option, index);
154-
155- QStyleOptionViewItemV4* v4 = qstyleoption_cast< QStyleOptionViewItemV4* >(option);
156-
157- if (v4)
158- {
159- v4->text.clear();
160- }
161-}
162-
163-// taken and modified from qitemdelegate.cpp
164-QSizeF SearchDelegate::doTextLayout(int lineWidth) const
165-{
166- updateAdditionalFormats();
167-
168- qreal height = 0;
169- qreal widthUsed = 0;
170-
171- m_textLayout.beginLayout();
172-
173- while (true)
174- {
175- QTextLine line = m_textLayout.createLine();
176-
177- if (!line.isValid())
178- {
179- break;
180- }
181-
182- line.setLineWidth(lineWidth);
183- line.setPosition(QPointF(0, height));
184- height += line.height();
185- widthUsed = qMax(widthUsed, line.naturalTextWidth());
186- }
187-
188- m_textLayout.endLayout();
189-
190- return QSizeF(widthUsed, height);
191-}
192-
193-void SearchDelegate::updateAdditionalFormats() const
194-{
195- m_additionalFormats.clear();
196- m_textLayout.clearAdditionalFormats();
197-
198- QFont layoutFont(m_textLayout.font());
199- layoutFont.setWeight(QFont::Light);
200- m_textLayout.setFont(layoutFont);
201-
202- const QString text = m_textLayout.text();
203-
204- if (m_keyword.isEmpty())
205+void SearchDelegate::emphasizePhrase(QTextLayout& textLayout) const
206+{
207+ if(m_phrase.isEmpty())
208 {
209 return;
210 }
211
212- int pos = 0;
213-
214- while ((pos = text.indexOf(m_keyword, pos, Qt::CaseInsensitive)) != -1)
215- {
216- QTextLayout::FormatRange fr;
217- fr.start = pos;
218- fr.length = m_keyword.length();
219-
220- pos += fr.length;
221-
222- fr.format.setFontWeight(99);
223-
224- m_additionalFormats << fr;
225- }
226-
227- if (!m_additionalFormats.isEmpty())
228- {
229- m_textLayout.setAdditionalFormats(m_additionalFormats);
230- }
231-}
232-
233-void SearchDelegate::setKeyword(const QString& text)
234-{
235- m_keyword = text;
236+ QFont font = textLayout.font();
237+ font.setWeight(QFont::Light);
238+ textLayout.setFont(font);
239+
240+ QList< QTextLayout::FormatRange > additionalFormats;
241+
242+ const QString text = textLayout.text();
243+ int position = 0;
244+
245+ while((position = text.indexOf(m_phrase, position, Qt::CaseInsensitive)) != -1)
246+ {
247+ QTextLayout::FormatRange formatRange;
248+ formatRange.start = position;
249+ formatRange.length = m_phrase.length();
250+ formatRange.format.setFontWeight(QFont::Bold);
251+
252+ additionalFormats.append(formatRange);
253+
254+ position += m_phrase.length();
255+ }
256+
257+ textLayout.setAdditionalFormats(additionalFormats);
258 }
259
260 } // qpdfview
261
262=== modified file 'sources/miscellaneous.h'
263--- sources/miscellaneous.h 2014-09-25 14:49:35 +0000
264+++ sources/miscellaneous.h 2014-09-28 13:18:11 +0000
265@@ -279,30 +279,24 @@
266 int value = 0, int min = -2147483647, int max = 2147483647,
267 bool* ok = 0, Qt::WindowFlags flags = 0);
268
269+// search delegate
270
271 class SearchDelegate : public QStyledItemDelegate
272 {
273 Q_OBJECT
274
275 public:
276- SearchDelegate(QWidget* parent = 0);
277+ explicit SearchDelegate(QObject* parent = 0);
278+
279 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
280
281-protected:
282- void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const;
283+ inline QString phrase() const { return m_phrase; }
284+ inline void setPhrase(const QString& phrase) { m_phrase = phrase; }
285
286 private:
287- QSizeF doTextLayout(int lineWidth) const;
288- void updateAdditionalFormats() const;
289-
290- QString m_keyword;
291-
292- mutable QTextLayout m_textLayout;
293- mutable QTextOption m_textOption;
294- mutable QList<QTextLayout::FormatRange> m_additionalFormats;
295-
296-public slots:
297- void setKeyword(const QString& text);
298+ QString m_phrase;
299+
300+ void emphasizePhrase(QTextLayout& textLayout) const;
301
302 };
303
304
305=== modified file 'sources/searchmodel.cpp'
306--- sources/searchmodel.cpp 2014-09-26 08:56:12 +0000
307+++ sources/searchmodel.cpp 2014-09-28 13:18:11 +0000
308@@ -34,6 +34,11 @@
309
310 }
311
312+static uint qHash(const QRect& rect)
313+{
314+ return qHash(QString("%1%2%3%4").arg(rect.x()).arg(rect.y()).arg(rect.width()).arg(rect.height()));
315+}
316+
317 namespace qpdfview
318 {
319
320@@ -88,7 +93,7 @@
321
322 int SearchModel::columnCount(const QModelIndex&) const
323 {
324- return 2;
325+ return 1;
326 }
327
328 int SearchModel::rowCount(const QModelIndex& parent) const
329@@ -114,21 +119,14 @@
330 case RectRole:
331 return result.second;
332 case Qt::DisplayRole:
333- return index.column() == 0 ? QString::number(result.first) : lineText(result.first, result.second);
334+ case Qt::ToolTipRole:
335 case ContainingLineRole:
336- case Qt::ToolTipRole:
337 return lineText(result.first, result.second);
338-
339 case Qt::TextAlignmentRole:
340 return int(Qt::AlignLeft | Qt::AlignVCenter);
341 }
342 }
343
344-uint qHash(const QPair< int, QRect >& pair)
345-{
346- return qHash(QString("%1%2%3%4%5").arg(pair.first).arg(pair.second.x()).arg(pair.second.y()).arg(pair.second.width()).arg(pair.second.height()));
347-}
348-
349 QString SearchModel::lineText(int page, const QRectF& rect) const
350 {
351 if (m_document == 0)

Subscribers

People subscribed via source and target branches

to all changes: