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
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/10/2010 Added this text. :-)
17 #include "filedialog.h"
20 #include "qg_graphicview.h"
25 * Shows a dialog for choosing a file name. Saving the file is up to
28 * @param type Will contain the file type that was chosen by the user.
29 * Can be NULL to be ignored.
31 * @return File name with path and extension to determine the file type
32 * or an empty string if the dialog was cancelled.
34 QString FileDialog::getSaveFileName(QWidget * parent, RS2::FormatType * type)
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)";
43 // prepare file save as dialog:
44 // Q3FileDialog * fileDlg = new Q3FileDialog(parent, "Save as", true);
45 QFileDialog * fileDlg = new QFileDialog(parent, "Save as");
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);
68 if (fileDlg->exec() == QDialog::Accepted)
70 // fn = fileDlg->selectedFile();
71 QStringList files = fileDlg->selectedFiles();
76 // fn = QDir::convertSeparators(QFileInfo(fn).absFilePath());
77 fn = QDir::convertSeparators(QFileInfo(fn).absoluteFilePath());
80 // append default extension:
81 // if (QFileInfo(fn).fileName().find('.') == -1)
82 if (QFileInfo(fn).fileName().indexOf('.') == -1)
84 if (fileDlg->selectedFilter() == "Font (*.cxf)")
93 if (fileDlg->selectedFilter() == "Font (*.cxf)")
94 *type = RS2::FormatCXF;
95 else if (fileDlg->selectedFilter() == "Drawing Exchange DXF R12 (*.dxf)")
96 *type = RS2::FormatDXF12;
98 *type = RS2::FormatDXF;
101 // overwrite warning:
102 if (QFileInfo(fn).exists())
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);
134 // store new default settings:
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());
150 * Shows a dialog for choosing a file name. Opening the file is up to
153 * @return File name with path and extension to determine the file type
154 * or an empty string if the dialog was cancelled.
156 QString FileDialog::getOpenFileName(QWidget * parent, RS2::FormatType * type)
158 DEBUG->print("FileDialog::getOpenFileName");
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)";
168 DEBUG->print("defDir: %s", defDir.toLatin1().data());
169 DEBUG->print("defFilter: %s", defFilter.toLatin1().data());
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)"));
175 DEBUG->print("fDxf: %s", fDxf.toLatin1().data());
176 DEBUG->print("fDxf1: %s", fDxf1.toLatin1().data());
177 DEBUG->print("fCxf: %s", fCxf.toLatin1().data());
182 // Q3FileDialog* fileDlg = new Q3FileDialog(parent, "File Dialog", true);
183 QFileDialog * fileDlg = new QFileDialog(parent);
186 // filters.append(fDxf);
187 // filters.append(fDxf1);
188 // filters.append(fCxf);
189 filters << fDxf << fDxf1 << fCxf;
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);
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);
215 if (fileDlg->exec() == QDialog::Accepted)
217 // fn = fileDlg->selectedFile();
218 QStringList files = fileDlg->selectedFiles();
220 if (!files.isEmpty())
223 // fn = QDir::convertSeparators(QFileInfo(fn).absFilePath());
224 fn = QDir::convertSeparators(QFileInfo(fn).absoluteFilePath());
226 //printf("FileDialog: selectedNameFilter = %s\n", fileDlg->selectedNameFilter().toAscii().data());
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;
243 // store new default settings:
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());
254 DEBUG->print("FileDialog::getOpenFileName: fileName: %s", fn.toLatin1().data());
255 DEBUG->print("FileDialog::getOpenFileName: OK");