TQtDocumentManagerWrap.h
Go to the documentation of this file.
1 #ifndef iqtdoc_TQtDocumentManagerWrap_included
2 #define iqtdoc_TQtDocumentManagerWrap_included
3 
4 
5 // Qt includes
6 #include <QtCore/QSettings>
7 #if QT_VERSION >= 0x050000
8 #include <QtWidgets/QFileDialog>
9 #else
10 #include <QtGui/QFileDialog>
11 #endif
12 
13 
14 // ACF includes
15 #include <idoc/IDocumentManager.h>
17 
18 
19 namespace iqtdoc
20 {
21 
22 
26 template <class Base, class Gui>
27 class TQtDocumentManagerWrap: public Base, public Gui
28 {
29 public:
30  typedef Base BaseClass;
31  typedef typename BaseClass::Ids Ids;
32 
33  // pseudo-reimplemented (iqtgui::TRestorableGuiWrap)
34  virtual void OnRestoreSettings(const QSettings& settings);
35  virtual void OnSaveSettings(QSettings& settings) const;
36 
37  // pseudo-reimplemented (idoc::CSingleDocumentManagerBase)
38  virtual QString GetSaveFilePath(const QByteArray& documentTypeId, const istd::IChangeable* dataObjectPtr, const QString& currentFilePath) const;
39 
40 protected:
45  QStringList CreateFileDialogFilters(const QByteArray* documentTypeIdPtr, const istd::IChangeable* dataObjectPtr, int flags) const;
46 
47  void UpdateLastDirectory(const QString& filePath) const;
48  virtual QStringList GetOpenFilePathesFromDialog(const QByteArray* documentTypeIdPtr) const;
49 
50 private:
51  mutable QString m_lastDirectory;
52 };
53 
54 
55 // public methods
56 
57 // pseudo-reimplemented (TRestorableGuiWrap)
58 
59 template <class Base, class Gui>
61 {
62  QVariant valueNotSet = QVariant(-1);
63 
64  QVariant lastDirectory = settings.value("Workspace/LastOpenDocumentDirectory", valueNotSet);
65 
66  if (lastDirectory != valueNotSet){
67  m_lastDirectory = lastDirectory.toString();
68  }
69 }
70 
71 
72 template <class Base, class Gui>
74 {
75  settings.setValue("Workspace/LastOpenDocumentDirectory", m_lastDirectory);
76 }
77 
78 
79 // pseudo-reimplemented (idoc::CSingleDocumentManagerBase)
80 
81 template <class Base, class Gui>
82 QString TQtDocumentManagerWrap<Base, Gui>::GetSaveFilePath(const QByteArray& documentTypeId, const istd::IChangeable* dataObjectPtr, const QString& currentFilePath) const
83 {
84  QStringList filters = CreateFileDialogFilters(&documentTypeId, dataObjectPtr, ifile::IFilePersistence::QF_FILE | ifile::IFilePersistence::QF_SAVE);
85 
86  QString defaultFilter = "*." + QFileInfo(currentFilePath).completeSuffix();
87  for (QStringList::ConstIterator filterIter = filters.constBegin(); filterIter != filters.constEnd(); ++filterIter){
88  if (filterIter->contains(defaultFilter)){
89  defaultFilter = *filterIter;
90  break;
91  }
92  }
93 
94  QString filePath = QFileDialog::getSaveFileName(
95  Gui::GetWidget(),
96  Gui::tr("Save..."),
97  currentFilePath.isEmpty()? m_lastDirectory: currentFilePath, filters.join(";;"),
98  &defaultFilter);
99 
100  if (!filePath.isEmpty()){
101  UpdateLastDirectory(filePath);
102  }
103 
104  return filePath;
105 }
106 
107 
108 // protected methods
109 
110 template <class Base, class Gui>
111 QStringList TQtDocumentManagerWrap<Base, Gui>::CreateFileDialogFilters(const QByteArray* documentTypeIdPtr, const istd::IChangeable* dataObjectPtr, int flags) const
112 {
113  QStringList filters;
114 
115  Ids docTypeIds = BaseClass::GetDocumentTypeIds();
116 
117  QStringList allExt;
118 
119  if (documentTypeIdPtr != NULL){
120  ifile::IFileTypeInfo* typeInfoPtr = BaseClass::GetDocumentFileTypeInfo(*documentTypeIdPtr);
121  if (typeInfoPtr != NULL){
122  ifilegui::CFileDialogLoaderComp::AppendLoaderFilterList(*typeInfoPtr, dataObjectPtr, flags, allExt, filters, false);
123  }
124  }
125  else{
126  for ( typename Ids::const_iterator docTypeIter = docTypeIds.begin();
127  docTypeIter != docTypeIds.end();
128  ++docTypeIter){
129  ifile::IFileTypeInfo* typeInfoPtr = BaseClass::GetDocumentFileTypeInfo(*docTypeIter);
130  if (typeInfoPtr != NULL){
131  ifilegui::CFileDialogLoaderComp::AppendLoaderFilterList(*typeInfoPtr, dataObjectPtr, flags, allExt, filters, docTypeIds.count() > 1);
132  }
133  }
134  }
135 
136  if ((allExt.size() > 1) && ((flags & ifile::IFilePersistence::QF_SAVE) == 0)){
137  filters.prepend(Gui::tr("All known documents (%1)").arg("*." + allExt.join(" *.")));
138  }
139 
140  return filters;
141 }
142 
143 
144 template <class Base, class Gui>
146 {
147  QFileInfo fileInfo(filePath);
148 
149  m_lastDirectory = fileInfo.dir().absolutePath();
150 }
151 
152 
153 template <class Base, class Gui>
154 QStringList TQtDocumentManagerWrap<Base, Gui>::GetOpenFilePathesFromDialog(const QByteArray* documentTypeIdPtr) const
155 {
156  QStringList filters = CreateFileDialogFilters(documentTypeIdPtr, NULL, ifile::IFilePersistence::QF_FILE | ifile::IFilePersistence::QF_LOAD);
157 
158  QString lastUsedDirectory = m_lastDirectory;
159 
160  QFileInfo fileInfo(lastUsedDirectory);
161  if (!fileInfo.exists()){
162  lastUsedDirectory.clear();
163  }
164 
165  return QFileDialog::getOpenFileNames(Gui::GetWidget(), Gui::tr("Open Files..."), lastUsedDirectory, filters.join(";;"));
166 }
167 
168 
169 } // namespace iqtdoc
170 
171 
172 #endif // !iqtdoc_TQtDocumentManagerWrap_included
173 
174 
Provide loading and saving of objects.
Definition: IFileTypeInfo.h:22
void UpdateLastDirectory(const QString &filePath) const
Check for load operation.
Definition: IFileTypeInfo.h:33
virtual void OnSaveSettings(QSettings &settings) const
Common interface for data model objects, which can be changed.
Definition: IChangeable.h:24
static void AppendLoaderFilterList(const ifile::IFileTypeInfo &fileTypeInfo, const istd::IChangeable *dataObjectPtr, int flags, QStringList &allExt, QStringList &result, bool allowCommonDescription=true)
Append filters extracted from file loader to Qt filter list accepted by QFileDialog.
Wrapper for general functionality of a Qt based document workspace.
Check for save operation.
Definition: IFileTypeInfo.h:37
#define NULL
Definition: istd.h:64
virtual QString GetSaveFilePath(const QByteArray &documentTypeId, const istd::IChangeable *dataObjectPtr, const QString &currentFilePath) const
virtual void OnRestoreSettings(const QSettings &settings)
QStringList CreateFileDialogFilters(const QByteArray *documentTypeIdPtr, const istd::IChangeable *dataObjectPtr, int flags) const
Creates the filter for the file selection dialog.
Operations with specified file name should be considered.
Definition: IFileTypeInfo.h:41
virtual QStringList GetOpenFilePathesFromDialog(const QByteArray *documentTypeIdPtr) const

© 2007-2017 Witold Gantzke and Kirill Lepskiy