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
=== modified file 'sources/mainwindow.cpp'
--- sources/mainwindow.cpp 2014-09-26 08:56:12 +0000
+++ sources/mainwindow.cpp 2014-09-28 13:18:11 +0000
@@ -431,7 +431,7 @@
431 m_bookmarksView->setModel(bookmarkModelForCurrentTab());431 m_bookmarksView->setModel(bookmarkModelForCurrentTab());
432 m_thumbnailsView->setScene(currentTab()->thumbnailsScene());432 m_thumbnailsView->setScene(currentTab()->thumbnailsScene());
433433
434 m_searchDelegate->setKeyword(currentTab()->searchModel()->lastPhrase());434 m_searchDelegate->setPhrase(currentTab()->searchModel()->lastPhrase());
435 m_searchView->setModel(currentTab()->searchModel());435 m_searchView->setModel(currentTab()->searchModel());
436436
437 on_currentTab_documentChanged();437 on_currentTab_documentChanged();
@@ -1100,7 +1100,7 @@
11001100
1101void MainWindow::on_cancelSearch_triggered()1101void MainWindow::on_cancelSearch_triggered()
1102{1102{
1103 m_searchDelegate->setKeyword(QString());1103 m_searchDelegate->setPhrase(QString());
11041104
1105 m_searchLineEdit->stopTimer();1105 m_searchLineEdit->stopTimer();
1106 m_searchLineEdit->setProgress(0);1106 m_searchLineEdit->setProgress(0);
@@ -1697,7 +1697,7 @@
1697{1697{
1698 if(!text.isEmpty())1698 if(!text.isEmpty())
1699 {1699 {
1700 m_searchDelegate->setKeyword(text);1700 m_searchDelegate->setPhrase(text);
17011701
1702 if(allTabs)1702 if(allTabs)
1703 {1703 {
@@ -1997,27 +1997,21 @@
19971997
1998void MainWindow::on_searchView_sectionCountChanged()1998void MainWindow::on_searchView_sectionCountChanged()
1999{1999{
2000 if(m_searchView->horizontalHeader()->count() > 0 && currentTab())2000 if(m_searchView->horizontalHeader()->count() > 0)
2001 {
2002 const int textMargin = m_searchView->style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
2003 m_searchView->setColumnWidth(0, 2 * textMargin + m_searchView->fontMetrics().width(QString::number(currentTab()->numberOfPages())));
2004 }
2005
2006 if(m_searchView->horizontalHeader()->count() > 1)
2007 {2001 {
2008#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)2002#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
20092003
2010 m_searchView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);2004 m_searchView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
20112005
2012#else2006#else
20132007
2014 m_searchView->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);2008 m_searchView->horizontalHeader()->setResizeMode(0, QHeaderView::ResizeToContents);
20152009
2016#endif // QT_VERSION2010#endif // QT_VERSION
2017 }2011 }
20182012
2019 m_searchView->horizontalHeader()->setMinimumSectionSize(0);2013 m_searchView->horizontalHeader()->setMinimumSectionSize(0);
2020 m_searchView->horizontalHeader()->setStretchLastSection(true);2014 m_searchView->horizontalHeader()->setStretchLastSection(false);
2021 m_searchView->horizontalHeader()->setVisible(false);2015 m_searchView->horizontalHeader()->setVisible(false);
20222016
2023 if(m_searchView->verticalHeader()->count() > 0)2017 if(m_searchView->verticalHeader()->count() > 0)
20242018
=== modified file 'sources/miscellaneous.cpp'
--- sources/miscellaneous.cpp 2014-09-25 14:49:35 +0000
+++ sources/miscellaneous.cpp 2014-09-28 13:18:11 +0000
@@ -487,133 +487,92 @@
487}487}
488488
489489
490SearchDelegate::SearchDelegate(QWidget* parent) : QStyledItemDelegate(parent)490SearchDelegate::SearchDelegate(QObject* parent) : QStyledItemDelegate(parent)
491{491{
492}492}
493493
494void SearchDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const494void SearchDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
495{495{
496 QStyledItemDelegate::paint(painter, option, index);
497
498 const QString text = index.data().toString();496 const QString text = index.data().toString();
499497
500 if (text.isEmpty())498 if(text.isEmpty())
501 {499 {
500 QStyledItemDelegate::paint(painter, option, index);
501
502 return;502 return;
503 }503 }
504504
505 const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;505 const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
506 QRect textRect = option.rect.adjusted(textMargin, 0, -textMargin, 0);506 const QRect textRect = option.rect.adjusted(textMargin, 0, -textMargin, 0);
507 QString elidedText = option.fontMetrics.elidedText(text, option.textElideMode, textRect.width());507 const QString elidedText = option.fontMetrics.elidedText(text, option.textElideMode, textRect.width());
508508
509 m_textOption.setWrapMode(QTextOption::NoWrap);509 QTextOption textOption;
510 m_textOption.setTextDirection(text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight);510 textOption.setWrapMode(QTextOption::NoWrap);
511 m_textOption.setAlignment(QStyle::visualAlignment(m_textOption.textDirection(), option.displayAlignment));511 textOption.setTextDirection(text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight);
512512 textOption.setAlignment(QStyle::visualAlignment(textOption.textDirection(), option.displayAlignment));
513 m_textLayout.setTextOption(m_textOption);513
514 m_textLayout.setFont(option.font);514 QTextLayout textLayout;
515 m_textLayout.setText(elidedText);515 textLayout.setTextOption(textOption);
516516 textLayout.setText(elidedText);
517 QSizeF textLayoutSize = doTextLayout(textRect.width());517 textLayout.setFont(option.font);
518518
519 if (m_textLayout.lineCount() <= 0) {519 emphasizePhrase(textLayout);
520
521 textLayout.beginLayout();
522
523 QTextLine textLine = textLayout.createLine();
524
525 if(!textLine.isValid())
526 {
520 return;527 return;
521 }528 }
522529
523 QTextLine textLine = m_textLayout.lineAt(0);530 textLine.setLineWidth(textRect.width());
524531
525 const QSize layoutSize(textRect.width(), int(textLayoutSize.height()));532 textLayout.endLayout();
533
534 const QSize layoutSize(textRect.width(), qFloor(textLine.height()));
526 const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment, layoutSize, textRect);535 const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment, layoutSize, textRect);
527 const QPointF &position = layoutRect.topLeft();
528536
529 painter->save();537 painter->save();
538
539 painter->setClipping(true);
530 painter->setClipRect(layoutRect);540 painter->setClipRect(layoutRect);
531 textLine.draw(painter, position);541
542 textLine.draw(painter, layoutRect.topLeft());
543
532 painter->restore();544 painter->restore();
533}545}
534546
535void SearchDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const547void SearchDelegate::emphasizePhrase(QTextLayout& textLayout) const
536{548{
537 QStyledItemDelegate::initStyleOption(option, index);549 if(m_phrase.isEmpty())
538
539 QStyleOptionViewItemV4* v4 = qstyleoption_cast< QStyleOptionViewItemV4* >(option);
540
541 if (v4)
542 {
543 v4->text.clear();
544 }
545}
546
547// taken and modified from qitemdelegate.cpp
548QSizeF SearchDelegate::doTextLayout(int lineWidth) const
549{
550 updateAdditionalFormats();
551
552 qreal height = 0;
553 qreal widthUsed = 0;
554
555 m_textLayout.beginLayout();
556
557 while (true)
558 {
559 QTextLine line = m_textLayout.createLine();
560
561 if (!line.isValid())
562 {
563 break;
564 }
565
566 line.setLineWidth(lineWidth);
567 line.setPosition(QPointF(0, height));
568 height += line.height();
569 widthUsed = qMax(widthUsed, line.naturalTextWidth());
570 }
571
572 m_textLayout.endLayout();
573
574 return QSizeF(widthUsed, height);
575}
576
577void SearchDelegate::updateAdditionalFormats() const
578{
579 m_additionalFormats.clear();
580 m_textLayout.clearAdditionalFormats();
581
582 QFont layoutFont(m_textLayout.font());
583 layoutFont.setWeight(QFont::Light);
584 m_textLayout.setFont(layoutFont);
585
586 const QString text = m_textLayout.text();
587
588 if (m_keyword.isEmpty())
589 {550 {
590 return;551 return;
591 }552 }
592553
593 int pos = 0;554 QFont font = textLayout.font();
594555 font.setWeight(QFont::Light);
595 while ((pos = text.indexOf(m_keyword, pos, Qt::CaseInsensitive)) != -1)556 textLayout.setFont(font);
596 {557
597 QTextLayout::FormatRange fr;558 QList< QTextLayout::FormatRange > additionalFormats;
598 fr.start = pos;559
599 fr.length = m_keyword.length();560 const QString text = textLayout.text();
600561 int position = 0;
601 pos += fr.length;562
602563 while((position = text.indexOf(m_phrase, position, Qt::CaseInsensitive)) != -1)
603 fr.format.setFontWeight(99);564 {
604565 QTextLayout::FormatRange formatRange;
605 m_additionalFormats << fr;566 formatRange.start = position;
606 }567 formatRange.length = m_phrase.length();
607568 formatRange.format.setFontWeight(QFont::Bold);
608 if (!m_additionalFormats.isEmpty())569
609 {570 additionalFormats.append(formatRange);
610 m_textLayout.setAdditionalFormats(m_additionalFormats);571
611 }572 position += m_phrase.length();
612}573 }
613574
614void SearchDelegate::setKeyword(const QString& text)575 textLayout.setAdditionalFormats(additionalFormats);
615{
616 m_keyword = text;
617}576}
618577
619} // qpdfview578} // qpdfview
620579
=== modified file 'sources/miscellaneous.h'
--- sources/miscellaneous.h 2014-09-25 14:49:35 +0000
+++ sources/miscellaneous.h 2014-09-28 13:18:11 +0000
@@ -279,30 +279,24 @@
279 int value = 0, int min = -2147483647, int max = 2147483647,279 int value = 0, int min = -2147483647, int max = 2147483647,
280 bool* ok = 0, Qt::WindowFlags flags = 0);280 bool* ok = 0, Qt::WindowFlags flags = 0);
281281
282// search delegate
282283
283class SearchDelegate : public QStyledItemDelegate284class SearchDelegate : public QStyledItemDelegate
284{285{
285 Q_OBJECT286 Q_OBJECT
286287
287public:288public:
288 SearchDelegate(QWidget* parent = 0);289 explicit SearchDelegate(QObject* parent = 0);
290
289 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;291 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
290292
291protected:293 inline QString phrase() const { return m_phrase; }
292 void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const;294 inline void setPhrase(const QString& phrase) { m_phrase = phrase; }
293295
294private:296private:
295 QSizeF doTextLayout(int lineWidth) const;297 QString m_phrase;
296 void updateAdditionalFormats() const;298
297299 void emphasizePhrase(QTextLayout& textLayout) const;
298 QString m_keyword;
299
300 mutable QTextLayout m_textLayout;
301 mutable QTextOption m_textOption;
302 mutable QList<QTextLayout::FormatRange> m_additionalFormats;
303
304public slots:
305 void setKeyword(const QString& text);
306300
307};301};
308302
309303
=== modified file 'sources/searchmodel.cpp'
--- sources/searchmodel.cpp 2014-09-26 08:56:12 +0000
+++ sources/searchmodel.cpp 2014-09-28 13:18:11 +0000
@@ -34,6 +34,11 @@
3434
35}35}
3636
37static uint qHash(const QRect& rect)
38{
39 return qHash(QString("%1%2%3%4").arg(rect.x()).arg(rect.y()).arg(rect.width()).arg(rect.height()));
40}
41
37namespace qpdfview42namespace qpdfview
38{43{
3944
@@ -88,7 +93,7 @@
8893
89int SearchModel::columnCount(const QModelIndex&) const94int SearchModel::columnCount(const QModelIndex&) const
90{95{
91 return 2;96 return 1;
92}97}
9398
94int SearchModel::rowCount(const QModelIndex& parent) const99int SearchModel::rowCount(const QModelIndex& parent) const
@@ -114,21 +119,14 @@
114 case RectRole:119 case RectRole:
115 return result.second;120 return result.second;
116 case Qt::DisplayRole:121 case Qt::DisplayRole:
117 return index.column() == 0 ? QString::number(result.first) : lineText(result.first, result.second);122 case Qt::ToolTipRole:
118 case ContainingLineRole:123 case ContainingLineRole:
119 case Qt::ToolTipRole:
120 return lineText(result.first, result.second);124 return lineText(result.first, result.second);
121
122 case Qt::TextAlignmentRole:125 case Qt::TextAlignmentRole:
123 return int(Qt::AlignLeft | Qt::AlignVCenter);126 return int(Qt::AlignLeft | Qt::AlignVCenter);
124 }127 }
125}128}
126129
127uint qHash(const QPair< int, QRect >& pair)
128{
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()));
130}
131
132QString SearchModel::lineText(int page, const QRectF& rect) const130QString SearchModel::lineText(int page, const QRectF& rect) const
133{131{
134 if (m_document == 0)132 if (m_document == 0)

Subscribers

People subscribed via source and target branches

to all changes: