| Home · All Classes · Main Classes · Deprecated |
00001 /*************************************************************************** 00002 ** 00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 00004 ** All rights reserved. 00005 ** Contact: Nokia Corporation (directui@nokia.com) 00006 ** 00007 ** This file is part of libmeegotouch. 00008 ** 00009 ** If you have questions regarding the use of this file, please contact 00010 ** Nokia at directui@nokia.com. 00011 ** 00012 ** This library is free software; you can redistribute it and/or 00013 ** modify it under the terms of the GNU Lesser General Public 00014 ** License version 2.1 as published by the Free Software Foundation 00015 ** and appearing in the file LICENSE.LGPL included in the packaging 00016 ** of this file. 00017 ** 00018 ****************************************************************************/ 00019 00020 #include "mcombobox.h" 00021 #include "mcombobox_p.h" 00022 00023 #include <QStandardItemModel> 00024 #include <QItemSelectionModel> 00025 00026 #include "mwidgetcreator.h" 00027 M_REGISTER_WIDGET(MComboBox) 00028 00029 MComboBoxPrivate::MComboBoxPrivate() 00030 { 00031 } 00032 00033 MComboBoxPrivate::~MComboBoxPrivate() 00034 { 00035 } 00036 00037 void MComboBoxPrivate::init() 00038 { 00039 Q_Q(MComboBox); 00040 q->setItemModel(new QStandardItemModel(0, 1, q)); 00041 } 00042 00043 void MComboBoxPrivate::_q_modelDestroyed() 00044 { 00045 Q_Q(MComboBox); 00046 q->model()->setItemModel(NULL); 00047 } 00048 00049 void MComboBoxPrivate::_q_selectionModelSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) 00050 { 00051 Q_UNUSED(deselected); 00052 Q_Q(MComboBox); 00053 if (!selected.isEmpty()) { 00054 QModelIndex current = selected.indexes().first(); 00055 q->selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate); 00056 emit q->currentIndexChanged(current.row()); 00057 emit q->currentIndexChanged(q->itemText(current.row())); 00058 } else if (q->currentIndex() == -1) { 00059 emit q->currentIndexChanged(-1); 00060 emit q->currentIndexChanged(QString()); 00061 } 00062 } 00063 00064 void MComboBoxPrivate::_q_itemModelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) 00065 { 00066 Q_Q(MComboBox); 00067 int curRow = q->currentIndex(); 00068 if (curRow >= topLeft.row() && curRow <= bottomRight.row()) { 00069 emit q->currentIndexChanged(curRow); 00070 emit q->currentIndexChanged(q->itemText(curRow)); 00071 } 00072 } 00073 00074 void MComboBoxPrivate::_q_itemModelReset() 00075 { 00076 Q_Q(MComboBox); 00077 emit q->currentIndexChanged(-1); 00078 emit q->currentIndexChanged(QString()); 00079 } 00080 00081 void MComboBoxPrivate::_q_itemClicked(const QModelIndex &index) 00082 { 00083 Q_Q(MComboBox); 00084 00085 int row = index.row(); 00086 00087 emit q->activated(row); 00088 emit q->activated(q->itemText(row)); 00089 } 00090 00091 MComboBox::MComboBox(QGraphicsItem *parent) 00092 : MWidgetController(new MComboBoxPrivate(), new MComboBoxModel(), parent) 00093 { 00094 Q_D(MComboBox); 00095 d->init(); 00096 } 00097 00098 MComboBox::MComboBox(MComboBoxPrivate *dd, MComboBoxModel *model, QGraphicsItem *parent) 00099 : MWidgetController(dd, model, parent) 00100 { 00101 Q_D(MComboBox); 00102 d->init(); 00103 } 00104 00105 MComboBox::~MComboBox() 00106 { 00107 disconnect(model()->itemModel(), SIGNAL(destroyed()), this, SLOT(_q_modelDestroyed())); 00108 } 00109 00110 void MComboBox::setItemModel(QAbstractItemModel *itemModel) 00111 { 00112 QAbstractItemModel *oldModel = model()->itemModel(); 00113 if (itemModel == NULL || oldModel == itemModel) 00114 return; 00115 00116 QItemSelectionModel *oldSelModel = model()->selectionModel(); 00117 00118 if (oldSelModel) 00119 oldSelModel->clear(); 00120 00121 if (oldModel) { 00122 disconnect(oldModel, SIGNAL(destroyed()), this, SLOT(_q_modelDestroyed())); 00123 disconnect(oldModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(_q_itemModelDataChanged(QModelIndex, QModelIndex))); 00124 disconnect(oldModel, SIGNAL(modelReset()), this, SLOT(_q_itemModelReset())); 00125 } 00126 00127 QItemSelectionModel *itemSelectionModel = new QItemSelectionModel(itemModel, this); 00128 00129 model()->setItemModel(itemModel); 00130 model()->setSelectionModel(itemSelectionModel); 00131 00132 if (oldSelModel && oldSelModel->QObject::parent() == this) 00133 delete oldSelModel; 00134 if (oldModel && oldModel->QObject::parent() == this) 00135 delete oldModel; 00136 00137 00138 connect(itemModel, SIGNAL(destroyed()), this, SLOT(_q_modelDestroyed())); 00139 connect(itemModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(_q_itemModelDataChanged(QModelIndex, QModelIndex))); 00140 connect(itemModel, SIGNAL(modelReset()), this, SLOT(_q_itemModelReset())); 00141 connect(itemSelectionModel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(_q_selectionModelSelectionChanged(QItemSelection, QItemSelection))); 00142 } 00143 00144 QAbstractItemModel *MComboBox::itemModel() const 00145 { 00146 if (model()->itemModel() == NULL) { 00147 MComboBox *that = const_cast<MComboBox *>(this); 00148 that->setItemModel(new QStandardItemModel(0, 1, that)); 00149 } 00150 return model()->itemModel(); 00151 } 00152 00153 void MComboBox::setSelectionModel(QItemSelectionModel *selectionModel) 00154 { 00155 if (selectionModel == NULL) return; 00156 if (selectionModel->model() != itemModel()) { 00157 qWarning("MComboBox::setSelectionModel() failed: " 00158 "Trying to set a selection model, which works on " 00159 "a different model than the view."); 00160 return; 00161 } 00162 00163 model()->setSelectionModel(selectionModel); 00164 } 00165 00166 QItemSelectionModel *MComboBox::selectionModel() const 00167 { 00168 return model()->selectionModel(); 00169 } 00170 00171 QString MComboBox::iconID() const 00172 { 00173 return model()->iconID(); 00174 } 00175 00176 bool MComboBox::isIconVisible() const 00177 { 00178 return model()->iconVisible(); 00179 } 00180 00181 QString MComboBox::title() const 00182 { 00183 return model()->title(); 00184 } 00185 00186 bool MComboBox::isProgressIndicatorVisible() const 00187 { 00188 return model()->progressIndicatorVisible(); 00189 } 00190 00191 void MComboBox::addItem(const QString &text) 00192 { 00193 insertItem(count(), text); 00194 } 00195 00196 void MComboBox::addItem(const QString &iconID, const QString &text) 00197 { 00198 insertItem(count(), iconID, text); 00199 } 00200 00201 void MComboBox::addItems(const QStringList &texts) 00202 { 00203 insertItems(count(), texts); 00204 } 00205 00206 int MComboBox::count() const 00207 { 00208 return model()->itemModel()->rowCount(); 00209 } 00210 00211 int MComboBox::currentIndex() const 00212 { 00213 QItemSelectionModel *itemSelectionModel = model()->selectionModel(); 00214 00215 if (itemSelectionModel == NULL || !itemSelectionModel->hasSelection()) 00216 return -1; 00217 00218 return itemSelectionModel->currentIndex().row(); 00219 } 00220 00221 QString MComboBox::currentText() const 00222 { 00223 int index = currentIndex(); 00224 if (index != -1) 00225 return itemText(index); 00226 else 00227 return QString(); 00228 } 00229 00230 QString MComboBox::itemText(int index) const 00231 { 00232 QAbstractItemModel *itemModel = model()->itemModel(); 00233 00234 QModelIndex mi = itemModel->index(index, 0); 00235 if (mi.isValid()) 00236 return itemModel->data(mi, Qt::DisplayRole).toString(); 00237 else 00238 return QString(); 00239 } 00240 00241 QString MComboBox::itemIconID(int index) const 00242 { 00243 QAbstractItemModel *itemModel = model()->itemModel(); 00244 00245 QModelIndex mi = itemModel->index(index, 0); 00246 if (mi.isValid()) 00247 return itemModel->data(mi, Qt::DecorationRole).toString(); 00248 else 00249 return QString(); 00250 } 00251 00252 void MComboBox::insertItem(int index, const QString &text) 00253 { 00254 insertItem(index, QString(), text); 00255 } 00256 00257 void MComboBox::insertItem(int index, const QString &iconID, const QString &text) 00258 { 00259 int itemCount = count(); 00260 index = qBound(0, index, itemCount); 00261 00262 // For the common case where we are using the built in QStandardItemModel 00263 // construct a QStandardItem, reducing the number of expensive signals from the model 00264 QAbstractItemModel *itemModel = model()->itemModel(); 00265 00266 if (QStandardItemModel *m = qobject_cast<QStandardItemModel *>(itemModel)) { 00267 QStandardItem *item = new QStandardItem(text); 00268 if (!iconID.isNull()) item->setData(iconID, Qt::DecorationRole); 00269 m->insertRow(index, item); 00270 } else { 00271 if (itemModel->insertRows(index, 1)) { 00272 QModelIndex item = itemModel->index(index, 0); 00273 itemModel->setData(item, text, Qt::DisplayRole); 00274 00275 if (!iconID.isEmpty()) 00276 itemModel->setData(item, iconID, Qt::DecorationRole); 00277 } 00278 } 00279 } 00280 00281 void MComboBox::insertItems(int index, const QStringList &list) 00282 { 00283 if (list.isEmpty()) 00284 return; 00285 00286 index = qBound(0, index, count()); 00287 int insertCount = list.count(); 00288 00289 // For the common case where we are using the built in QStandardItemModel 00290 // construct a QStandardItem, reducing the number of expensive signals from the model 00291 QAbstractItemModel *itemModel = model()->itemModel(); 00292 00293 if (QStandardItemModel *m = qobject_cast<QStandardItemModel *>(itemModel)) { 00294 00295 QList<QStandardItem *> items; 00296 QStandardItem *hiddenRoot = m->invisibleRootItem(); 00297 for (int i = 0; i < insertCount; ++i) 00298 items.append(new QStandardItem(list.at(i))); 00299 hiddenRoot->insertRows(index, items); 00300 00301 } else { 00302 if (itemModel->insertRows(index, insertCount)) { 00303 QModelIndex item; 00304 for (int i = 0; i < insertCount; ++i) { 00305 item = itemModel->index(i + index, 0); 00306 itemModel->setData(item, list.at(i), Qt::DisplayRole); 00307 } 00308 } 00309 } 00310 } 00311 00312 void MComboBox::removeItem(int index) 00313 { 00314 if (index >= 0 && index < count()) { 00315 QAbstractItemModel *itemModel = model()->itemModel(); 00316 itemModel->removeRows(index, 1); 00317 } 00318 } 00319 00320 void MComboBox::setItemText(int index, const QString &text) 00321 { 00322 QAbstractItemModel *itemModel = model()->itemModel(); 00323 QModelIndex item = itemModel->index(index, 0); 00324 if (item.isValid()) { 00325 itemModel->setData(item, text, Qt::DisplayRole); 00326 } 00327 } 00328 00329 void MComboBox::setItemIconID(int index, const QString &iconID) 00330 { 00331 QAbstractItemModel *itemModel = model()->itemModel(); 00332 QModelIndex item = itemModel->index(index, 0); 00333 if (item.isValid()) { 00334 itemModel->setData(item, iconID, Qt::DecorationRole); 00335 } 00336 } 00337 00338 void MComboBox::clear() 00339 { 00340 QAbstractItemModel *itemModel = model()->itemModel(); 00341 itemModel->removeRows(0, itemModel->rowCount()); 00342 } 00343 00344 void MComboBox::setIconID(const QString &id) 00345 { 00346 model()->setIconID(id); 00347 } 00348 00349 void MComboBox::setIconVisible(bool visible) 00350 { 00351 model()->setIconVisible(visible); 00352 } 00353 00354 void MComboBox::setTitle(const QString &title) 00355 { 00356 model()->setTitle(title); 00357 } 00358 00359 void MComboBox::setProgressIndicatorVisible(bool enable) 00360 { 00361 model()->setProgressIndicatorVisible(enable); 00362 } 00363 00364 void MComboBox::setCurrentIndex(int index) 00365 { 00366 if (index == currentIndex()) return; 00367 00368 QItemSelectionModel *itemSelectionModel = model()->selectionModel(); 00369 if (itemSelectionModel == 0) return; 00370 00371 QModelIndex item = model()->itemModel()->index(index, 0); 00372 00373 if (item.isValid()) 00374 itemSelectionModel->setCurrentIndex(item, QItemSelectionModel::ClearAndSelect); 00375 else 00376 itemSelectionModel->clear(); 00377 } 00378 00379 void MComboBox::click() 00380 { 00381 emit clicked(); 00382 } 00383 00384 void MComboBox::dismiss() 00385 { 00386 emit dismissed(); 00387 } 00388 00389 #include "moc_mcombobox.cpp"
| Copyright © 2010 Nokia Corporation | Generated on Thu Nov 4 2010 18:14:20 (PDT) Doxygen 1.7.1 |
MeeGo Touch |