PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llinventorylistitem.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 463 lines | 363 code | 61 blank | 39 comment | 40 complexity | 1af445cad04a6b200ef4e4b1bc8d37d9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llinventorylistitem.cpp
  3. * @brief Inventory list item panel.
  4. *
  5. * Class LLPanelInventoryListItemBase displays inventory item as an element
  6. * of LLInventoryItemsList.
  7. *
  8. * $LicenseInfo:firstyear=2010&license=viewerlgpl$
  9. * Second Life Viewer Source Code
  10. * Copyright (C) 2010, Linden Research, Inc.
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation;
  15. * version 2.1 of the License only.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. *
  26. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  27. * $/LicenseInfo$
  28. */
  29. #include "llviewerprecompiledheaders.h"
  30. #include "llinventorylistitem.h"
  31. // llui
  32. #include "lliconctrl.h"
  33. #include "lltextbox.h"
  34. #include "lltextutil.h"
  35. // newview
  36. #include "llinventorymodel.h"
  37. #include "llviewerinventory.h"
  38. static LLWidgetNameRegistry::StaticRegistrar sRegisterPanelInventoryListItemBaseParams(&typeid(LLPanelInventoryListItemBase::Params), "inventory_list_item");
  39. static const S32 WIDGET_SPACING = 3;
  40. LLPanelInventoryListItemBase::Params::Params()
  41. : default_style("default_style"),
  42. worn_style("worn_style"),
  43. hover_image("hover_image"),
  44. selected_image("selected_image"),
  45. separator_image("separator_image"),
  46. item_icon("item_icon"),
  47. item_name("item_name")
  48. {};
  49. LLPanelInventoryListItemBase* LLPanelInventoryListItemBase::create(LLViewerInventoryItem* item)
  50. {
  51. LLPanelInventoryListItemBase* list_item = NULL;
  52. if (item)
  53. {
  54. const LLPanelInventoryListItemBase::Params& params = LLUICtrlFactory::getDefaultParams<LLPanelInventoryListItemBase>();
  55. list_item = new LLPanelInventoryListItemBase(item, params);
  56. list_item->initFromParams(params);
  57. list_item->postBuild();
  58. }
  59. return list_item;
  60. }
  61. void LLPanelInventoryListItemBase::draw()
  62. {
  63. if (getNeedsRefresh())
  64. {
  65. LLViewerInventoryItem* inv_item = getItem();
  66. if (inv_item)
  67. {
  68. updateItem(inv_item->getName());
  69. }
  70. setNeedsRefresh(false);
  71. }
  72. if (mHovered && mHoverImage)
  73. {
  74. mHoverImage->draw(getLocalRect());
  75. }
  76. if (mSelected && mSelectedImage)
  77. {
  78. mSelectedImage->draw(getLocalRect());
  79. }
  80. if (mSeparatorVisible && mSeparatorImage)
  81. {
  82. // place under bottom of listitem, using image height
  83. // item_pad in list using the item should be >= image height
  84. // to avoid cropping of top of the next item.
  85. LLRect separator_rect = getLocalRect();
  86. separator_rect.mTop = separator_rect.mBottom;
  87. separator_rect.mBottom -= mSeparatorImage->getHeight();
  88. F32 alpha = getCurrentTransparency();
  89. mSeparatorImage->draw(separator_rect, UI_VERTEX_COLOR % alpha);
  90. }
  91. LLPanel::draw();
  92. }
  93. // virtual
  94. void LLPanelInventoryListItemBase::updateItem(const std::string& name,
  95. EItemState item_state)
  96. {
  97. setIconImage(mIconImage);
  98. setTitle(name, mHighlightedText, item_state);
  99. }
  100. void LLPanelInventoryListItemBase::addWidgetToLeftSide(const std::string& name, bool show_widget/* = true*/)
  101. {
  102. LLUICtrl* ctrl = findChild<LLUICtrl>(name);
  103. if(ctrl)
  104. {
  105. addWidgetToLeftSide(ctrl, show_widget);
  106. }
  107. }
  108. void LLPanelInventoryListItemBase::addWidgetToLeftSide(LLUICtrl* ctrl, bool show_widget/* = true*/)
  109. {
  110. mLeftSideWidgets.push_back(ctrl);
  111. setShowWidget(ctrl, show_widget);
  112. }
  113. void LLPanelInventoryListItemBase::addWidgetToRightSide(const std::string& name, bool show_widget/* = true*/)
  114. {
  115. LLUICtrl* ctrl = findChild<LLUICtrl>(name);
  116. if(ctrl)
  117. {
  118. addWidgetToRightSide(ctrl, show_widget);
  119. }
  120. }
  121. void LLPanelInventoryListItemBase::addWidgetToRightSide(LLUICtrl* ctrl, bool show_widget/* = true*/)
  122. {
  123. mRightSideWidgets.push_back(ctrl);
  124. setShowWidget(ctrl, show_widget);
  125. }
  126. void LLPanelInventoryListItemBase::setShowWidget(const std::string& name, bool show)
  127. {
  128. LLUICtrl* widget = findChild<LLUICtrl>(name);
  129. if(widget)
  130. {
  131. setShowWidget(widget, show);
  132. }
  133. }
  134. void LLPanelInventoryListItemBase::setShowWidget(LLUICtrl* ctrl, bool show)
  135. {
  136. // Enable state determines whether widget may become visible in setWidgetsVisible()
  137. ctrl->setEnabled(show);
  138. }
  139. BOOL LLPanelInventoryListItemBase::postBuild()
  140. {
  141. LLViewerInventoryItem* inv_item = getItem();
  142. if (inv_item)
  143. {
  144. mIconImage = LLInventoryIcon::getIcon(inv_item->getType(), inv_item->getInventoryType(), inv_item->getFlags(), FALSE);
  145. updateItem(inv_item->getName());
  146. }
  147. setNeedsRefresh(true);
  148. setWidgetsVisible(false);
  149. reshapeWidgets();
  150. return TRUE;
  151. }
  152. void LLPanelInventoryListItemBase::setValue(const LLSD& value)
  153. {
  154. if (!value.isMap()) return;
  155. if (!value.has("selected")) return;
  156. mSelected = value["selected"];
  157. }
  158. void LLPanelInventoryListItemBase::onMouseEnter(S32 x, S32 y, MASK mask)
  159. {
  160. mHovered = true;
  161. LLPanel::onMouseEnter(x, y, mask);
  162. }
  163. void LLPanelInventoryListItemBase::onMouseLeave(S32 x, S32 y, MASK mask)
  164. {
  165. mHovered = false;
  166. LLPanel::onMouseLeave(x, y, mask);
  167. }
  168. const std::string& LLPanelInventoryListItemBase::getItemName() const
  169. {
  170. LLViewerInventoryItem* inv_item = getItem();
  171. if (NULL == inv_item)
  172. {
  173. return LLStringUtil::null;
  174. }
  175. return inv_item->getName();
  176. }
  177. LLAssetType::EType LLPanelInventoryListItemBase::getType() const
  178. {
  179. LLViewerInventoryItem* inv_item = getItem();
  180. if (NULL == inv_item)
  181. {
  182. return LLAssetType::AT_NONE;
  183. }
  184. return inv_item->getType();
  185. }
  186. LLWearableType::EType LLPanelInventoryListItemBase::getWearableType() const
  187. {
  188. LLViewerInventoryItem* inv_item = getItem();
  189. if (NULL == inv_item)
  190. {
  191. return LLWearableType::WT_NONE;
  192. }
  193. return inv_item->getWearableType();
  194. }
  195. const std::string& LLPanelInventoryListItemBase::getDescription() const
  196. {
  197. LLViewerInventoryItem* inv_item = getItem();
  198. if (NULL == inv_item)
  199. {
  200. return LLStringUtil::null;
  201. }
  202. return inv_item->getDescription();
  203. }
  204. time_t LLPanelInventoryListItemBase::getCreationDate() const
  205. {
  206. LLViewerInventoryItem* inv_item = getItem();
  207. if (NULL == inv_item)
  208. {
  209. return 0;
  210. }
  211. return inv_item->getCreationDate();
  212. }
  213. LLViewerInventoryItem* LLPanelInventoryListItemBase::getItem() const
  214. {
  215. return gInventory.getItem(mInventoryItemUUID);
  216. }
  217. S32 LLPanelInventoryListItemBase::notify(const LLSD& info)
  218. {
  219. S32 rv = 0;
  220. if(info.has("match_filter"))
  221. {
  222. mHighlightedText = info["match_filter"].asString();
  223. std::string test(mTitleCtrl->getText());
  224. LLStringUtil::toUpper(test);
  225. if(mHighlightedText.empty() || std::string::npos != test.find(mHighlightedText))
  226. {
  227. rv = 0; // substring is found
  228. }
  229. else
  230. {
  231. rv = -1;
  232. }
  233. setNeedsRefresh(true);
  234. }
  235. else
  236. {
  237. rv = LLPanel::notify(info);
  238. }
  239. return rv;
  240. }
  241. LLPanelInventoryListItemBase::LLPanelInventoryListItemBase(LLViewerInventoryItem* item, const LLPanelInventoryListItemBase::Params& params)
  242. : LLPanel(params),
  243. mInventoryItemUUID(item ? item->getUUID() : LLUUID::null),
  244. mIconCtrl(NULL),
  245. mTitleCtrl(NULL),
  246. mWidgetSpacing(WIDGET_SPACING),
  247. mLeftWidgetsWidth(0),
  248. mRightWidgetsWidth(0),
  249. mNeedsRefresh(false),
  250. mHovered(false),
  251. mSelected(false),
  252. mSeparatorVisible(false),
  253. mHoverImage(params.hover_image),
  254. mSelectedImage(params.selected_image),
  255. mSeparatorImage(params.separator_image)
  256. {
  257. LLIconCtrl::Params icon_params(params.item_icon);
  258. applyXUILayout(icon_params, this);
  259. mIconCtrl = LLUICtrlFactory::create<LLIconCtrl>(icon_params);
  260. if (mIconCtrl)
  261. {
  262. addChild(mIconCtrl);
  263. }
  264. else
  265. {
  266. LLIconCtrl::Params icon_params;
  267. icon_params.name = "item_icon";
  268. mIconCtrl = LLUICtrlFactory::create<LLIconCtrl>(icon_params);
  269. }
  270. LLTextBox::Params text_params(params.item_name);
  271. applyXUILayout(text_params, this);
  272. mTitleCtrl = LLUICtrlFactory::create<LLTextBox>(text_params);
  273. if (mTitleCtrl)
  274. {
  275. addChild(mTitleCtrl);
  276. }
  277. else
  278. {
  279. LLTextBox::Params text_aprams;
  280. text_params.name = "item_title";
  281. mTitleCtrl = LLUICtrlFactory::create<LLTextBox>(text_params);
  282. }
  283. }
  284. class WidgetVisibilityChanger
  285. {
  286. public:
  287. WidgetVisibilityChanger(bool visible) : mVisible(visible){}
  288. void operator()(LLUICtrl* widget)
  289. {
  290. // Disabled widgets never become visible. see LLPanelInventoryListItemBase::setShowWidget()
  291. widget->setVisible(mVisible && widget->getEnabled());
  292. }
  293. private:
  294. bool mVisible;
  295. };
  296. void LLPanelInventoryListItemBase::setWidgetsVisible(bool visible)
  297. {
  298. std::for_each(mLeftSideWidgets.begin(), mLeftSideWidgets.end(), WidgetVisibilityChanger(visible));
  299. std::for_each(mRightSideWidgets.begin(), mRightSideWidgets.end(), WidgetVisibilityChanger(visible));
  300. }
  301. void LLPanelInventoryListItemBase::reshapeWidgets()
  302. {
  303. // disabled reshape left for now to reserve space for 'delete' button in LLPanelClothingListItem
  304. /*reshapeLeftWidgets();*/
  305. reshapeRightWidgets();
  306. reshapeMiddleWidgets();
  307. }
  308. void LLPanelInventoryListItemBase::setIconImage(const LLUIImagePtr& image)
  309. {
  310. if(image)
  311. {
  312. mIconImage = image;
  313. mIconCtrl->setImage(mIconImage);
  314. }
  315. }
  316. void LLPanelInventoryListItemBase::setTitle(const std::string& title,
  317. const std::string& highlit_text,
  318. EItemState item_state)
  319. {
  320. mTitleCtrl->setToolTip(title);
  321. LLStyle::Params style_params;
  322. const LLPanelInventoryListItemBase::Params& params = LLUICtrlFactory::getDefaultParams<LLPanelInventoryListItemBase>();
  323. switch(item_state)
  324. {
  325. case IS_DEFAULT:
  326. style_params = params.default_style();
  327. break;
  328. case IS_WORN:
  329. style_params = params.worn_style();
  330. break;
  331. default:;
  332. }
  333. LLTextUtil::textboxSetHighlightedVal(
  334. mTitleCtrl,
  335. style_params,
  336. title,
  337. highlit_text);
  338. }
  339. BOOL LLPanelInventoryListItemBase::handleToolTip( S32 x, S32 y, MASK mask)
  340. {
  341. LLRect text_box_rect = mTitleCtrl->getRect();
  342. if (text_box_rect.pointInRect(x, y) &&
  343. mTitleCtrl->getTextPixelWidth() <= text_box_rect.getWidth())
  344. {
  345. return FALSE;
  346. }
  347. return LLPanel::handleToolTip(x, y, mask);
  348. }
  349. void LLPanelInventoryListItemBase::reshapeLeftWidgets()
  350. {
  351. S32 widget_left = 0;
  352. mLeftWidgetsWidth = 0;
  353. widget_array_t::const_iterator it = mLeftSideWidgets.begin();
  354. const widget_array_t::const_iterator it_end = mLeftSideWidgets.end();
  355. for( ; it_end != it; ++it)
  356. {
  357. LLUICtrl* widget = *it;
  358. if(!widget->getVisible())
  359. {
  360. continue;
  361. }
  362. LLRect widget_rect(widget->getRect());
  363. widget_rect.setLeftTopAndSize(widget_left, widget_rect.mTop, widget_rect.getWidth(), widget_rect.getHeight());
  364. widget->setShape(widget_rect);
  365. widget_left += widget_rect.getWidth() + getWidgetSpacing();
  366. mLeftWidgetsWidth = widget_rect.mRight;
  367. }
  368. }
  369. void LLPanelInventoryListItemBase::reshapeRightWidgets()
  370. {
  371. S32 widget_right = getLocalRect().getWidth();
  372. S32 widget_left = widget_right;
  373. widget_array_t::const_reverse_iterator it = mRightSideWidgets.rbegin();
  374. const widget_array_t::const_reverse_iterator it_end = mRightSideWidgets.rend();
  375. for( ; it_end != it; ++it)
  376. {
  377. LLUICtrl* widget = *it;
  378. if(!widget->getVisible())
  379. {
  380. continue;
  381. }
  382. LLRect widget_rect(widget->getRect());
  383. widget_left = widget_right - widget_rect.getWidth();
  384. widget_rect.setLeftTopAndSize(widget_left, widget_rect.mTop, widget_rect.getWidth(), widget_rect.getHeight());
  385. widget->setShape(widget_rect);
  386. widget_right = widget_left - getWidgetSpacing();
  387. }
  388. mRightWidgetsWidth = getLocalRect().getWidth() - widget_left;
  389. }
  390. void LLPanelInventoryListItemBase::reshapeMiddleWidgets()
  391. {
  392. LLRect icon_rect(mIconCtrl->getRect());
  393. icon_rect.setLeftTopAndSize(mLeftWidgetsWidth + getWidgetSpacing(), icon_rect.mTop,
  394. icon_rect.getWidth(), icon_rect.getHeight());
  395. mIconCtrl->setShape(icon_rect);
  396. S32 name_left = icon_rect.mRight + getWidgetSpacing();
  397. S32 name_right = getLocalRect().getWidth() - mRightWidgetsWidth - getWidgetSpacing();
  398. LLRect name_rect(mTitleCtrl->getRect());
  399. name_rect.set(name_left, name_rect.mTop, name_right, name_rect.mBottom);
  400. mTitleCtrl->setShape(name_rect);
  401. }
  402. // EOF