pqFileDialog.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
2 // SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef pqFileDialog_h
6 #define pqFileDialog_h
7 
8 #include "pqCoreModule.h"
9 #include "pqFileDialogModel.h"
10 
11 #include "vtkType.h" // needed for vtkTypeUInt32
12 
13 #include <optional>
14 
15 #include <QDialog>
16 #include <QMap>
17 #include <QPointer>
18 #include <QStringList>
19 
20 class QModelIndex;
21 class QPoint;
22 class QShowEvent;
23 class pqServer;
24 
65 class PQCORE_EXPORT pqFileDialog : public QDialog
66 {
67  typedef QDialog Superclass;
68  Q_OBJECT
69 public:
89  enum FileMode
90  {
95  ExistingFilesAndDirectories
96  };
97 
106  using pqFileValidityCallback = std::function<bool( //
107  QStringList group, //
108  vtkTypeUInt32 location, //
109  QString& reason //
110  )>;
111 
127  pqFileDialog(pqServer* server, QWidget* parent, const QString& title = QString(),
128  const QString& directory = QString(), const QString& filter = QString(),
129  bool supportGroupFiles = true, bool onlyBrowseRemotely = true);
130  ~pqFileDialog() override;
131 
133 
136  void setFileMode(FileMode, vtkTypeUInt32);
137  void setFileMode(FileMode);
139 
141 
144  void setRecentlyUsedExtension(const QString& fileExtension, vtkTypeUInt32 location);
145  void setRecentlyUsedExtension(const QString& fileExtension);
147 
153  void setFileValidityCallback(
154  const std::optional<pqFileValidityCallback>& callback = std::nullopt);
155 
159  std::optional<pqFileValidityCallback> getFileValidityCallback();
160 
164  QStringList getSelectedFiles(int index = 0);
165 
169  QList<QStringList> getAllSelectedFiles();
170 
174  int getSelectedFilterIndex();
175 
179  void accept() override;
180 
184  bool selectFile(const QString&);
185 
189  void setShowHidden(const bool& hidden);
190 
194  bool getShowHidden();
195 
200  vtkTypeUInt32 getSelectedLocation() const { return this->SelectedLocation; }
201 
203 
207  static QString getSaveFileName(pqServer* server, QWidget* parentWdg,
208  const QString& title = QString(), const QString& directory = QString(),
209  const QString& filter = QString())
210  {
211  const QPair<QString, vtkTypeUInt32> result =
212  pqFileDialog::getSaveFileNameAndLocation(server, parentWdg, title, directory, filter);
213  return result.first;
214  }
215  static QPair<QString, vtkTypeUInt32> getSaveFileNameAndLocation(pqServer* server,
216  QWidget* parentWdg, const QString& title = QString(), const QString& directory = QString(),
217  const QString& filter = QString(), bool supportGroupFiles = false,
218  bool onlyBrowseRemotely = true);
220 Q_SIGNALS:
224  void filesSelected(const QList<QStringList>&);
225 
233  void filesSelected(const QStringList&);
234 
240  void fileAccepted(const QString&);
241 
242 protected:
243  bool acceptExistingFiles();
244  bool acceptDefault(const bool& checkForGrouping);
245 
246  QStringList buildFileGroup(const QString& filename);
247  bool areFilesValid(const QStringList& filenames, QString& reason);
248 
249  void showEvent(QShowEvent* showEvent) override;
250 
251 private Q_SLOTS:
252  void onLocationChanged(int fs);
253  void onModelReset();
254  void onNavigate(const QString& = QString());
255  void onNavigateUp();
256  void onNavigateBack();
257  void onNavigateForward();
258  void onNavigateDown(const QModelIndex&);
259  void onFilterChange(const QString&);
260 
261  void onClickedRecent(const QModelIndex&);
262  void onClickedFavorite(const QModelIndex&);
263  void onClickedFile(const QModelIndex&);
264 
265  void onActivateFavorite(const QModelIndex&);
266  void onActivateLocation(const QModelIndex&);
267  void onActivateRecent(const QModelIndex&);
268  void onDoubleClickFile(const QModelIndex&);
269 
270  void onTextEdited(const QString&);
271 
272  void onShowHiddenFiles(const bool& hide);
273 
274  void onShowDetailToggled(bool show);
275 
276  void onGroupFilesToggled(bool group);
277 
278  // Called when the user changes the file selection.
279  void fileSelectionChanged();
280 
281  // Called when the user right-clicks in the file qtreeview
282  void onContextMenuRequested(const QPoint& pos);
283 
284  // Called when the user right-clicks in the favorites qlistview
285  void onFavoritesContextMenuRequested(const QPoint& pos);
286 
287  void AddDirectoryToFavorites(QString const&);
288  void RemoveDirectoryFromFavorites(QString const&);
289  void FilterDirectoryFromFavorites(const QString& filter);
290 
291  void onAddCurrentDirectoryToFavorites();
292  void onRemoveSelectedDirectoriesFromFavorites();
293  void onResetFavoritesToSystemDefault();
294 
295  // Called when the user requests to create a new directory in the cwd
296  void onCreateNewFolder();
297 
301  void addToFilesSelected(const QStringList&);
302 
306  void emitFilesSelectionDone();
307 
313  void updateButtonStates(vtkTypeUInt32 fileSystem);
314 
315 private: // NOLINT(readability-redundant-access-specifiers)
316  pqFileDialog(const pqFileDialog&);
317  pqFileDialog& operator=(const pqFileDialog&);
318 
319  class pqImplementation;
320  QMap<vtkTypeUInt32, QPointer<pqImplementation>> Implementations;
321  vtkTypeUInt32 SelectedLocation;
322 
323  QPointer<pqServer> Server;
324  const QString StartDirectory;
325  const QString NameFilter;
326  bool SupportsGroupFiles;
327  vtkTypeUInt32 DefaultLocation;
328  QString RecentlyUsedExtension;
329  std::optional<pqFileValidityCallback> FileValidityCallback;
330 
331  void addImplementation(vtkTypeUInt32 location);
332 
333  // returns if true if files are loaded
334  bool acceptInternal(const QStringList& selected_files);
335  QString fixFileExtension(const QString& filename, const QString& filter);
336 
338 
341  void saveState(vtkTypeUInt32 fileSystem);
342  void saveState();
344 
346 
349  void restoreState(vtkTypeUInt32 fileSystem);
350  void restoreState();
352 };
353 
354 #endif // pqFileDialog_h
location
vtkTypeUInt32 getSelectedLocation() const
Get the location that the selected files/directories belong to.
Definition: pqFileDialog.h:200
static QString getSaveFileName(pqServer *server, QWidget *parentWdg, const QString &title=QString(), const QString &directory=QString(), const QString &filter=QString())
static method similar to QFileDialog::getSaveFileName(...) to make it easier to get a file name to sa...
Definition: pqFileDialog.h:207
FileMode
choose mode for selecting file/folder.
Definition: pqFileDialog.h:89
Provides a standard file dialog "front-end" for the pqFileDialogModel "back-end", i...
Definition: pqFileDialog.h:65
static QPair< QString, vtkTypeUInt32 > getSaveFileNameAndLocation(pqServer *server, QWidget *parentWdg, const QString &title=QString(), const QString &directory=QString(), const QString &filter=QString(), bool supportGroupFiles=false, bool onlyBrowseRemotely=true)
static method similar to QFileDialog::getSaveFileName(...) to make it easier to get a file name to sa...
std::function< bool(QStringList group, vtkTypeUInt32 location, QString &reason)> pqFileValidityCallback
Callback to evaluate the validity of a selected file group.
Definition: pqFileDialog.h:110
pqServer (should be renamed to pqSession) is a pqServerManagerModelItem subclass that represents a vt...
Definition: pqServer.h:37
title