File indexing completed on 2024-05-19 16:43:48
0001 /************************************************************************* 0002 * Copyright (C) 2009 by Renaud Guezennec * 0003 * * 0004 * https://rolisteam.org/ * 0005 * * 0006 * Rolisteam is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published * 0008 * by the Free Software Foundation; either version 2 of the License, * 0009 * or (at your option) any later version. * 0010 * * 0011 * This program is distributed in the hope that it will be useful, * 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0014 * GNU General Public License for more details. * 0015 * * 0016 * You should have received a copy of the GNU General Public License * 0017 * along with this program; if not, write to the * 0018 * Free Software Foundation, Inc., * 0019 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0020 *************************************************************************/ 0021 #include "filedirchooser.h" 0022 0023 #include <QDir> 0024 #include <QFileDialog> 0025 #include <QHBoxLayout> 0026 0027 /************** 0028 * FileDirChooser * 0029 **************/ 0030 0031 FileDirChooser::FileDirChooser(bool isDirectory, QWidget* parent) : QWidget(parent), m_directory(isDirectory) 0032 { 0033 // Childrens 0034 m_lineEdit= new QLineEdit(this); 0035 m_lineEdit->setText(QDir::homePath()); 0036 QPushButton* button= new QPushButton(QStringLiteral("..."), this); 0037 0038 // Layout 0039 QHBoxLayout* layout= new QHBoxLayout; 0040 layout->addWidget(m_lineEdit, 1); 0041 layout->addWidget(button, 0); 0042 setLayout(layout); 0043 0044 // Connections 0045 connect(button, &QPushButton::clicked, this, &FileDirChooser::browse); 0046 connect(m_lineEdit, &QLineEdit::editingFinished, this, [this]() { emit pathChanged(path()); }); 0047 0048 // Misc 0049 button->setMaximumWidth(28); 0050 setContentsMargins(0, 0, 0, 0); 0051 layout->setContentsMargins(0, 0, 0, 0); 0052 } 0053 0054 FileDirChooser::~FileDirChooser() 0055 { 0056 // QObject should do it right for us already. 0057 } 0058 void FileDirChooser::setMode(bool isDirectory) 0059 { 0060 m_directory= isDirectory; 0061 } 0062 0063 void FileDirChooser::setPath(const QString& path) 0064 { 0065 m_lineEdit->setText(path); 0066 } 0067 0068 QString FileDirChooser::path() const 0069 { 0070 return m_lineEdit->text(); 0071 } 0072 void FileDirChooser::setFilter(const QString& filter) 0073 { 0074 m_filter= filter; 0075 } 0076 0077 QString FileDirChooser::getFilter() 0078 { 0079 return m_filter; 0080 } 0081 0082 void FileDirChooser::browse() 0083 { 0084 QString result; 0085 if(m_directory) 0086 { 0087 result= QFileDialog::getExistingDirectory(this, tr("Select directory"), m_lineEdit->text(), 0088 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); 0089 } 0090 else 0091 { 0092 result= QFileDialog::getOpenFileName(this, tr("Open File"), m_lineEdit->text(), m_filter); 0093 } 0094 if(!result.isEmpty()) 0095 { 0096 m_lineEdit->setText(result); 0097 emit pathChanged(path()); 0098 } 0099 }