drop QMultiComboBox for activities selection
--QMultiComboBox couldnt cooperate nicely with the TableView, a different approach is needed. Probably it is easier to just add a text indicator for the selected activitiespull/1/head
parent
747fe2fd02
commit
0dfdd75746
@ -1,213 +0,0 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (c) 2012 Richard Steffen and/or its subsidiary(-ies).
|
||||
** All rights reserved.
|
||||
** Contact: rsteffen@messbild.de, rsteffen@uni-bonn.de
|
||||
**
|
||||
** QMultiComboBox is free to use unter the terms of the LGPL 2.1 License in
|
||||
** Free and Commercial Products.
|
||||
****************************************************************************/
|
||||
|
||||
#include "QMultiComboBox.h"
|
||||
#include <QApplication>
|
||||
#include <QCoreApplication>
|
||||
#include <QDesktopWidget>
|
||||
|
||||
|
||||
QMultiComboBox::QMultiComboBox(QWidget *widget ) :
|
||||
QComboBox(widget),
|
||||
popheight_(0),
|
||||
screenbound_(50),
|
||||
popframe_(NULL, Qt::Popup)
|
||||
{
|
||||
|
||||
SetDisplayText("Not Set");
|
||||
|
||||
// setup the popup list
|
||||
vlist_.setSelectionMode(QAbstractItemView::MultiSelection);
|
||||
vlist_.setSelectionBehavior(QAbstractItemView::SelectItems);
|
||||
vlist_.clearSelection();
|
||||
popframe_.setLayout(new QVBoxLayout());
|
||||
popframe_.layout()->addWidget(&vlist_);
|
||||
popframe_.layout()->setContentsMargins(0,0,0,0);
|
||||
|
||||
connect(&vlist_, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(scanItemSelect(QListWidgetItem*)));
|
||||
|
||||
}
|
||||
|
||||
|
||||
QMultiComboBox::~QMultiComboBox()
|
||||
{
|
||||
disconnect(&vlist_,0,0,0);
|
||||
}
|
||||
|
||||
|
||||
void QMultiComboBox::SetDisplayText(QString text)
|
||||
{
|
||||
m_DisplayText_ = text;
|
||||
const int textWidth = fontMetrics().width(text);
|
||||
setMinimumWidth(textWidth + 30);
|
||||
updateGeometry();
|
||||
repaint();
|
||||
}
|
||||
|
||||
|
||||
QString QMultiComboBox::GetDisplayText() const
|
||||
{
|
||||
return m_DisplayText_;
|
||||
}
|
||||
|
||||
|
||||
void QMultiComboBox::setPopupHeight(int h)
|
||||
{
|
||||
popheight_ = h;
|
||||
}
|
||||
|
||||
|
||||
void QMultiComboBox::paintEvent(QPaintEvent *e)
|
||||
{
|
||||
QStylePainter painter(this);
|
||||
painter.setPen(palette().color(QPalette::Text));
|
||||
// draw the combobox frame, focusrect and selected etc.
|
||||
QStyleOptionComboBox opt;
|
||||
|
||||
initStyleOption(&opt);
|
||||
opt.currentText = m_DisplayText_;
|
||||
painter.drawComplexControl(QStyle::CC_ComboBox, opt);
|
||||
// draw the icon and text
|
||||
painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
|
||||
}
|
||||
|
||||
|
||||
void QMultiComboBox::showPopup()
|
||||
{
|
||||
|
||||
QRect rec = QRect(geometry());
|
||||
|
||||
//QPoint p = this->mapToGlobal(QPoint(0,rec.height()));
|
||||
//QRect rec2(p , p + QPoint(rec.width(), rec.height()));
|
||||
|
||||
// get the two possible list points and height
|
||||
QRect screen = QApplication::desktop()->screenGeometry(this);
|
||||
QPoint above = this->mapToGlobal(QPoint(0,0));
|
||||
int aboveHeight = above.y() - screen.y();
|
||||
QPoint below = this->mapToGlobal(QPoint(0,rec.height()));
|
||||
int belowHeight = screen.bottom() - below.y();
|
||||
|
||||
// first activate it with height 1px to get all the items initialized
|
||||
QRect rec2;
|
||||
rec2.setTopLeft(below);
|
||||
rec2.setWidth(rec.width());
|
||||
rec.setHeight(1);
|
||||
popframe_.setGeometry(rec2);
|
||||
popframe_.raise();
|
||||
popframe_.show();
|
||||
QCoreApplication::processEvents();
|
||||
|
||||
// determine rect
|
||||
int contheight = vlist_.count()*vlist_.sizeHintForRow(0) + 4; // +4 - should be determined by margins?
|
||||
belowHeight = min(abs(belowHeight)-screenbound_, contheight);
|
||||
aboveHeight = min(abs(aboveHeight)-screenbound_, contheight);
|
||||
|
||||
if (popheight_ > 0) // fixed
|
||||
{
|
||||
rec2.setHeight(popheight_);
|
||||
}
|
||||
else // dynamic
|
||||
{
|
||||
// do we use below or above
|
||||
if (belowHeight==contheight || belowHeight>aboveHeight)
|
||||
{
|
||||
rec2.setTopLeft(below);
|
||||
rec2.setHeight(belowHeight);
|
||||
}
|
||||
else
|
||||
{
|
||||
rec2.setTopLeft(above - QPoint(0,aboveHeight));
|
||||
rec2.setHeight(aboveHeight);
|
||||
}
|
||||
}
|
||||
|
||||
popframe_.setGeometry(rec2);
|
||||
popframe_.raise();
|
||||
popframe_.show();
|
||||
}
|
||||
|
||||
|
||||
void QMultiComboBox::hidePopup()
|
||||
{
|
||||
popframe_.hide();
|
||||
}
|
||||
|
||||
|
||||
void QMultiComboBox::addItem ( const QString & text, const QVariant & userData)
|
||||
{
|
||||
QListWidgetItem* wi = new QListWidgetItem(text);
|
||||
wi->setFlags(wi->flags() | Qt::ItemIsUserCheckable);
|
||||
if (userData.toBool())
|
||||
wi->setCheckState(Qt::Checked);
|
||||
else
|
||||
wi->setCheckState(Qt::Unchecked);
|
||||
vlist_.addItem(wi);
|
||||
}
|
||||
|
||||
|
||||
int QMultiComboBox::count()
|
||||
{
|
||||
return vlist_.count();
|
||||
}
|
||||
|
||||
|
||||
void QMultiComboBox::setCurrentIndex(int index)
|
||||
{
|
||||
cout << __FUNCTION__ << "DONT USE THIS ................" << endl;
|
||||
}
|
||||
|
||||
|
||||
QString QMultiComboBox::currentText()
|
||||
{
|
||||
return vlist_.currentItem()->text();
|
||||
}
|
||||
|
||||
|
||||
QString QMultiComboBox::itemText(int row)
|
||||
{
|
||||
return vlist_.item(row)->text();
|
||||
}
|
||||
|
||||
|
||||
QVariant QMultiComboBox::itemData(int row)
|
||||
{
|
||||
QListWidgetItem* item = vlist_.item(row);
|
||||
if (item->checkState() == Qt::Checked) return QVariant(true);
|
||||
return QVariant(false);
|
||||
}
|
||||
|
||||
|
||||
void QMultiComboBox::scanItemSelect(QListWidgetItem* item)
|
||||
{
|
||||
|
||||
QList<QListWidgetItem*> list = vlist_.selectedItems();
|
||||
for (int i = 0; i < list.count(); i++)
|
||||
{
|
||||
if (item->checkState() == Qt::Checked)
|
||||
{
|
||||
list[i]->setCheckState(Qt::Checked);
|
||||
}
|
||||
else
|
||||
{
|
||||
list[i]->setCheckState(Qt::Unchecked);
|
||||
}
|
||||
list[i]->setSelected(false);
|
||||
}
|
||||
emit itemChanged();
|
||||
}
|
||||
|
||||
void QMultiComboBox::initStyleOption(QStyleOptionComboBox *option) const
|
||||
{
|
||||
//Initializes the state, direction, rect, palette, and fontMetrics member variables based on the specified widget.
|
||||
//This is a convenience function; the member variables can also be initialized manually.
|
||||
option->initFrom(this);
|
||||
|
||||
}
|
||||
|
@ -1,89 +0,0 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2010 Richard Steffen and/or its subsidiary(-ies).
|
||||
** All rights reserved.
|
||||
** Contact: rsteffen@messbild.de, rsteffen@uni-bonn.de
|
||||
**
|
||||
** Observe the License Information
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __MULTIBOXCOMBO_H__
|
||||
#define __MULTIBOXCOMBO_H__
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include <QComboBox>
|
||||
#include <QListWidget>
|
||||
#include <QVBoxLayout>
|
||||
#include <QStylePainter>
|
||||
|
||||
class QMultiComboBox: public QComboBox
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
/// Constructor
|
||||
QMultiComboBox(QWidget *widget = 0);
|
||||
|
||||
virtual ~QMultiComboBox();
|
||||
|
||||
/// the main display text
|
||||
void SetDisplayText(QString text);
|
||||
|
||||
/// get the main display text
|
||||
QString GetDisplayText() const;
|
||||
|
||||
/// add a item to the list
|
||||
void addItem(const QString& text, const QVariant& userData = QVariant());
|
||||
|
||||
/// custom paint
|
||||
virtual void paintEvent(QPaintEvent *e);
|
||||
|
||||
/// set the height of the popup
|
||||
void setPopupHeight(int h);
|
||||
|
||||
/// replace standard QComboBox Popup
|
||||
void showPopup();
|
||||
void hidePopup();
|
||||
|
||||
/// replace neccessary data access
|
||||
int count();
|
||||
void setCurrentIndex(int index);
|
||||
QString currentText();
|
||||
QString itemText(int row);
|
||||
QVariant itemData(int row);
|
||||
|
||||
signals:
|
||||
/// item changed
|
||||
void itemChanged();
|
||||
|
||||
public slots:
|
||||
|
||||
/// react on changes of the item checkbox
|
||||
void scanItemSelect(QListWidgetItem* item);
|
||||
|
||||
/// the init style
|
||||
void initStyleOption(QStyleOptionComboBox *option) const;
|
||||
|
||||
protected:
|
||||
|
||||
/// the height of the popup
|
||||
int popheight_;
|
||||
|
||||
/// lower/upper screen bound
|
||||
int screenbound_;
|
||||
|
||||
/// hold the main display text
|
||||
QString m_DisplayText_;
|
||||
|
||||
/// popup frame
|
||||
QFrame popframe_;
|
||||
|
||||
/// multi selection list in the popup frame
|
||||
QListWidget vlist_;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue