| 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 examples 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 "mainwindow.h" | 
| 52 |  | 
| 53 | #include <QAction> | 
| 54 | #include <QFileDialog> | 
| 55 | #include <QMenuBar> | 
| 56 |  | 
| 57 | //! [0] | 
| 58 | MainWindow::MainWindow() | 
| 59 |     : QMainWindow(), | 
| 60 |       addressWidget(new AddressWidget) | 
| 61 | { | 
| 62 |     setCentralWidget(addressWidget); | 
| 63 |     createMenus(); | 
| 64 |     setWindowTitle(tr(s: "Address Book" )); | 
| 65 | } | 
| 66 | //! [0] | 
| 67 |  | 
| 68 | //! [1a] | 
| 69 | void MainWindow::createMenus() | 
| 70 | { | 
| 71 |     QMenu * = menuBar()->addMenu(title: tr(s: "&File" )); | 
| 72 |  | 
| 73 |     QAction *openAct = new QAction(tr(s: "&Open..." ), this); | 
| 74 |     fileMenu->addAction(action: openAct); | 
| 75 |     connect(sender: openAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::openFile); | 
| 76 | //! [1a] | 
| 77 |  | 
| 78 |     QAction *saveAct = new QAction(tr(s: "&Save As..." ), this); | 
| 79 |     fileMenu->addAction(action: saveAct); | 
| 80 |     connect(sender: saveAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::saveFile); | 
| 81 |  | 
| 82 |     fileMenu->addSeparator(); | 
| 83 |  | 
| 84 |     QAction *exitAct = new QAction(tr(s: "E&xit" ), this); | 
| 85 |     fileMenu->addAction(action: exitAct); | 
| 86 |     connect(sender: exitAct, signal: &QAction::triggered, receiver: this, slot: &QWidget::close); | 
| 87 |  | 
| 88 |     QMenu * = menuBar()->addMenu(title: tr(s: "&Tools" )); | 
| 89 |  | 
| 90 |     QAction *addAct = new QAction(tr(s: "&Add Entry..." ), this); | 
| 91 |     toolMenu->addAction(action: addAct); | 
| 92 |     connect(sender: addAct, signal: &QAction::triggered, | 
| 93 |             receiver: addressWidget, slot: &AddressWidget::showAddEntryDialog); | 
| 94 |  | 
| 95 | //! [1b] | 
| 96 |     editAct = new QAction(tr(s: "&Edit Entry..." ), this); | 
| 97 |     editAct->setEnabled(false); | 
| 98 |     toolMenu->addAction(action: editAct); | 
| 99 |     connect(sender: editAct, signal: &QAction::triggered, receiver: addressWidget, slot: &AddressWidget::editEntry); | 
| 100 |  | 
| 101 |     toolMenu->addSeparator(); | 
| 102 |  | 
| 103 |     removeAct = new QAction(tr(s: "&Remove Entry" ), this); | 
| 104 |     removeAct->setEnabled(false); | 
| 105 |     toolMenu->addAction(action: removeAct); | 
| 106 |     connect(sender: removeAct, signal: &QAction::triggered, receiver: addressWidget, slot: &AddressWidget::removeEntry); | 
| 107 |  | 
| 108 |     connect(sender: addressWidget, signal: &AddressWidget::selectionChanged, | 
| 109 |         receiver: this, slot: &MainWindow::updateActions); | 
| 110 | } | 
| 111 | //! [1b] | 
| 112 |  | 
| 113 | //! [2] | 
| 114 | void MainWindow::openFile() | 
| 115 | { | 
| 116 |     QString fileName = QFileDialog::getOpenFileName(parent: this); | 
| 117 |     if (!fileName.isEmpty()) | 
| 118 |         addressWidget->readFromFile(fileName); | 
| 119 | } | 
| 120 | //! [2] | 
| 121 |  | 
| 122 | //! [3] | 
| 123 | void MainWindow::saveFile() | 
| 124 | { | 
| 125 |     QString fileName = QFileDialog::getSaveFileName(parent: this); | 
| 126 |     if (!fileName.isEmpty()) | 
| 127 |         addressWidget->writeToFile(fileName); | 
| 128 | } | 
| 129 | //! [3] | 
| 130 |  | 
| 131 | //! [4] | 
| 132 | void MainWindow::updateActions(const QItemSelection &selection) | 
| 133 | { | 
| 134 |     QModelIndexList indexes = selection.indexes(); | 
| 135 |  | 
| 136 |     if (!indexes.isEmpty()) { | 
| 137 |         removeAct->setEnabled(true); | 
| 138 |         editAct->setEnabled(true); | 
| 139 |     } else { | 
| 140 |         removeAct->setEnabled(false); | 
| 141 |         editAct->setEnabled(false); | 
| 142 |     } | 
| 143 | } | 
| 144 | //! [4] | 
| 145 |  |