-
Notifications
You must be signed in to change notification settings - Fork 38
/
Plugin.cc
152 lines (131 loc) · 4.01 KB
/
Plugin.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (C) 2017 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <ignition/common/Console.hh>
#include "ignition/gui/Plugin.hh"
class ignition::gui::PluginPrivate
{
/// \brief Set this to true if the plugin should be deleted as soon as it has
/// a parent.
public: bool deleteLater{false};
/// \brief Holds the value of the `delete_later` attribute on the
/// configuration. Subclasses can check this value for example to return
/// before the end of LoadConfig.
public: bool deleteLaterRequested{false};
};
using namespace ignition;
using namespace gui;
/////////////////////////////////////////////////
Plugin::Plugin() : dataPtr(new PluginPrivate)
{
}
/////////////////////////////////////////////////
Plugin::~Plugin()
{
}
/////////////////////////////////////////////////
void Plugin::Load(const tinyxml2::XMLElement *_pluginElem)
{
if (!_pluginElem)
{
ignerr << "Failed to load plugin with a NULL element." << std::endl;
return;
}
// TODO: Too complicated to deep clone elements with tinyxml2, storing
// string for now and consider moving away from tinyxml
tinyxml2::XMLPrinter printer;
if (!_pluginElem->Accept(&printer))
{
ignwarn << "There was an error parsing the plugin element for " <<
"[" << this->title << "]." << std::endl;
}
else
{
this->configStr = std::string(printer.CStr());
}
// Delete later
if (_pluginElem->Attribute("delete_later"))
{
// Store param
_pluginElem->QueryBoolAttribute("delete_later",
&this->dataPtr->deleteLaterRequested);
// Use it
if (this->dataPtr->deleteLaterRequested)
this->DeleteLater();
}
// Read default params
if (auto titleElem = _pluginElem->FirstChildElement("title"))
this->title = titleElem->GetText();
// Weird things happen if the bool is not initialized again here
this->hasTitlebar = true;
if (auto hasTitleElem = _pluginElem->FirstChildElement("has_titlebar"))
{
bool has = true;
hasTitleElem->QueryBoolText(&has);
this->hasTitlebar = has;
}
// Setup default context menu
this->setContextMenuPolicy(Qt::CustomContextMenu);
this->connect(this,
SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(ShowContextMenu(const QPoint &)));
// Load custom configuration
this->LoadConfig(_pluginElem);
}
/////////////////////////////////////////////////
std::string Plugin::ConfigStr() const
{
return this->configStr;
}
/////////////////////////////////////////////////
void Plugin::ShowContextMenu(const QPoint &_pos)
{
// Close action
QAction closeAct(QString::fromStdString("Close [" + this->title + "]"),
this);
this->connect(&closeAct, SIGNAL(triggered()), this->parent(), SLOT(close()));
// Context menu
QMenu contextMenu(tr("Context menu"), this);
contextMenu.addAction(&closeAct);
contextMenu.exec(this->mapToGlobal(_pos));
}
/////////////////////////////////////////////////
void Plugin::changeEvent(QEvent *_e)
{
if (_e->type() == QEvent::ParentChange && this->parent() &&
this->dataPtr->deleteLater)
{
qobject_cast<QWidget *>(this->parent())->close();
}
}
/////////////////////////////////////////////////
void Plugin::DeleteLater()
{
this->dataPtr->deleteLaterRequested = true;
if (this->parent())
{
qobject_cast<QWidget *>(this->parent())->close();
}
else
{
this->dataPtr->deleteLater = true;
}
}
/////////////////////////////////////////////////
bool Plugin::DeleteLaterRequested() const
{
return this->dataPtr->deleteLaterRequested;
}