Rev 24706 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
24706 | jghali | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
24444 | jghali | 7 | |
24706 | jghali | 8 | #include <cstdlib> |
9 | |||
10 | #include <QDebug> |
||
11 | #include <QStandardPaths> |
||
12 | #include <QLayout> |
||
13 | #include <QLineEdit> |
||
14 | #include <QListView> |
||
15 | #include <QPushButton> |
||
16 | #include <QStringList> |
||
17 | #include <QUrl> |
||
18 | |||
19 | #include "filedialogeventcatcher.h" |
||
20 | #include "scfilewidget.h" |
||
21 | |||
22 | |||
23 | ScFileWidget::ScFileWidget(QWidget * parent) : QFileDialog(parent, Qt::Widget) |
||
24 | { |
||
25 | setOption(QFileDialog::DontUseNativeDialog); |
||
26 | setSizeGripEnabled(false); |
||
27 | setModal(false); |
||
28 | setViewMode(QFileDialog::List); |
||
29 | setWindowFlags(Qt::Widget); |
||
30 | |||
24444 | jghali | 31 | // The margins' content should be set both on the widget and its layout. |
32 | setContentsMargins(0, 0, 0, 0); |
||
24706 | jghali | 33 | layout()->setContentsMargins(0, 0, 0, 0); |
24665 | jghali | 34 | |
24706 | jghali | 35 | #ifdef Q_OS_MAC |
36 | QList<QUrl> urls; |
||
37 | QUrl computer(QUrl::fromLocalFile(QLatin1String(""))); |
||
38 | if (!urls.contains(computer)) |
||
39 | urls << computer; |
||
40 | QUrl volumes(QUrl::fromLocalFile("/Volumes")); |
||
41 | if (!urls.contains(volumes)) |
||
42 | urls << volumes; |
||
25150 | craig | 43 | QUrl home(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::HomeLocation))); |
44 | if (!urls.contains(home)) |
||
45 | urls << home; |
||
46 | QUrl documents(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation))); |
||
47 | if (!urls.contains(documents)) |
||
48 | urls << documents; |
||
24706 | jghali | 49 | QUrl dt(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation))); |
50 | if (!urls.contains(dt)) |
||
51 | urls << dt; |
||
52 | setSidebarUrls(urls); |
||
53 | #endif |
||
54 | |||
55 | FileDialogEventCatcher* keyCatcher = new FileDialogEventCatcher(this); |
||
56 | QList<QListView *> childListViews = findChildren<QListView *>(); |
||
57 | for (QListView * lvi : childListViews) |
||
58 | lvi->installEventFilter(keyCatcher); |
||
59 | connect(keyCatcher, SIGNAL(escapePressed()), this, SLOT(reject())); |
||
60 | connect(keyCatcher, SIGNAL(dropLocation(QString)), this, SLOT(locationDropped(QString))); |
||
61 | connect(keyCatcher, SIGNAL(desktopPressed()), this, SLOT(gotoDesktopDirectory())); |
||
62 | connect(keyCatcher, SIGNAL(homePressed()), this, SLOT(gotoHomeDirectory())); |
||
63 | connect(keyCatcher, SIGNAL(parentPressed()), this, SLOT(gotoParentDirectory())); |
||
64 | connect(keyCatcher, SIGNAL(enterSelectedPressed()), this, SLOT(gotoSelectedDirectory())); |
||
65 | |||
66 | QList<QPushButton *> childPushButtons = findChildren<QPushButton *>(); |
||
67 | for (QPushButton* pb : childPushButtons) |
||
68 | pb->setVisible(false); |
||
69 | setMinimumSize(QSize(480, 310)); |
||
70 | setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); |
||
71 | } |
||
72 | |||
24665 | jghali | 73 | void ScFileWidget::forceDoubleClickActivation(bool force) |
74 | { |
||
75 | // Hack to make the previews in our file dialogs usable again, |
||
76 | // needed e.g on OpenSuse KDE. Otherwise file would open on first |
||
77 | // click, leaving user no time to see preview. |
||
78 | if (m_forceDoubleClickActivation == force) |
||
24706 | jghali | 79 | return; |
80 | |||
81 | if (force) |
||
82 | setStyleSheet(QStringLiteral("QAbstractItemView { activate-on-singleclick: 0; }")); |
||
83 | else |
||
84 | setStyleSheet(QString()); |
||
85 | m_forceDoubleClickActivation = force; |
||
86 | } |
||
87 | |||
88 | QString ScFileWidget::selectedFile() |
||
89 | { |
||
90 | QStringList l(selectedFiles()); |
||
91 | if (l.count() == 0) |
||
92 | return QString(); |
||
93 | return l.at(0); |
||
94 | } |
||
95 | |||
96 | void ScFileWidget::locationDropped(const QString& fileUrl) |
||
97 | { |
||
98 | QFileInfo fi(fileUrl); |
||
99 | if (fi.isDir()) |
||
100 | { |
||
101 | setDirectory(fi.absoluteFilePath()); |
||
102 | return; |
||
103 | } |
||
104 | |||
105 | QString absFilePath = fi.absolutePath(); |
||
106 | QString fileName = fi.fileName(); |
||
107 | |||
108 | setDirectory(absFilePath); |
||
109 | selectFile(fileName); |
||
110 | } |
||
111 | |||
112 | void ScFileWidget::gotoParentDirectory() |
||
113 | { |
||
114 | QDir d(directory()); |
||
115 | d.cdUp(); |
||
116 | setDirectory(d); |
||
117 | } |
||
118 | |||
119 | void ScFileWidget::gotoSelectedDirectory() |
||
120 | { |
||
121 | QStringList s(selectedFiles()); |
||
25150 | craig | 122 | if (s.isEmpty()) |
123 | return; |
||
124 | QFileInfo fi(s.first()); |
||
125 | // qDebug()<<s.first()<<fi.absoluteFilePath(); |
||
126 | if (fi.isDir()) |
||
127 | setDirectory(fi.absoluteFilePath()); |
||
24706 | jghali | 128 | } |
129 | |||
130 | void ScFileWidget::gotoDesktopDirectory() |
||
131 | { |
||
25150 | craig | 132 | QString dp = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); |
24706 | jghali | 133 | QFileInfo fi(dp); |
134 | if (fi.exists()) |
||
135 | setDirectory(dp); |
||
136 | } |
||
137 | |||
138 | void ScFileWidget::gotoHomeDirectory() |
||
139 | { |
||
25150 | craig | 140 | QString dp = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); |
24706 | jghali | 141 | QFileInfo fi(dp); |
142 | if (fi.exists()) |
||
143 | setDirectory(dp); |
||
144 | } |
||
145 |