3 RawView::RawView(QWidget *parent)
4 : QAbstractItemView(parent)
6 horizontalScrollBar()->setRange(0, 0);
7 verticalScrollBar()->setRange(0, 0);
18 void RawView::dataChanged(
const QModelIndex &topLeft,
19 const QModelIndex &bottomRight,
22 QAbstractItemView::dataChanged(topLeft, bottomRight);
27 for (
int row = 0; row < model()->rowCount(rootIndex()); ++row) {
29 QModelIndex index = model()->index(row, 1, rootIndex());
30 double value = model()->data(index).toDouble();
44 QModelIndex RawView::indexAt(
const QPoint &point)
const
50 double cx = wx - totalSize / 2;
51 double cy = totalSize / 2 - wy;
54 double d = pow(pow(cx, 2) + pow(cy, 2), 0.5);
56 if (d == 0 || d > pieSize / 2)
60 double angle = (180 / M_PI) * acos(cx / d);
65 double startAngle = 0.0;
67 for (
int row = 0; row < model()->rowCount(rootIndex()); ++row) {
69 QModelIndex index = model()->index(row, 1, rootIndex());
70 double value = model()->data(index).toDouble();
73 double sliceAngle = 360 * value / totalValue;
75 if (angle >= startAngle && angle < (startAngle + sliceAngle))
76 return model()->index(row, 1, rootIndex());
78 startAngle += sliceAngle;
86 bool RawView::isIndexHidden(
const QModelIndex & )
const
96 QRect RawView::itemRect(
const QModelIndex &index)
const
103 QModelIndex valueIndex;
105 if (index.column() != 1)
106 valueIndex = model()->index(index.row(), 1, rootIndex());
110 if (model()->data(valueIndex).toDouble() <= 0.0)
114 for (
int row = index.row()-1; row >= 0; --row) {
115 if (model()->data(model()->index(row, 1, rootIndex())).toDouble() > 0.0)
121 switch (index.column()) {
123 itemHeight = QFontMetrics(viewOptions().font).height();
125 return QRect(totalSize,
126 int(margin + listItem*itemHeight),
127 totalSize - margin,
int(itemHeight));
129 return viewport()->rect();
135 QRegion RawView::itemRegion(
const QModelIndex &index)
const
137 if (!index.isValid())
140 if (index.column() != 1)
141 return itemRect(index);
143 if (model()->data(index).toDouble() <= 0.0)
146 double startAngle = 0.0;
147 for (
int row = 0; row < model()->rowCount(rootIndex()); ++row) {
149 QModelIndex sliceIndex = model()->index(row, 1, rootIndex());
150 double value = model()->data(sliceIndex).toDouble();
153 double angle = 360 * value / totalValue;
155 if (sliceIndex == index) {
156 QPainterPath slicePath;
157 slicePath.moveTo(totalSize / 2, totalSize / 2);
158 slicePath.arcTo(margin, margin, margin+pieSize, margin+pieSize,
160 slicePath.closeSubpath();
162 return QRegion(slicePath.toFillPolygon().toPolygon());
172 int RawView::horizontalOffset()
const
174 return horizontalScrollBar()->value();
177 void RawView::mousePressEvent(QMouseEvent *event)
187 void RawView::mouseMoveEvent(QMouseEvent *event)
194 void RawView::mouseReleaseEvent(QMouseEvent *event)
202 QModelIndex RawView::moveCursor(QAbstractItemView::CursorAction cursorAction,
203 Qt::KeyboardModifiers )
205 QModelIndex current = currentIndex();
207 switch (cursorAction) {
210 if (current.row() > 0)
211 current = model()->index(current.row() - 1, current.column(),
214 current = model()->index(0, current.column(), rootIndex());
218 if (current.row() < rows(current) - 1)
219 current = model()->index(current.row() + 1, current.column(),
222 current = model()->index(rows(current) - 1, current.column(),
229 viewport()->update();
233 void RawView::paintEvent(QPaintEvent *event)
235 QItemSelectionModel *selections = selectionModel();
236 QStyleOptionViewItem option = viewOptions();
238 QBrush background = option.palette.base();
239 QPen foreground(option.palette.color(QPalette::WindowText));
241 QPainter painter(viewport());
242 painter.setRenderHint(QPainter::Antialiasing);
244 painter.fillRect(event->rect(), background);
245 painter.setPen(foreground);
248 QRect pieRect = QRect(margin, margin, pieSize, pieSize);
254 painter.translate(pieRect.x() - horizontalScrollBar()->value(),
255 pieRect.y() - verticalScrollBar()->value());
256 painter.drawEllipse(0, 0, pieSize, pieSize);
257 double startAngle = 0.0;
260 for (row = 0; row < model()->rowCount(rootIndex()); ++row) {
261 QModelIndex index = model()->index(row, 1, rootIndex());
262 double value = model()->data(index).toDouble();
265 double angle = 360*value/totalValue;
267 QModelIndex colorIndex = model()->index(row, 0, rootIndex());
268 QColor color = QColor(model()->data(colorIndex, Qt::DecorationRole).toString());
270 if (currentIndex() == index)
271 painter.setBrush(QBrush(color, Qt::Dense4Pattern));
272 else if (selections->isSelected(index))
273 painter.setBrush(QBrush(color, Qt::Dense3Pattern));
275 painter.setBrush(QBrush(color));
277 painter.drawPie(0, 0, pieSize, pieSize,
int(startAngle*16),
int(angle*16));
286 for (row = 0; row < model()->rowCount(rootIndex()); ++row) {
287 QModelIndex index = model()->index(row, 1, rootIndex());
288 double value = model()->data(index).toDouble();
291 QModelIndex labelIndex = model()->index(row, 0, rootIndex());
293 QStyleOptionViewItem option = viewOptions();
294 option.rect = visualRect(labelIndex);
295 if (selections->isSelected(labelIndex))
296 option.state |= QStyle::State_Selected;
297 if (currentIndex() == labelIndex)
298 option.state |= QStyle::State_HasFocus;
299 itemDelegate()->paint(&painter, option, labelIndex);
306 void RawView::resizeEvent(QResizeEvent * )
311 int RawView::rows(
const QModelIndex &index)
const
313 return model()->rowCount(model()->parent(index));
345 void RawView::scrollContentsBy(
int dx,
int dy)
347 viewport()->scroll(dx, dy);
350 void RawView::scrollTo(
const QModelIndex &index, ScrollHint)
352 QRect area = viewport()->rect();
353 QRect rect = visualRect(index);
355 if (rect.left() < area.left()) {
356 horizontalScrollBar()->setValue(
357 horizontalScrollBar()->value() + rect.left() - area.left());
358 }
else if (rect.right() > area.right()) {
359 horizontalScrollBar()->setValue(
360 horizontalScrollBar()->value() + qMin(
361 rect.right() - area.right(), rect.left() - area.left()));
364 if (rect.top() < area.top()) {
365 verticalScrollBar()->setValue(
366 verticalScrollBar()->value() + rect.top() - area.top());
367 }
else if (rect.bottom() > area.bottom()) {
368 verticalScrollBar()->setValue(
369 verticalScrollBar()->value() + qMin(
370 rect.bottom() - area.bottom(), rect.top() - area.top()));
380 void PieView::setSelection(
const QRect &rect, QItemSelectionModel::SelectionFlags command)
385 QRect contentsRect = rect.translated(
386 horizontalScrollBar()->value(),
387 verticalScrollBar()->value()).normalized();
389 int rows = model()->rowCount(rootIndex());
390 int columns = model()->columnCount(rootIndex());
391 QModelIndexList indexes;
393 for (
int row = 0; row < rows; ++row) {
394 for (
int column = 0; column < columns; ++column) {
395 QModelIndex index = model()->index(row, column, rootIndex());
396 QRegion region = itemRegion(index);
397 if (region.intersects(contentsRect))
398 indexes.append(index);
402 if (indexes.size() > 0) {
403 int firstRow = indexes[0].row();
404 int lastRow = indexes[0].row();
405 int firstColumn = indexes[0].column();
406 int lastColumn = indexes[0].column();
408 for (
int i = 1; i < indexes.size(); ++i) {
409 firstRow = qMin(firstRow, indexes[i].row());
410 lastRow = qMax(lastRow, indexes[i].row());
411 firstColumn = qMin(firstColumn, indexes[i].column());
412 lastColumn = qMax(lastColumn, indexes[i].column());
415 QItemSelection selection(
416 model()->index(firstRow, firstColumn, rootIndex()),
417 model()->index(lastRow, lastColumn, rootIndex()));
418 selectionModel()->select(selection, command);
421 QItemSelection selection(noIndex, noIndex);
422 selectionModel()->select(selection, command);
428 void RawView::updateGeometries()
430 horizontalScrollBar()->setPageStep(viewport()->width());
431 horizontalScrollBar()->setRange(0, qMax(0, 2*totalSize - viewport()->width()));
432 verticalScrollBar()->setPageStep(viewport()->height());
433 verticalScrollBar()->setRange(0, qMax(0, totalSize - viewport()->height()));
436 int RawView::verticalOffset()
const
438 return verticalScrollBar()->value();
445 QRect RawView::visualRect(
const QModelIndex &index)
const
447 QRect rect = itemRect(index);
451 return QRect(rect.left() - horizontalScrollBar()->value(),
452 rect.top() - verticalScrollBar()->value(),
453 rect.width(), rect.height());