~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/draganddrop/puzzle/mainwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
#include <stdlib.h>
 
31
 
 
32
#include "mainwindow.h"
 
33
#include "pieceslist.h"
 
34
#include "puzzlewidget.h"
 
35
 
 
36
MainWindow::MainWindow(QWidget *parent)
 
37
    : QMainWindow(parent)
 
38
{
 
39
    setupMenus();
 
40
    setupWidgets();
 
41
 
 
42
    setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
 
43
    setWindowTitle(tr("Puzzle"));
 
44
}
 
45
 
 
46
void MainWindow::openImage(const QString &path)
 
47
{
 
48
    QString fileName = path;
 
49
 
 
50
    if (fileName.isNull())
 
51
        fileName = QFileDialog::getOpenFileName(this,
 
52
            tr("Open Image"), "", "Image Files (*.png *.jpg *.bmp)");
 
53
 
 
54
    if (!fileName.isEmpty()) {
 
55
        QPixmap newImage;
 
56
        if (!newImage.load(fileName)) {
 
57
            QMessageBox::warning(this, tr("Open Image"),
 
58
                                 tr("The image file could not be loaded."),
 
59
                                 QMessageBox::Cancel, QMessageBox::NoButton);
 
60
            return;
 
61
        }
 
62
        puzzleImage = newImage;
 
63
        setupPuzzle();
 
64
    }
 
65
}
 
66
 
 
67
void MainWindow::setCompleted()
 
68
{
 
69
    QMessageBox::information(this, tr("Puzzle Completed"),
 
70
        tr("Congratulations! You have completed the puzzle!\n"
 
71
           "Click OK to start again."),
 
72
        QMessageBox::Ok);
 
73
 
 
74
    setupPuzzle();
 
75
}
 
76
 
 
77
void MainWindow::setupPuzzle()
 
78
{
 
79
    int size = qMin(puzzleImage.width(), puzzleImage.height());
 
80
    puzzleImage = puzzleImage.copy((puzzleImage.width() - size)/2,
 
81
        (puzzleImage.height() - size)/2, size, size).scaled(400,
 
82
            400, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
 
83
 
 
84
    piecesList->clear();
 
85
 
 
86
    for (int y = 0; y < 5; ++y) {
 
87
        for (int x = 0; x < 5; ++x) {
 
88
            QPixmap pieceImage = puzzleImage.copy(x*80, y*80, 80, 80);
 
89
            piecesList->addPiece(pieceImage, QPoint(x, y));
 
90
        }
 
91
    }
 
92
 
 
93
    srand(QCursor::pos().x() ^ QCursor::pos().y());
 
94
 
 
95
    for (int i = 0; i < piecesList->count(); ++i) {
 
96
        if (int(2.0*rand()/(RAND_MAX+1.0)) == 1) {
 
97
            QListWidgetItem *item = piecesList->takeItem(i);
 
98
            piecesList->insertItem(0, item);
 
99
        }
 
100
    }
 
101
 
 
102
    puzzleWidget->clear();
 
103
}
 
104
 
 
105
void MainWindow::setupMenus()
 
106
{
 
107
    QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
 
108
 
 
109
    QAction *openAction = fileMenu->addAction(tr("&Open..."));
 
110
    openAction->setShortcut(QKeySequence(tr("Ctrl+O")));
 
111
 
 
112
    QAction *exitAction = fileMenu->addAction(tr("E&xit"));
 
113
    exitAction->setShortcut(QKeySequence(tr("Ctrl+Q")));
 
114
 
 
115
    QMenu *gameMenu = menuBar()->addMenu(tr("&Game"));
 
116
 
 
117
    QAction *restartAction = gameMenu->addAction(tr("&Restart"));
 
118
 
 
119
    connect(openAction, SIGNAL(triggered()), this, SLOT(openImage()));
 
120
    connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
 
121
    connect(restartAction, SIGNAL(triggered()), this, SLOT(setupPuzzle()));
 
122
}
 
123
 
 
124
void MainWindow::setupWidgets()
 
125
{
 
126
    QFrame *frame = new QFrame;
 
127
    QHBoxLayout *frameLayout = new QHBoxLayout(frame);
 
128
 
 
129
    piecesList = new PiecesList;
 
130
    puzzleWidget = new PuzzleWidget;
 
131
 
 
132
    connect(puzzleWidget, SIGNAL(puzzleCompleted()),
 
133
            this, SLOT(setCompleted()), Qt::QueuedConnection);
 
134
 
 
135
    frameLayout->addWidget(piecesList);
 
136
    frameLayout->addWidget(puzzleWidget);
 
137
    setCentralWidget(frame);
 
138
}