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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/*!
@file
@author Albert Semenov
@date 07/2012
*/
#include "Precompiled.h"
#include "ListBoxDataControl.h"
#include "DataManager.h"
#include "DataSelectorManager.h"
#include "Localise.h"
#include "DataUtility.h"
namespace tools
{
ListBoxDataControl::ListBoxDataControl() :
mListBox(nullptr),
mHelpPanel(nullptr),
mParentData(nullptr),
mLastIndex(MyGUI::ITEM_NONE),
mContextMenu(nullptr),
mTextFieldControl(nullptr),
mEnableChangePosition(false)
{
}
ListBoxDataControl::~ListBoxDataControl()
{
delete mTextFieldControl;
mTextFieldControl = nullptr;
}
void ListBoxDataControl::OnInitialise(Control* _parent, MyGUI::Widget* _place, const std::string& _layoutName)
{
Control::OnInitialise(_parent, _place, _layoutName);
mListBox = mMainWidget->castType<MyGUI::ListBox>(false);
assignWidget(mContextMenu, "ContextMenu", false);
mContextMenu->setVisible(false);
if (mListBox != nullptr)
{
mListBox->eventListChangePosition += MyGUI::newDelegate(this, &ListBoxDataControl::notifyListChangePosition);
mListBox->eventNotifyItem += MyGUI::newDelegate(this, &ListBoxDataControl::notifyItem);
}
mTextFieldControl = new TextFieldControl();
mTextFieldControl->Initialise();
mTextFieldControl->eventEndDialog.connect(this, &ListBoxDataControl::notifyEndDialog);
assignWidget(mHelpPanel, "HelpPanel", false, false);
mColourName = "ColourError";
}
void ListBoxDataControl::notifyListChangePosition(MyGUI::ListBox* _sender, size_t _index)
{
if (mLastIndex != MyGUI::ITEM_NONE && _index != MyGUI::ITEM_NONE)
{
if (MyGUI::InputManager::getInstance().isControlPressed())
{
if (mEnableChangePosition)
{
DataPtr data1 = *mListBox->getItemDataAt<DataPtr>(mLastIndex);
DataPtr data2 = *mListBox->getItemDataAt<DataPtr>(_index);
eventChangePosition(data1, data2);
}
}
}
mLastIndex = _index;
DataPtr selection = _index != MyGUI::ITEM_NONE ? *mListBox->getItemDataAt<DataPtr>(_index) : nullptr;
DataSelectorManager::getInstance().changeParentSelection(mParentData, selection);
}
void ListBoxDataControl::invalidateList()
{
mLastIndex = MyGUI::ITEM_NONE;
mListBox->setIndexSelected(MyGUI::ITEM_NONE);
if (mParentData != nullptr)
{
for (size_t index = 0; index < mListBox->getItemCount(); index ++)
mListBox->setItemDataAt(index, nullptr);
const Data::VectorData& childs = DataUtility::getChildsByType(mParentData, mThisType);
while (mListBox->getItemCount() > childs.size())
mListBox->removeItemAt(mListBox->getItemCount() - 1);
while (mListBox->getItemCount() < childs.size())
mListBox->addItem("", nullptr);
for (size_t index = 0; index < childs.size(); index ++)
{
DataPtr child = childs.at(index);
bool unique = isDataEnabled(child);
if (unique)
mListBox->setItemNameAt(index, child->getPropertyValue(mPropertyForName));
else
mListBox->setItemNameAt(index, replaceTags(mColourName) + child->getPropertyValue(mPropertyForName));
mListBox->setItemDataAt(index, child);
connectToProperty(child);
}
}
else
{
mListBox->removeAllItems();
}
if (mHelpPanel != nullptr)
mHelpPanel->setVisible(mListBox->getItemCount() == 0);
}
void ListBoxDataControl::invalidateSelection()
{
if (mParentData != nullptr)
{
size_t currentIndex = mListBox->getIndexSelected();
DataPtr selection = currentIndex != MyGUI::ITEM_NONE ? *mListBox->getItemDataAt<DataPtr>(currentIndex) : nullptr;
if (selection != mParentData->getChildSelected())
selectListItemByData(mParentData->getChildSelected());
}
}
void ListBoxDataControl::selectListItemByData(DataPtr _data)
{
for (size_t index = 0; index < mListBox->getItemCount(); index ++)
{
DataPtr selection = *mListBox->getItemDataAt<DataPtr>(index);
if (selection == _data)
{
mListBox->setIndexSelected(index);
return;
}
}
mListBox->setIndexSelected(MyGUI::ITEM_NONE);
}
void ListBoxDataControl::notifyChangeDataSelector(DataPtr _data, bool _changeOnlySelection)
{
mParentData = _data;
if (!_changeOnlySelection)
invalidateList();
invalidateSelection();
}
void ListBoxDataControl::notifyItem(MyGUI::ListBox* _sender, const MyGUI::IBNotifyItemData& _info)
{
if (_info.notify == MyGUI::IBNotifyItemData::MousePressed)
{
if (_info.id == MyGUI::MouseButton::Right)
{
mListBox->setIndexSelected(_info.index);
mLastIndex = _info.index;
if (mParentData != nullptr)
{
DataPtr selection = _info.index != MyGUI::ITEM_NONE ? *mListBox->getItemDataAt<DataPtr>(_info.index) : nullptr;
DataSelectorManager::getInstance().changeParentSelection(mParentData, selection);
}
}
}
else if (_info.notify == MyGUI::IBNotifyItemData::MouseReleased)
{
if (_info.id == MyGUI::MouseButton::Right)
{
if (mContextMenu->getChildCount() != 0)
{
MyGUI::IntPoint point = MyGUI::IntPoint(_info.x, _info.y);
if ((_info.y + mContextMenu->getHeight()) >= MyGUI::RenderManager::getInstance().getViewSize().height)
point.top -= mContextMenu->getHeight();
if ((_info.x + mContextMenu->getWidth()) >= MyGUI::RenderManager::getInstance().getViewSize().width)
point.left -= mContextMenu->getWidth();
mContextMenu->setPosition(point.left, point.top);
mContextMenu->setVisibleSmooth(true);
}
}
}
}
void ListBoxDataControl::OnRenameData()
{
size_t index = mListBox->getIndexSelected();
if (index != MyGUI::ITEM_NONE)
{
mListBox->beginToItemAt(index);
MyGUI::Widget* widget = mListBox->getWidgetByIndex(index);
if (widget != nullptr)
{
DataPtr data = *mListBox->getItemDataAt<DataPtr>(index);
mTextFieldControl->setCaption(replaceTags("CaptionEnterName"));
mTextFieldControl->setTextField(data->getPropertyValue(mPropertyForName));
mTextFieldControl->setUserData(data);
mTextFieldControl->setCoord(MyGUI::IntCoord(widget->getAbsoluteLeft(), widget->getAbsoluteTop(), widget->getWidth(), widget->getHeight()));
mTextFieldControl->doModal();
}
}
}
void ListBoxDataControl::notifyEndDialog(Dialog* _sender, bool _result)
{
_sender->endModal();
if (_result)
{
DataPtr data = *mTextFieldControl->getUserData<DataPtr>();
eventChangeName(data, mTextFieldControl->getTextField());
}
}
void ListBoxDataControl::setEnableChangePosition(bool _value)
{
mEnableChangePosition = _value;
}
void ListBoxDataControl::setDataInfo(const std::string& _parentType, const std::string& _thisType, const std::string& _propertyName)
{
mPropertyForName = _propertyName;
mThisType = _thisType;
DataSelectorManager::getInstance().getEvent(_parentType)->connect(this, &ListBoxDataControl::notifyChangeDataSelector);
mParentData = DataUtility::getSelectedDataByType(_parentType);
notifyChangeDataSelector(mParentData, false);
}
void ListBoxDataControl::connectToProperty(DataPtr _data)
{
PropertyPtr property = _data->getProperty(mPropertyForName);
if (!property->eventChangeProperty.exist(this, &ListBoxDataControl::notifyChangeProperty))
property->eventChangeProperty.connect(this, &ListBoxDataControl::notifyChangeProperty);
for (VectorString::const_iterator name = mPropertyNamesEnable.begin(); name != mPropertyNamesEnable.end(); name ++)
{
property = _data->getProperty(*name);
if (!property->eventChangeProperty.exist(this, &ListBoxDataControl::notifyChangeProperty))
property->eventChangeProperty.connect(this, &ListBoxDataControl::notifyChangeProperty);
}
}
void ListBoxDataControl::notifyChangeProperty(PropertyPtr _sender)
{
if (mParentData == nullptr)
return;
if (mParentData != _sender->getOwner()->getParent())
return;
for (size_t index = 0; index < mListBox->getItemCount(); index ++)
{
DataPtr data = *mListBox->getItemDataAt<DataPtr>(index);
if (data == _sender->getOwner())
{
bool unique = isDataEnabled(data);
if (unique)
mListBox->setItemNameAt(index, data->getPropertyValue(mPropertyForName));
else
mListBox->setItemNameAt(index, replaceTags(mColourName) + data->getPropertyValue(mPropertyForName));
}
}
}
bool ListBoxDataControl::isDataEnabled(DataPtr _data)
{
for (VectorString::const_iterator name = mPropertyNamesEnable.begin(); name != mPropertyNamesEnable.end(); name ++)
{
if (!_data->getPropertyValue<bool>(*name))
return false;
}
return true;
}
void ListBoxDataControl::setReplaceColourName(const std::string& _value)
{
mColourName = _value;
}
void ListBoxDataControl::addPropertyNameEnabled(const std::string& _propertyName)
{
mPropertyNamesEnable.push_back(_propertyName);
}
}
|