source: trunk/examples/itemviews/chart/mainwindow.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at qt-sales@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "pieview.h"
45#include "mainwindow.h"
46
47MainWindow::MainWindow()
48{
49 QMenu *fileMenu = new QMenu(tr("&File"), this);
50 QAction *openAction = fileMenu->addAction(tr("&Open..."));
51 openAction->setShortcut(QKeySequence(tr("Ctrl+O")));
52 QAction *saveAction = fileMenu->addAction(tr("&Save As..."));
53 saveAction->setShortcut(QKeySequence(tr("Ctrl+S")));
54 QAction *quitAction = fileMenu->addAction(tr("E&xit"));
55 quitAction->setShortcut(QKeySequence(tr("Ctrl+Q")));
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
73void 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
80void 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
103void 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
145void 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}
Note: See TracBrowser for help on using the repository browser.