mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 14:16:16 -04:00
added for plugins own dir
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1850 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
97d8640f3a
commit
87344de7d4
809 changed files with 790 additions and 722 deletions
33
plugins/puzzle_plugin/src/PuzzlePlugin.cpp
Normal file
33
plugins/puzzle_plugin/src/PuzzlePlugin.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
//#include <QApplication>
|
||||
//#include <QString>
|
||||
//#include <QPushButton>
|
||||
|
||||
#include "PuzzlePlugin.h"
|
||||
#include "mainwindow.h"
|
||||
|
||||
QString
|
||||
PuzzlePlugin::pluginDescription() const
|
||||
{
|
||||
QString res;
|
||||
res = "A simple plugin, based on QT puzzle example" ;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
QString
|
||||
PuzzlePlugin::pluginName() const
|
||||
{
|
||||
return "Puzzle" ;
|
||||
}
|
||||
|
||||
QWidget*
|
||||
PuzzlePlugin::pluginWidget(QWidget * parent )
|
||||
{
|
||||
MainWindow* window = new MainWindow(parent);
|
||||
window->openImage(":/images/example.jpg");
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
|
||||
Q_EXPORT_PLUGIN2(puzzle_plugin, PuzzlePlugin)
|
27
plugins/puzzle_plugin/src/PuzzlePlugin.h
Normal file
27
plugins/puzzle_plugin/src/PuzzlePlugin.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#ifndef _HWA_PLUGIN_H_
|
||||
#define _HWA_PLUGIN_H_
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include <QString>
|
||||
#include <QWidget>
|
||||
|
||||
#include <PluginInterface.h>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
class PuzzlePlugin: public QObject, public PluginInterface
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_INTERFACES(PluginInterface)
|
||||
|
||||
public slots:
|
||||
|
||||
virtual QString pluginDescription() const ;
|
||||
virtual QString pluginName() const ;
|
||||
|
||||
virtual QWidget* pluginWidget(QWidget * parent = 0) ;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
160
plugins/puzzle_plugin/src/mainwindow.cpp
Normal file
160
plugins/puzzle_plugin/src/mainwindow.cpp
Normal file
|
@ -0,0 +1,160 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** Contact: Qt Software Information (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** Commercial Usage
|
||||
** Licensees holding valid Qt Commercial licenses may use this file in
|
||||
** accordance with the Qt Commercial License Agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and Nokia.
|
||||
**
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License versions 2.0 or 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 GNU General Public Licensing requirements will be met:
|
||||
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
||||
** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
||||
** exception, Nokia gives you certain additional rights. These rights
|
||||
** are described in the Nokia Qt GPL Exception version 1.3, included in
|
||||
** the file GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** Qt for Windows(R) Licensees
|
||||
** As a special exception, Nokia, as the sole copyright holder for Qt
|
||||
** Designer, grants users of the Qt/Eclipse Integration plug-in the
|
||||
** right for the Qt/Eclipse Integration to link to functionality
|
||||
** provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** contact the sales department at qt-sales@nokia.com.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtGui>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "mainwindow.h"
|
||||
#include "pieceslist.h"
|
||||
#include "puzzlewidget.h"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
:QFrame(parent)
|
||||
//: QMainWindow(parent)
|
||||
{
|
||||
//setupMenus();
|
||||
setupWidgets();
|
||||
|
||||
setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
|
||||
//setWindowTitle(tr("Puzzle"));
|
||||
}
|
||||
|
||||
void MainWindow::openImage(const QString &path)
|
||||
{
|
||||
QString fileName = path;
|
||||
|
||||
if (fileName.isNull())
|
||||
fileName = QFileDialog::getOpenFileName(this,
|
||||
tr("Open Image"), "", "Image Files (*.png *.jpg *.bmp)");
|
||||
|
||||
if (!fileName.isEmpty()) {
|
||||
QPixmap newImage;
|
||||
if (!newImage.load(fileName)) {
|
||||
QMessageBox::warning(this, tr("Open Image"),
|
||||
tr("The image file could not be loaded."),
|
||||
QMessageBox::Cancel);
|
||||
return;
|
||||
}
|
||||
puzzleImage = newImage;
|
||||
setupPuzzle();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::setCompleted()
|
||||
{
|
||||
QMessageBox::information(this, tr("Puzzle Completed"),
|
||||
tr("Congratulations! You have completed the puzzle!\n"
|
||||
"Click OK to start again."),
|
||||
QMessageBox::Ok);
|
||||
|
||||
setupPuzzle();
|
||||
}
|
||||
|
||||
void MainWindow::setupPuzzle()
|
||||
{
|
||||
int size = qMin(puzzleImage.width(), puzzleImage.height());
|
||||
puzzleImage = puzzleImage.copy((puzzleImage.width() - size)/2,
|
||||
(puzzleImage.height() - size)/2, size, size).scaled(400,
|
||||
400, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
||||
|
||||
piecesList->clear();
|
||||
|
||||
for (int y = 0; y < 5; ++y) {
|
||||
for (int x = 0; x < 5; ++x) {
|
||||
QPixmap pieceImage = puzzleImage.copy(x*80, y*80, 80, 80);
|
||||
piecesList->addPiece(pieceImage, QPoint(x, y));
|
||||
}
|
||||
}
|
||||
|
||||
qsrand(QCursor::pos().x() ^ QCursor::pos().y());
|
||||
|
||||
for (int i = 0; i < piecesList->count(); ++i) {
|
||||
if (int(2.0*qrand()/(RAND_MAX+1.0)) == 1) {
|
||||
QListWidgetItem *item = piecesList->takeItem(i);
|
||||
piecesList->insertItem(0, item);
|
||||
}
|
||||
}
|
||||
|
||||
puzzleWidget->clear();
|
||||
}
|
||||
/*
|
||||
void MainWindow::setupMenus()
|
||||
{
|
||||
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
||||
|
||||
QAction *openAction = fileMenu->addAction(tr("&Open..."));
|
||||
openAction->setShortcut(QKeySequence(tr("Ctrl+O")));
|
||||
|
||||
QAction *exitAction = fileMenu->addAction(tr("E&xit"));
|
||||
exitAction->setShortcut(QKeySequence(tr("Ctrl+Q")));
|
||||
|
||||
QMenu *gameMenu = menuBar()->addMenu(tr("&Game"));
|
||||
|
||||
QAction *restartAction = gameMenu->addAction(tr("&Restart"));
|
||||
|
||||
connect(openAction, SIGNAL(triggered()), this, SLOT(openImage()));
|
||||
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
||||
connect(restartAction, SIGNAL(triggered()), this, SLOT(setupPuzzle()));
|
||||
}
|
||||
*/
|
||||
void MainWindow::setupWidgets()
|
||||
{/*
|
||||
QFrame *frame = new QFrame;
|
||||
QHBoxLayout *frameLayout = new QHBoxLayout(frame);
|
||||
|
||||
piecesList = new PiecesList;
|
||||
puzzleWidget = new PuzzleWidget;
|
||||
|
||||
connect(puzzleWidget, SIGNAL(puzzleCompleted()),
|
||||
this, SLOT(setCompleted()), Qt::QueuedConnection);
|
||||
|
||||
frameLayout->addWidget(piecesList);
|
||||
frameLayout->addWidget(puzzleWidget);
|
||||
setCentralWidget(frame);
|
||||
*/
|
||||
|
||||
QHBoxLayout *frameLayout = new QHBoxLayout(this);
|
||||
|
||||
piecesList = new PiecesList;
|
||||
puzzleWidget = new PuzzleWidget;
|
||||
|
||||
connect(puzzleWidget, SIGNAL(puzzleCompleted()),
|
||||
this, SLOT(setCompleted()), Qt::QueuedConnection);
|
||||
|
||||
frameLayout->addWidget(piecesList);
|
||||
frameLayout->addWidget(puzzleWidget);
|
||||
}
|
74
plugins/puzzle_plugin/src/mainwindow.h
Normal file
74
plugins/puzzle_plugin/src/mainwindow.h
Normal file
|
@ -0,0 +1,74 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** Contact: Qt Software Information (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** Commercial Usage
|
||||
** Licensees holding valid Qt Commercial licenses may use this file in
|
||||
** accordance with the Qt Commercial License Agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and Nokia.
|
||||
**
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License versions 2.0 or 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 GNU General Public Licensing requirements will be met:
|
||||
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
||||
** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
||||
** exception, Nokia gives you certain additional rights. These rights
|
||||
** are described in the Nokia Qt GPL Exception version 1.3, included in
|
||||
** the file GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** Qt for Windows(R) Licensees
|
||||
** As a special exception, Nokia, as the sole copyright holder for Qt
|
||||
** Designer, grants users of the Qt/Eclipse Integration plug-in the
|
||||
** right for the Qt/Eclipse Integration to link to functionality
|
||||
** provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** contact the sales department at qt-sales@nokia.com.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QPixmap>
|
||||
//#include <QMainWindow>
|
||||
#include <QFrame>
|
||||
|
||||
class PiecesList;
|
||||
class PuzzleWidget;
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QListWidgetItem;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class MainWindow : public QFrame//QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow(QWidget *parent = 0);
|
||||
|
||||
public slots:
|
||||
void openImage(const QString &path = QString());
|
||||
void setupPuzzle();
|
||||
|
||||
private slots:
|
||||
void setCompleted();
|
||||
|
||||
private:
|
||||
//void setupMenus();
|
||||
void setupWidgets();
|
||||
|
||||
QPixmap puzzleImage;
|
||||
PiecesList *piecesList;
|
||||
PuzzleWidget *puzzleWidget;
|
||||
};
|
||||
|
||||
#endif
|
118
plugins/puzzle_plugin/src/pieceslist.cpp
Normal file
118
plugins/puzzle_plugin/src/pieceslist.cpp
Normal file
|
@ -0,0 +1,118 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** Contact: Qt Software Information (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** Commercial Usage
|
||||
** Licensees holding valid Qt Commercial licenses may use this file in
|
||||
** accordance with the Qt Commercial License Agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and Nokia.
|
||||
**
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License versions 2.0 or 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 GNU General Public Licensing requirements will be met:
|
||||
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
||||
** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
||||
** exception, Nokia gives you certain additional rights. These rights
|
||||
** are described in the Nokia Qt GPL Exception version 1.3, included in
|
||||
** the file GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** Qt for Windows(R) Licensees
|
||||
** As a special exception, Nokia, as the sole copyright holder for Qt
|
||||
** Designer, grants users of the Qt/Eclipse Integration plug-in the
|
||||
** right for the Qt/Eclipse Integration to link to functionality
|
||||
** provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** contact the sales department at qt-sales@nokia.com.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtGui>
|
||||
|
||||
#include "pieceslist.h"
|
||||
|
||||
PiecesList::PiecesList(QWidget *parent)
|
||||
: QListWidget(parent)
|
||||
{
|
||||
setDragEnabled(true);
|
||||
setViewMode(QListView::IconMode);
|
||||
setIconSize(QSize(60, 60));
|
||||
setSpacing(10);
|
||||
setAcceptDrops(true);
|
||||
setDropIndicatorShown(true);
|
||||
}
|
||||
|
||||
void PiecesList::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat("image/x-puzzle-piece"))
|
||||
event->accept();
|
||||
else
|
||||
event->ignore();
|
||||
}
|
||||
|
||||
void PiecesList::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat("image/x-puzzle-piece")) {
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else
|
||||
event->ignore();
|
||||
}
|
||||
|
||||
void PiecesList::dropEvent(QDropEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat("image/x-puzzle-piece")) {
|
||||
QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece");
|
||||
QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
|
||||
QPixmap pixmap;
|
||||
QPoint location;
|
||||
dataStream >> pixmap >> location;
|
||||
|
||||
addPiece(pixmap, location);
|
||||
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else
|
||||
event->ignore();
|
||||
}
|
||||
|
||||
void PiecesList::addPiece(QPixmap pixmap, QPoint location)
|
||||
{
|
||||
QListWidgetItem *pieceItem = new QListWidgetItem(this);
|
||||
pieceItem->setIcon(QIcon(pixmap));
|
||||
pieceItem->setData(Qt::UserRole, QVariant(pixmap));
|
||||
pieceItem->setData(Qt::UserRole+1, location);
|
||||
pieceItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable
|
||||
| Qt::ItemIsDragEnabled);
|
||||
}
|
||||
|
||||
void PiecesList::startDrag(Qt::DropActions /*supportedActions*/)
|
||||
{
|
||||
QListWidgetItem *item = currentItem();
|
||||
|
||||
QByteArray itemData;
|
||||
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
|
||||
QPixmap pixmap = qVariantValue<QPixmap>(item->data(Qt::UserRole));
|
||||
QPoint location = item->data(Qt::UserRole+1).toPoint();
|
||||
|
||||
dataStream << pixmap << location;
|
||||
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
mimeData->setData("image/x-puzzle-piece", itemData);
|
||||
|
||||
QDrag *drag = new QDrag(this);
|
||||
drag->setMimeData(mimeData);
|
||||
drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2));
|
||||
drag->setPixmap(pixmap);
|
||||
|
||||
if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
|
||||
delete takeItem(row(item));
|
||||
}
|
58
plugins/puzzle_plugin/src/pieceslist.h
Normal file
58
plugins/puzzle_plugin/src/pieceslist.h
Normal file
|
@ -0,0 +1,58 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** Contact: Qt Software Information (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** Commercial Usage
|
||||
** Licensees holding valid Qt Commercial licenses may use this file in
|
||||
** accordance with the Qt Commercial License Agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and Nokia.
|
||||
**
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License versions 2.0 or 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 GNU General Public Licensing requirements will be met:
|
||||
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
||||
** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
||||
** exception, Nokia gives you certain additional rights. These rights
|
||||
** are described in the Nokia Qt GPL Exception version 1.3, included in
|
||||
** the file GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** Qt for Windows(R) Licensees
|
||||
** As a special exception, Nokia, as the sole copyright holder for Qt
|
||||
** Designer, grants users of the Qt/Eclipse Integration plug-in the
|
||||
** right for the Qt/Eclipse Integration to link to functionality
|
||||
** provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** contact the sales department at qt-sales@nokia.com.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef PIECESLIST_H
|
||||
#define PIECESLIST_H
|
||||
|
||||
#include <QListWidget>
|
||||
|
||||
class PiecesList : public QListWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
PiecesList(QWidget *parent = 0);
|
||||
void addPiece(QPixmap pixmap, QPoint location);
|
||||
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event);
|
||||
void dragMoveEvent(QDragMoveEvent *event);
|
||||
void dropEvent(QDropEvent *event);
|
||||
void startDrag(Qt::DropActions supportedActions);
|
||||
};
|
||||
|
||||
#endif
|
201
plugins/puzzle_plugin/src/puzzlewidget.cpp
Normal file
201
plugins/puzzle_plugin/src/puzzlewidget.cpp
Normal file
|
@ -0,0 +1,201 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** Contact: Qt Software Information (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** Commercial Usage
|
||||
** Licensees holding valid Qt Commercial licenses may use this file in
|
||||
** accordance with the Qt Commercial License Agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and Nokia.
|
||||
**
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License versions 2.0 or 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 GNU General Public Licensing requirements will be met:
|
||||
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
||||
** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
||||
** exception, Nokia gives you certain additional rights. These rights
|
||||
** are described in the Nokia Qt GPL Exception version 1.3, included in
|
||||
** the file GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** Qt for Windows(R) Licensees
|
||||
** As a special exception, Nokia, as the sole copyright holder for Qt
|
||||
** Designer, grants users of the Qt/Eclipse Integration plug-in the
|
||||
** right for the Qt/Eclipse Integration to link to functionality
|
||||
** provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** contact the sales department at qt-sales@nokia.com.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtGui>
|
||||
|
||||
#include "puzzlewidget.h"
|
||||
|
||||
PuzzleWidget::PuzzleWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
setAcceptDrops(true);
|
||||
setMinimumSize(400, 400);
|
||||
setMaximumSize(400, 400);
|
||||
}
|
||||
|
||||
void PuzzleWidget::clear()
|
||||
{
|
||||
pieceLocations.clear();
|
||||
piecePixmaps.clear();
|
||||
pieceRects.clear();
|
||||
highlightedRect = QRect();
|
||||
inPlace = 0;
|
||||
update();
|
||||
}
|
||||
|
||||
void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat("image/x-puzzle-piece"))
|
||||
event->accept();
|
||||
else
|
||||
event->ignore();
|
||||
}
|
||||
|
||||
void PuzzleWidget::dragLeaveEvent(QDragLeaveEvent *event)
|
||||
{
|
||||
QRect updateRect = highlightedRect;
|
||||
highlightedRect = QRect();
|
||||
update(updateRect);
|
||||
event->accept();
|
||||
}
|
||||
|
||||
void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
QRect updateRect = highlightedRect.unite(targetSquare(event->pos()));
|
||||
|
||||
if (event->mimeData()->hasFormat("image/x-puzzle-piece")
|
||||
&& findPiece(targetSquare(event->pos())) == -1) {
|
||||
|
||||
highlightedRect = targetSquare(event->pos());
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
highlightedRect = QRect();
|
||||
event->ignore();
|
||||
}
|
||||
|
||||
update(updateRect);
|
||||
}
|
||||
|
||||
void PuzzleWidget::dropEvent(QDropEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat("image/x-puzzle-piece")
|
||||
&& findPiece(targetSquare(event->pos())) == -1) {
|
||||
|
||||
QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece");
|
||||
QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
|
||||
QRect square = targetSquare(event->pos());
|
||||
QPixmap pixmap;
|
||||
QPoint location;
|
||||
dataStream >> pixmap >> location;
|
||||
|
||||
pieceLocations.append(location);
|
||||
piecePixmaps.append(pixmap);
|
||||
pieceRects.append(square);
|
||||
|
||||
highlightedRect = QRect();
|
||||
update(square);
|
||||
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
|
||||
if (location == QPoint(square.x()/80, square.y()/80)) {
|
||||
inPlace++;
|
||||
if (inPlace == 25)
|
||||
emit puzzleCompleted();
|
||||
}
|
||||
} else {
|
||||
highlightedRect = QRect();
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
|
||||
int PuzzleWidget::findPiece(const QRect &pieceRect) const
|
||||
{
|
||||
for (int i = 0; i < pieceRects.size(); ++i) {
|
||||
if (pieceRect == pieceRects[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void PuzzleWidget::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
QRect square = targetSquare(event->pos());
|
||||
int found = findPiece(square);
|
||||
|
||||
if (found == -1)
|
||||
return;
|
||||
|
||||
QPoint location = pieceLocations[found];
|
||||
QPixmap pixmap = piecePixmaps[found];
|
||||
pieceLocations.removeAt(found);
|
||||
piecePixmaps.removeAt(found);
|
||||
pieceRects.removeAt(found);
|
||||
|
||||
if (location == QPoint(square.x()/80, square.y()/80))
|
||||
inPlace--;
|
||||
|
||||
update(square);
|
||||
|
||||
QByteArray itemData;
|
||||
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
|
||||
|
||||
dataStream << pixmap << location;
|
||||
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
mimeData->setData("image/x-puzzle-piece", itemData);
|
||||
|
||||
QDrag *drag = new QDrag(this);
|
||||
drag->setMimeData(mimeData);
|
||||
drag->setHotSpot(event->pos() - square.topLeft());
|
||||
drag->setPixmap(pixmap);
|
||||
|
||||
if (!(drag->exec(Qt::MoveAction) == Qt::MoveAction)) {
|
||||
pieceLocations.insert(found, location);
|
||||
piecePixmaps.insert(found, pixmap);
|
||||
pieceRects.insert(found, square);
|
||||
update(targetSquare(event->pos()));
|
||||
|
||||
if (location == QPoint(square.x()/80, square.y()/80))
|
||||
inPlace++;
|
||||
}
|
||||
}
|
||||
|
||||
void PuzzleWidget::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter;
|
||||
painter.begin(this);
|
||||
painter.fillRect(event->rect(), Qt::white);
|
||||
|
||||
if (highlightedRect.isValid()) {
|
||||
painter.setBrush(QColor("#ffcccc"));
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1));
|
||||
}
|
||||
|
||||
for (int i = 0; i < pieceRects.size(); ++i) {
|
||||
painter.drawPixmap(pieceRects[i], piecePixmaps[i]);
|
||||
}
|
||||
painter.end();
|
||||
}
|
||||
|
||||
const QRect PuzzleWidget::targetSquare(const QPoint &position) const
|
||||
{
|
||||
return QRect(position.x()/80 * 80, position.y()/80 * 80, 80, 80);
|
||||
}
|
82
plugins/puzzle_plugin/src/puzzlewidget.h
Normal file
82
plugins/puzzle_plugin/src/puzzlewidget.h
Normal file
|
@ -0,0 +1,82 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** Contact: Qt Software Information (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** Commercial Usage
|
||||
** Licensees holding valid Qt Commercial licenses may use this file in
|
||||
** accordance with the Qt Commercial License Agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and Nokia.
|
||||
**
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License versions 2.0 or 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 GNU General Public Licensing requirements will be met:
|
||||
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
||||
** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
||||
** exception, Nokia gives you certain additional rights. These rights
|
||||
** are described in the Nokia Qt GPL Exception version 1.3, included in
|
||||
** the file GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** Qt for Windows(R) Licensees
|
||||
** As a special exception, Nokia, as the sole copyright holder for Qt
|
||||
** Designer, grants users of the Qt/Eclipse Integration plug-in the
|
||||
** right for the Qt/Eclipse Integration to link to functionality
|
||||
** provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** contact the sales department at qt-sales@nokia.com.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef PUZZLEWIDGET_H
|
||||
#define PUZZLEWIDGET_H
|
||||
|
||||
#include <QList>
|
||||
#include <QPoint>
|
||||
#include <QPixmap>
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QDragEnterEvent;
|
||||
class QDropEvent;
|
||||
class QMouseEvent;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class PuzzleWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
PuzzleWidget(QWidget *parent = 0);
|
||||
void clear();
|
||||
|
||||
signals:
|
||||
void puzzleCompleted();
|
||||
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event);
|
||||
void dragLeaveEvent(QDragLeaveEvent *event);
|
||||
void dragMoveEvent(QDragMoveEvent *event);
|
||||
void dropEvent(QDropEvent *event);
|
||||
void mousePressEvent(QMouseEvent *event);
|
||||
void paintEvent(QPaintEvent *event);
|
||||
|
||||
private:
|
||||
int findPiece(const QRect &pieceRect) const;
|
||||
const QRect targetSquare(const QPoint &position) const;
|
||||
|
||||
QList<QPixmap> piecePixmaps;
|
||||
QList<QRect> pieceRects;
|
||||
QList<QPoint> pieceLocations;
|
||||
QRect highlightedRect;
|
||||
int inPlace;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue