diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:34:13 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:34:13 (GMT) |
commit | 67ad0519fd165acee4a4d2a94fa502e9e4847bd0 (patch) | |
tree | 1dbf50b3dff8d5ca7e9344733968c72704eb15ff /examples/itemviews/pixelator | |
download | Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.zip Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.tar.gz Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.tar.bz2 |
Long live Qt!
Diffstat (limited to 'examples/itemviews/pixelator')
-rw-r--r-- | examples/itemviews/pixelator/imagemodel.cpp | 92 | ||||
-rw-r--r-- | examples/itemviews/pixelator/imagemodel.h | 69 | ||||
-rw-r--r-- | examples/itemviews/pixelator/images.qrc | 5 | ||||
-rw-r--r-- | examples/itemviews/pixelator/images/qt.png | bin | 0 -> 656 bytes | |||
-rw-r--r-- | examples/itemviews/pixelator/main.cpp | 55 | ||||
-rw-r--r-- | examples/itemviews/pixelator/mainwindow.cpp | 245 | ||||
-rw-r--r-- | examples/itemviews/pixelator/mainwindow.h | 75 | ||||
-rw-r--r-- | examples/itemviews/pixelator/pixelator.pro | 14 | ||||
-rw-r--r-- | examples/itemviews/pixelator/pixeldelegate.cpp | 108 | ||||
-rw-r--r-- | examples/itemviews/pixelator/pixeldelegate.h | 80 |
10 files changed, 743 insertions, 0 deletions
diff --git a/examples/itemviews/pixelator/imagemodel.cpp b/examples/itemviews/pixelator/imagemodel.cpp new file mode 100644 index 0000000..ea42b62 --- /dev/null +++ b/examples/itemviews/pixelator/imagemodel.cpp @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "imagemodel.h" + +//! [0] +ImageModel::ImageModel(QObject *parent) + : QAbstractTableModel(parent) +{ +} +//! [0] + +//! [1] +void ImageModel::setImage(const QImage &image) +{ + modelImage = image; + reset(); +} +//! [1] + +//! [2] +int ImageModel::rowCount(const QModelIndex & /* parent */) const +{ + return modelImage.height(); +} + +int ImageModel::columnCount(const QModelIndex & /* parent */) const +//! [2] //! [3] +{ + return modelImage.width(); +} +//! [3] + +//! [4] +QVariant ImageModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || role != Qt::DisplayRole) + return QVariant(); + return qGray(modelImage.pixel(index.column(), index.row())); +} +//! [4] + +//! [5] +QVariant ImageModel::headerData(int /* section */, + Qt::Orientation /* orientation */, + int role) const +{ + if (role == Qt::SizeHintRole) + return QSize(1, 1); + return QVariant(); +} +//! [5] diff --git a/examples/itemviews/pixelator/imagemodel.h b/examples/itemviews/pixelator/imagemodel.h new file mode 100644 index 0000000..e92115a --- /dev/null +++ b/examples/itemviews/pixelator/imagemodel.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef IMAGEMODEL_H +#define IMAGEMODEL_H + +#include <QAbstractTableModel> +#include <QImage> + +//! [0] +class ImageModel : public QAbstractTableModel +{ + Q_OBJECT + +public: + ImageModel(QObject *parent = 0); + + void setImage(const QImage &image); + + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + +private: + QImage modelImage; +}; +//! [0] + +#endif diff --git a/examples/itemviews/pixelator/images.qrc b/examples/itemviews/pixelator/images.qrc new file mode 100644 index 0000000..c105e13 --- /dev/null +++ b/examples/itemviews/pixelator/images.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/qt.png</file> +</qresource> +</RCC> diff --git a/examples/itemviews/pixelator/images/qt.png b/examples/itemviews/pixelator/images/qt.png Binary files differnew file mode 100644 index 0000000..a2c9c77 --- /dev/null +++ b/examples/itemviews/pixelator/images/qt.png diff --git a/examples/itemviews/pixelator/main.cpp b/examples/itemviews/pixelator/main.cpp new file mode 100644 index 0000000..7a30347 --- /dev/null +++ b/examples/itemviews/pixelator/main.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(images); + + QApplication app(argc, argv); + MainWindow window; + window.show(); + window.openImage(":/images/qt.png"); + return app.exec(); +} diff --git a/examples/itemviews/pixelator/mainwindow.cpp b/examples/itemviews/pixelator/mainwindow.cpp new file mode 100644 index 0000000..a2b98cf --- /dev/null +++ b/examples/itemviews/pixelator/mainwindow.cpp @@ -0,0 +1,245 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "imagemodel.h" +#include "mainwindow.h" +#include "pixeldelegate.h" + +//! [0] +MainWindow::MainWindow() +{ +//! [0] + currentPath = QDir::homePath(); + model = new ImageModel(this); + + QWidget *centralWidget = new QWidget; + +//! [1] + view = new QTableView; + view->setShowGrid(false); + view->horizontalHeader()->hide(); + view->verticalHeader()->hide(); + view->horizontalHeader()->setMinimumSectionSize(1); + view->verticalHeader()->setMinimumSectionSize(1); + view->setModel(model); +//! [1] + +//! [2] + PixelDelegate *delegate = new PixelDelegate(this); + view->setItemDelegate(delegate); +//! [2] + +//! [3] + QLabel *pixelSizeLabel = new QLabel(tr("Pixel size:")); + QSpinBox *pixelSizeSpinBox = new QSpinBox; + pixelSizeSpinBox->setMinimum(4); + pixelSizeSpinBox->setMaximum(32); + pixelSizeSpinBox->setValue(12); +//! [3] + + QMenu *fileMenu = new QMenu(tr("&File"), this); + QAction *openAction = fileMenu->addAction(tr("&Open...")); + openAction->setShortcut(QKeySequence(tr("Ctrl+O"))); + + printAction = fileMenu->addAction(tr("&Print...")); + printAction->setEnabled(false); + printAction->setShortcut(QKeySequence(tr("Ctrl+P"))); + + QAction *quitAction = fileMenu->addAction(tr("E&xit")); + quitAction->setShortcut(QKeySequence(tr("Ctrl+Q"))); + + QMenu *helpMenu = new QMenu(tr("&Help"), this); + QAction *aboutAction = helpMenu->addAction(tr("&About")); + + menuBar()->addMenu(fileMenu); + menuBar()->addSeparator(); + menuBar()->addMenu(helpMenu); + + connect(openAction, SIGNAL(triggered()), this, SLOT(chooseImage())); + connect(printAction, SIGNAL(triggered()), this, SLOT(printImage())); + connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAboutBox())); +//! [4] + connect(pixelSizeSpinBox, SIGNAL(valueChanged(int)), + delegate, SLOT(setPixelSize(int))); + connect(pixelSizeSpinBox, SIGNAL(valueChanged(int)), + this, SLOT(updateView())); +//! [4] + + QHBoxLayout *controlsLayout = new QHBoxLayout; + controlsLayout->addWidget(pixelSizeLabel); + controlsLayout->addWidget(pixelSizeSpinBox); + controlsLayout->addStretch(1); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(view); + mainLayout->addLayout(controlsLayout); + centralWidget->setLayout(mainLayout); + + setCentralWidget(centralWidget); + + setWindowTitle(tr("Pixelator")); + resize(640, 480); +//! [5] +} +//! [5] + +void MainWindow::chooseImage() +{ + QString fileName = QFileDialog::getOpenFileName(this, + tr("Choose an image"), currentPath, "*"); + + if (!fileName.isEmpty()) + openImage(fileName); +} + +void MainWindow::openImage(const QString &fileName) +{ + QImage image; + + if (image.load(fileName)) { + model->setImage(image); + if (!fileName.startsWith(":/")) { + currentPath = fileName; + setWindowTitle(tr("%1 - Pixelator").arg(currentPath)); + } + + printAction->setEnabled(true); + updateView(); + } +} + +void MainWindow::printImage() +{ +#ifndef QT_NO_PRINTER + if (model->rowCount(QModelIndex())*model->columnCount(QModelIndex()) + > 90000) { + QMessageBox::StandardButton answer; + answer = QMessageBox::question(this, tr("Large Image Size"), + tr("The printed image may be very large. Are you sure that " + "you want to print it?"), + QMessageBox::Yes | QMessageBox::No); + if (answer == QMessageBox::No) + return; + } + + QPrinter printer(QPrinter::HighResolution); + + QPrintDialog *dlg = new QPrintDialog(&printer, this); + dlg->setWindowTitle(tr("Print Image")); + + if (dlg->exec() != QDialog::Accepted) + return; + + QPainter painter; + painter.begin(&printer); + + int rows = model->rowCount(QModelIndex()); + int columns = model->columnCount(QModelIndex()); + int sourceWidth = (columns+1) * ItemSize; + int sourceHeight = (rows+1) * ItemSize; + + painter.save(); + + double xscale = printer.pageRect().width()/double(sourceWidth); + double yscale = printer.pageRect().height()/double(sourceHeight); + double scale = qMin(xscale, yscale); + + painter.translate(printer.paperRect().x() + printer.pageRect().width()/2, + printer.paperRect().y() + printer.pageRect().height()/2); + painter.scale(scale, scale); + painter.translate(-sourceWidth/2, -sourceHeight/2); + + QStyleOptionViewItem option; + QModelIndex parent = QModelIndex(); + + QProgressDialog progress(tr("Printing..."), tr("Cancel"), 0, rows, this); + progress.setWindowModality(Qt::ApplicationModal); + float y = ItemSize/2; + + for (int row = 0; row < rows; ++row) { + progress.setValue(row); + qApp->processEvents(); + if (progress.wasCanceled()) + break; + + float x = ItemSize/2; + + for (int column = 0; column < columns; ++column) { + option.rect = QRect(int(x), int(y), ItemSize, ItemSize); + view->itemDelegate()->paint(&painter, option, + model->index(row, column, parent)); + x = x + ItemSize; + } + y = y + ItemSize; + } + progress.setValue(rows); + + painter.restore(); + painter.end(); + + if (progress.wasCanceled()) { + QMessageBox::information(this, tr("Printing canceled"), + tr("The printing process was canceled."), QMessageBox::Cancel); + } +#else + QMessageBox::information(this, tr("Printing canceled"), + tr("Printing is not supported on this Qt build"), QMessageBox::Cancel); +#endif +} + +void MainWindow::showAboutBox() +{ + QMessageBox::about(this, tr("About the Pixelator example"), + tr("This example demonstrates how a standard view and a custom\n" + "delegate can be used to produce a specialized representation\n" + "of data in a simple custom model.")); +} + +//! [6] +void MainWindow::updateView() +{ + view->resizeColumnsToContents(); + view->resizeRowsToContents(); +} +//! [6] diff --git a/examples/itemviews/pixelator/mainwindow.h b/examples/itemviews/pixelator/mainwindow.h new file mode 100644 index 0000000..df9510e --- /dev/null +++ b/examples/itemviews/pixelator/mainwindow.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +class ImageModel; +QT_BEGIN_NAMESPACE +class QAction; +class QTableView; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + + void openImage(const QString &fileName); + +public slots: + void chooseImage(); + void printImage(); + void showAboutBox(); + void updateView(); + +private: + ImageModel *model; + QAction *printAction; + QString currentPath; + QTableView *view; +}; + +#endif diff --git a/examples/itemviews/pixelator/pixelator.pro b/examples/itemviews/pixelator/pixelator.pro new file mode 100644 index 0000000..dc05296 --- /dev/null +++ b/examples/itemviews/pixelator/pixelator.pro @@ -0,0 +1,14 @@ +HEADERS = imagemodel.h \ + mainwindow.h \ + pixeldelegate.h +SOURCES = imagemodel.cpp \ + main.cpp \ + mainwindow.cpp \ + pixeldelegate.cpp +RESOURCES += images.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/pixelator +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/pixelator +INSTALLS += target sources diff --git a/examples/itemviews/pixelator/pixeldelegate.cpp b/examples/itemviews/pixelator/pixeldelegate.cpp new file mode 100644 index 0000000..496b365 --- /dev/null +++ b/examples/itemviews/pixelator/pixeldelegate.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "pixeldelegate.h" + +//! [0] +PixelDelegate::PixelDelegate(QObject *parent) + : QAbstractItemDelegate(parent) +{ + pixelSize = 12; +} +//! [0] + +//! [1] +void PixelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const +{ +//! [2] + if (option.state & QStyle::State_Selected) + painter->fillRect(option.rect, option.palette.highlight()); +//! [1] + +//! [3] + int size = qMin(option.rect.width(), option.rect.height()); +//! [3] //! [4] + int brightness = index.model()->data(index, Qt::DisplayRole).toInt(); + double radius = (size/2.0) - (brightness/255.0 * size/2.0); + if (radius == 0.0) + return; +//! [4] + +//! [5] + painter->save(); +//! [5] //! [6] + painter->setRenderHint(QPainter::Antialiasing, true); +//! [6] //! [7] + painter->setPen(Qt::NoPen); +//! [7] //! [8] + if (option.state & QStyle::State_Selected) +//! [8] //! [9] + painter->setBrush(option.palette.highlightedText()); + else +//! [2] + painter->setBrush(QBrush(Qt::black)); +//! [9] + +//! [10] + painter->drawEllipse(QRectF(option.rect.x() + option.rect.width()/2 - radius, + option.rect.y() + option.rect.height()/2 - radius, + 2*radius, 2*radius)); + painter->restore(); +} +//! [10] + +//! [11] +QSize PixelDelegate::sizeHint(const QStyleOptionViewItem & /* option */, + const QModelIndex & /* index */) const +{ + return QSize(pixelSize, pixelSize); +} +//! [11] + +//! [12] +void PixelDelegate::setPixelSize(int size) +{ + pixelSize = size; +} +//! [12] diff --git a/examples/itemviews/pixelator/pixeldelegate.h b/examples/itemviews/pixelator/pixeldelegate.h new file mode 100644 index 0000000..b38e470 --- /dev/null +++ b/examples/itemviews/pixelator/pixeldelegate.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PIXELDELEGATE_H +#define PIXELDELEGATE_H + +#include <QAbstractItemDelegate> +#include <QFontMetrics> +#include <QModelIndex> +#include <QSize> + +QT_BEGIN_NAMESPACE +class QAbstractItemModel; +class QObject; +class QPainter; +QT_END_NAMESPACE + +static const int ItemSize = 256; + +//! [0] +class PixelDelegate : public QAbstractItemDelegate +{ + Q_OBJECT + +public: + PixelDelegate(QObject *parent = 0); + + void paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const; + + QSize sizeHint(const QStyleOptionViewItem &option, + const QModelIndex &index ) const; + +public slots: + void setPixelSize(int size); + +private: + int pixelSize; +}; +//! [0] + +#endif |