QMultiComboBox.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (c) 2012 Richard Steffen and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: rsteffen@messbild.de, rsteffen@uni-bonn.de
6 **
7 ** QMultiComboBox is free to use unter the terms of the LGPL 2.1 License in
8 ** Free and Commercial Products.
9 ****************************************************************************/
10 
11 #include "QMultiComboBox.h"
12 #include <QApplication>
13 #include <QCoreApplication>
14 #include <QDesktopWidget>
15 
17 
18 QMultiComboBox::QMultiComboBox(QWidget *widget ) :
19  QComboBox(widget),
20  popheight_(0),
21  screenbound_(50),
22  popframe_(NULL, Qt::Popup)
23 {
24 
25  SetDisplayText("Not Set");
26 
27  // setup the popup list
28  vlist_.setSelectionMode(QAbstractItemView::MultiSelection);
29  vlist_.setSelectionBehavior(QAbstractItemView::SelectItems);
30  vlist_.clearSelection();
31  popframe_.setLayout(new QVBoxLayout());
32  popframe_.layout()->addWidget(&vlist_);
33  popframe_.layout()->setContentsMargins(0,0,0,0);
34 
35  connect(&vlist_, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(scanItemSelect(QListWidgetItem*)));
36 
37 }
38 
39 
41 {
42  disconnect(&vlist_,0,0,0);
43 }
44 
45 
47 {
48  m_DisplayText_ = text;
49  const int textWidth = fontMetrics().width(text);
50  setMinimumWidth(textWidth + 30);
51  updateGeometry();
52  repaint();
53 }
54 
55 
57 {
58  return m_DisplayText_;
59 }
60 
61 
63 {
64  popheight_ = h;
65 }
66 
67 
68 void QMultiComboBox::paintEvent(QPaintEvent *e)
69 {
70  QStylePainter painter(this);
71  painter.setPen(palette().color(QPalette::Text));
72  // draw the combobox frame, focusrect and selected etc.
73  QStyleOptionComboBox opt;
74 
75  initStyleOption(&opt);
76  opt.currentText = m_DisplayText_;
77  painter.drawComplexControl(QStyle::CC_ComboBox, opt);
78  // draw the icon and text
79  painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
80 }
81 
82 
84 {
85  QRect rec = QRect(geometry());
86 
87  //QPoint p = this->mapToGlobal(QPoint(0,rec.height()));
88  //QRect rec2(p , p + QPoint(rec.width(), rec.height()));
89 
90  // get the two possible list points and height
91  QRect screen = QApplication::desktop()->screenGeometry(this);
92  QPoint above = this->mapToGlobal(QPoint(0,0));
93  int aboveHeight = above.y() - screen.y();
94  QPoint below = this->mapToGlobal(QPoint(0,rec.height()));
95  int belowHeight = screen.bottom() - below.y();
96 
97  // compute width
98  int textWidth = vlist_.sizeHint().width();
99 
100  // first activate it with height 1px to get all the items initialized
101  QRect rec2;
102  rec2.setTopLeft(below);
103  rec2.setWidth(textWidth>rec.width()? textWidth:rec.width());
104  rec2.setHeight(rec.height());
105  popframe_.setGeometry(rec2);
106  popframe_.raise();
107  popframe_.show();
108  QCoreApplication::processEvents();
109 
110  // determine rect
111  int contheight = vlist_.count()*vlist_.sizeHintForRow(0) + 4; // +4 - should be determined by margins?
112  belowHeight = min(abs(belowHeight)-screenbound_, contheight);
113  aboveHeight = min(abs(aboveHeight)-screenbound_, contheight);
114  if (popheight_ > 0) // fixed
115  {
116  rec2.setHeight(popheight_);
117  }
118  else // dynamic
119  {
120  // do we use below or above
121  if (belowHeight==contheight || belowHeight>aboveHeight)
122  {
123  rec2.setTopLeft(below);
124  rec2.setHeight(belowHeight);
125  }
126  else
127  {
128  rec2.setTopLeft(above - QPoint(0,aboveHeight));
129  rec2.setHeight(aboveHeight);
130  }
131  }
132  popframe_.setGeometry(rec2);
133  popframe_.raise();
134  popframe_.show();
135 }
136 
137 
139 {
140  popframe_.hide();
141 }
142 
143 
144 void QMultiComboBox::addItem ( const QString & text, const QVariant & userData)
145 {
146  QListWidgetItem* wi = new QListWidgetItem(text);
147  wi->setFlags(wi->flags() | Qt::ItemIsUserCheckable);
148  if (userData.toBool())
149  wi->setCheckState(Qt::Checked);
150  else
151  wi->setCheckState(Qt::Unchecked);
152  vlist_.addItem(wi);
153  vlist_.setMinimumWidth(vlist_.sizeHintForColumn(0));
154 }
155 
156 
158 {
159  return vlist_.count();
160 }
161 
162 
164 {
165  // cout << __FUNCTION__ << "DONT USE THIS ................" << endl;
166 }
167 
168 
170 {
171  return vlist_.currentItem()->text();
172 }
173 
174 
176 {
177  return vlist_.item(row)->text();
178 }
179 
180 
182 {
183  QListWidgetItem* item = vlist_.item(row);
184  if (item->checkState() == Qt::Checked) return QVariant(true);
185  return QVariant(false);
186 }
187 
188 void QMultiComboBox::setItemChecked(int row, bool checked)
189 {
190  QListWidgetItem* item = vlist_.item(row);
191  bool wasChecked = item->checkState() == Qt::Checked;
192  if (wasChecked != checked)
193  {
194  item->setCheckState(checked?Qt::Checked:Qt::Unchecked);
195  Q_EMIT itemChanged();
196  }
197 }
198 
199 
200 void QMultiComboBox::scanItemSelect(QListWidgetItem* item)
201 {
202 
203  QList<QListWidgetItem*> list = vlist_.selectedItems();
204  for (int i = 0; i < list.count(); i++)
205  {
206  if (item->checkState() == Qt::Checked)
207  {
208  list[i]->setCheckState(Qt::Checked);
209  }
210  else
211  {
212  list[i]->setCheckState(Qt::Unchecked);
213  }
214  list[i]->setSelected(false);
215  }
216  Q_EMIT itemChanged();
217 }
218 
219 void QMultiComboBox::initStyleOption(QStyleOptionComboBox *option) const
220 {
221  //Initializes the state, direction, rect, palette, and fontMetrics member variables based on the specified widget.
222  //This is a convenience function; the member variables can also be initialized manually.
223  option->initFrom(this);
224 
225 }
226 
228 {
229  vlist_.clear();
230  QComboBox::clear();
231 }
232 
virtual void paintEvent(QPaintEvent *e)
custom paint
#define NULL
GLM_FUNC_DECL genType min(genType const &x, genType const &y)
void setItemChecked(int row, bool checked)
void initStyleOption(QStyleOptionComboBox *option) const
the init style
void itemChanged()
item changed
GLM_FUNC_DECL genType e()
GLM_FUNC_DECL genType::row_type row(genType const &m, length_t const &index)
void scanItemSelect(QListWidgetItem *item)
react on changes of the item checkbox
void addItem(const QString &text, const QVariant &userData=QVariant())
add a item to the list
QVariant itemData(int row)
void SetDisplayText(QString text)
the main display text
int screenbound_
lower/upper screen bound
QString currentText()
void showPopup()
replace standard QComboBox Popup
QString GetDisplayText() const
get the main display text
GLM_FUNC_DECL genType abs(genType const &x)
QMultiComboBox(QWidget *widget=0)
Constructor.
void setPopupHeight(int h)
set the height of the popup
QFrame popframe_
popup frame
void setCurrentIndex(int index)
int popheight_
the height of the popup
QString m_DisplayText_
hold the main display text
ULogger class and convenient macros.
int count()
replace neccessary data access
virtual ~QMultiComboBox()
QString itemText(int row)
QListWidget vlist_
multi selection list in the popup frame


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:34:59