#include "importdialog.hxx"
#include <cadex/ACIS_Reader.hxx>
#include <cadex/Base_ProgressScope.hxx>
#include <cadex/Base_ProgressStatus.hxx>
#include <cadex/LicenseManager_Activate.h>
#include <cadex/ModelAlgo_BRepMesher.hxx>
#include <cadex/ModelData_Model.hxx>
#include <iostream>
#include <cmath>
#include <QtCore/QThreadPool>
#if QT_VERSION >= 0x050000
#include <QtWidgets/QApplication>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QLabel>
#include <QtWidgets/QProgressBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#else
#include <QtGui/QApplication>
#include <QtGui/QCheckBox>
#include <QtGui/QFileDialog>
#include <QtGui/QLabel>
#include <QtGui/QProgressBar>
#include <QtGui/QPushButton>
#include <QtGui/QVBoxLayout>
#endif
#include "../../cadex_license.cxx"
using namespace std;
static void Perform (
const Base_UTF16String& theFileName, ImportDialog* theOwner);
enum EventType {
ProgressValueId = QEvent::User + 1,
CompletedId
};
{
public:
CancellationChecker() : myIsCanceled (false) {}
__CADEX_DEFINE_PRIMITIVE_PROPERTY (bool, IsCanceled)
bool operator()() const override
{
return myIsCanceled;
}
};
class ProgressValueEvent : public QEvent
{
public:
ProgressValueEvent (int theValue) : QEvent (static_cast<QEvent::Type> (ProgressValueId)), myValue (theValue)
{}
int Value() const
{
return myValue;
}
private:
int myValue;
};
class ProgressBarProxy : public QObject
{
public:
ProgressBarProxy (QObject* theParent, QProgressBar* theProgressBar) :
QObject (theParent), myProgressBar (theProgressBar) {}
{
bool anIsGuiThread = thread() == QThread::currentThread();
int aValue = static_cast<int> (floor (theValue + 0.5f));
if (anIsGuiThread) {
myProgressBar->setValue (aValue);
QCoreApplication::processEvents();
} else {
QCoreApplication::postEvent (this, new ProgressValueEvent (aValue));
}
}
protected:
void customEvent (QEvent* theEvent) override
{
int aType = theEvent->type();
if (aType == ProgressValueId) {
ProgressValueEvent* anEvent = static_cast<ProgressValueEvent*> (theEvent);
int aValue = anEvent->Value();
myProgressBar->setValue (aValue);
theEvent->accept();
} else {
QObject::customEvent (theEvent);
}
}
QProgressBar* myProgressBar;
};
{
public:
ProgressBarObserver (ProgressBarProxy* theProxy) : myProxy (theProxy) {}
{
cout << theInfo.
Value() << endl;
}
{
cout << theInfo.
Value() <<
": complete!" << endl;
}
private:
ProgressBarProxy* myProxy;
};
class Worker : public QRunnable
{
public:
Worker (
const Base_UTF16String& theFileName, ImportDialog* theOwner) : myFileName (theFileName), myOwner (theOwner)
{}
protected:
void run() override
{
Perform (myFileName, myOwner);
}
ImportDialog* myOwner;
};
ImportDialog::ImportDialog()
{
QVBoxLayout *aMainLayout = new QVBoxLayout;
QLabel* aFileLabel = new QLabel (tr ("File:"));
myFileNameLabel = new QLabel ();
myFileNameLabel->setFrameShape (QFrame::StyledPanel);
QPushButton* aBrowseButton = new QPushButton (tr ("..."));
myThreadCheckBox = new QCheckBox (tr ("Start in a worker thread"));
myStartButton = new QPushButton (tr ("Start"));
myStartButton->setEnabled (false);
myCancelButton = new QPushButton (tr ("Cancel"));
myCancelButton->setEnabled (false);
myProgressBar = new QProgressBar;
myProgressBar->setMinimum (Base_ProgressStatus::MinValue());
myProgressBar->setMaximum (Base_ProgressStatus::MaxValue());
QHBoxLayout* aFileNameLayout = new QHBoxLayout;
aFileNameLayout->addWidget (aFileLabel);
aFileNameLayout->addWidget (myFileNameLabel, 1 );
aFileNameLayout->addWidget (aBrowseButton);
QHBoxLayout* aButtonLayout = new QHBoxLayout;
aButtonLayout->addWidget (myStartButton);
aButtonLayout->addWidget (myCancelButton);
aMainLayout->addLayout (aFileNameLayout);
aMainLayout->addWidget (myThreadCheckBox);
aMainLayout->addLayout (aButtonLayout);
aMainLayout->addSpacing(12);
aMainLayout->addWidget (myProgressBar);
aMainLayout->addStretch (1);
setLayout (aMainLayout);
myProgressBarProxy = new ProgressBarProxy (this, myProgressBar);
myCancellationChecker.reset (new CancellationChecker());
connect (aBrowseButton, SIGNAL (clicked()), this, SLOT (BrowseFile()));
connect (myStartButton, SIGNAL (clicked()), this, SLOT (Start()));
connect (myCancelButton, SIGNAL (clicked()), this, SLOT (Cancel()));
}
void ImportDialog::BrowseFile()
{
QString aFileName = QFileDialog::getOpenFileName (this, tr ("Open File"),
myFileNameLabel->text(), tr ("ACIS-SAT files (*.sat)"));
if (!aFileName.isEmpty()) {
myFileNameLabel->setText (aFileName);
myStartButton->setEnabled (true);
}
}
void ImportDialog::Start()
{
if (myCancellationChecker)
myCancellationChecker->IsCanceled() = false;
myStartButton->setEnabled (false);
myCancelButton->setEnabled (true);
myProgressBar->reset();
bool anIsWorkerThread = myThreadCheckBox->isChecked();
if (anIsWorkerThread) {
Worker* aWorker = new Worker (aFileName, this);
QThreadPool::globalInstance()->start (aWorker);
} else {
Perform (aFileName, this);
}
}
void ImportDialog::Cancel()
{
if (myCancellationChecker)
myCancellationChecker->IsCanceled() = true;
}
void ImportDialog::customEvent (QEvent* theEvent)
{
int aType = theEvent->type();
if (aType == CompletedId) {
myStartButton->setEnabled (true);
myCancelButton->setEnabled (false);
if (myCancellationChecker)
myCancellationChecker->IsCanceled() = true;
theEvent->accept();
} else {
QDialog::customEvent (theEvent);
}
}
static void Perform (
const Base_UTF16String& theFileName, ImportDialog* theOwner)
{
ProgressBarProxy* aProgressBarProxy = theOwner->GetProgressBarProxy();
ProgressBarObserver anObserver (aProgressBarProxy);
anObserver.SetAllNotifyingThreads();
{
if (theOwner->GetCancellationChecker()) {
}
bool anIsOK = false;
{
anIsOK = aReader.
ReadFile (theFileName);
}
}
}
}
}
QCoreApplication::postEvent (theOwner, new QEvent (static_cast<QEvent::Type> (CompletedId)));
}
int main (int argc, char *argv[])
{
auto aKey = LicenseKey::Value();
if (!CADExLicense_Activate (aKey)) {
cerr << "Failed to activate CAD Exchanger license." << endl;
return 1;
}
QApplication anApp (argc, argv);
ImportDialog aDialog;
return aDialog.exec();
return 0;
}
Reads ACIS files.
Definition: ACIS_Reader.hxx:28
Represents a node in a hierarchy of progress scopes.
Definition: Base_ProgressScope.hxx:31
Base abstract class for cancelation checker registered in Base_ProgressStatus.
Definition: Base_ProgressStatus.hxx:97
Base abstract class for observers registered in Base_ProgressStatus.
Definition: Base_ProgressStatus.hxx:43
Provides progress status and notification mechanism.
Definition: Base_ProgressStatus.hxx:33
void SetCancellationChecker(const CancellationChecker &theChecker)
Sets the cancellation checker.
Definition: Base_ProgressStatus.cxx:352
ValueType value_type
Defines a type of the progress status value and ranges.
Definition: Base_ProgressStatus.hxx:40
value_type Value() const
Returns a current value.
Definition: Base_ProgressStatus.cxx:304
bool WasCanceled() const
Returns true if the operation has been canceled.
Definition: Base_ProgressStatus.cxx:380
void Register(Observer &theObserver, value_type theValueThreshold=0.1f, unsigned int theTimeThreshold=20U)
Adds an observer that will be notified when the progress status has changed.
Definition: Base_ProgressStatus.cxx:324
bool Transfer(ModelData_Model &theModel)
Converts read file into a resulting model.
Definition: Base_Reader.cxx:296
bool ReadFile(const Base_UTF16String &theFileName)
Reads the file into memory.
Definition: Base_Reader.cxx:207
Base_ProgressStatus & ProgressStatus() const
Returns a progress status.
Definition: Base_Reader.cxx:418
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Computes a polygonal representation from a B-Rep one.
Definition: ModelAlgo_BRepMesher.hxx:48
Base_ProgressStatus & ProgressStatus() const
Returns a progress status.
Definition: ModelAlgo_BRepMesher.cxx:683
void Compute(const ModelData_Model &theModel, bool theEnforceAddition=false) const
Computes polygonal representations for all parts in the model.
Definition: ModelAlgo_BRepMesher.cxx:478
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:43
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22