Changeset 846 for trunk/src/gui/util/qcompleter.cpp
- Timestamp:
- May 5, 2011, 5:36:53 AM (14 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/vendor/nokia/qt/4.7.2 (added) merged: 845 /branches/vendor/nokia/qt/current merged: 844 /branches/vendor/nokia/qt/4.6.3 removed
- Property svn:mergeinfo changed
-
trunk/src/gui/util/qcompleter.cpp
r769 r846 1 1 /**************************************************************************** 2 2 ** 3 ** Copyright (C) 201 0Nokia Corporation and/or its subsidiary(-ies).3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). 4 4 ** All rights reserved. 5 5 ** Contact: Nokia Corporation (qt-info@nokia.com) … … 63 63 \snippet doc/src/snippets/code/src_gui_util_qcompleter.cpp 0 64 64 65 A Q DirModel can be used to provide auto completion of file names.65 A QFileSystemModel can be used to provide auto completion of file names. 66 66 For example: 67 67 … … 121 121 122 122 Let's take the example of a user typing in a file system path. 123 The model is a (hierarchical) Q DirModel. The completion123 The model is a (hierarchical) QFileSystemModel. The completion 124 124 occurs for every element in the path. For example, if the current 125 125 text is \c C:\Wind, QCompleter might suggest \c Windows to … … 131 131 For \c C:\Windows\Sy, it needs to be split as "C:", "Windows" and "Sy". 132 132 The default implementation of splitPath(), splits the completionPrefix 133 using QDir::separator() if the model is a Q DirModel.133 using QDir::separator() if the model is a QFileSystemModel. 134 134 135 135 To provide completions, QCompleter needs to know the path from an index. 136 136 This is provided by pathFromIndex(). The default implementation of 137 137 pathFromIndex(), returns the data for the \l{Qt::EditRole}{edit role} 138 for list models and the absolute file path if the mode is a Q DirModel.138 for list models and the absolute file path if the mode is a QFileSystemModel. 139 139 140 140 \sa QAbstractItemModel, QLineEdit, QComboBox, {Completer Example} … … 148 148 #include "QtGui/qstringlistmodel.h" 149 149 #include "QtGui/qdirmodel.h" 150 #include "QtGui/qfilesystemmodel.h" 150 151 #include "QtGui/qheaderview.h" 151 152 #include "QtGui/qlistview.h" … … 154 155 #include "QtGui/qheaderview.h" 155 156 #include "QtGui/qdesktopwidget.h" 157 #include "QtGui/qlineedit.h" 156 158 157 159 QT_BEGIN_NAMESPACE … … 471 473 if (curParts.count() <= 1 || c->proxy->showAll || !source) 472 474 return QMatchData(); 473 bool dirModel = false; 475 bool isDirModel = false; 476 bool isFsModel = false; 474 477 #ifndef QT_NO_DIRMODEL 475 dirModel = (qobject_cast<QDirModel *>(source) != 0); 478 isDirModel = (qobject_cast<QDirModel *>(source) != 0); 479 #endif 480 #ifndef QT_NO_FILESYSTEMMODEL 481 isFsModel = (qobject_cast<QFileSystemModel *>(source) != 0); 476 482 #endif 477 483 QVector<int> v; … … 483 489 if (str.startsWith(c->prefix, c->cs) 484 490 #if (!defined(Q_OS_WIN) || defined(Q_OS_WINCE)) && !defined(Q_OS_SYMBIAN) && !defined(Q_OS_OS2) 485 && ( !dirModel|| QDir::toNativeSeparators(str) != QDir::separator())491 && ((!isFsModel && !isDirModel) || QDir::toNativeSeparators(str) != QDir::separator()) 486 492 #endif 487 493 ) … … 777 783 QCompleterPrivate::QCompleterPrivate() 778 784 : widget(0), proxy(0), popup(0), cs(Qt::CaseSensitive), role(Qt::EditRole), column(0), 779 maxVisibleItems(7), sorting(QCompleter::UnsortedModel), wrap(true), eatFocusOut(true) 785 maxVisibleItems(7), sorting(QCompleter::UnsortedModel), wrap(true), eatFocusOut(true), 786 hiddenBecauseNoMatch(false) 780 787 { 781 788 } … … 844 851 } 845 852 #endif 853 #ifndef QT_NO_FILESYSTEMMODEL 854 // add a trailing separator in inline 855 if (mode == QCompleter::InlineCompletion) { 856 if (qobject_cast<QFileSystemModel *>(proxy->sourceModel()) && QFileInfo(completion).isDir()) 857 completion += QDir::separator(); 858 } 859 #endif 846 860 } 847 861 … … 867 881 Qt::LayoutDirection dir = widget->layoutDirection(); 868 882 QPoint pos; 869 int r w, rh, w;883 int rh, w; 870 884 int h = (popup->sizeHintForRow(0) * qMin(maxVisibleItems, popup->model()->rowCount()) + 3) + 3; 871 885 QScrollBar *hsb = popup->horizontalScrollBar(); … … 875 889 if (rect.isValid()) { 876 890 rh = rect.height(); 877 w = r w = rect.width();891 w = rect.width(); 878 892 pos = widget->mapToGlobal(dir == Qt::RightToLeft ? rect.bottomRight() : rect.bottomLeft()); 879 893 } else { 880 894 rh = widget->height(); 881 rw = widget->width();882 895 pos = widget->mapToGlobal(QPoint(0, widget->height() - 2)); 883 896 w = widget->width(); 884 897 } 885 898 886 if ((pos.x() + rw) > (screen.x() + screen.width())) 899 if (w > screen.width()) 900 w = screen.width(); 901 if ((pos.x() + w) > (screen.x() + screen.width())) 887 902 pos.setX(screen.x() + screen.width() - w); 888 903 if (pos.x() < screen.x()) 889 904 pos.setX(screen.x()); 890 if (((pos.y() + rh) > (screen.y() + screen.height())) && ((pos.y() - h - rh) >= 0)) 891 pos.setY(pos.y() - qMax(h, popup->minimumHeight()) - rh + 2); 905 906 int top = pos.y() - rh - screen.top() + 2; 907 int bottom = screen.bottom() - pos.y(); 908 h = qMax(h, popup->minimumHeight()); 909 if (h > bottom) { 910 h = qMin(qMax(top, bottom), h); 911 912 if (top > bottom) 913 pos.setY(pos.y() - h - rh + 2); 914 } 892 915 893 916 popup->setGeometry(pos.x(), pos.y(), w, h); … … 895 918 if (!popup->isVisible()) 896 919 popup->show(); 920 } 921 922 void QCompleterPrivate::_q_fileSystemModelDirectoryLoaded(const QString &path) 923 { 924 Q_Q(QCompleter); 925 // Slot called when QFileSystemModel has finished loading. 926 // If we hide the popup because there was no match because the model was not loaded yet, 927 // we re-start the completion when we get the results 928 if (hiddenBecauseNoMatch 929 && prefix.startsWith(path) && prefix != (path + '/') 930 && widget) { 931 q->complete(); 932 } 897 933 } 898 934 … … 977 1013 and it has the QCompleter as its parent, it is deleted. 978 1014 979 For convenience, if \a model is a Q DirModel, QCompleter switches its1015 For convenience, if \a model is a QFileSystemModel, QCompleter switches its 980 1016 caseSensitivity to Qt::CaseInsensitive on Windows and Qt::CaseSensitive 981 1017 on other platforms. … … 1001 1037 } 1002 1038 #endif // QT_NO_DIRMODEL 1039 #ifndef QT_NO_FILESYSTEMMODEL 1040 QFileSystemModel *fsModel = qobject_cast<QFileSystemModel *>(model); 1041 if (fsModel) { 1042 #if (defined(Q_OS_WIN) && !defined(Q_OS_WINCE)) || defined(Q_OS_SYMBIAN) 1043 setCaseSensitivity(Qt::CaseInsensitive); 1044 #else 1045 setCaseSensitivity(Qt::CaseSensitive); 1046 #endif 1047 setCompletionRole(QFileSystemModel::FileNameRole); 1048 connect(fsModel, SIGNAL(directoryLoaded(QString)), this, SLOT(_q_fileSystemModelDirectoryLoaded(QString))); 1049 } 1050 #endif // QT_NO_FILESYSTEMMODEL 1003 1051 } 1004 1052 … … 1084 1132 if (popup->model() != d->proxy) 1085 1133 popup->setModel(d->proxy); 1086 #if def Q_OS_MAC1134 #if defined(Q_OS_MAC) && !defined(QT_MAC_USE_COCOA) 1087 1135 popup->show(); 1088 1136 #else … … 1156 1204 1157 1205 if (d->eatFocusOut && o == d->widget && e->type() == QEvent::FocusOut) { 1206 d->hiddenBecauseNoMatch = false; 1158 1207 if (d->popup && d->popup->isVisible()) 1159 1208 return true; … … 1334 1383 Q_D(QCompleter); 1335 1384 QModelIndex idx = d->proxy->currentIndex(false); 1385 d->hiddenBecauseNoMatch = false; 1336 1386 if (d->mode == QCompleter::InlineCompletion) { 1337 1387 if (idx.isValid()) … … 1345 1395 if (d->popup) 1346 1396 d->popup->hide(); // no suggestion, hide 1397 d->hiddenBecauseNoMatch = true; 1347 1398 return; 1348 1399 } … … 1632 1683 The default implementation returns the \l{Qt::EditRole}{edit role} of the 1633 1684 item for list models. It returns the absolute file path if the model is a 1634 Q DirModel.1685 QFileSystemModel. 1635 1686 1636 1687 \sa splitPath() 1637 1688 */ 1689 1638 1690 QString QCompleter::pathFromIndex(const QModelIndex& index) const 1639 1691 { … … 1645 1697 if (!sourceModel) 1646 1698 return QString(); 1699 bool isDirModel = false; 1700 bool isFsModel = false; 1647 1701 #ifndef QT_NO_DIRMODEL 1648 QDirModel *dirModel = qobject_cast<QDirModel *>(sourceModel); 1649 if (!dirModel) 1650 #endif 1702 isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0; 1703 #endif 1704 #ifndef QT_NO_FILESYSTEMMODEL 1705 isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) != 0; 1706 #endif 1707 if (!isDirModel && !isFsModel) 1651 1708 return sourceModel->data(index, d->role).toString(); 1652 1709 … … 1654 1711 QStringList list; 1655 1712 do { 1656 QString t = sourceModel->data(idx, Qt::EditRole).toString(); 1713 QString t; 1714 if (isDirModel) 1715 t = sourceModel->data(idx, Qt::EditRole).toString(); 1716 #ifndef QT_NO_FILESYSTEMMODEL 1717 else 1718 t = sourceModel->data(idx, QFileSystemModel::FileNameRole).toString(); 1719 #endif 1657 1720 list.prepend(t); 1658 1721 QModelIndex parent = idx.parent(); … … 1674 1737 1675 1738 The default implementation of splitPath() splits a file system path based on 1676 QDir::separator() when the sourceModel() is a Q DirModel.1739 QDir::separator() when the sourceModel() is a QFileSystemModel. 1677 1740 1678 1741 When used with list models, the first item in the returned list is used for … … 1684 1747 { 1685 1748 bool isDirModel = false; 1749 bool isFsModel = false; 1686 1750 #ifndef QT_NO_DIRMODEL 1687 1751 Q_D(const QCompleter); 1688 1752 isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0; 1689 1753 #endif 1690 1691 if (!isDirModel || path.isEmpty()) 1754 #ifndef QT_NO_FILESYSTEMMODEL 1755 #ifdef QT_NO_DIRMODEL 1756 Q_D(const QCompleter); 1757 #endif 1758 isFsModel = qobject_cast<QFileSystemModel *>(d->proxy->sourceModel()) != 0; 1759 #endif 1760 1761 if ((!isDirModel && !isFsModel) || path.isEmpty()) 1692 1762 return QStringList(completionPrefix()); 1693 1763
Note:
See TracChangeset
for help on using the changeset viewer.