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


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:15