|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
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 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 |
|
44 #include "pieview.h" |
|
45 #include "mainwindow.h" |
|
46 |
|
47 MainWindow::MainWindow() |
|
48 { |
|
49 QMenu *fileMenu = new QMenu(tr("&File"), this); |
|
50 QAction *openAction = fileMenu->addAction(tr("&Open...")); |
|
51 openAction->setShortcuts(QKeySequence::Open); |
|
52 QAction *saveAction = fileMenu->addAction(tr("&Save As...")); |
|
53 saveAction->setShortcuts(QKeySequence::SaveAs); |
|
54 QAction *quitAction = fileMenu->addAction(tr("E&xit")); |
|
55 quitAction->setShortcuts(QKeySequence::Quit); |
|
56 |
|
57 setupModel(); |
|
58 setupViews(); |
|
59 |
|
60 connect(openAction, SIGNAL(triggered()), this, SLOT(openFile())); |
|
61 connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile())); |
|
62 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
|
63 |
|
64 menuBar()->addMenu(fileMenu); |
|
65 statusBar(); |
|
66 |
|
67 openFile(":/Charts/qtdata.cht"); |
|
68 |
|
69 setWindowTitle(tr("Chart")); |
|
70 resize(870, 550); |
|
71 } |
|
72 |
|
73 void MainWindow::setupModel() |
|
74 { |
|
75 model = new QStandardItemModel(8, 2, this); |
|
76 model->setHeaderData(0, Qt::Horizontal, tr("Label")); |
|
77 model->setHeaderData(1, Qt::Horizontal, tr("Quantity")); |
|
78 } |
|
79 |
|
80 void MainWindow::setupViews() |
|
81 { |
|
82 QSplitter *splitter = new QSplitter; |
|
83 QTableView *table = new QTableView; |
|
84 pieChart = new PieView; |
|
85 splitter->addWidget(table); |
|
86 splitter->addWidget(pieChart); |
|
87 splitter->setStretchFactor(0, 0); |
|
88 splitter->setStretchFactor(1, 1); |
|
89 |
|
90 table->setModel(model); |
|
91 pieChart->setModel(model); |
|
92 |
|
93 QItemSelectionModel *selectionModel = new QItemSelectionModel(model); |
|
94 table->setSelectionModel(selectionModel); |
|
95 pieChart->setSelectionModel(selectionModel); |
|
96 |
|
97 QHeaderView *headerView = table->horizontalHeader(); |
|
98 headerView->setStretchLastSection(true); |
|
99 |
|
100 setCentralWidget(splitter); |
|
101 } |
|
102 |
|
103 void MainWindow::openFile(const QString &path) |
|
104 { |
|
105 QString fileName; |
|
106 if (path.isNull()) |
|
107 fileName = QFileDialog::getOpenFileName(this, tr("Choose a data file"), |
|
108 "", "*.cht"); |
|
109 else |
|
110 fileName = path; |
|
111 |
|
112 if (!fileName.isEmpty()) { |
|
113 QFile file(fileName); |
|
114 |
|
115 if (file.open(QFile::ReadOnly | QFile::Text)) { |
|
116 QTextStream stream(&file); |
|
117 QString line; |
|
118 |
|
119 model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex()); |
|
120 |
|
121 int row = 0; |
|
122 do { |
|
123 line = stream.readLine(); |
|
124 if (!line.isEmpty()) { |
|
125 |
|
126 model->insertRows(row, 1, QModelIndex()); |
|
127 |
|
128 QStringList pieces = line.split(",", QString::SkipEmptyParts); |
|
129 model->setData(model->index(row, 0, QModelIndex()), |
|
130 pieces.value(0)); |
|
131 model->setData(model->index(row, 1, QModelIndex()), |
|
132 pieces.value(1)); |
|
133 model->setData(model->index(row, 0, QModelIndex()), |
|
134 QColor(pieces.value(2)), Qt::DecorationRole); |
|
135 row++; |
|
136 } |
|
137 } while (!line.isEmpty()); |
|
138 |
|
139 file.close(); |
|
140 statusBar()->showMessage(tr("Loaded %1").arg(fileName), 2000); |
|
141 } |
|
142 } |
|
143 } |
|
144 |
|
145 void MainWindow::saveFile() |
|
146 { |
|
147 QString fileName = QFileDialog::getSaveFileName(this, |
|
148 tr("Save file as"), "", "*.cht"); |
|
149 |
|
150 if (!fileName.isEmpty()) { |
|
151 QFile file(fileName); |
|
152 QTextStream stream(&file); |
|
153 |
|
154 if (file.open(QFile::WriteOnly | QFile::Text)) { |
|
155 for (int row = 0; row < model->rowCount(QModelIndex()); ++row) { |
|
156 |
|
157 QStringList pieces; |
|
158 |
|
159 pieces.append(model->data(model->index(row, 0, QModelIndex()), |
|
160 Qt::DisplayRole).toString()); |
|
161 pieces.append(model->data(model->index(row, 1, QModelIndex()), |
|
162 Qt::DisplayRole).toString()); |
|
163 pieces.append(model->data(model->index(row, 0, QModelIndex()), |
|
164 Qt::DecorationRole).toString()); |
|
165 |
|
166 stream << pieces.join(",") << "\n"; |
|
167 } |
|
168 } |
|
169 |
|
170 file.close(); |
|
171 statusBar()->showMessage(tr("Saved %1").arg(fileName), 2000); |
|
172 } |
|
173 } |