1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 | #include "mainwindow.h"
|
---|
43 |
|
---|
44 | //! [0]
|
---|
45 | MainWindow::MainWindow()
|
---|
46 | {
|
---|
47 | addressWidget = new AddressWidget;
|
---|
48 | setCentralWidget(addressWidget);
|
---|
49 | createMenus();
|
---|
50 | setWindowTitle(tr("Address Book"));
|
---|
51 | }
|
---|
52 | //! [0]
|
---|
53 |
|
---|
54 | //! [1a]
|
---|
55 | void MainWindow::createMenus()
|
---|
56 | {
|
---|
57 | fileMenu = menuBar()->addMenu(tr("&File"));
|
---|
58 |
|
---|
59 | openAct = new QAction(tr("&Open..."), this);
|
---|
60 | fileMenu->addAction(openAct);
|
---|
61 | connect(openAct, SIGNAL(triggered()),
|
---|
62 | this, SLOT(openFile()));
|
---|
63 | //! [1a]
|
---|
64 |
|
---|
65 | saveAct = new QAction(tr("&Save As..."), this);
|
---|
66 | fileMenu->addAction(saveAct);
|
---|
67 | connect(saveAct, SIGNAL(triggered()),
|
---|
68 | this, SLOT(saveFile()));
|
---|
69 |
|
---|
70 | fileMenu->addSeparator();
|
---|
71 |
|
---|
72 | exitAct = new QAction(tr("E&xit"), this);
|
---|
73 | fileMenu->addAction(exitAct);
|
---|
74 | connect(exitAct, SIGNAL(triggered()),
|
---|
75 | this, SLOT(close()));
|
---|
76 |
|
---|
77 | toolMenu = menuBar()->addMenu(tr("&Tools"));
|
---|
78 |
|
---|
79 | addAct = new QAction(tr("&Add Entry..."), this);
|
---|
80 | toolMenu->addAction(addAct);
|
---|
81 | connect(addAct, SIGNAL(triggered()),
|
---|
82 | addressWidget, SLOT(addEntry()));
|
---|
83 |
|
---|
84 | //! [1b]
|
---|
85 | editAct = new QAction(tr("&Edit Entry..."), this);
|
---|
86 | editAct->setEnabled(false);
|
---|
87 | toolMenu->addAction(editAct);
|
---|
88 | connect(editAct, SIGNAL(triggered()),
|
---|
89 | addressWidget, SLOT(editEntry()));
|
---|
90 |
|
---|
91 | toolMenu->addSeparator();
|
---|
92 |
|
---|
93 | removeAct = new QAction(tr("&Remove Entry"), this);
|
---|
94 | removeAct->setEnabled(false);
|
---|
95 | toolMenu->addAction(removeAct);
|
---|
96 | connect(removeAct, SIGNAL(triggered()),
|
---|
97 | addressWidget, SLOT(removeEntry()));
|
---|
98 |
|
---|
99 | connect(addressWidget, SIGNAL(selectionChanged(QItemSelection)),
|
---|
100 | this, SLOT(updateActions(QItemSelection)));
|
---|
101 | }
|
---|
102 | //! [1b]
|
---|
103 |
|
---|
104 | //! [2]
|
---|
105 | void MainWindow::openFile()
|
---|
106 | {
|
---|
107 | QString fileName = QFileDialog::getOpenFileName(this);
|
---|
108 | if (!fileName.isEmpty()) {
|
---|
109 | addressWidget->readFromFile(fileName);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | //! [2]
|
---|
113 |
|
---|
114 | //! [3]
|
---|
115 | void MainWindow::saveFile()
|
---|
116 | {
|
---|
117 | QString fileName = QFileDialog::getSaveFileName(this);
|
---|
118 | if (!fileName.isEmpty()) {
|
---|
119 | addressWidget->writeToFile(fileName);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | //! [3]
|
---|
123 |
|
---|
124 | //! [4]
|
---|
125 | void MainWindow::updateActions(const QItemSelection &selection)
|
---|
126 | {
|
---|
127 | QModelIndexList indexes = selection.indexes();
|
---|
128 |
|
---|
129 | if (!indexes.isEmpty()) {
|
---|
130 | removeAct->setEnabled(true);
|
---|
131 | editAct->setEnabled(true);
|
---|
132 | } else {
|
---|
133 | removeAct->setEnabled(false);
|
---|
134 | editAct->setEnabled(false);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | //! [4]
|
---|