/
FileDialog.cpp
897 lines (784 loc) · 30.6 KB
/
FileDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/***************************************************************************
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 51 Franklin Street, *
* Fifth Floor, Boston, MA 02110-1301, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QApplication>
# include <QButtonGroup>
# include <QCompleter>
# include <QComboBox>
# include <QDesktopServices>
# include <QGridLayout>
# include <QGroupBox>
# include <QLineEdit>
# include <QPushButton>
# include <QRadioButton>
# include <QStyle>
# include <QUrl>
#endif
#include <Base/Parameter.h>
#include <App/Application.h>
#include "FileDialog.h"
#include "MainWindow.h"
#include "BitmapFactory.h"
using namespace Gui;
/* TRANSLATOR Gui::FileDialog */
FileDialog::FileDialog(QWidget * parent)
: QFileDialog(parent)
{
connect(this, SIGNAL(filterSelected(const QString&)),
this, SLOT(onSelectedFilter(const QString&)));
}
FileDialog::~FileDialog()
{
}
void FileDialog::onSelectedFilter(const QString& filter)
{
QRegExp rx(QLatin1String("\\(\\*.(\\w+)"));
QString suf = selectedFilter();
if (rx.indexIn(suf) >= 0) {
suf = rx.cap(1);
setDefaultSuffix(suf.toLower());
}
}
bool FileDialog::hasSuffix(const QString& ext) const
{
QRegExp rx(QString::fromLatin1("\\*.(%1)\\W").arg(ext));
rx.setCaseSensitivity(Qt::CaseInsensitive);
QStringList filters = nameFilters();
for (QStringList::iterator it = filters.begin(); it != filters.end(); ++it) {
QString str = *it;
if (rx.indexIn(str) != -1) {
return true;
}
}
return false;
}
void FileDialog::accept()
{
// When saving to a file make sure that the entered filename ends with the selected
// file filter
if (acceptMode() == QFileDialog::AcceptSave) {
QStringList files = selectedFiles();
if (!files.isEmpty()) {
QString ext = this->defaultSuffix();
QString file = files.front();
QString suffix = QFileInfo(file).suffix();
// #0001928: do not add a suffix if a file with suffix is entered
// #0002209: make sure that the entered suffix is part of one of the filters
if (!ext.isEmpty() && (suffix.isEmpty() || !hasSuffix(suffix))) {
file = QString::fromLatin1("%1.%2").arg(file).arg(ext);
// That's the built-in line edit
QLineEdit* fileNameEdit = this->findChild<QLineEdit*>(QString::fromLatin1("fileNameEdit"));
if (fileNameEdit)
fileNameEdit->setText(file);
}
}
}
QFileDialog::accept();
}
/**
* This is a convenience static function that will return a file name selected by the user. The file does not have to exist.
*/
QString FileDialog::getSaveFileName (QWidget * parent, const QString & caption, const QString & dir,
const QString & filter, QString * selectedFilter, Options options)
{
QString dirName = dir;
if (dirName.isEmpty()) {
dirName = getWorkingDirectory();
} else {
QFileInfo fi(dir);
if (fi.isRelative()) {
dirName = getWorkingDirectory();
dirName += QLatin1String("/");
dirName += fi.fileName();
}
// get the suffix for the filter
QRegExp rx;
rx.setPattern(QLatin1String("\\s(\\(\\*\\.\\w{1,})\\W"));
int index = rx.indexIn(filter);
if (index != -1) {
// get the suffix with the leading dot
QString suffix = filter.mid(index+3, rx.matchedLength()-4);
if (fi.suffix().isEmpty())
dirName += suffix;
}
}
QString windowTitle = caption;
if (windowTitle.isEmpty())
windowTitle = FileDialog::tr("Save as");
#if QT_VERSION < 0x040800 && defined(FC_OS_MACOSX)
options |= QFileDialog::DontUseNativeDialog;
#endif
// NOTE: We must not change the specified file name afterwards as we may return the name of an already
// existing file. Hence we must extract the first matching suffix from the filter list and append it
// before showing the file dialog.
#if defined(FC_OS_LINUX)
QList<QUrl> urls;
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
urls << QUrl::fromLocalFile(getWorkingDirectory());
QString file;
FileDialog dlg(parent);
dlg.setWindowTitle(windowTitle);
dlg.setSidebarUrls(urls);
dlg.setIconProvider(new FileIconProvider());
dlg.setFileMode(QFileDialog::AnyFile);
dlg.setAcceptMode(QFileDialog::AcceptSave);
dlg.setDirectory(dirName);
dlg.setOptions(options);
dlg.setFilters(filter.split(QLatin1String(";;")));
dlg.onSelectedFilter(dlg.selectedFilter());
dlg.setNameFilterDetailsVisible(true);
dlg.setConfirmOverwrite(true);
if (dlg.exec() == QDialog::Accepted) {
if (selectedFilter)
*selectedFilter = dlg.selectedFilter();
file = dlg.selectedFiles().front();
}
#else
QString file = QFileDialog::getSaveFileName(parent, windowTitle, dirName, filter, selectedFilter, options);
#if QT_VERSION >= 0x040600
file = QDir::fromNativeSeparators(file);
#endif
#endif
if (!file.isEmpty()) {
setWorkingDirectory(file);
return file;
} else {
return QString::null;
}
}
/**
* This is a convenience static function that will return an existing directory selected by the user.
*/
QString FileDialog::getExistingDirectory( QWidget * parent, const QString & caption, const QString & dir, Options options )
{
#if QT_VERSION < 0x040800 && defined(FC_OS_MACOSX)
options |= QFileDialog::DontUseNativeDialog;
#endif
QString path = QFileDialog::getExistingDirectory(parent, caption, dir, options);
// valid path was selected
if ( !path.isEmpty() ) {
QDir d(path);
path = d.path(); // get path in Qt manner
}
return path;
}
/**
* This is a convenience static function that returns an existing file selected by the user.
* If the user pressed Cancel, it returns a null string.
*/
QString FileDialog::getOpenFileName(QWidget * parent, const QString & caption, const QString & dir,
const QString & filter, QString * selectedFilter, Options options)
{
QString dirName = dir;
if (dirName.isEmpty()) {
dirName = getWorkingDirectory();
}
QString windowTitle = caption;
if (windowTitle.isEmpty())
windowTitle = FileDialog::tr("Open");
#if QT_VERSION < 0x040800 && defined(FC_OS_MACOSX)
options |= QFileDialog::DontUseNativeDialog;
#endif
#if defined(FC_OS_LINUX)
QList<QUrl> urls;
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
urls << QUrl::fromLocalFile(getWorkingDirectory());
QString file;
FileDialog dlg(parent);
dlg.setWindowTitle(windowTitle);
dlg.setSidebarUrls(urls);
dlg.setIconProvider(new FileIconProvider());
dlg.setFileMode(QFileDialog::ExistingFile);
dlg.setAcceptMode(QFileDialog::AcceptOpen);
dlg.setDirectory(dirName);
dlg.setOptions(options);
dlg.setFilters(filter.split(QLatin1String(";;")));
dlg.setNameFilterDetailsVisible(true);
if (dlg.exec() == QDialog::Accepted) {
if (selectedFilter)
*selectedFilter = dlg.selectedFilter();
file = dlg.selectedFiles().front();
}
#else
QString file = QFileDialog::getOpenFileName(parent, windowTitle, dirName, filter, selectedFilter, options);
#if QT_VERSION >= 0x040600
file = QDir::fromNativeSeparators(file);
#endif
#endif
if (!file.isEmpty()) {
setWorkingDirectory(file);
return file;
} else {
return QString::null;
}
}
/**
* This is a convenience static function that will return one or more existing files selected by the user.
*/
QStringList FileDialog::getOpenFileNames (QWidget * parent, const QString & caption, const QString & dir,
const QString & filter, QString * selectedFilter, Options options)
{
QString dirName = dir;
if (dirName.isEmpty()) {
dirName = getWorkingDirectory();
}
QString windowTitle = caption;
if (windowTitle.isEmpty())
windowTitle = FileDialog::tr("Open");
#if QT_VERSION < 0x040800 && defined(FC_OS_MACOSX)
options |= QFileDialog::DontUseNativeDialog;
#endif
#if defined(FC_OS_LINUX)
QList<QUrl> urls;
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation));
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
urls << QUrl::fromLocalFile(getWorkingDirectory());
QStringList files;
FileDialog dlg(parent);
dlg.setWindowTitle(windowTitle);
dlg.setSidebarUrls(urls);
dlg.setIconProvider(new FileIconProvider());
dlg.setFileMode(QFileDialog::ExistingFiles);
dlg.setAcceptMode(QFileDialog::AcceptOpen);
dlg.setDirectory(dirName);
dlg.setOptions(options);
dlg.setFilters(filter.split(QLatin1String(";;")));
dlg.setNameFilterDetailsVisible(true);
if (dlg.exec() == QDialog::Accepted) {
if (selectedFilter)
*selectedFilter = dlg.selectedFilter();
files = dlg.selectedFiles();
}
#else
QStringList files = QFileDialog::getOpenFileNames(parent, windowTitle, dirName, filter, selectedFilter, options);
#if QT_VERSION >= 0x040600
for (QStringList::iterator it = files.begin(); it != files.end(); ++it) {
*it = QDir::fromNativeSeparators(*it);
}
#endif
#endif
if (!files.isEmpty()) {
setWorkingDirectory(files.front());
}
return files;
}
/**
* Returns the working directory for the file dialog. This path can be used in
* combination with getSaveFileName(), getOpenFileName(), getOpenFileNames() or
* getExistingDirectory() to open the dialog in this path.
*/
QString FileDialog::getWorkingDirectory()
{
std::string path = App::GetApplication().Config()["UserHomePath"];
Base::Reference<ParameterGrp> hPath = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
->GetGroup("Preferences")->GetGroup("General");
std::string dir = hPath->GetASCII("FileOpenSavePath", path.c_str());
QFileInfo fi(QString::fromUtf8(dir.c_str()));
if (!fi.exists())
dir = path;
return QString::fromUtf8(dir.c_str());
}
/**
* Sets the working directory to \a dir for the file dialog.
* If \a dir is a file then the path only is taken.
* getWorkingDirectory() returns the working directory.
*/
void FileDialog::setWorkingDirectory(const QString& dir)
{
QString dirName = dir;
if (!dir.isEmpty()) {
QFileInfo info(dir);
dirName = info.absolutePath();
}
Base::Reference<ParameterGrp> hPath = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
->GetGroup("Preferences")->GetGroup("General");
hPath->SetASCII("FileOpenSavePath", dirName.toUtf8());
}
// ======================================================================
/* TRANSLATOR Gui::FileOptionsDialog */
FileOptionsDialog::FileOptionsDialog( QWidget* parent, Qt::WFlags fl )
: QFileDialog( parent, fl )
{
extensionButton = new QPushButton( this );
extensionButton->setText( tr( "Extended" ) );
//search for the grid layout and add the new button
QGridLayout* grid = this->findChild<QGridLayout*>();
#if QT_VERSION >= 0x040500
grid->addWidget(extensionButton, 4, 2, Qt::AlignLeft);
#else
grid->addWidget(extensionButton, 4, 5, Qt::AlignLeft);
#endif
connect(extensionButton, SIGNAL(clicked()), this, SLOT(toggleExtension()));
}
FileOptionsDialog::~FileOptionsDialog()
{
}
void FileOptionsDialog::accept()
{
// Fixes a bug of the default implementation when entering an asterik
QLineEdit* filename = this->findChild<QLineEdit*>();
QString fn = filename->text();
if (fn.startsWith(QLatin1String("*"))) {
QFileInfo fi(fn);
QString ext = fi.suffix();
ext.prepend(QLatin1String("*."));
QStringList filters = this->filters();
bool ok=false;
// Compare the given suffix with the suffixes of all filters
QString filter;
for (QStringList::ConstIterator it = filters.begin(); it != filters.end(); ++it) {
if ((*it).contains(ext)) {
filter = *it;
ok = true;
break;
}
}
// if no appropriate filter was found the add the 'All files' filter
if (!ok) {
filter = tr("All files (*.*)");
filters << filter;
setFilters(filters);
}
// empty the line edit
filename->blockSignals(true);
filename->clear();
filename->blockSignals(false);
selectFilter(filter);
return;
}
else if (!fn.isEmpty()) {
QFileInfo fi(fn);
QString ext = fi.completeSuffix();
QRegExp rx(QLatin1String("\\(\\*.(\\w+)"));
QString suf = selectedFilter();
if (rx.indexIn(suf) >= 0)
suf = rx.cap(1);
if (ext.isEmpty())
setDefaultSuffix(suf);
else if (ext.toLower() != suf.toLower()) {
fn = QString::fromAscii("%1.%2").arg(fn).arg(suf);
selectFile(fn);
}
}
QFileDialog::accept();
}
void FileOptionsDialog::toggleExtension()
{
QWidget* w = extension();
if (w)
showExtension(!w->isVisible());
}
void FileOptionsDialog::setOptionsWidget(FileOptionsDialog::ExtensionPosition pos, QWidget* w, bool show)
{
if (pos == ExtensionRight) {
setExtension(w);
setOrientation(Qt::Horizontal);
}
else if (pos == ExtensionBottom) {
setExtension(w);
setOrientation(Qt::Vertical);
}
w->hide();
if (show)
toggleExtension();
}
QWidget* FileOptionsDialog::getOptionsWidget() const
{
return this->extension();
}
// ======================================================================
/**
* Constructs an empty file icon provider called \a name, with the parent \a parent.
*/
FileIconProvider::FileIconProvider()
{
}
FileIconProvider::~FileIconProvider()
{
}
QIcon FileIconProvider::icon(IconType type) const
{
return QFileIconProvider::icon(type);
}
QIcon FileIconProvider::icon(const QFileInfo & info) const
{
if (info.suffix().toLower() == QLatin1String("fcstd")) {
// return QApplication::windowIcon();
return QIcon(QString::fromLatin1(":/icons/freecad-doc.png"));
}
else if (info.suffix().toLower().startsWith(QLatin1String("fcstd"))) {
QIcon icon(QString::fromLatin1(":/icons/freecad-doc.png"));
QIcon darkIcon;
int w = QApplication::style()->pixelMetric(QStyle::PM_ListViewIconSize);
darkIcon.addPixmap(icon.pixmap(w, w, QIcon::Disabled, QIcon::Off), QIcon::Normal, QIcon::Off);
darkIcon.addPixmap(icon.pixmap(w, w, QIcon::Disabled, QIcon::On ), QIcon::Normal, QIcon::On );
return darkIcon;
}
return QFileIconProvider::icon(info);
}
QString FileIconProvider::type(const QFileInfo & info) const
{
return QFileIconProvider::type(info);
}
// --------------------------------------------------------------------
/* TRANSLATOR Gui::FileChooser */
/**
* Constructs a file chooser called \a name with the parent \a parent.
*/
FileChooser::FileChooser ( QWidget * parent )
: QWidget(parent), md( File ), _filter( QString::null )
{
QHBoxLayout *layout = new QHBoxLayout( this );
layout->setMargin( 0 );
layout->setSpacing( 6 );
lineEdit = new QLineEdit ( this );
completer = new QCompleter ( this );
completer->setMaxVisibleItems( 12 );
fs_model = new QFileSystemModel( completer );
fs_model->setRootPath(QString::fromUtf8(""));
completer->setModel( fs_model );
lineEdit->setCompleter( completer );
layout->addWidget( lineEdit );
connect(lineEdit, SIGNAL(textChanged(const QString &)),
this, SIGNAL(fileNameChanged(const QString &)));
connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
button = new QPushButton(QLatin1String("..."), this);
button->setFixedWidth(2*button->fontMetrics().width(QLatin1String(" ... ")));
layout->addWidget(button);
connect( button, SIGNAL(clicked()), this, SLOT(chooseFile()));
setFocusProxy(lineEdit);
}
FileChooser::~FileChooser()
{
}
/**
* \property FileChooser::fileName
*
* This property holds the file name.
* Set this property's value with setFileName() and get this property's value with fileName().
*
* \sa fileName(), setFileName().
*/
QString FileChooser::fileName() const
{
return lineEdit->text();
}
void FileChooser::editingFinished()
{
QString le_converted = lineEdit->text();
le_converted.replace(QString::fromStdString("\\"), QString::fromStdString("/"));
lineEdit->setText(le_converted);
FileDialog::setWorkingDirectory(le_converted);
fileNameSelected(le_converted);
}
/**
* Sets the file name \a s.
*/
void FileChooser::setFileName( const QString& s )
{
lineEdit->setText( s );
}
/**
* Opens a FileDialog to choose either a file or a directory in dependency of the
* value of the Mode property.
*/
void FileChooser::chooseFile()
{
QString prechosenDirectory = lineEdit->text();
if (prechosenDirectory.isEmpty()) {
prechosenDirectory = FileDialog::getWorkingDirectory();
}
QString fn;
if ( mode() == File )
fn = QFileDialog::getOpenFileName( this, tr( "Select a file" ), prechosenDirectory, _filter );
else
fn = QFileDialog::getExistingDirectory( this, tr( "Select a directory" ), prechosenDirectory );
if (!fn.isEmpty()) {
lineEdit->setText(fn);
FileDialog::setWorkingDirectory(fn);
fileNameSelected(fn);
}
}
/**
* \property FileChooser::mode
*
* This property holds whether the widgets selects either a file or a directory.
* The default value of chooseFile is set to File.
*
* \sa chooseFile(), mode(), setMode().
*/
FileChooser::Mode FileChooser::mode() const
{
return md;
}
/**
* If \a m is File the widget is set to choose a file, otherwise it is set to
* choose a directory.
*/
void FileChooser::setMode( FileChooser::Mode m )
{
md = m;
}
/**
* \property FileChooser::filter
*
* This property holds the set filter to choose a file. This property is used only if
* FileChooser::Mode is set to File.
*
* \sa chooseFile(), filter(), setFilter().
*/
QString FileChooser::filter() const
{
return _filter;
}
/**
* Sets the filter for choosing a file.
*/
void FileChooser::setFilter ( const QString& filter )
{
_filter = filter;
}
/**
* Sets the browse button's text to \a txt.
*/
void FileChooser::setButtonText( const QString& txt )
{
button->setText( txt );
int w1 = 2*button->fontMetrics().width(txt);
int w2 = 2*button->fontMetrics().width(QLatin1String(" ... "));
button->setFixedWidth( (w1 > w2 ? w1 : w2) );
}
/**
* Returns the browse button's text.
*/
QString FileChooser::buttonText() const
{
return button->text();
}
// ----------------------------------------------------------------------
/* TRANSLATOR Gui::SelectModule */
SelectModule::SelectModule (const QString& type, const SelectModule::Dict& types, QWidget * parent)
: QDialog(parent, Qt::WindowTitleHint)
{
setWindowTitle(tr("Select module"));
groupBox = new QGroupBox(this);
groupBox->setTitle(tr("Open %1 as").arg(type));
group = new QButtonGroup(this);
gridLayout = new QGridLayout(this);
gridLayout->setSpacing(6);
gridLayout->setMargin(9);
gridLayout1 = new QGridLayout(groupBox);
gridLayout1->setSpacing(6);
gridLayout1->setMargin(9);
int index = 0;
for (SelectModule::Dict::const_iterator it = types.begin(); it != types.end(); ++it) {
QRadioButton* button = new QRadioButton(groupBox);
QRegExp rx;
QString filter = it.key();
QString module = it.value();
// ignore file types in (...)
rx.setPattern(QLatin1String("\\s+\\([\\w\\*\\s\\.]+\\)$"));
int pos = rx.indexIn(filter);
if (pos != -1) {
filter = filter.left(pos);
}
// ignore Gui suffix in module name
rx.setPattern(QLatin1String("Gui$"));
pos = rx.indexIn(module);
if (pos != -1) {
module = module.left(pos);
}
button->setText(QString::fromAscii("%1 (%2)").arg(filter).arg(module));
button->setObjectName(it.value());
gridLayout1->addWidget(button, index, 0, 1, 1);
group->addButton(button, index);
index++;
}
gridLayout->addWidget(groupBox, 0, 0, 1, 1);
spacerItem = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
gridLayout->addItem(spacerItem, 1, 0, 1, 1);
hboxLayout = new QHBoxLayout();
hboxLayout->setSpacing(6);
hboxLayout->setMargin(0);
spacerItem1 = new QSpacerItem(131, 31, QSizePolicy::Expanding, QSizePolicy::Minimum);
hboxLayout->addItem(spacerItem1);
okButton = new QPushButton(this);
okButton->setObjectName(QString::fromUtf8("okButton"));
okButton->setText(tr("Select"));
okButton->setEnabled(false);
hboxLayout->addWidget(okButton);
gridLayout->addLayout(hboxLayout, 2, 0, 1, 1);
// connections
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(group, SIGNAL(buttonClicked(int)), this, SLOT(onButtonClicked()));
}
SelectModule::~SelectModule()
{
}
void SelectModule::accept()
{
if (group->checkedButton())
QDialog::accept();
}
void SelectModule::reject()
{
if (group->checkedButton())
QDialog::reject();
}
void SelectModule::onButtonClicked()
{
if (group->checkedButton())
okButton->setEnabled(true);
else
okButton->setEnabled(false);
}
QString SelectModule::getModule() const
{
QAbstractButton* button = group->checkedButton();
return (button ? button->objectName() : QString());
}
SelectModule::Dict SelectModule::exportHandler(const QString& fileName, const QString& filter)
{
return exportHandler(QStringList() << fileName, filter);
}
SelectModule::Dict SelectModule::exportHandler(const QStringList& fileNames, const QString& filter)
{
// first check if there is a certain filter selected
SelectModule::Dict dict;
if (!filter.isEmpty()) {
// If an export filter is specified search directly for the module
std::map<std::string, std::string> filterList = App::GetApplication().getExportFilters();
std::map<std::string, std::string>::const_iterator it;
it = filterList.find((const char*)filter.toUtf8());
if (it != filterList.end()) {
QString module = QString::fromAscii(it->second.c_str());
for (QStringList::const_iterator it = fileNames.begin(); it != fileNames.end(); ++it) {
dict[*it] = module;
}
return dict;
}
}
// the global filter (or no filter) was selected. We now try to sort filetypes that are
// handled by more than one module and ask to the user to select one.
QMap<QString, SelectModule::Dict> filetypeHandler;
QMap<QString, QStringList > fileExtension;
for (QStringList::const_iterator it = fileNames.begin(); it != fileNames.end(); ++it) {
QFileInfo fi(*it);
QString ext = fi.completeSuffix().toLower();
std::map<std::string, std::string> filters = App::GetApplication().getExportFilters(ext.toAscii());
if (filters.empty()) {
ext = fi.suffix().toLower();
filters = App::GetApplication().getExportFilters(ext.toAscii());
}
fileExtension[ext].push_back(*it);
for (std::map<std::string, std::string>::iterator jt = filters.begin(); jt != filters.end(); ++jt)
filetypeHandler[ext][QString::fromUtf8(jt->first.c_str())] = QString::fromAscii(jt->second.c_str());
// set the default module handler
if (!filters.empty())
dict[*it] = QString::fromAscii(filters.begin()->second.c_str());
}
for (QMap<QString, SelectModule::Dict>::const_iterator it = filetypeHandler.begin();
it != filetypeHandler.end(); ++it) {
if (it.value().size() > 1) {
SelectModule dlg(it.key(),it.value(), getMainWindow());
QApplication::beep();
if (dlg.exec()) {
QString mod = dlg.getModule();
const QStringList& files = fileExtension[it.key()];
for (QStringList::const_iterator jt = files.begin(); jt != files.end(); ++jt)
dict[*jt] = mod;
}
}
}
return dict;
}
SelectModule::Dict SelectModule::importHandler(const QString& fileName, const QString& filter)
{
return importHandler(QStringList() << fileName, filter);
}
SelectModule::Dict SelectModule::importHandler(const QStringList& fileNames, const QString& filter)
{
// first check if there is a certain filter selected
SelectModule::Dict dict;
if (!filter.isEmpty()) {
// If an import filter is specified search directly for the module
std::map<std::string, std::string> filterList = App::GetApplication().getImportFilters();
std::map<std::string, std::string>::const_iterator it;
it = filterList.find((const char*)filter.toUtf8());
if (it != filterList.end()) {
QString module = QString::fromAscii(it->second.c_str());
for (QStringList::const_iterator it = fileNames.begin(); it != fileNames.end(); ++it) {
dict[*it] = module;
}
return dict;
}
}
// the global filter (or no filter) was selected. We now try to sort filetypes that are
// handled by more than one module and ask to the user to select one.
QMap<QString, SelectModule::Dict> filetypeHandler;
QMap<QString, QStringList > fileExtension;
for (QStringList::const_iterator it = fileNames.begin(); it != fileNames.end(); ++it) {
QFileInfo fi(*it);
QString ext = fi.completeSuffix().toLower();
std::map<std::string, std::string> filters = App::GetApplication().getImportFilters(ext.toAscii());
if (filters.empty()) {
ext = fi.suffix().toLower();
filters = App::GetApplication().getImportFilters(ext.toAscii());
}
fileExtension[ext].push_back(*it);
for (std::map<std::string, std::string>::iterator jt = filters.begin(); jt != filters.end(); ++jt)
filetypeHandler[ext][QString::fromUtf8(jt->first.c_str())] = QString::fromAscii(jt->second.c_str());
// set the default module handler
if (!filters.empty())
dict[*it] = QString::fromAscii(filters.begin()->second.c_str());
}
for (QMap<QString, SelectModule::Dict>::const_iterator it = filetypeHandler.begin();
it != filetypeHandler.end(); ++it) {
if (it.value().size() > 1) {
SelectModule dlg(it.key(),it.value(), getMainWindow());
QApplication::beep();
if (dlg.exec()) {
QString mod = dlg.getModule();
const QStringList& files = fileExtension[it.key()];
for (QStringList::const_iterator jt = files.begin(); jt != files.end(); ++jt)
dict[*jt] = mod;
}
}
}
return dict;
}
#include "moc_FileDialog.cpp"