OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / find / searchresulttreeview.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "searchresulttreeview.h"
34 #include "searchresulttreeitemroles.h"
35 #include "searchresulttreemodel.h"
36 #include "searchresulttreeitemdelegate.h"
37
38 #include <QtGui/QHeaderView>
39 #include <QtGui/QKeyEvent>
40
41 using namespace Find::Internal;
42
43 SearchResultTreeView::SearchResultTreeView(QWidget *parent)
44     : QTreeView(parent)
45     , m_model(new SearchResultTreeModel(this))
46     , m_autoExpandResults(false)
47 {
48     setModel(m_model);
49     setItemDelegate(new SearchResultTreeItemDelegate(this));
50     setIndentation(14);
51     setUniformRowHeights(true);
52     setExpandsOnDoubleClick(true);
53     header()->hide();
54
55     connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(emitJumpToSearchResult(QModelIndex)));
56 }
57
58 void SearchResultTreeView::setAutoExpandResults(bool expand)
59 {
60     m_autoExpandResults = expand;
61 }
62
63 void SearchResultTreeView::setTextEditorFont(const QFont &font)
64 {
65     m_model->setTextEditorFont(font);
66 }
67
68 void SearchResultTreeView::clear()
69 {
70     m_model->clear();
71 }
72
73 void SearchResultTreeView::addResults(const QList<Find::SearchResultItem> &items, Find::SearchResultWindow::AddMode mode)
74 {
75     QList<QModelIndex> addedParents = m_model->addResults(items, mode);
76     if (m_autoExpandResults && !addedParents.isEmpty()) {
77         foreach (const QModelIndex &index, addedParents)
78             setExpanded(index, true);
79     }
80 }
81
82 void SearchResultTreeView::emitJumpToSearchResult(const QModelIndex &index)
83 {
84     if (model()->data(index, ItemDataRoles::IsGeneratedRole).toBool())
85         return;
86     SearchResultItem item = model()->data(index, ItemDataRoles::ResultItemRole).value<SearchResultItem>();
87
88     emit jumpToSearchResult(item);
89 }
90
91 void SearchResultTreeView::keyPressEvent(QKeyEvent *e)
92 {
93     if (!e->modifiers() && e->key() == Qt::Key_Return) {
94         emit activated(currentIndex());
95         e->accept();
96         return;
97     }
98     QTreeView::keyPressEvent(e);
99 }
100
101 SearchResultTreeModel *SearchResultTreeView::model() const
102 {
103     return m_model;
104 }