src/gui/itemviews/qitemselectionmodel.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
   290         }
   290         }
   291     }
   291     }
   292 }
   292 }
   293 
   293 
   294 /*!
   294 /*!
       
   295     Returns true if the selection range contains no selectable item
       
   296     \since 4.7
       
   297 */
       
   298 
       
   299 bool QItemSelectionRange::isEmpty() const
       
   300 {
       
   301     if (!isValid() || !model())
       
   302         return true;
       
   303 
       
   304     for (int column = left(); column <= right(); ++column) {
       
   305         for (int row = top(); row <= bottom(); ++row) {
       
   306             QModelIndex index = model()->index(row, column, parent());
       
   307             Qt::ItemFlags flags = model()->flags(index);
       
   308             if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled))
       
   309                 return false;
       
   310         }
       
   311     }
       
   312     return true;
       
   313 }
       
   314 
       
   315 /*!
   295     Returns the list of model index items stored in the selection.
   316     Returns the list of model index items stored in the selection.
   296 */
   317 */
   297 
   318 
   298 QModelIndexList QItemSelectionRange::indexes() const
   319 QModelIndexList QItemSelectionRange::indexes() const
   299 {
   320 {
   522     if (other_right < right) {
   543     if (other_right < right) {
   523         QModelIndex tl = model->index(top, other_right + 1, parent);
   544         QModelIndex tl = model->index(top, other_right + 1, parent);
   524         QModelIndex br = model->index(bottom, right, parent);
   545         QModelIndex br = model->index(bottom, right, parent);
   525         result->append(QItemSelectionRange(tl, br));
   546         result->append(QItemSelectionRange(tl, br));
   526         right = other_right;
   547         right = other_right;
       
   548     }
       
   549 }
       
   550 
       
   551 
       
   552 void QItemSelectionModelPrivate::initModel(QAbstractItemModel *model)
       
   553 {
       
   554     this->model = model;
       
   555     if (model) {
       
   556         Q_Q(QItemSelectionModel);
       
   557         QObject::connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
       
   558                 q, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
       
   559         QObject::connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
       
   560                 q, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
       
   561         QObject::connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
       
   562                 q, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
       
   563         QObject::connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
       
   564                 q, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
       
   565         QObject::connect(model, SIGNAL(layoutAboutToBeChanged()),
       
   566                 q, SLOT(_q_layoutAboutToBeChanged()));
       
   567         QObject::connect(model, SIGNAL(layoutChanged()),
       
   568                 q, SLOT(_q_layoutChanged()));
   527     }
   569     }
   528 }
   570 }
   529 
   571 
   530 /*!
   572 /*!
   531     \internal
   573     \internal
   791         QModelIndex br = colSpans.at(i).bottomRight();
   833         QModelIndex br = colSpans.at(i).bottomRight();
   792         QModelIndex prevTl = tl;
   834         QModelIndex prevTl = tl;
   793         while (++i < colSpans.count()) {
   835         while (++i < colSpans.count()) {
   794             QModelIndex nextTl = colSpans.at(i).topLeft();
   836             QModelIndex nextTl = colSpans.at(i).topLeft();
   795             QModelIndex nextBr = colSpans.at(i).bottomRight();
   837             QModelIndex nextBr = colSpans.at(i).bottomRight();
       
   838 
       
   839             if (nextTl.parent() != tl.parent())
       
   840                 break; // we can't merge selection ranges from different parents
       
   841 
   796             if ((nextTl.column() == prevTl.column()) && (nextBr.column() == br.column())
   842             if ((nextTl.column() == prevTl.column()) && (nextBr.column() == br.column())
   797                 && (nextTl.row() == prevTl.row() + 1) && (nextBr.row() == br.row() + 1)) {
   843                 && (nextTl.row() == prevTl.row() + 1) && (nextBr.row() == br.row() + 1)) {
   798                 br = nextBr;
   844                 br = nextBr;
   799                 prevTl = nextTl;
   845                 prevTl = nextTl;
   800             } else {
   846             } else {
   888     Constructs a selection model that operates on the specified item \a model.
   934     Constructs a selection model that operates on the specified item \a model.
   889 */
   935 */
   890 QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model)
   936 QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model)
   891     : QObject(*new QItemSelectionModelPrivate, model)
   937     : QObject(*new QItemSelectionModelPrivate, model)
   892 {
   938 {
   893     d_func()->model = model;
   939     d_func()->initModel(model);
   894     if (model) {
       
   895         connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
       
   896                 this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
       
   897         connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
       
   898                 this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
       
   899         connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
       
   900                 this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
       
   901         connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
       
   902                 this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
       
   903         connect(model, SIGNAL(layoutAboutToBeChanged()),
       
   904                 this, SLOT(_q_layoutAboutToBeChanged()));
       
   905         connect(model, SIGNAL(layoutChanged()),
       
   906                 this, SLOT(_q_layoutChanged()));
       
   907     }
       
   908 }
   940 }
   909 
   941 
   910 /*!
   942 /*!
   911     Constructs a selection model that operates on the specified item \a model with \a parent.
   943     Constructs a selection model that operates on the specified item \a model with \a parent.
   912 */
   944 */
   913 QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model, QObject *parent)
   945 QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model, QObject *parent)
   914     : QObject(*new QItemSelectionModelPrivate, parent)
   946     : QObject(*new QItemSelectionModelPrivate, parent)
   915 {
   947 {
   916     d_func()->model = model;
   948     d_func()->initModel(model);
   917     if (model) {
       
   918         connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
       
   919                 this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
       
   920         connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
       
   921                 this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
       
   922         connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
       
   923                 this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
       
   924         connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
       
   925                 this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
       
   926         connect(model, SIGNAL(layoutAboutToBeChanged()),
       
   927                 this, SLOT(_q_layoutAboutToBeChanged()));
       
   928         connect(model, SIGNAL(layoutChanged()),
       
   929                 this, SLOT(_q_layoutChanged()));
       
   930     }
       
   931 }
   949 }
   932 
   950 
   933 /*!
   951 /*!
   934     \internal
   952     \internal
   935 */
   953 */
   936 QItemSelectionModel::QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model)
   954 QItemSelectionModel::QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model)
   937     : QObject(dd, model)
   955     : QObject(dd, model)
   938 {
   956 {
   939     d_func()->model = model;
   957     dd.initModel(model);
   940     if (model) {
       
   941         connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
       
   942                 this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
       
   943         connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
       
   944                 this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
       
   945         connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
       
   946                 this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
       
   947         connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
       
   948                 this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
       
   949         connect(model, SIGNAL(layoutAboutToBeChanged()),
       
   950                 this, SLOT(_q_layoutAboutToBeChanged()));
       
   951         connect(model, SIGNAL(layoutChanged()),
       
   952                 this, SLOT(_q_layoutChanged()));
       
   953     }
       
   954 }
   958 }
   955 
   959 
   956 /*!
   960 /*!
   957     Destroys the selection model.
   961     Destroys the selection model.
   958 */
   962 */
   959 QItemSelectionModel::~QItemSelectionModel()
   963 QItemSelectionModel::~QItemSelectionModel()
   960 {
   964 {
   961     Q_D(QItemSelectionModel);
       
   962     if (d->model) {
       
   963         disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
       
   964                 this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
       
   965         disconnect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
       
   966                 this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
       
   967         disconnect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
       
   968                 this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
       
   969         disconnect(d->model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
       
   970                 this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
       
   971         disconnect(d->model, SIGNAL(layoutAboutToBeChanged()),
       
   972                 this, SLOT(_q_layoutAboutToBeChanged()));
       
   973         disconnect(d->model, SIGNAL(layoutChanged()),
       
   974                 this, SLOT(_q_layoutChanged()));
       
   975     }
       
   976 }
   965 }
   977 
   966 
   978 /*!
   967 /*!
   979     Selects the model item \a index using the specified \a command, and emits
   968     Selects the model item \a index using the specified \a command, and emits
   980     selectionChanged().
   969     selectionChanged().