| 1 | /* ------------------------------------------------------------------------- | 
|---|
| 2 | * Generally useful widget | 
|---|
| 3 | * ------------------------------------------------------------------------- | 
|---|
| 4 | * | 
|---|
| 5 | * Class:      qtFileChooser | 
|---|
| 6 | * | 
|---|
| 7 | * Purpose:    Widget for selecting a file or directory | 
|---|
| 8 | * | 
|---|
| 9 | * Author:     L. Mervart | 
|---|
| 10 | * | 
|---|
| 11 | * Created:    10-May-2011 | 
|---|
| 12 | * | 
|---|
| 13 | * Changes: | 
|---|
| 14 | * | 
|---|
| 15 | * -----------------------------------------------------------------------*/ | 
|---|
| 16 |  | 
|---|
| 17 | #include "qtfilechooser.h" | 
|---|
| 18 |  | 
|---|
| 19 | // Constructor | 
|---|
| 20 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 21 | qtFileChooser::qtFileChooser(QWidget* parent) : QWidget(parent), _mode(File) { | 
|---|
| 22 |  | 
|---|
| 23 | QHBoxLayout* layout = new QHBoxLayout( this ); | 
|---|
| 24 | layout->setMargin(0); | 
|---|
| 25 |  | 
|---|
| 26 | _lineEdit = new QLineEdit(this); | 
|---|
| 27 | layout->addWidget(_lineEdit); | 
|---|
| 28 |  | 
|---|
| 29 | connect(_lineEdit, SIGNAL(textChanged(const QString &)), | 
|---|
| 30 | this, SIGNAL(fileNameChanged(const QString &))); | 
|---|
| 31 |  | 
|---|
| 32 | _button = new QPushButton("...", this); | 
|---|
| 33 | _button->setFixedWidth(_button->fontMetrics().width(" ... ")); | 
|---|
| 34 | layout->addWidget(_button); | 
|---|
| 35 |  | 
|---|
| 36 | connect(_button, SIGNAL(clicked()), this, SLOT(chooseFile())); | 
|---|
| 37 | setFocusProxy(_lineEdit); | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | // Destructor | 
|---|
| 41 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 42 | qtFileChooser::~qtFileChooser() { | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | // | 
|---|
| 46 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 47 | void qtFileChooser::setFileName(const QString& fileName) { | 
|---|
| 48 | _lineEdit->setText(fileName); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | // | 
|---|
| 52 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 53 | QString qtFileChooser::fileName() const { | 
|---|
| 54 | return _lineEdit->text(); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | // | 
|---|
| 58 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 59 | void qtFileChooser::chooseFile() { | 
|---|
| 60 | QString fileName; | 
|---|
| 61 | if (mode() == File) { | 
|---|
| 62 | fileName = QFileDialog::getOpenFileName(this); | 
|---|
| 63 | } | 
|---|
| 64 | else { | 
|---|
| 65 | fileName = QFileDialog::getExistingDirectory(this); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | if (!fileName.isEmpty()) { | 
|---|
| 69 | _lineEdit->setText(fileName); | 
|---|
| 70 | emit fileNameChanged(fileName); | 
|---|
| 71 | } | 
|---|
| 72 | } | 
|---|