Skip to content

Commit a772f51

Browse files
committed
Modified to TextModel, format
1 parent 8fc3348 commit a772f51

8 files changed

Lines changed: 307 additions & 254 deletions

File tree

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#include "PluginDefinition.hpp"
22

3-
#include "TextSourceDataModel.hpp"
3+
#include "TextModel.hpp"
44

55
Plugin *Plugin::_this_plugin = nullptr;
66

7-
Plugin::Plugin() { _this_plugin = this; }
7+
Plugin::
8+
Plugin()
9+
{ _this_plugin = this; }
810

9-
void Plugin::registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> &reg)
11+
void
12+
Plugin::
13+
registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> &reg)
1014
{
11-
assert(reg);
15+
assert(reg);
1216

13-
reg->registerModel<TextSourceDataModel>();
17+
reg->registerModel<TextModel>();
1418
}

examples/plugin_text/PluginDefinition.hpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include <QtNodes/NodeDelegateModelRegistry>
55
#include <QtNodes/PluginInterface>
66

7-
#if defined(plugin_text_EXPORTS) // 这里需要和您的工程文件名一致${PROJECT_NAME}
7+
// This needs to be the same as the name of your project file ${PROJECT_NAME}
8+
// 这里需要和您的工程文件名一致 ${PROJECT_NAME}
9+
#if defined(plugin_text_EXPORTS)
810
#define DLL_EXPORT Q_DECL_EXPORT
911
#else
1012
#define DLL_EXPORT Q_DECL_IMPORT
@@ -13,24 +15,26 @@
1315
using QtNodes::NodeDelegateModelRegistry;
1416
using QtNodes::PluginInterface;
1517

16-
/* 插件名称,一个插件可以存在多个节点 */
1718
#define PLUGIN_NAME "plugin_text"
1819

1920
class DLL_EXPORT Plugin
2021
: public QObject
2122
, public QtNodes::PluginInterface
2223
{
23-
Q_OBJECT
24-
Q_INTERFACES(QtNodes::PluginInterface)
25-
Q_PLUGIN_METADATA(IID PLUGIN_NAME)
24+
Q_OBJECT
25+
Q_INTERFACES(QtNodes::PluginInterface)
26+
Q_PLUGIN_METADATA(IID PLUGIN_NAME)
2627

27-
public:
28-
Plugin();
28+
public:
29+
Plugin();
2930

30-
QString name() const override { return PLUGIN_NAME; };
31+
QString
32+
name() const override
33+
{ return PLUGIN_NAME; };
3134

32-
void registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> &reg) override;
35+
void
36+
registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> &reg) override;
3337

34-
private:
35-
static Plugin *_this_plugin;
38+
private:
39+
static Plugin *_this_plugin;
3640
};

examples/plugin_text/TextModel.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "TextModel.hpp"
2+
3+
#include <QtWidgets/QTextEdit>
4+
5+
TextModel::
6+
TextModel()
7+
: _lineEdit{nullptr}
8+
{
9+
//
10+
}
11+
12+
13+
unsigned int
14+
TextModel::
15+
nPorts(PortType portType) const
16+
{
17+
unsigned int result = 1;
18+
19+
switch (portType)
20+
{
21+
case PortType::In:
22+
result = 1;
23+
break;
24+
25+
case PortType::Out:
26+
result = 1;
27+
28+
default:
29+
break;
30+
}
31+
32+
return result;
33+
}
34+
35+
36+
void
37+
TextModel::
38+
onTextEdited()
39+
{
40+
Q_EMIT dataUpdated(0);
41+
}
42+
43+
44+
NodeDataType
45+
TextModel::
46+
dataType(PortType, PortIndex) const
47+
{
48+
return TextData().type();
49+
}
50+
51+
52+
std::shared_ptr<NodeData>
53+
TextModel::
54+
outData(PortIndex const portIndex)
55+
{
56+
Q_UNUSED(portIndex);
57+
return std::make_shared<TextData>(_lineEdit->toPlainText());
58+
}
59+
60+
61+
QWidget *
62+
TextModel::
63+
embeddedWidget()
64+
{
65+
if (!_lineEdit)
66+
{
67+
_lineEdit = new QTextEdit();
68+
69+
connect(_lineEdit, &QTextEdit::textChanged,
70+
this, &TextModel::onTextEdited);
71+
72+
}
73+
74+
return _lineEdit;
75+
}
76+
77+
78+
void
79+
TextModel::
80+
setInData(std::shared_ptr<NodeData> data, PortIndex const)
81+
{
82+
auto textData = std::dynamic_pointer_cast<TextData>(data);
83+
84+
QString inputText;
85+
86+
if (textData)
87+
{
88+
inputText = textData->text();
89+
}
90+
else
91+
{
92+
inputText = "";
93+
}
94+
95+
_lineEdit->setText(inputText);
96+
}
97+

examples/plugin_text/TextSourceDataModel.hpp renamed to examples/plugin_text/TextModel.hpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,32 @@ using QtNodes::PortIndex;
1313
using QtNodes::NodeData;
1414
using QtNodes::NodeDelegateModel;
1515

16-
class QLineEdit;
16+
class QTextEdit;
1717

1818
/// The model dictates the number of inputs and outputs for the Node.
1919
/// In this example it has no logic.
20-
class TextSourceDataModel : public NodeDelegateModel
20+
class TextModel : public NodeDelegateModel
2121
{
2222
Q_OBJECT
2323

2424
public:
25-
TextSourceDataModel();
25+
TextModel();
2626

2727
public:
2828
QString
2929
caption() const override
30-
{ return QString("Text Source"); }
30+
{ return QString("Text"); }
3131

3232
bool
33-
captionVisible() const override { return false; }
33+
captionVisible() const override { return true; }
3434

3535
static QString
3636
Name()
37-
{ return QString("TextSourceDataModel"); }
37+
{ return QString("TextModel"); }
3838

3939
QString
4040
name() const override
41-
{ return TextSourceDataModel::Name(); }
41+
{ return TextModel::Name(); }
4242

4343
public:
4444
unsigned int
@@ -51,16 +51,19 @@ class TextSourceDataModel : public NodeDelegateModel
5151
outData(PortIndex const portIndex) override;
5252

5353
void
54-
setInData(std::shared_ptr<NodeData>, PortIndex const) override { }
54+
setInData(std::shared_ptr<NodeData>, PortIndex const) override;
5555

5656
QWidget *
5757
embeddedWidget() override;
5858

59+
bool
60+
resizable() const override { return true; }
61+
5962
private Q_SLOTS:
6063

6164
void
62-
onTextEdited(QString const & string);
65+
onTextEdited();
6366

6467
private:
65-
QLineEdit * _lineEdit;
68+
QTextEdit * _lineEdit;
6669
};

examples/plugin_text/TextSourceDataModel.cpp

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)