File indexing completed on 2024-12-22 04:18:18

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "datasourceselector.h"
0014 
0015 #include "datasourceselectordialog.h"
0016 #include "geticon.h"
0017 
0018 #include <QStyle>
0019 #include <QLineEdit>
0020 #include <QToolButton>
0021 #include <QHBoxLayout>
0022 #include <QFileDialog>
0023 #include <QFileSystemModel>
0024 #include <QCompleter>
0025 #include <QPointer>
0026 
0027 #include <QDebug>
0028 #include <QLabel>
0029 
0030 namespace Kst {
0031 
0032 DataSourceSelector::DataSourceSelector(QWidget *parent)
0033   : QWidget(parent), _mode(QFileDialog::ExistingFile) {
0034   setup();
0035 }
0036 
0037 
0038 DataSourceSelector::~DataSourceSelector() {
0039 }
0040 
0041 
0042 void DataSourceSelector::setup() {
0043 
0044   _fileEdit = new QLineEdit(this);
0045   _fileButton = new QToolButton(this);
0046 
0047   int h = fontMetrics().lineSpacing()*4/3;
0048 
0049   _fileEdit->setFixedHeight(h);
0050 
0051 
0052   QHBoxLayout * layout = new QHBoxLayout(this);
0053   layout->setMargin(0);
0054   layout->addWidget(_fileEdit);
0055   layout->addWidget(_fileButton);
0056 
0057   _fileButton->setFixedSize(h,h);
0058   setLayout(layout);
0059 
0060   //int size = style()->pixelMetric(QStyle::PM_SmallIconSize);
0061   _fileButton->setFixedSize(h,h);
0062   _fileButton->setIcon(KstGetIcon("kst_changefile"));
0063   //qDebug() << "file button small icon size" << size;
0064 
0065   setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
0066   //connect (_fileEdit, SIGNAL(textChanged(const QString &)), this, SIGNAL(changed(const QString &)));
0067   connect (_fileEdit, SIGNAL(textChanged(QString)), this, SLOT(updateFile(QString)));
0068   connect (_fileButton, SIGNAL(clicked()), this, SLOT(chooseFile()));
0069 
0070   QFileSystemModel *dirModel = new QFileSystemModel;
0071   dirModel->setFilter(QDir::AllEntries);
0072   dirModel->setRootPath(QString('/'));
0073 
0074   QCompleter *completer = new QCompleter(this);
0075   completer->setModel(dirModel); 
0076 
0077   _fileEdit->setCompleter(completer);
0078   setFixedHeight(h);
0079 }
0080 
0081 
0082 QString DataSourceSelector::file() const {
0083   return QDir::cleanPath(_file);
0084 }
0085 
0086 
0087 void DataSourceSelector::setFile(const QString &file) {
0088   _file = file;
0089   //FIXME grrr QLineEdit doc *lies* to me... the textEdited signal is being triggered!!
0090   _fileEdit->blockSignals(true);
0091   _fileEdit->setText(_file);
0092   _fileEdit->blockSignals(false);
0093   emit changed(file);
0094 }
0095 
0096 void DataSourceSelector::updateFile(const QString &file) {
0097   if (file.contains('~')) {
0098     QString home = qgetenv("HOME"); // linux
0099     if (!home.isEmpty()) {
0100       QString changed_file = file;
0101       changed_file.replace('~', home);
0102       setFile(changed_file);
0103     }
0104     home = qgetenv("USERPROFILE"); // windows, maybe (?)
0105     if (!home.isEmpty()) {
0106       QString changed_file = file;
0107       changed_file.replace('~', home);
0108       setFile(changed_file);
0109     }
0110   } else {
0111     _file = file;
0112     emit changed(file);
0113   }
0114 }
0115 
0116 
0117 
0118 void DataSourceSelector::chooseFile() {
0119   QString file;
0120   QPointer<DataSourceSelectorDialog> dialog = new DataSourceSelectorDialog( _file, this );
0121 
0122   if (dialog->exec() == QDialog::Accepted) {
0123     file = dialog->selectedDataSource();
0124 
0125     if (!file.isEmpty()) {
0126       setFile(file);
0127     }
0128   }
0129 
0130   delete dialog;
0131 }
0132 
0133 }
0134 
0135 // vim: ts=2 sw=2 et