mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-26 09:35:28 +00:00
Refine DropLineEdit code
This commit is contained in:
parent
2565452052
commit
a23de394f8
@ -3,33 +3,20 @@
|
|||||||
#include <QDropEvent>
|
#include <QDropEvent>
|
||||||
|
|
||||||
#include "../../model/world/tablemimedata.hpp"
|
#include "../../model/world/tablemimedata.hpp"
|
||||||
|
#include "../../model/world/universalid.hpp"
|
||||||
|
|
||||||
namespace
|
#include "../world/dragdroputils.hpp"
|
||||||
{
|
|
||||||
const CSMWorld::TableMimeData *getEventMimeData(QDropEvent *event)
|
|
||||||
{
|
|
||||||
Q_ASSERT(event != NULL);
|
|
||||||
return dynamic_cast<const CSMWorld::TableMimeData *>(event->mimeData());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CSVWidget::DropLineEdit::DropLineEdit(CSMWorld::UniversalId::Type type, QWidget *parent)
|
CSVWidget::DropLineEdit::DropLineEdit(CSMWorld::ColumnBase::Display type, QWidget *parent)
|
||||||
: QLineEdit(parent),
|
: QLineEdit(parent),
|
||||||
mDropType(type)
|
mDropType(type)
|
||||||
{
|
{
|
||||||
setAcceptDrops(true);
|
setAcceptDrops(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
CSVWidget::DropLineEdit::DropLineEdit(CSMWorld::ColumnBase::Display display, QWidget *parent)
|
|
||||||
: QLineEdit(parent),
|
|
||||||
mDropType(CSMWorld::TableMimeData::convertEnums(display))
|
|
||||||
{
|
|
||||||
setAcceptDrops(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CSVWidget::DropLineEdit::dragEnterEvent(QDragEnterEvent *event)
|
void CSVWidget::DropLineEdit::dragEnterEvent(QDragEnterEvent *event)
|
||||||
{
|
{
|
||||||
if (canAcceptEventData(event))
|
if (CSVWorld::DragDropUtils::canAcceptData(*event, mDropType))
|
||||||
{
|
{
|
||||||
event->acceptProposedAction();
|
event->acceptProposedAction();
|
||||||
}
|
}
|
||||||
@ -37,7 +24,7 @@ void CSVWidget::DropLineEdit::dragEnterEvent(QDragEnterEvent *event)
|
|||||||
|
|
||||||
void CSVWidget::DropLineEdit::dragMoveEvent(QDragMoveEvent *event)
|
void CSVWidget::DropLineEdit::dragMoveEvent(QDragMoveEvent *event)
|
||||||
{
|
{
|
||||||
if (canAcceptEventData(event))
|
if (CSVWorld::DragDropUtils::canAcceptData(*event, mDropType))
|
||||||
{
|
{
|
||||||
event->accept();
|
event->accept();
|
||||||
}
|
}
|
||||||
@ -45,48 +32,10 @@ void CSVWidget::DropLineEdit::dragMoveEvent(QDragMoveEvent *event)
|
|||||||
|
|
||||||
void CSVWidget::DropLineEdit::dropEvent(QDropEvent *event)
|
void CSVWidget::DropLineEdit::dropEvent(QDropEvent *event)
|
||||||
{
|
{
|
||||||
const CSMWorld::TableMimeData *data = getEventMimeData(event);
|
if (CSVWorld::DragDropUtils::canAcceptData(*event, mDropType))
|
||||||
if (data == NULL) // May happen when non-records (e.g. plain text) are dragged and dropped
|
|
||||||
{
|
{
|
||||||
return;
|
CSMWorld::UniversalId id = CSVWorld::DragDropUtils::getAcceptedData(*event, mDropType);
|
||||||
}
|
setText(id.getId().c_str());
|
||||||
|
emit tableMimeDataDropped(id, CSVWorld::DragDropUtils::getTableMimeData(*event)->getDocumentPtr());
|
||||||
int dataIndex = getAcceptedDataIndex(*data);
|
|
||||||
if (dataIndex != -1)
|
|
||||||
{
|
|
||||||
std::vector<CSMWorld::UniversalId> idData = data->getData();
|
|
||||||
setText(idData[dataIndex].getId().c_str());
|
|
||||||
emit tableMimeDataDropped(idData[dataIndex], data->getDocumentPtr());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CSVWidget::DropLineEdit::canAcceptEventData(QDropEvent *event) const
|
|
||||||
{
|
|
||||||
const CSMWorld::TableMimeData *data = getEventMimeData(event);
|
|
||||||
if (data == NULL) // May happen when non-records (e.g. plain text) are dragged and dropped
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return getAcceptedDataIndex(*data) != -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int CSVWidget::DropLineEdit::getAcceptedDataIndex(const CSMWorld::TableMimeData &data) const
|
|
||||||
{
|
|
||||||
if (mDropType == CSMWorld::UniversalId::Type_None)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isReferenceable = mDropType == CSMWorld::UniversalId::Type_Referenceable;
|
|
||||||
std::vector<CSMWorld::UniversalId> idData = data.getData();
|
|
||||||
int size = static_cast<int>(idData.size());
|
|
||||||
for (int i = 0; i < size; ++i)
|
|
||||||
{
|
|
||||||
CSMWorld::UniversalId::Type type = idData[i].getType();
|
|
||||||
if (type == mDropType || isReferenceable && CSMWorld::TableMimeData::isReferencable(type))
|
|
||||||
{
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <QLineEdit>
|
#include <QLineEdit>
|
||||||
|
|
||||||
#include "../../model/world/columnbase.hpp"
|
#include "../../model/world/columnbase.hpp"
|
||||||
#include "../../model/world/universalid.hpp"
|
|
||||||
|
|
||||||
namespace CSMDoc
|
namespace CSMDoc
|
||||||
{
|
{
|
||||||
@ -14,6 +13,7 @@ namespace CSMDoc
|
|||||||
namespace CSMWorld
|
namespace CSMWorld
|
||||||
{
|
{
|
||||||
class TableMimeData;
|
class TableMimeData;
|
||||||
|
class UniversalId;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace CSVWidget
|
namespace CSVWidget
|
||||||
@ -22,20 +22,11 @@ namespace CSVWidget
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
CSMWorld::UniversalId::Type mDropType;
|
CSMWorld::ColumnBase::Display mDropType;
|
||||||
///< The accepted ID type for this LineEdit.
|
///< The accepted Display type for this LineEdit.
|
||||||
///< If \a mDropType has Type_None type, this LineEdit accepts all ID types
|
|
||||||
|
|
||||||
bool canAcceptEventData(QDropEvent *event) const;
|
|
||||||
///< Checks whether the \a event contains CSMWorld::TableMimeData with a proper ID type
|
|
||||||
|
|
||||||
int getAcceptedDataIndex(const CSMWorld::TableMimeData &data) const;
|
|
||||||
///< Checks whether the \a data has a proper type
|
|
||||||
///< \return -1 if there is no suitable data (ID type)
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DropLineEdit(CSMWorld::UniversalId::Type type, QWidget *parent = 0);
|
DropLineEdit(CSMWorld::ColumnBase::Display type, QWidget *parent = 0);
|
||||||
DropLineEdit(CSMWorld::ColumnBase::Display display, QWidget *parent = 0);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void dragEnterEvent(QDragEnterEvent *event);
|
void dragEnterEvent(QDragEnterEvent *event);
|
||||||
|
@ -243,7 +243,7 @@ QWidget *CSVWorld::CommandDelegate::createEditor (QWidget *parent, const QStyleO
|
|||||||
case CSMWorld::ColumnBase::Display_String:
|
case CSMWorld::ColumnBase::Display_String:
|
||||||
// For other Display types (that represent record IDs) with drop support IdCompletionDelegate is used
|
// For other Display types (that represent record IDs) with drop support IdCompletionDelegate is used
|
||||||
|
|
||||||
return new CSVWidget::DropLineEdit(CSMWorld::UniversalId::Type_None, parent);
|
return new CSVWidget::DropLineEdit(display, parent);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user