Merge lp:~adamreichold/qpdfview/extend-search-dock2 into lp:~srazi/qpdfview/extend-search-dock2
- extend-search-dock2
- Merge into 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 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Razi Alavizadeh | Approve | ||
Review via email: mp+236249@code.launchpad.net |
Commit message
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 | 431 | m_bookmarksView->setModel(bookmarkModelForCurrentTab()); | 431 | m_bookmarksView->setModel(bookmarkModelForCurrentTab()); |
6 | 432 | m_thumbnailsView->setScene(currentTab()->thumbnailsScene()); | 432 | m_thumbnailsView->setScene(currentTab()->thumbnailsScene()); |
7 | 433 | 433 | ||
9 | 434 | m_searchDelegate->setKeyword(currentTab()->searchModel()->lastPhrase()); | 434 | m_searchDelegate->setPhrase(currentTab()->searchModel()->lastPhrase()); |
10 | 435 | m_searchView->setModel(currentTab()->searchModel()); | 435 | m_searchView->setModel(currentTab()->searchModel()); |
11 | 436 | 436 | ||
12 | 437 | on_currentTab_documentChanged(); | 437 | on_currentTab_documentChanged(); |
13 | @@ -1100,7 +1100,7 @@ | |||
14 | 1100 | 1100 | ||
15 | 1101 | void MainWindow::on_cancelSearch_triggered() | 1101 | void MainWindow::on_cancelSearch_triggered() |
16 | 1102 | { | 1102 | { |
18 | 1103 | m_searchDelegate->setKeyword(QString()); | 1103 | m_searchDelegate->setPhrase(QString()); |
19 | 1104 | 1104 | ||
20 | 1105 | m_searchLineEdit->stopTimer(); | 1105 | m_searchLineEdit->stopTimer(); |
21 | 1106 | m_searchLineEdit->setProgress(0); | 1106 | m_searchLineEdit->setProgress(0); |
22 | @@ -1697,7 +1697,7 @@ | |||
23 | 1697 | { | 1697 | { |
24 | 1698 | if(!text.isEmpty()) | 1698 | if(!text.isEmpty()) |
25 | 1699 | { | 1699 | { |
27 | 1700 | m_searchDelegate->setKeyword(text); | 1700 | m_searchDelegate->setPhrase(text); |
28 | 1701 | 1701 | ||
29 | 1702 | if(allTabs) | 1702 | if(allTabs) |
30 | 1703 | { | 1703 | { |
31 | @@ -1997,27 +1997,21 @@ | |||
32 | 1997 | 1997 | ||
33 | 1998 | void MainWindow::on_searchView_sectionCountChanged() | 1998 | void MainWindow::on_searchView_sectionCountChanged() |
34 | 1999 | { | 1999 | { |
42 | 2000 | if(m_searchView->horizontalHeader()->count() > 0 && currentTab()) | 2000 | if(m_searchView->horizontalHeader()->count() > 0) |
36 | 2001 | { | ||
37 | 2002 | const int textMargin = m_searchView->style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; | ||
38 | 2003 | m_searchView->setColumnWidth(0, 2 * textMargin + m_searchView->fontMetrics().width(QString::number(currentTab()->numberOfPages()))); | ||
39 | 2004 | } | ||
40 | 2005 | |||
41 | 2006 | if(m_searchView->horizontalHeader()->count() > 1) | ||
43 | 2007 | { | 2001 | { |
44 | 2008 | #if QT_VERSION >= QT_VERSION_CHECK(5,0,0) | 2002 | #if QT_VERSION >= QT_VERSION_CHECK(5,0,0) |
45 | 2009 | 2003 | ||
47 | 2010 | m_searchView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); | 2004 | m_searchView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); |
48 | 2011 | 2005 | ||
49 | 2012 | #else | 2006 | #else |
50 | 2013 | 2007 | ||
52 | 2014 | m_searchView->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch); | 2008 | m_searchView->horizontalHeader()->setResizeMode(0, QHeaderView::ResizeToContents); |
53 | 2015 | 2009 | ||
54 | 2016 | #endif // QT_VERSION | 2010 | #endif // QT_VERSION |
55 | 2017 | } | 2011 | } |
56 | 2018 | 2012 | ||
57 | 2019 | m_searchView->horizontalHeader()->setMinimumSectionSize(0); | 2013 | m_searchView->horizontalHeader()->setMinimumSectionSize(0); |
59 | 2020 | m_searchView->horizontalHeader()->setStretchLastSection(true); | 2014 | m_searchView->horizontalHeader()->setStretchLastSection(false); |
60 | 2021 | m_searchView->horizontalHeader()->setVisible(false); | 2015 | m_searchView->horizontalHeader()->setVisible(false); |
61 | 2022 | 2016 | ||
62 | 2023 | if(m_searchView->verticalHeader()->count() > 0) | 2017 | if(m_searchView->verticalHeader()->count() > 0) |
63 | 2024 | 2018 | ||
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 | 487 | } | 487 | } |
69 | 488 | 488 | ||
70 | 489 | 489 | ||
72 | 490 | SearchDelegate::SearchDelegate(QWidget* parent) : QStyledItemDelegate(parent) | 490 | SearchDelegate::SearchDelegate(QObject* parent) : QStyledItemDelegate(parent) |
73 | 491 | { | 491 | { |
74 | 492 | } | 492 | } |
75 | 493 | 493 | ||
76 | 494 | void SearchDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const | 494 | void SearchDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const |
77 | 495 | { | 495 | { |
78 | 496 | QStyledItemDelegate::paint(painter, option, index); | ||
79 | 497 | |||
80 | 498 | const QString text = index.data().toString(); | 496 | const QString text = index.data().toString(); |
81 | 499 | 497 | ||
83 | 500 | if (text.isEmpty()) | 498 | if(text.isEmpty()) |
84 | 501 | { | 499 | { |
85 | 500 | QStyledItemDelegate::paint(painter, option, index); | ||
86 | 501 | |||
87 | 502 | return; | 502 | return; |
88 | 503 | } | 503 | } |
89 | 504 | 504 | ||
90 | 505 | const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; | 505 | const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; |
105 | 506 | QRect textRect = option.rect.adjusted(textMargin, 0, -textMargin, 0); | 506 | const QRect textRect = option.rect.adjusted(textMargin, 0, -textMargin, 0); |
106 | 507 | QString elidedText = option.fontMetrics.elidedText(text, option.textElideMode, textRect.width()); | 507 | const QString elidedText = option.fontMetrics.elidedText(text, option.textElideMode, textRect.width()); |
107 | 508 | 508 | ||
108 | 509 | m_textOption.setWrapMode(QTextOption::NoWrap); | 509 | QTextOption textOption; |
109 | 510 | m_textOption.setTextDirection(text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight); | 510 | textOption.setWrapMode(QTextOption::NoWrap); |
110 | 511 | m_textOption.setAlignment(QStyle::visualAlignment(m_textOption.textDirection(), option.displayAlignment)); | 511 | textOption.setTextDirection(text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight); |
111 | 512 | 512 | textOption.setAlignment(QStyle::visualAlignment(textOption.textDirection(), option.displayAlignment)); | |
112 | 513 | m_textLayout.setTextOption(m_textOption); | 513 | |
113 | 514 | m_textLayout.setFont(option.font); | 514 | QTextLayout textLayout; |
114 | 515 | m_textLayout.setText(elidedText); | 515 | textLayout.setTextOption(textOption); |
115 | 516 | 516 | textLayout.setText(elidedText); | |
116 | 517 | QSizeF textLayoutSize = doTextLayout(textRect.width()); | 517 | textLayout.setFont(option.font); |
117 | 518 | 518 | ||
118 | 519 | if (m_textLayout.lineCount() <= 0) { | 519 | emphasizePhrase(textLayout); |
119 | 520 | |||
120 | 521 | textLayout.beginLayout(); | ||
121 | 522 | |||
122 | 523 | QTextLine textLine = textLayout.createLine(); | ||
123 | 524 | |||
124 | 525 | if(!textLine.isValid()) | ||
125 | 526 | { | ||
126 | 520 | return; | 527 | return; |
127 | 521 | } | 528 | } |
128 | 522 | 529 | ||
132 | 523 | QTextLine textLine = m_textLayout.lineAt(0); | 530 | textLine.setLineWidth(textRect.width()); |
133 | 524 | 531 | ||
134 | 525 | const QSize layoutSize(textRect.width(), int(textLayoutSize.height())); | 532 | textLayout.endLayout(); |
135 | 533 | |||
136 | 534 | const QSize layoutSize(textRect.width(), qFloor(textLine.height())); | ||
137 | 526 | const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment, layoutSize, textRect); | 535 | const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment, layoutSize, textRect); |
138 | 527 | const QPointF &position = layoutRect.topLeft(); | ||
139 | 528 | 536 | ||
140 | 529 | painter->save(); | 537 | painter->save(); |
141 | 538 | |||
142 | 539 | painter->setClipping(true); | ||
143 | 530 | painter->setClipRect(layoutRect); | 540 | painter->setClipRect(layoutRect); |
145 | 531 | textLine.draw(painter, position); | 541 | |
146 | 542 | textLine.draw(painter, layoutRect.topLeft()); | ||
147 | 543 | |||
148 | 532 | painter->restore(); | 544 | painter->restore(); |
149 | 533 | } | 545 | } |
150 | 534 | 546 | ||
205 | 535 | void SearchDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const | 547 | void SearchDelegate::emphasizePhrase(QTextLayout& textLayout) const |
206 | 536 | { | 548 | { |
207 | 537 | QStyledItemDelegate::initStyleOption(option, index); | 549 | if(m_phrase.isEmpty()) |
154 | 538 | |||
155 | 539 | QStyleOptionViewItemV4* v4 = qstyleoption_cast< QStyleOptionViewItemV4* >(option); | ||
156 | 540 | |||
157 | 541 | if (v4) | ||
158 | 542 | { | ||
159 | 543 | v4->text.clear(); | ||
160 | 544 | } | ||
161 | 545 | } | ||
162 | 546 | |||
163 | 547 | // taken and modified from qitemdelegate.cpp | ||
164 | 548 | QSizeF SearchDelegate::doTextLayout(int lineWidth) const | ||
165 | 549 | { | ||
166 | 550 | updateAdditionalFormats(); | ||
167 | 551 | |||
168 | 552 | qreal height = 0; | ||
169 | 553 | qreal widthUsed = 0; | ||
170 | 554 | |||
171 | 555 | m_textLayout.beginLayout(); | ||
172 | 556 | |||
173 | 557 | while (true) | ||
174 | 558 | { | ||
175 | 559 | QTextLine line = m_textLayout.createLine(); | ||
176 | 560 | |||
177 | 561 | if (!line.isValid()) | ||
178 | 562 | { | ||
179 | 563 | break; | ||
180 | 564 | } | ||
181 | 565 | |||
182 | 566 | line.setLineWidth(lineWidth); | ||
183 | 567 | line.setPosition(QPointF(0, height)); | ||
184 | 568 | height += line.height(); | ||
185 | 569 | widthUsed = qMax(widthUsed, line.naturalTextWidth()); | ||
186 | 570 | } | ||
187 | 571 | |||
188 | 572 | m_textLayout.endLayout(); | ||
189 | 573 | |||
190 | 574 | return QSizeF(widthUsed, height); | ||
191 | 575 | } | ||
192 | 576 | |||
193 | 577 | void SearchDelegate::updateAdditionalFormats() const | ||
194 | 578 | { | ||
195 | 579 | m_additionalFormats.clear(); | ||
196 | 580 | m_textLayout.clearAdditionalFormats(); | ||
197 | 581 | |||
198 | 582 | QFont layoutFont(m_textLayout.font()); | ||
199 | 583 | layoutFont.setWeight(QFont::Light); | ||
200 | 584 | m_textLayout.setFont(layoutFont); | ||
201 | 585 | |||
202 | 586 | const QString text = m_textLayout.text(); | ||
203 | 587 | |||
204 | 588 | if (m_keyword.isEmpty()) | ||
208 | 589 | { | 550 | { |
209 | 590 | return; | 551 | return; |
210 | 591 | } | 552 | } |
211 | 592 | 553 | ||
236 | 593 | int pos = 0; | 554 | QFont font = textLayout.font(); |
237 | 594 | 555 | font.setWeight(QFont::Light); | |
238 | 595 | while ((pos = text.indexOf(m_keyword, pos, Qt::CaseInsensitive)) != -1) | 556 | textLayout.setFont(font); |
239 | 596 | { | 557 | |
240 | 597 | QTextLayout::FormatRange fr; | 558 | QList< QTextLayout::FormatRange > additionalFormats; |
241 | 598 | fr.start = pos; | 559 | |
242 | 599 | fr.length = m_keyword.length(); | 560 | const QString text = textLayout.text(); |
243 | 600 | 561 | int position = 0; | |
244 | 601 | pos += fr.length; | 562 | |
245 | 602 | 563 | while((position = text.indexOf(m_phrase, position, Qt::CaseInsensitive)) != -1) | |
246 | 603 | fr.format.setFontWeight(99); | 564 | { |
247 | 604 | 565 | QTextLayout::FormatRange formatRange; | |
248 | 605 | m_additionalFormats << fr; | 566 | formatRange.start = position; |
249 | 606 | } | 567 | formatRange.length = m_phrase.length(); |
250 | 607 | 568 | formatRange.format.setFontWeight(QFont::Bold); | |
251 | 608 | if (!m_additionalFormats.isEmpty()) | 569 | |
252 | 609 | { | 570 | additionalFormats.append(formatRange); |
253 | 610 | m_textLayout.setAdditionalFormats(m_additionalFormats); | 571 | |
254 | 611 | } | 572 | position += m_phrase.length(); |
255 | 612 | } | 573 | } |
256 | 613 | 574 | ||
257 | 614 | void SearchDelegate::setKeyword(const QString& text) | 575 | textLayout.setAdditionalFormats(additionalFormats); |
234 | 615 | { | ||
235 | 616 | m_keyword = text; | ||
258 | 617 | } | 576 | } |
259 | 618 | 577 | ||
260 | 619 | } // qpdfview | 578 | } // qpdfview |
261 | 620 | 579 | ||
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 | 279 | int value = 0, int min = -2147483647, int max = 2147483647, | 279 | int value = 0, int min = -2147483647, int max = 2147483647, |
267 | 280 | bool* ok = 0, Qt::WindowFlags flags = 0); | 280 | bool* ok = 0, Qt::WindowFlags flags = 0); |
268 | 281 | 281 | ||
269 | 282 | // search delegate | ||
270 | 282 | 283 | ||
271 | 283 | class SearchDelegate : public QStyledItemDelegate | 284 | class SearchDelegate : public QStyledItemDelegate |
272 | 284 | { | 285 | { |
273 | 285 | Q_OBJECT | 286 | Q_OBJECT |
274 | 286 | 287 | ||
275 | 287 | public: | 288 | public: |
277 | 288 | SearchDelegate(QWidget* parent = 0); | 289 | explicit SearchDelegate(QObject* parent = 0); |
278 | 290 | |||
279 | 289 | void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; | 291 | void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; |
280 | 290 | 292 | ||
283 | 291 | protected: | 293 | inline QString phrase() const { return m_phrase; } |
284 | 292 | void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const; | 294 | inline void setPhrase(const QString& phrase) { m_phrase = phrase; } |
285 | 293 | 295 | ||
286 | 294 | private: | 296 | private: |
298 | 295 | QSizeF doTextLayout(int lineWidth) const; | 297 | QString m_phrase; |
299 | 296 | void updateAdditionalFormats() const; | 298 | |
300 | 297 | 299 | void emphasizePhrase(QTextLayout& textLayout) const; | |
290 | 298 | QString m_keyword; | ||
291 | 299 | |||
292 | 300 | mutable QTextLayout m_textLayout; | ||
293 | 301 | mutable QTextOption m_textOption; | ||
294 | 302 | mutable QList<QTextLayout::FormatRange> m_additionalFormats; | ||
295 | 303 | |||
296 | 304 | public slots: | ||
297 | 305 | void setKeyword(const QString& text); | ||
301 | 306 | 300 | ||
302 | 307 | }; | 301 | }; |
303 | 308 | 302 | ||
304 | 309 | 303 | ||
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 | 34 | 34 | ||
310 | 35 | } | 35 | } |
311 | 36 | 36 | ||
312 | 37 | static uint qHash(const QRect& rect) | ||
313 | 38 | { | ||
314 | 39 | return qHash(QString("%1%2%3%4").arg(rect.x()).arg(rect.y()).arg(rect.width()).arg(rect.height())); | ||
315 | 40 | } | ||
316 | 41 | |||
317 | 37 | namespace qpdfview | 42 | namespace qpdfview |
318 | 38 | { | 43 | { |
319 | 39 | 44 | ||
320 | @@ -88,7 +93,7 @@ | |||
321 | 88 | 93 | ||
322 | 89 | int SearchModel::columnCount(const QModelIndex&) const | 94 | int SearchModel::columnCount(const QModelIndex&) const |
323 | 90 | { | 95 | { |
325 | 91 | return 2; | 96 | return 1; |
326 | 92 | } | 97 | } |
327 | 93 | 98 | ||
328 | 94 | int SearchModel::rowCount(const QModelIndex& parent) const | 99 | int SearchModel::rowCount(const QModelIndex& parent) const |
329 | @@ -114,21 +119,14 @@ | |||
330 | 114 | case RectRole: | 119 | case RectRole: |
331 | 115 | return result.second; | 120 | return result.second; |
332 | 116 | case Qt::DisplayRole: | 121 | case Qt::DisplayRole: |
334 | 117 | return index.column() == 0 ? QString::number(result.first) : lineText(result.first, result.second); | 122 | case Qt::ToolTipRole: |
335 | 118 | case ContainingLineRole: | 123 | case ContainingLineRole: |
336 | 119 | case Qt::ToolTipRole: | ||
337 | 120 | return lineText(result.first, result.second); | 124 | return lineText(result.first, result.second); |
338 | 121 | |||
339 | 122 | case Qt::TextAlignmentRole: | 125 | case Qt::TextAlignmentRole: |
340 | 123 | return int(Qt::AlignLeft | Qt::AlignVCenter); | 126 | return int(Qt::AlignLeft | Qt::AlignVCenter); |
341 | 124 | } | 127 | } |
342 | 125 | } | 128 | } |
343 | 126 | 129 | ||
344 | 127 | uint qHash(const QPair< int, QRect >& pair) | ||
345 | 128 | { | ||
346 | 129 | 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 | 130 | } | ||
348 | 131 | |||
349 | 132 | QString SearchModel::lineText(int page, const QRectF& rect) const | 130 | QString SearchModel::lineText(int page, const QRectF& rect) const |
350 | 133 | { | 131 | { |
351 | 134 | if (m_document == 0) | 132 | if (m_document == 0) |