qwt_legend.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. #include "qwt_legend.h"
  10. #include "qwt_legend_itemmanager.h"
  11. #include "qwt_legend_item.h"
  12. #include "qwt_dyngrid_layout.h"
  13. #include "qwt_math.h"
  14. #include <qapplication.h>
  15. #include <qmap.h>
  16. #include <qscrollbar.h>
  17. #include <qscrollarea.h>
  18. class QwtLegend::PrivateData
  19. {
  20. public:
  21. class LegendMap
  22. {
  23. public:
  24. void insert( const QwtLegendItemManager *, QWidget * );
  25. void remove( const QwtLegendItemManager * );
  26. void remove( QWidget * );
  27. void clear();
  28. uint count() const;
  29. inline const QWidget *find( const QwtLegendItemManager * ) const;
  30. inline QWidget *find( const QwtLegendItemManager * );
  31. inline const QwtLegendItemManager *find( const QWidget * ) const;
  32. inline QwtLegendItemManager *find( const QWidget * );
  33. const QMap<QWidget *, const QwtLegendItemManager *> &widgetMap() const;
  34. QMap<QWidget *, const QwtLegendItemManager *> &widgetMap();
  35. private:
  36. QMap<QWidget *, const QwtLegendItemManager *> d_widgetMap;
  37. QMap<const QwtLegendItemManager *, QWidget *> d_itemMap;
  38. };
  39. QwtLegend::LegendItemMode itemMode;
  40. LegendMap map;
  41. class LegendView;
  42. LegendView *view;
  43. };
  44. class QwtLegend::PrivateData::LegendView: public QScrollArea
  45. {
  46. public:
  47. LegendView( QWidget *parent ):
  48. QScrollArea( parent )
  49. {
  50. contentsWidget = new QWidget( this );
  51. setWidget( contentsWidget );
  52. setWidgetResizable( false );
  53. setFocusPolicy( Qt::NoFocus );
  54. }
  55. virtual bool viewportEvent( QEvent *e )
  56. {
  57. bool ok = QScrollArea::viewportEvent( e );
  58. if ( e->type() == QEvent::Resize )
  59. {
  60. QEvent event( QEvent::LayoutRequest );
  61. QApplication::sendEvent( contentsWidget, &event );
  62. }
  63. return ok;
  64. }
  65. QSize viewportSize( int w, int h ) const
  66. {
  67. const int sbHeight = horizontalScrollBar()->sizeHint().height();
  68. const int sbWidth = verticalScrollBar()->sizeHint().width();
  69. const int cw = contentsRect().width();
  70. const int ch = contentsRect().height();
  71. int vw = cw;
  72. int vh = ch;
  73. if ( w > vw )
  74. vh -= sbHeight;
  75. if ( h > vh )
  76. {
  77. vw -= sbWidth;
  78. if ( w > vw && vh == ch )
  79. vh -= sbHeight;
  80. }
  81. return QSize( vw, vh );
  82. }
  83. QWidget *contentsWidget;
  84. };
  85. void QwtLegend::PrivateData::LegendMap::insert(
  86. const QwtLegendItemManager *item, QWidget *widget )
  87. {
  88. d_itemMap.insert( item, widget );
  89. d_widgetMap.insert( widget, item );
  90. }
  91. void QwtLegend::PrivateData::LegendMap::remove( const QwtLegendItemManager *item )
  92. {
  93. QWidget *widget = d_itemMap[item];
  94. d_itemMap.remove( item );
  95. d_widgetMap.remove( widget );
  96. }
  97. void QwtLegend::PrivateData::LegendMap::remove( QWidget *widget )
  98. {
  99. const QwtLegendItemManager *item = d_widgetMap[widget];
  100. d_itemMap.remove( item );
  101. d_widgetMap.remove( widget );
  102. }
  103. void QwtLegend::PrivateData::LegendMap::clear()
  104. {
  105. /*
  106. We can't delete the widgets in the following loop, because
  107. we would get ChildRemoved events, changing d_itemMap, while
  108. we are iterating.
  109. */
  110. QList<const QWidget *> widgets;
  111. QMap<const QwtLegendItemManager *, QWidget *>::const_iterator it;
  112. for ( it = d_itemMap.begin(); it != d_itemMap.end(); ++it )
  113. widgets.append( it.value() );
  114. d_itemMap.clear();
  115. d_widgetMap.clear();
  116. for ( int i = 0; i < ( int )widgets.size(); i++ )
  117. delete widgets[i];
  118. }
  119. uint QwtLegend::PrivateData::LegendMap::count() const
  120. {
  121. return d_itemMap.count();
  122. }
  123. inline const QWidget *QwtLegend::PrivateData::LegendMap::find(
  124. const QwtLegendItemManager *item ) const
  125. {
  126. if ( !d_itemMap.contains( item ) )
  127. return NULL;
  128. return d_itemMap[item];
  129. }
  130. inline QWidget *QwtLegend::PrivateData::LegendMap::find(
  131. const QwtLegendItemManager *item )
  132. {
  133. if ( !d_itemMap.contains( item ) )
  134. return NULL;
  135. return d_itemMap[item];
  136. }
  137. inline const QwtLegendItemManager *QwtLegend::PrivateData::LegendMap::find(
  138. const QWidget *widget ) const
  139. {
  140. QWidget *w = const_cast<QWidget *>( widget );
  141. if ( !d_widgetMap.contains( w ) )
  142. return NULL;
  143. return d_widgetMap[w];
  144. }
  145. inline QwtLegendItemManager *QwtLegend::PrivateData::LegendMap::find(
  146. const QWidget *widget )
  147. {
  148. QWidget *w = const_cast<QWidget *>( widget );
  149. if ( !d_widgetMap.contains( w ) )
  150. return NULL;
  151. return const_cast<QwtLegendItemManager *>( d_widgetMap[w] );
  152. }
  153. inline const QMap<QWidget *, const QwtLegendItemManager *> &
  154. QwtLegend::PrivateData::LegendMap::widgetMap() const
  155. {
  156. return d_widgetMap;
  157. }
  158. inline QMap<QWidget *, const QwtLegendItemManager *> &
  159. QwtLegend::PrivateData::LegendMap::widgetMap()
  160. {
  161. return d_widgetMap;
  162. }
  163. /*!
  164. Constructor
  165. \param parent Parent widget
  166. */
  167. QwtLegend::QwtLegend( QWidget *parent ):
  168. QFrame( parent )
  169. {
  170. setFrameStyle( NoFrame );
  171. d_data = new QwtLegend::PrivateData;
  172. d_data->itemMode = QwtLegend::ReadOnlyItem;
  173. d_data->view = new QwtLegend::PrivateData::LegendView( this );
  174. d_data->view->setFrameStyle( NoFrame );
  175. QwtDynGridLayout *layout = new QwtDynGridLayout(
  176. d_data->view->contentsWidget );
  177. layout->setAlignment( Qt::AlignHCenter | Qt::AlignTop );
  178. d_data->view->contentsWidget->installEventFilter( this );
  179. }
  180. //! Destructor
  181. QwtLegend::~QwtLegend()
  182. {
  183. delete d_data;
  184. }
  185. //! \sa LegendItemMode
  186. void QwtLegend::setItemMode( LegendItemMode mode )
  187. {
  188. d_data->itemMode = mode;
  189. }
  190. //! \sa LegendItemMode
  191. QwtLegend::LegendItemMode QwtLegend::itemMode() const
  192. {
  193. return d_data->itemMode;
  194. }
  195. /*!
  196. The contents widget is the only child of the viewport() and
  197. the parent widget of all legend items.
  198. */
  199. QWidget *QwtLegend::contentsWidget()
  200. {
  201. return d_data->view->contentsWidget;
  202. }
  203. /*!
  204. \return Horizontal scrollbar
  205. \sa verticalScrollBar()
  206. */
  207. QScrollBar *QwtLegend::horizontalScrollBar() const
  208. {
  209. return d_data->view->horizontalScrollBar();
  210. }
  211. /*!
  212. \return Vertical scrollbar
  213. \sa horizontalScrollBar()
  214. */
  215. QScrollBar *QwtLegend::verticalScrollBar() const
  216. {
  217. return d_data->view->verticalScrollBar();
  218. }
  219. /*!
  220. The contents widget is the only child of the viewport() and
  221. the parent widget of all legend items.
  222. */
  223. const QWidget *QwtLegend::contentsWidget() const
  224. {
  225. return d_data->view->contentsWidget;
  226. }
  227. /*!
  228. Insert a new item for a plot item
  229. \param plotItem Plot item
  230. \param legendItem New legend item
  231. \note The parent of item will be changed to QwtLegend::contentsWidget()
  232. */
  233. void QwtLegend::insert( const QwtLegendItemManager *plotItem, QWidget *legendItem )
  234. {
  235. if ( legendItem == NULL || plotItem == NULL )
  236. return;
  237. QWidget *contentsWidget = d_data->view->contentsWidget;
  238. if ( legendItem->parent() != contentsWidget )
  239. legendItem->setParent( contentsWidget );
  240. legendItem->show();
  241. d_data->map.insert( plotItem, legendItem );
  242. layoutContents();
  243. if ( contentsWidget->layout() )
  244. {
  245. contentsWidget->layout()->addWidget( legendItem );
  246. // set tab focus chain
  247. QWidget *w = NULL;
  248. for ( int i = 0; i < contentsWidget->layout()->count(); i++ )
  249. {
  250. QLayoutItem *item = contentsWidget->layout()->itemAt( i );
  251. if ( w && item->widget() )
  252. QWidget::setTabOrder( w, item->widget() );
  253. w = item->widget();
  254. }
  255. }
  256. if ( parentWidget() && parentWidget()->layout() == NULL )
  257. {
  258. /*
  259. updateGeometry() doesn't post LayoutRequest in certain
  260. situations, like when we are hidden. But we want the
  261. parent widget notified, so it can show/hide the legend
  262. depending on its items.
  263. */
  264. QApplication::postEvent( parentWidget(),
  265. new QEvent( QEvent::LayoutRequest ) );
  266. }
  267. }
  268. /*!
  269. Find the widget that represents a plot item
  270. \param plotItem Plot item
  271. \return Widget on the legend, or NULL
  272. */
  273. QWidget *QwtLegend::find( const QwtLegendItemManager *plotItem ) const
  274. {
  275. return d_data->map.find( plotItem );
  276. }
  277. /*!
  278. Find the widget that represents a plot item
  279. \param legendItem Legend item
  280. \return Widget on the legend, or NULL
  281. */
  282. QwtLegendItemManager *QwtLegend::find( const QWidget *legendItem ) const
  283. {
  284. return d_data->map.find( legendItem );
  285. }
  286. /*!
  287. Find the corresponding item for a plotItem and remove it
  288. from the item list.
  289. \param plotItem Plot item
  290. */
  291. void QwtLegend::remove( const QwtLegendItemManager *plotItem )
  292. {
  293. QWidget *legendItem = d_data->map.find( plotItem );
  294. d_data->map.remove( legendItem );
  295. delete legendItem;
  296. }
  297. //! Remove all items.
  298. void QwtLegend::clear()
  299. {
  300. bool doUpdate = updatesEnabled();
  301. if ( doUpdate )
  302. setUpdatesEnabled( false );
  303. d_data->map.clear();
  304. if ( doUpdate )
  305. setUpdatesEnabled( true );
  306. update();
  307. }
  308. //! Return a size hint.
  309. QSize QwtLegend::sizeHint() const
  310. {
  311. QSize hint = d_data->view->contentsWidget->sizeHint();
  312. hint += QSize( 2 * frameWidth(), 2 * frameWidth() );
  313. return hint;
  314. }
  315. /*!
  316. \return The preferred height, for the width w.
  317. \param width Width
  318. */
  319. int QwtLegend::heightForWidth( int width ) const
  320. {
  321. width -= 2 * frameWidth();
  322. int h = d_data->view->contentsWidget->heightForWidth( width );
  323. if ( h >= 0 )
  324. h += 2 * frameWidth();
  325. return h;
  326. }
  327. /*!
  328. Adjust contents widget and item layout to the size of the viewport().
  329. */
  330. void QwtLegend::layoutContents()
  331. {
  332. const QSize visibleSize = d_data->view->viewport()->size();
  333. const QLayout *l = d_data->view->contentsWidget->layout();
  334. if ( l && l->inherits( "QwtDynGridLayout" ) )
  335. {
  336. const QwtDynGridLayout *tl = ( const QwtDynGridLayout * )l;
  337. const int minW = int( tl->maxItemWidth() ) + 2 * tl->margin();
  338. int w = qMax( visibleSize.width(), minW );
  339. int h = qMax( tl->heightForWidth( w ), visibleSize.height() );
  340. const int vpWidth = d_data->view->viewportSize( w, h ).width();
  341. if ( w > vpWidth )
  342. {
  343. w = qMax( vpWidth, minW );
  344. h = qMax( tl->heightForWidth( w ), visibleSize.height() );
  345. }
  346. d_data->view->contentsWidget->resize( w, h );
  347. }
  348. }
  349. /*!
  350. Filter layout related events of QwtLegend::contentsWidget().
  351. \param o Object to be filtered
  352. \param e Event
  353. */
  354. bool QwtLegend::eventFilter( QObject *o, QEvent *e )
  355. {
  356. if ( o == d_data->view->contentsWidget )
  357. {
  358. switch ( e->type() )
  359. {
  360. case QEvent::ChildRemoved:
  361. {
  362. const QChildEvent *ce = ( const QChildEvent * )e;
  363. if ( ce->child()->isWidgetType() )
  364. d_data->map.remove( ( QWidget * )ce->child() );
  365. break;
  366. }
  367. case QEvent::LayoutRequest:
  368. {
  369. layoutContents();
  370. break;
  371. }
  372. default:
  373. break;
  374. }
  375. }
  376. return QFrame::eventFilter( o, e );
  377. }
  378. //! Return true, if there are no legend items.
  379. bool QwtLegend::isEmpty() const
  380. {
  381. return d_data->map.count() == 0;
  382. }
  383. //! Return the number of legend items.
  384. uint QwtLegend::itemCount() const
  385. {
  386. return d_data->map.count();
  387. }
  388. //! Return a list of all legend items
  389. QList<QWidget *> QwtLegend::legendItems() const
  390. {
  391. const QMap<QWidget *, const QwtLegendItemManager *> &map =
  392. d_data->map.widgetMap();
  393. QList<QWidget *> list;
  394. QMap<QWidget *, const QwtLegendItemManager *>::const_iterator it;
  395. for ( it = map.begin(); it != map.end(); ++it )
  396. list += it.key();
  397. return list;
  398. }
  399. /*!
  400. Resize event
  401. \param e Resize event
  402. */
  403. void QwtLegend::resizeEvent( QResizeEvent *e )
  404. {
  405. QFrame::resizeEvent( e );
  406. d_data->view->setGeometry( contentsRect() );
  407. }