SeExpr
ExprFileDialog.cpp
Go to the documentation of this file.
1/*
2* Copyright Disney Enterprises, Inc. All rights reserved.
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License
6* and the following modification to it: Section 6 Trademarks.
7* deleted and replaced with:
8*
9* 6. Trademarks. This License does not grant permission to use the
10* trade names, trademarks, service marks, or product names of the
11* Licensor and its affiliates, except as required for reproducing
12* the content of the NOTICE file.
13*
14* You may obtain a copy of the License at
15* http://www.apache.org/licenses/LICENSE-2.0
16*/
17
18// NOTE: This is based on Dan's paint3d FileDialog
19
20#include "ExprFileDialog.h"
21
22#include <QToolButton>
23#include <QPalette>
24#include <QMenu>
25#include <QTimer>
26#include <QUrl>
27#include <iostream>
28
29using std::max;
30using std::min;
31
32static const char* folder_fav[] = {"17 16 4 1", "# c #000000", ". c None", "a c #ffff98",
33 "b c #cc0000", ".................", ".................", "...#####.........",
34 "..#aaaaa#........", ".###############.", ".#aaaaaaaaaaaaa#.", ".#aaaa##a##aaaa#.",
35 ".#aaa#bb#bb#aaa#.", ".#aaa#bbbbb#aaa#.", ".#aaa#bbbbb#aaa#.", ".#aaaa#bbb#aaaa#.",
36 ".#aaaaa#b#aaaaa#.", ".#aaaaaa#aaaaaa#.", ".#aaaaaaaaaaaaa#.", ".###############.",
37 "................."};
38
39void ExprPreviewWidget::makePreview(const QString& path) {
40 QFileInfo fi(path);
41
42 if (fi.isDir()) {
43 QString s = fi.absoluteFilePath() + "/preview.tif";
44 if (!QFile::exists(s)) s = fi.absoluteFilePath() + "/preview.png";
45 if (!QFile::exists(s)) _pm->setPixmap(QPixmap()); // nothing to preview
46
47 QPixmap pix(s);
48 if (!pix.isNull())
49 _pm->setPixmap(pix);
50 else
51 _pm->setPixmap(QPixmap());
52 } else if (fi.exists()) {
53 QImage img(fi.absoluteFilePath());
54 if (!img.isNull())
55 _pm->setPixmap(QPixmap::fromImage(img.scaled(128, 128, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
56 else
57 _pm->setPixmap(QPixmap());
58 } else
59 _pm->setPixmap(QPixmap());
60 _pm->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
61}
62
63ExprPreviewWidget::ExprPreviewWidget(QWidget* parent) : QWidget(parent) {
64 _pm = new QLabel(this);
65 _pm->setFrameStyle(QFrame::StyledPanel);
66 _pm->setBackgroundRole(QPalette::Base);
67 _pm->setAutoFillBackground(true);
68 QVBoxLayout* layout = new QVBoxLayout;
69 layout->setSpacing(0);
70 layout->setMargin(0);
71 layout->addWidget(_pm);
72 setLayout(layout);
73}
74
75ExprFileDialog::ExprFileDialog(QWidget* parent) : QFileDialog(parent) {
76 // QStringList pathlist(QString(globals.startpath.c_str()));
77 // addLookInEntries(pathlist);
78
79 _nameEdit = 0;
80 _okButton = 0;
81
82 // disconnect broken return press handling (mishandles new directory names)
83 QList<QLineEdit*> lineedits = findChildren<QLineEdit*>(QRegExp());
84 if (lineedits.size()) _nameEdit = (QLineEdit*)lineedits.at(0);
85 if (_nameEdit) {
86 _nameEdit->disconnect(SIGNAL(returnPressed()));
87 connect(_nameEdit, SIGNAL(returnPressed()), SLOT(editReturnPress()));
88 }
89
90 // connect custom ok clicked handler
91 QList<QPushButton*> myWidgets = findChildren<QPushButton*>(QRegExp());
92 for (int w = 0; w < myWidgets.size(); w++) {
93 QPushButton* item = (QPushButton*)myWidgets.at(w);
94 if (item->text().contains("Open")) _okButton = item;
95 }
96 if (_okButton) connect(_okButton, SIGNAL(clicked()), SLOT(handleOk()));
97
98 connect(this, SIGNAL(currentChanged(const QString&)), this, SLOT(selChanged(const QString&)));
99
100 // don't create missing directories by default
101 _createDir = 0;
102 _pw = 0;
103 _favDir = "";
104 _combo = 0;
105 _combolabel = 0;
106 _cb = 0;
107 _temppath = "";
108
109 setMinimumWidth(680);
110 resize(840, 440);
111}
112
114 if (fileMode() != QFileDialog::DirectoryOnly) return;
115 QString entry = _nameEdit->text();
116 if (entry == "") return;
117
118 // create directory if needed
119 if (_createDir) {
120 QDir d(directory());
121 if (!d.exists(entry)) {
122 if (d.mkdir(entry)) {
123 _temppath = directory().absolutePath();
124 setDirectory(_temppath + "/" + entry);
125 _nameEdit->setText("");
126 if (_okButton) _okButton->animateClick(); // retry click to accept entry
127
128 QTimer::singleShot(200, this, SLOT(resetDir()));
129 }
130 }
131 }
132}
133
135 if (!_nameEdit) return;
136
137 QString str = _nameEdit->text();
138 if (str.contains('/')) {
139 QDir d;
140 if (d.cd(str)) {
141 setDirectory(str);
142 _nameEdit->setText("");
143 } else {
144 int slashcount = str.count('/');
145
146 QString foundDir = "";
147 for (int i = 0; i < slashcount; i++) {
148 QString section = str.section('/', 0, i);
149 if (d.cd(section)) foundDir = section;
150 }
151 if (foundDir.length()) {
152 setDirectory(foundDir);
153 QString remainder = str.right(str.length() - (foundDir.length() + 1));
154 _nameEdit->setText(remainder);
155 }
156
157 if (d.cd(str)) setDirectory(str);
158 }
159 } else if (fileMode() == QFileDialog::DirectoryOnly)
160 handleOk();
161 else
162 accept();
163}
164
165void ExprFileDialog::addFavoritesButton(QString dirname, QString linkname, QString linkdir) {
166 QGridLayout* layout = findChild<QGridLayout*>("gridLayout");
167 if (!layout) return;
168
169 QDir d;
170
171 std::string favlocation = getenv("HOME");
172 favlocation += "/paint3d/favorites/";
173
174 QString dirpath = QString::fromStdString(favlocation);
175 if (!d.cd(dirpath)) d.mkpath(dirpath);
176 dirpath += dirname;
177 if (!d.cd(dirpath)) d.mkpath(dirpath);
178
179 if (!(linkdir.isEmpty() || linkname.isEmpty())) {
180 if (!QFile::exists(dirpath + linkname)) QFile::link(linkdir, dirpath + linkname);
181 }
182
183 _favDir = dirpath;
184
185 static QPixmap folderFav(folder_fav);
186 QToolButton* fav = new QToolButton(this);
187 fav->setFixedSize(18, 18);
188 fav->setIcon(folderFav);
189 fav->setToolTip("Favorites");
190
191 layout->addWidget(fav, 0, 3);
192
193 connect(fav, SIGNAL(clicked()), SLOT(gotoFavorites()));
194}
195
197 if (_favDir != "") setDirectory(_favDir);
198}
199
200void ExprFileDialog::addLookInEntries(QStringList paths) {
201 if (paths.isEmpty()) return;
202
203 QStringList h = history();
204 for (QStringList::Iterator it = paths.begin(); it != paths.end(); ++it) {
205 if (!h.contains(*it)) h.push_back(*it);
206 }
207 setHistory(h);
208}
209
211
213
214static QStringList makeFiltersList(const QString& filter) {
215 if (filter.isEmpty()) return QStringList();
216
217 int i = filter.indexOf(";;", 0);
218 QString sep(";;");
219 if (i == -1) {
220 if (filter.indexOf("\n", 0) != -1) {
221 sep = "\n";
222 i = filter.indexOf(sep, 0);
223 }
224 }
225
226 return filter.split(sep);
227}
228
229QString ExprFileDialog::getOpenFileName(const QString& caption, const QString& startWith, const QString& filter) {
230 if (!filter.isEmpty()) {
231 QStringList filters = makeFiltersList(filter);
232 setNameFilters(filters);
233 }
234
235 if (!startWith.isEmpty()) setDirectory(startWith);
236 if (!caption.isNull()) setWindowTitle(caption);
237 setFileMode(QFileDialog::ExistingFile);
238 setAcceptMode(QFileDialog::AcceptOpen);
239 selectFile("");
240
241 QString result;
242 if (exec() == QDialog::Accepted) {
243 result = selectedFiles().first();
244 _workingDirectory = directory().absolutePath();
245 }
246 resetPreview();
247
248 return result;
249}
250
251QStringList ExprFileDialog::getOpenFileNames(const QString& caption, const QString& startWith, const QString& filter) {
252 if (!filter.isEmpty()) {
253 QStringList filters = makeFiltersList(filter);
254 setNameFilters(filters);
255 }
256
257 if (!startWith.isEmpty()) setDirectory(startWith);
258 if (!caption.isNull()) setWindowTitle(caption);
259 setFileMode(QFileDialog::ExistingFiles);
260 setAcceptMode(QFileDialog::AcceptOpen);
261 selectFile("");
262
263 QString result;
264 QStringList lst;
265 if (exec() == QDialog::Accepted) {
266 lst = selectedFiles();
267 _workingDirectory = directory().absolutePath();
268 }
269 resetPreview();
270
271 return lst;
272}
273
274QString ExprFileDialog::getExistingDirectory(const QString& caption, const QString& startWith, const QString& filter) {
275 if (!filter.isEmpty()) {
276 QStringList filters = makeFiltersList(filter);
277 setNameFilters(filters);
278 }
279
280 if (!startWith.isEmpty()) setDirectory(startWith);
281 if (!caption.isNull()) setWindowTitle(caption);
282 setFileMode(QFileDialog::DirectoryOnly);
283 selectFile("");
284
285 QString result;
286 if (exec() == QDialog::Accepted) {
287 result = selectedFiles().first();
288 _workingDirectory = directory().absolutePath();
289 }
290 resetPreview();
291
292 return result;
293}
294
295QString ExprFileDialog::getExistingOrNewDirectory(const QString& caption,
296 const QString& startWith,
297 const QString& filter) {
298 _createDir = 1;
299 QString result = getExistingDirectory(caption, startWith, filter);
300 _createDir = 0;
301 resetPreview();
302 return result;
303}
304
305QString ExprFileDialog::getSaveFileName(const QString& caption, const QString& startWith, const QString& filter) {
306 if (!filter.isEmpty()) {
307 QStringList filters = makeFiltersList(filter);
308 setNameFilters(filters);
309 }
310
311 if (!startWith.isEmpty()) setDirectory(startWith);
312 if (!caption.isNull()) setWindowTitle(caption);
313 setFileMode(QFileDialog::AnyFile);
314 setAcceptMode(QFileDialog::AcceptSave);
315 selectFile("");
316
317 QString result;
318 if (exec() == QDialog::Accepted) {
319 result = selectedFiles().first();
320 _workingDirectory = directory().absolutePath();
321 }
322 resetPreview();
323
324 return result;
325}
326
328 QGridLayout* layout = findChild<QGridLayout*>("gridLayout");
329 if (!layout) return;
330
331 _pw = new ExprPreviewWidget(this);
332 _pw->setFixedWidth(160);
333 _pw->setMinimumHeight(160);
334 layout->addWidget(_pw, 1, 3);
335}
336
338 if (_pw) _pw->reset();
339}
340
342 QGridLayout* layout = findChild<QGridLayout*>("gridLayout");
343 if (!layout) return;
344
345 _cb = new QCheckBox(s, this);
346 _cb->setChecked(false);
347
348 layout->addWidget(_cb, 4, _combo ? 2 : 0);
349}
350
352 if (!_cb) return false;
353 return _cb->isChecked();
354}
355
357 if (_cb) _cb->show();
358}
359
361 if (_cb) _cb->hide();
362}
363
364void ExprFileDialog::addComboBox(QString s, QStringList sl) {
365 QGridLayout* layout = findChild<QGridLayout*>("gridLayout");
366 if (!layout) return;
367
368 _combolabel = new QLabel(s, this);
369 _combolabel->setFixedWidth(58);
370 _combo = new QComboBox(this);
371 _combo->setEditable(true);
372 _combo->setFixedWidth(160);
373 for (QStringList::Iterator it = sl.begin(); it != sl.end(); ++it) _combo->addItem(*it);
374
375 int rownum = layout->rowCount();
376 layout->addWidget(_combo, rownum, 1);
377 layout->addWidget(_combolabel, rownum, 0);
378}
379
381 if (_combo) _combo->show();
382 if (_combolabel) _combolabel->show();
383}
384
386 if (_combo) _combo->hide();
387 if (_combolabel) _combolabel->hide();
388}
389
390void ExprFileDialog::selChanged(const QString& path) {
391 if (_pw) _pw->makePreview(path);
392}
393
394void ExprFileDialog::setButtonName(const QString& str) {
395 if (_okButton) _okButton->setText(str);
396}
397
399 QList<QUrl> urls = sidebarUrls();
400 QUrl url = QUrl::fromLocalFile(s);
401 if (url.isValid() && QFile::exists(s)) {
402 urls.push_back(url);
403 setSidebarUrls(urls);
404 }
405}
static const char * folder_fav[]
static QStringList makeFiltersList(const QString &filter)
QString _workingDirectory
void addLookInEntries(QStringList paths)
QString getExistingOrNewDirectory(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
QString getOpenFileName(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
QStringList getOpenFileNames(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
ExprFileDialog(QWidget *parent=0)
void addCheckBox(QString s)
ExprPreviewWidget * _pw
QComboBox * _combo
QLabel * _combolabel
void setButtonName(const QString &str)
void addSidebarShortcut(const QString &s)
QString getExistingDirectory(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
void addComboBox(QString s, QStringList sl)
QLineEdit * _nameEdit
QStringList _lookInList
QPushButton * _okButton
QCheckBox * _cb
QString getSaveFileName(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
void addFavoritesButton(QString dirname, QString linkname, QString linkdir)
void selChanged(const QString &path)
void makePreview(const QString &path)
ExprPreviewWidget(QWidget *parent)
you may not use this file except in compliance with the License and the following modification to it
Definition license.txt:10