summaryrefslogtreecommitdiffstats
path: root/examples/widgets/itemviews/chart
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/itemviews/chart')
-rw-r--r--examples/widgets/itemviews/chart/mainwindow.cpp88
-rw-r--r--examples/widgets/itemviews/chart/mainwindow.h2
-rw-r--r--examples/widgets/itemviews/chart/pieview.cpp159
-rw-r--r--examples/widgets/itemviews/chart/pieview.h17
4 files changed, 125 insertions, 141 deletions
diff --git a/examples/widgets/itemviews/chart/mainwindow.cpp b/examples/widgets/itemviews/chart/mainwindow.cpp
index b94ca35100..ce791ec7f3 100644
--- a/examples/widgets/itemviews/chart/mainwindow.cpp
+++ b/examples/widgets/itemviews/chart/mainwindow.cpp
@@ -103,42 +103,41 @@ void MainWindow::openFile(const QString &path)
{
QString fileName;
if (path.isNull())
- fileName = QFileDialog::getOpenFileName(this, tr("Choose a data file"),
- "", "*.cht");
+ fileName = QFileDialog::getOpenFileName(this, tr("Choose a data file"), "", "*.cht");
else
fileName = path;
- if (!fileName.isEmpty()) {
- QFile file(fileName);
+ if (fileName.isEmpty())
+ return;
- if (file.open(QFile::ReadOnly | QFile::Text)) {
- QTextStream stream(&file);
- QString line;
+ QFile file(fileName);
+ if (!file.open(QFile::ReadOnly | QFile::Text))
+ return;
- model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex());
+ QTextStream stream(&file);
+ QString line;
- int row = 0;
- do {
- line = stream.readLine();
- if (!line.isEmpty()) {
+ model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex());
- model->insertRows(row, 1, QModelIndex());
+ int row = 0;
+ do {
+ line = stream.readLine();
+ if (!line.isEmpty()) {
+ model->insertRows(row, 1, QModelIndex());
- QStringList pieces = line.split(",", QString::SkipEmptyParts);
- model->setData(model->index(row, 0, QModelIndex()),
- pieces.value(0));
- model->setData(model->index(row, 1, QModelIndex()),
- pieces.value(1));
- model->setData(model->index(row, 0, QModelIndex()),
- QColor(pieces.value(2)), Qt::DecorationRole);
- row++;
- }
- } while (!line.isEmpty());
-
- file.close();
- statusBar()->showMessage(tr("Loaded %1").arg(fileName), 2000);
+ QStringList pieces = line.split(",", QString::SkipEmptyParts);
+ model->setData(model->index(row, 0, QModelIndex()),
+ pieces.value(0));
+ model->setData(model->index(row, 1, QModelIndex()),
+ pieces.value(1));
+ model->setData(model->index(row, 0, QModelIndex()),
+ QColor(pieces.value(2)), Qt::DecorationRole);
+ row++;
}
- }
+ } while (!line.isEmpty());
+
+ file.close();
+ statusBar()->showMessage(tr("Loaded %1").arg(fileName), 2000);
}
void MainWindow::saveFile()
@@ -146,27 +145,28 @@ void MainWindow::saveFile()
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save file as"), "", "*.cht");
- if (!fileName.isEmpty()) {
- QFile file(fileName);
- QTextStream stream(&file);
+ if (fileName.isEmpty())
+ return;
- if (file.open(QFile::WriteOnly | QFile::Text)) {
- for (int row = 0; row < model->rowCount(QModelIndex()); ++row) {
+ QFile file(fileName);
+ if (!file.open(QFile::WriteOnly | QFile::Text))
+ return;
- QStringList pieces;
+ QTextStream stream(&file);
+ for (int row = 0; row < model->rowCount(QModelIndex()); ++row) {
- pieces.append(model->data(model->index(row, 0, QModelIndex()),
- Qt::DisplayRole).toString());
- pieces.append(model->data(model->index(row, 1, QModelIndex()),
- Qt::DisplayRole).toString());
- pieces.append(model->data(model->index(row, 0, QModelIndex()),
- Qt::DecorationRole).toString());
+ QStringList pieces;
- stream << pieces.join(',') << "\n";
- }
- }
+ pieces.append(model->data(model->index(row, 0, QModelIndex()),
+ Qt::DisplayRole).toString());
+ pieces.append(model->data(model->index(row, 1, QModelIndex()),
+ Qt::DisplayRole).toString());
+ pieces.append(model->data(model->index(row, 0, QModelIndex()),
+ Qt::DecorationRole).toString());
- file.close();
- statusBar()->showMessage(tr("Saved %1").arg(fileName), 2000);
+ stream << pieces.join(',') << "\n";
}
+
+ file.close();
+ statusBar()->showMessage(tr("Saved %1").arg(fileName), 2000);
}
diff --git a/examples/widgets/itemviews/chart/mainwindow.h b/examples/widgets/itemviews/chart/mainwindow.h
index 840c95ba4c..5832744856 100644
--- a/examples/widgets/itemviews/chart/mainwindow.h
+++ b/examples/widgets/itemviews/chart/mainwindow.h
@@ -69,4 +69,4 @@ private:
QItemSelectionModel *selectionModel;
};
-#endif
+#endif // MAINWINDOW_H
diff --git a/examples/widgets/itemviews/chart/pieview.cpp b/examples/widgets/itemviews/chart/pieview.cpp
index e3b00830ae..215453c689 100644
--- a/examples/widgets/itemviews/chart/pieview.cpp
+++ b/examples/widgets/itemviews/chart/pieview.cpp
@@ -55,7 +55,7 @@ PieView::PieView(QWidget *parent)
margin = 8;
totalSize = 300;
- pieSize = totalSize - 2*margin;
+ pieSize = totalSize - 2 * margin;
validItems = 0;
totalValue = 0.0;
rubberBand = 0;
@@ -105,17 +105,17 @@ QModelIndex PieView::indexAt(const QPoint &point) const
int wy = point.y() + verticalScrollBar()->value();
if (wx < totalSize) {
- double cx = wx - totalSize/2;
- double cy = totalSize/2 - wy; // positive cy for items above the center
+ double cx = wx - totalSize / 2;
+ double cy = totalSize / 2 - wy; // positive cy for items above the center
// Determine the distance from the center point of the pie chart.
double d = pow(pow(cx, 2) + pow(cy, 2), 0.5);
- if (d == 0 || d > pieSize/2)
+ if (d == 0 || d > pieSize / 2)
return QModelIndex();
// Determine the angle of the point.
- double angle = (180 / M_PI) * acos(cx/d);
+ double angle = (180 / M_PI) * acos(cx / d);
if (cy < 0)
angle = 360 - angle;
@@ -128,7 +128,7 @@ QModelIndex PieView::indexAt(const QPoint &point) const
double value = model()->data(index).toDouble();
if (value > 0.0) {
- double sliceAngle = 360*value/totalValue;
+ double sliceAngle = 360 * value / totalValue;
if (angle >= startAngle && angle < (startAngle + sliceAngle))
return model()->index(row, 1, rootIndex());
@@ -150,7 +150,7 @@ QModelIndex PieView::indexAt(const QPoint &point) const
return model()->index(row, 0, rootIndex());
// Update the list index that corresponds to the next valid row.
- validRow++;
+ ++validRow;
}
}
}
@@ -182,27 +182,26 @@ QRect PieView::itemRect(const QModelIndex &index) const
else
valueIndex = index;
- if (model()->data(valueIndex).toDouble() > 0.0) {
-
- int listItem = 0;
- for (int row = index.row()-1; row >= 0; --row) {
- if (model()->data(model()->index(row, 1, rootIndex())).toDouble() > 0.0)
- listItem++;
- }
+ if (model()->data(valueIndex).toDouble() <= 0.0)
+ return QRect();
- double itemHeight;
+ int listItem = 0;
+ for (int row = index.row()-1; row >= 0; --row) {
+ if (model()->data(model()->index(row, 1, rootIndex())).toDouble() > 0.0)
+ listItem++;
+ }
- switch (index.column()) {
- case 0:
- itemHeight = QFontMetrics(viewOptions().font).height();
+ double itemHeight;
- return QRect(totalSize,
- int(margin + listItem*itemHeight),
- totalSize - margin, int(itemHeight));
- case 1:
- return viewport()->rect();
- }
+ switch (index.column()) {
+ case 0:
+ itemHeight = QFontMetrics(viewOptions().font).height();
+ return QRect(totalSize,
+ int(margin + listItem*itemHeight),
+ totalSize - margin, int(itemHeight));
+ case 1:
+ return viewport()->rect();
}
return QRect();
}
@@ -225,11 +224,11 @@ QRegion PieView::itemRegion(const QModelIndex &index) const
double value = model()->data(sliceIndex).toDouble();
if (value > 0.0) {
- double angle = 360*value/totalValue;
+ double angle = 360 * value / totalValue;
if (sliceIndex == index) {
QPainterPath slicePath;
- slicePath.moveTo(totalSize/2, totalSize/2);
+ slicePath.moveTo(totalSize / 2, totalSize / 2);
slicePath.arcTo(margin, margin, margin+pieSize, margin+pieSize,
startAngle, angle);
slicePath.closeSubpath();
@@ -322,62 +321,58 @@ void PieView::paintEvent(QPaintEvent *event)
// Viewport rectangles
QRect pieRect = QRect(margin, margin, pieSize, pieSize);
- if (validItems > 0) {
-
- painter.save();
- painter.translate(pieRect.x() - horizontalScrollBar()->value(),
- pieRect.y() - verticalScrollBar()->value());
- painter.drawEllipse(0, 0, pieSize, pieSize);
- double startAngle = 0.0;
- int row;
+ if (validItems <= 0)
+ return;
- for (row = 0; row < model()->rowCount(rootIndex()); ++row) {
+ painter.save();
+ painter.translate(pieRect.x() - horizontalScrollBar()->value(),
+ pieRect.y() - verticalScrollBar()->value());
+ painter.drawEllipse(0, 0, pieSize, pieSize);
+ double startAngle = 0.0;
+ int row;
- QModelIndex index = model()->index(row, 1, rootIndex());
- double value = model()->data(index).toDouble();
+ for (row = 0; row < model()->rowCount(rootIndex()); ++row) {
+ QModelIndex index = model()->index(row, 1, rootIndex());
+ double value = model()->data(index).toDouble();
- if (value > 0.0) {
- double angle = 360*value/totalValue;
+ if (value > 0.0) {
+ double angle = 360*value/totalValue;
- QModelIndex colorIndex = model()->index(row, 0, rootIndex());
- QColor color = QColor(model()->data(colorIndex,
- Qt::DecorationRole).toString());
+ QModelIndex colorIndex = model()->index(row, 0, rootIndex());
+ QColor color = QColor(model()->data(colorIndex, Qt::DecorationRole).toString());
- if (currentIndex() == index)
- painter.setBrush(QBrush(color, Qt::Dense4Pattern));
- else if (selections->isSelected(index))
- painter.setBrush(QBrush(color, Qt::Dense3Pattern));
- else
- painter.setBrush(QBrush(color));
+ if (currentIndex() == index)
+ painter.setBrush(QBrush(color, Qt::Dense4Pattern));
+ else if (selections->isSelected(index))
+ painter.setBrush(QBrush(color, Qt::Dense3Pattern));
+ else
+ painter.setBrush(QBrush(color));
- painter.drawPie(0, 0, pieSize, pieSize, int(startAngle*16),
- int(angle*16));
+ painter.drawPie(0, 0, pieSize, pieSize, int(startAngle*16), int(angle*16));
- startAngle += angle;
- }
+ startAngle += angle;
}
- painter.restore();
-
- int keyNumber = 0;
+ }
+ painter.restore();
- for (row = 0; row < model()->rowCount(rootIndex()); ++row) {
+ int keyNumber = 0;
- QModelIndex index = model()->index(row, 1, rootIndex());
- double value = model()->data(index).toDouble();
+ for (row = 0; row < model()->rowCount(rootIndex()); ++row) {
+ QModelIndex index = model()->index(row, 1, rootIndex());
+ double value = model()->data(index).toDouble();
- if (value > 0.0) {
- QModelIndex labelIndex = model()->index(row, 0, rootIndex());
+ if (value > 0.0) {
+ QModelIndex labelIndex = model()->index(row, 0, rootIndex());
- QStyleOptionViewItem option = viewOptions();
- option.rect = visualRect(labelIndex);
- if (selections->isSelected(labelIndex))
- option.state |= QStyle::State_Selected;
- if (currentIndex() == labelIndex)
- option.state |= QStyle::State_HasFocus;
- itemDelegate()->paint(&painter, option, labelIndex);
+ QStyleOptionViewItem option = viewOptions();
+ option.rect = visualRect(labelIndex);
+ if (selections->isSelected(labelIndex))
+ option.state |= QStyle::State_Selected;
+ if (currentIndex() == labelIndex)
+ option.state |= QStyle::State_HasFocus;
+ itemDelegate()->paint(&painter, option, labelIndex);
- keyNumber++;
- }
+ ++keyNumber;
}
}
}
@@ -395,13 +390,12 @@ int PieView::rows(const QModelIndex &index) const
void PieView::rowsInserted(const QModelIndex &parent, int start, int end)
{
for (int row = start; row <= end; ++row) {
-
QModelIndex index = model()->index(row, 1, rootIndex());
double value = model()->data(index).toDouble();
if (value > 0.0) {
totalValue += value;
- validItems++;
+ ++validItems;
}
}
@@ -411,12 +405,11 @@ void PieView::rowsInserted(const QModelIndex &parent, int start, int end)
void PieView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{
for (int row = start; row <= end; ++row) {
-
QModelIndex index = model()->index(row, 1, rootIndex());
double value = model()->data(index).toDouble();
if (value > 0.0) {
totalValue -= value;
- validItems--;
+ --validItems;
}
}
@@ -433,21 +426,23 @@ void PieView::scrollTo(const QModelIndex &index, ScrollHint)
QRect area = viewport()->rect();
QRect rect = visualRect(index);
- if (rect.left() < area.left())
+ if (rect.left() < area.left()) {
horizontalScrollBar()->setValue(
horizontalScrollBar()->value() + rect.left() - area.left());
- else if (rect.right() > area.right())
+ } else if (rect.right() > area.right()) {
horizontalScrollBar()->setValue(
horizontalScrollBar()->value() + qMin(
rect.right() - area.right(), rect.left() - area.left()));
+ }
- if (rect.top() < area.top())
+ if (rect.top() < area.top()) {
verticalScrollBar()->setValue(
verticalScrollBar()->value() + rect.top() - area.top());
- else if (rect.bottom() > area.bottom())
+ } else if (rect.bottom() > area.bottom()) {
verticalScrollBar()->setValue(
verticalScrollBar()->value() + qMin(
rect.bottom() - area.bottom(), rect.top() - area.top()));
+ }
update();
}
@@ -524,12 +519,12 @@ int PieView::verticalOffset() const
QRect PieView::visualRect(const QModelIndex &index) const
{
QRect rect = itemRect(index);
- if (rect.isValid())
- return QRect(rect.left() - horizontalScrollBar()->value(),
- rect.top() - verticalScrollBar()->value(),
- rect.width(), rect.height());
- else
+ if (!rect.isValid())
return rect;
+
+ return QRect(rect.left() - horizontalScrollBar()->value(),
+ rect.top() - verticalScrollBar()->value(),
+ rect.width(), rect.height());
}
/*
diff --git a/examples/widgets/itemviews/chart/pieview.h b/examples/widgets/itemviews/chart/pieview.h
index 517aa8e272..a6a0612ff3 100644
--- a/examples/widgets/itemviews/chart/pieview.h
+++ b/examples/widgets/itemviews/chart/pieview.h
@@ -42,18 +42,6 @@
#define PIEVIEW_H
#include <QAbstractItemView>
-#include <QFont>
-#include <QItemSelection>
-#include <QItemSelectionModel>
-#include <QModelIndex>
-#include <QRect>
-#include <QSize>
-#include <QPoint>
-#include <QWidget>
-
-QT_BEGIN_NAMESPACE
-class QRubberBand;
-QT_END_NAMESPACE
//! [0]
class PieView : public QAbstractItemView
@@ -68,7 +56,8 @@ public:
QModelIndex indexAt(const QPoint &point) const;
protected slots:
- void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> & = QVector<int>());
+ void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
+ const QVector<int> &roles = QVector<int>());
void rowsInserted(const QModelIndex &parent, int start, int end);
void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
@@ -111,4 +100,4 @@ private:
};
//! [0]
-#endif
+#endif // PIEVIEW_H