Ignore:
Timestamp:
May 5, 2011, 5:36:53 AM (14 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/gui/util/qcompleter.cpp

    r769 r846  
    11/****************************************************************************
    22**
    3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
    44** All rights reserved.
    55** Contact: Nokia Corporation (qt-info@nokia.com)
     
    6363    \snippet doc/src/snippets/code/src_gui_util_qcompleter.cpp 0
    6464
    65     A QDirModel can be used to provide auto completion of file names.
     65    A QFileSystemModel can be used to provide auto completion of file names.
    6666    For example:
    6767
     
    121121
    122122    Let's take the example of a user typing in a file system path.
    123     The model is a (hierarchical) QDirModel. The completion
     123    The model is a (hierarchical) QFileSystemModel. The completion
    124124    occurs for every element in the path. For example, if the current
    125125    text is \c C:\Wind, QCompleter might suggest \c Windows to
     
    131131    For \c C:\Windows\Sy, it needs to be split as "C:", "Windows" and "Sy".
    132132    The default implementation of splitPath(), splits the completionPrefix
    133     using QDir::separator() if the model is a QDirModel.
     133    using QDir::separator() if the model is a QFileSystemModel.
    134134
    135135    To provide completions, QCompleter needs to know the path from an index.
    136136    This is provided by pathFromIndex(). The default implementation of
    137137    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 QDirModel.
     138    for list models and the absolute file path if the mode is a QFileSystemModel.
    139139
    140140    \sa QAbstractItemModel, QLineEdit, QComboBox, {Completer Example}
     
    148148#include "QtGui/qstringlistmodel.h"
    149149#include "QtGui/qdirmodel.h"
     150#include "QtGui/qfilesystemmodel.h"
    150151#include "QtGui/qheaderview.h"
    151152#include "QtGui/qlistview.h"
     
    154155#include "QtGui/qheaderview.h"
    155156#include "QtGui/qdesktopwidget.h"
     157#include "QtGui/qlineedit.h"
    156158
    157159QT_BEGIN_NAMESPACE
     
    471473    if (curParts.count() <= 1 || c->proxy->showAll || !source)
    472474        return QMatchData();
    473     bool dirModel = false;
     475    bool isDirModel = false;
     476    bool isFsModel = false;
    474477#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);
    476482#endif
    477483    QVector<int> v;
     
    483489        if (str.startsWith(c->prefix, c->cs)
    484490#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())
    486492#endif
    487493            )
     
    777783QCompleterPrivate::QCompleterPrivate()
    778784: 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)
    780787{
    781788}
     
    844851        }
    845852#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
    846860    }
    847861
     
    867881    Qt::LayoutDirection dir = widget->layoutDirection();
    868882    QPoint pos;
    869     int rw, rh, w;
     883    int rh, w;
    870884    int h = (popup->sizeHintForRow(0) * qMin(maxVisibleItems, popup->model()->rowCount()) + 3) + 3;
    871885    QScrollBar *hsb = popup->horizontalScrollBar();
     
    875889    if (rect.isValid()) {
    876890        rh = rect.height();
    877         w = rw = rect.width();
     891        w = rect.width();
    878892        pos = widget->mapToGlobal(dir == Qt::RightToLeft ? rect.bottomRight() : rect.bottomLeft());
    879893    } else {
    880894        rh = widget->height();
    881         rw = widget->width();
    882895        pos = widget->mapToGlobal(QPoint(0, widget->height() - 2));
    883896        w = widget->width();
    884897    }
    885898
    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()))
    887902        pos.setX(screen.x() + screen.width() - w);
    888903    if (pos.x() < screen.x())
    889904        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    }
    892915
    893916    popup->setGeometry(pos.x(), pos.y(), w, h);
     
    895918    if (!popup->isVisible())
    896919        popup->show();
     920}
     921
     922void 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    }
    897933}
    898934
     
    9771013    and it has the QCompleter as its parent, it is deleted.
    9781014
    979     For convenience, if \a model is a QDirModel, QCompleter switches its
     1015    For convenience, if \a model is a QFileSystemModel, QCompleter switches its
    9801016    caseSensitivity to Qt::CaseInsensitive on Windows and Qt::CaseSensitive
    9811017    on other platforms.
     
    10011037    }
    10021038#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
    10031051}
    10041052
     
    10841132    if (popup->model() != d->proxy)
    10851133        popup->setModel(d->proxy);
    1086 #ifdef Q_OS_MAC
     1134#if defined(Q_OS_MAC) && !defined(QT_MAC_USE_COCOA)
    10871135     popup->show();
    10881136#else
     
    11561204
    11571205    if (d->eatFocusOut && o == d->widget && e->type() == QEvent::FocusOut) {
     1206        d->hiddenBecauseNoMatch = false;
    11581207        if (d->popup && d->popup->isVisible())
    11591208            return true;
     
    13341383    Q_D(QCompleter);
    13351384    QModelIndex idx = d->proxy->currentIndex(false);
     1385    d->hiddenBecauseNoMatch = false;
    13361386    if (d->mode == QCompleter::InlineCompletion) {
    13371387        if (idx.isValid())
     
    13451395        if (d->popup)
    13461396            d->popup->hide(); // no suggestion, hide
     1397        d->hiddenBecauseNoMatch = true;
    13471398        return;
    13481399    }
     
    16321683    The default implementation returns the \l{Qt::EditRole}{edit role} of the
    16331684    item for list models. It returns the absolute file path if the model is a
    1634     QDirModel.
     1685    QFileSystemModel.
    16351686
    16361687    \sa splitPath()
    16371688*/
     1689
    16381690QString QCompleter::pathFromIndex(const QModelIndex& index) const
    16391691{
     
    16451697    if (!sourceModel)
    16461698        return QString();
     1699    bool isDirModel = false;
     1700    bool isFsModel = false;
    16471701#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)
    16511708        return sourceModel->data(index, d->role).toString();
    16521709
     
    16541711    QStringList list;
    16551712    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
    16571720        list.prepend(t);
    16581721        QModelIndex parent = idx.parent();
     
    16741737
    16751738    The default implementation of splitPath() splits a file system path based on
    1676     QDir::separator() when the sourceModel() is a QDirModel.
     1739    QDir::separator() when the sourceModel() is a QFileSystemModel.
    16771740
    16781741    When used with list models, the first item in the returned list is used for
     
    16841747{
    16851748    bool isDirModel = false;
     1749    bool isFsModel = false;
    16861750#ifndef QT_NO_DIRMODEL
    16871751    Q_D(const QCompleter);
    16881752    isDirModel = qobject_cast<QDirModel *>(d->proxy->sourceModel()) != 0;
    16891753#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())
    16921762        return QStringList(completionPrefix());
    16931763
Note: See TracChangeset for help on using the changeset viewer.