#include "MdispWindowQt.h"
#include <QApplication>
#include <QAction>
#include <QMainWindow>
#include <QMessageBox>
#include <QToolBar>
#include <mil.h>
#if M_MIL_USE_LINUX
#include <X11/Xlib.h>
#endif
#if STATIC_QT5
#include <QtPlugin>
#if !M_MIL_USE_LINUX
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
#else
Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
#endif
#endif
#define MIL_APPLICATION_NAME "MIL Application"
#define DEFAULT_IMAGE_SIZE_X 640
#define DEFAULT_IMAGE_SIZE_Y 480
#define DEFAULT_IMAGE_SIZE_BAND 1
void MilApplication(PaintArea *Area)
{
MIL_ID MilApplication,
MilDisplay,
MilSystem,
MilDigitizer,
MilImage;
MIL_INT BufSizeX;
MIL_INT BufSizeY;
MIL_INT BufSizeBand;
MappAlloc(M_NULL, M_DEFAULT, &MilApplication);
MsysAlloc(M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, M_DEFAULT, &MilSystem);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay);
if (MsysInquire(MilSystem, M_DIGITIZER_NUM, M_NULL) > 0)
{
MdigAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDigitizer);
MdigInquire(MilDigitizer, M_SIZE_X, &BufSizeX);
MdigInquire(MilDigitizer, M_SIZE_Y, &BufSizeY);
MdigInquire(MilDigitizer, M_SIZE_BAND, &BufSizeBand);
if((BufSizeX > DEFAULT_IMAGE_SIZE_X) || (BufSizeY > DEFAULT_IMAGE_SIZE_Y))
{
if(Area->parentWidget())
{
MilWindow* MainWindow = (MilWindow*)Area->parentWidget();
MainWindow->resize(BufSizeX, BufSizeY+ MainWindow->ToolBar()->height());
}
}
}
else
{
MilDigitizer = M_NULL;
BufSizeX = DEFAULT_IMAGE_SIZE_X;
BufSizeY = DEFAULT_IMAGE_SIZE_Y;
BufSizeBand = DEFAULT_IMAGE_SIZE_BAND;
}
MbufAllocColor(MilSystem, BufSizeBand, BufSizeX, BufSizeY, 8+M_UNSIGNED,
(MilDigitizer ? M_IMAGE+M_DISP+M_GRAB : M_IMAGE+M_DISP), &MilImage);
MbufClear(MilImage,0);
MdispSelectWindow(MilDisplay, MilImage, (MIL_WINDOW_HANDLE)Area->UserWindowHandle());
MgraFont(M_DEFAULT, M_FONT_DEFAULT_LARGE);
MgraText(M_DEFAULT, MilImage, (BufSizeX/8)*2, BufSizeY/2,
MIL_TEXT(" Welcome to MIL !!! "));
MgraRect(M_DEFAULT, MilImage, ((BufSizeX/8)*2)-60, (BufSizeY/2)-80,
((BufSizeX/8)*2)+370, (BufSizeY/2)+100);
MgraRect(M_DEFAULT, MilImage, ((BufSizeX/8)*2)-40, (BufSizeY/2)-60,
((BufSizeX/8)*2)+350, (BufSizeY/2)+80);
MgraRect(M_DEFAULT, MilImage, ((BufSizeX/8)*2)-20, (BufSizeY/2)-40,
((BufSizeX/8)*2)+330, (BufSizeY/2)+60);
QMessageBox::information(0, "MIL application example",
"\"Welcome to MIL !!!\" was printed", QMessageBox::Ok);
if (MilDigitizer)
{
MdigGrabContinuous(MilDigitizer, MilImage);
QMessageBox::information(0, "MIL application example",
"Continuous grab in progress", QMessageBox::Ok);
MdigHalt(MilDigitizer);
}
Area->setAttribute(Qt::WA_OpaquePaintEvent, false);
Area->setAttribute(Qt::WA_PaintOnScreen, false);
MdispSelect(MilDisplay, M_NULL);
MbufFree(MilImage);
if (MilDigitizer)
MdigFree(MilDigitizer);
MdispFree(MilDisplay);
MsysFree(MilSystem);
MappFree(MilApplication);
}
PaintArea::PaintArea(QWidget* parent)
: QWidget(parent)
, m_UserWindowHandle(0)
{
}
void PaintArea::startMil()
{
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground);
m_UserWindowHandle = winId();
MilApplication(this);
repaint();
}
bool PaintArea::event(QEvent* e )
{
#if QT_VERSION >= 0x040602 && M_MIL_USE_LINUX
if(e->type() == QEvent::WinIdChange || e->type() == QEvent::Show)
#else
if(e->type() == QEvent::Show)
#endif
m_UserWindowHandle = winId();
return QWidget::event(e);
}
QSize PaintArea::sizeHint() const
{
return QSize(DEFAULT_IMAGE_SIZE_X, DEFAULT_IMAGE_SIZE_Y);
}
MilWindow::MilWindow()
: QMainWindow(),
m_PaintArea(0)
{
setWindowTitle(MIL_APPLICATION_NAME);
QAction *startAct = new QAction(tr("&Start"), this);
startAct->setShortcut(tr("Ctrl+s"));
connect(startAct, SIGNAL(triggered()), this, SLOT(start()));
m_Tools = new QToolBar(tr("Tool Bar"), this);
m_Tools->addAction(startAct);
addToolBar(m_Tools);
m_PaintArea = new PaintArea(this);
m_PaintArea->resize(DEFAULT_IMAGE_SIZE_X, DEFAULT_IMAGE_SIZE_Y);
setCentralWidget(m_PaintArea);
}
void MilWindow::start()
{
m_PaintArea->startMil();
}
int main(int argc, char* argv[])
{
#if M_MIL_USE_LINUX
XInitThreads();
#endif
QApplication a(argc, argv);
MilWindow *w = new MilWindow;
w->show();
return a.exec();
}
#if STATIC_QT5
#include "moc_mdispwindowqt.cpp"
#endif