| 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( "&File"), this); | 
|---|
| 60 | QAction *openAction = fileMenu->addAction(tr( "&Open...")); | 
|---|
| 61 | openAction->setShortcuts(QKeySequence::Open); | 
|---|
| 62 | QAction *saveAction = fileMenu->addAction(tr( "&Save As...")); | 
|---|
| 63 | saveAction->setShortcuts(QKeySequence::SaveAs); | 
|---|
| 64 | QAction *quitAction = fileMenu->addAction(tr( "E&xit")); | 
|---|
| 65 | quitAction->setShortcuts(QKeySequence::Quit); | 
|---|
| 66 |  | 
|---|
| 67 | setupModel(); | 
|---|
| 68 | setupViews(); | 
|---|
| 69 |  | 
|---|
| 70 | connect(openAction, &QAction::triggered, this, &MainWindow::openFile); | 
|---|
| 71 | connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile); | 
|---|
| 72 | connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit); | 
|---|
| 73 |  | 
|---|
| 74 | menuBar()->addMenu(fileMenu); | 
|---|
| 75 | statusBar(); | 
|---|
| 76 |  | 
|---|
| 77 | loadFile( ":/Charts/qtdata.cht"); | 
|---|
| 78 |  | 
|---|
| 79 | setWindowTitle(tr( "Chart")); | 
|---|
| 80 | resize(870, 550); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void MainWindow::setupModel() | 
|---|
| 84 | { | 
|---|
| 85 | model = new QStandardItemModel(8, 2, this); | 
|---|
| 86 | model->setHeaderData(0, Qt::Horizontal, tr( "Label")); | 
|---|
| 87 | model->setHeaderData(1, Qt::Horizontal, tr( "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(table); | 
|---|
| 96 | splitter->addWidget(pieChart); | 
|---|
| 97 | splitter->setStretchFactor(0, 0); | 
|---|
| 98 | splitter->setStretchFactor(1, 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(this, tr( "Choose a data file"), "", "*.cht"); | 
|---|
| 117 | if (!fileName.isEmpty()) | 
|---|
| 118 | loadFile(fileName); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | void MainWindow::loadFile(const QString &fileName) | 
|---|
| 122 | { | 
|---|
| 123 | QFile file(fileName); | 
|---|
| 124 | if (!file.open(QFile::ReadOnly | QFile::Text)) | 
|---|
| 125 | return; | 
|---|
| 126 |  | 
|---|
| 127 | QTextStream stream(&file); | 
|---|
| 128 |  | 
|---|
| 129 | model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex()); | 
|---|
| 130 |  | 
|---|
| 131 | int row = 0; | 
|---|
| 132 | while (!stream.atEnd()) { | 
|---|
| 133 | const QString line = stream.readLine(); | 
|---|
| 134 | if (!line.isEmpty()) { | 
|---|
| 135 | model->insertRows(row, 1, QModelIndex()); | 
|---|
| 136 |  | 
|---|
| 137 | const QStringList pieces = line.split(QLatin1Char(','), Qt::SkipEmptyParts); | 
|---|
| 138 | if (pieces.size() < 3) | 
|---|
| 139 | continue; | 
|---|
| 140 | model->setData(model->index(row, 0, QModelIndex()), | 
|---|
| 141 | pieces.value(0)); | 
|---|
| 142 | model->setData(model->index(row, 1, QModelIndex()), | 
|---|
| 143 | pieces.value(1)); | 
|---|
| 144 | model->setData(model->index(row, 0, QModelIndex()), | 
|---|
| 145 | QColor(pieces.value(2)), Qt::DecorationRole); | 
|---|
| 146 | row++; | 
|---|
| 147 | } | 
|---|
| 148 | }; | 
|---|
| 149 |  | 
|---|
| 150 | file.close(); | 
|---|
| 151 | statusBar()->showMessage(tr( "Loaded %1").arg(fileName), 2000); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | void MainWindow::saveFile() | 
|---|
| 155 | { | 
|---|
| 156 | QString fileName = QFileDialog::getSaveFileName(this, | 
|---|
| 157 | tr( "Save file as"), "", "*.cht"); | 
|---|
| 158 |  | 
|---|
| 159 | if (fileName.isEmpty()) | 
|---|
| 160 | return; | 
|---|
| 161 |  | 
|---|
| 162 | QFile file(fileName); | 
|---|
| 163 | if (!file.open(QFile::WriteOnly | QFile::Text)) | 
|---|
| 164 | return; | 
|---|
| 165 |  | 
|---|
| 166 | QTextStream stream(&file); | 
|---|
| 167 | for (int row = 0; row < model->rowCount(QModelIndex()); ++row) { | 
|---|
| 168 |  | 
|---|
| 169 | QStringList pieces; | 
|---|
| 170 |  | 
|---|
| 171 | pieces.append(model->data(model->index(row, 0, QModelIndex()), | 
|---|
| 172 | Qt::DisplayRole).toString()); | 
|---|
| 173 | pieces.append(model->data(model->index(row, 1, QModelIndex()), | 
|---|
| 174 | Qt::DisplayRole).toString()); | 
|---|
| 175 | pieces.append(model->data(model->index(row, 0, QModelIndex()), | 
|---|
| 176 | Qt::DecorationRole).toString()); | 
|---|
| 177 |  | 
|---|
| 178 | stream << pieces.join(',') << "\n"; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | file.close(); | 
|---|
| 182 | statusBar()->showMessage(tr( "Saved %1").arg(fileName), 2000); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|