Rework DropLineEdit. Make it type-sensitive
parent
8aba52170f
commit
27ece7f36a
@ -0,0 +1,82 @@
|
||||
#include "droplineedit.hpp"
|
||||
|
||||
#include <QDropEvent>
|
||||
|
||||
#include "../../model/world/tablemimedata.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
const CSMWorld::TableMimeData *getEventMimeData(QDropEvent *event)
|
||||
{
|
||||
Q_ASSERT(event != NULL);
|
||||
return dynamic_cast<const CSMWorld::TableMimeData *>(event->mimeData());
|
||||
}
|
||||
}
|
||||
|
||||
CSVWidget::DropLineEdit::DropLineEdit(QWidget *parent, CSMWorld::UniversalId::Type type)
|
||||
: QLineEdit(parent),
|
||||
mDropType(type)
|
||||
{
|
||||
setAcceptDrops(true);
|
||||
}
|
||||
|
||||
void CSVWidget::DropLineEdit::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
if (canAcceptEventData(event))
|
||||
{
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
}
|
||||
|
||||
void CSVWidget::DropLineEdit::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
if (canAcceptEventData(event))
|
||||
{
|
||||
event->accept();
|
||||
}
|
||||
}
|
||||
|
||||
void CSVWidget::DropLineEdit::dropEvent(QDropEvent *event)
|
||||
{
|
||||
const CSMWorld::TableMimeData *data = getEventMimeData(event);
|
||||
if (data == NULL) // May happen when non-records (e.g. plain text) are dragged and dropped
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int dataIndex = getAcceptedDataIndex(*data);
|
||||
if (dataIndex != -1)
|
||||
{
|
||||
setText(data->getData()[dataIndex].getId().c_str());
|
||||
emit tableMimeDataDropped(data->getData(), 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;
|
||||
}
|
||||
|
||||
std::vector<CSMWorld::UniversalId> idData = data.getData();
|
||||
int size = static_cast<int>(idData.size());
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
if (idData[i].getType() == mDropType)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
#ifndef CSV_WIDGET_DROPLINEEDIT_HPP
|
||||
#define CSV_WIDGET_DROPLINEEDIT_HPP
|
||||
|
||||
#include <QLineEdit>
|
||||
|
||||
#include "../../model/world/universalid.hpp"
|
||||
|
||||
namespace CSMDoc
|
||||
{
|
||||
class Document;
|
||||
}
|
||||
|
||||
namespace CSMWorld
|
||||
{
|
||||
class TableMimeData;
|
||||
}
|
||||
|
||||
namespace CSVWidget
|
||||
{
|
||||
class DropLineEdit : public QLineEdit
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
CSMWorld::UniversalId::Type mDropType;
|
||||
///< The accepted ID 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:
|
||||
DropLineEdit(QWidget *parent = 0,
|
||||
CSMWorld::UniversalId::Type type = CSMWorld::UniversalId::Type_None);
|
||||
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event);
|
||||
void dragMoveEvent(QDragMoveEvent *event);
|
||||
void dropEvent(QDropEvent *event);
|
||||
|
||||
signals:
|
||||
void tableMimeDataDropped(const std::vector<CSMWorld::UniversalId> &data,
|
||||
const CSMDoc::Document *document);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue