git-svn-id: svn://db.shs.com.ru/libs@452 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
133
qad/widgets/image_view.cpp
Normal file
133
qad/widgets/image_view.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#include "image_view.h"
|
||||
#include <QGraphicsScene>
|
||||
#include <QBuffer>
|
||||
#include <QEvent>
|
||||
#include <QMouseEvent>
|
||||
#include <QWheelEvent>
|
||||
#include <QDebug>
|
||||
#include <qmath.h>
|
||||
|
||||
|
||||
ImageView::ImageView(QWidget * parent): QGraphicsView(parent) {
|
||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
setDragMode(QGraphicsView::NoDrag);
|
||||
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
|
||||
setScene(new QGraphicsScene());
|
||||
item.setTransformationMode(Qt::SmoothTransformation);
|
||||
item.setFlags(0);
|
||||
scene()->addItem(&item);
|
||||
viewport()->installEventFilter(this);
|
||||
autofit_ = true;
|
||||
}
|
||||
|
||||
|
||||
ImageView::~ImageView() {
|
||||
}
|
||||
|
||||
|
||||
QPixmap ImageView::pixmap() const {
|
||||
return item.pixmap();
|
||||
}
|
||||
|
||||
|
||||
void ImageView::setPixmap(QPixmap pixmap) {
|
||||
item.setPixmap(pixmap);
|
||||
adjustView();
|
||||
}
|
||||
|
||||
|
||||
void ImageView::setImage(const QImage & i) {
|
||||
im_data.clear();
|
||||
if (i.isNull()) {
|
||||
item.setPixmap(QPixmap());
|
||||
return;
|
||||
}
|
||||
QBuffer b(&im_data); b.open(QIODevice::ReadWrite);
|
||||
i.save(&b, "png");
|
||||
b.close();
|
||||
item.setPixmap(QPixmap::fromImage(i));
|
||||
adjustView();
|
||||
}
|
||||
|
||||
|
||||
void ImageView::setImage(const QByteArray & i) {
|
||||
im_data = i;
|
||||
if (i.isEmpty()) {
|
||||
item.setPixmap(QPixmap());
|
||||
return;
|
||||
}
|
||||
item.setPixmap(QPixmap::fromImage(QImage::fromData(i)));
|
||||
adjustView();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ImageView::clear() {
|
||||
im_data.clear();
|
||||
item.setPixmap(QPixmap());
|
||||
}
|
||||
|
||||
|
||||
void ImageView::mouseDoubleClickEvent(QMouseEvent * e) {
|
||||
autofit();
|
||||
}
|
||||
|
||||
|
||||
void ImageView::mousePressEvent(QMouseEvent * e) {
|
||||
QGraphicsView::mousePressEvent(e);
|
||||
emit clicked(mapToScene(e->pos()), e->buttons());
|
||||
}
|
||||
|
||||
|
||||
void ImageView::mouseMoveEvent(QMouseEvent * e) {
|
||||
//if (e->buttons().testFlag(Qt::RightButton) && !autofit_ && isInteractive()) return;;
|
||||
QGraphicsView::mouseMoveEvent(e);
|
||||
}
|
||||
|
||||
|
||||
void ImageView::wheelEvent(QWheelEvent * e) {
|
||||
if (!e->modifiers().testFlag(Qt::ControlModifier) || !isInteractive()) return;
|
||||
double scl = 1. + e->delta() / 500.;
|
||||
//autofit_ = false;
|
||||
//scale(scl, scl);
|
||||
}
|
||||
|
||||
|
||||
bool ImageView::eventFilter(QObject * o, QEvent * e) {
|
||||
QMouseEvent * me = (QMouseEvent *)e;
|
||||
switch (e->type()) {
|
||||
case QEvent::Resize:
|
||||
adjustView();
|
||||
break;
|
||||
case QEvent::MouseButtonPress:
|
||||
prev_pos = me->pos();
|
||||
break;
|
||||
case QEvent::MouseMove:
|
||||
if (!me->buttons().testFlag(Qt::RightButton) || autofit_ || !isInteractive()) break;
|
||||
{
|
||||
double scl = 1. / qSqrt(transform().determinant());
|
||||
QPointF dp = QPointF(me->pos() - prev_pos) * scl;
|
||||
//qDebug() << dp;
|
||||
//translate(0.00001, 0);
|
||||
prev_pos = me->pos();
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
return QGraphicsView::eventFilter(o, e);
|
||||
}
|
||||
|
||||
|
||||
void ImageView::adjustView() {
|
||||
if (!autofit_) return;
|
||||
setSceneRect(item.boundingRect());
|
||||
fitInView(&item, Qt::KeepAspectRatio);
|
||||
centerOn(&item);
|
||||
}
|
||||
|
||||
|
||||
void ImageView::autofit() {
|
||||
autofit_ = true;
|
||||
adjustView();
|
||||
}
|
||||
46
qad/widgets/image_view.h
Normal file
46
qad/widgets/image_view.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef IMAGE_VIEW_H
|
||||
#define IMAGE_VIEW_H
|
||||
|
||||
#include <QGraphicsView>
|
||||
#include <QGraphicsPixmapItem>
|
||||
|
||||
|
||||
class ImageView: public QGraphicsView
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap)
|
||||
public:
|
||||
ImageView(QWidget * parent = 0);
|
||||
~ImageView();
|
||||
|
||||
void setImage(const QImage & i);
|
||||
void setImage(const QByteArray & i);
|
||||
QByteArray image() const {return im_data;}
|
||||
QPixmap pixmap() const;
|
||||
|
||||
void clear();
|
||||
|
||||
private:
|
||||
void mouseDoubleClickEvent(QMouseEvent * e);
|
||||
void mousePressEvent(QMouseEvent * e);
|
||||
void mouseMoveEvent(QMouseEvent * e);
|
||||
void wheelEvent(QWheelEvent * e);
|
||||
bool eventFilter(QObject * o, QEvent * e);
|
||||
void adjustView();
|
||||
|
||||
QGraphicsPixmapItem item;
|
||||
QByteArray im_data;
|
||||
QPoint prev_pos;
|
||||
bool autofit_;
|
||||
|
||||
|
||||
public slots:
|
||||
void autofit();
|
||||
void setPixmap(QPixmap pixmap);
|
||||
|
||||
signals:
|
||||
void clicked(QPointF, Qt::MouseButtons);
|
||||
|
||||
};
|
||||
|
||||
#endif // IMAGE_VIEW_H
|
||||
69
qad/widgets/plugin/imageviewplugin.cpp
Normal file
69
qad/widgets/plugin/imageviewplugin.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "image_view.h"
|
||||
#include "imageviewplugin.h"
|
||||
#include <QtCore/QtPlugin>
|
||||
|
||||
|
||||
ImageViewPlugin::ImageViewPlugin(QObject * parent): QObject(parent) {
|
||||
m_initialized = false;
|
||||
}
|
||||
|
||||
|
||||
void ImageViewPlugin::initialize(QDesignerFormEditorInterface * /* core */) {
|
||||
if (m_initialized)
|
||||
return;
|
||||
|
||||
// Add extension registrations, etc. here
|
||||
|
||||
m_initialized = true;
|
||||
}
|
||||
|
||||
|
||||
bool ImageViewPlugin::isInitialized() const {
|
||||
return m_initialized;
|
||||
}
|
||||
|
||||
|
||||
QWidget * ImageViewPlugin::createWidget(QWidget * parent) {
|
||||
return new ImageView(parent);
|
||||
}
|
||||
|
||||
|
||||
QString ImageViewPlugin::name() const {
|
||||
return QLatin1String("ImageView");
|
||||
}
|
||||
|
||||
|
||||
QString ImageViewPlugin::group() const {
|
||||
return QLatin1String("Display Widgets");
|
||||
}
|
||||
|
||||
|
||||
QIcon ImageViewPlugin::icon() const {
|
||||
return QIcon(/*":/icons/spinslider.png"*/);
|
||||
}
|
||||
|
||||
|
||||
QString ImageViewPlugin::toolTip() const {
|
||||
return QLatin1String("Image viewer");
|
||||
}
|
||||
|
||||
|
||||
QString ImageViewPlugin::whatsThis() const {
|
||||
return QLatin1String("Image viewer");
|
||||
}
|
||||
|
||||
|
||||
bool ImageViewPlugin::isContainer() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
QString ImageViewPlugin::domXml() const {
|
||||
return QLatin1String("<widget class=\"ImageView\" name=\"imageView\">\n</widget>\n");
|
||||
}
|
||||
|
||||
|
||||
QString ImageViewPlugin::includeFile() const {
|
||||
return QLatin1String("image_view.h");
|
||||
}
|
||||
|
||||
36
qad/widgets/plugin/imageviewplugin.h
Normal file
36
qad/widgets/plugin/imageviewplugin.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef IMAGEVIEWPLUGIN_H
|
||||
#define IMAGEVIEWPLUGIN_H
|
||||
|
||||
#include <QObject>
|
||||
#if QT_VERSION >= 0x050000
|
||||
# include <QtUiPlugin/QDesignerCustomWidgetInterface>
|
||||
#else
|
||||
# include <QDesignerCustomWidgetInterface>
|
||||
#endif
|
||||
|
||||
class ImageViewPlugin: public QObject, public QDesignerCustomWidgetInterface
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_INTERFACES(QDesignerCustomWidgetInterface)
|
||||
|
||||
public:
|
||||
ImageViewPlugin(QObject * parent = 0);
|
||||
|
||||
bool isContainer() const;
|
||||
bool isInitialized() const;
|
||||
QIcon icon() const;
|
||||
QString domXml() const;
|
||||
QString group() const;
|
||||
QString includeFile() const;
|
||||
QString name() const;
|
||||
QString toolTip() const;
|
||||
QString whatsThis() const;
|
||||
QWidget * createWidget(QWidget * parent);
|
||||
void initialize(QDesignerFormEditorInterface * core);
|
||||
|
||||
private:
|
||||
bool m_initialized;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "qvarianteditplugin.h"
|
||||
#include "qpiconfigplugin.h"
|
||||
#include "evalspinboxplugin.h"
|
||||
#include "imageviewplugin.h"
|
||||
|
||||
|
||||
QADWidgets::QADWidgets(QObject * parent): QObject(parent) {
|
||||
@@ -32,6 +33,7 @@ QADWidgets::QADWidgets(QObject * parent): QObject(parent) {
|
||||
m_widgets.append(new QVariantEditPlugin(this));
|
||||
m_widgets.append(new QPIConfigPlugin(this));
|
||||
m_widgets.append(new EvalSpinBoxPlugin(this));
|
||||
m_widgets.append(new ImageViewPlugin(this));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user