QMultiComboBox.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (c) 2012 Richard Steffen and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: rsteffen@messbild.de, rsteffen@uni-bonn.de
00006 **
00007 ** QMultiComboBox is free to use unter the terms of the LGPL 2.1 License in
00008 ** Free and Commercial Products.
00009 ****************************************************************************/
00010 
00011 #include "QMultiComboBox.h"
00012 #include <QApplication>
00013 #include <QCoreApplication>
00014 #include <QDesktopWidget>
00015 
00016 #include "rtabmap/utilite/ULogger.h"
00017 
00018 QMultiComboBox::QMultiComboBox(QWidget *widget ) :
00019     QComboBox(widget),
00020     popheight_(0),
00021     screenbound_(50),
00022     popframe_(NULL, Qt::Popup)
00023 {
00024 
00025     SetDisplayText("Not Set");
00026 
00027     // setup the popup list
00028     vlist_.setSelectionMode(QAbstractItemView::MultiSelection);
00029     vlist_.setSelectionBehavior(QAbstractItemView::SelectItems);
00030     vlist_.clearSelection();
00031     popframe_.setLayout(new QVBoxLayout());
00032     popframe_.layout()->addWidget(&vlist_);
00033     popframe_.layout()->setContentsMargins(0,0,0,0);
00034 
00035     connect(&vlist_, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(scanItemSelect(QListWidgetItem*)));
00036 
00037 }
00038 
00039 
00040 QMultiComboBox::~QMultiComboBox()
00041 {
00042     disconnect(&vlist_,0,0,0);
00043 }
00044 
00045 
00046 void QMultiComboBox::SetDisplayText(QString text)
00047 {
00048     m_DisplayText_ = text;
00049     const int textWidth = fontMetrics().width(text);
00050     setMinimumWidth(textWidth + 30);
00051     updateGeometry();
00052     repaint();
00053 }
00054 
00055 
00056 QString QMultiComboBox::GetDisplayText() const
00057 {
00058     return m_DisplayText_;
00059 }
00060 
00061 
00062 void QMultiComboBox::setPopupHeight(int h)
00063 {
00064     popheight_ = h;
00065 }
00066 
00067 
00068 void QMultiComboBox::paintEvent(QPaintEvent *e)
00069 {
00070     QStylePainter painter(this);
00071     painter.setPen(palette().color(QPalette::Text));
00072     // draw the combobox frame, focusrect and selected etc.
00073     QStyleOptionComboBox opt;
00074 
00075     initStyleOption(&opt);
00076     opt.currentText = m_DisplayText_;
00077     painter.drawComplexControl(QStyle::CC_ComboBox, opt);
00078     // draw the icon and text
00079     painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
00080 }
00081 
00082 
00083 void QMultiComboBox::showPopup()
00084 {
00085     QRect rec = QRect(geometry());
00086 
00087     //QPoint p = this->mapToGlobal(QPoint(0,rec.height()));
00088     //QRect rec2(p , p + QPoint(rec.width(), rec.height()));
00089 
00090     // get the two possible list points and height
00091     QRect screen = QApplication::desktop()->screenGeometry(this);
00092     QPoint above = this->mapToGlobal(QPoint(0,0));
00093     int aboveHeight = above.y() - screen.y();
00094     QPoint below = this->mapToGlobal(QPoint(0,rec.height()));
00095     int belowHeight = screen.bottom() - below.y();
00096 
00097         // compute width
00098         int textWidth = vlist_.sizeHint().width();
00099 
00100     // first activate it with height 1px to get all the items initialized
00101     QRect rec2;
00102     rec2.setTopLeft(below);
00103     rec2.setWidth(textWidth>rec.width()? textWidth:rec.width());
00104     rec2.setHeight(rec.height());
00105     popframe_.setGeometry(rec2);
00106     popframe_.raise();
00107     popframe_.show();
00108     QCoreApplication::processEvents();
00109 
00110     // determine rect
00111     int contheight = vlist_.count()*vlist_.sizeHintForRow(0) + 4; // +4 - should be determined by margins?
00112     belowHeight = min(abs(belowHeight)-screenbound_, contheight);
00113     aboveHeight = min(abs(aboveHeight)-screenbound_, contheight);
00114     if (popheight_ > 0) // fixed
00115     {
00116         rec2.setHeight(popheight_);
00117     }
00118     else // dynamic
00119     {
00120         // do we use below or above
00121         if (belowHeight==contheight || belowHeight>aboveHeight)
00122         {
00123             rec2.setTopLeft(below);
00124             rec2.setHeight(belowHeight);
00125         }
00126         else
00127         {
00128             rec2.setTopLeft(above - QPoint(0,aboveHeight));
00129             rec2.setHeight(aboveHeight);
00130         }
00131     }
00132     popframe_.setGeometry(rec2);
00133     popframe_.raise();
00134     popframe_.show();
00135 }
00136 
00137 
00138 void QMultiComboBox::hidePopup()
00139 {
00140     popframe_.hide();
00141 }
00142 
00143 
00144 void QMultiComboBox::addItem ( const QString & text, const QVariant & userData)
00145 {
00146     QListWidgetItem* wi = new QListWidgetItem(text);
00147     wi->setFlags(wi->flags() | Qt::ItemIsUserCheckable);
00148     if (userData.toBool())
00149         wi->setCheckState(Qt::Checked);
00150     else
00151         wi->setCheckState(Qt::Unchecked);
00152     vlist_.addItem(wi);
00153         vlist_.setMinimumWidth(vlist_.sizeHintForColumn(0));
00154 }
00155 
00156 
00157 int QMultiComboBox::count()
00158 {
00159     return vlist_.count();
00160 }
00161 
00162 
00163 void QMultiComboBox::setCurrentIndex(int index)
00164 {
00165   //  cout << __FUNCTION__ << "DONT USE THIS ................" << endl;
00166 }
00167 
00168 
00169 QString QMultiComboBox::currentText()
00170 {
00171     return vlist_.currentItem()->text();
00172 }
00173 
00174 
00175 QString QMultiComboBox::itemText(int row)
00176 {
00177     return vlist_.item(row)->text();
00178 }
00179 
00180 
00181 QVariant QMultiComboBox::itemData(int row)
00182 {
00183     QListWidgetItem* item = vlist_.item(row);
00184     if (item->checkState() == Qt::Checked) return QVariant(true);
00185     return QVariant(false);
00186 }
00187 
00188 void QMultiComboBox::setItemChecked(int row, bool checked)
00189 {
00190         QListWidgetItem* item = vlist_.item(row);
00191         bool wasChecked = item->checkState() == Qt::Checked;
00192         if (wasChecked != checked)
00193         {
00194                 item->setCheckState(checked?Qt::Checked:Qt::Unchecked);
00195                 Q_EMIT itemChanged();
00196         }
00197 }
00198 
00199 
00200 void QMultiComboBox::scanItemSelect(QListWidgetItem* item)
00201 {
00202 
00203     QList<QListWidgetItem*> list = vlist_.selectedItems();
00204     for (int i = 0; i < list.count(); i++)
00205     {
00206         if (item->checkState() == Qt::Checked)
00207         {
00208             list[i]->setCheckState(Qt::Checked);
00209         }
00210         else
00211         {
00212             list[i]->setCheckState(Qt::Unchecked);
00213         }
00214         list[i]->setSelected(false);
00215     }
00216     Q_EMIT itemChanged();
00217 }
00218 
00219 void QMultiComboBox::initStyleOption(QStyleOptionComboBox *option) const
00220 {
00221     //Initializes the state, direction, rect, palette, and fontMetrics member variables based on the specified widget.
00222     //This is a convenience function; the member variables can also be initialized manually.
00223     option->initFrom(this);
00224 
00225 }
00226 
00227 void QMultiComboBox::clear()
00228 {
00229         vlist_.clear();
00230         QComboBox::clear();
00231 }
00232 


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:21