00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "SeExprEdFileDialog.h"
00014
00015
00016 #include <QtGui/QToolButton>
00017 #include <QtGui/QPalette>
00018 #include <QtGui/QMenu>
00019 #include <QtCore/QTimer>
00020 #include <QtCore/QUrl>
00021 #include <iostream>
00022
00023 using std::max;
00024 using std::min;
00025
00026 static const char* folder_fav[]={
00027 "17 16 4 1",
00028 "# c #000000",
00029 ". c None",
00030 "a c #ffff98",
00031 "b c #cc0000",
00032 ".................",
00033 ".................",
00034 "...#####.........",
00035 "..#aaaaa#........",
00036 ".###############.",
00037 ".#aaaaaaaaaaaaa#.",
00038 ".#aaaa##a##aaaa#.",
00039 ".#aaa#bb#bb#aaa#.",
00040 ".#aaa#bbbbb#aaa#.",
00041 ".#aaa#bbbbb#aaa#.",
00042 ".#aaaa#bbb#aaaa#.",
00043 ".#aaaaa#b#aaaaa#.",
00044 ".#aaaaaa#aaaaaa#.",
00045 ".#aaaaaaaaaaaaa#.",
00046 ".###############.",
00047 "................."};
00048
00049
00050 void SeExprEdPreviewWidget::makePreview(const QString& path)
00051 {
00052 QFileInfo fi( path );
00053
00054 if ( fi.isDir() )
00055 {
00056 QString s = fi.absoluteFilePath()+"/preview.tif";
00057 if (!QFile::exists(s)) s = fi.absoluteFilePath()+"/preview.png";
00058 if (!QFile::exists(s)) _pm->setPixmap( QPixmap());
00059
00060 QPixmap pix( s );
00061 if ( !pix.isNull() ) _pm->setPixmap( pix );
00062 else _pm->setPixmap( QPixmap() );
00063 }
00064 else if (fi.exists())
00065 {
00066 QImage img(fi.absoluteFilePath());
00067 if(!img.isNull())
00068 _pm->setPixmap(QPixmap::fromImage(img.scaled(128,128,Qt::KeepAspectRatio,Qt::SmoothTransformation)));
00069 else
00070 _pm->setPixmap( QPixmap() );
00071 }
00072 else _pm->setPixmap( QPixmap() );
00073 _pm->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
00074 }
00075
00076
00077 SeExprEdPreviewWidget::SeExprEdPreviewWidget( QWidget *parent )
00078 : QWidget( parent )
00079 {
00080 _pm = new QLabel( this );
00081 _pm->setFrameStyle( QFrame::StyledPanel );
00082 _pm->setBackgroundRole( QPalette::Base );
00083 _pm->setAutoFillBackground(true);
00084 QVBoxLayout *layout = new QVBoxLayout;
00085 layout->setSpacing( 0 );
00086 layout->setMargin( 0 );
00087 layout->addWidget(_pm);
00088 setLayout(layout);
00089 }
00090
00091 SeExprEdFileDialog::SeExprEdFileDialog(QWidget* parent) : QFileDialog(parent)
00092 {
00093
00094
00095
00096 _nameEdit = 0;
00097 _okButton = 0;
00098
00099
00100 QList<QLineEdit*> lineedits = findChildren<QLineEdit*>(QRegExp());
00101 if (lineedits.size()) _nameEdit = (QLineEdit*)lineedits.at(0);
00102 if (_nameEdit) {
00103 _nameEdit->disconnect(SIGNAL(returnPressed()));
00104 connect(_nameEdit, SIGNAL(returnPressed()), SLOT(editReturnPress()));
00105 }
00106
00107
00108 QList<QPushButton*> myWidgets = findChildren<QPushButton*>(QRegExp());
00109 for (int w = 0; w < myWidgets.size(); w++)
00110 {
00111 QPushButton* item = (QPushButton*)myWidgets.at(w);
00112 if (item->text().contains("Open")) _okButton = item;
00113 }
00114 if (_okButton) connect(_okButton, SIGNAL(clicked()), SLOT(handleOk()));
00115
00116 connect(this, SIGNAL(currentChanged(const QString&)),
00117 this, SLOT(selChanged(const QString&)));
00118
00119
00120 _createDir = 0;
00121 _pw = 0;
00122 _favDir = "";
00123 _combo = 0;
00124 _combolabel = 0;
00125 _cb = 0;
00126 _temppath = "";
00127
00128 setMinimumWidth(680);
00129 resize(840,440);
00130 }
00131
00132 void SeExprEdFileDialog::handleOk()
00133 {
00134 if (fileMode() != QFileDialog::DirectoryOnly) return;
00135 QString entry = _nameEdit->text();
00136 if (entry == "") return;
00137
00138
00139 if (_createDir) {
00140 QDir d(directory());
00141 if (!d.exists(entry)) {
00142 if (d.mkdir(entry)) {
00143 _temppath = directory().absolutePath();
00144 setDirectory(_temppath+"/"+entry);
00145 _nameEdit->setText("");
00146 if (_okButton)
00147 _okButton->animateClick();
00148
00149 QTimer::singleShot( 200, this, SLOT(resetDir()) );
00150 }
00151 }
00152 }
00153 }
00154
00155 void SeExprEdFileDialog::editReturnPress()
00156 {
00157 if (!_nameEdit) return;
00158
00159 QString str = _nameEdit->text();
00160 if (str.contains('/'))
00161 {
00162 QDir d;
00163 if (d.cd(str))
00164 {
00165 setDirectory(str);
00166 _nameEdit->setText("");
00167 }
00168 else
00169 {
00170 int slashcount = str.count('/');
00171
00172 QString foundDir = "";
00173 for (int i=0; i<slashcount; i++)
00174 {
00175 QString section = str.section('/', 0, i);
00176 if (d.cd(section)) foundDir = section;
00177 }
00178 if (foundDir.length())
00179 {
00180 setDirectory(foundDir);
00181 QString remainder = str.right(str.length()-(foundDir.length()+1));
00182 _nameEdit->setText(remainder);
00183 }
00184
00185 if (d.cd(str)) setDirectory(str);
00186
00187 }
00188 }
00189 else if (fileMode()==QFileDialog::DirectoryOnly) handleOk();
00190 else accept();
00191 }
00192
00193 void SeExprEdFileDialog::addFavoritesButton(QString dirname, QString linkname,
00194 QString linkdir)
00195 {
00196 QGridLayout *layout = findChild<QGridLayout *>("gridLayout");
00197 if (!layout) return;
00198
00199 QDir d;
00200
00201 std::string favlocation = getenv("HOME");
00202 favlocation += "/paint3d/favorites/";
00203
00204 QString dirpath = QString::fromStdString(favlocation);
00205 if (!d.cd(dirpath)) d.mkpath(dirpath);
00206 dirpath += dirname;
00207 if (!d.cd(dirpath)) d.mkpath(dirpath);
00208
00209 if (!(linkdir.isEmpty() || linkname.isEmpty()))
00210 {
00211 if (!QFile::exists(dirpath+linkname))
00212 QFile::link(linkdir,dirpath+linkname);
00213 }
00214
00215 _favDir = dirpath;
00216
00217 static QPixmap folderFav( folder_fav );
00218 QToolButton* fav = new QToolButton(this);
00219 fav->setFixedSize(18,18);
00220 fav->setIcon(folderFav);
00221 fav->setToolTip("Favorites");
00222
00223 layout->addWidget(fav,0,3);
00224
00225 connect(fav, SIGNAL(clicked()), SLOT(gotoFavorites()));
00226 }
00227
00228 void SeExprEdFileDialog::gotoFavorites()
00229 {
00230 if (_favDir!="") setDirectory( _favDir );
00231 }
00232
00233 void SeExprEdFileDialog::addLookInEntries(QStringList paths)
00234 {
00235 if ( paths.isEmpty ()) return;
00236
00237 QStringList h = history();
00238 for (QStringList::Iterator it=paths.begin(); it!=paths.end(); ++it)
00239 {
00240 if (!h.contains(*it)) h.push_back(*it);
00241 }
00242 setHistory(h);
00243 }
00244
00245 void SeExprEdFileDialog::saveLookInEntries()
00246 {
00247 _lookInList = history();
00248 }
00249
00250 void SeExprEdFileDialog::restoreLookInEntries()
00251 {
00252 setHistory(_lookInList);
00253 }
00254
00255 static QStringList makeFiltersList( const QString &filter )
00256 {
00257 if ( filter.isEmpty() )
00258 return QStringList();
00259
00260 int i = filter.indexOf( ";;", 0 );
00261 QString sep( ";;" );
00262 if ( i == -1 ) {
00263 if ( filter.indexOf( "\n", 0 ) != -1 ) {
00264 sep = "\n";
00265 i = filter.indexOf( sep, 0 );
00266 }
00267 }
00268
00269 return filter.split( sep );
00270 }
00271
00272 QString SeExprEdFileDialog::getOpenFileName( const QString& caption,
00273 const QString& startWith, const QString& filter )
00274 {
00275 if ( !filter.isEmpty() )
00276 {
00277 QStringList filters = makeFiltersList( filter );
00278 setNameFilters( filters );
00279 }
00280
00281 if ( !startWith.isEmpty() ) setDirectory( startWith );
00282 if ( !caption.isNull() ) setWindowTitle( caption );
00283 setFileMode( QFileDialog::ExistingFile );
00284 setAcceptMode( QFileDialog::AcceptOpen );
00285 selectFile("");
00286
00287 QString result;
00288 if ( exec()==QDialog::Accepted )
00289 {
00290 result = selectedFiles().first();
00291 _workingDirectory = directory().absolutePath();
00292 }
00293 resetPreview();
00294
00295 return result;
00296 }
00297
00298 QStringList SeExprEdFileDialog::getOpenFileNames( const QString& caption,
00299 const QString& startWith, const QString& filter )
00300 {
00301 if ( !filter.isEmpty() )
00302 {
00303 QStringList filters = makeFiltersList( filter );
00304 setNameFilters( filters );
00305 }
00306
00307 if ( !startWith.isEmpty() ) setDirectory( startWith );
00308 if ( !caption.isNull() ) setWindowTitle( caption );
00309 setFileMode( QFileDialog::ExistingFiles );
00310 setAcceptMode( QFileDialog::AcceptOpen );
00311 selectFile("");
00312
00313 QString result;
00314 QStringList lst;
00315 if ( exec()==QDialog::Accepted )
00316 {
00317 lst = selectedFiles();
00318 _workingDirectory = directory().absolutePath();
00319 }
00320 resetPreview();
00321
00322 return lst;
00323 }
00324
00325 QString SeExprEdFileDialog::getExistingDirectory( const QString& caption,
00326 const QString& startWith, const QString& filter )
00327 {
00328 if ( !filter.isEmpty() )
00329 {
00330 QStringList filters = makeFiltersList( filter );
00331 setNameFilters( filters );
00332 }
00333
00334 if ( !startWith.isEmpty() ) setDirectory( startWith );
00335 if ( !caption.isNull() ) setWindowTitle( caption );
00336 setFileMode( QFileDialog::DirectoryOnly );
00337 selectFile("");
00338
00339 QString result;
00340 if ( exec()==QDialog::Accepted )
00341 {
00342 result = selectedFiles().first();
00343 _workingDirectory = directory().absolutePath();
00344 }
00345 resetPreview();
00346
00347 return result;
00348 }
00349
00350 QString SeExprEdFileDialog::getExistingOrNewDirectory( const QString& caption,
00351 const QString& startWith, const QString& filter )
00352 {
00353 _createDir = 1;
00354 QString result = getExistingDirectory(caption, startWith, filter);
00355 _createDir = 0;
00356 resetPreview();
00357 return result;
00358 }
00359
00360 QString SeExprEdFileDialog::getSaveFileName( const QString& caption,
00361 const QString& startWith, const QString& filter )
00362 {
00363 if ( !filter.isEmpty() )
00364 {
00365 QStringList filters = makeFiltersList( filter );
00366 setNameFilters( filters );
00367 }
00368
00369 if ( !startWith.isEmpty() ) setDirectory( startWith );
00370 if ( !caption.isNull() ) setWindowTitle( caption );
00371 setFileMode( QFileDialog::AnyFile );
00372 setAcceptMode( QFileDialog::AcceptSave );
00373 selectFile("");
00374
00375 QString result;
00376 if ( exec()==QDialog::Accepted )
00377 {
00378 result = selectedFiles().first();
00379 _workingDirectory = directory().absolutePath();
00380 }
00381 resetPreview();
00382
00383 return result;
00384 }
00385
00386
00387 void SeExprEdFileDialog::setPreview()
00388 {
00389 QGridLayout *layout = findChild<QGridLayout *>("gridLayout");
00390 if (!layout) return;
00391
00392 _pw = new SeExprEdPreviewWidget( this );
00393 _pw->setFixedWidth(160);
00394 _pw->setMinimumHeight(160);
00395 layout->addWidget(_pw,1,3);
00396 }
00397
00398 void SeExprEdFileDialog::resetPreview()
00399 {
00400 if (_pw) _pw->reset();
00401 }
00402
00403 void SeExprEdFileDialog::addCheckBox(QString s)
00404 {
00405 QGridLayout *layout = findChild<QGridLayout *>("gridLayout");
00406 if (!layout) return;
00407
00408 _cb = new QCheckBox( s, this );
00409 _cb->setChecked(false);
00410
00411 layout->addWidget(_cb, 4, _combo ? 2 : 0);
00412 }
00413
00414 bool SeExprEdFileDialog::checkBoxStatus()
00415 {
00416 if (!_cb) return false;
00417 return _cb->isChecked();
00418 }
00419
00420 void SeExprEdFileDialog::showCheckBox()
00421 {
00422 if (_cb) _cb->show();
00423 }
00424
00425 void SeExprEdFileDialog::hideCheckBox()
00426 {
00427 if (_cb) _cb->hide();
00428 }
00429
00430 void SeExprEdFileDialog::addComboBox(QString s, QStringList sl)
00431 {
00432 QGridLayout *layout = findChild<QGridLayout *>("gridLayout");
00433 if (!layout) return;
00434
00435 _combolabel = new QLabel( s, this );
00436 _combolabel->setFixedWidth(58);
00437 _combo = new QComboBox( this );
00438 _combo->setEditable(true);
00439 _combo->setFixedWidth(160);
00440 for ( QStringList::Iterator it = sl.begin(); it != sl.end(); ++it )
00441 _combo->addItem(*it);
00442
00443 int rownum = layout->rowCount();
00444 layout->addWidget(_combo,rownum,1);
00445 layout->addWidget(_combolabel,rownum,0);
00446 }
00447
00448 void SeExprEdFileDialog::showComboBox()
00449 {
00450 if (_combo) _combo->show();
00451 if (_combolabel) _combolabel->show();
00452 }
00453
00454 void SeExprEdFileDialog::hideComboBox()
00455 {
00456 if (_combo) _combo->hide();
00457 if (_combolabel) _combolabel->hide();
00458 }
00459
00460 void SeExprEdFileDialog::selChanged(const QString& path)
00461 {
00462 if (_pw) _pw->makePreview(path);
00463 }
00464
00465 void SeExprEdFileDialog::setButtonName(const QString& str)
00466 {
00467 if (_okButton) _okButton->setText( str );
00468 }
00469
00470 void SeExprEdFileDialog::addSidebarShortcut(const QString& s)
00471 {
00472 QList<QUrl> urls = sidebarUrls();
00473 QUrl url = QUrl::fromLocalFile(s);
00474 if (url.isValid() && QFile::exists(s))
00475 {
00476 urls.push_back(url);
00477 setSidebarUrls(urls);
00478 }
00479 }