Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "qxtcheckcombobox.h"
00026 #include "qxtcheckcombobox_p.h"
00027 #include <QLineEdit>
00028 #include <QKeyEvent>
00029
00030 QxtCheckComboBoxPrivate::QxtCheckComboBoxPrivate() : containerMousePress(false)
00031 {
00032 separator = QLatin1String(",");
00033 }
00034
00035 bool QxtCheckComboBoxPrivate::eventFilter(QObject* receiver, QEvent* event)
00036 {
00037 switch (event->type())
00038 {
00039 case QEvent::KeyPress:
00040 case QEvent::KeyRelease:
00041 {
00042 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
00043 if (receiver == &qxt_p() && (keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down))
00044 {
00045 qxt_p().showPopup();
00046 return true;
00047 }
00048 else if (keyEvent->key() == Qt::Key_Enter ||
00049 keyEvent->key() == Qt::Key_Return ||
00050 keyEvent->key() == Qt::Key_Escape)
00051 {
00052
00053 qxt_p().QComboBox::hidePopup();
00054 if (keyEvent->key() != Qt::Key_Escape)
00055 return true;
00056 }
00057 }
00058 case QEvent::MouseButtonPress:
00059 containerMousePress = (receiver == qxt_p().view()->window());
00060 break;
00061 case QEvent::MouseButtonRelease:
00062 containerMousePress = false;;
00063 break;
00064 default:
00065 break;
00066 }
00067 return false;
00068 }
00069
00070 void QxtCheckComboBoxPrivate::updateCheckedItems()
00071 {
00072 QStringList items = qxt_p().checkedItems();
00073 if (items.isEmpty())
00074 qxt_p().setEditText(defaultText);
00075 else
00076 qxt_p().setEditText(items.join(separator));
00077
00078
00079
00080 emit qxt_p().checkedItemsChanged(items);
00081 }
00082
00083 void QxtCheckComboBoxPrivate::toggleCheckState(int index)
00084 {
00085 QVariant value = qxt_p().itemData(index, Qt::CheckStateRole);
00086 if (value.isValid())
00087 {
00088 Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
00089 qxt_p().setItemData(index, (state == Qt::Unchecked ? Qt::Checked : Qt::Unchecked), Qt::CheckStateRole);
00090 }
00091 }
00092
00093 QxtCheckComboModel::QxtCheckComboModel(QObject* parent)
00094 : QStandardItemModel(0, 1, parent)
00095 {
00096 }
00097
00098 Qt::ItemFlags QxtCheckComboModel::flags(const QModelIndex& index) const
00099 {
00100 return QStandardItemModel::flags(index) | Qt::ItemIsUserCheckable;
00101 }
00102
00103 QVariant QxtCheckComboModel::data(const QModelIndex& index, int role) const
00104 {
00105 QVariant value = QStandardItemModel::data(index, role);
00106 if (index.isValid() && role == Qt::CheckStateRole && !value.isValid())
00107 value = Qt::Unchecked;
00108 return value;
00109 }
00110
00111 bool QxtCheckComboModel::setData(const QModelIndex& index, const QVariant& value, int role)
00112 {
00113 bool ok = QStandardItemModel::setData(index, value, role);
00114 if (ok && role == Qt::CheckStateRole)
00115 {
00116 emit dataChanged(index, index);
00117 emit checkStateChanged();
00118 }
00119 return ok;
00120 }
00121
00162 QxtCheckComboBox::QxtCheckComboBox(QWidget* parent) : QComboBox(parent)
00163 {
00164 QXT_INIT_PRIVATE(QxtCheckComboBox);
00165 setModel(new QxtCheckComboModel(this));
00166 connect(this, SIGNAL(activated(int)), &qxt_d(), SLOT(toggleCheckState(int)));
00167 connect(model(), SIGNAL(checkStateChanged()), &qxt_d(), SLOT(updateCheckedItems()));
00168 connect(model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), &qxt_d(), SLOT(updateCheckedItems()));
00169 connect(model(), SIGNAL(rowsRemoved(const QModelIndex &, int, int)), &qxt_d(), SLOT(updateCheckedItems()));
00170
00171
00172 QLineEdit* lineEdit = new QLineEdit(this);
00173 lineEdit->setReadOnly(true);
00174 setLineEdit(lineEdit);
00175 setInsertPolicy(QComboBox::NoInsert);
00176
00177 view()->installEventFilter(&qxt_d());
00178 view()->window()->installEventFilter(&qxt_d());
00179 view()->viewport()->installEventFilter(&qxt_d());
00180 this->installEventFilter(&qxt_d());
00181 }
00182
00186 QxtCheckComboBox::~QxtCheckComboBox()
00187 {
00188 }
00189
00193 void QxtCheckComboBox::hidePopup()
00194 {
00195 if (qxt_d().containerMousePress)
00196 QComboBox::hidePopup();
00197 }
00198
00202 Qt::CheckState QxtCheckComboBox::itemCheckState(int index) const
00203 {
00204 return static_cast<Qt::CheckState>(itemData(index, Qt::CheckStateRole).toInt());
00205 }
00206
00210 void QxtCheckComboBox::setItemCheckState(int index, Qt::CheckState state)
00211 {
00212 setItemData(index, state, Qt::CheckStateRole);
00213 }
00214
00219 QStringList QxtCheckComboBox::checkedItems() const
00220 {
00221 QStringList items;
00222 if (model())
00223 {
00224 QModelIndex index = model()->index(0, modelColumn(), rootModelIndex());
00225 QModelIndexList indexes = model()->match(index, Qt::CheckStateRole, Qt::Checked, -1, Qt::MatchExactly);
00226 foreach(const QModelIndex& index, indexes)
00227 items += index.data().toString();
00228 }
00229 return items;
00230 }
00231
00232 void QxtCheckComboBox::setCheckedItems(const QStringList& items)
00233 {
00234
00235
00236 foreach(const QString& text, items)
00237 {
00238 const int index = findText(text);
00239 setItemCheckState(index, index != -1 ? Qt::Checked : Qt::Unchecked);
00240 }
00241 }
00242
00250 QString QxtCheckComboBox::defaultText() const
00251 {
00252 return qxt_d().defaultText;
00253 }
00254
00255 void QxtCheckComboBox::setDefaultText(const QString& text)
00256 {
00257 if (qxt_d().defaultText != text)
00258 {
00259 qxt_d().defaultText = text;
00260 qxt_d().updateCheckedItems();
00261 }
00262 }
00263
00271 QString QxtCheckComboBox::separator() const
00272 {
00273 return qxt_d().separator;
00274 }
00275
00276 void QxtCheckComboBox::setSeparator(const QString& separator)
00277 {
00278 if (qxt_d().separator != separator)
00279 {
00280 qxt_d().separator = separator;
00281 qxt_d().updateCheckedItems();
00282 }
00283 }