+ show all supported image formats in file dialog by Qt

git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5116 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d
This commit is contained in:
wmayer 2011-11-11 11:00:04 +00:00
parent c2fefaeff9
commit 39f9452295
2 changed files with 55 additions and 32 deletions

View File

@ -24,6 +24,7 @@
#ifndef _PreComp_
# include <QIcon>
# include <QImage>
# include <QFileInfo>
#endif
#include "ImageView.h"
@ -49,36 +50,39 @@ open(PyObject *self, PyObject *args)
PY_TRY {
QString fileName = QString::fromUtf8(Name);
Base::FileInfo file(Name);
if (file.hasExtension("png") || file.hasExtension("xpm") ||
file.hasExtension("jpg") || file.hasExtension("bmp"))
{
QImage imageq(fileName);
int format;
if (imageq.isNull() == false)
{
if ((imageq.depth() == 8) && (imageq.isGrayscale() == true))
format = IB_CF_GREY8;
else if ((imageq.depth() == 16) && (imageq.isGrayscale() == true))
format = IB_CF_GREY16;
else if ((imageq.depth() == 32) && (imageq.isGrayscale() == false))
format = IB_CF_BGRA32;
else
Py_Error(PyExc_Exception,"Unsupported image format");
}
else
Py_Error(PyExc_Exception,"Could not load image");
QFileInfo file(fileName);
// Load image from file into a QImage object
QImage imageq(fileName);
// Displaying the image in a view
ImageView* iView = new ImageView(Gui::getMainWindow());
iView->setWindowIcon( Gui::BitmapFactory().pixmap("colors") );
iView->setWindowTitle(QObject::tr("Image viewer"));
iView->resize( 400, 300 );
Gui::getMainWindow()->addWindow( iView );
iView->createImageCopy((void *)(imageq.bits()), (unsigned long)imageq.width(), (unsigned long)imageq.height(), format, 0);
// Extract image into a general RGB format recognised by the ImageView class
int format = IB_CF_RGB24;
unsigned char *pPixelData = NULL;
if (imageq.isNull() == false) {
pPixelData = new unsigned char[3 * (unsigned long)imageq.width() * (unsigned long)imageq.height()];
unsigned char *pPix = pPixelData;
for (int r = 0; r < imageq.height(); r++) {
for (int c = 0; c < imageq.width(); c++) {
QRgb rgb = imageq.pixel(c,r);
*pPix = (unsigned char)qRed(rgb);
*(pPix + 1) = (unsigned char)qGreen(rgb);
*(pPix + 2) = (unsigned char)qBlue(rgb);
pPix += 3;
}
}
}
else
Py_Error(PyExc_Exception,"unknown file ending");
Py_Error(PyExc_Exception,"Could not load image");
// Displaying the image in a view.
// This ImageView object takes ownership of the pixel data (in 'pointImageTo') so we don't need to delete it here
ImageView* iView = new ImageView(Gui::getMainWindow());
iView->setWindowIcon( Gui::BitmapFactory().pixmap("colors") );
iView->setWindowTitle(file.fileName());
iView->resize( 400, 300 );
Gui::getMainWindow()->addWindow( iView );
iView->pointImageTo((void *)pPixelData, (unsigned long)imageq.width(), (unsigned long)imageq.height(), format, 0, true);
} PY_CATCH;
Py_Return;

View File

@ -14,7 +14,9 @@
# include <QAction>
# include <QFileDialog>
# include <QImage>
# include <QImageReader>
# include <QMessageBox>
# include <QTextStream>
#endif
#include <time.h>
@ -56,9 +58,18 @@ CmdImageOpen::CmdImageOpen()
void CmdImageOpen::activated(int iMsg)
{
// add all supported QImage formats
QString formats;
QTextStream str(&formats);
str << QObject::tr("Images") << " (";
QList<QByteArray> qtformats = QImageReader::supportedImageFormats();
for (QList<QByteArray>::Iterator it = qtformats.begin(); it != qtformats.end(); ++it) {
str << "*." << it->toLower() << " ";
}
str << ");;" << QObject::tr("All files") << " (*.*)";
// Reading an image
QString s = QFileDialog::getOpenFileName(Gui::getMainWindow(), QObject::tr("Choose an image file to open"), QString::null,
QObject::tr("Images (*.png *.xpm *.jpg *.bmp)"));
QString s = QFileDialog::getOpenFileName(Gui::getMainWindow(), QObject::tr("Choose an image file to open"),
QString::null, formats);
if (!s.isEmpty()) {
try{
// load the file with the module
@ -81,7 +92,7 @@ CmdCreateImagePlane::CmdCreateImagePlane()
sAppModule = "Image";
sGroup = QT_TR_NOOP("Image");
sMenuText = QT_TR_NOOP("Create image plane...");
sToolTipText = QT_TR_NOOP("create a planar image in the 3D space");
sToolTipText = QT_TR_NOOP("Create a planar image in the 3D space");
sWhatsThis = sToolTipText;
sStatusTip = sToolTipText;
sPixmap = "image-import";
@ -89,9 +100,17 @@ CmdCreateImagePlane::CmdCreateImagePlane()
void CmdCreateImagePlane::activated(int iMsg)
{
QString formats;
QTextStream str(&formats);
str << QObject::tr("Images") << " (";
QList<QByteArray> qtformats = QImageReader::supportedImageFormats();
for (QList<QByteArray>::Iterator it = qtformats.begin(); it != qtformats.end(); ++it) {
str << "*." << it->toLower() << " ";
}
str << ");;" << QObject::tr("All files") << " (*.*)";
// Reading an image
QString s = QFileDialog::getOpenFileName(Gui::getMainWindow(), QObject::tr("Choose an image file to open"), QString::null,
QObject::tr("Images (*.png *.xpm *.jpg *.bmp)"));
QString s = QFileDialog::getOpenFileName(Gui::getMainWindow(), QObject::tr("Choose an image file to open"),
QString::null, formats);
if (!s.isEmpty()) {
QImage impQ(s);