Create a context menu handler for dialogue editors with ID information

pull/646/head
Stanislav Bas 10 years ago
parent 6691891bee
commit 47b66b57ae

@ -19,6 +19,7 @@
#include <QComboBox>
#include <QHeaderView>
#include <QScrollBar>
#include <QMenu>
#include "../../model/world/nestedtableproxymodel.hpp"
#include "../../model/world/columnbase.hpp"
@ -314,6 +315,74 @@ CSVWorld::DialogueDelegateDispatcher::~DialogueDelegateDispatcher()
}
}
CSVWorld::IdContextMenu::IdContextMenu(QWidget *widget, CSMWorld::ColumnBase::Display display)
: QObject(widget),
mWidget(widget),
mIdType(CSMWorld::TableMimeData::convertEnums(display))
{
Q_ASSERT(mWidget != NULL);
Q_ASSERT(CSMWorld::ColumnBase::isId(display));
Q_ASSERT(mIdType != CSMWorld::UniversalId::Type_None);
mWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(mWidget,
SIGNAL(customContextMenuRequested(const QPoint &)),
this,
SLOT(showContextMenu(const QPoint &)));
mEditIdAction = new QAction(this);
QLineEdit *lineEdit = qobject_cast<QLineEdit *>(mWidget);
if (lineEdit != NULL)
{
mContextMenu = lineEdit->createStandardContextMenu();
mContextMenu->setParent(mWidget);
QAction *action = mContextMenu->actions().first();
mContextMenu->insertAction(action, mEditIdAction);
mContextMenu->insertSeparator(action);
}
else
{
mContextMenu = new QMenu(mWidget);
mContextMenu->addAction(mEditIdAction);
}
}
QString CSVWorld::IdContextMenu::getWidgetValue() const
{
static QLineEdit *lineEdit = qobject_cast<QLineEdit *>(mWidget);
static QLabel *label = qobject_cast<QLabel *>(mWidget);
QString value = "";
if (lineEdit != NULL)
{
value = lineEdit->text();
}
else if (label != NULL)
{
value = label->text();
}
return value;
}
void CSVWorld::IdContextMenu::showContextMenu(const QPoint &pos)
{
QString value = getWidgetValue();
if (!value.isEmpty())
{
mEditIdAction->setText("Edit '" + value + "'");
QAction *selectedAction = mContextMenu->exec(mWidget->mapToGlobal(pos));
if (selectedAction != NULL && selectedAction == mEditIdAction)
{
CSMWorld::UniversalId editId(mIdType, value.toUtf8().constData());
emit editIdRequest(editId, "");
}
}
}
/*
=============================================================EditWidget=====================================================
*/

@ -11,12 +11,14 @@
#include "../../model/world/columnbase.hpp"
#include "../../model/world/commanddispatcher.hpp"
#include "../../model/world/universalid.hpp"
class QDataWidgetMapper;
class QSize;
class QEvent;
class QLabel;
class QVBoxLayout;
class QMenu;
namespace CSMWorld
{
@ -149,6 +151,28 @@ namespace CSVWorld
CSMWorld::ColumnBase::Display display);
};
class IdContextMenu : public QObject
{
Q_OBJECT
QWidget *mWidget;
CSMWorld::UniversalId::Type mIdType;
QMenu *mContextMenu;
QAction *mEditIdAction;
QString getWidgetValue() const;
public:
IdContextMenu(QWidget *widget, CSMWorld::ColumnBase::Display display);
private slots:
void showContextMenu(const QPoint &pos);
signals:
void editIdRequest(const CSMWorld::UniversalId &id, const std::string &hint);
};
class EditWidget : public QScrollArea
{
Q_OBJECT

Loading…
Cancel
Save