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 "filerequester.h" 0014 #include "geticon.h" 0015 0016 #include <QStyle> 0017 #include <QLineEdit> 0018 #include <QToolButton> 0019 #include <QHBoxLayout> 0020 #include <QFileDialog> 0021 #include <QFileSystemModel> 0022 #include <QCompleter> 0023 0024 #include <QDebug> 0025 0026 namespace Kst { 0027 0028 FileRequester::FileRequester(QWidget *parent) 0029 : QWidget(parent), _mode(QFileDialog::AnyFile) { 0030 setup(); 0031 } 0032 0033 0034 FileRequester::~FileRequester() { 0035 } 0036 0037 0038 void FileRequester::setup() { 0039 _fileEdit = new QLineEdit(this); 0040 _fileButton = new QToolButton(this); 0041 0042 QHBoxLayout * layout = new QHBoxLayout(this); 0043 layout->setMargin(0); 0044 layout->addWidget(_fileEdit); 0045 layout->addWidget(_fileButton); 0046 setLayout(layout); 0047 0048 int size = style()->pixelMetric(QStyle::PM_SmallIconSize); 0049 _fileButton->setIcon(KstGetIcon("kst_changefile")); 0050 _fileButton->setFixedSize(size + 8, size + 8); 0051 0052 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); 0053 connect (_fileEdit, SIGNAL(textChanged(QString)), this, SLOT(updateFile(QString))); 0054 connect (_fileButton, SIGNAL(clicked()), this, SLOT(chooseFile())); 0055 0056 QFileSystemModel *dirModel = new QFileSystemModel(this); 0057 dirModel->setFilter(QDir::AllEntries); 0058 dirModel->setRootPath(QString('/')); 0059 0060 QCompleter *completer = new QCompleter(this); 0061 completer->setModel(dirModel); 0062 0063 _fileEdit->setCompleter(completer); 0064 } 0065 0066 0067 QString FileRequester::file() const { 0068 return _file; 0069 } 0070 0071 0072 void FileRequester::setFile(const QString &file) { 0073 _file = file; 0074 //FIXME grrr QLineEdit doc *lies* to me... the textEdited signal is being triggered!! 0075 _fileEdit->blockSignals(true); 0076 _fileEdit->setText(_file); 0077 _fileEdit->blockSignals(false); 0078 emit changed(file); 0079 } 0080 0081 void FileRequester::updateFile(const QString &file) { 0082 if (file.contains('~')) { 0083 QString home = qgetenv("HOME"); // linux 0084 if (!home.isEmpty()) { 0085 QString changed_file = file; 0086 changed_file.replace('~', home); 0087 setFile(changed_file); 0088 return; 0089 } 0090 home = qgetenv("USERPROFILE"); // windows, maybe (?) 0091 if (!home.isEmpty()) { 0092 QString changed_file = file; 0093 changed_file.replace('~', home); 0094 setFile(changed_file); 0095 return; 0096 } 0097 } 0098 0099 _file = file; 0100 emit changed(file); 0101 } 0102 0103 0104 void FileRequester::chooseFile() { 0105 QString file; 0106 if (_mode == QFileDialog::ExistingFile) { 0107 file = QFileDialog::getOpenFileName(this, tr("Open File"), _file, tr("All Files (*)")); 0108 } else if (_mode == QFileDialog::Directory) { 0109 file = QFileDialog::getExistingDirectory(this, tr("Logfile Directory"), _file); 0110 } else { 0111 file = QFileDialog::getSaveFileName(this, tr("Save File"), _file, tr("All Files (*)")); 0112 } 0113 0114 if (!file.isEmpty()) { 0115 setFile(file); 0116 } 0117 } 0118 0119 } 0120 0121 // vim: ts=2 sw=2 et