/
Command.cpp
244 lines (209 loc) · 8.31 KB
/
Command.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* Jürgen Riegel 2002 *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QAction>
# include <QFileDialog>
# include <QImage>
# include <QImageReader>
# include <QMessageBox>
# include <QTextStream>
#endif
#include <time.h>
#if defined(FC_OS_WIN32)
#include <sys/timeb.h>
#endif
#include <Base/Exception.h>
#include <Base/Tools.h>
#include <Base/Interpreter.h>
#include <App/Document.h>
#include <Gui/Application.h>
#include <Gui/MainWindow.h>
#include <Gui/Command.h>
#include <Gui/BitmapFactory.h>
#include "ImageOrientationDialog.h"
#if HAVE_OPENCV2
# include "opencv2/opencv.hpp"
#endif
#include "ImageView.h"
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
using namespace ImageGui;
DEF_STD_CMD(CmdImageOpen)
CmdImageOpen::CmdImageOpen()
: Command("Image_Open")
{
sAppModule = "Image";
sGroup = QT_TR_NOOP("Image");
sMenuText = QT_TR_NOOP("Open...");
sToolTipText = QT_TR_NOOP("Open image view");
sWhatsThis = "Image_Open";
sStatusTip = sToolTipText;
sPixmap = "Image_Open";
}
void CmdImageOpen::activated(int iMsg)
{
Q_UNUSED(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(), formats);
if (!s.isEmpty()) {
try {
s = Base::Tools::escapeEncodeFilename(s);
// load the file with the module
Command::doCommand(Command::Gui, "import Image, ImageGui");
#if PY_MAJOR_VERSION < 3
Command::doCommand(Command::Gui, "ImageGui.open(unicode(\"%s\",\"utf-8\"))", (const char*)s.toUtf8());
#else
Command::doCommand(Command::Gui, "ImageGui.open(\"%s\",\"utf-8\")", (const char*)s.toUtf8());
#endif
}
catch (const Base::PyException& e){
// Usually thrown if the file is invalid somehow
e.ReportException();
}
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DEF_STD_CMD_A(CmdCreateImagePlane)
CmdCreateImagePlane::CmdCreateImagePlane()
:Command("Image_CreateImagePlane")
{
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");
sWhatsThis = "Image_CreateImagePlane";
sStatusTip = sToolTipText;
sPixmap = "Image_CreateImagePlane";
}
void CmdCreateImagePlane::activated(int iMsg)
{
Q_UNUSED(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(), formats);
if (!s.isEmpty()) {
QImage impQ(s);
if (impQ.isNull()) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Error opening image"),
QObject::tr("Could not load the chosen image"));
return;
}
// ask user for orientation
ImageOrientationDialog Dlg;
if (Dlg.exec() != QDialog::Accepted)
return; // canceled
Base::Vector3d p = Dlg.Pos.getPosition();
Base::Rotation r = Dlg.Pos.getRotation();
std::string FeatName = getUniqueObjectName("ImagePlane");
double xPixelsPerM = impQ.dotsPerMeterX();
double width = impQ.width();
width = width * 1000 / xPixelsPerM;
int nWidth = static_cast<int>(width+0.5);
double yPixelsPerM = impQ.dotsPerMeterY();
double height = impQ.height();
height = height * 1000 / yPixelsPerM;
int nHeight = static_cast<int>(height+0.5);
QString pyfile = Base::Tools::escapeEncodeFilename(s);
openCommand(QT_TRANSLATE_NOOP("Command", "Create ImagePlane"));
doCommand(Doc,"App.activeDocument().addObject('Image::ImagePlane','%s\')",FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.ImageFile = '%s'",FeatName.c_str(),(const char*)pyfile.toUtf8());
doCommand(Doc,"App.activeDocument().%s.XSize = %d",FeatName.c_str(),nWidth);
doCommand(Doc,"App.activeDocument().%s.YSize = %d",FeatName.c_str(),nHeight);
doCommand(Doc,"App.activeDocument().%s.Placement = App.Placement(App.Vector(%f,%f,%f),App.Rotation(%f,%f,%f,%f))"
,FeatName.c_str(),p.x,p.y,p.z,r[0],r[1],r[2],r[3]);
doCommand(Doc,"Gui.SendMsgToActiveView('ViewFit')");
commitCommand();
}
}
bool CmdCreateImagePlane::isActive()
{
return App::GetApplication().getActiveDocument();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DEF_STD_CMD(CmdImageScaling)
CmdImageScaling::CmdImageScaling()
: Command("Image_Scaling")
{
sAppModule = "Image";
sGroup = QT_TR_NOOP("Image");
sMenuText = QT_TR_NOOP("Scale...");
sToolTipText = QT_TR_NOOP("Image Scaling");
sWhatsThis = "Image_Scaling";
sStatusTip = sToolTipText;
sPixmap = "Image_Scaling";
}
void CmdImageScaling::activated(int iMsg)
{
Q_UNUSED(iMsg);
// To Be Defined
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#if HAVE_OPENCV2
DEF_STD_CMD(CmdImageCapturerTest);
CmdImageCapturerTest::CmdImageCapturerTest()
: Command("Image_CapturerTest")
{
sAppModule = "Image";
sGroup = ("Image");
sMenuText = ("CapturerTest");
sToolTipText = ("test camara capturing");
sWhatsThis = "Image_CapturerTest";
sStatusTip = sToolTipText;
sPixmap = "camera-photo";
}
void CmdImageCapturerTest::activated(int iMsg)
{
using namespace cv;
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
}
#endif
void CreateImageCommands(void)
{
Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
rcCmdMgr.addCommand(new CmdImageOpen());
rcCmdMgr.addCommand(new CmdCreateImagePlane());
#if HAVE_OPENCV2
rcCmdMgr.addCommand(new CmdImageCapturerTest());
#endif
}