DnD to load image file
This commit is contained in:
parent
a86efaaeb3
commit
771ec335fe
|
@ -1,43 +1,45 @@
|
||||||
#-------------------------------------------------
|
#-------------------------------------------------
|
||||||
#
|
#
|
||||||
# Project created by QtCreator 2019-09-26T23:36:07
|
# Project created by QtCreator 2019-09-26T23:36:07
|
||||||
#
|
#
|
||||||
#-------------------------------------------------
|
#-------------------------------------------------
|
||||||
|
|
||||||
QT += core gui
|
QT += core gui
|
||||||
|
|
||||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||||
|
|
||||||
TARGET = PineapplePictures
|
TARGET = PineapplePictures
|
||||||
TEMPLATE = app
|
TEMPLATE = app
|
||||||
|
|
||||||
# The following define makes your compiler emit warnings if you use
|
# The following define makes your compiler emit warnings if you use
|
||||||
# any feature of Qt which has been marked as deprecated (the exact warnings
|
# any feature of Qt which has been marked as deprecated (the exact warnings
|
||||||
# depend on your compiler). Please consult the documentation of the
|
# depend on your compiler). Please consult the documentation of the
|
||||||
# deprecated API in order to know how to port your code away from it.
|
# deprecated API in order to know how to port your code away from it.
|
||||||
DEFINES += QT_DEPRECATED_WARNINGS
|
DEFINES += QT_DEPRECATED_WARNINGS
|
||||||
|
|
||||||
# You can also make your code fail to compile if you use deprecated APIs.
|
# You can also make your code fail to compile if you use deprecated APIs.
|
||||||
# In order to do so, uncomment the following line.
|
# In order to do so, uncomment the following line.
|
||||||
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||||
|
|
||||||
CONFIG += c++11
|
CONFIG += c++11
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
main.cpp \
|
main.cpp \
|
||||||
mainwindow.cpp \
|
mainwindow.cpp \
|
||||||
graphicsview.cpp \
|
graphicsview.cpp \
|
||||||
bottombuttongroup.cpp
|
bottombuttongroup.cpp \
|
||||||
|
graphicsscene.cpp
|
||||||
HEADERS += \
|
|
||||||
mainwindow.h \
|
HEADERS += \
|
||||||
graphicsview.h \
|
mainwindow.h \
|
||||||
bottombuttongroup.h
|
graphicsview.h \
|
||||||
|
bottombuttongroup.h \
|
||||||
FORMS +=
|
graphicsscene.h
|
||||||
|
|
||||||
# Default rules for deployment.
|
FORMS +=
|
||||||
qnx: target.path = /tmp/$${TARGET}/bin
|
|
||||||
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
# Default rules for deployment.
|
||||||
!isEmpty(target.path): INSTALLS += target
|
qnx: target.path = /tmp/$${TARGET}/bin
|
||||||
|
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
||||||
|
!isEmpty(target.path): INSTALLS += target
|
||||||
|
|
34
graphicsscene.cpp
Normal file
34
graphicsscene.cpp
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
#include "graphicsscene.h"
|
||||||
|
|
||||||
|
#include <QGraphicsSceneMouseEvent>
|
||||||
|
#include <QMimeData>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QGraphicsItem>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
|
GraphicsScene::GraphicsScene(QObject *parent)
|
||||||
|
: QGraphicsScene(parent)
|
||||||
|
{
|
||||||
|
showText("Drag image here");
|
||||||
|
}
|
||||||
|
|
||||||
|
GraphicsScene::~GraphicsScene()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void GraphicsScene::showImage(const QPixmap &pixmap)
|
||||||
|
{
|
||||||
|
this->clear();
|
||||||
|
m_theThing = this->addPixmap(pixmap);
|
||||||
|
this->setSceneRect(m_theThing->boundingRect());
|
||||||
|
}
|
||||||
|
|
||||||
|
void GraphicsScene::showText(const QString &text)
|
||||||
|
{
|
||||||
|
this->clear();
|
||||||
|
QGraphicsTextItem * textItem = this->addText(text);
|
||||||
|
textItem->setDefaultTextColor(QColor("White"));
|
||||||
|
m_theThing = textItem;
|
||||||
|
this->setSceneRect(m_theThing->boundingRect());
|
||||||
|
}
|
20
graphicsscene.h
Normal file
20
graphicsscene.h
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef GRAPHICSSCENE_H
|
||||||
|
#define GRAPHICSSCENE_H
|
||||||
|
|
||||||
|
#include <QGraphicsScene>
|
||||||
|
|
||||||
|
class GraphicsScene : public QGraphicsScene
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
GraphicsScene(QObject *parent = nullptr);
|
||||||
|
~GraphicsScene();
|
||||||
|
|
||||||
|
void showImage(const QPixmap &pixmap);
|
||||||
|
void showText(const QString &text);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QGraphicsItem * m_theThing;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // GRAPHICSSCENE_H
|
|
@ -1,8 +1,12 @@
|
||||||
#include "graphicsview.h"
|
#include "graphicsview.h"
|
||||||
|
|
||||||
|
#include "graphicsscene.h"
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
#include <QScrollBar>
|
#include <QScrollBar>
|
||||||
|
#include <QMimeData>
|
||||||
|
#include <QImageReader>
|
||||||
|
|
||||||
GraphicsView::GraphicsView(QWidget *parent)
|
GraphicsView::GraphicsView(QWidget *parent)
|
||||||
: QGraphicsView (parent)
|
: QGraphicsView (parent)
|
||||||
|
@ -12,6 +16,27 @@ GraphicsView::GraphicsView(QWidget *parent)
|
||||||
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
setStyleSheet("background-color: rgba(0, 0, 0, 180);"
|
setStyleSheet("background-color: rgba(0, 0, 0, 180);"
|
||||||
"border-radius: 3px;");
|
"border-radius: 3px;");
|
||||||
|
setAcceptDrops(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GraphicsView::showImage(const QPixmap &pixmap)
|
||||||
|
{
|
||||||
|
scene()->showImage(pixmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GraphicsView::showText(const QString &text)
|
||||||
|
{
|
||||||
|
scene()->showText(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
GraphicsScene *GraphicsView::scene() const
|
||||||
|
{
|
||||||
|
return qobject_cast<GraphicsScene*>(QGraphicsView::scene());
|
||||||
|
}
|
||||||
|
|
||||||
|
void GraphicsView::setScene(GraphicsScene *scene)
|
||||||
|
{
|
||||||
|
return QGraphicsView::setScene(scene);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GraphicsView::mousePressEvent(QMouseEvent *event)
|
void GraphicsView::mousePressEvent(QMouseEvent *event)
|
||||||
|
@ -57,3 +82,52 @@ void GraphicsView::wheelEvent(QWheelEvent *event)
|
||||||
scale(0.8, 0.8);
|
scale(0.8, 0.8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GraphicsView::dragEnterEvent(QDragEnterEvent *event)
|
||||||
|
{
|
||||||
|
if (event->mimeData()->hasUrls() || event->mimeData()->hasImage() || event->mimeData()->hasText()) {
|
||||||
|
event->acceptProposedAction();
|
||||||
|
} else {
|
||||||
|
event->ignore();
|
||||||
|
}
|
||||||
|
qDebug() << event->mimeData() << "Drag Enter Event"
|
||||||
|
<< event->mimeData()->hasUrls() << event->mimeData()->hasImage()
|
||||||
|
<< event->mimeData()->formats() << event->mimeData()->hasFormat("text/uri-list");
|
||||||
|
|
||||||
|
return QGraphicsView::dragEnterEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GraphicsView::dragMoveEvent(QDragMoveEvent *event)
|
||||||
|
{
|
||||||
|
Q_UNUSED(event);
|
||||||
|
// by default, QGraphicsView/Scene will ignore the action if there are no QGraphicsItem under cursor.
|
||||||
|
// We actually doesn't care and would like to keep the drag event as-is, so just do nothing here.
|
||||||
|
}
|
||||||
|
|
||||||
|
void GraphicsView::dropEvent(QDropEvent *event)
|
||||||
|
{
|
||||||
|
event->acceptProposedAction();
|
||||||
|
|
||||||
|
const QMimeData * mimeData = event->mimeData();
|
||||||
|
|
||||||
|
if (mimeData->hasUrls()) {
|
||||||
|
QUrl url(mimeData->urls().first());
|
||||||
|
QImageReader imageReader(url.toLocalFile());
|
||||||
|
QImage::Format imageFormat = imageReader.imageFormat();
|
||||||
|
if (imageFormat == QImage::Format_Invalid) {
|
||||||
|
showText("File is not a valid image");
|
||||||
|
} else {
|
||||||
|
showImage(QPixmap::fromImageReader(&imageReader));
|
||||||
|
}
|
||||||
|
} else if (mimeData->hasImage()) {
|
||||||
|
QImage img = qvariant_cast<QImage>(mimeData->imageData());
|
||||||
|
QPixmap pixmap = QPixmap::fromImage(img);
|
||||||
|
if (pixmap.isNull()) {
|
||||||
|
showText("Image data is invalid");
|
||||||
|
} else {
|
||||||
|
showImage(pixmap);
|
||||||
|
}
|
||||||
|
} else if (mimeData->hasText()) {
|
||||||
|
showText(mimeData->text());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,16 +3,27 @@
|
||||||
|
|
||||||
#include <QGraphicsView>
|
#include <QGraphicsView>
|
||||||
|
|
||||||
|
class GraphicsScene;
|
||||||
class GraphicsView : public QGraphicsView
|
class GraphicsView : public QGraphicsView
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GraphicsView(QWidget *parent = nullptr);
|
GraphicsView(QWidget *parent = nullptr);
|
||||||
|
|
||||||
|
void showImage(const QPixmap &pixmap);
|
||||||
|
void showText(const QString &text);
|
||||||
|
|
||||||
|
GraphicsScene * scene() const;
|
||||||
|
void setScene(GraphicsScene *scene);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void mousePressEvent(QMouseEvent * event) override;
|
void mousePressEvent(QMouseEvent * event) override;
|
||||||
void mouseMoveEvent(QMouseEvent * event) override;
|
void mouseMoveEvent(QMouseEvent * event) override;
|
||||||
void mouseReleaseEvent(QMouseEvent * event) override;
|
void mouseReleaseEvent(QMouseEvent * event) override;
|
||||||
void wheelEvent(QWheelEvent *event) override;
|
void wheelEvent(QWheelEvent *event) override;
|
||||||
|
|
||||||
|
void dragEnterEvent(QDragEnterEvent *event) override;
|
||||||
|
void dragMoveEvent(QDragMoveEvent *event) override;
|
||||||
|
void dropEvent(QDropEvent *event) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // GRAPHICSVIEW_H
|
#endif // GRAPHICSVIEW_H
|
||||||
|
|
|
@ -2,12 +2,15 @@
|
||||||
|
|
||||||
#include "bottombuttongroup.h"
|
#include "bottombuttongroup.h"
|
||||||
#include "graphicsview.h"
|
#include "graphicsview.h"
|
||||||
|
#include "graphicsscene.h"
|
||||||
|
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
#include <QMovie>
|
#include <QMovie>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QGraphicsTextItem>
|
#include <QGraphicsTextItem>
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
#include <QStyle>
|
||||||
|
#include <QScreen>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
@ -33,9 +36,7 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||||
connect(m_exitAnimationGroup, &QParallelAnimationGroup::finished,
|
connect(m_exitAnimationGroup, &QParallelAnimationGroup::finished,
|
||||||
this, &QMainWindow::close);
|
this, &QMainWindow::close);
|
||||||
|
|
||||||
QGraphicsScene * scene = new QGraphicsScene(this);
|
GraphicsScene * scene = new GraphicsScene(this);
|
||||||
QGraphicsTextItem * textItem = scene->addText("Hello, world!");
|
|
||||||
textItem->setDefaultTextColor(QColor("White"));
|
|
||||||
|
|
||||||
GraphicsView * pictureView = new GraphicsView(this);
|
GraphicsView * pictureView = new GraphicsView(this);
|
||||||
pictureView->setScene(scene);
|
pictureView->setScene(scene);
|
||||||
|
@ -55,6 +56,15 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||||
this, &MainWindow::closeWindow);
|
this, &MainWindow::closeWindow);
|
||||||
|
|
||||||
m_bottomButtonGroup = new BottomButtonGroup(this);
|
m_bottomButtonGroup = new BottomButtonGroup(this);
|
||||||
|
|
||||||
|
this->setGeometry(
|
||||||
|
QStyle::alignedRect(
|
||||||
|
Qt::LeftToRight,
|
||||||
|
Qt::AlignCenter,
|
||||||
|
this->size(),
|
||||||
|
qApp->screenAt(QCursor::pos())->geometry()
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user