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
29 using std::max;
30 using std::min;
31
32 static 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
39 void 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
63 ExprPreviewWidget::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
75 ExprFileDialog::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
113 void ExprFileDialog::handleOk() {
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
134 void ExprFileDialog::editReturnPress() {
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
165 void 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
196 void ExprFileDialog::gotoFavorites() {
197 if (_favDir != "") setDirectory(_favDir);
198 }
199
200 void 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
210 void ExprFileDialog::saveLookInEntries() { _lookInList = history(); }
211
212 void ExprFileDialog::restoreLookInEntries() { setHistory(_lookInList); }
213
214 static 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
229 QString 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
251 QStringList 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
274 QString 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
295 QString 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
305 QString 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
327 void ExprFileDialog::setPreview() {
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
337 void ExprFileDialog::resetPreview() {
338 if (_pw) _pw->reset();
339 }
340
341 void ExprFileDialog::addCheckBox(QString s) {
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
351 bool ExprFileDialog::checkBoxStatus() {
352 if (!_cb) return false;
353 return _cb->isChecked();
354 }
355
356 void ExprFileDialog::showCheckBox() {
357 if (_cb) _cb->show();
358 }
359
360 void ExprFileDialog::hideCheckBox() {
361 if (_cb) _cb->hide();
362 }
363
364 void 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
380 void ExprFileDialog::showComboBox() {
381 if (_combo) _combo->show();
382 if (_combolabel) _combolabel->show();
383 }
384
385 void ExprFileDialog::hideComboBox() {
386 if (_combo) _combo->hide();
387 if (_combolabel) _combolabel->hide();
388 }
389
390 void ExprFileDialog::selChanged(const QString& path) {
391 if (_pw) _pw->makePreview(path);
392 }
393
394 void ExprFileDialog::setButtonName(const QString& str) {
395 if (_okButton) _okButton->setText(str);
396 }
397
398 void ExprFileDialog::addSidebarShortcut(const QString& s) {
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 }
ExprFileDialog::resetPreview
void resetPreview()
Definition: ExprFileDialog.cpp:337
ExprFileDialog::_okButton
QPushButton * _okButton
Definition: ExprFileDialog.h:94
ExprPreviewWidget::ExprPreviewWidget
ExprPreviewWidget(QWidget *parent)
Definition: ExprFileDialog.cpp:63
ExprFileDialog::saveLookInEntries
void saveLookInEntries()
Definition: ExprFileDialog.cpp:210
ExprFileDialog::restoreLookInEntries
void restoreLookInEntries()
Definition: ExprFileDialog.cpp:212
ExprFileDialog::_cb
QCheckBox * _cb
Definition: ExprFileDialog.h:97
ExprFileDialog::handleOk
void handleOk()
Definition: ExprFileDialog.cpp:113
ExprFileDialog::hideComboBox
void hideComboBox()
Definition: ExprFileDialog.cpp:385
ExprFileDialog::_combo
QComboBox * _combo
Definition: ExprFileDialog.h:99
ExprFileDialog::getExistingOrNewDirectory
QString getExistingOrNewDirectory(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
Definition: ExprFileDialog.cpp:295
ExprFileDialog::getExistingDirectory
QString getExistingDirectory(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
Definition: ExprFileDialog.cpp:274
ExprFileDialog::addSidebarShortcut
void addSidebarShortcut(const QString &s)
Definition: ExprFileDialog.cpp:398
ExprFileDialog::getSaveFileName
QString getSaveFileName(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
Definition: ExprFileDialog.cpp:305
ExprFileDialog::hideCheckBox
void hideCheckBox()
Definition: ExprFileDialog.cpp:360
ExprFileDialog::_temppath
QString _temppath
Definition: ExprFileDialog.h:91
ExprFileDialog::getOpenFileNames
QStringList getOpenFileNames(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
Definition: ExprFileDialog.cpp:251
ExprFileDialog::_lookInList
QStringList _lookInList
Definition: ExprFileDialog.h:92
SeExpr2::min
double min(double x, double y)
Definition: ExprBuiltins.h:43
ExprFileDialog::_pw
ExprPreviewWidget * _pw
Definition: ExprFileDialog.h:96
folder_fav
static const char * folder_fav[]
Definition: ExprFileDialog.cpp:32
ExprFileDialog::_workingDirectory
QString _workingDirectory
Definition: ExprFileDialog.h:90
it
you may not use this file except in compliance with the License and the following modification to it
Definition: license.txt:10
ExprPreviewWidget::makePreview
void makePreview(const QString &path)
Definition: ExprFileDialog.cpp:39
SeExpr2::max
double max(double x, double y)
Definition: ExprBuiltins.h:42
ExprFileDialog::_createDir
bool _createDir
Definition: ExprFileDialog.h:95
ExprFileDialog::showComboBox
void showComboBox()
Definition: ExprFileDialog.cpp:380
ExprFileDialog::resetDir
void resetDir()
Definition: ExprFileDialog.h:84
ExprFileDialog::_combolabel
QLabel * _combolabel
Definition: ExprFileDialog.h:98
ExprPreviewWidget::reset
void reset()
Definition: ExprFileDialog.h:36
ExprFileDialog::addFavoritesButton
void addFavoritesButton(QString dirname, QString linkname, QString linkdir)
Definition: ExprFileDialog.cpp:165
ExprFileDialog::setButtonName
void setButtonName(const QString &str)
Definition: ExprFileDialog.cpp:394
ExprFileDialog::getOpenFileName
QString getOpenFileName(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
Definition: ExprFileDialog.cpp:229
ExprFileDialog::selChanged
void selChanged(const QString &path)
Definition: ExprFileDialog.cpp:390
ExprFileDialog::addCheckBox
void addCheckBox(QString s)
Definition: ExprFileDialog.cpp:341
ExprFileDialog::addLookInEntries
void addLookInEntries(QStringList paths)
Definition: ExprFileDialog.cpp:200
ExprFileDialog::addComboBox
void addComboBox(QString s, QStringList sl)
Definition: ExprFileDialog.cpp:364
ExprFileDialog::showCheckBox
void showCheckBox()
Definition: ExprFileDialog.cpp:356
makeFiltersList
static QStringList makeFiltersList(const QString &filter)
Definition: ExprFileDialog.cpp:214
ExprFileDialog::_nameEdit
QLineEdit * _nameEdit
Definition: ExprFileDialog.h:93
ExprFileDialog::editReturnPress
void editReturnPress()
Definition: ExprFileDialog.cpp:134
ExprPreviewWidget::_pm
QLabel * _pm
Definition: ExprFileDialog.h:39
ExprFileDialog.h
ExprFileDialog::ExprFileDialog
ExprFileDialog(QWidget *parent=0)
Definition: ExprFileDialog.cpp:75
ExprFileDialog::_favDir
QString _favDir
Definition: ExprFileDialog.h:90
ExprFileDialog::setPreview
void setPreview()
Definition: ExprFileDialog.cpp:327
ExprPreviewWidget
Definition: ExprFileDialog.h:32
ExprFileDialog::gotoFavorites
void gotoFavorites()
Definition: ExprFileDialog.cpp:196
ExprFileDialog::checkBoxStatus
bool checkBoxStatus()
Definition: ExprFileDialog.cpp:351