Mod/Image/Gui/Command.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU Library General Public License as       *
00005  *   published by the Free Software Foundation; either version 2 of the    *
00006  *   License, or (at your option) any later version.                       *
00007  *   for detail see the LICENCE text file.                                 *
00008  *   Jürgen Riegel 2002                                                    *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00012 #include "PreCompiled.h"
00013 #ifndef _PreComp_
00014 # include <QAction>
00015 # include <QFileDialog>
00016 # include <QImage>
00017 # include <QImageReader>
00018 # include <QMessageBox>
00019 # include <QTextStream>
00020 #endif
00021 
00022 #include <time.h>
00023 #include <sys/timeb.h>
00024 
00025 #include <Base/Exception.h>
00026 #include <Base/Interpreter.h>
00027 #include <App/Document.h>
00028 #include <Gui/Application.h>
00029 #include <Gui/MainWindow.h>
00030 #include <Gui/Command.h>
00031 #include <Gui/BitmapFactory.h>
00032 
00033 //#include <Mod/Image/App/CaptureClass.h>
00034 
00035 //#include <cv.h>
00036 //#include <highgui.h>
00037 
00038 
00039 #include "ImageView.h"
00040 
00041 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00042 
00043 using namespace ImageGui;
00044 
00045 DEF_STD_CMD(CmdImageOpen);
00046 
00047 CmdImageOpen::CmdImageOpen()
00048   : Command("Image_Open")
00049 {
00050     sAppModule      = "Image";
00051     sGroup          = QT_TR_NOOP("Image");
00052     sMenuText       = QT_TR_NOOP("Open...");
00053     sToolTipText    = QT_TR_NOOP("Open image view");
00054     sWhatsThis      = sToolTipText;
00055     sStatusTip      = sToolTipText;
00056     sPixmap         = "image-import";
00057 }
00058 
00059 void CmdImageOpen::activated(int iMsg)
00060 {
00061     // add all supported QImage formats
00062     QString formats;
00063     QTextStream str(&formats);
00064     str << QObject::tr("Images") << " (";
00065     QList<QByteArray> qtformats = QImageReader::supportedImageFormats();
00066     for (QList<QByteArray>::Iterator it = qtformats.begin(); it != qtformats.end(); ++it) {
00067         str << "*." << it->toLower() << " ";
00068     }
00069     str << ");;" << QObject::tr("All files") << " (*.*)";
00070     // Reading an image
00071     QString s = QFileDialog::getOpenFileName(Gui::getMainWindow(), QObject::tr("Choose an image file to open"),
00072                                              QString::null, formats);
00073     if (!s.isEmpty()) {
00074         try{
00075             // load the file with the module
00076             Command::doCommand(Command::Gui, "import Image, ImageGui");
00077             Command::doCommand(Command::Gui, "ImageGui.open(\"%s\")", (const char*)s.toUtf8());
00078         }
00079         catch (const Base::PyException& e){
00080             // Usually thrown if the file is invalid somehow
00081             e.ReportException();
00082         }
00083     }
00084 }
00085 
00086 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00087 DEF_STD_CMD_A(CmdCreateImagePlane);
00088 
00089 CmdCreateImagePlane::CmdCreateImagePlane()
00090     :Command("Image_CreateImagePlane")
00091 {
00092     sAppModule      = "Image";
00093     sGroup          = QT_TR_NOOP("Image");
00094     sMenuText       = QT_TR_NOOP("Create image plane...");
00095     sToolTipText    = QT_TR_NOOP("Create a planar image in the 3D space");
00096     sWhatsThis      = sToolTipText;
00097     sStatusTip      = sToolTipText;
00098     sPixmap         = "image-import";
00099 }
00100 
00101 void CmdCreateImagePlane::activated(int iMsg)
00102 {
00103     QString formats;
00104     QTextStream str(&formats);
00105     str << QObject::tr("Images") << " (";
00106     QList<QByteArray> qtformats = QImageReader::supportedImageFormats();
00107     for (QList<QByteArray>::Iterator it = qtformats.begin(); it != qtformats.end(); ++it) {
00108         str << "*." << it->toLower() << " ";
00109     }
00110     str << ");;" << QObject::tr("All files") << " (*.*)";
00111     // Reading an image
00112     QString s = QFileDialog::getOpenFileName(Gui::getMainWindow(), QObject::tr("Choose an image file to open"),
00113                                              QString::null, formats);
00114     if (!s.isEmpty()) {
00115 
00116         QImage impQ(s);
00117         if (impQ.isNull()) {
00118             QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Error open image"),
00119                 QObject::tr("Could not load the choosen image"));
00120             return;
00121         }
00122 
00123         std::string FeatName = getUniqueObjectName("ImagePlane");
00124 
00125         openCommand("Create ImagePlane");
00126         doCommand(Doc,"App.activeDocument().addObject('Image::ImagePlane','%s\')",FeatName.c_str());
00127         doCommand(Doc,"App.activeDocument().%s.ImageFile = '%s'",FeatName.c_str(),(const char*)s.toUtf8());
00128         doCommand(Doc,"App.activeDocument().%s.XSize = %d",FeatName.c_str(),impQ.width () );
00129         doCommand(Doc,"App.activeDocument().%s.YSize = %d",FeatName.c_str(),impQ.height() );
00130     }
00131 }
00132 
00133 bool CmdCreateImagePlane::isActive()
00134 {
00135     return App::GetApplication().getActiveDocument();
00136 }
00137 
00138 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00139 #if 0
00140 DEF_STD_CMD(CmdImageCapturerTest);
00141 
00142 CmdImageCapturerTest::CmdImageCapturerTest()
00143   : Command("Image_CapturerTest")
00144 {
00145     sAppModule      = "Image";
00146     sGroup          = ("Image");
00147     sMenuText       = ("CapturerTest");
00148     sToolTipText    = ("test camara capturing");
00149     sWhatsThis      = sToolTipText;
00150     sStatusTip      = sToolTipText;
00151     sPixmap         = "camera-photo";
00152 }
00153 
00154 void CmdImageCapturerTest::activated(int iMsg)
00155 {
00156 #if 0
00157     // Reading an image
00158     QString s = QFileDialog::getOpenFileName(Gui::getMainWindow(), QObject::tr("Choose an image file to open"), QString::null, 
00159                                              QObject::tr("Images (*.png *.xpm *.jpg *.bmp)"));
00160     if (s.isEmpty()) return;
00161 
00162     IplImage* image = cvLoadImage( 
00163         (const char*)s.toLatin1(),
00164         CV_LOAD_IMAGE_GRAYSCALE
00165     );
00166     IplImage* src = cvLoadImage( (const char*)s.toLatin1() ); //Changed for prettier show in color
00167     CvMemStorage* storage = cvCreateMemStorage(0);
00168     cvSmooth(image, image, CV_GAUSSIAN, 5, 5 );
00169     CvSeq* results = cvHoughCircles( 
00170         image, 
00171         storage, 
00172         CV_HOUGH_GRADIENT, 
00173         2, 
00174         image->width/10 
00175     ); 
00176     for( int i = 0; i < results->total; i++ ) {
00177         float* p = (float*) cvGetSeqElem( results, i );
00178         CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) );
00179         cvCircle( 
00180             src,
00181             pt, 
00182             cvRound( p[2] ),
00183             CV_RGB(0xff,0,0) 
00184         );
00185     }
00186     cvNamedWindow( "cvHoughCircles", 1 );
00187     cvShowImage( "cvHoughCircles", src);
00188     cvWaitKey(0);
00189 #else
00190     struct tm *newtime;
00191 #if defined (_MSC_VER)
00192     struct _timeb tstruct;
00193     __int64 ltime;
00194 #elif defined(__GNUC__)
00195     struct timeb tstruct;
00196     time_t ltime;
00197 #endif
00198 
00199     char buff[100];
00200     Capturerer cap(Capturerer::chooseCamNum());
00201     cap.setCaptureWindows(true);
00202     for(int i = 0; i< 200;i++){
00203 #if defined (_MSC_VER)
00204         _ftime( &tstruct ); 
00205         _time64( &ltime );
00206         // Obtain coordinated universal time:
00207         newtime = _gmtime64( &ltime ); // C4996
00208 #elif defined(__GNUC__)
00209         ftime( &tstruct ); 
00210         time( &ltime );
00211         // Obtain coordinated universal time:
00212         newtime = gmtime( &ltime ); // C4996
00213 #endif
00214         sprintf(buff,"%2d:%2d:%2d:%3d - %4d",newtime->tm_hour,newtime->tm_min,newtime->tm_sec,tstruct.millitm,i );
00215         if (cap.getOneCapture(buff)==27)
00216             break;
00217     }
00218 #endif
00219 }
00220 #endif
00221 
00222 void CreateImageCommands(void)
00223 {
00224     Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
00225 
00226     rcCmdMgr.addCommand(new CmdImageOpen());
00227     rcCmdMgr.addCommand(new CmdCreateImagePlane());
00228   //rcCmdMgr.addCommand(new CmdImageCapturerTest());
00229 }

Generated on Wed Nov 23 19:00:01 2011 for FreeCAD by  doxygen 1.6.1