[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 |
|
---|
| 43 | #include "imagemodel.h"
|
---|
| 44 | #include "mainwindow.h"
|
---|
| 45 | #include "pixeldelegate.h"
|
---|
| 46 |
|
---|
| 47 | //! [0]
|
---|
| 48 | MainWindow::MainWindow()
|
---|
| 49 | {
|
---|
| 50 | //! [0]
|
---|
| 51 | currentPath = QDir::homePath();
|
---|
| 52 | model = new ImageModel(this);
|
---|
| 53 |
|
---|
| 54 | QWidget *centralWidget = new QWidget;
|
---|
| 55 |
|
---|
| 56 | //! [1]
|
---|
| 57 | view = new QTableView;
|
---|
| 58 | view->setShowGrid(false);
|
---|
| 59 | view->horizontalHeader()->hide();
|
---|
| 60 | view->verticalHeader()->hide();
|
---|
| 61 | view->horizontalHeader()->setMinimumSectionSize(1);
|
---|
| 62 | view->verticalHeader()->setMinimumSectionSize(1);
|
---|
| 63 | view->setModel(model);
|
---|
| 64 | //! [1]
|
---|
| 65 |
|
---|
| 66 | //! [2]
|
---|
| 67 | PixelDelegate *delegate = new PixelDelegate(this);
|
---|
| 68 | view->setItemDelegate(delegate);
|
---|
| 69 | //! [2]
|
---|
| 70 |
|
---|
| 71 | //! [3]
|
---|
| 72 | QLabel *pixelSizeLabel = new QLabel(tr("Pixel size:"));
|
---|
| 73 | QSpinBox *pixelSizeSpinBox = new QSpinBox;
|
---|
| 74 | pixelSizeSpinBox->setMinimum(4);
|
---|
| 75 | pixelSizeSpinBox->setMaximum(32);
|
---|
| 76 | pixelSizeSpinBox->setValue(12);
|
---|
| 77 | //! [3]
|
---|
| 78 |
|
---|
| 79 | QMenu *fileMenu = new QMenu(tr("&File"), this);
|
---|
| 80 | QAction *openAction = fileMenu->addAction(tr("&Open..."));
|
---|
[561] | 81 | openAction->setShortcuts(QKeySequence::Open);
|
---|
[2] | 82 |
|
---|
| 83 | printAction = fileMenu->addAction(tr("&Print..."));
|
---|
| 84 | printAction->setEnabled(false);
|
---|
[561] | 85 | printAction->setShortcut(QKeySequence::Print);
|
---|
[2] | 86 |
|
---|
| 87 | QAction *quitAction = fileMenu->addAction(tr("E&xit"));
|
---|
[561] | 88 | quitAction->setShortcuts(QKeySequence::Quit);
|
---|
[2] | 89 |
|
---|
| 90 | QMenu *helpMenu = new QMenu(tr("&Help"), this);
|
---|
| 91 | QAction *aboutAction = helpMenu->addAction(tr("&About"));
|
---|
| 92 |
|
---|
| 93 | menuBar()->addMenu(fileMenu);
|
---|
| 94 | menuBar()->addSeparator();
|
---|
| 95 | menuBar()->addMenu(helpMenu);
|
---|
| 96 |
|
---|
| 97 | connect(openAction, SIGNAL(triggered()), this, SLOT(chooseImage()));
|
---|
| 98 | connect(printAction, SIGNAL(triggered()), this, SLOT(printImage()));
|
---|
| 99 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
---|
| 100 | connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAboutBox()));
|
---|
| 101 | //! [4]
|
---|
| 102 | connect(pixelSizeSpinBox, SIGNAL(valueChanged(int)),
|
---|
| 103 | delegate, SLOT(setPixelSize(int)));
|
---|
| 104 | connect(pixelSizeSpinBox, SIGNAL(valueChanged(int)),
|
---|
| 105 | this, SLOT(updateView()));
|
---|
| 106 | //! [4]
|
---|
| 107 |
|
---|
| 108 | QHBoxLayout *controlsLayout = new QHBoxLayout;
|
---|
| 109 | controlsLayout->addWidget(pixelSizeLabel);
|
---|
| 110 | controlsLayout->addWidget(pixelSizeSpinBox);
|
---|
| 111 | controlsLayout->addStretch(1);
|
---|
| 112 |
|
---|
| 113 | QVBoxLayout *mainLayout = new QVBoxLayout;
|
---|
| 114 | mainLayout->addWidget(view);
|
---|
| 115 | mainLayout->addLayout(controlsLayout);
|
---|
| 116 | centralWidget->setLayout(mainLayout);
|
---|
| 117 |
|
---|
| 118 | setCentralWidget(centralWidget);
|
---|
| 119 |
|
---|
| 120 | setWindowTitle(tr("Pixelator"));
|
---|
| 121 | resize(640, 480);
|
---|
| 122 | //! [5]
|
---|
| 123 | }
|
---|
| 124 | //! [5]
|
---|
| 125 |
|
---|
| 126 | void MainWindow::chooseImage()
|
---|
| 127 | {
|
---|
| 128 | QString fileName = QFileDialog::getOpenFileName(this,
|
---|
| 129 | tr("Choose an image"), currentPath, "*");
|
---|
| 130 |
|
---|
| 131 | if (!fileName.isEmpty())
|
---|
| 132 | openImage(fileName);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | void MainWindow::openImage(const QString &fileName)
|
---|
| 136 | {
|
---|
| 137 | QImage image;
|
---|
| 138 |
|
---|
| 139 | if (image.load(fileName)) {
|
---|
| 140 | model->setImage(image);
|
---|
| 141 | if (!fileName.startsWith(":/")) {
|
---|
| 142 | currentPath = fileName;
|
---|
| 143 | setWindowTitle(tr("%1 - Pixelator").arg(currentPath));
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | printAction->setEnabled(true);
|
---|
| 147 | updateView();
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | void MainWindow::printImage()
|
---|
| 152 | {
|
---|
| 153 | #ifndef QT_NO_PRINTER
|
---|
| 154 | if (model->rowCount(QModelIndex())*model->columnCount(QModelIndex())
|
---|
| 155 | > 90000) {
|
---|
| 156 | QMessageBox::StandardButton answer;
|
---|
| 157 | answer = QMessageBox::question(this, tr("Large Image Size"),
|
---|
| 158 | tr("The printed image may be very large. Are you sure that "
|
---|
| 159 | "you want to print it?"),
|
---|
| 160 | QMessageBox::Yes | QMessageBox::No);
|
---|
| 161 | if (answer == QMessageBox::No)
|
---|
| 162 | return;
|
---|
| |
---|