]> Shamusworld >> Repos - architektonas/blob - src/widgets/filedialog.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / widgets / filedialog.cpp
1 // filedialog.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  05/10/2010  Added this text. :-)
15 //
16
17 #include "filedialog.h"
18
19 #include "drawing.h"
20 #include "qg_graphicview.h"
21 #include "settings.h"
22 #include "system.h"
23
24 /**
25  * Shows a dialog for choosing a file name. Saving the file is up to
26  * the caller.
27  *
28  * @param type Will contain the file type that was chosen by the user.
29  *             Can be NULL to be ignored.
30  *
31  * @return File name with path and extension to determine the file type
32  *         or an empty string if the dialog was cancelled.
33  */
34 QString FileDialog::getSaveFileName(QWidget * parent, RS2::FormatType * type)
35 {
36         // read default settings:
37         settings.beginGroup("Paths");
38         QString defDir = settings.value("/Save", SYSTEM->getHomeDir()).toString();
39         QString defFilter = settings.value("/SaveFilter", "Drawing Exchange DXF 2000 (*.dxf)").toString();
40         //QString defFilter = "Drawing Exchange (*.dxf)";
41         settings.endGroup();
42
43         // prepare file save as dialog:
44 //      Q3FileDialog * fileDlg = new Q3FileDialog(parent, "Save as", true);
45         QFileDialog * fileDlg = new QFileDialog(parent, "Save as");
46         QStringList filters;
47         bool done = false;
48         bool cancel = false;
49         QString fn = "";
50
51         filters.append("Drawing Exchange DXF 2000 (*.dxf)");
52         filters.append("Drawing Exchange DXF R12 (*.dxf)");
53         filters.append("Font (*.cxf)");
54         fileDlg->setFilters(filters);
55 //      fileDlg->setMode(Q3FileDialog::AnyFile);
56         fileDlg->setFileMode(QFileDialog::AnyFile);
57 //      fileDlg->setCaption(QObject::tr("Save Drawing As"));
58         fileDlg->setWindowTitle(QObject::tr("Save Drawing As"));
59 //      fileDlg->setDir(defDir);
60         fileDlg->setDirectory(defDir);
61 //      fileDlg->setSelectedFilter(defFilter);
62         fileDlg->selectFilter(defFilter);
63
64         // run dialog:
65         do
66         {
67                 // accepted:
68                 if (fileDlg->exec() == QDialog::Accepted)
69                 {
70 //                      fn = fileDlg->selectedFile();
71                         QStringList files = fileDlg->selectedFiles();
72
73                         if (!files.isEmpty())
74                                 fn = files[0];
75
76 //                      fn = QDir::convertSeparators(QFileInfo(fn).absFilePath());
77                         fn = QDir::convertSeparators(QFileInfo(fn).absoluteFilePath());
78                         cancel = false;
79
80                         // append default extension:
81 //                      if (QFileInfo(fn).fileName().find('.') == -1)
82                         if (QFileInfo(fn).fileName().indexOf('.') == -1)
83                         {
84                                 if (fileDlg->selectedFilter() == "Font (*.cxf)")
85                                         fn += ".cxf";
86                                 else
87                                         fn += ".dxf";
88                         }
89
90                         // set format:
91                         if (type != NULL)
92                         {
93                                 if (fileDlg->selectedFilter() == "Font (*.cxf)")
94                                         *type = RS2::FormatCXF;
95                                 else if (fileDlg->selectedFilter() == "Drawing Exchange DXF R12 (*.dxf)")
96                                         *type = RS2::FormatDXF12;
97                                 else
98                                         *type = RS2::FormatDXF;
99                         }
100
101                         // overwrite warning:
102                         if (QFileInfo(fn).exists())
103                         {
104                                 int choice = QMessageBox::warning(parent, QObject::tr("Save Drawing As"),
105                                         QObject::tr("%1 already exists.\nWould you like to replace it?").arg(fn),
106                                         QObject::tr("Yes"), QObject::tr("No"), QObject::tr("Cancel"), 0, 1);
107
108                                 switch (choice)
109                                 {
110                                 case 0:
111                                         done = true;
112                                         break;
113                                 case 1:
114                                 case 2:
115                                 default:
116                                         done = false;
117                                         break;
118                                 }
119                         }
120                         else
121                         {
122                                 done = true;
123                         }
124                 }
125                 else
126                 {
127                         done = true;
128                         cancel = true;
129                         fn = "";
130                 }
131         }
132         while (!done);
133
134         // store new default settings:
135         if (!cancel)
136         {
137                 settings.beginGroup("Paths");
138 //              RS_SETTINGS->writeEntry("/Save", QFileInfo(fn).dirPath(true));
139                 settings.setValue("Save", QFileInfo(fn).absolutePath());
140                 //RS_SETTINGS->writeEntry("/SaveFilter", fileDlg->selectedFilter());
141                 settings.endGroup();
142         }
143
144         delete fileDlg;
145
146         return fn;
147 }
148
149 /**
150  * Shows a dialog for choosing a file name. Opening the file is up to
151  * the caller.
152  *
153  * @return File name with path and extension to determine the file type
154  *         or an empty string if the dialog was cancelled.
155  */
156 QString FileDialog::getOpenFileName(QWidget * parent, RS2::FormatType * type)
157 {
158         DEBUG->print("FileDialog::getOpenFileName");
159
160         // read default settings:
161         settings.beginGroup("/Paths");
162         QString defDir = settings.value("/Open", SYSTEM->getHomeDir()).toString();
163         //QString defFilter = RS_SETTINGS->readEntry("/OpenFilter",
164         //                      "Drawing Exchange (*.dxf *.DXF)");
165         QString defFilter = "Drawing Exchange (*.dxf *.DXF)";
166         settings.endGroup();
167
168         DEBUG->print("defDir: %s", defDir.toLatin1().data());
169         DEBUG->print("defFilter: %s", defFilter.toLatin1().data());
170
171         QString fDxf(QObject::tr("Drawing Exchange %1").arg("(*.dxf *.DXF)"));
172         QString fDxf1(QObject::tr("QCad 1.x file %1").arg("(*.dxf *.DXF)"));
173         QString fCxf(QObject::tr("Font %1").arg("(*.cxf)"));
174
175         DEBUG->print("fDxf: %s", fDxf.toLatin1().data());
176         DEBUG->print("fDxf1: %s", fDxf1.toLatin1().data());
177         DEBUG->print("fCxf: %s", fCxf.toLatin1().data());
178
179         QString fn = "";
180         bool cancel = false;
181
182 //      Q3FileDialog* fileDlg = new Q3FileDialog(parent, "File Dialog", true);
183         QFileDialog * fileDlg = new QFileDialog(parent);
184
185         QStringList filters;
186 //      filters.append(fDxf);
187 //      filters.append(fDxf1);
188 //      filters.append(fCxf);
189         filters << fDxf << fDxf1 << fCxf;
190
191 //      fileDlg->setFilters(filters);
192         fileDlg->setNameFilters(filters);
193 //      fileDlg->setMode(Q3FileDialog::ExistingFile);
194 //      fileDlg->setMode(QFileDialog::ExistingFile);
195         fileDlg->setFileMode(QFileDialog::ExistingFile);
196 //      fileDlg->setCaption(QObject::tr("Open Drawing"));
197         fileDlg->setWindowTitle(QObject::tr("Open Drawing"));
198 //      fileDlg->setDir(defDir);
199         fileDlg->setDirectory(defDir);
200 //      fileDlg->setSelectedFilter(defFilter);
201 //      fileDlg->selectFilter(defFilter);
202         fileDlg->selectNameFilter(defFilter);
203
204         // preview:
205         Drawing * gr = new Drawing;
206         QG_GraphicView * prev = new QG_GraphicView(parent);
207         prev->setContainer(gr);
208         prev->setBorders(1, 1, 1, 1);
209 #warning "!!! FileDialog: Commented out Qt3 previews !!!"
210 //      fileDlg->setContentsPreviewEnabled(true);
211 //      fileDlg->setContentsPreview(prev, prev);
212
213         cancel = true;
214
215         if (fileDlg->exec() == QDialog::Accepted)
216         {
217 //              fn = fileDlg->selectedFile();
218                 QStringList files = fileDlg->selectedFiles();
219
220                 if (!files.isEmpty())
221                         fn = files[0];
222
223 //              fn = QDir::convertSeparators(QFileInfo(fn).absFilePath());
224                 fn = QDir::convertSeparators(QFileInfo(fn).absoluteFilePath());
225
226 //printf("FileDialog: selectedNameFilter = %s\n", fileDlg->selectedNameFilter().toAscii().data());
227                 if (type != NULL)
228                 {
229 //                      if (fileDlg->selectedFilter() == fDxf1)
230                         if (fileDlg->selectedNameFilter() == fDxf1)
231                                 *type = RS2::FormatDXF1;
232 //                      else if (fileDlg->selectedFilter() == fDxf)
233                         else if (fileDlg->selectedNameFilter() == fDxf)
234                                 *type = RS2::FormatDXF;
235 //                      else if (fileDlg->selectedFilter() == fCxf)
236                         else if (fileDlg->selectedNameFilter() == fCxf)
237                                 *type = RS2::FormatCXF;
238                 }
239
240                 cancel = false;
241         }
242
243         // store new default settings:
244         if (!cancel)
245         {
246                 settings.beginGroup("Paths");
247 //              RS_SETTINGS->writeEntry("/Open", QFileInfo(fn).dirPath(true));
248                 settings.setValue("Open", QFileInfo(fn).absolutePath());
249 //              RS_SETTINGS->writeEntry("/OpenFilter", fileDlg->selectedFilter());
250                 settings.setValue("OpenFilter", fileDlg->selectedNameFilter());
251                 settings.endGroup();
252         }
253
254         DEBUG->print("FileDialog::getOpenFileName: fileName: %s", fn.toLatin1().data());
255         DEBUG->print("FileDialog::getOpenFileName: OK");
256
257         delete prev;
258         delete gr;
259         delete fileDlg;
260
261         return fn;
262 }