RasterProcessTool/LAMPSARProcessProgram/ToolBoxWidget.cpp

225 lines
6.8 KiB
C++
Raw Normal View History

#include "ToolBoxWidget.h"
#include <QObject>
#include <QPushButton>
#include "QToolAbstract.h"
#include <QMenu>
#include "ui_ToolBoxWidget.h"
#include <QStringList>
#include <QDir>
#include <QFileInfo>
#ifdef Q_OS_WIN
#include <windows.h>
#include <WinBase.h>
#endif
#ifdef Q_OS_UNIX
#include <dlfcn.h>
#endif
ToolBoxWidget::ToolBoxWidget(LAMPMainWidget::RasterMainWidget* mainWindows, QWidget *parent)
: QWidget(parent), ui(new Ui::ToolBoxWidget), _mainWindows(mainWindows)
{
ui->setupUi(this);
setContextMenuPolicy(Qt::CustomContextMenu); // <20><><EFBFBD><EFBFBD><EFBFBD>Ҽ<EFBFBD><D2BC><EFBFBD><EFBFBD>ݲ˵<DDB2>
QObject::connect(this, SIGNAL(addBoxToolItemSIGNAL(QToolAbstract*)), this, SLOT(addBoxToolItemSLOT(QToolAbstract*)));
}
ToolBoxWidget::~ToolBoxWidget()
{}
void ToolBoxWidget::initMenu()
{
contextmenu = new QMenu(this);
//QAction* action1 = contextmenu->addAction(tr(u8"<22><><EFBFBD>ӹ<EFBFBD><D3B9><EFBFBD><EFBFBD><EFBFBD>"));
}
void ToolBoxWidget::initToolbox(QString dlltoolPath)
{
// <20><>ȡdll <20>ļ<EFBFBD><C4BC><EFBFBD>
QDir dir(dlltoolPath); // <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Ŀ¼
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>˹<EFBFBD><CBB9><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>Ұ<EFBFBD><D2B0><EFBFBD> "plugin_" <20><> dll <20>ļ<EFBFBD>
QStringList filters;
filters << "PluginTool_*.dll";
// <20><>ȡ<EFBFBD><C8A1>ǰĿ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><D0B7>Ϲ<EFBFBD><CFB9>˹<EFBFBD><CBB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
QFileInfoList files = dir.entryInfoList(filters, QDir::Files);
for (const QFileInfo& fileInfo : files) {
QString plugpath = fileInfo.absoluteFilePath();
qDebug() << "Found DLL:" << plugpath;
//continue;
QString pluginname = fileInfo.fileName();
typedef void (*Reg)(LAMPMainWidget::RasterMainWidget* mainwindows, ToolBoxWidget* toolbox);
Reg fun = nullptr;
if (!pluginname.toLower().startsWith("plugintool_"))
return ;
if (!pluginname.toLower().endsWith(".dll"))
return ;
HMODULE hmodel = LoadLibrary(LPCWSTR(plugpath.utf16()));
qDebug() << "Error Code :" + QString::number(GetLastError());
if (hmodel) {
fun = (Reg)GetProcAddress(hmodel, "RegisterPreToolBox");
if (fun) {
qDebug() << "loading Plugin started!!!";
fun(this->_mainWindows, this);
qDebug() << "loading Plugin finished : " << plugpath;
}
else {
FreeLibrary(hmodel);
qDebug() << "do not find Register function Plugin: " << plugpath;
return ;
}
}
else {
qDebug() << "loading Plugin dll Failure: " << plugpath;
}
}
}
void ToolBoxWidget::addBoxToolItemSLOT(QToolAbstract* item)
{
QVector<QString> xnodepath = item->getToolXpath();
QString toolName = item->getToolName();
QTreeWidgetItem* parentItem = findOrCreateParentItem(xnodepath);
// <20><><EFBFBD><EFBFBD><EFBFBD>ø<EFBFBD><C3B8><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> toolButton
if (parentItem && ui->treeWidgetToolBox->itemWidget(parentItem, 0) == nullptr) {
toollist.append(QToolboxTreeWidgetItem(ui->treeWidgetToolBox,parentItem,item));
//QTreeWidgetItem* actionItem = new QTreeWidgetItem(parentItem);
//parentItem->addChild(actionItem);
//actionItem->setText(0,toolName);
//QIcon icon(QString::fromUtf8(":/ToolBoxWidget/toolicon"));
//QPushButton* button = new QPushButton(ui->treeWidgetToolBox);
//button->setIcon(icon);
//button->setText(toolName);
//button->setLayoutDirection(Qt::LeftToRight);
//button->setStyleSheet("QPushButton { text-align: left; }");
//ui->treeWidgetToolBox->setItemWidget(actionItem, 0, button);
//connect(button, SIGNAL(clicked()), item, SLOT(excute()));
//item->setParent(ui->treeWidgetToolBox);
//qDebug() << "ToolButton bound to parent:" << actionItem->text(0);
}
else {
qDebug() << "ToolButton already bound to parent:" << parentItem->text(0);
}
}
// <20><><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>һ򴴽<D2BB><F2B4B4BD><EFBFBD><EFBFBD><EFBFBD>
QTreeWidgetItem* ToolBoxWidget::findOrCreateParentItem( QVector<QString>& path) {
QTreeWidgetItem* currentItem = nullptr;
// <20><><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6>ʼ<E3BFAA><CABC><EFBFBD><EFBFBD>
for ( QString& nodeName : path) {
if (currentItem == nullptr) {
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD>ڵ<EFBFBD>
currentItem = findOrCreateTopLevelItem(nodeName);
}
else {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽڵ㣬<DAB5><E3A3AC><EFBFBD>ҵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8>ӽڵ<D3BD>
currentItem = findChildItemByName(currentItem, nodeName);
if (currentItem == nullptr) {
currentItem = new QTreeWidgetItem(currentItem);
currentItem->setText(0, nodeName);
}
}
}
return currentItem;
}
// <20><><EFBFBD>Ҷ<EFBFBD><D2B6><EFBFBD><EFBFBD>ڵ㣬<DAB5><E3A3AC><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD><D2B5>򴴽<EFBFBD>
QTreeWidgetItem* ToolBoxWidget::findOrCreateTopLevelItem( QString& name) {
for (int i = 0; i < ui->treeWidgetToolBox->topLevelItemCount(); ++i) {
QTreeWidgetItem* item = ui->treeWidgetToolBox->topLevelItem(i);
if (item->text(0) == name) {
return item;
}
}
// <20><><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µĶ<C2B5><C4B6><EFBFBD><EFBFBD>ڵ<EFBFBD>
QTreeWidgetItem* newItem = new QTreeWidgetItem(ui->treeWidgetToolBox);
QIcon icon(QString::fromUtf8(":/RasterProcessTool/toolboxIcon"));
newItem->setIcon(0,icon);
newItem->setTextAlignment(0, Qt::AlignLeft);
newItem->setText(0, name);
return newItem;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ㣬<DAB5><E3A3AC><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD><D2B5>򷵻<EFBFBD> nullptr
QTreeWidgetItem* ToolBoxWidget::findChildItemByName(QTreeWidgetItem* parentItem, QString& name) {
for (int i = 0; i < parentItem->childCount(); ++i) {
QTreeWidgetItem* childItem = parentItem->child(i);
if (childItem->text(0) == name) {
return childItem;
}
}
return nullptr;
}
void ToolBoxWidget::OpenToolboxManagerWidget()
{
}
QToolboxTreeWidgetItem::QToolboxTreeWidgetItem(QTreeWidget* IntreeWidgetToolBox, QTreeWidgetItem* InparentItem, QToolAbstract* Initem)
{
this->parentItem = InparentItem;
this->item = Initem;
this->actionItem = new QTreeWidgetItem(parentItem);
this->button = new QPushButton(IntreeWidgetToolBox);
QString toolName = item->getToolName();
parentItem->addChild(actionItem);
actionItem->setText(0, toolName);
QIcon icon(QString::fromUtf8(":/RasterProcessTool/toolicon.png"));
button->setIcon(icon);
button->setText(toolName);
button->setLayoutDirection(Qt::LeftToRight);
button->setStyleSheet("QPushButton { text-align: left;font-size: 12px; font-weight: normal; }");
IntreeWidgetToolBox->setItemWidget(actionItem, 0, button);
QObject::connect(button, SIGNAL(clicked()), item, SLOT(excute()));
item->setParent(IntreeWidgetToolBox);
qDebug() << "ToolButton bound to parent:" << actionItem->text(0);
}
QToolboxTreeWidgetItem::~QToolboxTreeWidgetItem()
{
}
QTreeWidgetItem* QToolboxTreeWidgetItem::getTreeWidgetItem()
{
return this->actionItem;
}
QPushButton* QToolboxTreeWidgetItem::getPushButton()
{
return this->button;
}
QToolAbstract* QToolboxTreeWidgetItem::getToolAbstract()
{
return this->item;
}
QTreeWidgetItem* QToolboxTreeWidgetItem::getParantTreeWidgetItem()
{
return this->parentItem;
}