summaryrefslogtreecommitdiffstats
path: root/doc/src/examples/treemodelcompleter.qdoc
blob: 82e9c24ead9959508e4fc0f022a64665ae40ad6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example tools/treemodelcompleter
    \title Tree Model Completer Example

    The Tree Model Completer example shows how to provide completion
    facilities for a hierarchical model, using a period as the separator
    to access Child, GrandChild and GrandGrandChild level objects.

    \image treemodelcompleter-example.png

    Similar to the \l{Completer Example}, we provide QComboBox objects to
    enable selection for completion mode and case sensitivity, as well as
    a QCheckBox for wrap completions.

    \section1 The Resource File

    The contents of the TreeModelCompleter is read from \e treemodel.txt.
    This file is embedded within the \e treemodelcompleter.qrc resource file,
    which contains the following:

    \quotefile examples/tools/treemodelcompleter/treemodelcompleter.qrc

    \section1 TreeModelCompleter Class Definition

    The \c TreeModelCompleter is a subclass of QCompleter with two
    constructors - one with \a parent as an argument and another with
    \a parent and \a model as arguments.

    \snippet examples/tools/treemodelcompleter/treemodelcompleter.h 0

    The class reimplements the protected functions
    \l{QCompleter::splitPath()}{splitPath()}  and
    \l{QCompleter::pathFromIndex()}{pathFromIndex()} to suit a tree model.
    For more information on customizing QCompleter to suit tree models, refer
    to \l{QCompleter#Handling Tree Models}{Handling Tree Models}.

    \c TreeModelCompleter also has a separator property which is declared
    using the Q_PROPERTY() macro. The separator has READ and WRITE attributes
    and the corresponding functions \c separator() and \c setSeparator(). For
    more information on Q_PROPERTY(), refer to \l{Qt's Property System}.

    \section1 TreeModelCompleter Class Implementation

    The first constructor constructs a \c TreeModelCompleter object with a
    parent while the second constructor constructs an object with a parent
    and a QAbstractItemModel, \a model.

    \snippet examples/tools/treemodelcompleter/treemodelcompleter.cpp 0
    \codeline
    \snippet examples/tools/treemodelcompleter/treemodelcompleter.cpp 1

    The \c separator() function is a getter function that returns the
    separator string.

    \snippet examples/tools/treemodelcompleter/treemodelcompleter.cpp 2

    As mentioned earlier, the \c splitPath() function is reimplemented because
    the default implementation is more suited to QDirModel or list models. In
    order for QCompleter to split the path into a list of strings that are
    matched at each level, we split it using QString::split() with \c sep as its
    separator.

    \snippet examples/tools/treemodelcompleter/treemodelcompleter.cpp 3

    The \c pathFromIndex() function returns data for the completionRole() for a
    tree model. This function is reimplemented as its default implementation is
    more suitable for list models. If there is no separator, we use
    \l{QCompleter}'s default implementation, otherwise we use the
    \l{QStringList::prepend()}{prepend()} function to navigate upwards and
    accumulate the data. The function then returns a QStringList, \c dataList,
    using a separator to join objects of different levels.

    \snippet examples/tools/treemodelcompleter/treemodelcompleter.cpp 4

    \section1 MainWindow Class Definition

    The \c MainWindow class is a subclass of QMainWindow and implements five
    custom slots: \c about(), \c changeCase(), \c changeMode(),
    \c highlight(), and \c updateContentsLabel().

    \snippet examples/tools/treemodelcompleter/mainwindow.h 0

    In addition, the class has two private functions, \c createMenu() and
    \c modelFromFile(), as well as private instances of QTreeView, QComboBox,
    QLabel, \c TreeModelCompleter and QLineEdit.

    \snippet examples/tools/treemodelcompleter/mainwindow.h 1

    \section1 MainWindow Class Implementation

    The \c{MainWindow}'s constructor creates a \c MainWindow object with a
    parent and initializes the \c completer and \c lineEdit. The
    \c createMenu() function is invoked to set up the "File" menu and "Help"
    menu. The \c{completer}'s model is set to the QAbstractItemModel obtained
    from \c modelFromFile(), and the \l{QCompleter::highlighted()}
    {highlighted()} signal is connected to \c{MainWindow}'s \c highlight()
    slot.

    \snippet examples/tools/treemodelcompleter/mainwindow.cpp 0

    The QLabel objects \c modelLabel, \c modeLabel and \c caseLabel are
    instantiated. Also, the QComboBox objects, \c modeCombo and \c caseCombo,
    are instantiated and populated. By default, the \c{completer}'s mode is
    "Filtered Popup" and the case is insensitive.

    \snippet examples/tools/treemodelcompleter/mainwindow.cpp 1
    \codeline
    \snippet examples/tools/treemodelcompleter/mainwindow.cpp 2

    We use a QGridLayout to place all the objects in the \c MainWindow.

    \snippet examples/tools/treemodelcompleter/mainwindow.cpp 3

    The \c createMenu() function sets up the QAction objects required and
    adds them to the "File" menu and "Help" menu. The
    \l{QAction::triggered()}{triggered()} signals from these actions are
    connected to their respective slots.

    \snippet examples/tools/treemodelcompleter/mainwindow.cpp 4

    The \c changeMode() function accepts an \a index corresponding to the
    user's choice of completion mode and changes the \c{completer}'s mode
    accordingly.

    \snippet examples/tools/treemodelcompleter/mainwindow.cpp 5

    The \c about() function provides a brief description on the Tree Model
    Completer example.

    \snippet examples/tools/treemodelcompleter/mainwindow.cpp 6

    The \c changeCase() function alternates between \l{Qt::CaseSensitive}
    {Case Sensitive} and \l{Qt::CaseInsensitive}{Case Insensitive} modes,
    depending on the value of \a cs.

    \snippet examples/tools/treemodelcompleter/mainwindow.cpp 7

    \section1 \c main() Function

    The \c main() function instantiates \c MainWindow and invokes the
    \l{QWidget::show()}{show()} function to display it.

    \snippet examples/tools/treemodelcompleter/main.cpp 0
*/