#include <QtGui>
#include <stdlib.h>
#include "caliper.h"
{
currentDistance = 0;
currentAngle = 0;
rotating = false;
moving = false;
piecePt
= QPointF(mainPix
().
rect().
width(),mainPix
().
rect().
height()/3.38);
movePt
= QPointF(mainPix
().
rect().
width() + piecePix
().
rect().
width(),
-2.0);
setMouseTracking(true);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
setWindowTitle(tr("Caliper Widget"));
}
void Caliper::setAngle(int angle)
{
if (currentAngle == angle)
return;
currentAngle = angle;
update(movePix().rect());
emit angleChanged(currentAngle);
}
void Caliper::setDistance(int distance)
{
if (currentDistance == distance)
return;
currentDistance = distance;
emit distanceChanged(currentDistance);
}
void Caliper::startRotate()
{
if (isRotating())
return;
else rotating=true;
}
bool Caliper::isMoving() const
{
return moving;
}
bool Caliper:: isRotating() const
{
return rotating;
}
void Caliper::startMeasure()
{
if (isMoving())
return;
else moving = true;
}
void Caliper::moveMeasurer()
{
movePix().rect().moveRight(movePix().rect().x() + currentDistance);
update();
}
{
if (event->button() == Qt::LeftButton)
{
dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
{
if (event->buttons() == Qt::LeftButton)
{
if (movePix().rect().contains( event->globalPos() - frameGeometry().topLeft()) )
{
movePix().rect().moveRight( dragPosition.x() - movePix().rect().x() );
update();
event->accept();
// setDistance(event->globalPos().x() - frameGeometry().topLeft().x());
}
else if (mainPix().rect().contains( dragPosition ) )
{
move(event->globalPos() - dragPosition);
event->accept();
}
}
}
{
if (event->button() == Qt::LeftButton)
{
event->accept();
return;
}
}
{
paintMove(painter);
paintMain(painter);
paintPiece(painter);
fullPix = mainPix().rect().unite(movePix().rect().unite(piecePix().rect()));
//QRegion maskedRegion(fullPix, QRegion::Rectangle);
//setMask(maskedRegion);
}
{
QPixmap result
(":/imgs/moveBody.png");
return result;
}
{
QPixmap result
(":/imgs/mainBody2.png");
copy.setMask(copy.mask());
return result;
}
{
QPixmap result
(":/imgs/pieceBody.png");
return result;
}
void Caliper
::paintMove(QPainter &painter
) {
painter.drawPixmap(movePt, movePix());
}
void Caliper
::paintMain(QPainter &painter
) {
painter.drawPixmap(mainPt, mainPix());
}
void Caliper
::paintPiece(QPainter &painter
) {
painter.drawPixmap(piecePt, piecePix().scaled((movePix().rect().x()+movePix().rect().width() ) - mainPix().rect().width(),
piecePix().rect().height(), Qt::IgnoreAspectRatio));
}
QSize Caliper
::sizeHint() const {
}
#include <QtGui>
#include <stdlib.h>
#include "caliper.h"
Caliper::Caliper(QWidget *parent)
: QWidget(parent)
{
currentDistance = 0;
currentAngle = 0;
rotating = false;
moving = false;
mainPt = QPointF(0.0, 0.0);
piecePt = QPointF(mainPix().rect().width(),mainPix().rect().height()/3.38);
movePt = QPointF(mainPix().rect().width() + piecePix().rect().width(), -2.0);
setMouseTracking(true);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
setWindowTitle(tr("Caliper Widget"));
}
void Caliper::setAngle(int angle)
{
if (currentAngle == angle)
return;
currentAngle = angle;
update(movePix().rect());
emit angleChanged(currentAngle);
}
void Caliper::setDistance(int distance)
{
if (currentDistance == distance)
return;
currentDistance = distance;
emit distanceChanged(currentDistance);
}
void Caliper::startRotate()
{
if (isRotating())
return;
else rotating=true;
}
bool Caliper::isMoving() const
{
return moving;
}
bool Caliper:: isRotating() const
{
return rotating;
}
void Caliper::startMeasure()
{
if (isMoving())
return;
else moving = true;
}
void Caliper::moveMeasurer()
{
movePix().rect().moveRight(movePix().rect().x() + currentDistance);
update();
}
void Caliper::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
void Caliper::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton)
{
if (movePix().rect().contains( event->globalPos() - frameGeometry().topLeft()) )
{
movePix().rect().moveRight( dragPosition.x() - movePix().rect().x() );
update();
event->accept();
// setDistance(event->globalPos().x() - frameGeometry().topLeft().x());
}
else if (mainPix().rect().contains( dragPosition ) )
{
move(event->globalPos() - dragPosition);
event->accept();
}
}
}
void Caliper::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
event->accept();
return;
}
}
void Caliper::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
paintMove(painter);
paintMain(painter);
paintPiece(painter);
fullPix = mainPix().rect().unite(movePix().rect().unite(piecePix().rect()));
//QRegion maskedRegion(fullPix, QRegion::Rectangle);
//setMask(maskedRegion);
}
QPixmap Caliper::movePix() const
{
QPixmap result(":/imgs/moveBody.png");
return result;
}
QPixmap Caliper::mainPix() const
{
QPixmap result(":/imgs/mainBody2.png");
QPixmap copy = result;
copy.setMask(copy.mask());
return result;
}
QPixmap Caliper::piecePix() const
{
QPixmap result(":/imgs/pieceBody.png");
return result;
}
void Caliper::paintMove(QPainter &painter)
{
painter.drawPixmap(movePt, movePix());
}
void Caliper::paintMain(QPainter &painter)
{
painter.drawPixmap(mainPt, mainPix());
}
void Caliper::paintPiece(QPainter &painter)
{
painter.drawPixmap(piecePt, piecePix().scaled((movePix().rect().x()+movePix().rect().width() ) - mainPix().rect().width(),
piecePix().rect().height(), Qt::IgnoreAspectRatio));
}
QSize Caliper::sizeHint() const
{
return QSize(400,300);
}
To copy to clipboard, switch view to plain text mode
Bookmarks