1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include <QtWidgets>
52 
53 #include "mainwindow.h"
54 
MainWindow()55 MainWindow::MainWindow()
56 {
57     QMenu *fileMenu = new QMenu(tr("&File"));
58 
59     QAction *quitAction = fileMenu->addAction(tr("E&xit"));
60     quitAction->setShortcut(tr("Ctrl+Q"));
61 
62     QMenu *itemsMenu = new QMenu(tr("&Items"));
63 
64     insertAction = itemsMenu->addAction(tr("&Insert Item"));
65     removeAction = itemsMenu->addAction(tr("&Remove Item"));
66     QAction *ascendingAction = itemsMenu->addAction(tr("Sort in &Ascending Order"));
67     QAction *descendingAction = itemsMenu->addAction(tr("Sort in &Descending Order"));
68 
69     menuBar()->addMenu(fileMenu);
70     menuBar()->addMenu(itemsMenu);
71 
72 /*  For convenient quoting:
73 //! [0]
74     QListWidget *listWidget = new QListWidget(this);
75 //! [0]
76 */
77     listWidget = new QListWidget(this);
78     listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
79 
80     connect(quitAction, &QAction::triggered, this, &QWidget::close);
81     connect(ascendingAction, &QAction::triggered, this, &MainWindow::sortAscending);
82     connect(descendingAction, &QAction::triggered, this, &MainWindow::sortDescending);
83     connect(insertAction, &QAction::triggered, this, &MainWindow::insertItem);
84     connect(removeAction, &QAction::triggered, this, &MainWindow::removeItem);
85     connect(listWidget, &QListWidget::currentItemChanged,
86             this, &MainWindow::updateMenus);
87 
88     setupListItems();
89     updateMenus(listWidget->currentItem());
90 
91     setCentralWidget(listWidget);
92     setWindowTitle(tr("List Widget"));
93 }
94 
setupListItems()95 void MainWindow::setupListItems()
96 {
97 //! [1]
98     new QListWidgetItem(tr("Oak"), listWidget);
99     new QListWidgetItem(tr("Fir"), listWidget);
100     new QListWidgetItem(tr("Pine"), listWidget);
101 //! [1]
102     new QListWidgetItem(tr("Birch"), listWidget);
103 //! [2]
104     new QListWidgetItem(tr("Hazel"), listWidget);
105 //! [2]
106     new QListWidgetItem(tr("Redwood"), listWidget);
107 //! [3]
108     new QListWidgetItem(tr("Sycamore"), listWidget);
109     new QListWidgetItem(tr("Chestnut"), listWidget);
110     new QListWidgetItem(tr("Mahogany"), listWidget);
111 //! [3]
112 }
113 
sortAscending()114 void MainWindow::sortAscending()
115 {
116 //! [4]
117     listWidget->sortItems(Qt::AscendingOrder);
118 //! [4]
119 }
120 
sortDescending()121 void MainWindow::sortDescending()
122 {
123 //! [5]
124     listWidget->sortItems(Qt::DescendingOrder);
125 //! [5]
126 }
127 
insertItem()128 void MainWindow::insertItem()
129 {
130     if (!listWidget->currentItem())
131         return;
132 
133     QString itemText = QInputDialog::getText(this, tr("Insert Item"),
134         tr("Input text for the new item:"));
135 
136     if (itemText.isNull())
137         return;
138 
139 //! [6]
140     QListWidgetItem *newItem = new QListWidgetItem;
141     newItem->setText(itemText);
142 //! [6]
143     int row = listWidget->row(listWidget->currentItem());
144 //! [7]
145     listWidget->insertItem(row, newItem);
146 //! [7]
147 
148     QString toolTipText = tr("Tooltip:") + itemText;
149     QString statusTipText = tr("Status tip:") + itemText;
150     QString whatsThisText = tr("What's This?:") + itemText;
151 //! [8]
152     newItem->setToolTip(toolTipText);
153     newItem->setStatusTip(toolTipText);
154     newItem->setWhatsThis(whatsThisText);
155 //! [8]
156 }
157 
removeItem()158 void MainWindow::removeItem()
159 {
160     listWidget->takeItem(listWidget->row(listWidget->currentItem()));
161 }
162 
updateMenus(QListWidgetItem * current)163 void MainWindow::updateMenus(QListWidgetItem *current)
164 {
165     insertAction->setEnabled(current != 0);
166     removeAction->setEnabled(current != 0);
167 }
168