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 <QtGui/QToolButton>
23 #include <QtGui/QPalette>
24 #include <QtGui/QMenu>
25 #include <QtCore/QTimer>
26 #include <QtCore/QUrl>
27 #include <iostream>
28 
29 using std::max;
30 using std::min;
31 
32 static const char* folder_fav[]={
33  "17 16 4 1",
34  "# c #000000",
35  ". c None",
36  "a c #ffff98",
37  "b c #cc0000",
38  ".................",
39  ".................",
40  "...#####.........",
41  "..#aaaaa#........",
42  ".###############.",
43  ".#aaaaaaaaaaaaa#.",
44  ".#aaaa##a##aaaa#.",
45  ".#aaa#bb#bb#aaa#.",
46  ".#aaa#bbbbb#aaa#.",
47  ".#aaa#bbbbb#aaa#.",
48  ".#aaaa#bbb#aaaa#.",
49  ".#aaaaa#b#aaaaa#.",
50  ".#aaaaaa#aaaaaa#.",
51  ".#aaaaaaaaaaaaa#.",
52  ".###############.",
53  "................."};
54 
55 
56 void ExprPreviewWidget::makePreview(const QString& path)
57 {
58  QFileInfo fi( path );
59 
60  if ( fi.isDir() )
61  {
62  QString s = fi.absoluteFilePath()+"/preview.tif";
63  if (!QFile::exists(s)) s = fi.absoluteFilePath()+"/preview.png";
64  if (!QFile::exists(s)) _pm->setPixmap( QPixmap()); //nothing to preview
65 
66  QPixmap pix( s );
67  if ( !pix.isNull() ) _pm->setPixmap( pix );
68  else _pm->setPixmap( QPixmap() );
69  }
70  else if (fi.exists())
71  {
72  QImage img(fi.absoluteFilePath());
73  if(!img.isNull())
74  _pm->setPixmap(QPixmap::fromImage(img.scaled(128,128,Qt::KeepAspectRatio,Qt::SmoothTransformation)));
75  else
76  _pm->setPixmap( QPixmap() );
77  }
78  else _pm->setPixmap( QPixmap() );
79  _pm->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
80 }
81 
82 
84  : QWidget( parent )
85 {
86  _pm = new QLabel( this );
87  _pm->setFrameStyle( QFrame::StyledPanel );
88  _pm->setBackgroundRole( QPalette::Base );
89  _pm->setAutoFillBackground(true);
90  QVBoxLayout *layout = new QVBoxLayout;
91  layout->setSpacing( 0 );
92  layout->setMargin( 0 );
93  layout->addWidget(_pm);
94  setLayout(layout);
95 }
96 
97 ExprFileDialog::ExprFileDialog(QWidget* parent) : QFileDialog(parent)
98 {
99  //QStringList pathlist(QString(globals.startpath.c_str()));
100  //addLookInEntries(pathlist);
101 
102  _nameEdit = 0;
103  _okButton = 0;
104 
105  // disconnect broken return press handling (mishandles new directory names)
106  QList<QLineEdit*> lineedits = findChildren<QLineEdit*>(QRegExp());
107  if (lineedits.size()) _nameEdit = (QLineEdit*)lineedits.at(0);
108  if (_nameEdit) {
109  _nameEdit->disconnect(SIGNAL(returnPressed()));
110  connect(_nameEdit, SIGNAL(returnPressed()), SLOT(editReturnPress()));
111  }
112 
113  // connect custom ok clicked handler
114  QList<QPushButton*> myWidgets = findChildren<QPushButton*>(QRegExp());
115  for (int w = 0; w < myWidgets.size(); w++)
116  {
117  QPushButton* item = (QPushButton*)myWidgets.at(w);
118  if (item->text().contains("Open")) _okButton = item;
119  }
120  if (_okButton) connect(_okButton, SIGNAL(clicked()), SLOT(handleOk()));
121 
122  connect(this, SIGNAL(currentChanged(const QString&)),
123  this, SLOT(selChanged(const QString&)));
124 
125  // don't create missing directories by default
126  _createDir = 0;
127  _pw = 0;
128  _favDir = "";
129  _combo = 0;
130  _combolabel = 0;
131  _cb = 0;
132  _temppath = "";
133 
134  setMinimumWidth(680);
135  resize(840,440);
136 }
137 
139 {
140  if (fileMode() != QFileDialog::DirectoryOnly) return;
141  QString entry = _nameEdit->text();
142  if (entry == "") return;
143 
144  // create directory if needed
145  if (_createDir) {
146  QDir d(directory());
147  if (!d.exists(entry)) {
148  if (d.mkdir(entry)) {
149  _temppath = directory().absolutePath();
150  setDirectory(_temppath+"/"+entry);
151  _nameEdit->setText("");
152  if (_okButton)
153  _okButton->animateClick(); // retry click to accept entry
154 
155  QTimer::singleShot( 200, this, SLOT(resetDir()) );
156  }
157  }
158  }
159 }
160 
162 {
163  if (!_nameEdit) return;
164 
165  QString str = _nameEdit->text();
166  if (str.contains('/'))
167  {
168  QDir d;
169  if (d.cd(str))
170  {
171  setDirectory(str);
172  _nameEdit->setText("");
173  }
174  else
175  {
176  int slashcount = str.count('/');
177 
178  QString foundDir = "";
179  for (int i=0; i<slashcount; i++)
180  {
181  QString section = str.section('/', 0, i);
182  if (d.cd(section)) foundDir = section;
183  }
184  if (foundDir.length())
185  {
186  setDirectory(foundDir);
187  QString remainder = str.right(str.length()-(foundDir.length()+1));
188  _nameEdit->setText(remainder);
189  }
190 
191  if (d.cd(str)) setDirectory(str);
192 
193  }
194  }
195  else if (fileMode()==QFileDialog::DirectoryOnly) handleOk();
196  else accept();
197 }
198 
199 void ExprFileDialog::addFavoritesButton(QString dirname, QString linkname,
200  QString linkdir)
201 {
202  QGridLayout *layout = findChild<QGridLayout *>("gridLayout");
203  if (!layout) return;
204 
205  QDir d;
206 
207  std::string favlocation = getenv("HOME");
208  favlocation += "/paint3d/favorites/";
209 
210  QString dirpath = QString::fromStdString(favlocation);
211  if (!d.cd(dirpath)) d.mkpath(dirpath);
212  dirpath += dirname;
213  if (!d.cd(dirpath)) d.mkpath(dirpath);
214 
215  if (!(linkdir.isEmpty() || linkname.isEmpty()))
216  {
217  if (!QFile::exists(dirpath+linkname))
218  QFile::link(linkdir,dirpath+linkname);
219  }
220 
221  _favDir = dirpath;
222 
223  static QPixmap folderFav( folder_fav );
224  QToolButton* fav = new QToolButton(this);
225  fav->setFixedSize(18,18);
226  fav->setIcon(folderFav);
227  fav->setToolTip("Favorites");
228 
229  layout->addWidget(fav,0,3);
230 
231  connect(fav, SIGNAL(clicked()), SLOT(gotoFavorites()));
232 }
233 
235 {
236  if (_favDir!="") setDirectory( _favDir );
237 }
238 
239 void ExprFileDialog::addLookInEntries(QStringList paths)
240 {
241  if ( paths.isEmpty ()) return;
242 
243  QStringList h = history();
244  for (QStringList::Iterator it=paths.begin(); it!=paths.end(); ++it)
245  {
246  if (!h.contains(*it)) h.push_back(*it);
247  }
248  setHistory(h);
249 }
250 
252 {
253  _lookInList = history();
254 }
255 
257 {
258  setHistory(_lookInList);
259 }
260 
261 static QStringList makeFiltersList( const QString &filter )
262 {
263  if ( filter.isEmpty() )
264  return QStringList();
265 
266  int i = filter.indexOf( ";;", 0 );
267  QString sep( ";;" );
268  if ( i == -1 ) {
269  if ( filter.indexOf( "\n", 0 ) != -1 ) {
270  sep = "\n";
271  i = filter.indexOf( sep, 0 );
272  }
273  }
274 
275  return filter.split( sep );
276 }
277 
278 QString ExprFileDialog::getOpenFileName( const QString& caption,
279  const QString& startWith, const QString& filter )
280 {
281  if ( !filter.isEmpty() )
282  {
283  QStringList filters = makeFiltersList( filter );
284  setNameFilters( filters );
285  }
286 
287  if ( !startWith.isEmpty() ) setDirectory( startWith );
288  if ( !caption.isNull() ) setWindowTitle( caption );
289  setFileMode( QFileDialog::ExistingFile );
290  setAcceptMode( QFileDialog::AcceptOpen );
291  selectFile("");
292 
293  QString result;
294  if ( exec()==QDialog::Accepted )
295  {
296  result = selectedFiles().first();
297  _workingDirectory = directory().absolutePath();
298  }
299  resetPreview();
300 
301  return result;
302 }
303 
304 QStringList ExprFileDialog::getOpenFileNames( const QString& caption,
305  const QString& startWith, const QString& filter )
306 {
307  if ( !filter.isEmpty() )
308  {
309  QStringList filters = makeFiltersList( filter );
310  setNameFilters( filters );
311  }
312 
313  if ( !startWith.isEmpty() ) setDirectory( startWith );
314  if ( !caption.isNull() ) setWindowTitle( caption );
315  setFileMode( QFileDialog::ExistingFiles );
316  setAcceptMode( QFileDialog::AcceptOpen );
317  selectFile("");
318 
319  QString result;
320  QStringList lst;
321  if ( exec()==QDialog::Accepted )
322  {
323  lst = selectedFiles();
324  _workingDirectory = directory().absolutePath();
325  }
326  resetPreview();
327 
328  return lst;
329 }
330 
331 QString ExprFileDialog::getExistingDirectory( const QString& caption,
332  const QString& startWith, const QString& filter )
333 {
334  if ( !filter.isEmpty() )
335  {
336  QStringList filters = makeFiltersList( filter );
337  setNameFilters( filters );
338  }
339 
340  if ( !startWith.isEmpty() ) setDirectory( startWith );
341  if ( !caption.isNull() ) setWindowTitle( caption );
342  setFileMode( QFileDialog::DirectoryOnly );
343  selectFile("");
344 
345  QString result;
346  if ( exec()==QDialog::Accepted )
347  {
348  result = selectedFiles().first();
349  _workingDirectory = directory().absolutePath();
350  }
351  resetPreview();
352 
353  return result;
354 }
355 
356 QString ExprFileDialog::getExistingOrNewDirectory( const QString& caption,
357  const QString& startWith, const QString& filter )
358 {
359  _createDir = 1;
360  QString result = getExistingDirectory(caption, startWith, filter);
361  _createDir = 0;
362  resetPreview();
363  return result;
364 }
365 
366 QString ExprFileDialog::getSaveFileName( const QString& caption,
367  const QString& startWith, const QString& filter )
368 {
369  if ( !filter.isEmpty() )
370  {
371  QStringList filters = makeFiltersList( filter );
372  setNameFilters( filters );
373  }
374 
375  if ( !startWith.isEmpty() ) setDirectory( startWith );
376  if ( !caption.isNull() ) setWindowTitle( caption );
377  setFileMode( QFileDialog::AnyFile );
378  setAcceptMode( QFileDialog::AcceptSave );
379  selectFile("");
380 
381  QString result;
382  if ( exec()==QDialog::Accepted )
383  {
384  result = selectedFiles().first();
385  _workingDirectory = directory().absolutePath();
386  }
387  resetPreview();
388 
389  return result;
390 }
391 
392 
394 {
395  QGridLayout *layout = findChild<QGridLayout *>("gridLayout");
396  if (!layout) return;
397 
398  _pw = new ExprPreviewWidget( this );
399  _pw->setFixedWidth(160);
400  _pw->setMinimumHeight(160);
401  layout->addWidget(_pw,1,3);
402 }
403 
405 {
406  if (_pw) _pw->reset();
407 }
408 
410 {
411  QGridLayout *layout = findChild<QGridLayout *>("gridLayout");
412  if (!layout) return;
413 
414  _cb = new QCheckBox( s, this );
415  _cb->setChecked(false);
416 
417  layout->addWidget(_cb, 4, _combo ? 2 : 0);
418 }
419 
421 {
422  if (!_cb) return false;
423  return _cb->isChecked();
424 }
425 
427 {
428  if (_cb) _cb->show();
429 }
430 
432 {
433  if (_cb) _cb->hide();
434 }
435 
436 void ExprFileDialog::addComboBox(QString s, QStringList sl)
437 {
438  QGridLayout *layout = findChild<QGridLayout *>("gridLayout");
439  if (!layout) return;
440 
441  _combolabel = new QLabel( s, this );
442  _combolabel->setFixedWidth(58);
443  _combo = new QComboBox( this );
444  _combo->setEditable(true);
445  _combo->setFixedWidth(160);
446  for ( QStringList::Iterator it = sl.begin(); it != sl.end(); ++it )
447  _combo->addItem(*it);
448 
449  int rownum = layout->rowCount();
450  layout->addWidget(_combo,rownum,1);
451  layout->addWidget(_combolabel,rownum,0);
452 }
453 
455 {
456  if (_combo) _combo->show();
457  if (_combolabel) _combolabel->show();
458 }
459 
461 {
462  if (_combo) _combo->hide();
463  if (_combolabel) _combolabel->hide();
464 }
465 
466 void ExprFileDialog::selChanged(const QString& path)
467 {
468  if (_pw) _pw->makePreview(path);
469 }
470 
471 void ExprFileDialog::setButtonName(const QString& str)
472 {
473  if (_okButton) _okButton->setText( str );
474 }
475 
477 {
478  QList<QUrl> urls = sidebarUrls();
479  QUrl url = QUrl::fromLocalFile(s);
480  if (url.isValid() && QFile::exists(s))
481  {
482  urls.push_back(url);
483  setSidebarUrls(urls);
484  }
485 }
void selChanged(const QString &path)
void addFavoritesButton(QString dirname, QString linkname, QString linkdir)
void addCheckBox(QString s)
QString _workingDirectory
QString getExistingOrNewDirectory(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
static const char * folder_fav[]
QLabel * _combolabel
static QStringList makeFiltersList(const QString &filter)
void addLookInEntries(QStringList paths)
ExprPreviewWidget * _pw
QString getExistingDirectory(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
void addComboBox(QString s, QStringList sl)
double max(double x, double y)
Definition: ExprBuiltins.h:42
void makePreview(const QString &path)
void restoreLookInEntries()
QStringList getOpenFileNames(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
double min(double x, double y)
Definition: ExprBuiltins.h:43
void addSidebarShortcut(const QString &s)
void setButtonName(const QString &str)
QPushButton * _okButton
ExprFileDialog(QWidget *parent=0)
QString getSaveFileName(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
QComboBox * _combo
QLineEdit * _nameEdit
you may not use this file except in compliance with the License and the following modification to it
Definition: license.txt:10
QString getOpenFileName(const QString &caption=QString::null, const QString &startWith=QString::null, const QString &filter=QString::null)
ExprPreviewWidget(QWidget *parent)
QStringList _lookInList
QCheckBox * _cb