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 "pieview.h" |
52 | #include "mainwindow.h" |
53 | |
54 | #include <QtWidgets> |
55 | |
56 | MainWindow::MainWindow(QWidget *parent) |
57 | : QMainWindow(parent) |
58 | { |
59 | QMenu * = new QMenu(tr(s: "&File" ), this); |
60 | QAction *openAction = fileMenu->addAction(text: tr(s: "&Open..." )); |
61 | openAction->setShortcuts(QKeySequence::Open); |
62 | QAction *saveAction = fileMenu->addAction(text: tr(s: "&Save As..." )); |
63 | saveAction->setShortcuts(QKeySequence::SaveAs); |
64 | QAction *quitAction = fileMenu->addAction(text: tr(s: "E&xit" )); |
65 | quitAction->setShortcuts(QKeySequence::Quit); |
66 | |
67 | setupModel(); |
68 | setupViews(); |
69 | |
70 | connect(sender: openAction, signal: &QAction::triggered, receiver: this, slot: &MainWindow::openFile); |
71 | connect(sender: saveAction, signal: &QAction::triggered, receiver: this, slot: &MainWindow::saveFile); |
72 | connect(sender: quitAction, signal: &QAction::triggered, qApp, slot: &QCoreApplication::quit); |
73 | |
74 | menuBar()->addMenu(menu: fileMenu); |
75 | statusBar(); |
76 | |
77 | loadFile(path: ":/Charts/qtdata.cht" ); |
78 | |
79 | setWindowTitle(tr(s: "Chart" )); |
80 | resize(w: 870, h: 550); |
81 | } |
82 | |
83 | void MainWindow::setupModel() |
84 | { |
85 | model = new QStandardItemModel(8, 2, this); |
86 | model->setHeaderData(section: 0, orientation: Qt::Horizontal, value: tr(s: "Label" )); |
87 | model->setHeaderData(section: 1, orientation: Qt::Horizontal, value: tr(s: "Quantity" )); |
88 | } |
89 | |
90 | void MainWindow::setupViews() |
91 | { |
92 | QSplitter *splitter = new QSplitter; |
93 | QTableView *table = new QTableView; |
94 | pieChart = new PieView; |
95 | splitter->addWidget(widget: table); |
96 | splitter->addWidget(widget: pieChart); |
97 | splitter->setStretchFactor(index: 0, stretch: 0); |
98 | splitter->setStretchFactor(index: 1, stretch: 1); |
99 | |
100 | table->setModel(model); |
101 | pieChart->setModel(model); |
102 | |
103 | QItemSelectionModel *selectionModel = new QItemSelectionModel(model); |
104 | table->setSelectionModel(selectionModel); |
105 | pieChart->setSelectionModel(selectionModel); |
106 | |
107 | QHeaderView * = table->horizontalHeader(); |
108 | headerView->setStretchLastSection(true); |
109 | |
110 | setCentralWidget(splitter); |
111 | } |
112 | |
113 | void MainWindow::openFile() |
114 | { |
115 | const QString fileName = |
116 | QFileDialog::getOpenFileName(parent: this, caption: tr(s: "Choose a data file" ), dir: "" , filter: "*.cht" ); |
117 | if (!fileName.isEmpty()) |
118 | loadFile(path: fileName); |
119 | } |
120 | |
121 | void MainWindow::loadFile(const QString &fileName) |
122 | { |
123 | QFile file(fileName); |
124 | if (!file.open(flags: QFile::ReadOnly | QFile::Text)) |
125 | return; |
126 | |
127 | QTextStream stream(&file); |
128 | |
129 | model->removeRows(row: 0, count: model->rowCount(parent: QModelIndex()), parent: QModelIndex()); |
130 | |
131 | int row = 0; |
132 | while (!stream.atEnd()) { |
133 | const QString line = stream.readLine(); |
134 | if (!line.isEmpty()) { |
135 | model->insertRows(row, count: 1, parent: QModelIndex()); |
136 | |
137 | const QStringList pieces = line.split(sep: QLatin1Char(','), behavior: Qt::SkipEmptyParts); |
138 | if (pieces.size() < 3) |
139 | continue; |
140 | model->setData(index: model->index(row, column: 0, parent: QModelIndex()), |
141 | value: pieces.value(i: 0)); |
142 | model->setData(index: model->index(row, column: 1, parent: QModelIndex()), |
143 | value: pieces.value(i: 1)); |
144 | model->setData(index: model->index(row, column: 0, parent: QModelIndex()), |
145 | value: QColor(pieces.value(i: 2)), role: Qt::DecorationRole); |
146 | row++; |
147 | } |
148 | }; |
149 | |
150 | file.close(); |
151 | statusBar()->showMessage(text: tr(s: "Loaded %1" ).arg(a: fileName), timeout: 2000); |
152 | } |
153 | |
154 | void MainWindow::saveFile() |
155 | { |
156 | QString fileName = QFileDialog::getSaveFileName(parent: this, |
157 | caption: tr(s: "Save file as" ), dir: "" , filter: "*.cht" ); |
158 | |
159 | if (fileName.isEmpty()) |
160 | return; |
161 | |
162 | QFile file(fileName); |
163 | if (!file.open(flags: QFile::WriteOnly | QFile::Text)) |
164 | return; |
165 | |
166 | QTextStream stream(&file); |
167 | for (int row = 0; row < model->rowCount(parent: QModelIndex()); ++row) { |
168 | |
169 | QStringList pieces; |
170 | |
171 | pieces.append(t: model->data(index: model->index(row, column: 0, parent: QModelIndex()), |
172 | role: Qt::DisplayRole).toString()); |
173 | pieces.append(t: model->data(index: model->index(row, column: 1, parent: QModelIndex()), |
174 | role: Qt::DisplayRole).toString()); |
175 | pieces.append(t: model->data(index: model->index(row, column: 0, parent: QModelIndex()), |
176 | role: Qt::DecorationRole).toString()); |
177 | |
178 | stream << pieces.join(sep: ',') << "\n" ; |
179 | } |
180 | |
181 | file.close(); |
182 | statusBar()->showMessage(text: tr(s: "Saved %1" ).arg(a: fileName), timeout: 2000); |
183 | } |
184 | |