src/gui/itemviews/qitemselectionmodel.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
--- a/src/gui/itemviews/qitemselectionmodel.cpp	Wed Jun 23 19:07:03 2010 +0300
+++ b/src/gui/itemviews/qitemselectionmodel.cpp	Tue Jul 06 15:10:48 2010 +0300
@@ -292,6 +292,27 @@
 }
 
 /*!
+    Returns true if the selection range contains no selectable item
+    \since 4.7
+*/
+
+bool QItemSelectionRange::isEmpty() const
+{
+    if (!isValid() || !model())
+        return true;
+
+    for (int column = left(); column <= right(); ++column) {
+        for (int row = top(); row <= bottom(); ++row) {
+            QModelIndex index = model()->index(row, column, parent());
+            Qt::ItemFlags flags = model()->flags(index);
+            if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled))
+                return false;
+        }
+    }
+    return true;
+}
+
+/*!
     Returns the list of model index items stored in the selection.
 */
 
@@ -527,6 +548,27 @@
     }
 }
 
+
+void QItemSelectionModelPrivate::initModel(QAbstractItemModel *model)
+{
+    this->model = model;
+    if (model) {
+        Q_Q(QItemSelectionModel);
+        QObject::connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
+                q, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
+        QObject::connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
+                q, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
+        QObject::connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
+                q, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
+        QObject::connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
+                q, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
+        QObject::connect(model, SIGNAL(layoutAboutToBeChanged()),
+                q, SLOT(_q_layoutAboutToBeChanged()));
+        QObject::connect(model, SIGNAL(layoutChanged()),
+                q, SLOT(_q_layoutChanged()));
+    }
+}
+
 /*!
     \internal
 
@@ -793,6 +835,10 @@
         while (++i < colSpans.count()) {
             QModelIndex nextTl = colSpans.at(i).topLeft();
             QModelIndex nextBr = colSpans.at(i).bottomRight();
+
+            if (nextTl.parent() != tl.parent())
+                break; // we can't merge selection ranges from different parents
+
             if ((nextTl.column() == prevTl.column()) && (nextBr.column() == br.column())
                 && (nextTl.row() == prevTl.row() + 1) && (nextBr.row() == br.row() + 1)) {
                 br = nextBr;
@@ -890,21 +936,7 @@
 QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model)
     : QObject(*new QItemSelectionModelPrivate, model)
 {
-    d_func()->model = model;
-    if (model) {
-        connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
-                this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
-        connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
-                this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
-        connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
-                this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
-        connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
-                this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
-        connect(model, SIGNAL(layoutAboutToBeChanged()),
-                this, SLOT(_q_layoutAboutToBeChanged()));
-        connect(model, SIGNAL(layoutChanged()),
-                this, SLOT(_q_layoutChanged()));
-    }
+    d_func()->initModel(model);
 }
 
 /*!
@@ -913,21 +945,7 @@
 QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model, QObject *parent)
     : QObject(*new QItemSelectionModelPrivate, parent)
 {
-    d_func()->model = model;
-    if (model) {
-        connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
-                this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
-        connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
-                this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
-        connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
-                this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
-        connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
-                this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
-        connect(model, SIGNAL(layoutAboutToBeChanged()),
-                this, SLOT(_q_layoutAboutToBeChanged()));
-        connect(model, SIGNAL(layoutChanged()),
-                this, SLOT(_q_layoutChanged()));
-    }
+    d_func()->initModel(model);
 }
 
 /*!
@@ -936,21 +954,7 @@
 QItemSelectionModel::QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model)
     : QObject(dd, model)
 {
-    d_func()->model = model;
-    if (model) {
-        connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
-                this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
-        connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
-                this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
-        connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
-                this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
-        connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
-                this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
-        connect(model, SIGNAL(layoutAboutToBeChanged()),
-                this, SLOT(_q_layoutAboutToBeChanged()));
-        connect(model, SIGNAL(layoutChanged()),
-                this, SLOT(_q_layoutChanged()));
-    }
+    dd.initModel(model);
 }
 
 /*!
@@ -958,21 +962,6 @@
 */
 QItemSelectionModel::~QItemSelectionModel()
 {
-    Q_D(QItemSelectionModel);
-    if (d->model) {
-        disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
-                this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
-        disconnect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
-                this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
-        disconnect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
-                this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
-        disconnect(d->model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
-                this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
-        disconnect(d->model, SIGNAL(layoutAboutToBeChanged()),
-                this, SLOT(_q_layoutAboutToBeChanged()));
-        disconnect(d->model, SIGNAL(layoutChanged()),
-                this, SLOT(_q_layoutChanged()));
-    }
 }
 
 /*!