src/gui/dialogs/qfiledialog.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   689     The signal will be disconnected from the slot when the dialog is closed.
   689     The signal will be disconnected from the slot when the dialog is closed.
   690 */
   690 */
   691 void QFileDialog::open(QObject *receiver, const char *member)
   691 void QFileDialog::open(QObject *receiver, const char *member)
   692 {
   692 {
   693     Q_D(QFileDialog);
   693     Q_D(QFileDialog);
   694     const char *signal = (fileMode() == ExistingFiles) ? SIGNAL(filesSelected(const QStringList&))
   694     const char *signal = (fileMode() == ExistingFiles) ? SIGNAL(filesSelected(QStringList))
   695                                                        : SIGNAL(fileSelected(const QString&));
   695                                                        : SIGNAL(fileSelected(QString));
   696     connect(this, signal, receiver, member);
   696     connect(this, signal, receiver, member);
   697     d->signalToDisconnectOnClose = signal;
   697     d->signalToDisconnectOnClose = signal;
   698     d->receiverToDisconnectOnClose = receiver;
   698     d->receiverToDisconnectOnClose = receiver;
   699     d->memberToDisconnectOnClose = member;
   699     d->memberToDisconnectOnClose = member;
   700 
   700 
   775     if (d->rootPath() == newDirectory)
   775     if (d->rootPath() == newDirectory)
   776         return;
   776         return;
   777     QModelIndex root = d->model->setRootPath(newDirectory);
   777     QModelIndex root = d->model->setRootPath(newDirectory);
   778     d->qFileDialogUi->newFolderButton->setEnabled(d->model->flags(root) & Qt::ItemIsDropEnabled);
   778     d->qFileDialogUi->newFolderButton->setEnabled(d->model->flags(root) & Qt::ItemIsDropEnabled);
   779     if (root != d->rootIndex()) {
   779     if (root != d->rootIndex()) {
   780 #ifndef QT_NO_COMPLETER
   780 #ifndef QT_NO_FSCOMPLETER
   781     if (directory.endsWith(QLatin1Char('/')))
   781     if (directory.endsWith(QLatin1Char('/')))
   782         d->completer->setCompletionPrefix(newDirectory);
   782         d->completer->setCompletionPrefix(newDirectory);
   783     else
   783     else
   784         d->completer->setCompletionPrefix(newDirectory + QLatin1Char('/'));
   784         d->completer->setCompletionPrefix(newDirectory + QLatin1Char('/'));
   785 #endif
   785 #endif
  2143     model->setNameFilterDisables(true);
  2143     model->setNameFilterDisables(true);
  2144 #else
  2144 #else
  2145     model->setNameFilterDisables(false);
  2145     model->setNameFilterDisables(false);
  2146 #endif
  2146 #endif
  2147     model->d_func()->disableRecursiveSort = true;
  2147     model->d_func()->disableRecursiveSort = true;
  2148     QFileDialog::connect(model, SIGNAL(fileRenamed(const QString &, const QString &, const QString &)), q, SLOT(_q_fileRenamed(const QString &, const QString &, const QString &)));
  2148     QFileDialog::connect(model, SIGNAL(fileRenamed(QString,QString,QString)), q, SLOT(_q_fileRenamed(QString,QString,QString)));
  2149     QFileDialog::connect(model, SIGNAL(rootPathChanged(const QString &)),
  2149     QFileDialog::connect(model, SIGNAL(rootPathChanged(QString)),
  2150             q, SLOT(_q_pathChanged(const QString &)));
  2150             q, SLOT(_q_pathChanged(QString)));
  2151     QFileDialog::connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  2151     QFileDialog::connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
  2152             q, SLOT(_q_rowsInserted(const QModelIndex &)));
  2152             q, SLOT(_q_rowsInserted(QModelIndex)));
  2153     model->setReadOnly(false);
  2153     model->setReadOnly(false);
  2154 
  2154 
  2155     qFileDialogUi.reset(new Ui_QFileDialog());
  2155     qFileDialogUi.reset(new Ui_QFileDialog());
  2156     qFileDialogUi->setupUi(q);
  2156     qFileDialogUi->setupUi(q);
  2157 
  2157 
  2158     QList<QUrl> initialBookmarks;
  2158     QList<QUrl> initialBookmarks;
  2159     initialBookmarks << QUrl::fromLocalFile(QLatin1String(""))
  2159     initialBookmarks << QUrl::fromLocalFile(QLatin1String(""))
  2160                      << QUrl::fromLocalFile(QDir::homePath());
  2160                      << QUrl::fromLocalFile(QDir::homePath());
  2161     qFileDialogUi->sidebar->init(model, initialBookmarks);
  2161     qFileDialogUi->sidebar->init(model, initialBookmarks);
  2162     QFileDialog::connect(qFileDialogUi->sidebar, SIGNAL(goToUrl(const QUrl &)),
  2162     QFileDialog::connect(qFileDialogUi->sidebar, SIGNAL(goToUrl(QUrl)),
  2163                          q, SLOT(_q_goToUrl(const QUrl &)));
  2163                          q, SLOT(_q_goToUrl(QUrl)));
  2164 
  2164 
  2165     QObject::connect(qFileDialogUi->buttonBox, SIGNAL(accepted()), q, SLOT(accept()));
  2165     QObject::connect(qFileDialogUi->buttonBox, SIGNAL(accepted()), q, SLOT(accept()));
  2166     QObject::connect(qFileDialogUi->buttonBox, SIGNAL(rejected()), q, SLOT(reject()));
  2166     QObject::connect(qFileDialogUi->buttonBox, SIGNAL(rejected()), q, SLOT(reject()));
  2167 
  2167 
  2168 
  2168 
  2175     // filename
  2175     // filename
  2176     qFileDialogUi->fileNameEdit->init(this);
  2176     qFileDialogUi->fileNameEdit->init(this);
  2177 #ifndef QT_NO_SHORTCUT
  2177 #ifndef QT_NO_SHORTCUT
  2178     qFileDialogUi->fileNameLabel->setBuddy(qFileDialogUi->fileNameEdit);
  2178     qFileDialogUi->fileNameLabel->setBuddy(qFileDialogUi->fileNameEdit);
  2179 #endif
  2179 #endif
  2180 #ifndef QT_NO_COMPLETER
  2180 #ifndef QT_NO_FSCOMPLETER
  2181     completer = new QFSCompleter(model, q);
  2181     completer = new QFSCompleter(model, q);
  2182     qFileDialogUi->fileNameEdit->setCompleter(completer);
  2182     qFileDialogUi->fileNameEdit->setCompleter(completer);
  2183     QObject::connect(qFileDialogUi->fileNameEdit, SIGNAL(textChanged(QString)),
  2183     QObject::connect(qFileDialogUi->fileNameEdit, SIGNAL(textChanged(QString)),
  2184             q, SLOT(_q_autoCompleteFileName(QString)));
  2184             q, SLOT(_q_autoCompleteFileName(QString)));
  2185 #endif // QT_NO_COMPLETER
  2185 #endif // QT_NO_FSCOMPLETER
  2186     QObject::connect(qFileDialogUi->fileNameEdit, SIGNAL(textChanged(QString)),
  2186     QObject::connect(qFileDialogUi->fileNameEdit, SIGNAL(textChanged(QString)),
  2187                      q, SLOT(_q_updateOkButton()));
  2187                      q, SLOT(_q_updateOkButton()));
  2188 
  2188 
  2189     QObject::connect(qFileDialogUi->fileNameEdit, SIGNAL(returnPressed()), q, SLOT(accept()));
  2189     QObject::connect(qFileDialogUi->fileNameEdit, SIGNAL(returnPressed()), q, SLOT(accept()));
  2190 
  2190 
  2192     qFileDialogUi->fileTypeCombo->setDuplicatesEnabled(false);
  2192     qFileDialogUi->fileTypeCombo->setDuplicatesEnabled(false);
  2193     qFileDialogUi->fileTypeCombo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
  2193     qFileDialogUi->fileTypeCombo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
  2194     qFileDialogUi->fileTypeCombo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  2194     qFileDialogUi->fileTypeCombo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  2195     QObject::connect(qFileDialogUi->fileTypeCombo, SIGNAL(activated(int)),
  2195     QObject::connect(qFileDialogUi->fileTypeCombo, SIGNAL(activated(int)),
  2196                      q, SLOT(_q_useNameFilter(int)));
  2196                      q, SLOT(_q_useNameFilter(int)));
  2197     QObject::connect(qFileDialogUi->fileTypeCombo, SIGNAL(activated(const QString &)),
  2197     QObject::connect(qFileDialogUi->fileTypeCombo, SIGNAL(activated(QString)),
  2198                      q, SIGNAL(filterSelected(const QString &)));
  2198                      q, SIGNAL(filterSelected(QString)));
  2199 
  2199 
  2200     qFileDialogUi->listView->init(this);
  2200     qFileDialogUi->listView->init(this);
  2201     qFileDialogUi->listView->setModel(model);
  2201     qFileDialogUi->listView->setModel(model);
  2202     QObject::connect(qFileDialogUi->listView, SIGNAL(activated(QModelIndex)),
  2202     QObject::connect(qFileDialogUi->listView, SIGNAL(activated(QModelIndex)),
  2203                      q, SLOT(_q_enterDirectory(QModelIndex)));
  2203                      q, SLOT(_q_enterDirectory(QModelIndex)));
  2219     treeHeader->resizeSection(3, fm.width(QLatin1String("10/29/81 02:02PM")));
  2219     treeHeader->resizeSection(3, fm.width(QLatin1String("10/29/81 02:02PM")));
  2220     treeHeader->setContextMenuPolicy(Qt::ActionsContextMenu);
  2220     treeHeader->setContextMenuPolicy(Qt::ActionsContextMenu);
  2221 
  2221 
  2222     QActionGroup *showActionGroup = new QActionGroup(q);
  2222     QActionGroup *showActionGroup = new QActionGroup(q);
  2223     showActionGroup->setExclusive(false);
  2223     showActionGroup->setExclusive(false);
  2224     QObject::connect(showActionGroup, SIGNAL(triggered(QAction *)),
  2224     QObject::connect(showActionGroup, SIGNAL(triggered(QAction*)),
  2225                      q, SLOT(_q_showHeader(QAction *)));;
  2225                      q, SLOT(_q_showHeader(QAction*)));;
  2226 
  2226 
  2227     QAbstractItemModel *abstractModel = model;
  2227     QAbstractItemModel *abstractModel = model;
  2228 #ifndef QT_NO_PROXYMODEL
  2228 #ifndef QT_NO_PROXYMODEL
  2229     if (proxyModel)
  2229     if (proxyModel)
  2230         abstractModel = proxyModel;
  2230         abstractModel = proxyModel;
  2249     QObject::connect(shortcut, SIGNAL(activated()), q, SLOT(_q_deleteCurrent()));
  2249     QObject::connect(shortcut, SIGNAL(activated()), q, SLOT(_q_deleteCurrent()));
  2250 #endif
  2250 #endif
  2251 
  2251 
  2252     // Selections
  2252     // Selections
  2253     QItemSelectionModel *selections = qFileDialogUi->listView->selectionModel();
  2253     QItemSelectionModel *selections = qFileDialogUi->listView->selectionModel();
  2254     QObject::connect(selections, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
  2254     QObject::connect(selections, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
  2255                      q, SLOT(_q_selectionChanged()));
  2255                      q, SLOT(_q_selectionChanged()));
  2256     QObject::connect(selections, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
  2256     QObject::connect(selections, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
  2257                      q, SLOT(_q_currentChanged(QModelIndex)));
  2257                      q, SLOT(_q_currentChanged(QModelIndex)));
  2258     qFileDialogUi->splitter->setStretchFactor(qFileDialogUi->splitter->indexOf(qFileDialogUi->splitter->widget(1)), QSizePolicy::Expanding);
  2258     qFileDialogUi->splitter->setStretchFactor(qFileDialogUi->splitter->indexOf(qFileDialogUi->splitter->widget(1)), QSizePolicy::Expanding);
  2259 
  2259 
  2287         || (proxyModel == d->proxyModel))
  2287         || (proxyModel == d->proxyModel))
  2288         return;
  2288         return;
  2289 
  2289 
  2290     QModelIndex idx = d->rootIndex();
  2290     QModelIndex idx = d->rootIndex();
  2291     if (d->proxyModel) {
  2291     if (d->proxyModel) {
  2292         disconnect(d->proxyModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  2292         disconnect(d->proxyModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
  2293             this, SLOT(_q_rowsInserted(const QModelIndex &)));
  2293             this, SLOT(_q_rowsInserted(QModelIndex)));
  2294     } else {
  2294     } else {
  2295         disconnect(d->model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  2295         disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
  2296             this, SLOT(_q_rowsInserted(const QModelIndex &)));
  2296             this, SLOT(_q_rowsInserted(QModelIndex)));
  2297     }
  2297     }
  2298 
  2298 
  2299     if (proxyModel != 0) {
  2299     if (proxyModel != 0) {
  2300         proxyModel->setParent(this);
  2300         proxyModel->setParent(this);
  2301         d->proxyModel = proxyModel;
  2301         d->proxyModel = proxyModel;
  2302         proxyModel->setSourceModel(d->model);
  2302         proxyModel->setSourceModel(d->model);
  2303         d->qFileDialogUi->listView->setModel(d->proxyModel);
  2303         d->qFileDialogUi->listView->setModel(d->proxyModel);
  2304         d->qFileDialogUi->treeView->setModel(d->proxyModel);
  2304         d->qFileDialogUi->treeView->setModel(d->proxyModel);
  2305 #ifndef QT_NO_COMPLETER
  2305 #ifndef QT_NO_FSCOMPLETER
  2306         d->completer->setModel(d->proxyModel);
  2306         d->completer->setModel(d->proxyModel);
  2307         d->completer->proxyModel = d->proxyModel;
  2307         d->completer->proxyModel = d->proxyModel;
  2308 #endif
  2308 #endif
  2309         connect(d->proxyModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  2309         connect(d->proxyModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
  2310             this, SLOT(_q_rowsInserted(const QModelIndex &)));
  2310             this, SLOT(_q_rowsInserted(QModelIndex)));
  2311     } else {
  2311     } else {
  2312         d->proxyModel = 0;
  2312         d->proxyModel = 0;
  2313         d->qFileDialogUi->listView->setModel(d->model);
  2313         d->qFileDialogUi->listView->setModel(d->model);
  2314         d->qFileDialogUi->treeView->setModel(d->model);
  2314         d->qFileDialogUi->treeView->setModel(d->model);
  2315 #ifndef QT_NO_COMPLETER
  2315 #ifndef QT_NO_FSCOMPLETER
  2316         d->completer->setModel(d->model);
  2316         d->completer->setModel(d->model);
  2317         d->completer->sourceModel = d->model;
  2317         d->completer->sourceModel = d->model;
  2318         d->completer->proxyModel = 0;
  2318         d->completer->proxyModel = 0;
  2319 #endif
  2319 #endif
  2320         connect(d->model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  2320         connect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
  2321             this, SLOT(_q_rowsInserted(const QModelIndex &)));
  2321             this, SLOT(_q_rowsInserted(QModelIndex)));
  2322     }
  2322     }
  2323     QScopedPointer<QItemSelectionModel> selModel(d->qFileDialogUi->treeView->selectionModel());
  2323     QScopedPointer<QItemSelectionModel> selModel(d->qFileDialogUi->treeView->selectionModel());
  2324     d->qFileDialogUi->treeView->setSelectionModel(d->qFileDialogUi->listView->selectionModel());
  2324     d->qFileDialogUi->treeView->setSelectionModel(d->qFileDialogUi->listView->selectionModel());
  2325 
  2325 
  2326     d->setRootIndex(idx);
  2326     d->setRootIndex(idx);
  2327 
  2327 
  2328     // reconnect selection
  2328     // reconnect selection
  2329     QItemSelectionModel *selections = d->qFileDialogUi->listView->selectionModel();
  2329     QItemSelectionModel *selections = d->qFileDialogUi->listView->selectionModel();
  2330     QObject::connect(selections, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
  2330     QObject::connect(selections, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
  2331                      this, SLOT(_q_selectionChanged()));
  2331                      this, SLOT(_q_selectionChanged()));
  2332     QObject::connect(selections, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
  2332     QObject::connect(selections, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
  2333                      this, SLOT(_q_currentChanged(QModelIndex)));
  2333                      this, SLOT(_q_currentChanged(QModelIndex)));
  2334 }
  2334 }
  2335 
  2335 
  3020         }
  3020         }
  3021         break;
  3021         break;
  3022     case Qt::Key_Escape:
  3022     case Qt::Key_Escape:
  3023         q->hide();
  3023         q->hide();
  3024         return true;
  3024         return true;
  3025 #ifdef QT_KEYPAD_NAVIGATION
       
  3026     case Qt::Key_Down:
       
  3027     case Qt::Key_Up:
       
  3028         return (QApplication::navigationMode() != Qt::NavigationModeKeypadTabOrder
       
  3029                 && QApplication::navigationMode() != Qt::NavigationModeKeypadDirectional);
       
  3030 #endif
       
  3031     default:
  3025     default:
  3032         break;
  3026         break;
  3033     }
  3027     }
  3034     return false;
  3028     return false;
  3035 }
  3029 }
  3143     return QSize(QListView::sizeHint().width() * 2, height * 30);
  3137     return QSize(QListView::sizeHint().width() * 2, height * 30);
  3144 }
  3138 }
  3145 
  3139 
  3146 void QFileDialogListView::keyPressEvent(QKeyEvent *e)
  3140 void QFileDialogListView::keyPressEvent(QKeyEvent *e)
  3147 {
  3141 {
  3148     if (!d_ptr->itemViewKeyboardEvent(e)) {
  3142 #ifdef QT_KEYPAD_NAVIGATION
       
  3143     if (QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional) {
  3149         QListView::keyPressEvent(e);
  3144         QListView::keyPressEvent(e);
  3150     }
  3145         return;
  3151 #ifdef QT_KEYPAD_NAVIGATION
  3146     }
  3152     else if ((QApplication::navigationMode() == Qt::NavigationModeKeypadTabOrder
  3147 #endif // QT_KEYPAD_NAVIGATION
  3153          || QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional)
  3148 
  3154          && !hasEditFocus()) {
  3149     if (!d_ptr->itemViewKeyboardEvent(e))
  3155          e->ignore();
  3150         QListView::keyPressEvent(e);
  3156     } else {
       
  3157         e->accept();
       
  3158     }
       
  3159 #else
       
  3160     e->accept();
  3151     e->accept();
  3161 #endif
       
  3162 }
  3152 }
  3163 
  3153 
  3164 QFileDialogTreeView::QFileDialogTreeView(QWidget *parent) : QTreeView(parent)
  3154 QFileDialogTreeView::QFileDialogTreeView(QWidget *parent) : QTreeView(parent)
  3165 {
  3155 {
  3166 }
  3156 }
  3182 #endif
  3172 #endif
  3183 }
  3173 }
  3184 
  3174 
  3185 void QFileDialogTreeView::keyPressEvent(QKeyEvent *e)
  3175 void QFileDialogTreeView::keyPressEvent(QKeyEvent *e)
  3186 {
  3176 {
  3187     if (!d_ptr->itemViewKeyboardEvent(e)) {
  3177 #ifdef QT_KEYPAD_NAVIGATION
       
  3178     if (QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional) {
  3188         QTreeView::keyPressEvent(e);
  3179         QTreeView::keyPressEvent(e);
  3189     }
  3180         return;
       
  3181     }
       
  3182 #endif // QT_KEYPAD_NAVIGATION
       
  3183 
       
  3184     if (!d_ptr->itemViewKeyboardEvent(e))
       
  3185         QTreeView::keyPressEvent(e);
  3190     e->accept();
  3186     e->accept();
  3191 }
  3187 }
  3192 
  3188 
  3193 QSize QFileDialogTreeView::sizeHint() const
  3189 QSize QFileDialogTreeView::sizeHint() const
  3194 {
  3190 {
  3201     // FIXME: this is a hack to avoid propagating key press events
  3197     // FIXME: this is a hack to avoid propagating key press events
  3202     // to the dialog and from there to the "Ok" button
  3198     // to the dialog and from there to the "Ok" button
  3203 */
  3199 */
  3204 void QFileDialogLineEdit::keyPressEvent(QKeyEvent *e)
  3200 void QFileDialogLineEdit::keyPressEvent(QKeyEvent *e)
  3205 {
  3201 {
       
  3202 #ifdef QT_KEYPAD_NAVIGATION
       
  3203     if (QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional) {
       
  3204         QLineEdit::keyPressEvent(e);
       
  3205         return;
       
  3206     }
       
  3207 #endif // QT_KEYPAD_NAVIGATION
       
  3208 
  3206     int key = e->key();
  3209     int key = e->key();
  3207     QLineEdit::keyPressEvent(e);
  3210     QLineEdit::keyPressEvent(e);
  3208     if (key != Qt::Key_Escape
  3211     if (key != Qt::Key_Escape)
  3209 #ifdef QT_KEYPAD_NAVIGATION
       
  3210         && QApplication::navigationMode() == Qt::NavigationModeNone
       
  3211 #endif
       
  3212         )
       
  3213         e->accept();
  3212         e->accept();
  3214     if (hideOnEsc && (key == Qt::Key_Escape || key == Qt::Key_Return || key == Qt::Key_Enter)) {
  3213     if (hideOnEsc && (key == Qt::Key_Escape || key == Qt::Key_Return || key == Qt::Key_Enter)) {
  3215         e->accept();
  3214         e->accept();
  3216         hide();
  3215         hide();
  3217         d_ptr->currentView()->setFocus(Qt::ShortcutFocusReason);
  3216         d_ptr->currentView()->setFocus(Qt::ShortcutFocusReason);
  3218     }
  3217     }
  3219 }
  3218 }
  3220 
  3219 
  3221 #ifndef QT_NO_COMPLETER
  3220 #ifndef QT_NO_FSCOMPLETER
  3222 
  3221 
  3223 QString QFSCompleter::pathFromIndex(const QModelIndex &index) const
  3222 QString QFSCompleter::pathFromIndex(const QModelIndex &index) const
  3224 {
  3223 {
  3225     const QFileSystemModel *dirModel;
  3224     const QFileSystemModel *dirModel;
  3226     if (proxyModel)
  3225     if (proxyModel)