/src/qt3support/itemviews/q3listbox.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 4687 lines · 2611 code · 575 blank · 1501 comment · 854 complexity · f544e93302cca8e745ac1e355f7324e9 MD5 · raw file

Large files are truncated click here to view the full file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the Qt3Support module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qglobal.h"
  42. #if defined(Q_CC_BOR)
  43. // needed for qsort() because of a std namespace problem on Borland
  44. #include "qplatformdefs.h"
  45. #endif
  46. #include "q3listbox.h"
  47. #ifndef QT_NO_LISTBOX
  48. #include "qapplication.h"
  49. #include "qevent.h"
  50. #include "qfontmetrics.h"
  51. #include "qpainter.h"
  52. #include "qpixmap.h"
  53. #include "qstringlist.h"
  54. #include "qstyle.h"
  55. #include "qstyleoption.h"
  56. #include "qtimer.h"
  57. #include "qvector.h"
  58. #include "qpointer.h"
  59. #ifndef QT_NO_ACCESSIBILITY
  60. #include "qaccessible.h"
  61. #endif
  62. #include <stdlib.h>
  63. QT_BEGIN_NAMESPACE
  64. class Q3ListBoxPrivate
  65. {
  66. public:
  67. Q3ListBoxPrivate(Q3ListBox *lb):
  68. head(0), last(0), cache(0), cacheIndex(-1), current(0),
  69. highlighted(0), tmpCurrent(0), columnPos(1), rowPos(1), rowPosCache(0),
  70. columnPosOne(0), rowMode(Q3ListBox::FixedNumber),
  71. columnMode(Q3ListBox::FixedNumber), numRows(1), numColumns(1),
  72. currentRow(0), currentColumn(0),
  73. mousePressRow(-1), mousePressColumn(-1),
  74. mouseMoveRow(-1), mouseMoveColumn(-1), mouseInternalPress(false),
  75. scrollTimer(0), updateTimer(0), visibleTimer(0),
  76. selectionMode(Q3ListBox::Single),
  77. count(0),
  78. listBox(lb), currInputString(QString()),
  79. rowModeWins(false),
  80. ignoreMoves(false),
  81. layoutDirty(true),
  82. mustPaintAll(true),
  83. dragging(false),
  84. dirtyDrag (false),
  85. variableHeight(true /* !!! ### false */),
  86. variableWidth(false),
  87. inMenuMode(false)
  88. {}
  89. int findItemByName(int item, const QString &text);
  90. ~Q3ListBoxPrivate();
  91. Q3ListBoxItem * head, *last, *cache;
  92. int cacheIndex;
  93. Q3ListBoxItem * current, *highlighted, *tmpCurrent;
  94. QVector<int> columnPos;
  95. QVector<int> rowPos;
  96. int rowPosCache;
  97. int columnPosOne;
  98. Q3ListBox::LayoutMode rowMode;
  99. Q3ListBox::LayoutMode columnMode;
  100. int numRows;
  101. int numColumns;
  102. int currentRow;
  103. int currentColumn;
  104. int mousePressRow;
  105. int mousePressColumn;
  106. int mouseMoveRow;
  107. int mouseMoveColumn;
  108. bool mouseInternalPress;
  109. QTimer * scrollTimer;
  110. QTimer * updateTimer;
  111. QTimer * visibleTimer;
  112. QTimer * resizeTimer;
  113. QPoint scrollPos;
  114. Q3ListBox::SelectionMode selectionMode;
  115. int count;
  116. Q3ListBox *listBox;
  117. QString currInputString;
  118. QTimer *inputTimer;
  119. Q3ListBoxItem *pressedItem, *selectAnchor;
  120. uint select :1;
  121. uint pressedSelected :1;
  122. uint rowModeWins :1;
  123. uint ignoreMoves :1;
  124. uint clearing :1;
  125. uint layoutDirty :1;
  126. uint mustPaintAll :1;
  127. uint dragging :1;
  128. uint dirtyDrag :1;
  129. uint variableHeight :1;
  130. uint variableWidth :1;
  131. uint inMenuMode :1;
  132. QRect *rubber;
  133. struct SortableItem {
  134. Q3ListBoxItem *item;
  135. };
  136. };
  137. Q3ListBoxPrivate::~Q3ListBoxPrivate()
  138. {
  139. Q_ASSERT(!head);
  140. }
  141. /*!
  142. \class Q3ListBoxItem
  143. \brief The Q3ListBoxItem class is the base class of all list box items.
  144. \compat
  145. This class is an abstract base class used for all list box items.
  146. If you need to insert customized items into a Q3ListBox you must
  147. inherit this class and reimplement paint(), height() and width().
  148. \sa Q3ListBox
  149. */
  150. /*!
  151. Constructs an empty list box item in the list box \a listbox.
  152. */
  153. Q3ListBoxItem::Q3ListBoxItem(Q3ListBox* listbox)
  154. {
  155. lbox = listbox;
  156. s = false;
  157. dirty = true;
  158. custom_highlight = false;
  159. selectable = true;
  160. p = n = 0;
  161. if (listbox)
  162. listbox->insertItem(this);
  163. }
  164. /*!
  165. Constructs an empty list box item in the list box \a listbox and
  166. inserts it after the item \a after or at the beginning if \a after
  167. is 0.
  168. */
  169. Q3ListBoxItem::Q3ListBoxItem(Q3ListBox* listbox, Q3ListBoxItem *after)
  170. {
  171. lbox = listbox;
  172. s = false;
  173. dirty = true;
  174. custom_highlight = false;
  175. selectable = true;
  176. p = n = 0;
  177. if (listbox)
  178. listbox->insertItem(this, after);
  179. }
  180. /*!
  181. Destroys the list box item.
  182. */
  183. Q3ListBoxItem::~Q3ListBoxItem()
  184. {
  185. if (lbox)
  186. lbox->takeItem(this);
  187. }
  188. /*!
  189. Defines whether the list box item is responsible for drawing
  190. itself in a highlighted state when being selected.
  191. If \a b is false (the default), the list box will draw some
  192. default highlight indicator before calling paint().
  193. \sa isSelected(), paint()
  194. */
  195. void Q3ListBoxItem::setCustomHighlighting(bool b)
  196. {
  197. custom_highlight = b;
  198. }
  199. /*!
  200. \fn void Q3ListBoxItem::paint(QPainter *p)
  201. Implement this function to draw your item. The painter, \a p, is
  202. already open for painting.
  203. \sa height(), width()
  204. */
  205. /*!
  206. \fn int Q3ListBoxItem::width(const Q3ListBox* lb) const
  207. Reimplement this function to return the width of your item. The \a
  208. lb parameter is the same as listBox() and is provided for
  209. convenience and compatibility.
  210. The default implementation returns
  211. \l{QApplication::globalStrut()}'s width.
  212. \sa paint(), height()
  213. */
  214. int Q3ListBoxItem::width(const Q3ListBox*) const
  215. {
  216. return QApplication::globalStrut().width();
  217. }
  218. /*!
  219. \fn int Q3ListBoxItem::height(const Q3ListBox* lb) const
  220. Implement this function to return the height of your item. The \a
  221. lb parameter is the same as listBox() and is provided for
  222. convenience and compatibility.
  223. The default implementation returns
  224. \l{QApplication::globalStrut()}'s height.
  225. \sa paint(), width()
  226. */
  227. int Q3ListBoxItem::height(const Q3ListBox*) const
  228. {
  229. return QApplication::globalStrut().height();
  230. }
  231. /*!
  232. Returns the text of the item. This text is also used for sorting.
  233. \sa setText()
  234. */
  235. QString Q3ListBoxItem::text() const
  236. {
  237. return txt;
  238. }
  239. /*!
  240. Returns the pixmap associated with the item, or 0 if there isn't
  241. one.
  242. The default implementation returns 0.
  243. */
  244. const QPixmap *Q3ListBoxItem::pixmap() const
  245. {
  246. return 0;
  247. }
  248. /*! \fn void Q3ListBoxItem::setSelectable(bool b)
  249. If \a b is true (the default) then this item can be selected by
  250. the user; otherwise this item cannot be selected by the user.
  251. \sa isSelectable()
  252. */
  253. /*! \fn bool Q3ListBoxItem::isSelectable() const
  254. Returns true if this item is selectable (the default); otherwise
  255. returns false.
  256. \sa setSelectable()
  257. */
  258. /*!
  259. \fn void Q3ListBoxItem::setText(const QString &text)
  260. Sets the text of the Q3ListBoxItem to \a text. This \a text is also
  261. used for sorting. The text is not shown unless explicitly drawn in
  262. paint().
  263. \sa text()
  264. */
  265. /*!
  266. \class Q3ListBoxText
  267. \brief The Q3ListBoxText class provides list box items that display text.
  268. \compat
  269. The text is drawn in the widget's current font. If you need
  270. several different fonts, you must implement your own subclass of
  271. Q3ListBoxItem.
  272. \sa Q3ListBox, Q3ListBoxItem
  273. */
  274. /*!
  275. Constructs a list box item in list box \a listbox showing the text
  276. \a text.
  277. */
  278. Q3ListBoxText::Q3ListBoxText(Q3ListBox *listbox, const QString &text)
  279. :Q3ListBoxItem(listbox)
  280. {
  281. setText(text);
  282. }
  283. /*!
  284. Constructs a list box item showing the text \a text.
  285. */
  286. Q3ListBoxText::Q3ListBoxText(const QString &text)
  287. :Q3ListBoxItem()
  288. {
  289. setText(text);
  290. }
  291. /*!
  292. Constructs a list box item in list box \a listbox showing the text
  293. \a text. The item is inserted after the item \a after, or at the
  294. beginning if \a after is 0.
  295. */
  296. Q3ListBoxText::Q3ListBoxText(Q3ListBox* listbox, const QString &text, Q3ListBoxItem *after)
  297. : Q3ListBoxItem(listbox, after)
  298. {
  299. setText(text);
  300. }
  301. /*!
  302. Destroys the item.
  303. */
  304. Q3ListBoxText::~Q3ListBoxText()
  305. {
  306. }
  307. /*!
  308. Draws the text using \a painter.
  309. */
  310. void Q3ListBoxText::paint(QPainter *painter)
  311. {
  312. int itemHeight = height(listBox());
  313. QFontMetrics fm = painter->fontMetrics();
  314. int yPos = ((itemHeight - fm.height()) / 2) + fm.ascent();
  315. painter->drawText(3, yPos, text());
  316. }
  317. /*!
  318. Returns the height of a line of text in list box \a lb.
  319. \sa paint(), width()
  320. */
  321. int Q3ListBoxText::height(const Q3ListBox* lb) const
  322. {
  323. int h = lb ? lb->fontMetrics().lineSpacing() + 2 : 0;
  324. return qMax(h, QApplication::globalStrut().height());
  325. }
  326. /*!
  327. Returns the width of this line in list box \a lb.
  328. \sa paint(), height()
  329. */
  330. int Q3ListBoxText::width(const Q3ListBox* lb) const
  331. {
  332. int w = lb ? lb->fontMetrics().width(text()) + 6 : 0;
  333. return qMax(w, QApplication::globalStrut().width());
  334. }
  335. /*!
  336. \fn int Q3ListBoxText::rtti() const
  337. \reimp
  338. Returns 1.
  339. Make your derived classes return their own values for rtti(), and
  340. you can distinguish between listbox items. You should use values
  341. greater than 1000 preferably a large random number, to allow for
  342. extensions to this class.
  343. */
  344. int Q3ListBoxText::rtti() const
  345. {
  346. return RTTI;
  347. }
  348. /*!
  349. \class Q3ListBoxPixmap
  350. \brief The Q3ListBoxPixmap class provides list box items with a
  351. pixmap and optional text.
  352. \compat
  353. Items of this class are drawn with the pixmap on the left with the
  354. optional text to the right of the pixmap.
  355. \sa Q3ListBox, Q3ListBoxItem
  356. */
  357. /*!
  358. Constructs a new list box item in list box \a listbox showing the
  359. pixmap \a pixmap.
  360. */
  361. Q3ListBoxPixmap::Q3ListBoxPixmap(Q3ListBox* listbox, const QPixmap &pixmap)
  362. : Q3ListBoxItem(listbox)
  363. {
  364. pm = pixmap;
  365. }
  366. /*!
  367. Constructs a new list box item showing the pixmap \a pixmap.
  368. */
  369. Q3ListBoxPixmap::Q3ListBoxPixmap(const QPixmap &pixmap)
  370. : Q3ListBoxItem()
  371. {
  372. pm = pixmap;
  373. }
  374. /*!
  375. Constructs a new list box item in list box \a listbox showing the
  376. pixmap \a pixmap. The item gets inserted after the item \a after,
  377. or at the beginning if \a after is 0.
  378. */
  379. Q3ListBoxPixmap::Q3ListBoxPixmap(Q3ListBox* listbox, const QPixmap &pixmap, Q3ListBoxItem *after)
  380. : Q3ListBoxItem(listbox, after)
  381. {
  382. pm = pixmap;
  383. }
  384. /*!
  385. Destroys the item.
  386. */
  387. Q3ListBoxPixmap::~Q3ListBoxPixmap()
  388. {
  389. }
  390. /*!
  391. Constructs a new list box item in list box \a listbox showing the
  392. pixmap \a pix and the text \a text.
  393. */
  394. Q3ListBoxPixmap::Q3ListBoxPixmap(Q3ListBox* listbox, const QPixmap &pix, const QString& text)
  395. : Q3ListBoxItem(listbox)
  396. {
  397. pm = pix;
  398. setText(text);
  399. }
  400. /*!
  401. Constructs a new list box item showing the pixmap \a pix and the
  402. text to \a text.
  403. */
  404. Q3ListBoxPixmap::Q3ListBoxPixmap(const QPixmap & pix, const QString& text)
  405. : Q3ListBoxItem()
  406. {
  407. pm = pix;
  408. setText(text);
  409. }
  410. /*!
  411. Constructs a new list box item in list box \a listbox showing the
  412. pixmap \a pix and the string \a text. The item gets inserted after
  413. the item \a after, or at the beginning if \a after is 0.
  414. */
  415. Q3ListBoxPixmap::Q3ListBoxPixmap(Q3ListBox* listbox, const QPixmap & pix, const QString& text,
  416. Q3ListBoxItem *after)
  417. : Q3ListBoxItem(listbox, after)
  418. {
  419. pm = pix;
  420. setText(text);
  421. }
  422. /*!
  423. \fn const QPixmap *Q3ListBoxPixmap::pixmap() const
  424. Returns the pixmap associated with the item.
  425. */
  426. /*!
  427. Draws the pixmap using \a painter.
  428. */
  429. void Q3ListBoxPixmap::paint(QPainter *painter)
  430. {
  431. int itemHeight = height(listBox());
  432. int yPos;
  433. const QPixmap *pm = pixmap();
  434. if (pm && ! pm->isNull()) {
  435. yPos = (itemHeight - pm->height()) / 2;
  436. painter->drawPixmap(3, yPos, *pm);
  437. }
  438. if (!text().isEmpty()) {
  439. QFontMetrics fm = painter->fontMetrics();
  440. yPos = ((itemHeight - fm.height()) / 2) + fm.ascent();
  441. painter->drawText(pm->width() + 5, yPos, text());
  442. }
  443. }
  444. /*!
  445. Returns the height of the pixmap in list box \a lb.
  446. \sa paint(), width()
  447. */
  448. int Q3ListBoxPixmap::height(const Q3ListBox* lb) const
  449. {
  450. int h;
  451. if (text().isEmpty())
  452. h = pm.height();
  453. else
  454. h = qMax(pm.height(), lb->fontMetrics().lineSpacing() + 2);
  455. return qMax(h, QApplication::globalStrut().height());
  456. }
  457. /*!
  458. Returns the width of the pixmap plus some margin in list box \a lb.
  459. \sa paint(), height()
  460. */
  461. int Q3ListBoxPixmap::width(const Q3ListBox* lb) const
  462. {
  463. if (text().isEmpty())
  464. return qMax(pm.width() + 6, QApplication::globalStrut().width());
  465. return qMax(pm.width() + lb->fontMetrics().width(text()) + 6,
  466. QApplication::globalStrut().width());
  467. }
  468. /*!
  469. \fn int Q3ListBoxPixmap::rtti() const
  470. \reimp
  471. Returns 2.
  472. Make your derived classes return their own values for rtti(), and
  473. you can distinguish between listbox items. You should use values
  474. greater than 1000 preferably a large random number, to allow for
  475. extensions to this class.
  476. */
  477. int Q3ListBoxPixmap::rtti() const
  478. {
  479. return RTTI;
  480. }
  481. /*!
  482. \class Q3ListBox
  483. \brief The Q3ListBox widget provides a list of selectable, read-only items.
  484. \compat
  485. This is typically a single-column list in which either no item or
  486. one item is selected, but it can also be used in many other ways.
  487. Q3ListBox will add scroll bars as necessary, but it isn't intended
  488. for \e really big lists. If you want more than a few thousand
  489. items, it's probably better to use a different widget mainly
  490. because the scroll bars won't provide very good navigation, but
  491. also because Q3ListBox may become slow with huge lists. (See
  492. Q3ListView and Q3Table for possible alternatives.)
  493. There are a variety of selection modes described in the
  494. Q3ListBox::SelectionMode documentation. The default is \l Single
  495. selection mode, but you can change it using setSelectionMode().
  496. (setMultiSelection() is still provided for compatibility with Qt
  497. 1.x. We recommend using setSelectionMode() in all code.)
  498. Because Q3ListBox offers multiple selection it must display
  499. keyboard focus and selection state separately. Therefore there are
  500. functions both to set the selection state of an item, i.e.
  501. setSelected(), and to set which item displays keyboard focus, i.e.
  502. setCurrentItem().
  503. The list box normally arranges its items in a single column and
  504. adds a vertical scroll bar if required. It is possible to have a
  505. different fixed number of columns (setColumnMode()), or as many
  506. columns as will fit in the list box's assigned screen space
  507. (setColumnMode(FitToWidth)), or to have a fixed number of rows
  508. (setRowMode()) or as many rows as will fit in the list box's
  509. assigned screen space (setRowMode(FitToHeight)). In all these
  510. cases Q3ListBox will add scroll bars, as appropriate, in at least
  511. one direction.
  512. If multiple rows are used, each row can be as high as necessary
  513. (the normal setting), or you can request that all items will have
  514. the same height by calling setVariableHeight(false). The same
  515. applies to a column's width, see setVariableWidth().
  516. The Q3ListBox's items are Q3ListBoxItem objects. Q3ListBox provides
  517. methods to insert new items as strings, as pixmaps, and as
  518. Q3ListBoxItem * (insertItem() with various arguments), and to
  519. replace an existing item with a new string, pixmap or Q3ListBoxItem
  520. (changeItem() with various arguments). You can also remove items
  521. singly with removeItem() or clear() the entire list box. Note that
  522. if you create a Q3ListBoxItem yourself and insert it, Q3ListBox
  523. takes ownership of the item.
  524. You can also create a Q3ListBoxItem, such as Q3ListBoxText or
  525. Q3ListBoxPixmap, with the list box as first parameter. The item
  526. will then append itself. When you delete an item it is
  527. automatically removed from the list box.
  528. The list of items can be arbitrarily large; Q3ListBox will add
  529. scroll bars if necessary. Q3ListBox can display a single-column
  530. (the common case) or multiple-columns, and offers both single and
  531. multiple selection. Q3ListBox does not support multiple-column
  532. items (but Q3ListView and Q3Table do), or tree hierarchies (but
  533. Q3ListView does).
  534. The list box items can be accessed both as Q3ListBoxItem objects
  535. (recommended) and using integer indexes (the original Q3ListBox
  536. implementation used an array of strings internally, and the API
  537. still supports this mode of operation). Everything can be done
  538. using the new objects, and most things can be done using indexes.
  539. Each item in a Q3ListBox contains a Q3ListBoxItem. One of the items
  540. can be the current item. The currentChanged() signal and the
  541. highlighted() signal are emitted when a new item becomes current,
  542. e.g. because the user clicks on it or Q3ListBox::setCurrentItem()
  543. is called. The selected() signal is emitted when the user
  544. double-clicks on an item or presses Enter on the current item.
  545. If the user does not select anything, no signals are emitted and
  546. currentItem() returns -1.
  547. A list box has Qt::WheelFocus as a default focusPolicy(), i.e. it
  548. can get keyboard focus by tabbing, clicking and through the use of
  549. the mouse wheel.
  550. New items can be inserted using insertItem(), insertStrList() or
  551. insertStringList().
  552. By default, vertical and horizontal scroll bars are added and
  553. removed as necessary. setHScrollBarMode() and setVScrollBarMode()
  554. can be used to change this policy.
  555. If you need to insert types other than strings and pixmaps, you
  556. must define new classes which inherit Q3ListBoxItem.
  557. \warning The list box assumes ownership of all list box items and
  558. will delete them when it does not need them any more.
  559. \inlineimage qlistbox-m.png Screenshot in Motif style
  560. \inlineimage qlistbox-w.png Screenshot in Windows style
  561. \sa Q3ListView, QComboBox, QButtonGroup
  562. */
  563. /*!
  564. \enum Q3ListBox::SelectionMode
  565. This enumerated type is used by Q3ListBox to indicate how it reacts
  566. to selection by the user.
  567. \value Single When the user selects an item, any already-selected
  568. item becomes unselected and the user cannot unselect the selected
  569. item. This means that the user can never clear the selection, even
  570. though the selection may be cleared by the application programmer
  571. using Q3ListBox::clearSelection().
  572. \value Multi When the user selects an item the selection status
  573. of that item is toggled and the other items are left alone.
  574. \value Extended When the user selects an item the selection is
  575. cleared and the new item selected. However, if the user presses
  576. the Ctrl key when clicking on an item, the clicked item gets
  577. toggled and all other items are left untouched. And if the user
  578. presses the Shift key while clicking on an item, all items between
  579. the current item and the clicked item get selected or unselected,
  580. depending on the state of the clicked item. Also, multiple items
  581. can be selected by dragging the mouse while the left mouse button
  582. is kept pressed.
  583. \value NoSelection Items cannot be selected.
  584. In other words, \c Single is a real single-selection list box, \c
  585. Multi is a real multi-selection list box, \c Extended is a list
  586. box in which users can select multiple items but usually want to
  587. select either just one or a range of contiguous items, and \c
  588. NoSelection is for a list box where the user can look but not
  589. touch.
  590. */
  591. /*!
  592. \enum Q3ListBox::LayoutMode
  593. This enum type is used to specify how Q3ListBox lays out its rows
  594. and columns.
  595. \value FixedNumber There is a fixed number of rows (or columns).
  596. \value FitToWidth There are as many columns as will fit
  597. on-screen.
  598. \value FitToHeight There are as many rows as will fit on-screen.
  599. \value Variable There are as many rows as are required by the
  600. column mode. (Or as many columns as required by the row mode.)
  601. Example: When you call setRowMode(FitToHeight), columnMode()
  602. automatically becomes \c Variable to accommodate the row mode
  603. you've set.
  604. */
  605. /*!
  606. \fn void Q3ListBox::onItem(Q3ListBoxItem *i)
  607. This signal is emitted when the user moves the mouse cursor onto
  608. an item, similar to the QWidget::enterEvent() function. \a i is
  609. the Q3ListBoxItem that the mouse has moved on.
  610. */
  611. // ### bug here too? enter/leave event may noit considered. move the
  612. // mouse out of the window and back in, to the same item - does it
  613. // work?
  614. /*!
  615. \fn void Q3ListBox::onViewport()
  616. This signal is emitted when the user moves the mouse cursor from
  617. an item to an empty part of the list box.
  618. */
  619. /*!
  620. Constructs a new empty list box called \a name and with parent \a
  621. parent and widget attributes \a f.
  622. This constructor sets the Qt::WA_StaticContent and the
  623. Qt::WA_NoBackground attributes to boost performance when drawing
  624. Q3ListBoxItems. This may be unsuitable for custom Q3ListBoxItem
  625. classes, in which case Qt::WA_StaticContents and Qt::WA_NoBackground
  626. should be cleared on the viewport() after construction.
  627. */
  628. Q3ListBox::Q3ListBox(QWidget *parent, const char *name, Qt::WindowFlags f)
  629. : Q3ScrollView(parent, name, f | Qt::WStaticContents | Qt::WNoAutoErase)
  630. {
  631. d = new Q3ListBoxPrivate(this);
  632. d->updateTimer = new QTimer(this, "listbox update timer");
  633. d->visibleTimer = new QTimer(this, "listbox visible timer");
  634. d->inputTimer = new QTimer(this, "listbox input timer");
  635. d->resizeTimer = new QTimer(this, "listbox resize timer");
  636. d->clearing = false;
  637. d->pressedItem = 0;
  638. d->selectAnchor = 0;
  639. d->select = false;
  640. d->rubber = 0;
  641. setMouseTracking(true);
  642. viewport()->setMouseTracking(true);
  643. connect(d->updateTimer, SIGNAL(timeout()),
  644. this, SLOT(refreshSlot()));
  645. connect(d->visibleTimer, SIGNAL(timeout()),
  646. this, SLOT(ensureCurrentVisible()));
  647. connect(d->resizeTimer, SIGNAL(timeout()),
  648. this, SLOT(adjustItems()));
  649. viewport()->setBackgroundRole(QPalette::Base);
  650. viewport()->setFocusProxy(this);
  651. viewport()->setFocusPolicy(Qt::WheelFocus);
  652. setFocusPolicy(Qt::WheelFocus);
  653. setAttribute(Qt::WA_MacShowFocusRect);
  654. }
  655. Q3ListBox * Q3ListBox::changedListBox = 0;
  656. /*!
  657. Destroys the list box. Deletes all list box items.
  658. */
  659. Q3ListBox::~Q3ListBox()
  660. {
  661. if (changedListBox == this)
  662. changedListBox = 0;
  663. clear();
  664. delete d;
  665. d = 0;
  666. }
  667. /*!
  668. \fn void Q3ListBox::pressed(Q3ListBoxItem *item)
  669. This signal is emitted when the user presses any mouse button. If
  670. \a item is not 0, the cursor is on \a item. If \a item is 0, the
  671. mouse cursor isn't on any item.
  672. Note that you must not delete any Q3ListBoxItem objects in slots
  673. connected to this signal.
  674. */
  675. /*!
  676. \fn void Q3ListBox::pressed(Q3ListBoxItem *item, const QPoint &pnt)
  677. \overload
  678. This signal is emitted when the user presses any mouse button. If
  679. \a item is not 0, the cursor is on \a item. If \a item is 0, the
  680. mouse cursor isn't on any item.
  681. \a pnt is the position of the mouse cursor in the global
  682. coordinate system (QMouseEvent::globalPos()).
  683. Note that you must not delete any Q3ListBoxItem objects in slots
  684. connected to this signal.
  685. \sa mouseButtonPressed() rightButtonPressed() clicked()
  686. */
  687. /*!
  688. \fn void Q3ListBox::clicked(Q3ListBoxItem *item)
  689. This signal is emitted when the user clicks any mouse button. If
  690. \a item is not 0, the cursor is on \a item. If \a item is 0, the
  691. mouse cursor isn't on any item.
  692. Note that you must not delete any Q3ListBoxItem objects in slots
  693. connected to this signal.
  694. */
  695. /*!
  696. \fn void Q3ListBox::clicked(Q3ListBoxItem *item, const QPoint &pnt)
  697. \overload
  698. This signal is emitted when the user clicks any mouse button. If
  699. \a item is not 0, the cursor is on \a item. If \a item is 0, the
  700. mouse cursor isn't on any item.
  701. \a pnt is the position of the mouse cursor in the global
  702. coordinate system (QMouseEvent::globalPos()). (If the click's
  703. press and release differs by a pixel or two, \a pnt is the
  704. position at release time.)
  705. Note that you must not delete any Q3ListBoxItem objects in slots
  706. connected to this signal.
  707. */
  708. /*!
  709. \fn void Q3ListBox::mouseButtonClicked (int button, Q3ListBoxItem * item, const QPoint & pos)
  710. This signal is emitted when the user clicks mouse button \a
  711. button. If \a item is not 0, the cursor is on \a item. If \a item
  712. is 0, the mouse cursor isn't on any item.
  713. \a pos is the position of the mouse cursor in the global
  714. coordinate system (QMouseEvent::globalPos()). (If the click's
  715. press and release differs by a pixel or two, \a pos is the
  716. position at release time.)
  717. Note that you must not delete any Q3ListBoxItem objects in slots
  718. connected to this signal.
  719. */
  720. /*!
  721. \fn void Q3ListBox::mouseButtonPressed (int button, Q3ListBoxItem * item, const QPoint & pos)
  722. This signal is emitted when the user presses mouse button \a
  723. button. If \a item is not 0, the cursor is on \a item. If \a item
  724. is 0, the mouse cursor isn't on any item.
  725. \a pos is the position of the mouse cursor in the global
  726. coordinate system (QMouseEvent::globalPos()).
  727. Note that you must not delete any Q3ListBoxItem objects in slots
  728. connected to this signal.
  729. */
  730. /*!
  731. \fn void Q3ListBox::doubleClicked(Q3ListBoxItem *item)
  732. This signal is emitted whenever an item is double-clicked. It's
  733. emitted on the second button press, not the second button release.
  734. If \a item is not 0, the cursor is on \a item. If \a item is 0,
  735. the mouse cursor isn't on any item.
  736. */
  737. /*!
  738. \fn void Q3ListBox::returnPressed(Q3ListBoxItem *item)
  739. This signal is emitted when Enter or Return is pressed. The
  740. \a item passed in the argument is currentItem().
  741. */
  742. /*!
  743. \fn void Q3ListBox::rightButtonClicked(Q3ListBoxItem *item, const QPoint& point)
  744. This signal is emitted when the right button is clicked. The \a
  745. item is the item that the button was clicked on (which could be
  746. 0 if no item was clicked on), and the \a point is where the
  747. click took place in global coordinates.
  748. */
  749. /*!
  750. \fn void Q3ListBox::rightButtonPressed (Q3ListBoxItem *item, const QPoint &point)
  751. This signal is emitted when the right button is pressed. The \a
  752. item is the item that the button was pressed over (which could be
  753. 0 if no item was pressed over), and the \a point is where the
  754. press took place in global coordinates.
  755. */
  756. /*!
  757. \fn void Q3ListBox::contextMenuRequested(Q3ListBoxItem *item, const QPoint & pos)
  758. This signal is emitted when the user invokes a context menu with
  759. the right mouse button or with special system keys, with \a item
  760. being the item under the mouse cursor or the current item,
  761. respectively.
  762. \a pos is the position for the context menu in the global
  763. coordinate system.
  764. */
  765. /*!
  766. \fn void Q3ListBox::selectionChanged()
  767. This signal is emitted when the selection set of a list box
  768. changes. This signal is emitted in each selection mode. If the
  769. user selects five items by drag-selecting, Q3ListBox tries to emit
  770. just one selectionChanged() signal so the signal can be connected
  771. to computationally expensive slots.
  772. \sa selected() currentItem()
  773. */
  774. /*!
  775. \fn void Q3ListBox::selectionChanged(Q3ListBoxItem *item)
  776. \overload
  777. This signal is emitted when the selection in a \l Single selection
  778. list box changes. \a item is the newly selected list box item.
  779. \sa selected() currentItem()
  780. */
  781. /*!
  782. \fn void Q3ListBox::currentChanged(Q3ListBoxItem *item)
  783. This signal is emitted when the user makes a new item the current
  784. item. \a item is the new current list box item.
  785. \sa setCurrentItem() currentItem()
  786. */
  787. /*!
  788. \fn void Q3ListBox::highlighted(int index)
  789. This signal is emitted when the user makes a new item the current
  790. item. \a index is the index of the new current item.
  791. \sa currentChanged() selected() currentItem() selectionChanged()
  792. */
  793. /*!
  794. \fn void Q3ListBox::highlighted(Q3ListBoxItem *item)
  795. \overload
  796. This signal is emitted when the user makes a new \a item the current
  797. \a item.
  798. \sa currentChanged() selected() currentItem() selectionChanged()
  799. */
  800. /*!
  801. \fn void Q3ListBox::highlighted(const QString & text)
  802. \overload
  803. This signal is emitted when the user makes a new item the current
  804. item and the item is (or has) as string. The argument is the new
  805. current item's \a text.
  806. \sa currentChanged() selected() currentItem() selectionChanged()
  807. */
  808. /*!
  809. \fn void Q3ListBox::selected(int index)
  810. This signal is emitted when the user double-clicks on an item or
  811. presses Enter on the current item. \a index is the index of the
  812. selected item.
  813. \sa currentChanged() highlighted() selectionChanged()
  814. */
  815. /*!
  816. \fn void Q3ListBox::selected(Q3ListBoxItem *item)
  817. \overload
  818. This signal is emitted when the user double-clicks on an \a item or
  819. presses Enter on the current \a item.
  820. \sa currentChanged() highlighted() selectionChanged()
  821. */
  822. /*!
  823. \fn void Q3ListBox::selected(const QString &text)
  824. \overload
  825. This signal is emitted when the user double-clicks on an item or
  826. presses Enter on the current item, and the item is (or has) a
  827. string. The argument is the \a text of the selected item.
  828. \sa currentChanged() highlighted() selectionChanged()
  829. */
  830. /*!
  831. \property Q3ListBox::count
  832. \brief the number of items in the list box
  833. */
  834. uint Q3ListBox::count() const
  835. {
  836. return d->count;
  837. }
  838. #if 0
  839. /*!
  840. Inserts the string list \a list into the list at position \a
  841. index.
  842. If \a index is negative, \a list is inserted at the end of the
  843. list. If \a index is too large, the operation is ignored.
  844. \warning This function uses \c{const char *} rather than QString,
  845. so we recommend against using it. It is provided so that legacy
  846. code will continue to work, and so that programs that certainly
  847. will not need to handle code outside a single 8-bit locale can use
  848. it. See insertStringList() which uses real QStrings.
  849. \warning This function is never significantly faster than a loop
  850. around insertItem().
  851. \sa insertItem(), insertStringList()
  852. */
  853. void Q3ListBox::insertStrList(const QStrList *list, int index)
  854. {
  855. if (!list) {
  856. Q_ASSERT(list != 0);
  857. return;
  858. }
  859. insertStrList(*list, index);
  860. }
  861. #endif
  862. /*!
  863. Inserts the string list \a list into the list at position \a
  864. index.
  865. If \a index is negative, \a list is inserted at the end of the
  866. list. If \a index is too large, the operation is ignored.
  867. \warning This function is never significantly faster than a loop
  868. around insertItem().
  869. \sa insertItem(), insertStrList()
  870. */
  871. void Q3ListBox::insertStringList(const QStringList & list, int index)
  872. {
  873. if (index < 0)
  874. index = count();
  875. for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
  876. insertItem(new Q3ListBoxText(*it), index++);
  877. }
  878. #if 0
  879. /*!
  880. \overload
  881. Inserts the string list \a list into the list at position \a
  882. index.
  883. If \a index is negative, \a list is inserted at the end of the
  884. list. If \a index is too large, the operation is ignored.
  885. \warning This function uses \c{const char *} rather than QString,
  886. so we recommend against using it. It is provided so that legacy
  887. code will continue to work, and so that programs that certainly
  888. will not need to handle code outside a single 8-bit locale can use
  889. it. See insertStringList() which uses real QStrings.
  890. \warning This function is never significantly faster than a loop
  891. around insertItem().
  892. \sa insertItem(), insertStringList()
  893. */
  894. void Q3ListBox::insertStrList(const QStrList & list, int index)
  895. {
  896. QStrListIterator it(list);
  897. const char* txt;
  898. if (index < 0)
  899. index = count();
  900. while ((txt=it.current())) {
  901. ++it;
  902. insertItem(new Q3ListBoxText(QString::fromLatin1(txt)),
  903. index++);
  904. }
  905. if (hasFocus() && !d->current)
  906. setCurrentItem(d->head);
  907. }
  908. #endif
  909. /*!
  910. Inserts the \a numStrings strings of the array \a strings into the
  911. list at position \a index.
  912. If \a index is negative, insertStrList() inserts \a strings at the
  913. end of the list. If \a index is too large, the operation is
  914. ignored.
  915. \warning This function uses \c{const char *} rather than QString,
  916. so we recommend against using it. It is provided so that legacy
  917. code will continue to work, and so that programs that certainly
  918. will not need to handle code outside a single 8-bit locale can use
  919. it. See insertStringList() which uses real QStrings.
  920. \warning This function is never significantly faster than a loop
  921. around insertItem().
  922. \sa insertItem(), insertStringList()
  923. */
  924. void Q3ListBox::insertStrList(const char **strings, int numStrings, int index)
  925. {
  926. if (!strings) {
  927. Q_ASSERT(strings != 0);
  928. return;
  929. }
  930. if (index < 0)
  931. index = count();
  932. int i = 0;
  933. while ((numStrings<0 && strings[i]!=0) || i<numStrings) {
  934. insertItem(new Q3ListBoxText(QString::fromLatin1(strings[i])),
  935. index + i);
  936. i++;
  937. }
  938. if (hasFocus() && !d->current)
  939. setCurrentItem(d->head);
  940. }
  941. /*!
  942. Inserts the item \a lbi into the list at position \a index.
  943. If \a index is negative or larger than the number of items in the
  944. list box, \a lbi is inserted at the end of the list.
  945. \sa insertStrList()
  946. */
  947. void Q3ListBox::insertItem(const Q3ListBoxItem *lbi, int index)
  948. {
  949. if (!lbi)
  950. return;
  951. if (index < 0)
  952. index = d->count;
  953. if (index >= d->count) {
  954. insertItem(lbi, d->last);
  955. return;
  956. }
  957. Q3ListBoxItem * item = (Q3ListBoxItem *)lbi;
  958. d->count++;
  959. d->cache = 0;
  960. item->lbox = this;
  961. if (!d->head || index == 0) {
  962. item->n = d->head;
  963. item->p = 0;
  964. d->head = item;
  965. item->dirty = true;
  966. if (item->n)
  967. item->n->p = item;
  968. } else {
  969. Q3ListBoxItem * i = d->head;
  970. while (i->n && index > 1) {
  971. i = i->n;
  972. index--;
  973. }
  974. if (i->n) {
  975. item->n = i->n;
  976. item->p = i;
  977. item->n->p = item;
  978. item->p->n = item;
  979. } else {
  980. i->n = item;
  981. item->p = i;
  982. item->n = 0;
  983. }
  984. }
  985. if (hasFocus() && !d->current) {
  986. d->current = d->head;
  987. updateItem(d->current);
  988. emit highlighted(d->current);
  989. emit highlighted(d->current->text());
  990. emit highlighted(index);
  991. }
  992. triggerUpdate(true);
  993. }
  994. /*!
  995. \overload
  996. Inserts the item \a lbi into the list after the item \a after, or
  997. at the beginning if \a after is 0.
  998. \sa insertStrList()
  999. */
  1000. void Q3ListBox::insertItem(const Q3ListBoxItem *lbi, const Q3ListBoxItem *after)
  1001. {
  1002. if (!lbi)
  1003. return;
  1004. Q3ListBoxItem * item = (Q3ListBoxItem*)lbi;
  1005. d->count++;
  1006. d->cache = 0;
  1007. item->lbox = this;
  1008. if (!d->head || !after) {
  1009. item->n = d->head;
  1010. item->p = 0;
  1011. d->head = item;
  1012. item->dirty = true;
  1013. if (item->n)
  1014. item->n->p = item;
  1015. } else {
  1016. Q3ListBoxItem * i = (Q3ListBoxItem*) after;
  1017. if (i) {
  1018. item->n = i->n;
  1019. item->p = i;
  1020. if (item->n)
  1021. item->n->p = item;
  1022. if (item->p)
  1023. item->p->n = item;
  1024. }
  1025. }
  1026. if (after == d->last)
  1027. d->last = (Q3ListBoxItem*) lbi;
  1028. if (hasFocus() && !d->current) {
  1029. d->current = d->head;
  1030. updateItem(d->current);
  1031. emit highlighted(d->current);
  1032. emit highlighted(d->current->text());
  1033. emit highlighted(index(d->current));
  1034. }
  1035. triggerUpdate(true);
  1036. }
  1037. /*!
  1038. \overload
  1039. Inserts a new list box text item with the text \a text into the
  1040. list at position \a index.
  1041. If \a index is negative, \a text is inserted at the end of the
  1042. list.
  1043. \sa insertStrList()
  1044. */
  1045. void Q3ListBox::insertItem(const QString &text, int index)
  1046. {
  1047. insertItem(new Q3ListBoxText(text), index);
  1048. }
  1049. /*!
  1050. \overload
  1051. Inserts a new list box pixmap item with the pixmap \a pixmap into
  1052. the list at position \a index.
  1053. If \a index is negative, \a pixmap is inserted at the end of the
  1054. list.
  1055. \sa insertStrList()
  1056. */
  1057. void Q3ListBox::insertItem(const QPixmap &pixmap, int index)
  1058. {
  1059. insertItem(new Q3ListBoxPixmap(pixmap), index);
  1060. }
  1061. /*!
  1062. \overload
  1063. Inserts a new list box pixmap item with the pixmap \a pixmap and
  1064. the text \a text into the list at position \a index.
  1065. If \a index is negative, \a pixmap is inserted at the end of the
  1066. list.
  1067. \sa insertStrList()
  1068. */
  1069. void Q3ListBox::insertItem(const QPixmap &pixmap, const QString &text, int index)
  1070. {
  1071. insertItem(new Q3ListBoxPixmap(pixmap, text), index);
  1072. }
  1073. /*!
  1074. Removes and deletes the item at position \a index. If \a index is
  1075. equal to currentItem(), a new item becomes current and the
  1076. currentChanged() and highlighted() signals are emitted.
  1077. \sa insertItem(), clear()
  1078. */
  1079. void Q3ListBox::removeItem(int index)
  1080. {
  1081. bool wasVisible = itemVisible(currentItem());
  1082. delete item(index);
  1083. triggerUpdate(true);
  1084. if (wasVisible)
  1085. ensureCurrentVisible();
  1086. }
  1087. /*!
  1088. Deletes all the items in the list.
  1089. \sa removeItem()
  1090. */
  1091. void Q3ListBox::clear()
  1092. {
  1093. setContentsPos(0, 0);
  1094. bool blocked = signalsBlocked();
  1095. blockSignals(true);
  1096. d->clearing = true;
  1097. d->current = 0;
  1098. d->tmpCurrent = 0;
  1099. Q3ListBoxItem * i = d->head;
  1100. d->head = 0;
  1101. while (i) {
  1102. Q3ListBoxItem * n = i->n;
  1103. i->n = i->p = 0;
  1104. delete i;
  1105. i = n;
  1106. }
  1107. d->count = 0;
  1108. d->numRows = 1;
  1109. d->numColumns = 1;
  1110. d->currentRow = 0;
  1111. d->currentColumn = 0;
  1112. d->mousePressRow = -1;
  1113. d->mousePressColumn = -1;
  1114. d->mouseMoveRow = -1;
  1115. d->mouseMoveColumn = -1;
  1116. clearSelection();
  1117. d->selectAnchor = 0;
  1118. blockSignals(blocked);
  1119. triggerUpdate(true);
  1120. d->last = 0;
  1121. d->clearing = false;
  1122. }
  1123. /*!
  1124. Returns the text at position \a index, or an empty string if there
  1125. is no text at that position.
  1126. \sa pixmap()
  1127. */
  1128. QString Q3ListBox::text(int index) const
  1129. {
  1130. Q3ListBoxItem * i = item(index);
  1131. if (i)
  1132. return i->text();
  1133. return QString();
  1134. }
  1135. /*!
  1136. Returns a pointer to the pixmap at position \a index, or 0 if
  1137. there is no pixmap there.
  1138. \sa text()
  1139. */
  1140. const QPixmap *Q3ListBox::pixmap(int index) const
  1141. {
  1142. Q3ListBoxItem * i = item(index);
  1143. if (i)
  1144. return i->pixmap();
  1145. return 0;
  1146. }
  1147. /*!
  1148. \overload
  1149. Replaces the item at position \a index with a new list box text
  1150. item with text \a text.
  1151. The operation is ignored if \a index is out of range.
  1152. \sa insertItem(), removeItem()
  1153. */
  1154. void Q3ListBox::changeItem(const QString &text, int index)
  1155. {
  1156. if(index >= 0 && index < (int)count())
  1157. changeItem(new Q3ListBoxText(text), index);
  1158. }
  1159. /*!
  1160. \overload
  1161. Replaces the item at position \a index with a new list box pixmap
  1162. item with pixmap \a pixmap.
  1163. The operation is ignored if \a index is out of range.
  1164. \sa insertItem(), removeItem()
  1165. */
  1166. void Q3ListBox::changeItem(const QPixmap &pixmap, int index)
  1167. {
  1168. if(index >= 0 && index < (int)count())
  1169. changeItem(new Q3ListBoxPixmap(pixmap), index);
  1170. }
  1171. /*!
  1172. \overload
  1173. Replaces the item at position \a index with a new list box pixmap
  1174. item with pixmap \a pixmap and text \a text.
  1175. The operation is ignored if \a index is out of range.
  1176. \sa insertItem(), removeItem()
  1177. */
  1178. void Q3ListBox::changeItem(const QPixmap &pixmap, const QString &text, int index)
  1179. {
  1180. if(index >= 0 && index < (int)count())
  1181. changeItem(new Q3ListBoxPixmap(pixmap, text), index);
  1182. }
  1183. /*!
  1184. Replaces the item at position \a index with \a lbi. If \a index is
  1185. negative or too large, changeItem() does nothing.
  1186. The item that has been changed will become selected.
  1187. \sa insertItem(), removeItem()
  1188. */
  1189. void Q3ListBox::changeItem(const Q3ListBoxItem *lbi, int index)
  1190. {
  1191. if (!lbi || index < 0 || index >= (int)count())
  1192. return;
  1193. removeItem(index);
  1194. insertItem(lbi, index);
  1195. setCurrentItem(index);
  1196. }
  1197. /*!
  1198. \property Q3ListBox::numItemsVisible
  1199. \brief the number of visible items.
  1200. Both partially and entirely visible items are counted.
  1201. */
  1202. int Q3ListBox::numItemsVisible() const
  1203. {
  1204. doLayout();
  1205. int columns = 0;
  1206. int x = contentsX();
  1207. int i=0;
  1208. while (i < (int)d->columnPos.size()-1 &&
  1209. d->columnPos[i] < x)
  1210. i++;
  1211. if (i < (int)d->columnPos.size()-1 &&
  1212. d->columnPos[i] > x)
  1213. columns++;
  1214. x += visibleWidth();
  1215. while (i < (int)d->columnPos.size()-1 &&
  1216. d->columnPos[i] < x) {
  1217. i++;
  1218. columns++;
  1219. }
  1220. int y = contentsY();
  1221. int rows = 0;
  1222. while (i < (int)d->rowPos.size()-1 &&
  1223. d->rowPos[i] < y)
  1224. i++;
  1225. if (i < (int)d->rowPos.size()-1 &&
  1226. d->rowPos[i] > y)
  1227. rows++;
  1228. y += visibleHeight();
  1229. while (i < (int)d->rowPos.size()-1 &&
  1230. d->rowPos[i] < y) {
  1231. i++;
  1232. rows++;
  1233. }
  1234. return rows*columns;
  1235. }
  1236. int Q3ListBox::currentItem() const
  1237. {
  1238. if (!d->current || !d->head)
  1239. return -1;
  1240. return index(d->current);
  1241. }
  1242. /*!
  1243. \property Q3ListBox::currentText
  1244. \brief the text of the current item.
  1245. This is equivalent to text(currentItem()).
  1246. */
  1247. /*!
  1248. \property Q3ListBox::currentItem
  1249. \brief the current highlighted item
  1250. When setting this property, the highlighting is moved to the item
  1251. and the list box scrolled as necessary.
  1252. If no item is current, currentItem() returns -1.
  1253. */
  1254. void Q3ListBox::setCurrentItem(int index)
  1255. {
  1256. setCurrentItem(item(index));
  1257. }
  1258. /*!
  1259. \reimp
  1260. */
  1261. QVariant Q3ListBox::inputMethodQuery(Qt::InputMethodQuery query) const
  1262. {
  1263. if (query == Qt::ImMicroFocus)
  1264. return d->current ? itemRect(d->current) : QRect();
  1265. return QWidget::inputMethodQuery(query);
  1266. }
  1267. /*!
  1268. \overload
  1269. Sets the current item to the Q3ListBoxItem \a i.
  1270. */
  1271. void Q3ListBox::setCurrentItem(Q3ListBoxItem * i)
  1272. {
  1273. if (!i || d->current == i)
  1274. return;
  1275. Q3ListBoxItem * o = d->current;
  1276. d->current = i;
  1277. int ind = index(i);
  1278. if (i && selectionMode() == Single) {
  1279. bool changed = false;
  1280. if (o && o->s) {
  1281. changed = true;
  1282. o->s = false;
  1283. }
  1284. if (i && !i->s && d->selectionMode != NoSelection && i->isSelectable()) {
  1285. i->s = true;
  1286. changed = true;
  1287. emit selectionChanged(i);
  1288. #ifndef QT_NO_ACCESSIBILITY
  1289. QAccessible::updateAccessibility(viewport(), ind+1, QAccessible::StateChanged);
  1290. #endif
  1291. }
  1292. if (changed) {
  1293. emit selectionChanged();
  1294. #ifndef QT_NO_ACCESSIBILITY
  1295. QAccessible::updateAccessibility(viewport(), 0, QAccessible::Selection);
  1296. #endif
  1297. }
  1298. }
  1299. d->currentColumn = ind / numRows();
  1300. d->currentRow = ind % numRows();
  1301. if (o)
  1302. updateItem(o);
  1303. if (i)
  1304. updateItem(i);
  1305. // scroll after the items are redrawn
  1306. d->visibleTimer->start(1, true);
  1307. QString tmp;
  1308. if (i)
  1309. tmp = i->text();
  1310. emit highlighted(i);
  1311. if (!tmp.isNull())
  1312. emit highlighted(tmp);
  1313. emit highlighted(ind);
  1314. emit currentChanged(i);
  1315. #ifndef QT_NO_ACCESSIBILITY
  1316. QAccessible::updateAccessibility(viewport(), ind+1, QAccessible::Focus);
  1317. #endif
  1318. }
  1319. /*!
  1320. Returns a pointer to the item at position \a index, or 0 if \a
  1321. index is out of bounds.
  1322. \sa index()
  1323. */
  1324. Q3ListBoxItem *Q3ListBox::item(int index) const
  1325. {
  1326. if (index < 0 || index > d->count -1)
  1327. return 0;
  1328. Q3ListBoxItem * i = d->head;
  1329. if (d->cache && index > 0) {
  1330. i = d->cache;
  1331. int idx = d->cacheIndex;
  1332. while (i && idx < index) {
  1333. idx++;
  1334. i = i->n;
  1335. }
  1336. while (i && idx > index) {
  1337. idx--;
  1338. i = i->p;
  1339. }
  1340. } else {
  1341. int idx = index;
  1342. while (i && idx > 0) {
  1343. idx--;
  1344. i = i->n;
  1345. }
  1346. }
  1347. if (index > 0) {
  1348. d->cache = i;
  1349. d->cacheIndex = index;
  1350. }
  1351. return i;
  1352. }
  1353. /*!
  1354. Returns the index of \a lbi, or -1 if the item is not in this list
  1355. box or \a lbi is 0.
  1356. \sa item()
  1357. */
  1358. int Q3ListBox::index(const Q3ListBoxItem * lbi) const
  1359. {
  1360. if (!lbi)
  1361. return -1;
  1362. Q3ListBoxItem * i_n = d->head;
  1363. int c_n = 0;
  1364. if (d->cache) {
  1365. i_n = d->cache;
  1366. c_n = d->cacheIndex;
  1367. }
  1368. Q3ListBoxItem* i_p = i_n;
  1369. int c_p = c_n;
  1370. while ((i_n != 0 || i_p != 0) && i_n != lbi && i_p != lbi) {
  1371. if (i_n) {
  1372. c_n++;
  1373. i_n = i_n->n;
  1374. }
  1375. if (i_p) {
  1376. c_p--;
  1377. i_p = i_p->p;
  1378. }
  1379. }
  1380. if (i_p == lbi)
  1381. return c_p;
  1382. if (i_n == lbi)
  1383. return c_n;
  1384. return -1;
  1385. }
  1386. /*!
  1387. Returns true if the item at position \a index is at least partly
  1388. visible; otherwise returns false.
  1389. */
  1390. bool Q3ListBox::itemVisible(int index)
  1391. {
  1392. Q3ListBoxItem * i = item(index);
  1393. return i ? itemVisible(i) : false;
  1394. }
  1395. /*!
  1396. \overload
  1397. Returns true if \a item is at least partly visible; otherwise
  1398. returns false.
  1399. */
  1400. bool Q3ListBox::itemVisible(const Q3ListBoxItem * item)
  1401. {
  1402. if (d->layoutDirty)
  1403. doLayout();
  1404. int i = index(item);
  1405. int col = i / numRows();
  1406. int row = i % numRows();
  1407. return (d->columnPos[col] < contentsX()+visibleWidth() &&
  1408. d->rowPos[row] < contentsY()+visibleHeight() &&
  1409. d->columnPos[col+1] > contentsX() &&
  1410. d->rowPos[row+1] > contentsY());
  1411. }
  1412. /*! \reimp */
  1413. void Q3ListBox::mousePressEvent(QMouseEvent *e)
  1414. {
  1415. mousePressEventEx(e);
  1416. }
  1417. void Q3ListBox::mousePressEventEx(QMouseEvent *e)
  1418. {
  1419. d->mouseInternalPress = true;
  1420. Q3ListBoxItem * i = itemAt(e->pos());
  1421. if (!i && !d->current && d->head) {
  1422. d->current = d->head;
  1423. updateItem(d->head);
  1424. }
  1425. if (!i && (d->selectionMode != Single || e->button() == Qt::RightButton)
  1426. && !(e->state() & Qt::ControlButton))
  1427. clearSelection();
  1428. d->select = d->selectionMode == Multi ? (i ? !i->isSelected() : false) : true;
  1429. d->pressedSelected = i && i->s;
  1430. if (i)
  1431. d->selectAnchor = i;
  1432. if (i) {
  1433. switch(selectionMode()) {
  1434. default:
  1435. case Single:
  1436. if (!i->s || i != d->current) {
  1437. if (i->isSelectable())
  1438. setSelected(i, true);
  1439. else
  1440. setCurrentItem(i);
  1441. }
  1442. break;
  1443. case Extended:
  1444. if (i) {
  1445. bool shouldBlock = false;
  1446. if (!(e->state() & Qt::ShiftButton) &&
  1447. !(e->state() & Qt::ControlButton)) {
  1448. if (!i->isSelected()) {
  1449. bool b = signalsBlocked();
  1450. blockSignals(true);
  1451. clearSelection();
  1452. blockSignals(b);
  1453. }
  1454. setSelected(i, true);
  1455. d->dragging = true; // always assume dragging
  1456. shouldBlock = true;
  1457. } else if (e->state() & Qt::ShiftButton) {
  1458. d->pressedSelected = false;
  1459. Q3ListBoxItem *oldCurrent = item(currentItem());
  1460. bool down = index(oldCurrent) < index(i);
  1461. Q3ListBoxItem *lit = down ? oldCurrent : i;
  1462. bool select = d->select;
  1463. bool blocked = signalsBlocked();
  1464. blockSignals(true);
  1465. for (;; lit = lit->n) {
  1466. if (!lit) {
  1467. triggerUpdate(false);
  1468. break;
  1469. }
  1470. if (down && lit == i) {
  1471. setSelected(i, select);
  1472. triggerUpdate(false);
  1473. break;
  1474. }
  1475. if (!down && lit == oldCurrent) {
  1476. setSelected(oldCurrent, select);
  1477. triggerUpdate(false);
  1478. break;
  1479. }
  1480. setSelected(lit, select);
  1481. }
  1482. blockSignals(blocked);
  1483. emit selectionChanged();
  1484. } else if (e->state() & Qt::ControlButton) {
  1485. setSelected(i, !i->isSelected()