341 lines
9.9 KiB
C++
341 lines
9.9 KiB
C++
#include <QHBoxLayout>
|
|
#include <QApplication>
|
|
#include <QMap>
|
|
#include <QDialog>
|
|
#include <QFileDialog>
|
|
#include <QKeyEvent>
|
|
#include "QtOpenfileDialogfactory.h"
|
|
#include "BaseObject.h"
|
|
#include "ObjectID.h"
|
|
#include "DialogFactory.h"
|
|
/*#include "geo"*/
|
|
using namespace pai::datamodel;
|
|
static inline void setupTreeViewEditorMargin(QLayout *lt)
|
|
{
|
|
enum { DecorationMargin = 4 };
|
|
if (QApplication::layoutDirection() == Qt::LeftToRight)
|
|
lt->setContentsMargins(DecorationMargin, 0, 0, 0);
|
|
else
|
|
lt->setContentsMargins(0, 0, DecorationMargin, 0);
|
|
}
|
|
|
|
template <class Value, class PrivateData>
|
|
static Value getData(const QMap<const QtProperty *, PrivateData> &propertyMap,
|
|
Value PrivateData::*data,
|
|
const QtProperty *property, const Value &defaultValue = Value())
|
|
{
|
|
typedef QMap<const QtProperty *, PrivateData> PropertyToData;
|
|
typedef Q_TYPENAME PropertyToData::const_iterator PropertyToDataConstIterator;
|
|
const PropertyToDataConstIterator it = propertyMap.constFind(property);
|
|
if (it == propertyMap.constEnd())
|
|
return defaultValue;
|
|
return it.value().*data;
|
|
}
|
|
|
|
template <class Value, class PrivateData>
|
|
static Value getValue(const QMap<const QtProperty *, PrivateData> &propertyMap,
|
|
const QtProperty *property, const Value &defaultValue = Value())
|
|
{
|
|
return getData<Value>(propertyMap, &PrivateData::val, property, defaultValue);
|
|
}
|
|
|
|
CQtOpenfileDialogFactory::CQtOpenfileDialogFactory(QObject *parent)
|
|
: QtAbstractEditorFactory<CQtOpenfileDialogPropertyManager>(parent), d_ptr(new CQtOpenfileDialogFactoryPrivate())
|
|
{
|
|
d_ptr->q_ptr = this;
|
|
|
|
}
|
|
|
|
/*!
|
|
Destroys this factory, and all the widgets it has created.
|
|
*/
|
|
CQtOpenfileDialogFactory::~CQtOpenfileDialogFactory()
|
|
{
|
|
qDeleteAll(d_ptr->m_editorToProperty.keys());
|
|
}
|
|
|
|
/*!
|
|
\internal
|
|
|
|
Reimplemented from the QtAbstractEditorFactory class.
|
|
*/
|
|
void CQtOpenfileDialogFactory::connectPropertyManager(CQtOpenfileDialogPropertyManager *manager)
|
|
{
|
|
connect(manager, SIGNAL(valueChanged(QtProperty*,int)),
|
|
this, SLOT(slotPropertyChanged(QtProperty*,int)));
|
|
}
|
|
|
|
|
|
/*!
|
|
\internal
|
|
|
|
Reimplemented from the QtAbstractEditorFactory class.
|
|
*/
|
|
QWidget *CQtOpenfileDialogFactory::createEditor(CQtOpenfileDialogPropertyManager *manager, QtProperty *property,
|
|
QWidget *parent)
|
|
{
|
|
|
|
CQtOpenfileDialogEditWidget *editor = d_ptr->createEditor(property, parent);
|
|
editor->defaultDir=manager->defaultDir;
|
|
editor->filter=manager->filter;
|
|
editor->setValue(manager->value(property));
|
|
connect(editor, SIGNAL(valueChanged(QString)), this, SLOT(slotSetValue(QString)));
|
|
connect(editor, SIGNAL(destroyed(QObject*)), this, SLOT(slotEditorDestroyed(QObject*)));
|
|
return editor;
|
|
|
|
}
|
|
|
|
/*!
|
|
\internal
|
|
|
|
Reimplemented from the QtAbstractEditorFactory class.
|
|
*/
|
|
void CQtOpenfileDialogFactory::disconnectPropertyManager(CQtOpenfileDialogPropertyManager *manager)
|
|
{
|
|
disconnect(manager, SIGNAL(valueChanged(QtProperty*,QString)),
|
|
this, SLOT(slotPropertyChanged(QtProperty*,QString)));
|
|
}
|
|
|
|
|
|
void CQtOpenfileDialogFactoryPrivate::slotPropertyChanged(QtProperty *property, QString value)
|
|
{
|
|
|
|
const PropertyToEditorListMap::iterator it = m_createdEditors.find(property);
|
|
if (it == m_createdEditors.end())
|
|
return;
|
|
QListIterator<CQtOpenfileDialogEditWidget *> itEditor(it.value());
|
|
|
|
while (itEditor.hasNext())
|
|
itEditor.next()->setValue(value);
|
|
}
|
|
|
|
void CQtOpenfileDialogFactoryPrivate::slotSetValue(QString value)
|
|
{
|
|
QObject *object = q_ptr->sender();
|
|
const QMap<CQtOpenfileDialogEditWidget *, QtProperty *>::ConstIterator ecend = m_editorToProperty.constEnd();
|
|
for (QMap<CQtOpenfileDialogEditWidget *, QtProperty *>::ConstIterator itEditor = m_editorToProperty.constBegin(); itEditor != ecend; ++itEditor)
|
|
if (itEditor.key() == object) {
|
|
QtProperty *property = itEditor.value();
|
|
CQtOpenfileDialogPropertyManager *manager = q_ptr->propertyManager(property);
|
|
if (!manager)
|
|
return;
|
|
manager->setValue(property, value);
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
CQtOpenfileDialogPropertyManager::CQtOpenfileDialogPropertyManager(QObject *parent)
|
|
: QtAbstractPropertyManager(parent), d_ptr(new CQtOpenfileDialogPropertyManagerPrivate)
|
|
{
|
|
d_ptr->q_ptr = this;
|
|
}
|
|
|
|
/*!
|
|
Destroys this manager, and all the properties it has created.
|
|
*/
|
|
CQtOpenfileDialogPropertyManager::~CQtOpenfileDialogPropertyManager()
|
|
{
|
|
clear();
|
|
}
|
|
|
|
/*!
|
|
Returns the given \a property's value which is an index in the
|
|
list returned by enumNames()
|
|
|
|
If the given property is not managed by this manager, this
|
|
function returns -1.
|
|
|
|
\sa enumNames(), setValue()
|
|
*/
|
|
QString CQtOpenfileDialogPropertyManager::value(const QtProperty *property) const
|
|
{
|
|
return d_ptr->m_values.value(property, QString());
|
|
}
|
|
|
|
/*!
|
|
Returns the given \a property's list of enum names.
|
|
|
|
\sa value(), setEnumNames()
|
|
*/
|
|
/*!
|
|
\reimp
|
|
*/
|
|
QString CQtOpenfileDialogPropertyManager::valueText(const QtProperty *property) const
|
|
{
|
|
const CQtOpenfileDialogPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property);
|
|
if (it == d_ptr->m_values.constEnd())
|
|
return QString();
|
|
QString avalue=*it;
|
|
QString strFilePath = avalue;//得到用户输入的文件名
|
|
strFilePath.replace("\\","/");
|
|
QString file = strFilePath.split("/").takeLast();
|
|
//QString filename = file.split(".").takeFirst();
|
|
return file;
|
|
}
|
|
|
|
/*!
|
|
\fn void QtLineStyleEnumPropertyManager::setValue(QtProperty *property, int value)
|
|
|
|
Sets the value of the given \a property to \a value.
|
|
|
|
The specified \a value must be less than the size of the given \a
|
|
property's enumNames() list, and larger than (or equal to) 0.
|
|
|
|
\sa value(), valueChanged()
|
|
*/
|
|
void CQtOpenfileDialogPropertyManager::setValue(QtProperty *property, QString val)
|
|
{
|
|
QString strFilePath = val;//得到用户输入的文件名
|
|
strFilePath.replace("\\","/");
|
|
QString file = strFilePath.split("/").takeLast();
|
|
//QString filename = file.split(".").takeFirst();
|
|
const CQtOpenfileDialogPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property);
|
|
if (it == d_ptr->m_values.end())
|
|
return;
|
|
|
|
*it = file;
|
|
//m_label->setText(filename);
|
|
emit propertyChanged(property);
|
|
emit valueChanged(property, val);
|
|
|
|
|
|
}
|
|
|
|
/*!
|
|
Sets the given \a property's list of enum names to \a
|
|
enumNames. The \a property's current value is reset to 0
|
|
indicating the first item of the list.
|
|
|
|
If the specified \a enumNames list is empty, the \a property's
|
|
current value is set to -1.
|
|
|
|
\sa enumNames(), enumNamesChanged()
|
|
*/
|
|
|
|
/*!
|
|
Sets the given \a property's map of enum values to their icons to \a
|
|
enumIcons.
|
|
|
|
Each enum value can have associated icon. This association is represented with passed \a enumIcons map.
|
|
|
|
\sa enumNames(), enumNamesChanged()
|
|
*/
|
|
/*!
|
|
\reimp
|
|
*/
|
|
void CQtOpenfileDialogPropertyManager::initializeProperty(QtProperty *property)
|
|
{
|
|
d_ptr->m_values[property] = "油层";
|
|
}
|
|
|
|
/*!
|
|
\reimp
|
|
*/
|
|
void CQtOpenfileDialogPropertyManager::uninitializeProperty(QtProperty *property)
|
|
{
|
|
d_ptr->m_values.remove(property);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//CQTtGeoStraEditWidget
|
|
CQtOpenfileDialogEditWidget::CQtOpenfileDialogEditWidget(QWidget *parent) :
|
|
QWidget(parent),
|
|
m_label(new QLabel),
|
|
m_button(new QToolButton),
|
|
m_fileName("")
|
|
{
|
|
QHBoxLayout *lt = new QHBoxLayout(this);
|
|
setupTreeViewEditorMargin(lt);
|
|
lt->setSpacing(0);
|
|
lt->addWidget(m_label);
|
|
lt->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored));
|
|
|
|
m_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored);
|
|
m_button->setFixedWidth(20);
|
|
setFocusProxy(m_button);
|
|
setFocusPolicy(m_button->focusPolicy());
|
|
m_button->setText(tr("..."));
|
|
m_button->installEventFilter(this);
|
|
connect(m_button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
|
|
lt->addWidget(m_button);
|
|
m_label->setText(m_fileName);
|
|
}
|
|
|
|
void CQtOpenfileDialogEditWidget::setValue(const QString &c)
|
|
{
|
|
if (m_fileName != c)
|
|
{
|
|
m_fileName = c;
|
|
m_label->setText(c);
|
|
}
|
|
}
|
|
|
|
void CQtOpenfileDialogEditWidget::buttonClicked()
|
|
{
|
|
QFileDialog fileDialog;
|
|
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);//设置文件对话框为保存模式
|
|
fileDialog.setFileMode(QFileDialog::AnyFile);//设置文件对话框弹出的时候显示任何文件,不论是文件夹还是文件
|
|
fileDialog.setViewMode(QFileDialog::Detail);//文件以详细的形式显示,显示文件大小、日期等内容
|
|
fileDialog.setWindowTitle("打开文件");
|
|
|
|
fileDialog.setDirectory(defaultDir);
|
|
fileDialog.setFilter(filter);
|
|
fileDialog.setOption(QFileDialog::DontConfirmOverwrite);
|
|
QString strFilePath;
|
|
if(fileDialog.exec() == QDialog::Accepted)
|
|
{
|
|
strFilePath = fileDialog.selectedFiles()[0];//得到用户输入的文件名
|
|
strFilePath.replace("\\","/");
|
|
QString file = strFilePath.split("/").takeLast();
|
|
//QString filename = file.split(".").takeFirst();
|
|
setValue(file);
|
|
emit valueChanged(strFilePath);
|
|
}
|
|
}
|
|
|
|
QString GetImagePath()
|
|
{
|
|
static QString imgpath;
|
|
if(imgpath!="") return imgpath;
|
|
QString strImagePath;
|
|
{
|
|
QString strPathTmp = QCoreApplication::applicationDirPath() + QDir::separator();
|
|
strImagePath = QDir::toNativeSeparators( strPathTmp );
|
|
QDir dir;
|
|
if( dir.exists( strPathTmp + "image" ) )
|
|
{
|
|
return strPathTmp + "image" + QDir::separator();
|
|
}else
|
|
{
|
|
strImagePath = strImagePath+".."+ "/LogPlus/image/";
|
|
}
|
|
}
|
|
imgpath=strImagePath;
|
|
return imgpath;
|
|
}
|
|
bool CQtOpenfileDialogEditWidget::eventFilter(QObject *obj, QEvent *ev)
|
|
{
|
|
if (obj == m_button) {
|
|
switch (ev->type()) {
|
|
case QEvent::KeyPress:
|
|
case QEvent::KeyRelease: { // Prevent the QToolButton from handling Enter/Escape meant control the delegate
|
|
switch (static_cast<const QKeyEvent*>(ev)->key()) {
|
|
case Qt::Key_Escape:
|
|
case Qt::Key_Enter:
|
|
case Qt::Key_Return:
|
|
ev->ignore();
|
|
return true;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
return QWidget::eventFilter(obj, ev);
|
|
} |